int unixmessage_receiver_init (unixmessage_receiver_t *b, int fd, char *mainbuf, unsigned int mainlen, char *auxbuf, unsigned int auxlen) { if (!cbuffer_init(&b->mainb, mainbuf, mainlen) || !cbuffer_init(&b->auxb, auxbuf, auxlen)) return 0 ; b->fd = fd ; b->mainlen = b->auxlen = 0 ; b->maindata = stralloc_zero ; b->auxdata = stralloc_zero ; b->fds_ok = 3 ; return 1 ; }
/*! Init the usart port. * * Allocates the global usart pointer. * * \note does NOT start the port(s). * \warning this should be called only once in the code * to avoid memory leak. */ void usart_init(void) { /* Initialize the struct */ usart = malloc(sizeof(struct usart_t)); #ifdef USE_USART1 usart->rx1 = cbuffer_init(); usart->tx1_buffer = malloc(USART1_TXBUF_SIZE); #endif usart->rx0 = cbuffer_init(); usart->tx0_buffer = malloc(USART0_TXBUF_SIZE); }
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); }
/** * cbuffer_alloc - allocates a new circle buffer and its internal buffer * @size: the size of the internal buffer to be allocated. * */ struct cbuffer * cbuffer_alloc(unsigned int size) { unsigned char *buffer; struct cbuffer *cb; buffer = MALLOC(size); if (!buffer) return NULL; cb = cbuffer_init(buffer, size); if (!cb) { FREE(buffer, size); } return cb; }
/* Add another thread for file capture to disk or network * When settings are changed, snort must be restarted to get it applied */ void file_agent_init(FileInspectConf* conf) { /*Need to check configuration to decide whether to enable them*/ if (conf->file_type_enabled) { _dpd.fileAPI->enable_file_type(file_agent_type_callback); file_type_enabled = true; } if (conf->file_signature_enabled) { _dpd.fileAPI->enable_file_signature(file_agent_signature_callback); file_signature_enabled = true; } if (conf->file_capture_enabled) { _dpd.fileAPI->enable_file_capture(file_agent_signature_callback); file_capture_enabled = true; } if (conf->hostname) { file_agent_init_socket(conf->hostname, conf->portno); } file_list = cbuffer_init(conf->file_capture_queue_size); if(!file_list) { FILE_FATAL_ERROR("File capture: Unable to create file capture queue!"); } file_agent_thread_init(); #ifndef WIN32 /* In daemon mode we need to re-create cbuffer consumer thread */ pthread_atfork(file_agent_close,NULL,file_agent_thread_init); #endif }
echo *echo_create(hybrid *h) { echo *e = malloc(sizeof(echo)); e->rx_buf = cbuffer_init(NLMS_LEN); e->x = malloc((NLMS_LEN+NLMS_EXT) * sizeof(float)); e->xf = malloc((NLMS_LEN+NLMS_EXT) * sizeof(float)); e->w = malloc(NLMS_LEN * sizeof(float)); e->j = NLMS_EXT; /* HACK: we increment w rather than setting it directly, so it needs to have * a valid IEEE-754 value */ int i; int j = e->j; for (i = 0; i < NLMS_LEN; i+=2) { e->x[j+i] = 0; e->x[j+i+1] = 0; e->xf[j+i] = 1.0/NLMS_LEN; e->xf[j+i+1] = 1.0/NLMS_LEN; e->w[i] = 1.0/NLMS_LEN; e->w[i+1] = 1.0/NLMS_LEN; } e->hp = hp_fir_create(); e->Fx = iir_create(); e->Fe = iir_create(); e->iir_dc = iirdc_create(); e->dotp_xf_xf = M80dB_PCM; e->h = h; return e; }
void uartbuf_init(void) { cbuffer_init(&Uart[SLAVE_RX]); cbuffer_init(&Uart[SLAVE_TX]); cbuffer_init(&Uart[SLAVE_HPTX]); }
/* Add another thread for file capture to disk or network * When settings are changed, snort must be restarted to get it applied */ void file_agent_init(FileInspectConf* conf) { int rval; const struct timespec thread_sleep = { 0, 100 }; sigset_t mask; /*Need to check configuration to decide whether to enable them*/ if (conf->file_type_enabled) { _dpd.fileAPI->enable_file_type(file_agent_type_callback); file_type_enabled = true; } if (conf->file_signature_enabled) { _dpd.fileAPI->enable_file_signature(file_agent_signature_callback); file_signature_enabled = true; } if (conf->file_capture_enabled) { _dpd.fileAPI->enable_file_capture(file_agent_signature_callback); file_capture_enabled = true; } if (conf->hostname) { file_agent_init_socket(conf->hostname, conf->portno); } /* Spin off the file capture handler thread. */ sigemptyset(&mask); sigaddset(&mask, SIGTERM); sigaddset(&mask, SIGQUIT); sigaddset(&mask, SIGPIPE); sigaddset(&mask, SIGINT); sigaddset(&mask, SIGHUP); sigaddset(&mask, SIGUSR1); sigaddset(&mask, SIGUSR2); sigaddset(&mask, SIGCHLD); sigaddset(&mask, SIGURG); sigaddset(&mask, SIGVTALRM); pthread_sigmask(SIG_SETMASK, &mask, NULL); file_list = cbuffer_init(conf->file_capture_queue_size); if(!file_list) { FILE_FATAL_ERROR("File capture: Unable to create file capture queue!"); } if ((rval = pthread_create(&capture_thread_tid, NULL, &FileCaptureThread, conf)) != 0) { sigemptyset(&mask); pthread_sigmask(SIG_SETMASK, &mask, NULL); FILE_FATAL_ERROR("File capture: Unable to create a " "processing thread: %s", strerror(rval)); } while (!capture_thread_running) nanosleep(&thread_sleep, NULL); sigemptyset(&mask); pthread_sigmask(SIG_SETMASK, &mask, NULL); _dpd.logMsg("File capture thread started tid=%p (pid=%u)\n", (void *) capture_thread_tid, capture_thread_pid); }
void task_user_cli( ) { char c; char cbuff_storage[CONFIG_CLI_MAX_CHARACTERS]; struct char_buffer cbuff; cbuffer_init( &cbuff, cbuff_storage, CONFIG_CLI_MAX_CHARACTERS ); //used for parsing out the first token char first_token[CONFIG_CLI_MAX_CHARACTERS]; //keep track of which track we are int track = TRACK_UNINITIALIZED; //Copy of track data struct track_node track_data[TRACK_MAX]; //for complex prints struct print_buffer pbuff; ap_init_buff( &pbuff ); //go to the CLI location, and save it Printf( COM2, "\x1B[24;20f\x1B[s" ); while( 1 ) { ap_init_buff( &pbuff ); c = Getc( COM2 ); //catch special characters switch( c ) { case CHAR_GRAVE_ACCENT: CommandOutput( "[CLI] ABORT!" ); Abort( ); break; case CHAR_BACKSPACE: if( cbuff.state == CBUFF_EMPTY ) { break; } //do nothing if empty //remove the last character cbuffer_pop_last( &cbuff ); //move the cursor back one space and delete the character Printf( COM2, "\x1B[u\x1B[D\x1B[K\x1B[s" ); break; case CHAR_PERIOD: TabRight( ); break; case CHAR_COMMA: TabLeft( ); break; case CHAR_NEWLINE: case CHAR_ENTER: if( cbuff.state == CBUFF_EMPTY ) { break; } //do nothing if empty //we need to parse, get the first token if( next_token( &cbuff, first_token ) == 0 ) { break; } //we were just spaces //now check which command the first token matches if( strcmp( first_token, "tr" ) == 0 ) { command_tr( &cbuff ); } else if( strcmp( first_token, "q" ) == 0 ) { command_q( &cbuff ); } else if( strcmp( first_token, "game" ) == 0 ) { command_game( &cbuff ); } else if( strcmp( first_token, "wh" ) == 0 ) { command_wh( &cbuff ); } else if( strcmp( first_token, "st" ) == 0 ) { command_st( &cbuff ); } else if( strcmp( first_token, "sw" ) == 0 ) { command_sw( &cbuff ); } else if( strcmp( first_token, "swa" ) == 0 ) { command_swa( &cbuff ); } else if( strcmp( first_token, "rv" ) == 0 ) { command_rv( &cbuff ); } else if( strcmp( first_token, "add" ) == 0 ) { command_add( &cbuff, track ); } else if( strcmp( first_token, "echo" ) == 0 ) { command_echo( &cbuff ); } else if( strcmp( first_token, "delay" ) == 0 ) { command_delay( &cbuff ); } else if( strcmp( first_token, "beep" ) == 0 ) { command_beep( &cbuff ); } else if( strcmp( first_token, "looptest" ) == 0 ) { command_looptest( &cbuff ); } else if( strcmp( first_token, "zombtest" ) == 0 ) { command_zombtest( &cbuff ); } else if( strcmp( first_token, "track" ) == 0 ) { command_track( &cbuff, &track, track_data ); } else if( strcmp( first_token, "sos" ) == 0 ) { command_sos( &cbuff ); } else if( strcmp( first_token, "calibrationout" ) == 0 ) { command_calibrationout( &cbuff ); } else if( strcmp( first_token, "route" ) == 0 ) { command_route( &cbuff, track_data ); } else if( strcmp( first_token, "tdir" ) == 0 ) { command_tdir( &cbuff ); } else if( strcmp( first_token, "resv" ) == 0 ) { command_resv( &cbuff, track_data ); } else if( strcmp( first_token, "free" ) == 0 ) { command_free( &cbuff, track_data ); } else { CommandOutput( "Invalid Command" ); } //empty the command buffer cbuffer_empty( &cbuff ); //clear the command line Printf( COM2, "\x1B[24;20f\x1B[K\x1B[s" ); break; default: //if it is not a valid character ignore it if( !validate_char( &c ) ) { break; } //if we are full, ignore the character if( cbuff.state == CBUFF_FULL ) { break; } //we have a valid character buffer and print it cbuffer_push_char( &cbuff, c ); Printf( COM2, "\x1B[u%c\x1B[s", c ); break; } //re-initialize the parse first_token[0] = '\0'; } }