END_TEST START_TEST (test_to_rb_from_fd) { int fds[2]; ck_assert(pipe(fds) == 0); ck_assert(write(fds[1], sample16, 16) == 16); ck_assert(rb_is_empty(rb)); ck_assert_int_eq(rb_used(rb), 0); ck_assert_int_eq(rb_space(rb), 16); ssize_t result = rb_read(rb, fds[0], 10); ck_assert_int_eq(result, 10); ck_assert_int_eq(rb_used(rb), 10); ck_assert_int_eq(rb_space(rb), 6); ck_assert(!rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); result = rb_read(rb, fds[0], 10); ck_assert_int_eq(result, 6); ck_assert_int_eq(rb_used(rb), 16); ck_assert_int_eq(rb_space(rb), 0); ck_assert(!rb_is_empty(rb)); ck_assert(rb_is_full(rb)); ck_assert(memcmp(rb->buffer, "0123456789ABCDEF", 16) == 0); }
int main(int argc, char* argv[]) { unsigned int left = 0; unsigned int size = 0; unsigned int i = 0; struct ring_buffer rb; char buffer[100]; for(i = 0; i < 100; ++i) { buffer[i] = i; } rb_alloc(&rb, 100); for(i = 0; i < 100; ++i) { rb_write(&rb, buffer, 33); rb_read(&rb, buffer, 32); } printf("%d,%d %d,%d\n", rb_length(&rb), rb_left(&rb), rb_is_full(&rb), rb_is_empty(&rb)); rb_free(&rb); return 0; }
END_TEST START_TEST (test_to_scattered_memory_from_rb) { rb_append(rb, sample16, 4); rb_append(rb, sample16, 8); rb_discard(rb, 4); rb_append(rb, sample16+8, 8); char outbuf[32]; struct iovec iov[3]; iov[0].iov_base = outbuf+12; iov[0].iov_len = 2; iov[1].iov_base = outbuf+7; iov[1].iov_len = 5; iov[2].iov_base = outbuf; iov[2].iov_len = 7; size_t result = rb_extractv(rb, iov, 3); ck_assert_int_eq(result, 14); ck_assert_int_eq(rb_space(rb), 14); ck_assert(!rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); ck_assert(memcmp(outbuf, "789ABCD2345601", 14) == 0); }
END_TEST START_TEST (test_to_fd_from_rb) { int fds[2]; ck_assert(pipe(fds) == 0); rb_append(rb, sample16, 16); ck_assert(rb_is_full(rb)); ssize_t result = rb_write(rb, fds[1], 10); ck_assert_int_eq(result, 10); ck_assert_int_eq(rb_used(rb), 6); ck_assert_int_eq(rb_space(rb), 10); char outbuf[32]; ck_assert_int_eq(read(fds[0], outbuf, sizeof(outbuf)), 10); ck_assert(memcmp(outbuf, "0123456789", 10) == 0); result = rb_write(rb, fds[1], 10); ck_assert_int_eq(result, 6); ck_assert(rb_is_empty(rb)); ck_assert_int_eq(read(fds[0], outbuf, sizeof(outbuf)), 6); ck_assert(memcmp(outbuf, "ABCDEF", 6) == 0); }
/*! \brief Interrupt Callback function for read */ void at45_read_id_cb(spi_master_xact_data* caller, spi_master_xact_data* spi_xact, void* data) { // data is NULL in this cb. // if (data != NULL) {} uint16_t i = 0; at45_opcode opcode = 0; // copy out read buffer. for(i=0; i<spi_xact->read_numbytes; ++i) { caller->readbuf[i] = spi_xact->readbuf[i]; } // copy out write buffer. for(i=0; i<spi_xact->write_numbytes; ++i) { caller->writebuf[i] = spi_xact->writebuf[i]; } // The register address is always the first byte of the write buffer. opcode = caller->writebuf[0]; switch(opcode) { case AT45_ID_OPCODE: BLUE_LED_ON; at45_data_cache.at45_family_code.data = caller->readbuf[0]; at45_data_cache.at45_man_id.data = caller->readbuf[1]; at45_data_cache.at45_mlc_code.data = caller->readbuf[2]; break; default: break; } if(!rb_is_full(&at45_tx_done_q)) { rb_put_elem((char) opcode, &at45_tx_done_q); } // now check queue not empty in main loop to see if there is fresh data. }
int ringbuffer_full(RingBuffer *rb) { int ret; pthread_mutex_lock(&rb->lock); ret = rb_is_full(rb); pthread_mutex_unlock(&rb->lock); return ret; }
uint32_t usart_tx(usart_dev *dev, const uint8_t *buf, uint32_t len) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DATA(Data)); uint32_t tosend = len; uint32_t sent = 0; if (dev->usetxrb) { while (tosend) { if (rb_is_full(dev->txrb)) break; rb_insert(dev->txrb, *buf++); sent++; tosend--; } if (dev->txbusy == 0 && sent > 0) { dev->txbusy = 1; USART_ITConfig(dev->USARTx, USART_IT_TXE, ENABLE); } } else { #if 0 uint32_t rtime; if (dev->use_timeout) stopwatch_reset(); #endif while (tosend) { while (!(dev->USARTx->SR & USART_FLAG_TXE)) { #if 0 if (dev->use_timeout) { rtime = stopwatch_getus(); if (rtime >= dev->tx_timeout) { return sent; } } #endif } dev->USARTx->DR = *buf++; tosend--; sent++; } } return sent; }
END_TEST START_TEST (test_clear) { rb_append(rb, sample16, 16); ck_assert(rb_is_full(rb)); rb_clear(rb); ck_assert(rb_is_empty(rb)); }
void kipc_post_process(IPC* ipc, KPROCESS* sender) { KPROCESS* receiver = (KPROCESS*)ipc->process; int index = -1; bool wake = false; IPC* cur; CHECK_HANDLE(receiver, sizeof(KPROCESS)); CHECK_MAGIC(receiver, MAGIC_PROCESS); if (ipc->cmd & HAL_IO_FLAG) { KIO* kio = (KIO*)(((IO*)ipc->param2)->kio); CHECK_HANDLE(kio, sizeof(KIO)); CHECK_MAGIC(kio, MAGIC_KIO); if (!kio_send(kio, receiver)) return; } disable_interrupts(); if ((wake = ((receiver->kipc.wait_process == sender || receiver->kipc.wait_process == (KPROCESS*)ANY_HANDLE) && (receiver->kipc.cmd == ipc->cmd || receiver->kipc.cmd == ANY_CMD) && ((receiver->kipc.param1 == ipc->param1) || (receiver->kipc.param1 == ANY_HANDLE)))) == true) { receiver->kipc.wait_process = (KPROCESS*)INVALID_HANDLE; } if (!rb_is_full(&receiver->process->ipcs)) index = rb_put(&receiver->process->ipcs); enable_interrupts(); if (index >= 0) { cur = KIPC_ITEM(receiver, index); cur->cmd = ipc->cmd; cur->param1 = ipc->param1; cur->param2 = ipc->param2; cur->param3 = ipc->param3; cur->process = (HANDLE)sender; //already waiting? Wakeup him if (wake) kprocess_wakeup(receiver); } else { kprocess_error(sender, ERROR_OVERFLOW); #if (KERNEL_IPC_DEBUG) printk("Error: receiver %s IPC overflow!\n", kprocess_name(receiver)); if (sender == (KPROCESS*)KERNEL_HANDLE) printk("Sender: kernel\n"); else printk("Sender: %s\n", kprocess_name((KPROCESS*)sender)); printk("cmd: %#X, p1: %#X, p2: %#X, p3: %#X\n", ipc->cmd, ipc->param1, ipc->param2, ipc->param3); #if (KERNEL_DEVELOPER_MODE) HALT(); #endif #endif } }
void scsis_error_put(SCSIS* scsis, uint8_t key_sense, uint16_t ascq) { unsigned int idx; if (rb_is_full(&scsis->rb_error)) rb_get(&scsis->rb_error); idx = rb_put(&scsis->rb_error); scsis->errors[idx].key_sense = key_sense; scsis->errors[idx].ascq = ascq; #if (SCSI_DEBUG_ERRORS) printf("SCSI error: sense key: %02xh, ASC: %02xh, ASQ: %02xh\n", key_sense, ascq >> 8, ascq & 0xff); #endif //SCSI_DEBUG_ERRORS }
END_TEST START_TEST (test_to_memory_from_rb) { rb_append(rb, sample16, 16); char outbuf[32]; size_t result = rb_extract(rb, outbuf, 10); ck_assert_int_eq(result, 10); ck_assert_int_eq(rb_space(rb), 10); ck_assert(!rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); result = rb_extract(rb, outbuf + 10, 10); ck_assert_int_eq(result, 6); ck_assert(rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); ck_assert(memcmp(outbuf, "0123456789ABCDEF", 16) == 0); }
/* VCP_DataRx * Data received over USB OUT endpoint are sent over CDC interface * through this function. * * Note * This function will block any OUT packet reception on USB endpoint * untill exiting this function. If you exit this function before transfer * is complete on CDC interface (ie. using DMA controller) it will result * in receiving more data while previous ones are still not sent. * * @param Buf: Buffer of data to be received * @param Len: Number of data received (in bytes) * @retval Result of the opeartion: USBD_OK if all operations are OK else VCP_FAIL */ static U16 VCP_DataRx(U8 *buffer, U32 nbytes) { if (!rxfifo) return 0; U32 sent = 0; U32 tosend = nbytes; while(tosend) { if (rb_is_full(rxfifo)) return sent; rb_insert(rxfifo, *buffer++); sent++; tosend--; } return sent; }
END_TEST START_TEST (test_to_rb_from_memory_wrapped_around) { ck_assert_int_eq(rb_append(rb, sample16, 8), 8); ck_assert_int_eq(rb_discard(rb, 7), 7); ck_assert_int_eq(rb_used(rb), 1); size_t result = rb_append(rb, sample16, 16); ck_assert_int_eq(result, 15); ck_assert(!rb_is_empty(rb)); ck_assert(rb_is_full(rb)); ck_assert(memcmp(rb->buffer, "89ABCDE701234567", 16) == 0); }
END_TEST START_TEST (test_to_rb_from_memory_in_center) { ck_assert_int_eq(rb_append(rb, sample16, 8), 8); ck_assert_int_eq(rb_discard(rb, 7), 7); ck_assert_int_eq(rb_append(rb, sample16, 11), 11); ck_assert_int_eq(rb_used(rb), 12); ck_assert_int_eq(rb_space(rb), 4); size_t result = rb_append(rb, sample16, 16); ck_assert_int_eq(result, 4); ck_assert(!rb_is_empty(rb)); ck_assert(rb_is_full(rb)); ck_assert(memcmp(rb->buffer, "89A0123701234567", 16) == 0); }
END_TEST START_TEST (test_to_memory_from_rb_wrapped_around) { rb_append(rb, sample16, 16); rb_discard(rb, 10); rb_append(rb, sample16, 6); ck_assert_int_eq(rb_space(rb), 4); char outbuf[32]; size_t result = rb_extract(rb, outbuf, 16); ck_assert_int_eq(result, 12); ck_assert(rb_is_empty(rb)); ck_assert(!rb_is_full(rb)); ck_assert(memcmp(outbuf, "ABCDEF012345", 12) == 0); }
int ringbuffer_put(RingBuffer *rb, void *elem) { int ret = 0; pthread_mutex_lock(&rb->lock); if (!rb_is_full(rb)) { rb->used++; memcpy(rb_item_at(rb, rb->end), elem, rb->elem_size); rb->dump(rb->dump_ud, rb_item_at(rb, rb->end)); rb->end = (rb->end + 1) % rb->size; if (rb->waiters > 0) { pthread_cond_signal(&rb->cond); } } else { ret = -1; } pthread_mutex_unlock(&rb->lock); return ret; }
uint32_t usart_tx(const usart_dev *dev, const uint8_t *buf, uint32_t len) { /* Check the parameters */ assert_param(IS_USART_ALL_PERIPH(USARTx)); assert_param(IS_USART_DATA(Data)); uint32_t tosend = len; uint32_t sent = 0; while (tosend) { if (rb_is_full(dev->txrb)) break; rb_insert(dev->txrb, *buf++); sent++; tosend--; } if (dev->state->txbusy == 0 && sent > 0) { dev->state->txbusy = 1; dev->USARTx->CR1 |= USART_MASK_TXEIE; } return sent; }
END_TEST START_TEST (test_to_rb_from_scattered_memory) { ck_assert_int_eq(rb_append(rb, sample16, 8), 8); ck_assert_int_eq(rb_discard(rb, 7), 7); ck_assert_int_eq(rb_used(rb), 1); struct iovec iov[3]; iov[0].iov_base = sample16 + 4; iov[0].iov_len = 5; iov[1].iov_base = sample16; iov[1].iov_len = 4; iov[2].iov_base = sample16 + 9; iov[2].iov_len = 7; size_t result = rb_appendv(rb, iov, 3); ck_assert_int_eq(result, 15); ck_assert(!rb_is_empty(rb)); ck_assert(rb_is_full(rb)); ck_assert(memcmp(rb->buffer, "39ABCDE745678012", 16) == 0); }