blob: e3728451b9fa6fe5de8777d846173b5ae72f2785 [file] [log] [blame]
Andrew Jeffery1b868442023-04-19 13:16:32 +09301#include <stdbool.h>
2
3#include "console-server.h"
Jeremy Kerrc9775ce2017-02-07 16:25:34 +08004
5struct rb_test_ctx {
Andrew Jefferya72711a2023-04-18 18:19:41 +09306 struct ringbuffer_consumer *rbc;
7 bool ignore_poll;
8 bool force_only;
9 int count;
10 uint8_t *data;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093011 size_t len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080012};
13
14void ringbuffer_test_context_init(struct rb_test_ctx *ctx)
15{
16 ctx->count = 0;
17 ctx->data = NULL;
18 ctx->len = 0;
19 ctx->ignore_poll = false;
20 ctx->force_only = false;
21}
22
23void ringbuffer_test_context_fini(struct rb_test_ctx *ctx)
24{
25 free(ctx->data);
26}
27
Andrew Jefferya72711a2023-04-18 18:19:41 +093028enum ringbuffer_poll_ret ringbuffer_poll_nop(void *data __attribute__((unused)),
29 size_t force_len
30 __attribute__((unused)))
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080031{
32 return RINGBUFFER_POLL_OK;
33}
34
35enum ringbuffer_poll_ret ringbuffer_poll_append_all(void *data,
Andrew Jefferya72711a2023-04-18 18:19:41 +093036 size_t force_len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080037{
38 struct rb_test_ctx *ctx = data;
Andrew Jefferyb70f8712023-04-19 12:53:34 +093039 size_t len;
40 size_t total_len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080041 uint8_t *buf;
42
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093043 if (ctx->ignore_poll) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080044 return RINGBUFFER_POLL_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093045 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080046
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093047 if (ctx->force_only && !force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080048 return RINGBUFFER_POLL_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093049 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080050
51 ctx->count++;
52
53 total_len = 0;
54 for (;;) {
55 len = ringbuffer_dequeue_peek(ctx->rbc, total_len, &buf);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093056 if (!len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080057 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093058 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080059
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093060 if (ctx->force_only && total_len + len > force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080061 len = force_len - total_len;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093062 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080063
64 ctx->data = realloc(ctx->data, ctx->len + len);
65 memcpy(ctx->data + ctx->len, buf, len);
66 ctx->len += len;
67 total_len += len;
68
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093069 if (ctx->force_only && total_len >= force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080070 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093071 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080072 }
73 ringbuffer_dequeue_commit(ctx->rbc, total_len);
74
75 return RINGBUFFER_POLL_OK;
76}
77
78void ringbuffer_dump(struct ringbuffer *rb)
79{
80 struct ringbuffer_consumer *rbc;
Andrew Jeffery8f548f62023-04-18 11:48:49 +093081 size_t i;
82 int j;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080083
84 printf("---- ringbuffer (%d consumer%s)\n", rb->n_consumers,
Andrew Jefferya72711a2023-04-18 18:19:41 +093085 rb->n_consumers == 1 ? "" : "s");
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080086
87 for (i = 0; i < rb->size; i++) {
88 bool has_consumer = false;
89 const char *prefix = "";
90
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093091 if (rb->tail == i) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080092 prefix = "tail=>";
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093093 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080094
95 printf("%6s %02x", prefix, rb->buf[i]);
96 for (j = 0; j < rb->n_consumers; j++) {
97 rbc = rb->consumers[j];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093098 if (rbc->pos != i) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080099 continue;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930100 }
101 if (!has_consumer) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800102 printf(" <=");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930103 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800104 printf("c[%d],len=%zd ", j, ringbuffer_len(rbc));
105 has_consumer = true;
106 }
107 printf("\n");
108 }
109}