/** * UART RX Interrupt */ static void uart_recv_next_char(uint8_t num) { #ifdef CONFIG_MODULE_UART_9BITS if (uart_getconf_nbits() == 9) { int elt = 0; elt = uart_get_udr_9bits(num); if (CIRBUF_GET_FREELEN(&g_rx_fifo[num]) >= 2) { cirbuf_add_buf_head(&g_rx_fifo[num], (char *)&elt, 2); } if (rx_event[num]) ((event_9bits *)rx_event[num])(elt); } else #endif /* CONFIG_MODULE_UART_9BITS */ { char elt = 0; elt = uart_get_udr(num); if (!CIRBUF_IS_FULL(&g_rx_fifo[num])) { cirbuf_add_head(&g_rx_fifo[num], elt); } if (rx_event[num]) rx_event[num](elt); } }
/* try to read/delete less than written */ static int test_cirbuf_string_get_del_partial(void) { struct cirbuf cb; char buf[CMDLINE_TEST_BUFSIZE]; char tmp[CMDLINE_TEST_BUFSIZE]; char tmp2[CMDLINE_TEST_BUFSIZE]; /* initialize buffers */ memset(buf, 0, sizeof(buf)); memset(tmp, 0, sizeof(tmp)); memset(tmp2, 0, sizeof(tmp)); strlcpy(tmp2, CIRBUF_STR_HEAD, sizeof(tmp2)); /* * initialize circular buffer */ if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to initialize circular buffer!\n"); return -1; } /* add string to head */ if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != (sizeof(CIRBUF_STR_HEAD))) { printf("Error: failed to add string to head!\n"); return -1; } /* read less than written (head) */ if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1) != sizeof(CIRBUF_STR_HEAD) - 1) { printf("Error: unexpected result when reading from head!\n"); return -1; } /* verify string */ if (strncmp(tmp, tmp2, sizeof(CIRBUF_STR_HEAD) - 1) != 0) { printf("Error: strings mismatch!\n"); return -1; } memset(tmp, 0, sizeof(tmp)); /* read less than written (tail) */ if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1) != sizeof(CIRBUF_STR_HEAD) - 1) { printf("Error: unexpected result when reading from tail!\n"); return -1; } /* verify string */ if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) { printf("Error: strings mismatch!\n"); return -1; } /* * verify correct deletion */ /* clear buffer */ memset(tmp, 0, sizeof(tmp)); /* delete less than written (head) */ if (cirbuf_del_buf_head(&cb, 1) != 0) { printf("Error: delete from head failed!\n"); return -1; } /* read from head */ if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 1) != sizeof(CIRBUF_STR_HEAD) - 1) { printf("Error: unexpected result when reading from head!\n"); return -1; } /* since we deleted from head, first char should be deleted */ if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 1) != 0) { printf("Error: strings mismatch!\n"); return -1; } /* clear buffer */ memset(tmp, 0, sizeof(tmp)); /* delete less than written (tail) */ if (cirbuf_del_buf_tail(&cb, 1) != 0) { printf("Error: delete from tail failed!\n"); return -1; } /* read from tail */ if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) - 2) != sizeof(CIRBUF_STR_HEAD) - 2) { printf("Error: unexpected result when reading from head!\n"); return -1; } /* since we deleted from tail, last char should be deleted */ if (strncmp(tmp, &tmp2[1], sizeof(CIRBUF_STR_HEAD) - 2) != 0) { printf("Error: strings mismatch!\n"); return -1; } return 0; }
/* test right alignment */ static int test_cirbuf_align_right(void) { #define END_OFFSET CMDLINE_TEST_BUFSIZE - 1 struct cirbuf cb; char buf[CMDLINE_TEST_BUFSIZE]; char tmp[CMDLINE_TEST_BUFSIZE]; unsigned i; /* * align right when start < end and start in left half */ /* * initialize circular buffer */ memset(buf, 0, sizeof(buf)); if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to initialize circular buffer!\n"); return -1; } /* push end into left half */ for (i = 0; i < HALF_OFFSET - 1; i++) cirbuf_add_tail_safe(&cb, 't'); /* push start into left half < end */ for (i = 0; i < SMALL_OFFSET; i++) cirbuf_del_head_safe(&cb); /* align */ cirbuf_align_right(&cb); /* verify result */ if (cb.start != END_OFFSET || cb.len != LEN1 || cb.end != cb.len - 2) { printf("Error: buffer alignment is wrong!\n"); return -1; } /* * align right when start > end and start in left half */ /* * reinitialize circular buffer */ memset(buf, 0, sizeof(buf)); if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to reinitialize circular buffer!\n"); return -1; } /* push start into left half */ for (i = 0; i < HALF_OFFSET + 2; i++) cirbuf_add_head_safe(&cb, 'h'); /* push end into left half > start */ for (i = 0; i < SMALL_OFFSET; i++) cirbuf_add_tail_safe(&cb, 't'); /* align */ cirbuf_align_right(&cb); /* verify result */ if (cb.start != END_OFFSET || cb.len != LEN2 || cb.end != cb.len - 2) { printf("Error: buffer alignment is wrong!"); return -1; } /* * align right when start < end and start in right half */ /* * reinitialize circular buffer */ memset(buf, 0, sizeof(buf)); if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to reinitialize circular buffer!\n"); return -1; } /* push start into the right half */ for (i = 0; i < HALF_OFFSET; i++) cirbuf_add_head_safe(&cb, 'h'); /* push end into left half > start */ for (i = 0; i < SMALL_OFFSET; i++) cirbuf_del_tail_safe(&cb); /* align */ cirbuf_align_right(&cb); /* verify result */ if (cb.end != END_OFFSET || cb.len != LEN3 || cb.start != cb.end - cb.len + 1) { printf("Error: buffer alignment is wrong!"); return -1; } /* * align right when start > end and start in right half */ /* * reinitialize circular buffer */ memset(buf, 0, sizeof(buf)); if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to reinitialize circular buffer!\n"); return -1; } /* push start into the right half */ for (i = 0; i < HALF_OFFSET - 1; i++) cirbuf_add_head_safe(&cb, 'h'); /* push end into left half < start */ for (i = 0; i < SMALL_OFFSET; i++) cirbuf_add_tail_safe(&cb, 't'); /* align */ cirbuf_align_right(&cb); /* verify result */ if (cb.end != END_OFFSET || cb.len != LEN4 || cb.start != cb.end - cb.len + 1) { printf("Error: buffer alignment is wrong!"); return -1; } /* * Verify that alignment doesn't corrupt data */ /* * reinitialize circular buffer */ memset(buf, 0, sizeof(buf)); if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to reinitialize circular buffer!\n"); return -1; } /* add string to tail and head */ if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) < 0 || cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) < 0) { printf("Error: failed to add strings!\n"); return -1; } /* align */ if (cirbuf_align_right(&cb) < 0) { printf("Error: alignment failed!\n"); return -1; } /* get string from head */ if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) { printf("Error: failed to read string from head!\n"); return -1; } /* verify string */ if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) { printf("Error: strings mismatch!\n"); return -1; } /* reset tmp buffer */ memset(tmp, 0, sizeof(tmp)); /* get string from tail */ if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) < 0) { printf("Error: failed to read string from head!\n"); return -1; } /* verify string */ if (strncmp(tmp, CIRBUF_STR_HEAD "\0" CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_HEAD) + sizeof(CIRBUF_STR_TAIL)) != 0) { printf("Error: strings mismatch!\n"); return -1; } return 0; }
/* try to read/delete more than written */ static int test_cirbuf_string_get_del_boundaries(void) { struct cirbuf cb; char buf[CMDLINE_TEST_BUFSIZE]; char tmp[CMDLINE_TEST_BUFSIZE]; /* initialize buffers */ memset(buf, 0, sizeof(buf)); memset(tmp, 0, sizeof(tmp)); /* * initialize circular buffer */ if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to initialize circular buffer!\n"); return -1; } /* add string to head */ if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != (sizeof(CIRBUF_STR_HEAD))) { printf("Error: failed to add string to head!\n"); return -1; } /* read more than written (head) */ if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1) != sizeof(CIRBUF_STR_HEAD)) { printf("Error: unexpected result when reading too much data!\n"); return -1; } /* read more than written (tail) */ if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_HEAD) + 1) != sizeof(CIRBUF_STR_HEAD)) { printf("Error: unexpected result when reading too much data!\n"); return -1; } /* delete more than written (head) */ if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) { printf("Error: unexpected result when deleting too much data!\n"); return -1; } /* delete more than written (tail) */ if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD) + 1) == 0) { printf("Error: unexpected result when deleting too much data!\n"); return -1; } /* * reinitialize circular buffer */ memset(buf, 0, sizeof(buf)); if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to reinitialize circular buffer!\n"); return -1; } /* add string to tail */ if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != (sizeof(CIRBUF_STR_TAIL))) { printf("Error: failed to add string to tail!\n"); return -1; } /* read more than written (tail) */ if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1) != sizeof(CIRBUF_STR_TAIL)) { printf("Error: unexpected result when reading too much data!\n"); return -1; } /* read more than written (head) */ if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_TAIL) + 1) != sizeof(CIRBUF_STR_TAIL)) { printf("Error: unexpected result when reading too much data!\n"); return -1; } /* delete more than written (tail) */ if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) { printf("Error: unexpected result when deleting too much data!\n"); return -1; } /* delete more than written (head) */ if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_TAIL) + 1) == 0) { printf("Error: unexpected result when deleting too much data!\n"); return -1; } return 0; }
/* try to write more than available */ static int test_cirbuf_string_add_boundaries(void) { struct cirbuf cb; char buf[CMDLINE_TEST_BUFSIZE]; unsigned i; /* initialize buffers */ memset(buf, 0, sizeof(buf)); /* * initialize circular buffer */ if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to initialize circular buffer!\n"); return -1; } /* fill the buffer from tail */ for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_TAIL) + 1; i++) cirbuf_add_tail_safe(&cb, 't'); /* try adding a string to tail */ if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) > 0) { printf("Error: buffer should have been full!\n"); return -1; } /* try adding a string to head */ if (cirbuf_add_buf_head(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) > 0) { printf("Error: buffer should have been full!\n"); return -1; } /* * reinitialize circular buffer */ memset(buf, 0, sizeof(buf)); if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to reinitialize circular buffer!\n"); return -1; } /* fill the buffer from head */ for (i = 0; i < CMDLINE_TEST_BUFSIZE - sizeof(CIRBUF_STR_HEAD) + 1; i++) cirbuf_add_head_safe(&cb, 'h'); /* try adding a string to head */ if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) > 0) { printf("Error: buffer should have been full!\n"); return -1; } /* try adding a string to tail */ if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) > 0) { printf("Error: buffer should have been full!\n"); return -1; } return 0; }
/* test adding from head and deleting from tail, and vice versa */ static int test_cirbuf_string_add_del_reverse(void) { struct cirbuf cb; char buf[CMDLINE_TEST_BUFSIZE]; char tmp[CMDLINE_TEST_BUFSIZE]; /* initialize buffers */ memset(buf, 0, sizeof(buf)); memset(tmp, 0, sizeof(tmp)); /* * initialize circular buffer */ if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to initialize circular buffer!\n"); return -1; } /* add string to head */ if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != (sizeof(CIRBUF_STR_HEAD))) { printf("Error: failed to add string to head!\n"); return -1; } /* delete string from tail */ if (cirbuf_del_buf_tail(&cb, sizeof(CIRBUF_STR_HEAD)) < 0) { printf("Error: failed to delete string from tail!\n"); return -1; } /* verify string was deleted */ if (cirbuf_del_tail_safe(&cb) == 0) { printf("Error: buffer should have been empty!\n"); return -1; } /* clear tmp buffer */ memset(tmp, 0, sizeof(tmp)); /* * reinitialize circular buffer */ memset(buf, 0, sizeof(buf)); if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to reinitialize circular buffer!\n"); return -1; } /* add string to tail */ if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != (sizeof(CIRBUF_STR_TAIL))) { printf("Error: failed to add string to tail!\n"); return -1; } /* delete string from head */ if (cirbuf_del_buf_head(&cb, sizeof(CIRBUF_STR_TAIL)) < 0) { printf("Error: failed to delete string from head!\n"); return -1; } /* verify string was deleted */ if (cirbuf_del_head_safe(&cb) == 0) { printf("Error: buffer should have been empty!\n"); return -1; } return 0; }
/* miscellaneous tests - they make bullseye happy */ static int test_cirbuf_string_misc(void) { struct cirbuf cb; char buf[CMDLINE_TEST_BUFSIZE]; char tmp[CMDLINE_TEST_BUFSIZE]; /* initialize buffers */ memset(buf, 0, sizeof(buf)); memset(tmp, 0, sizeof(tmp)); /* * initialize circular buffer */ if (cirbuf_init(&cb, buf, 0, sizeof(buf)) < 0) { printf("Error: failed to initialize circular buffer!\n"); return -1; } /* * add strings to head and tail, but read only tail * this results in read operation that does not transcend * from buffer end to buffer beginning (in other words, * strlen <= cb->maxlen - cb->end) */ /* add string to head */ if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != (sizeof(CIRBUF_STR_HEAD))) { printf("Error: failed to add string to head!\n"); return -1; } /* add string to tail */ if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != (sizeof(CIRBUF_STR_TAIL))) { printf("Error: failed to add string to head!\n"); return -1; } /* read string from tail */ if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL)) != (sizeof(CIRBUF_STR_TAIL))) { printf("Error: failed to get string from tail!\n"); return -1; } /* verify string */ if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) { printf("Error: tail strings do not match!\n"); return -1; } /* clear buffers */ memset(tmp, 0, sizeof(tmp)); memset(buf, 0, sizeof(buf)); /* * add a string to buffer when start/end is at end of buffer */ /* * reinitialize circular buffer with start at the end of cirbuf */ if (cirbuf_init(&cb, buf, CMDLINE_TEST_BUFSIZE - 2, sizeof(buf)) < 0) { printf("Error: failed to reinitialize circular buffer!\n"); return -1; } /* add string to tail */ if (cirbuf_add_buf_tail(&cb, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != (sizeof(CIRBUF_STR_TAIL))) { printf("Error: failed to add string to tail!\n"); return -1; } /* read string from tail */ if (cirbuf_get_buf_tail(&cb, tmp, sizeof(CIRBUF_STR_TAIL)) != (sizeof(CIRBUF_STR_TAIL))) { printf("Error: failed to get string from tail!\n"); return -1; } /* verify string */ if (strncmp(tmp, CIRBUF_STR_TAIL, sizeof(CIRBUF_STR_TAIL)) != 0) { printf("Error: tail strings do not match!\n"); return -1; } /* clear tmp buffer */ memset(tmp, 0, sizeof(tmp)); /* add string to head */ if (cirbuf_add_buf_head(&cb, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != (sizeof(CIRBUF_STR_HEAD))) { printf("Error: failed to add string to head!\n"); return -1; } /* read string from tail */ if (cirbuf_get_buf_head(&cb, tmp, sizeof(CIRBUF_STR_HEAD)) != (sizeof(CIRBUF_STR_HEAD))) { printf("Error: failed to get string from head!\n"); return -1; } /* verify string */ if (strncmp(tmp, CIRBUF_STR_HEAD, sizeof(CIRBUF_STR_HEAD)) != 0) { printf("Error: headstrings do not match!\n"); return -1; } return 0; }
/* call functions with invalid parameters */ int test_cirbuf_invalid_param(void) { struct cirbuf cb; char buf[CMDLINE_TEST_BUFSIZE]; /* null cirbuf */ if (cirbuf_init(0, buf, 0, sizeof(buf)) == 0) return -1; /* null buffer */ if (cirbuf_init(&cb, 0, 0, sizeof(buf)) == 0) return -1; /* null cirbuf */ if (cirbuf_add_head_safe(0, 'h') == 0) return -1; if (cirbuf_add_tail_safe(0, 't') == 0) return -1; if (cirbuf_del_head_safe(0) == 0) return -1; if (cirbuf_del_tail_safe(0) == 0) return -1; /* null buffer */ if (cirbuf_add_buf_head(&cb, 0, 0) == 0) return -1; if (cirbuf_add_buf_tail(&cb, 0, 0) == 0) return -1; /* null cirbuf */ if (cirbuf_add_buf_head(0, buf, 0) == 0) return -1; if (cirbuf_add_buf_tail(0, buf, 0) == 0) return -1; /* null size */ if (cirbuf_add_buf_head(&cb, buf, 0) == 0) return -1; if (cirbuf_add_buf_tail(&cb, buf, 0) == 0) return -1; /* null cirbuf */ if (cirbuf_del_buf_head(0, 0) == 0) return -1; if (cirbuf_del_buf_tail(0, 0) == 0) return -1; /* null size */ if (cirbuf_del_buf_head(&cb, 0) == 0) return -1; if (cirbuf_del_buf_tail(&cb, 0) == 0) return -1; /* null cirbuf */ if (cirbuf_get_buf_head(0, 0, 0) == 0) return -1; if (cirbuf_get_buf_tail(0, 0, 0) == 0) return -1; /* null buffer */ if (cirbuf_get_buf_head(&cb, 0, 0) == 0) return -1; if (cirbuf_get_buf_tail(&cb, 0, 0) == 0) return -1; /* null size, this is valid but should return 0 */ if (cirbuf_get_buf_head(&cb, buf, 0) != 0) return -1; if (cirbuf_get_buf_tail(&cb, buf, 0) != 0) return -1; /* null cirbuf */ if (cirbuf_align_left(0) == 0) return -1; if (cirbuf_align_right(0) == 0) return -1; return 0; }