/** * transmit next character of fifo if any, and call the event function. * This function is executed with intr locked. */ void uart_send_next_char(uint8_t num) { #ifdef CONFIG_MODULE_UART_9BITS if (uart_getconf_nbits(num) == 9) { int elt = 0; /* for 9 bits, it uses 2 places in the fifo */ if (CIRBUF_GET_LEN(&g_tx_fifo[num]) < 2) { cbi(*uart_regs[num].REGISTER_FOR_UART_IE, UDRIE); return; } cirbuf_get_buf_tail(&g_tx_fifo[num], (char *)&elt, 2); cirbuf_del_buf_tail(&g_tx_fifo[num], 2); uart_set_udr_9bits(num, elt); sbi(*uart_regs[num].REGISTER_FOR_UART_IE, UDRIE); } else /* 5, 6, 7 or 8 bits */ #endif /* CONFIG_MODULE_UART_9BITS */ { char elt = 0; if (CIRBUF_IS_EMPTY(&g_tx_fifo[num])) { cbi(*uart_regs[num].REGISTER_FOR_UART_IE, UDRIE); return; } elt = cirbuf_get_tail(&g_tx_fifo[num]); cirbuf_del_tail(&g_tx_fifo[num]); uart_set_udr(num, elt); sbi(*uart_regs[num].REGISTER_FOR_UART_IE, UDRIE); } }
/* 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 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; }
/* 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; }
/* 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; }