blob: 0a2c4fff3d19a6153f0778a03cd4ac83b114d3b0 [file] [log] [blame]
Andrew Jeffery1b868442023-04-19 13:16:32 +09301#include <stdbool.h>
Andrew Jeffery4dfefa92023-05-02 15:53:16 +09302#include <stdlib.h>
Andrew Jeffery1b868442023-04-19 13:16:32 +09303
4#include "console-server.h"
Jeremy Kerrc9775ce2017-02-07 16:25:34 +08005
6struct rb_test_ctx {
Andrew Jefferya72711a2023-04-18 18:19:41 +09307 struct ringbuffer_consumer *rbc;
8 bool ignore_poll;
9 bool force_only;
10 int count;
11 uint8_t *data;
Andrew Jeffery5c359cc2023-04-18 22:50:07 +093012 size_t len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080013};
14
15void ringbuffer_test_context_init(struct rb_test_ctx *ctx)
16{
17 ctx->count = 0;
18 ctx->data = NULL;
19 ctx->len = 0;
20 ctx->ignore_poll = false;
21 ctx->force_only = false;
22}
23
24void ringbuffer_test_context_fini(struct rb_test_ctx *ctx)
25{
26 free(ctx->data);
27}
28
Andrew Jefferya72711a2023-04-18 18:19:41 +093029enum ringbuffer_poll_ret ringbuffer_poll_nop(void *data __attribute__((unused)),
30 size_t force_len
31 __attribute__((unused)))
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080032{
33 return RINGBUFFER_POLL_OK;
34}
35
36enum ringbuffer_poll_ret ringbuffer_poll_append_all(void *data,
Andrew Jefferya72711a2023-04-18 18:19:41 +093037 size_t force_len)
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080038{
39 struct rb_test_ctx *ctx = data;
Andrew Jefferyb70f8712023-04-19 12:53:34 +093040 size_t len;
41 size_t total_len;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080042 uint8_t *buf;
43
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093044 if (ctx->ignore_poll) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080045 return RINGBUFFER_POLL_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093046 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080047
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093048 if (ctx->force_only && !force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080049 return RINGBUFFER_POLL_OK;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093050 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080051
52 ctx->count++;
53
54 total_len = 0;
55 for (;;) {
56 len = ringbuffer_dequeue_peek(ctx->rbc, total_len, &buf);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093057 if (!len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080058 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093059 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080060
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093061 if (ctx->force_only && total_len + len > force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080062 len = force_len - total_len;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093063 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080064
65 ctx->data = realloc(ctx->data, ctx->len + len);
66 memcpy(ctx->data + ctx->len, buf, len);
67 ctx->len += len;
68 total_len += len;
69
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093070 if (ctx->force_only && total_len >= force_len) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080071 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093072 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080073 }
74 ringbuffer_dequeue_commit(ctx->rbc, total_len);
75
76 return RINGBUFFER_POLL_OK;
77}
78
79void ringbuffer_dump(struct ringbuffer *rb)
80{
81 struct ringbuffer_consumer *rbc;
Andrew Jeffery8f548f62023-04-18 11:48:49 +093082 size_t i;
83 int j;
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080084
85 printf("---- ringbuffer (%d consumer%s)\n", rb->n_consumers,
Andrew Jefferya72711a2023-04-18 18:19:41 +093086 rb->n_consumers == 1 ? "" : "s");
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080087
88 for (i = 0; i < rb->size; i++) {
89 bool has_consumer = false;
90 const char *prefix = "";
91
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093092 if (rb->tail == i) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080093 prefix = "tail=>";
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093094 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +080095
96 printf("%6s %02x", prefix, rb->buf[i]);
97 for (j = 0; j < rb->n_consumers; j++) {
98 rbc = rb->consumers[j];
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093099 if (rbc->pos != i) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800100 continue;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930101 }
102 if (!has_consumer) {
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800103 printf(" <=");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930104 }
Jeremy Kerrc9775ce2017-02-07 16:25:34 +0800105 printf("c[%d],len=%zd ", j, ringbuffer_len(rbc));
106 has_consumer = true;
107 }
108 printf("\n");
109 }
110}