int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg) { /* do basic initialization */ int res = uart_init_blocking(uart, baudrate); if(res < 0) { return res; } /* remember callback addresses */ uart_config[uart].rx_cb = rx_cb; uart_config[uart].arg = arg; /* enable receive and send interrupt */ switch(uart) { #if UART_0_EN case UART_0: NVIC_SetPriority(UART_0_IRQ_RX_CHAN, UART_0_IRQ_RX_PRIO); NVIC_EnableIRQ(UART_0_IRQ_RX_CHAN); USART_IntEnable(UART_0_DEV, _USART_IEN_RXDATAV_MASK); #if UART_0_ENABLE_BUF ringbuffer_init(&rb_uart0, buffer0, UART_0_BUFSIZE); NVIC_SetPriority(UART_0_IRQ_TX_CHAN, UART_0_IRQ_TX_PRIO); NVIC_EnableIRQ(UART_0_IRQ_TX_CHAN); USART_IntEnable(UART_0_DEV, _USART_IEN_TXBL_MASK); #endif break; #endif #if UART_1_EN case UART_1: NVIC_SetPriority(UART_1_IRQ_RX_CHAN, UART_1_IRQ_RX_PRIO); NVIC_SetPriority(UART_1_IRQ_TX_CHAN, UART_1_IRQ_TX_PRIO); NVIC_EnableIRQ(UART_1_IRQ_RX_CHAN); NVIC_EnableIRQ(UART_1_IRQ_TX_CHAN); USART_IntEnable(UART_1_DEV, _USART_IEN_RXDATAV_MASK); #if UART_1_ENABLE_BUF ringbuffer_init(&rb_uart1, buffer1, UART_1_BUFSIZE); NVIC_SetPriority(UART_1_IRQ_TX_CHAN, UART_1_IRQ_TX_PRIO); NVIC_EnableIRQ(UART_1_IRQ_TX_CHAN); USART_IntEnable(UART_1_DEV, _USART_IEN_TXBL_MASK); #endif break; #endif /* UART2 is considered as LEUART device which is functionable in low power mode */ #if UART_2_EN case UART_2: NVIC_SetPriority(UART_2_IRQ, UART_2_IRQ_PRIO); NVIC_EnableIRQ(UART_2_IRQ); LEUART_IntEnable(UART_2_DEV, LEUART_IEN_RXDATAV); #if UART_2_ENABLE_BUF ringbuffer_init(&rb_uart2, buffer2, UART_2_BUFSIZE); LEUART_IntEnable(UART_2_DEV, _LEUART_IEN_TXBL_MASK); #endif break; #endif default: /* Invalid usart device */ return -1; } return 0; }
inline static void usbdev_acm_reset(void) { ringbuffer_init(&cdcacm_tx_rb, tx_buffer, CDCACM_BUFSIZE); ringbuffer_init(&cdcacm_rx_rb, rx_buffer, CDCACM_BUFSIZE); /* TODO: reset data_send_zlp .... */ return; }
/** * @brief USBD_CDC_ClassInit * Initilaize the CDC interface * @param pdev: device instance * @param cfgidx: Configuration index * @retval status */ static uint8_t USBD_CDC_ClassInit (void* pdev, uint8_t cfgidx) { DCD_EP_Open(pdev, USBD_Dev_CDC_D2H_EP, USBD_Dev_CDC_D2H_EP_SZ, USB_OTG_EP_BULK); DCD_EP_Open(pdev, USBD_Dev_CDC_H2D_EP, USBD_Dev_CDC_H2D_EP_SZ, USB_OTG_EP_BULK); DCD_EP_Open(pdev, USBD_Dev_CDC_CMD_EP, USBD_Dev_CDC_CMD_EP_SZ, USB_OTG_EP_INT); USBD_CDC_InFlight = 0; ringbuffer_init(&USBD_CDC_H2D_FIFO, malloc(CDC_DATA_IN_PACKET_SIZE), CDC_DATA_IN_PACKET_SIZE); ringbuffer_init(&USBD_CDC_D2H_FIFO, malloc(CDC_DATA_MAX_PACKET_SIZE), CDC_DATA_MAX_PACKET_SIZE); USBD_CDC_D2H_Buff = malloc(CDC_DATA_IN_PACKET_SIZE); USBD_CDC_H2D_Buff = malloc(CDC_DATA_MAX_PACKET_SIZE); USBD_CDC_CMD_Buff = malloc(CDC_CMD_PACKET_SZE); /* Prepare Out endpoint to receive next packet */ DCD_EP_PrepareRx(pdev, USBD_Dev_CDC_H2D_EP, (uint8_t*)(USBD_CDC_H2D_Buff), USBD_Dev_CDC_H2D_EP_SZ); return USBD_OK; }
void ICACHE_FLASH_ATTR com_poll(void) { if(ringbuffer_is_empty(&com_out_rb)) { return; } // cid is in data[0], TFP packet has off by one, use +1 for access uint8_t data[TFP_RECV_BUFFER_SIZE + 1]; uint8_t length = ringbuffer_peak(&com_out_rb, data, TFP_RECV_BUFFER_SIZE + 1); if(length > TFP_RECV_BUFFER_SIZE + 1) { ringbuffer_init(&com_out_rb, com_out_rb.buffer, com_out_rb.buffer_length); logd("com_poll: length > %d: %d\n", TFP_RECV_BUFFER_SIZE, length); return; } if(length < TFP_MIN_LENGTH + 1) { ringbuffer_init(&com_out_rb, com_out_rb.buffer, com_out_rb.buffer_length); logd("com_poll: length < TFP_MIN_LENGTH\n"); return; } if(tfp_send_w_cid(data + 1, data[TFP_RECV_INDEX_LENGTH + 1], data[0]) != 0) { ringbuffer_remove(&com_out_rb, data[TFP_RECV_INDEX_LENGTH + 1] + 1); } else { //logd("com_poll send error\n"); } }
void ICACHE_FLASH_ATTR tfp_poll(void) { if(ringbuffer_is_empty(&tfp_rb)) { return; } uint8_t data[TFP_RECV_BUFFER_SIZE]; uint8_t length = ringbuffer_peak(&tfp_rb, data, TFP_RECV_BUFFER_SIZE); if(length > TFP_RECV_BUFFER_SIZE) { ringbuffer_init(&tfp_rb, tfp_rb.buffer, tfp_rb.buffer_length); logd("tfp_poll: length > %d: %d\n", TFP_RECV_BUFFER_SIZE, length); return; } if(length < TFP_MIN_LENGTH) { ringbuffer_init(&tfp_rb, tfp_rb.buffer, tfp_rb.buffer_length); logd("tfp_poll: length < TFP_MIN_LENGTH\n"); return; } if(uart_con_send(data, data[TFP_RECV_INDEX_LENGTH]) != 0) { ringbuffer_remove(&tfp_rb, data[TFP_RECV_INDEX_LENGTH]); } else { logd("tfp_poll send error\n"); } }
kernel_pid_t ng_slip_init(ng_slip_dev_t *dev, uart_t uart, uint32_t baudrate, char *stack, size_t stack_size, char priority) { int res; kernel_pid_t pid; /* reset device descriptor fields */ dev->in_bytes = 0; dev->in_esc = 0; dev->slip_pid = KERNEL_PID_UNDEF; /* initialize buffers */ ringbuffer_init(dev->in_buf, dev->rx_mem, sizeof(dev->rx_mem)); ringbuffer_init(dev->out_buf, dev->tx_mem, sizeof(dev->tx_mem)); /* initialize UART */ DEBUG("slip: initialize UART_%d\n", uart); res = uart_init(uart, baudrate, _slip_rx_cb, _slip_tx_cb, dev); if (res < 0) { DEBUG("slip: error initializing UART_%i with baudrate %u\n", uart, baudrate); return -ENODEV; } /* start SLIP thread */ DEBUG("slip: starting SLIP thread\n"); pid = thread_create(stack, stack_size, priority, CREATE_STACKTEST, _slip, dev, _SLIP_NAME); if (pid < 0) { DEBUG("slip: unable to create SLIP thread\n"); return -EFAULT; } return res; }
int main() { struct RingBuffer *h = NULL; int x; assert(ringbuffer_init(&h, 3, sizeof(x)) == 0); assert(ringbuffer_top(h, (unsigned char *)&x) == RBE_NO_SPACE_OR_DATA); x = 10; assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_SUCCESS); x = -1; assert(ringbuffer_top(h, (unsigned char *)&x) == RBE_SUCCESS); assert(x == 10); x = 666; assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_SUCCESS); x = -1; assert(ringbuffer_top(h, (unsigned char *)&x) == RBE_SUCCESS); assert(x == 10); assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_NO_SPACE_OR_DATA); ringbuffer_destroy(&h); assert(ringbuffer_init(&h, 3, sizeof(x)) == 0); x = INT_MIN; assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_SUCCESS); x = -1; assert(ringbuffer_top(h, (unsigned char *)&x) == RBE_SUCCESS); assert(x == INT_MIN); x = INT_MAX; assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_SUCCESS); x = -1; assert(ringbuffer_top(h, (unsigned char *)&x) == RBE_SUCCESS); assert(x == INT_MIN); ringbuffer_destroy(&h); return 0; }
int main(void) { char *status = "I am written to the UART every 2 seconds\n"; char buf[128]; int res; msg_t msg; main_pid = thread_getpid(); printf("Main thread pid %i \n", main_pid); printf("Testing interrupt driven mode of UART driver\n\n"); printf("Setting up buffers...\n"); ringbuffer_init(&rx_buf, rx_mem, 128); ringbuffer_init(&tx_buf, tx_mem, 128); printf("Initializing UART @ %i", BAUD); if (uart_init(DEV, BAUD, rx, NULL, tx, NULL) >= 0) { printf(" ...done\n"); } else { printf(" ...failed\n"); return 1; } ringbuffer_add(&tx_buf, status, strlen(status)); uart_tx_begin(DEV); while (1) { printf("Going into receive message state\n"); //msg_receive(&msg); if (status) { printf("INPUT: "); res = ringbuffer_get(&rx_buf, buf, rx_buf.avail); buf[res] = '\0'; printf("%s", buf); status = 0; } /* printf("got message"); if (msg.type == MSG_LINE_RDY) { printf("INPUT: "); res = ringbuffer_get(&rx_buf, buf, rx_buf.avail); buf[res] = '\0'; printf("%s", buf); } */ } return 0; }
/* libev read handler for the bound socket. Socket is accepted, * the proxystate is allocated and initalized, and we're off the races * connecting to the backend */ static void handle_accept(struct ev_loop *loop, ev_io *w, int revents) { (void) revents; struct sockaddr_storage addr; socklen_t sl = sizeof(addr); int client = accept(w->fd, (struct sockaddr *) &addr, &sl); if (client == -1) { assert(errno == EINTR || errno == EWOULDBLOCK || errno == EAGAIN); return; } setnonblocking(client); int back = create_back_socket(); if (back == -1) { close(client); perror("{backend-connect}"); return; } SSL_CTX * ctx = (SSL_CTX *)w->data; SSL *ssl = SSL_new(ctx); SSL_set_mode(ssl, SSL_MODE_ENABLE_PARTIAL_WRITE); SSL_set_accept_state(ssl); SSL_set_fd(ssl, client); proxystate *ps = (proxystate *)malloc(sizeof(proxystate)); ps->fd_up = client; ps->fd_down = back; ps->ssl = ssl; ps->want_shutdown = 0; ps->remote_ip = addr; ringbuffer_init(&ps->ring_up); ringbuffer_init(&ps->ring_down); /* set up events */ ev_io_init(&ps->ev_r_up, client_read, client, EV_READ); ev_io_init(&ps->ev_w_up, client_write, client, EV_WRITE); ev_io_init(&ps->ev_r_handshake, client_handshake, client, EV_READ); ev_io_init(&ps->ev_w_handshake, client_handshake, client, EV_WRITE); ev_io_init(&ps->ev_w_down, handle_connect, back, EV_WRITE); ev_io_start(loop, &ps->ev_w_down); ps->ev_r_up.data = ps; ps->ev_w_up.data = ps; ps->ev_r_down.data = ps; ps->ev_w_down.data = ps; ps->ev_r_handshake.data = ps; ps->ev_w_handshake.data = ps; }
void bud_client_side_init(bud_client_side_t* side, bud_client_side_type_t type, bud_client_t* client) { side->type = type; side->tcp.data = client; ringbuffer_init(&side->input); ringbuffer_init(&side->output); side->reading = kBudProgressNone; side->shutdown = kBudProgressNone; side->close = kBudProgressNone; side->write = kBudProgressNone; side->write_size = 0; }
file_socket * new_file_socket(char *filename) { struct stat st; if (stat(filename, &st) == -1) { printf("Error accessing %s: %s\n", filename, strerror(errno)); return NULL; } file_socket * s = malloc(sizeof(file_socket)); s->type = SOCKTYPE_FILE; s->fd = open(filename, O_RDONLY | O_NOCTTY); if (s->fd == -1) { printf("Error opening %s: %s\n", filename, strerror(errno)); free(s); return NULL; } s->filesize = st.st_size; ringbuffer_init(&s->rxbuffer); memset(&s->starttime, 0, sizeof(struct timeval)); memset(&s->lastpausetime, 0, sizeof(struct timeval)); memset(&s->pausedtime, 0, sizeof(struct timeval)); s->paused = 1; s->eof = 0; errorsockets = array_push(errorsockets, s); return s; }
rdpTcp* tcp_new(rdpSettings* settings) { rdpTcp* tcp; tcp = (rdpTcp*) calloc(1, sizeof(rdpTcp)); if (!tcp) return NULL; if (!ringbuffer_init(&tcp->xmitBuffer, 0x10000)) goto out_free; tcp->sockfd = -1; tcp->settings = settings; if (0) goto out_ringbuffer; /* avoid unreferenced label warning on Windows */ #ifndef _WIN32 tcp->event = CreateFileDescriptorEvent(NULL, FALSE, FALSE, tcp->sockfd); if (!tcp->event || tcp->event == INVALID_HANDLE_VALUE) goto out_ringbuffer; #endif return tcp; out_ringbuffer: ringbuffer_destroy(&tcp->xmitBuffer); out_free: free(tcp); return NULL; }
int main(void) { puts("\nManual UART driver test application"); puts("==================================="); puts("This application is intended for testing additional UART\n" "interfaces, that might be defined for a board. The 'primary' UART\n" "interface is tested implicitly, as it is running the shell...\n\n" "When receiving data on one of the additional UART interfaces, this\n" "data will be outputted via STDIO. So the easiest way to test an \n" "UART interface, is to simply connect the RX with the TX pin. Then \n" "you can send data on that interface and you should see the data \n" "being printed to STDOUT\n"); puts("UART INFO:"); printf("Available devices: %i\n", UART_NUMOF); printf("UART used for STDIO (the shell): UART_DEV(%i)\n\n", UART_STDIO_DEV); /* initialize ringbuffers */ for (int i = 0; i < UART_NUMOF; i++) { ringbuffer_init(&(ctx[i].rx_buf), ctx[i].rx_mem, UART_BUFSIZE); } /* start the printer thread */ printer_pid = thread_create(printer_stack, sizeof(printer_stack), PRINTER_PRIO, 0, printer, NULL, "printer"); /* run the shell */ char line_buf[SHELL_DEFAULT_BUFSIZE]; shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE); return 0; }
void fm_init(struct fm_state *fm) { fm->freqs[0] = 100000000; fm->sample_rate = DEFAULT_SAMPLE_RATE; fm->squelch_level = 0; fm->conseq_squelch = 20; fm->terminate_on_squelch = 0; fm->squelch_hits = 0; fm->freq_len = 0; fm->edge = 0; fm->fir_enable = 0; fm->prev_index = 0; fm->post_downsample = 1; // once this works, default = 4 fm->custom_atan = 0; fm->deemph = 0; fm->output_rate = -1; // flag for disabled fm->mode_demod = &fm_demod; fm->pre_j = fm->pre_r = fm->now_r = fm->now_j = 0; fm->prev_lpr_index = 0; fm->deemph_a = 0; fm->now_lpr = 0; fm->dc_block = 0; fm->dc_avg = 0; fm->buf = malloc(sizeof(ringbuffer) + MAXIMUM_BUF_LENGTH * RINGBUFFER_NUM_ELEMENTS); ringbuffer_init((ringbuffer*)fm->buf, MAXIMUM_BUF_LENGTH * RINGBUFFER_NUM_ELEMENTS, DEFAULT_BUF_LENGTH); }
void test_poll_force(void) { uint8_t in_buf[] = { 'a', 'b', 'c', 'd', 'e', 'f', }; struct rb_test_ctx _ctx, *ctx = &_ctx; struct ringbuffer *rb; int rc; ringbuffer_test_context_init(ctx); rb = ringbuffer_init(5); ctx->rbc = ringbuffer_consumer_register(rb, ringbuffer_poll_append_all, ctx); ctx->force_only = true; /* fill the ringbuffer */ rc = ringbuffer_queue(rb, in_buf, 4); assert(!rc); assert(ctx->count == 0); /* add more data */ rc = ringbuffer_queue(rb, in_buf + 4, 2); assert(!rc); /* we should have had a forced poll for the initial two bytes */ assert(ctx->count == 1); assert(ctx->len == 2); assert(!memcmp(in_buf, ctx->data, 2)); ringbuffer_fini(rb); ringbuffer_test_context_fini(ctx); }
/** * @brief Initialize NewLib, called by __libc_init_array() from the startup script */ void _init(void) { #ifndef MODULE_UART0 mutex_init(&uart_rx_mutex); ringbuffer_init(&rx_buf, rx_buf_mem, STDIO_RX_BUFSIZE); #endif uart_init(STDIO, STDIO_BAUDRATE, rx_cb, 0, 0); }
int main() { struct RingBuffer *h = NULL; assert(ringbuffer_init(&h, 1, 100) == 0); assert(h != NULL); ringbuffer_destroy(&h); assert(h == NULL); return 0; }
http_socket * new_http_socket(listen_socket * listener) { http_socket * newcs = malloc(sizeof(http_socket)); newhs->type = SOCKTYPE_CLIENT; ringbuffer_init(&newhs->rxbuffer); ringbuffer_init(&newhs->txbuffer); unsigned int socksize = sizeof(struct sockaddr_storage); newhs->fd = accept(listener->fd, (struct sockaddr *) &newhs->addr, &socksize); char *buf = malloc(BUFFER_SIZE); sock2a(&newhs->addr, buf, BUFFER_SIZE); printf("New HTTP connection from %s (%d/%p)\n", buf, newhs->fd, newhs); free(buf); readsockets = array_push(readsockets, newhs); errorsockets = array_push(errorsockets, newhs); return newhs; }
printer_socket * new_printer_socket(char * portname, int baud) { printer_socket *s = malloc(sizeof(printer_socket)); s->type = SOCKTYPE_PRINTER; s->fd = open(portname, O_RDWR | O_NOCTTY); if (s->fd == -1) { fprintf(stderr, "error opening %s: %s\n", portname, strerror(errno)); exit(1); } if (tcgetattr(s->fd, &s->termios) == -1) { fprintf(stderr, "error getting attributes for %s: %s\n", portname, strerror(errno)); exit(1); } cfmakeraw(&s->termios); if (cfsetspeed(&s->termios, baud2termios(baud)) == -1) { fprintf(stderr, "error setting baud rate on %s: %s\n", portname, strerror(errno)); exit(1); } if (tcsetattr(s->fd, TCSANOW, &s->termios) == -1) { fprintf(stderr, "error setting attributes for %s: %s\n", portname, strerror(errno)); exit(1); } s->tokens = 1; s->maxtoken = 1; ringbuffer_init(&s->rxbuffer); ringbuffer_init(&s->txbuffer); readsockets = array_push(readsockets, s); errorsockets = array_push(errorsockets, s); return s; }
void cereal_init(uint32_t baud) { ringbuffer_init(&cereal_incoming, malloc(CEREAL_INCOMING_MAX_SIZE), CEREAL_INCOMING_MAX_SIZE); #ifdef ENABLE_CEREAL_BUFFERED_TX ringbuffer_init(&cereal_outgoing, malloc(CEREAL_OUTGOING_MAX_SIZE), CEREAL_OUTGOING_MAX_SIZE); #endif RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1); GPIO_InitTypeDef gi; GPIO_StructInit(&gi); gi.GPIO_Pin = GPIO_Pin_9; gi.GPIO_Mode = GPIO_Mode_AF; gi.GPIO_Speed = GPIO_Speed_50MHz; gi.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &gi); gi.GPIO_Pin = GPIO_Pin_10; GPIO_Init(GPIOA, &gi); USART_InitTypeDef ui; USART_StructInit(&ui); ui.USART_BaudRate = baud; USART_Init(CEREAL_USARTx, &ui); USART_Cmd(CEREAL_USARTx, ENABLE); USART_ITConfig(CEREAL_USARTx, USART_IT_RXNE, ENABLE); NVIC_InitTypeDef ni; ni.NVIC_IRQChannel = USART1_IRQn; ni.NVIC_IRQChannelPreemptionPriority = 0; ni.NVIC_IRQChannelSubPriority = 0; ni.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&ni); }
void board_uart0_init(void) { ringbuffer_init(&uart0_ringbuffer, buffer, UART0_BUFSIZE); int pid = thread_create( uart0_thread_stack, sizeof(uart0_thread_stack), PRIORITY_MAIN - 1, CREATE_STACKTEST | CREATE_SLEEPING, chardev_thread_entry, &uart0_ringbuffer, "uart0" ); uart0_handler_pid = pid; thread_wakeup(pid); puts("uart0_init() [OK]"); }
/** * @brief 设备的初始化 * @param[in] #define USB_VIRTUAL_PORT 1 //虚拟串口 * #define USB_KEYBOARD 2 //USB键盘 * #define USB_MASSSTORAGE 3 //大容量存储设备 * #define USB_PRINTER 4 //USB打印机设备 * @note 函数调用者保证device_type确实是已经实现的USB类 */ void usb_device_init(unsigned char device_type) { if (device_type == USB_PRINTER) { MEMSET(usb_rec_buffer,0,USB_BUFFER_LEN); ringbuffer_init(&spp_ringbuf[USB_PRINT_CHANNEL_OFFSET],usb_rec_buffer,USB_BUFFER_LEN); } if (g_usb_type != device_type) { g_usb_type = device_type; usb_SetClock(); usb_Interrupts_Config(); USB_Init(); } }
static int transport_bio_buffered_new(BIO* bio) { WINPR_BIO_BUFFERED_SOCKET* ptr; BIO_set_init(bio, 1); BIO_set_flags(bio, BIO_FLAGS_SHOULD_RETRY); ptr = (WINPR_BIO_BUFFERED_SOCKET*) calloc(1, sizeof(WINPR_BIO_BUFFERED_SOCKET)); if (!ptr) return -1; BIO_set_data(bio, (void*) ptr); if (!ringbuffer_init(&ptr->xmitBuffer, 0x10000)) return -1; return 1; }
void vs_ring_write(msg_level_t level, const char *format, va_list ap) { int n; rtapi_msg_t logmsg; #if defined(RTAPI) && defined(BUILD_SYS_USER_DSO) static pid_t rtapi_pid; if (rtapi_pid == 0) rtapi_pid = getpid(); #endif if (global_data) { // one-time initialisation if (!rtapi_message_buffer.header) { ringbuffer_init(&global_data->rtapi_messages, &rtapi_message_buffer); } logmsg.hdr.origin = MSG_ORIGIN; #if defined(RTAPI) && defined(BUILD_SYS_KBUILD) logmsg.hdr.pid = 0; #endif #if defined(RTAPI) && defined(BUILD_SYS_USER_DSO) logmsg.hdr.pid = rtapi_pid; #endif #if defined(ULAPI) logmsg.hdr.pid = getpid(); #endif logmsg.hdr.level = level; logmsg.hdr.encoding = MSG_ASCII; strncpy(logmsg.hdr.tag, logtag, sizeof(logmsg.hdr.tag)); // do format outside critical section n = vsnprintf(logmsg.buf, RTPRINTBUFFERLEN, format, ap); if (rtapi_message_buffer.header->use_wmutex && rtapi_mutex_try(&rtapi_message_buffer.header->wmutex)) { global_data->error_ring_locked++; return; } // use copying writer to shorten criticial section record_write(&rtapi_message_buffer, (void *) &logmsg, sizeof(rtapi_msgheader_t) + n + 1); // trailing zero if (rtapi_message_buffer.header->use_wmutex) rtapi_mutex_give(&rtapi_message_buffer.header->wmutex); } }
dsp_filter_t dsp_init(ruuvi_dsp_function_t type, uint8_t dsp_parameter) { dsp_filter_t filter; memset(&filter, 0, sizeof(filter)); switch(type) { case DSP_STDEV: filter.process = dsp_process_stdev; filter.read = dsp_read_stdev; filter.dsp_parameter = dsp_parameter; ringbuffer_init(&filter.z, dsp_parameter, sizeof(float)); break; default: NRF_LOG_ERROR("Unknown filter type\r\n"); break; } return filter; }
static int transport_bio_buffered_new(BIO* bio) { WINPR_BIO_BUFFERED_SOCKET* ptr; bio->init = 1; bio->num = 0; bio->ptr = NULL; bio->flags = BIO_FLAGS_SHOULD_RETRY; ptr = (WINPR_BIO_BUFFERED_SOCKET*) calloc(1, sizeof(WINPR_BIO_BUFFERED_SOCKET)); if (!ptr) return -1; bio->ptr = (void*) ptr; if (!ringbuffer_init(&ptr->xmitBuffer, 0x10000)) return -1; return 1; }
void ICACHE_FLASH_ATTR tfp_open_connection(void) { // Common initialisations. ringbuffer_init(&tfp_rb, tfp_rb_buffer, TFP_RING_BUFFER_SIZE); brickd_init(); com_init(); for(uint8_t i = 0; i < TFP_MAX_CONNECTIONS; i++) { tfp_init_con(i); } /* * When mesh mode is enabled all the socket setup is done from mesh specific * callbacks. Existing TFP socket callbacks and implementation are used but as * a layer underneath the mesh layer. */ if(!configuration_current.mesh_enable) { ets_memset(&tfp_con_listen, 0, sizeof(struct espconn)); // Initialize the ESPConn espconn_create(&tfp_con_listen); tfp_con_listen.type = ESPCONN_TCP; tfp_con_listen.state = ESPCONN_NONE; // Make it a TCP connection tfp_con_listen.proto.tcp = &tfp_con_listen_tcp; tfp_con_listen.proto.tcp->local_port = configuration_current.general_port; espconn_regist_reconcb(&tfp_con_listen, tfp_reconnect_callback); espconn_regist_connectcb(&tfp_con_listen, tfp_connect_callback); // Start listening espconn_accept(&tfp_con_listen); // Set server timeout (in seconds) espconn_regist_time(&tfp_con_listen, 7200, 0); } else { logi("MSH:TFP init\n"); } }
int main() { struct RingBuffer *h = NULL; int x = 10, y = INT_MAX, z = 2863311530, m = -3333; int offset_remove = 2; int offset_insert = 3; int offset_data = 4; assert(ringbuffer_init(&h, 3, sizeof(x)) == 0); assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_SUCCESS); assert(ringbuffer_push(h, (unsigned char *)&y) == RBE_SUCCESS); assert(ringbuffer_full(h) != 0); assert(ringbuffer_pop(h) == RBE_SUCCESS); assert(ringbuffer_push(h, (unsigned char *)&z) == RBE_SUCCESS); assert(ringbuffer_full(h) != 0); assert(ringbuffer_resize(&h, 5) == RBE_SUCCESS); assert(ringbuffer_full(h) == 0); assert(ringbuffer_empty(h) == 0); assert(ringbuffer_push(h, (unsigned char *)&m) == RBE_SUCCESS); assert(ringbuffer_push(h, (unsigned char *)&x) == RBE_SUCCESS); assert(ringbuffer_full(h) != 0); { /* checking the data: x y z m x */ unsigned *off = (unsigned *) h; int *values = (int *) off + offset_data; assert(*(off + offset_remove) == 1 * sizeof(x)); assert(*(off + offset_insert) == 0); assert(values[0] == x); assert(values[1] == y); assert(values[2] == z); assert(values[3] == m); assert(values[4] == x); } ringbuffer_destroy(&h); return 0; }
int main() { int i; int j; int r; int after; ssize_t len; char* ptr; data = malloc(TEST_DATA_SIZE); assert(data != NULL); ringbuffer_init(&rb); /* Fill test data */ for (i = 0; i < TEST_DATA_SIZE; i++) data[i] = (i * i) % 137; /* Fill ringbuffer */ i = 0; after = 0; while (i < TEST_DATA_SIZE - TEST_INSERT_LEN) { if (after) len = TEST_DATA_SIZE - i - TEST_INSERT_LEN; else len = TEST_INSERT_OFF - i; ptr = ringbuffer_write_ptr(&rb, &len); ASSERT(ptr != NULL); /* Always make progress */ ASSERT(len > 0); if (after) memcpy(ptr, data + i + TEST_INSERT_LEN, len); else memcpy(ptr, data + i, len); i += len; r = ringbuffer_write_append(&rb, len); ASSERT(r == 0); if (i == TEST_INSERT_OFF) after = 1; } ASSERT(ringbuffer_size(&rb) == TEST_DATA_SIZE - TEST_INSERT_LEN); /* Insert stuff */ ringbuffer_insert(&rb, TEST_INSERT_OFF, data + TEST_INSERT_OFF, TEST_INSERT_LEN); /* Read from it */ i = 0; while (i < TEST_DATA_SIZE) { len = TEST_DATA_SIZE - i; ptr = ringbuffer_read_next(&rb, &len); ASSERT(ptr != NULL); /* Always make progress */ ASSERT(len > 0); for (j = 0; j < len; j++) ASSERT(ptr[j] == data[i + j]); ringbuffer_read_skip(&rb, len); i += len; } /* Destroy it */ ringbuffer_destroy(&rb); return 0; }
void delay_init(delay_s *_this) { memset(_this, 0, sizeof(*_this)); ringbuffer_init(&_this->buffer); }