/*---------------------------------------------------------------------------*/ int simple_udp_register(struct simple_udp_connection *c, uint16_t local_port, uip_ipaddr_t *remote_addr, uint16_t remote_port, simple_udp_callback receive_callback) { init_simple_udp(); c->local_port = local_port; c->remote_port = remote_port; if(remote_addr != NULL) { uip_ipaddr_copy(&c->remote_addr, remote_addr); } c->receive_callback = receive_callback; PROCESS_CONTEXT_BEGIN(&simple_udp_process); c->udp_conn = udp_new(remote_addr, UIP_HTONS(remote_port), c); if(c->udp_conn != NULL) { udp_bind(c->udp_conn, UIP_HTONS(local_port)); } PROCESS_CONTEXT_END(); if(c->udp_conn == NULL) { return 0; } return 1; }
PROCESS_THREAD(ctimer_process, ev, data) { struct ctimer *c; PROCESS_BEGIN(); for(c = list_head(ctimer_list); c != NULL; c = c->next) { etimer_set(&c->etimer, c->etimer.tmr.interval); } initialized = 1; while(1) { PROCESS_YIELD_UNTIL(ev == PROCESS_EVENT_TIMER); for(c = list_head(ctimer_list); c != NULL; c = c->next) { if(&c->etimer == data) { list_remove(ctimer_list, c); PROCESS_CONTEXT_BEGIN(c->p); if(c->f != NULL) { c->f(c->ptr); } PROCESS_CONTEXT_END(c->p); break; } } } PROCESS_END(); }
static void run_window_events(windowproc window, struct _event *events) { struct _event *ev = events; PROCESS_CONTEXT_BEGIN(ui_process); int ret = window(ev->event, ev->lparam, ev->rparam); //GrContextFontSet(&context, (const tFont*)NULL); if (ret != 0x80) { status_process(EVENT_WINDOW_PAINT, 0, &context); GrContextClipRegionSet(&context, &client_clip); } else GrContextClipRegionSet(&context, &fullscreen_clip); ev++; for(; ev->delta != -1; ev++) { window(ev->event, ev->lparam, ev->rparam); if (ev->event == EVENT_WINDOW_PAINT) { GrFlush(&context); } } PROCESS_CONTEXT_END(); }
//APP Callback function static void app_recv(void) { //printf("Received from RDC\n"); PROCESS_CONTEXT_BEGIN(&null_app_process); uint8_t *data = packetbuf_dataptr(); uint8_t flag = 0; int i; rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); uint8_t rx_sn_id = sent_sn_addr->u8[0]; uint8_t pkt_seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); uint8_t payload_len = packetbuf_datalen(); // printf("%u,%u,%u%c",rx_sn_id,pkt_seq,payload_len,'|'); // for(i=0;i<payload_len;i++){ // printf("%02x",data[i]); // } // printf("\n"); app_output(data,rx_sn_id,pkt_seq,payload_len); PROCESS_CONTEXT_END(&null_app_process); }
/*---------------------------------------------------------------------------*/ static void start_timeout_timer(struct http_socket *s) { PROCESS_CONTEXT_BEGIN(&http_socket_process); etimer_set(&s->timeout_timer, HTTP_SOCKET_TIMEOUT); PROCESS_CONTEXT_END(&http_socket_process); s->timeout_timer_started = 1; }
//APP Callback function static void app_recv(void) { PROCESS_CONTEXT_BEGIN(&null_app_process); #ifdef SF_MOTE_TYPE_AP if(disable_sending == 0) return; mpu_data_acc_gyro_union data[SAMPLES_PER_FRAME]; // TX info uint8_t i; rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); uint8_t rx_sn_id = sent_sn_addr->u8[0]; if (rx_sn_id != MY_SN_ID) return; uint8_t pkt_seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); //DATA_SIZE; uint8_t payload_len = packetbuf_datalen(); if(payload_len != MPU_DATA_ACC_GYRO_SIZE*SAMPLES_PER_FRAME) { //printf("Ignore packet %u,%u %u\n",rx_sn_id,pkt_seq,payload_len); //return; memcpy((uint8_t*)data,packetbuf_dataptr(),MPU_DATA_ACC_GYRO_SIZE*SAMPLES_PER_FRAME); } else { memcpy((uint8_t*)data,packetbuf_dataptr(),MPU_DATA_ACC_GYRO_SIZE*SAMPLES_PER_FRAME); } for(i=0;i<SAMPLES_PER_FRAME;i++) { // MPU data mpu_data_acc_gyro_union samples = data[i]; // MPU_PRINT_BYTE(rx_sn_id); // MPU_PRINT_BYTE(0); // MPU_PRINT_BYTE(pkt_seq); // MPU_PRINT_BYTE(0); // print_mpu_sample_acc_gyro(&samples); // MPU_PRINT_BYTE('\n'); printf("%d,%d,%d,%d\n",pkt_seq,samples.data.x,samples.data.y,samples.data.z); } #endif PROCESS_CONTEXT_END(&null_app_process); }
/*---------------------------------------------------------------------------*/ int tcp_socket_unlisten(struct tcp_socket *s) { PROCESS_CONTEXT_BEGIN(&tcp_socket_process); tcp_unlisten(uip_htons(s->listen_port)); PROCESS_CONTEXT_END(); s->listen_port = 0; s->flags &= ~TCP_SOCKET_FLAGS_LISTENING; return 0; }
/*---------------------------------------------------------------------------*/ int tcp_socket_listen(struct tcp_socket *s, uint16_t port) { s->listen_port = port; PROCESS_CONTEXT_BEGIN(&tcp_socket_process); tcp_listen(uip_htons(port)); PROCESS_CONTEXT_END(); s->flags |= TCP_SOCKET_FLAGS_LISTENING; return 0; }
/*---------------------------------------------------------------------------*/ int tcp_socket_listen(struct tcp_socket *s, uint16_t port) { s->listen_port = port; PROCESS_CONTEXT_BEGIN(&tcp_socket_process); tcp_listen(uip_htons(port)); PROCESS_CONTEXT_END(); s->is_listening = 1; return 0; }
/*---------------------------------------------------------------------------*/ void ctimer_restart(struct ctimer *c) { if(initialized) { PROCESS_CONTEXT_BEGIN(&ctimer_process); etimer_restart(&c->etimer); PROCESS_CONTEXT_END(&ctimer_process); } list_remove(ctimer_list, c); list_add(ctimer_list, c); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(udp_socket_process, ev, data, buf, user_data) { struct udp_socket *c; PROCESS_BEGIN(); while(1) { PROCESS_WAIT_EVENT(); if(ev == tcpip_event) { /* An appstate pointer is passed to use from the IP stack through the 'data' pointer. We registered this appstate when we did the udp_new() call in udp_socket_register() as the struct udp_socket pointer. So we extract this pointer and use it when calling the reception callback. */ c = (struct udp_socket *)data; /* Defensive coding: although the appstate *should* be non-null here, we make sure to avoid the program crashing on us. */ if(c != NULL) { /* If we were called because of incoming data, we should call the reception callback. */ if(uip_newdata(buf)) { /* Copy the data from the uIP data buffer into our own buffer to avoid the uIP buffer being messed with by the callee. */ #if 0 /* Note that we cannot do this as the stack is suppose to be * re-entrant. */ memcpy(bad_buf, uip_appdata(buf), uip_datalen(buf)); #endif /* Call the client process. We use the PROCESS_CONTEXT mechanism to temporarily switch process context to the client process. */ if(c->input_callback != NULL) { PROCESS_CONTEXT_BEGIN(c->p); c->input_callback(c, c->ptr, &(UIP_IP_BUF(buf)->srcipaddr), UIP_HTONS(UIP_IP_BUF(buf)->srcport), &(UIP_IP_BUF(buf)->destipaddr), UIP_HTONS(UIP_IP_BUF(buf)->destport), uip_buf(buf), uip_datalen(buf)); PROCESS_CONTEXT_END(); } } } } } PROCESS_END(); }
//APP Callback function static void app_recv(void) { PROCESS_CONTEXT_BEGIN(&null_app_process); uint8_t *data = packetbuf_dataptr(); rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); uint8_t rx_sn_id = sent_sn_addr->u8[0]; uint8_t pkt_seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); uint8_t payload_len = packetbuf_datalen(); /* uart1_writeb(rx_sn_id); uart1_writeb(pkt_seq); uart1_writeb(payload_len); */ if(node_id != 0) { printf("%u,%u,%u\n",rx_sn_id,pkt_seq,payload_len); } else { //printf("%u,%u,%u,",rx_sn_id,pkt_seq,payload_len); putchar(rx_sn_id); putchar(pkt_seq); putchar(payload_len); //printf("%u",rx_sn_id); uint8_t i = 0; for(i = 0; i < (payload_len); i++) { //printf("%d,",((data[2*i+1]<<8)|data[2*i])); putchar(data[i]); } } /* for(i = 0; i < payload_len; i++) { uart1_writeb(data[i]); } */ uart1_writeb('\n'); PROCESS_CONTEXT_END(&null_app_process); }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(simple_udp_process, ev, data) { struct simple_udp_connection *c; PROCESS_BEGIN(); while(1) { PROCESS_WAIT_EVENT(); if(ev == tcpip_event) { /* An appstate pointer is passed to use from the IP stack through the 'data' pointer. We registered this appstate when we did the udp_new() call in simple_udp_register() as the struct simple_udp_connection pointer. So we extract this pointer and use it when calling the reception callback. */ c = (struct simple_udp_connection *)data; /* Defensive coding: although the appstate *should* be non-null here, we make sure to avoid the program crashing on us. */ if(c != NULL) { /* If we were called because of incoming data, we should call the reception callback. */ if(uip_newdata()) { /* Copy the data from the uIP data buffer into our own buffer to avoid the uIP buffer being messed with by the callee. */ memcpy(databuffer, uip_appdata, uip_datalen()); /* Call the client process. We use the PROCESS_CONTEXT mechanism to temporarily switch process context to the client process. */ if(c->receive_callback != NULL) { PROCESS_CONTEXT_BEGIN(c->client_process); c->receive_callback(c, &(UIP_IP_BUF->srcipaddr), UIP_HTONS(UIP_IP_BUF->srcport), &(UIP_IP_BUF->destipaddr), UIP_HTONS(UIP_IP_BUF->destport), databuffer, uip_datalen()); PROCESS_CONTEXT_END(); } } } } } PROCESS_END(); }
/*---------------------------------------------------------------------------*/ int tcp_socket_connect(struct tcp_socket *s, uip_ipaddr_t *ipaddr, uint16_t port) { struct uip_conn *c; PROCESS_CONTEXT_BEGIN(&tcp_socket_process); c = tcp_connect(ipaddr, uip_htons(port), s); PROCESS_CONTEXT_END(); if(c == NULL) { return 0; } else { return 1; } }
/*---------------------------------------------------------------------------*/ void ctimer_set(struct ctimer *c, clock_time_t t, void (*f)(void *), void *ptr) { PRINTF("ctimer_set %p %u\n", c, (unsigned)t); c->p = PROCESS_CURRENT(); c->f = f; c->ptr = ptr; if(initialized) { PROCESS_CONTEXT_BEGIN(&ctimer_process); etimer_set(&c->etimer, t); PROCESS_CONTEXT_END(&ctimer_process); } else { c->etimer.tmr.interval = t; } list_remove(ctimer_list, c); list_add(ctimer_list, c); }
/*---------------------------------------------------------------------------*/ int tcp_socket_connect(struct tcp_socket *s, const uip_ipaddr_t *ipaddr, uint16_t port) { if(s == NULL) { return -1; } if(s->c != NULL) { tcp_markconn(s->c, NULL); } PROCESS_CONTEXT_BEGIN(&tcp_socket_process); s->c = tcp_connect(ipaddr, uip_htons(port), s); PROCESS_CONTEXT_END(); if(s->c == NULL) { return -1; } else { return 1; } }
int knot_register_controller(struct process *client_proc, knot_callback callback, uint16_t rate, char controller_name[], uint8_t device_role, uint8_t device_type){ ChannelState *state = &home_channel_state; if (started == 0){ process_start(&knot_controller_process,NULL); PROCESS_CONTEXT_BEGIN(&knot_controller_process); init_table(); init_knot_network(); init_home_channel(); PROCESS_CONTEXT_END(); started = 1; } strcpy(controller_name, controller_name); if (client_proc){ state->ccb.callback = callback; state->ccb.client_process = client_proc; } else return -2; state->rate = rate; service_search(state, device_type); return 1; }
/*---------------------------------------------------------------------------*/ int udp_socket_register(struct udp_socket *c, void *ptr, udp_socket_input_callback_t input_callback) { init(); if(c == NULL) { return -1; } c->ptr = ptr; c->input_callback = input_callback; c->p = PROCESS_CURRENT(); PROCESS_CONTEXT_BEGIN(&udp_socket_process); c->udp_conn = udp_new(NULL, 0, c); PROCESS_CONTEXT_END(); if(c->udp_conn == NULL) { return -1; } return 1; }
/*---------------------------------------------------------------------------*/ void servreg_hack_register(servreg_hack_id_t id, const uip_ipaddr_t *addr) { static servreg_hack_item_t *t; static struct servreg_hack_registration *r; /* Walk through list, see if we already have a service ID registered. If not, allocate a new registration and put it on our list. If we cannot allocate a service registration, we reuse one from the service registrations made by others. */ servreg_hack_init(); for(t = list_head(own_services); t != NULL; t = list_item_next(t)) { if(servreg_hack_item_id(t) == id) { return; } } r = memb_alloc(®istrations); if(r == NULL) { printf("servreg_hack_register: error, could not allocate memory, should reclaim another registration but this has not been implemented yet.\n"); return; } r->id = id; r->seqno = 1; uip_ipaddr_copy(&r->addr, addr); timer_set(&r->timer, LIFETIME / 2); list_push(own_services, r); PROCESS_CONTEXT_BEGIN(&servreg_hack_process); etimer_set(&sendtimer, random_rand() % (NEW_REG_TIME)); PROCESS_CONTEXT_END(&servreg_hack_process); }
PROCESS_THREAD( timer_process, ev, data ) { PROCESS_BEGIN(); while (1) { PROCESS_YIELD_UNTIL( ev == PROCESS_EVENT_TIMER ); for ( int i = 0; i < MAX_REGISTERED_TIMER; i++ ) { struct timer_item *c = &timer_items[i]; if ( &c->etimer == data ) { PROCESS_CONTEXT_BEGIN( c->p ); if ( c->cb ) c->cb( c->ptr ); PROCESS_CONTEXT_END( c->p ); c->cb = contiki_timer_delegate_t(); break; } } } PROCESS_END(); }
/** * \brief Makes a resource available under the given URI path * \param resource A pointer to a resource implementation * \param path The URI path string for this resource * * The resource implementation must be imported first using the * extern keyword. The build system takes care of compiling every * *.c file in the ./resources/ sub-directory (see example Makefile). */ void rest_activate_resource(resource_t *resource, char *path) { resource->url = path; list_add(restful_services, resource); PRINTF("Activating: %s\n", resource->url); /* Only add periodic resources with a periodic_handler and a period > 0. */ if(resource->flags & IS_PERIODIC && resource->periodic->periodic_handler && resource->periodic->period) { PRINTF("Periodic resource: %p (%s)\n", resource->periodic, resource->periodic->resource->url); list_add(restful_periodic_services, resource->periodic); if(process_is_running(&rest_engine_process)) { PRINTF("Periodic: Set timer for /%s to %lu\n", resource->url, resource->periodic->period); PROCESS_CONTEXT_BEGIN(&rest_engine_process); etimer_set(&resource->periodic->periodic_timer, resource->periodic->period); PROCESS_CONTEXT_END(&rest_engine_process); } } }
//APP Callback function static void app_recv(void) { PROCESS_CONTEXT_BEGIN(&null_app_process); #ifdef SF_MOTE_TYPE_AP if(disable_sending == 0) return; //uint8_t *data = packetbuf_dataptr(); rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); uint8_t rx_sn_id = sent_sn_addr->u8[0]; uint8_t pkt_seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); uint8_t payload_len = packetbuf_datalen(); uint8_t num_sample = (uint8_t)(payload_len/MPU_DATA_ACC_GYRO_SIZE); //only print my information if (rx_sn_id != MY_SN_ID) return; /* If this node is base station, then print out received messages. The current print function prints the output in ASCII code because the execution time can be reduced significantly. If the execution time is not a constraint, then printf should work as well. Please see readUSB_MPU6050_BS.py */ mpu_data_acc_gyro_union SensorData[40]; memcpy((uint8_t*)SensorData,packetbuf_dataptr(),num_sample*MPU_DATA_ACC_GYRO_SIZE); uint8_t i = 0; for(i = 0; i < num_sample; i++) { printf("%u,%d,%d,%d",pkt_seq,SensorData[i].data.x,SensorData[i].data.y,SensorData[i].data.z); uart1_writeb('\n'); } // uart1_writeb('\n'); // uart1_writeb('\n'); #endif #ifdef SF_MOTE_TYPE_SENSOR rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); uint8_t rx_sn_id = sent_sn_addr->u8[0]; uint8_t pkt_seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); uint8_t payload_len = packetbuf_datalen(); if(rx_sn_id == 0) { printf("RX packet %u and %u bytes from BS\n",pkt_seq,payload_len); } #endif PROCESS_CONTEXT_END(&null_app_process); }
//APP Callback function static void app_recv(void) { PROCESS_CONTEXT_BEGIN(&null_app_process); uint8_t *data = packetbuf_dataptr(); rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); uint8_t rx_sn_id = sent_sn_addr->u8[0]; uint8_t pkt_seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); uint8_t payload_len = packetbuf_datalen(); /* If this node is base station, then print out received messages. The current print function prints the output in ASCII code because the execution time can be reduced significantly. If the execution time is not a constraint, then printf should work as well. Please see readUSB_MPU6050_BS.py */ if(node_id == 0) { //start byte putchar(126); putchar(126); // packet information packet_counter = packet_counter + 1; packet_counter = ((packet_counter == 10) ? 11 : packet_counter); putchar(rx_sn_id); putchar(packet_counter); putchar(pkt_seq); putchar(payload_len); //putchar(0); // //printf("%u",rx_sn_id); uint8_t i = 0; for(i = 0; i < (payload_len); i++) { //printf("%d,",((data[2*i+1]<<8)|data[2*i])); putchar(data[i]); } } uart1_writeb('\n'); uart1_writeb('\n'); PROCESS_CONTEXT_END(&null_app_process); }
//APP Callback function static void app_recv(void) { //printf("Received from RDC\n"); PROCESS_CONTEXT_BEGIN(&null_app_process); //int16_t *data = (int16_t*)packetbuf_dataptr(); #ifdef SF_MOTE_TYPE_AP mpu_data_union data[SAMPLES_PER_FRAME]; //int8_t *raw_data = (int8_t *)packetbuf_dataptr(); uint8_t i; rimeaddr_t *sent_sn_addr = packetbuf_addr(PACKETBUF_ADDR_SENDER); uint8_t rx_sn_id = sent_sn_addr->u8[0]; uint8_t pkt_seq = packetbuf_attr(PACKETBUF_ATTR_PACKET_ID); //DATA_SIZE; uint8_t payload_len = packetbuf_datalen(); if(payload_len != MPU_DATA_SIZE*SAMPLES_PER_FRAME) { printf("Ignore packet %u,%u %u\n",rx_sn_id,pkt_seq,payload_len); return; } else { memcpy((uint8_t*)data,packetbuf_dataptr(),MPU_DATA_SIZE*SAMPLES_PER_FRAME); } // printf("%u,%u,",rx_sn_id,pkt_seq); /* for(i = 0;i < payload_len;i++) { printf("%02x,",raw_data[i]); } printf("\n"); */ for(i=0;i<SAMPLES_PER_FRAME;i++) { // MPU data mpu_data_union samples = data[i]; //print result //printf("%d,%d,%d,%d,%d,%d,%d\n",sampled_data.data.accel_x,sampled_data.data.accel_y,sampled_data.data.accel_z,sampled_data.data.temperature,sampled_data.data.gyro_x,sampled_data.data.gyro_y,sampled_data.data.gyro_z); MPU_PRINT_BYTE(rx_sn_id); MPU_PRINT_BYTE(0); MPU_PRINT_BYTE(pkt_seq); MPU_PRINT_BYTE(0); MPU_PRINT_BYTE(samples.reg.x_accel_h); MPU_PRINT_BYTE(samples.reg.x_accel_l); MPU_PRINT_BYTE(samples.reg.y_accel_h); MPU_PRINT_BYTE(samples.reg.y_accel_l); MPU_PRINT_BYTE(samples.reg.z_accel_h); MPU_PRINT_BYTE(samples.reg.z_accel_l); MPU_PRINT_BYTE(samples.reg.x_gyro_h); MPU_PRINT_BYTE(samples.reg.x_gyro_l); MPU_PRINT_BYTE(samples.reg.y_gyro_h); MPU_PRINT_BYTE(samples.reg.y_gyro_l); MPU_PRINT_BYTE(samples.reg.z_gyro_h); MPU_PRINT_BYTE(samples.reg.z_gyro_l); MPU_PRINT_BYTE(samples.reg.t_h); MPU_PRINT_BYTE(samples.reg.t_l); MPU_PRINT_BYTE('\n'); } #endif /* app_output_16t(data,rx_sn_id,pkt_seq,payload_len); */ PROCESS_CONTEXT_END(&null_app_process); }