void gen_audio(void *data, uint8_t *stream, int len) { int i; int16_t *p = (void *)stream; int b; while(rb_used(rb_audio) < len/2) { unsigned v = 0; if(rb_used(rb_data) > 0) { unsigned c = rb_pop(rb_data); v = (((c ^ 0xff) << 1) | 0x1); } for(b=0; b<10; b++) { for(i=0; i<(SRATE / BRATE); i++) { int16_t y = cos(t * M_PI * 2) * 32000; rb_push(rb_audio, y); if(v & 1) { t += FREQ_1 / SRATE; } else { t += FREQ_0 / SRATE; } } v >>= 1; } } for(i=0; i<len/2; i++) { *p++ = rb_pop(rb_audio); } }
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); }
END_TEST START_TEST (test_connects_tcp_and_establishes_protocol) { uint32_t version = htonl(1); rb_append(in_rb, &version, sizeof(version)); neo4j_connection_t *connection = neo4j_tcp_connect( "localhost", 7687, config, 0); ck_assert_ptr_ne(connection, NULL); ck_assert_ptr_eq(connection->iostream, client_ios); ck_assert_int_eq(rb_used(out_rb), 20); uint8_t expected_hello[4] = { 0x60, 0x60, 0xB0, 0x17 }; uint8_t hello[4]; rb_extract(out_rb, hello, 4); ck_assert(memcmp(hello, expected_hello, 4) == 0); uint32_t expected_versions[4] = { htonl(1), 0, 0, 0 }; uint32_t versions[4]; rb_extract(out_rb, versions, 16); ck_assert(memcmp(versions, expected_versions, 16) == 0); neo4j_close(connection); }
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); }
END_TEST START_TEST (test_connects_URI_containing_credentials) { uint32_t version = htonl(1); rb_append(in_rb, &version, sizeof(version)); username = "******"; password = "******"; neo4j_connection_t *connection = neo4j_connect( "neo4j://john:smith@localhost:7687", config, 0); ck_assert_ptr_ne(connection, NULL); ck_assert_ptr_eq(connection->iostream, client_ios); ck_assert_int_eq(rb_used(out_rb), 20); uint8_t expected_hello[4] = { 0x60, 0x60, 0xB0, 0x17 }; uint8_t hello[4]; rb_extract(out_rb, hello, 4); ck_assert(memcmp(hello, expected_hello, 4) == 0); uint32_t expected_versions[4] = { htonl(1), 0, 0, 0 }; uint32_t versions[4]; rb_extract(out_rb, versions, 16); ck_assert(memcmp(versions, expected_versions, 16) == 0); neo4j_close(connection); }
/** Updates the Ribbons to a new frame. This is called by a timer. */ void Skitt::update_screen() { unsigned int k; for (k = 0; k < num_ribbons; k++) { if (rb_used(screen->get(k)->buffer) == 0) { return; } /* Change SPI control pins, and send the frame out. */ change_spi_address(screen->get(k)->mux_address); send_frame(screen->get(k)); } }
/*! * \brief Wait for activity on an socket * \param fd -- file descriptor * \param ms -- pointer to an int containing a timeout in ms * \return 0 on timeout and the socket fd (non-zero) otherwise * \retval 0 timeout */ EXPORT_DEF int at_wait (int fd, int* ms) { int exception, outfd; outfd = ast_waitfor_n_fd (&fd, 1, ms, &exception); if (outfd < 0) { outfd = 0; } return outfd; } #/* return number of bytes readed */ EXPORT_DEF ssize_t at_read (int fd, const char * dev, struct ringbuffer* rb) { struct iovec iov[2]; int iovcnt; ssize_t n = -1; /* TODO: read until major error */ iovcnt = rb_write_iov (rb, iov); if (iovcnt > 0) { n = readv (fd, iov, iovcnt); if (n < 0) { if (errno != EINTR && errno != EAGAIN) { ast_debug (1, "[%s] readv() error: %d\n", dev, errno); return n; } return 0; } else if (n > 0) { rb_write_upd (rb, n); ast_debug (5, "[%s] receive %zu byte, used %zu, free %zu, read %zu, write %zu\n", dev, n, rb_used (rb), rb_free (rb), rb->read, rb->write); iovcnt = rb_read_all_iov (rb, iov); if (iovcnt > 0) { if (iovcnt == 2) { ast_debug (5, "[%s] [%.*s%.*s]\n", dev, (int) iov[0].iov_len, (char*) iov[0].iov_base, (int) iov[1].iov_len, (char*) iov[1].iov_base); } else { ast_debug (5, "[%s] [%.*s]\n", dev, (int) iov[0].iov_len, (char*) iov[0].iov_base); } } } } else ast_log (LOG_ERROR, "[%s] at cmd receive buffer overflow\n", dev); return n; }
/** * This returns true if the number of frames inside each ribbons buffer is * the number of frames (variable num_frames). */ bool are_buffers_full(unsigned int num_frames) { /* Only return true if all buffers are full. If any one of them is not full * return false. */ unsigned int i; Ribbon *r; /* Temporary ribbon pointer. */ for (i = 0; i < num_ribbons; i++) { r = get(i); if (rb_used(r->buffer) < (num_frames * r->length * 3)) return false; } return true; }
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); }
void send(void *buf, size_t len) { uint8_t *p = buf; uint8_t sum = 0; int i; if(rb_used(rb_data) > 0) return; for(i=0; i<len; i++) { send_byte(p[i]); sum += p[i]; } send_byte(sum ^ 0xff); rb_push(rb_data, 0xff); printf("\n"); }
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); }
rbstream_p rbs_open(rb_p rbp, rbstream_input_f input_func, void *data) { medvdbg("[%s] rbp %p, input_func %p, data %p\n", __FUNCTION__, rbp, input_func, data); RETURN_VAL_IF_FAIL(rbp != NULL, NULL); rbstream_p rbsp = (rbstream_p) calloc(1, sizeof(rbstream_t)); RETURN_VAL_IF_FAIL(rbsp != NULL, NULL); rbsp->rbp = rbp; rbsp->options = 0; rbsp->cur_pos = 0; rbsp->rd_size = 0; rbsp->wr_size = rb_used(rbp); // Allow to preset data in ring-buffer. rbsp->data = data; rbsp->input_func = input_func; medvdbg("[%s] done, rbsp %p\n", __FUNCTION__, rbsp); return rbsp; }
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); }
ssize_t buffering_write(neo4j_iostream_t *stream, const void *buf, size_t nbyte) { struct buffering_iostream *ios = container_of(stream, struct buffering_iostream, _iostream); if (ios->delegate == NULL) { errno = EPIPE; return -1; } if (ios->sndbuf == NULL) { return neo4j_ios_write(ios->delegate, buf, nbyte); } if (nbyte > SSIZE_MAX) { nbyte = SSIZE_MAX; } if (nbyte <= rb_space(ios->sndbuf)) { return rb_append(ios->sndbuf, buf, nbyte); } size_t buffered = rb_used(ios->sndbuf); struct iovec iov[3]; unsigned int iovcnt = rb_data_iovec(ios->sndbuf, iov, rb_size(ios->sndbuf)); iov[iovcnt].iov_base = (uint8_t *)(intptr_t)buf; iov[iovcnt].iov_len = nbyte; ++iovcnt; ssize_t written = neo4j_ios_writev(ios->delegate, iov, iovcnt); if (written < 0) { return -1; } const uint8_t *rbytes; size_t remaining; if ((size_t)written < buffered) { rb_discard(ios->sndbuf, written); rbytes = buf; remaining = nbyte; written = 0; } else { rb_clear(ios->sndbuf); written -= buffered; assert((size_t)written <= nbyte); rbytes = (const uint8_t *)buf + written; remaining = nbyte - written; } if (remaining == 0) { return nbyte; } size_t appended = rb_append(ios->sndbuf, rbytes, remaining); return (size_t)written + appended; }
bool audio_decoder_dataspace_is_empty(audio_decoder_p decoder) { assert(decoder != NULL); return !rb_used(&decoder->ringbuffer); }
ssize_t buffering_writev(neo4j_iostream_t *stream, const struct iovec *iov, unsigned int iovcnt) { struct buffering_iostream *ios = container_of(stream, struct buffering_iostream, _iostream); if (ios->delegate == NULL) { errno = EPIPE; return -1; } if (ios->sndbuf == NULL) { return neo4j_ios_writev(ios->delegate, iov, iovcnt); } if (iovcnt > IOV_MAX-2) { iovcnt = IOV_MAX-2; } size_t nbyte = iovlen(iov, iovcnt); if (nbyte <= rb_space(ios->sndbuf)) { return rb_appendv(ios->sndbuf, iov, iovcnt); } size_t buffered = rb_used(ios->sndbuf); ALLOC_IOVEC(diov, iovcnt+2); if (diov == NULL) { return -1; } unsigned int diovcnt = rb_data_iovec(ios->sndbuf, diov, rb_size(ios->sndbuf)); memcpy(diov+diovcnt, iov, iovcnt * sizeof(struct iovec)); ssize_t written = neo4j_ios_writev(ios->delegate, diov, diovcnt + iovcnt); if (written < 0) { goto cleanup; } if ((size_t)written < buffered) { rb_discard(ios->sndbuf, written); memcpy(diov, iov, iovcnt * sizeof(struct iovec)); diovcnt = iovcnt; written = 0; } else { rb_clear(ios->sndbuf); written -= buffered; assert((size_t)written <= nbyte); diovcnt = iov_skip(diov, iov, iovcnt, written); } if (diovcnt == 0) { written = nbyte; goto cleanup; } written += rb_appendv(ios->sndbuf, diov, diovcnt); int errsv; cleanup: errsv = errno; FREE_IOVEC(diov); errno = errsv; return written; }
EXPORT_DEF int at_read_result_iov (const char * dev, int * read_result, struct ringbuffer* rb, struct iovec iov[2]) { int iovcnt = 0; int res; size_t s; s = rb_used (rb); if (s > 0) { /* ast_debug (5, "[%s] d_read_result %d len %d input [%.*s]\n", dev, *read_result, s, MIN(s, rb->size - rb->read), (char*)rb->buffer + rb->read); */ if (*read_result == 0) { res = rb_memcmp (rb, "\r\n", 2); if (res == 0) { rb_read_upd (rb, 2); *read_result = 1; return at_read_result_iov (dev, read_result, rb, iov); } else if (res > 0) { if (rb_memcmp (rb, "\n", 1) == 0) { ast_debug (5, "[%s] multiline response\n", dev); rb_read_upd (rb, 1); return at_read_result_iov (dev, read_result, rb, iov); } if (rb_read_until_char_iov (rb, iov, '\r') > 0) { s = iov[0].iov_len + iov[1].iov_len + 1; } rb_read_upd (rb, s); return at_read_result_iov (dev, read_result, rb, iov); } return 0; } else { if (rb_memcmp (rb, "+CSSI:", 6) == 0) { iovcnt = rb_read_n_iov (rb, iov, 8); if (iovcnt > 0) { *read_result = 0; } return iovcnt; } else if (rb_memcmp (rb, "\r\n+CSSU:", 8) == 0 || rb_memcmp (rb, "\r\n+CMS ERROR:", 13) == 0 || rb_memcmp (rb, "\r\n+CMGS:", 8) == 0) { rb_read_upd (rb, 2); return at_read_result_iov (dev, read_result, rb, iov); } else if (rb_memcmp (rb, "> ", 2) == 0) { *read_result = 0; return rb_read_n_iov (rb, iov, 2); } else if (rb_memcmp (rb, "+CMGR:", 6) == 0 || rb_memcmp (rb, "+CNUM:", 6) == 0 || rb_memcmp (rb, "ERROR+CNUM:", 11) == 0 || rb_memcmp (rb, "+CLCC:", 6) == 0) { iovcnt = rb_read_until_mem_iov (rb, iov, "\n\r\nOK\r\n", 7); if (iovcnt > 0) { *read_result = 0; } return iovcnt; } else { iovcnt = rb_read_until_mem_iov (rb, iov, "\r\n", 2); if (iovcnt > 0) { *read_result = 0; s = iov[0].iov_len + iov[1].iov_len + 1; return rb_read_n_iov (rb, iov, s); } } } } return 0; }