uint32 SPM_u32PullData(void *data, int len) { uint32 free_cnt = 0; uint32 min_cnt = 0; uint32 avlb_cnt = 0; /* Cal how much free space do SPM have */ OS_eEnterCriticalSection(mutexRxRb); free_cnt = ringbuffer_free_space(&rb_rx_spm); OS_eExitCriticalSection(mutexRxRb); min_cnt = MIN(free_cnt, len); DBG_vPrintf(TRACE_NODE, "rev_cnt: %u, free_cnt: %u \r\n", len, free_cnt); if(min_cnt > 0) { OS_eEnterCriticalSection(mutexRxRb); ringbuffer_push(&rb_rx_spm, data, min_cnt); avlb_cnt = ringbuffer_data_size(&rb_rx_spm); OS_eExitCriticalSection(mutexRxRb); } return avlb_cnt; }
static int wait_fill_event(struct tb_event *event, struct timeval *timeout) { /* ;-) */ #define ENOUGH_DATA_FOR_INPUT_PARSING 128 int result; char buf[ENOUGH_DATA_FOR_INPUT_PARSING]; fd_set events; memset(event, 0, sizeof(struct tb_event)); /* try to extract event from input buffer, return on success */ event->type = TB_EVENT_KEY; if (extract_event(event, &inbuf, inputmode) == 0) return TB_EVENT_KEY; /* it looks like input buffer is incomplete, let's try the short path */ size_t r = fread(buf, 1, ENOUGH_DATA_FOR_INPUT_PARSING, in); if (r < ENOUGH_DATA_FOR_INPUT_PARSING && feof(in)) clearerr(in); if (r > 0) { if (ringbuffer_free_space(&inbuf) < r) return -1; ringbuffer_push(&inbuf, buf, r); if (extract_event(event, &inbuf, inputmode) == 0) return TB_EVENT_KEY; } /* no stuff in FILE's internal buffer, block in select */ while (1) { FD_ZERO(&events); FD_SET(in_fileno, &events); FD_SET(winch_fds[0], &events); int maxfd = (winch_fds[0] > in_fileno) ? winch_fds[0] : in_fileno; result = select(maxfd+1, &events, 0, 0, timeout); if (!result) return 0; if (FD_ISSET(in_fileno, &events)) { event->type = TB_EVENT_KEY; size_t r = fread(buf, 1, ENOUGH_DATA_FOR_INPUT_PARSING, in); if (r < ENOUGH_DATA_FOR_INPUT_PARSING && feof(in)) clearerr(in); if (r == 0) continue; /* if there is no free space in input buffer, return error */ if (ringbuffer_free_space(&inbuf) < r) return -1; /* fill buffer */ ringbuffer_push(&inbuf, buf, r); if (extract_event(event, &inbuf, inputmode) == 0) return TB_EVENT_KEY; } if (FD_ISSET(winch_fds[0], &events)) { event->type = TB_EVENT_RESIZE; int zzz = 0; read(winch_fds[0], &zzz, sizeof(int)); buffer_size_change_request = 1; get_term_size(&event->w, &event->h); return TB_EVENT_RESIZE; } } }