static int mystify_timer(TWidget *wid) { if(paused){ return 0; } if(polygons.nb_items > nb_polygons){ /* We have too many polygons, we must drop some of them */ fifo_pop(&polygons); } if(nb_polygons == polygons.nb_items){ /* We have the good number of polygons, we can safely drop the last one to add the new one later */ fifo_pop(&polygons); } fifo_push(&polygons, &leading_polygon); /* * Then we update the leading polygon for the next round acording to * current move (the move may be altered in case of sreen border * collision) */ polygon_update(&leading_polygon, &move); change_color(); wid->dirty++; return 0; }
int main() { FIFO_DATA_TYPE out_val; fifo_init(&data_in_f, data_in, FIFO_SIZE); fifo_init(&data_out_f, data_out, FIFO_SIZE); fifo_push(&data_in_f, 1); fifo_push(&data_in_f, 2); fifo_push(&data_in_f, 3); fifo_push(&data_in_f, 4); fifo_push(&data_in_f, 5); if (fifo_pop(&data_in_f, &out_val)) printf("Valore: %d\n", out_val); if (fifo_pop(&data_in_f, &out_val)) printf("Valore: %d\n", out_val); if (fifo_pop(&data_in_f, &out_val)) printf("Valore: %d\n", out_val); if (fifo_pop(&data_in_f, &out_val)) printf("Valore: %d\n", out_val); if (fifo_pop(&data_in_f, &out_val)) printf("Valore: %d\n", out_val); //ft_init(); //ft_step(); return 0; }
static int bch_allocator_thread(void *arg) { struct cache *ca = arg; mutex_lock(&ca->set->bucket_lock); while (1) { /* * First, we pull buckets off of the unused and free_inc lists, * possibly issue discards to them, then we add the bucket to * the free list: */ while (1) { long bucket; if ((!atomic_read(&ca->set->prio_blocked) || !CACHE_SYNC(&ca->set->sb)) && !fifo_empty(&ca->unused)) fifo_pop(&ca->unused, bucket); else if (!fifo_empty(&ca->free_inc)) fifo_pop(&ca->free_inc, bucket); else break; if (ca->discard) { mutex_unlock(&ca->set->bucket_lock); blkdev_issue_discard(ca->bdev, bucket_to_sector(ca->set, bucket), ca->sb.block_size, GFP_KERNEL, 0); mutex_lock(&ca->set->bucket_lock); } allocator_wait(ca, bch_allocator_push(ca, bucket)); wake_up(&ca->set->bucket_wait); } /* * We've run out of free buckets, we need to find some buckets * we can invalidate. First, invalidate them in memory and add * them to the free_inc list: */ allocator_wait(ca, ca->set->gc_mark_valid && (ca->need_save_prio > 64 || !ca->invalidate_needs_gc)); invalidate_buckets(ca); /* * Now, we write their new gens to disk so we can start writing * new stuff to them: */ allocator_wait(ca, !atomic_read(&ca->set->prio_blocked)); if (CACHE_SYNC(&ca->set->sb) && (!fifo_empty(&ca->free_inc) || ca->need_save_prio > 64)) bch_prio_write(ca); } }
uint32_t alp_parse_length_operand(fifo_t* cmd_fifo) { uint8_t len = 0; fifo_pop(cmd_fifo, (uint8_t*)&len, 1); uint8_t field_len = len >> 6; if(field_len == 0) return (uint32_t)len; uint32_t full_length = (len & 0x3F) << ( 8 * field_len); // mask field length specificier bits and shift before adding other length bytes fifo_pop(cmd_fifo, (uint8_t*)&full_length, field_len); return full_length; }
int main() { struct fifo * list = fifo_create_thread_safe_fifo(); int i = 0; /*pop empty queue*/ fifo_pop(list); printf("length: %d\n",fifo_length(list)); for(;i<10;i++) { fifo_push(list,(void*)i); } printf("length: %d\n",fifo_length(list)); for(i=0;i<5;i++) { printf("%d\n",(int)fifo_pop(list)); } printf("length: %d\n",fifo_length(list)); for(i=0;i<2;i++) { fifo_push(list,(void*)(i+10)); } printf("length: %d\n",fifo_length(list)); for(i=0;i<7;i++) { printf("%d\n",(int)fifo_pop(list)); } printf("length: %d\n",fifo_length(list)); for(i=0;i<2;i++) { fifo_push(list,(void*)(i+10)); } printf("length: %d\n",fifo_length(list)); for(i=0;i<2;i++) { printf("%d\n",(int)fifo_pop(list)); } printf("length: %d\n",fifo_length(list)); fifo_delete(list,NULL); }
static void process_uart_rx_fifo() { if(fifo_get_size(&uart_rx_fifo) >= COMMAND_SIZE) { uint8_t received_cmd[COMMAND_SIZE]; fifo_pop(&uart_rx_fifo, received_cmd, COMMAND_SIZE); if(strncmp(received_cmd, COMMAND_CHAN, COMMAND_SIZE) == 0) { process_command_chan(); } else if(strncmp(received_cmd, COMMAND_TRAN, COMMAND_SIZE) == 0) { while(fifo_get_size(&uart_rx_fifo) < COMMAND_TRAN_PARAM_SIZE); char param[COMMAND_TRAN_PARAM_SIZE]; fifo_pop(&uart_rx_fifo, param, COMMAND_TRAN_PARAM_SIZE); tx_packet_delay_s = atoi(param); DPRINT("performing TRAN command with %d tx_packet_delay_s\r\n", tx_packet_delay_s); stop(); is_mode_rx = false; current_state = STATE_RUNNING; sched_post_task(&start); } else if(strncmp(received_cmd, COMMAND_RECV, COMMAND_SIZE) == 0) { DPRINT("entering RECV mode\r\n"); stop(); is_mode_rx = true; current_state = STATE_RUNNING; sched_post_task(&start); } else if(strncmp(received_cmd, COMMAND_RSET, COMMAND_SIZE) == 0) { DPRINT("resetting...\r\n"); hw_reset(); } else { char err[40]; DPRINT("ERROR invalid command %.4s\n\r", received_cmd); } fifo_clear(&uart_rx_fifo); } timer_post_task_delay(&process_uart_rx_fifo, TIMER_TICKS_PER_SEC); }
uint8_t alp_get_expected_response_length(uint8_t* alp_command, uint8_t alp_command_length) { uint8_t expected_response_length = 0; fifo_t fifo; fifo_init_filled(&fifo, alp_command, alp_command_length, alp_command_length + 1); while(fifo_get_size(&fifo) > 0) { alp_control_t control; fifo_pop(&fifo, (uint8_t*)&control.raw, 1); switch(control.operation) { case ALP_OP_READ_FILE_DATA: fifo_skip(&fifo, 1); // skip file ID alp_parse_length_operand(&fifo); // offset expected_response_length += alp_parse_length_operand(&fifo);; break; case ALP_OP_REQUEST_TAG: fifo_skip(&fifo, 1); // skip tag ID operand break; case ALP_OP_RETURN_FILE_DATA: case ALP_OP_WRITE_FILE_DATA: fifo_skip(&fifo, 1); // skip file ID alp_parse_length_operand(&fifo); // offset fifo_skip(&fifo, alp_parse_length_operand(&fifo)); break; case ALP_OP_FORWARD: ; uint8_t itf_id; fifo_pop(&fifo, &itf_id, 1); if(itf_id == ALP_ITF_ID_D7ASP) { fifo_skip(&fifo, 1); // skip QoS, dormant timeout d7ap_addressee_ctrl_t addressee_ctrl; fifo_pop(&fifo, (uint8_t*)&addressee_ctrl.raw, 1); fifo_skip(&fifo, 2 + alp_addressee_id_length(addressee_ctrl.id_type)); // skip addressee ctrl, access class // TODO refactor to reuse same logic for parsing and response length counting } // other ITFs have no configuration break; case ALP_OP_WRITE_FILE_PROPERTIES: fifo_skip(&fifo, 1 + sizeof(fs_file_header_t)); // skip file ID & header break; // TODO other operations default: DPRINT("op %i not implemented", control.operation); assert(false); } } DPRINT("Expected ALP response length=%i", expected_response_length); return expected_response_length; }
/** * Returns data to the pipeline for media processing * * Whe our downstream elements need more data, the GST framework sees to * it that this function is called so we can produce some data to give them. * For us that means taking data off of the FIFO being fed by the background * task. If it should be empty, we sit around and wait. Once data does * arrive, we take it and send it into the pipeline [we return]. * * \param psrc -> to the element context needing to produce data * \param offset \todo I don't use this, why? * \param size \todo I don't use this, why? * \param buf where the data is to be placed * \return a GST status showing if we were successful in getting data * \retval GST_FLOW_OK buffer has been loaded with data * \retval GST_FLOW_ERROR something bad has happened */ static GstFlowReturn gst_ccnxsrc_create (GstBaseSrc * psrc, /*@unused@ */ guint64 offset, /*@unused@ */ guint size, GstBuffer ** buf) { Gstccnxsrc *me; gboolean looping = TRUE; GstBuffer *ans = NULL; me = GST_CCNXSRC (psrc); GST_DEBUG ("create called"); while (looping) { GST_DEBUG ("create looping"); if (fifo_empty (me)) { msleep (50); } else { ans = fifo_pop (me); looping = FALSE; } } if (ans) { guint sz; sz = GST_BUFFER_SIZE (ans); GST_LOG_OBJECT (me, "got some data %d", sz); *buf = ans; } else { return GST_FLOW_ERROR; } GST_DEBUG ("create returning a buffer"); return GST_FLOW_OK; }
void *consumer(void *arg) { int id = (intptr_t) arg; printf("Consumer Started: %d\n", id); int sum = 0; struct timespec t = {0}; while (1) { int *ii = fifo_pop(fifo); if (ii == NULL) break; sum += *ii; free(ii); read(random_fd, &t.tv_nsec, sizeof(t.tv_nsec)); t.tv_nsec &= 0xFFFFFF; nanosleep(&t, NULL); } printf("Consumer finished: %d\n", id); return (void *)(intptr_t) sum; }
bool ancs_send_buffered_command() { if (!command_send_enable && ((millis() - last_command_send) < 1000)) { return true; } uint8_t* buffer; size_t len = fifo_pop(buffer_commands, &buffer); if (len == 0) { return false; } uint8_t cmd, aid; uint32_t uid; unpack(buffer, "BIB", &cmd, &uid, &aid); debug_print(F("[ANCS CP] Command sent 0x")); debug_print(cmd, HEX); debug_print(F(" for notification #")); debug_print(uid, DEC); debug_print(F(" attribute 0x")); debug_print(aid, HEX); debug_println(); if (lib_aci_send_data(PIPE_ANCS_CONTROL_POINT_TX_ACK, buffer, len)) { command_send_enable = false; last_command_send = millis(); } else { Serial.print(F("!! Error sending data for notification #")); Serial.println(uid, DEC); } free(buffer); return true; }
void alp_parse_action(fifo_t* fifo, alp_action_t* action) { uint8_t op; assert(fifo_pop(fifo, &op, 1) == SUCCESS); bool b6 = (op >> 6) & 1; bool b7 = op >> 7; op &= 0x3F; // op is in b5-b0 action->operation = op; switch(op) { case ALP_OP_WRITE_FILE_DATA: parse_op_write_file_data(fifo, action); break; case ALP_OP_RETURN_FILE_DATA: parse_op_return_file_data(fifo, action); break; case ALP_OP_RETURN_TAG: parse_op_return_tag(fifo, action, b6, b7); break; case ALP_OP_RETURN_STATUS: parse_op_return_status(fifo, action, b6, b7); break; default: DPRINT("op %x not implemented", op); assert(false); } DPRINT("parsed action"); }
void progressive_display_add_rect(struct xrdp_screen * self) { int i, j; struct list* update_rects = self->update_rects; struct update_rect* urect; struct update_rect* fu_rect; struct update_rect* ur; struct xrdp_rect intersection; struct update_rect* tmp; struct list* l_tmp = list_create(); l_tmp->auto_free = 1; bool no_inter = true; while (!fifo_is_empty(self->candidate_update_rects)) { fu_rect = (struct update_rect*) fifo_pop(self->candidate_update_rects); if (update_rects->count > 0) { no_inter = true; for (i = update_rects->count - 1; i >= 0; i--) { urect = (struct update_rect*) list_get_item(update_rects, i); if (!rect_equal(&urect->rect, &fu_rect->rect)) { if (rect_intersect(&urect->rect, &fu_rect->rect, &intersection)) { no_inter = false; progressive_display_rect_union(fu_rect, urect, l_tmp); list_remove_item(update_rects, i); for (j = 0; j < l_tmp->count; j++) { ur = (struct update_rect*) list_get_item(l_tmp, j); tmp = (struct update_rect*) g_malloc(sizeof(struct update_rect), 0); g_memcpy(tmp, ur, sizeof(struct update_rect)); fifo_push(self->candidate_update_rects, tmp); } list_clear(l_tmp); break; } } else { no_inter = false; urect->quality = fu_rect->quality; urect->quality_already_send = fu_rect->quality_already_send; break; } } if (no_inter) { list_add_progressive_display_rect(update_rects, fu_rect->rect.left, fu_rect->rect.top, fu_rect->rect.right, fu_rect->rect.bottom, fu_rect->quality, fu_rect->quality_already_send); } } else { list_add_progressive_display_rect(update_rects, fu_rect->rect.left, fu_rect->rect.top, fu_rect->rect.right, fu_rect->rect.bottom, fu_rect->quality, fu_rect->quality_already_send); } } list_delete(l_tmp); }
int fifo_num_rem(struct fifo_list *f, unsigned num, TYPE **data) { unsigned count, pos, start, end; TYPE **ldata; if (num == 0) return fifo_pop(f, data); count = FIFO_COUNT(f); if (num > count) { errno = ENOENT; return -1; } pos = (num + f->fifo_start) % f->fifo_size; start = f->fifo_start; end = f->fifo_end; ldata = f->fifo_data; if (start < pos) { (void)memmove(ldata + start + 1, ldata + start, FIFO_DATA_SIZE(pos - start)); f->fifo_start++; } else { (void)memmove(ldata + pos, ldata + pos + 1, FIFO_DATA_SIZE(end - pos)); f->fifo_end--; } if (FIFO_COUNT(f) < (f->fifo_size / 2)) fifo_decr(f); return 0; }
/** * @brief TIM period elapsed callback * @param htim: TIM handle * @retval None */ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(USBD_CDC_PACKET_SEND_OK == true) { USBD_CDC_PACKET_BYTES_COUNT = 0; for(; USBD_CDC_PACKET_BYTES_COUNT < APP_RX_DATA_SIZE; USBD_CDC_PACKET_BYTES_COUNT++) { fifo_pop_return_t data_return = fifo_pop(usb_cdc_dev_tx_fifo); if(data_return.status == false) break; UserTxBuffer[USBD_CDC_PACKET_BYTES_COUNT] = data_return.character; } } if(USBD_CDC_PACKET_BYTES_COUNT == 0) return; USBD_CDC_SetTxBuffer(&usb_cdc_dev_param, UserTxBuffer, USBD_CDC_PACKET_BYTES_COUNT); if(USBD_CDC_TransmitPacket(&usb_cdc_dev_param) == USBD_OK) { USBD_CDC_PACKET_SEND_OK = true; } else USBD_CDC_PACKET_SEND_OK = false; //} }
/*** * The thread pool has the property that all the threads will keep running until signalled to quit. * Each thread will call pthread_exit() once it has completed execution of it's current 'task_t'. */ void thread_pool_join_all( thread_pool_t *pool ) { dna_log(DEBUG, "Joining thread pool:"); while ( !fifo_is_empty(pool->thread_queue) ) { dna_mutex_lock( pool->mutex ); int threadsAreStillRunning = 0; while ( (threadsAreStillRunning = fifo_any( pool->thread_queue, &thread_context_is_running)) ) { dna_log(DEBUG, "Some threads are still running...%i", threadsAreStillRunning); dna_cond_wait( pool->wait, pool->mutex ); } dna_thread_context_t *context = (dna_thread_context_t*) fifo_pop( pool->thread_queue ); if (context->runstate == RUNNING) { dna_log(WARN, "context is still running, placing back in the queue."); fifo_push( pool->thread_queue, context ); } else { // if the context is != RUNNING, it's either SHOULD_QUIT or HAS_QUIT // so we have to rely on the null work we pushed earlier to clear // any blocks dna_thread_context_join( context ); dna_thread_context_destroy(context); } dna_mutex_unlock( pool->mutex ); } dna_log(DEBUG, "Thread pool joined."); }
//in: linda_ud key mincount [maxcount] int keepercall_receive_batched( lua_State* L) { int const min_count = (int) lua_tointeger( L, 3); if( min_count > 0) { keeper_fifo* fifo; int const max_count = (int) luaL_optinteger( L, 4, min_count); lua_settop( L, 2); // ud key lua_insert( L, 1); // key ud push_table( L, 2); // key ud fifos lua_remove( L, 2); // key fifos lua_pushvalue( L, 1); // key fifos key lua_rawget( L, 2); // key fifos fifo lua_remove( L, 2); // key fifo fifo = prepare_fifo_access( L, 2); // key fifo if( fifo != NULL && fifo->count >= min_count) { fifo_pop( L, fifo, __min( max_count, fifo->count)); // key ... } else { lua_settop( L, 0); } return lua_gettop( L); } else { return 0; } }
/** * Check if any work has appeared in the queue; work it if there * * The background task spends time waiting for something to do. * One of the places where work comes from is via the fifo queue, * which will contain data buffers that must be sent out over the ccn network. * This function will look for work and get it done if present. * Not too much work is donw however, since there are other things to be * done by the background task. Hence we limit the number of buffers will * will process from the queue. * We shall return soon enough to this spot to keep working the queue contents. * * \param me context sink element where the fifo queues are allocated */ static void check_fifo (Gstccnxsink * me) { GstClockTime ts; gint i; guint size; guint8 *data; GstBuffer *buffer; for (i = 0; i < 3; ++i) { if (fifo_empty (me)) return; if (!(buffer = fifo_pop (me))) return; size = GST_BUFFER_SIZE (buffer); data = GST_BUFFER_DATA (buffer); ts = 0; GST_INFO ("CCNxSink: pubish size: %d\n", size); if (0 == ts || GST_CLOCK_TIME_NONE == ts) ts = me->ts; if (0 == ts || GST_CLOCK_TIME_NONE == ts) { ts = tNow (); me->ts = ts; } GST_INFO ("CCNxSink: pubish time: %0X\n", ts); gst_ccnxsink_send (me, data, size, ts); gst_buffer_unref (buffer); } }
void process_uart_rx_fifo() { if(fifo_get_size(&uart_rx_fifo) >= COMMAND_SIZE) { uint8_t received_cmd[COMMAND_SIZE]; fifo_pop(&uart_rx_fifo, received_cmd, COMMAND_SIZE); if(strncmp(received_cmd, COMMAND_CHAN, COMMAND_SIZE) == 0) { process_command_chan(); } else if(strncmp(received_cmd, COMMAND_LOOP, COMMAND_SIZE) == 0) { process_command_loop(); } else if(strncmp(received_cmd, COMMAND_RSET, COMMAND_SIZE) == 0) { hw_reset(); } else { char err[40]; snprintf(err, sizeof(err), "ERROR invalid command %.4s\n", received_cmd); console_print(err); } fifo_clear(&uart_rx_fifo); } }
/* Pop an operation off the queue for tile @index * The user of this function is reponsible for freeing the result using free() * * Concurrency: This function is reentrant (and lock-free) on different @index */ OperationDataDrawDab * operation_queue_pop(OperationQueue *self, TileIndex index) { OperationDataDrawDab *op = NULL; if (!tile_map_contains(self->tile_map, index)) { return NULL; } Fifo **queue_pointer = tile_map_get(self->tile_map, index); Fifo *op_queue = *queue_pointer; if (!op_queue) { return NULL; } op = fifo_pop(op_queue); if (!op) { // Queue empty fifo_free(op_queue, operation_delete_func); *queue_pointer = NULL; return NULL; } else { assert(op != NULL); return op; } }
bool serial_handler_command_get(serial_cmd_t* cmd) { /* want to do this without being pre-empted, as there's a potential race condition */ NVIC_DisableIRQ(SPI1_TWI1_IRQn); enable_pin_listener(false); serial_data_t temp; if (fifo_pop(&rx_fifo, &temp) != NRF_SUCCESS) { enable_pin_listener(true); NVIC_EnableIRQ(SPI1_TWI1_IRQn); return false; } if (temp.buffer[SERIAL_LENGTH_POS] > 0) { memcpy(cmd, temp.buffer, temp.buffer[SERIAL_LENGTH_POS] + 1); } /* just made room in the queue */ if (serial_state == SERIAL_STATE_WAIT_FOR_QUEUE) { /* would have race condition here with several irq contexts */ do_transmit(); } else if (serial_state == SERIAL_STATE_IDLE) { /* only want GPIOTE IRQ in IDLE */ enable_pin_listener(true); } NVIC_EnableIRQ(SPI1_TWI1_IRQn); return true; }
static unsigned char read_byte(void) { unsigned char has_read = 0; unsigned char c = 0; while (!has_read) { int_wait(); fifo_lock(&gfifo); if (gfifo.size) { c = fifo_pop(&gfifo); has_read = 1; } else if (gfifo.error) { has_read = 1; } fifo_unlock(&gfifo); } return c; }
static void arp_pack_func(void** state) { // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x10c37b9309d1; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); uint32_t spa = 0xc0a80a6f; uint32_t dpa = 0xc0a80a90; arp_request(__nics[0], dpa, spa); Packet* packet = fifo_pop(__nics[0]->output_buffer); uint32_t comp_size = packet->end; packet->end = 0; arp_pack(packet); // Checking packet->end assert_int_equal(comp_size, packet->end); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
// in: linda_ud, key [, key]? // out: (key, val) or nothing int keepercall_receive( lua_State* L) { int top = lua_gettop( L); int i; push_table( L, 1); // ud keys fifos lua_replace( L, 1); // fifos keys for( i = 2; i <= top; ++ i) { keeper_fifo* fifo; lua_pushvalue( L, i); // fifos keys key[i] lua_rawget( L, 1); // fifos keys fifo fifo = prepare_fifo_access( L, -1); // fifos keys fifo if( fifo != NULL && fifo->count > 0) { fifo_pop( L, fifo, 1); // fifos keys val if( !lua_isnil( L, -1)) { lua_replace( L, 1); // val keys lua_settop( L, i); // val keys key[i] if( i != 2) { lua_replace( L, 2); // val key keys lua_settop( L, 2); // val key } lua_insert( L, 1); // key, val return 2; } } lua_settop( L, top); // data keys } // nothing to receive return 0; }
static void arp_announce_func(void** state) { // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x10c37b9309d1; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); uint32_t spa = 0xc0a80a6f; arp_announce(__nics[0], spa); Packet* packet = fifo_pop(__nics[0]->output_buffer); assert_memory_equal(packet->buffer + packet->start, arp_announce_packet, 46); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }
static void process_uart_rx_fifo() { if(fifo_get_size(&uart_rx_fifo) >= COMMAND_SIZE) { uint8_t received_cmd[COMMAND_SIZE]; fifo_pop(&uart_rx_fifo, received_cmd, COMMAND_SIZE); if(strncmp(received_cmd, COMMAND_CHAN, COMMAND_SIZE) == 0) { process_command_chan(); } else if(strncmp(received_cmd, COMMAND_TRAN, COMMAND_SIZE) == 0) { while(fifo_get_size(&uart_rx_fifo) < COMMAND_TRAN_PARAM_SIZE); char param[COMMAND_TRAN_PARAM_SIZE]; fifo_pop(&uart_rx_fifo, param, COMMAND_TRAN_PARAM_SIZE); tx_packet_delay_s = atoi(param); stop(); is_mode_rx = false; current_state = STATE_RUNNING; sched_post_task(&start); } else if(strncmp(received_cmd, COMMAND_RECV, COMMAND_SIZE) == 0) { stop(); is_mode_rx = true; current_state = STATE_RUNNING; sched_post_task(&start); } else if(strncmp(received_cmd, COMMAND_RSET, COMMAND_SIZE) == 0) { hw_reset(); } else { char err[40]; snprintf(err, sizeof(err), "ERROR invalid command %.4s\n", received_cmd); uart_transmit_string(err); } fifo_clear(&uart_rx_fifo); } timer_post_task_delay(&process_uart_rx_fifo, TIMER_TICKS_PER_SEC); }
void test2() { struct fifo p1; int buf1[256]; int elem; int output_buf[16]; int test_arr[19]; size_t i; int input; srand(time(NULL)); for(i = 0; i < ARRAY_SIZE(test_arr); i++) { test_arr[i] = rand(); } diag("++++test2++++"); fifo_init(&p1, buf1, sizeof(int), ARRAY_SIZE(buf1) ); ok1(fifo_amt(&p1) == 0); ok1(fifo_avail(&p1) == ARRAY_SIZE(buf1)); input = 345; fifo_push(&p1, &input); ok1(fifo_amt(&p1) == 1); ok1(fifo_avail(&p1) == (ARRAY_SIZE(buf1)-1)); fifo_top(&p1, &elem); ok1(elem == 345); elem = 0; fifo_write(&p1, test_arr, 7); ok1(fifo_amt(&p1) == 1+7); ok1(fifo_avail(&p1) == (ARRAY_SIZE(buf1)-1-7)); fifo_pop(&p1, &elem); ok1(elem == 345); ok1(fifo_amt(&p1) == 7); ok1(fifo_avail(&p1) == (ARRAY_SIZE(buf1)-7)); fifo_pop(&p1, &elem); ok1(elem == test_arr[0]); ok1(fifo_amt(&p1) == 6); ok1(fifo_avail(&p1) == (ARRAY_SIZE(buf1)-6)); fifo_consume(&p1, output_buf, 6); ok1(memcmp(test_arr+1, output_buf, 6*sizeof(int)) == 0); ok1(fifo_amt(&p1) == 0); ok1(fifo_avail(&p1) == (ARRAY_SIZE(buf1))); #define TEST2AMT 2 + 3 + 11 diag("----test2----\n#"); }
// TODO code duplication with noise_test, refactor later static void process_command_chan() { while(fifo_get_size(&uart_rx_fifo) < COMMAND_CHAN_PARAM_SIZE); char param[COMMAND_CHAN_PARAM_SIZE]; fifo_pop(&uart_rx_fifo, param, COMMAND_CHAN_PARAM_SIZE); channel_id_t new_channel; if(strncmp(param, "433", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_433; else if(strncmp(param, "868", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_868; else if(strncmp(param, "915", 3) == 0) new_channel.channel_header.ch_freq_band = PHY_BAND_915; else goto error; char channel_class = param[3]; if(channel_class == 'L') new_channel.channel_header.ch_class = PHY_CLASS_LO_RATE; else if(channel_class == 'N') new_channel.channel_header.ch_class = PHY_CLASS_NORMAL_RATE; else if(channel_class == 'H') new_channel.channel_header.ch_class = PHY_CLASS_HI_RATE; else goto error; uint16_t center_freq_index = atoi((const char*)(param + 5)); new_channel.center_freq_index = center_freq_index; // validate if(new_channel.channel_header.ch_freq_band == PHY_BAND_433) { if(new_channel.channel_header.ch_class == PHY_CLASS_NORMAL_RATE && (new_channel.center_freq_index % 8 != 0 || new_channel.center_freq_index > 56)) goto error; else if(new_channel.channel_header.ch_class == PHY_CLASS_LO_RATE && new_channel.center_freq_index > 68) goto error; else if(new_channel.channel_header.ch_class == PHY_CLASS_HI_RATE) goto error; } // TODO validate PHY_BAND_868 // valid band, apply ... current_channel_id = new_channel; char str[20]; channel_id_to_string(¤t_channel_id, str, sizeof(str)); uart_transmit_string(str); // change channel and restart // TODOsched_post_task(&start_rx); return; error: uart_transmit_string("Error parsing CHAN command. Expected format example: '433L001'\n"); fifo_clear(&uart_rx_fifo); }
void TXE_Handler() { if(!fifo_is_empty(&Serial.fifo_output)) { USART_WriteByte(fifo_pop(&Serial.fifo_output)); } else { USART_DisableInterrupts(USART_IT_TXE); } }
static void uart_txStart(struct SerialHardware * _hw) { struct EmulSerial *hw = (struct EmulSerial *)_hw; while(!fifo_isempty(&hw->ser->txfifo)) { char c = fifo_pop(&hw->ser->txfifo); write(hw->fd, &c, 1); } }
static void arp_process_func(void** state) { // Timer setting timer_init("IntelCore(TM) i5-4670 CPU @ 3.40GHz"); // Nic initialization void* malloc_pool = malloc(POOL_SIZE); init_memory_pool(POOL_SIZE, malloc_pool, 0); __nics[0] = malloc(sizeof(NIC)); __nic_count++; __nics[0]->mac = 0x74d4358f66cb; __nics[0]->pool_size = POOL_SIZE; __nics[0]->pool = malloc_pool; __nics[0]->output_buffer = fifo_create(8, malloc_pool); __nics[0]->config = map_create(8, NULL, NULL, __nics[0]->pool); // Arp request Packet* packet = nic_alloc(__nics[0], sizeof(arp_request_packet)); memcpy(packet->buffer + packet->start, arp_request_packet, sizeof(arp_request_packet)); Ether* ether = (Ether*)(packet->buffer + packet->start); ARP* arp = (ARP*)ether->payload; uint32_t addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); assert_true(arp_process(packet)); packet = fifo_pop(__nics[0]->output_buffer); assert_memory_equal(packet->buffer + packet->start, arp_reply_packet, 42); nic_free(packet); packet = NULL; // Arp response packet = nic_alloc(__nics[0], sizeof(arp_reply_packet)); memcpy(packet->buffer + packet->start, arp_reply_packet, sizeof(arp_reply_packet)); ether = (Ether*)(packet->buffer + packet->start); arp = (ARP*)ether->payload; addr = endian32(arp->tpa); nic_ip_add(__nics[0], addr); uint8_t comp_mac[6] = { 0xcb, 0x66, 0x8f, 0x35, 0xd4, 0x74 }; uint32_t sip = endian32(arp->spa); Map* arp_table = nic_config_get(packet->nic, "net.arp.arptable"); assert_true(arp_process(packet)); ARPEntity* entity = map_get(arp_table, (void*)(uintptr_t)sip); assert_memory_equal((uint8_t*)&entity->mac, comp_mac, 6); destroy_memory_pool(malloc_pool); free(malloc_pool); malloc_pool = NULL; }