static char* test_cbuffer_pop(void) { CircularBuffer* mybuf = cbuffer_new(); // put us at the end of the buffer mybuf->pos = IO_BUFFER_SIZE - 5; mybuf->tail = IO_BUFFER_SIZE - 5; uint32_t test_data[11] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; cbuffer_append(mybuf, test_data, 11); Buffer* bucky = cbuffer_pop(mybuf, 5); mu_assert_eq("size", cbuffer_size(mybuf), 6); mu_assert_eq("content", memcmp(bucky->data, test_data, 5*sizeof(uint32_t)), 0); Buffer* badger = cbuffer_pop(mybuf, 6); mu_assert_eq("size2", cbuffer_size(mybuf), 0); mu_assert_eq("content2", memcmp(badger->data, test_data + 5, 6), 0); // if we pop an empty collection, we get nothing. Buffer* empty = cbuffer_pop(mybuf, 10); mu_assert_eq("size3", empty->size, 0); cbuffer_free(mybuf); buffer_free(bucky); buffer_free(badger); buffer_free(empty); return 0; }
int main(void) { struct cbuffer_t *cbuffer; uint8_t *message; uint8_t FLloop, len, i; char rxc; FLloop=TRUE; cbuffer = cbuffer_init(); message = malloc(MSG_SIZE); printf("\nTest circular buffer.\n"); printf("Copyright (C) 2016 Enrico Rossi - GNU GPL\n"); help(); while (FLloop) { /* get the char */ rxc = getchar(); switch(rxc) { case 'g': len = cbuffer_pop(cbuffer, message, MSG_SIZE); if (len) { printf("> Data fetched: %d\n", len); for (i=0; i < len; i++) printf("%c", *(message + i)); printf("\n"); } else { printf("> No data\n"); } printit(cbuffer); break; case 'h': help(); break; case 'q': FLloop=FALSE; break; case 'c': cbuffer_clear(cbuffer); printit(cbuffer); break; case '\n': break; default: rxc = 'a' + cbuffer->idx; cbuffer_push(cbuffer, rxc); printit(cbuffer); } } free(message); cbuffer_shut(cbuffer); return(0); }
int next_token( struct char_buffer* cbuff, char* output ) { int i = 0; //clear the leading space while( cbuff->state != CBUFF_EMPTY && cbuffer_peek( cbuff ) == CHAR_SPACE ) { //pop the space cbuffer_pop( cbuff ); } //we have reached the token while( cbuff->state != CBUFF_EMPTY && cbuffer_peek( cbuff ) != CHAR_SPACE ) { output[i++] = cbuffer_pop( cbuff ); } //add the null terminator output[i] = '\0'; return i; }
int ircsock_join(IRCSock *ircsock) { /*{{{*/ int foundPing = 0; /* these use 16 as it should cover needed space for 4 letter command, a * space, and extra arguments besides nick/chan */ char *nickc = malloc(16 + strlen(ircsock->nick)); char *userc = malloc(16 + strlen(ircsock->nick) * 2); char *joinc = malloc(16 + strlen(ircsock->chan)); char *str = NULL; if(!nickc || !userc || !joinc) { fprintf(stderr, "Failed malloc in ircsock_join\n"); return IRCSOCK_MERROR; } strcpy(nickc, "NICK "); strcat(nickc, ircsock->nick); strcpy(userc, "USER "); strcat(userc, ircsock->nick); strcat(userc, " j j :"); strcat(userc, ircsock->nick); strcpy(joinc, "JOIN "); strcat(joinc, ircsock->chan); ircsock_send(ircsock, nickc); ircsock_send(ircsock, userc); usleep(100000); ircsock_send(ircsock, joinc); while(!foundPing) { ircsock_read(ircsock); while((str = cbuffer_pop(ircsock->cbuf)) != NULL) { if(strstr(str, " 433 ")) { fprintf(stderr, "Nickname in use!\n"); return 433; } if((str[0] == 'P') && (str[1] == 'I') && (str[2] == 'N') && (str[3] == 'G') && (str[4] == ' ') && (str[5] == ':')) { str[1] = 'O'; ircsock_send(ircsock, str); free(str); foundPing = 1; break; } free(str); } } return 0; } /*}}}*/
void command_echo( struct char_buffer* cbuff ) { char str_arg1[CONFIG_CLI_MAX_CHARACTERS]; char str_arg2[CONFIG_CLI_MAX_CHARACTERS]; if( next_token( cbuff, str_arg1 ) == 0 ) { CommandOutput( "[ECHO]\tInvalid Arguments" ); return; } int i = 0; while( cbuff->state != CBUFF_EMPTY ) { str_arg2[i] = cbuffer_pop( cbuff ); i++; } str_arg2[i] = '\0'; CommandOutput( "%s%s", str_arg1, str_arg2 ); }
/* This function is expected to update the samples in sb to remove echo - once * it completes, they are ready to go out the tx side of the hybrid */ void echo_update_tx(echo *e, SAMPLE_BLOCK *sb) { size_t i; int any_doubletalk = 0; for (i=0; i<sb->count; i++) { SAMPLE rx_s, tx_s; float tx, rx; rx_s = cbuffer_pop(e->rx_buf); tx_s = sb->s[i]; tx = (float)tx_s; rx = (float)rx_s; /* High-pass filter - filter out sub-300Hz signals */ tx = update_fir(e->hp, tx); /* Geigel double-talk detector */ int update = !dtd(e, tx); /* nlms-pw */ tx = nlms_pw(e, tx, rx, update); /* If we're not talking, let's attenuate our signal */ if (update) { tx *= M12dB; } else { any_doubletalk = 1; } /* clipping */ tx = clip(tx); sb->s[i] = (int)tx; } VERBOSE_LOG("%s\n", any_doubletalk ? "doubletalk" : " "); }