static void ftsp_recv(struct broadcast_conn *bc, rimeaddr_t *from) { packetbuf_copyto(&msg_rcv); if (packetbuf_attr(PACKETBUF_ATTR_TIMESTAMP)) { if ((state & STATE_PROCESSING) == 0) { msg_rcv.seq_num = packetbuf_attr(PACKETBUF_ATTR_EPACKET_ID); msg_rcv.local_time = ftsp_arrival_time; state |= STATE_PROCESSING; // remove the TIMESTAMP attribute packetbuf_set_attr(PACKETBUF_ATTR_TIMESTAMP, 0); process_post(&ftsp_msg_process, msg_ready, NULL); PRINTF("FTSP message rcvd: node_id=%u, root_id=%u, seq_num=%u\n", msg_rcv.node_id, msg_rcv.root_id, msg_rcv.seq_num); PRINTF(" global_time=%lu, local_time=%lu\n", msg_rcv.global_time, msg_rcv.local_time); PRINTF(" local_to_global(local_time)=%lu, diff=%ld\n", local_to_global(msg_rcv.local_time), local_to_global(msg_rcv.local_time) - msg_rcv.global_time); } else { PRINTF("FTSP message from %u ignored, still processing\n", msg_rcv.node_id); } } else { PRINTF("FTSP message from %u with timestamp attribute %d ignored\n", msg_rcv.node_id, packetbuf_attr(PACKETBUF_ATTR_TIMESTAMP)); } }
/*---------------------------------------------------------------------------*/ static void recv_uc(struct unicast_conn *c, const linkaddr_t *from) { struct measure_message msg; // Check if the message length corresponds to the used struct if (packetbuf_datalen() == sizeof(struct measure_message)){ packetbuf_copyto(&msg); // printf("unicast message received from %d.%d: \n", // from->u8[0], from->u8[1]); printf("seq %lu", msg.seq); printf(" bat %ld.%03d V\n", (long) msg.bat_voltage, (unsigned) ((msg.bat_voltage - floor(msg.bat_voltage)) * 1000) ); leds_toggle(LEDS_GREEN); } else { // Wrong message sizes printf("Got invalid message\n"); leds_toggle(LEDS_RED); } }
/*---------------------------------------------------------------------------*/ void slipnet_input(void) { int i; /* radio should be configured for filtering so this should be simple */ /* this should be sent over SLIP! */ /* so just copy into uip-but and send!!! */ /* Format: !R<data> ? */ uip_len = packetbuf_datalen(); i = packetbuf_copyto(uip_buf); if(DEBUG) { printf("Slipnet got input of len: %d, copied: %d\n", packetbuf_datalen(), i); for(i = 0; i < uip_len; i++) { printf("%02x", (unsigned char) uip_buf[i]); if((i & 15) == 15) printf("\n"); else if((i & 7) == 7) printf(" "); } printf("\n"); } /*printf("SUT: %u\n", uip_len); */ slip_send_packet(uip_buf, uip_len); }
/*---------------------------------------------------------------------------*/ static void recv_data(struct unicast_conn *c, const linkaddr_t *from) { struct route_entry *e; linkaddr_t source; uip_len = packetbuf_copyto(&uip_buf[UIP_LLH_LEN]); source.u8[0] = BUF->srcipaddr.u8[2]; source.u8[1] = BUF->srcipaddr.u8[3]; e = route_lookup(&source); if(e == NULL) { route_add(&source, from, 10, 0); } else { route_refresh(e); } /* If we received data via a gateway, we refresh the gateway route. * Note: we refresh OUR gateway route, although we are not sure it forwarded the data. */ if(!uip_ipaddr_maskcmp(&BUF->srcipaddr, &netaddr, &netmask)) { e = route_lookup(&gateway); if(e != NULL) { route_refresh(e); } } PRINTF("uip-over-mesh: %d.%d: recv_data with len %d\n", linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1], uip_len); tcpip_input(); }
/* this function has been defined to be called when a unicast is being received */ static void recv_runicast(struct runicast_conn *c, rimeaddr_t *from, uint8_t seqno) { printf("runicast message received from %d.%d, seqno %d\n", from->u8[0], from->u8[1], seqno); /* from the packet we have just received, read the data and write it into the * struct tmReceived we have declared and instantiated above (line 26) */ packetbuf_copyto(&tmReceived); printf("time received = %d clock ticks", (uint16_t)tmReceived.time); printf(" = %d secs ", (uint16_t)tmReceived.time / CLOCK_SECOND); printf("%d millis ", (1000L * ((uint16_t)tmReceived.time % CLOCK_SECOND)) / CLOCK_SECOND); printf("originator = %d\n", tmReceived.originator); leds_on(LEDS_BLUE); ctimer_set(&ledTimer, CLOCK_SECOND / 8, timerCallback_turnOffLeds, NULL); // If the packet received is not ours, send it back to the originator if(tmReceived.originator != node_id) { packetbuf_copyfrom(&tmReceived, sizeof(tmSent)); runicast_send(&runicast, &addr, MAX_RETRANSMISSIONS); printf("sending packet to %u\n", addr.u8[0]); } else { // Our packet has completed a round-trip rtt -= tmReceived.time; printf("RTT = %d ms\n", (1000L * ((uint16_t)rtt % CLOCK_SECOND)) / CLOCK_SECOND); } }
/*---------------------------------------------------------------------------*/ static void plb_send(mac_callback_t sent, void *ptr) { PRINTF("plb_send\n"); if ( packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == 0 ) //data { PRINTF("plb_send : DATA\n"); send_req = 1; sent_callback = sent; sent_ptr = ptr; //packetbuf_clear_hdr(); temp_len=packetbuf_datalen(); packetbuf_copyto(dataptr_temp); print_packet(dataptr_temp,packetbuf_totlen());//JJH3 } //kdw sync else if ( packetbuf_attr(PACKETBUF_ATTR_PACKET_TYPE) == 1 ) //sync { sent_callback = sent; sent_ptr = ptr; plb_send_sync_start(); } else // error { mac_call_sent_callback(sent, ptr, MAC_TX_ERR, 1); //error fill this } return; }
static void broadcast_recv(struct broadcast_conn *bc, const rimeaddr_t *from) /* Executes if bc is received */ { static data_message_t message; /* Temporary variable to store received data */ packetbuf_copyto(&message); /* Store contents of packet buffer */ printf("Message received from %d.%d:\n", from->u8[0], from->u8[1]); /* Print the message */ printf("The message key is %d, the temperature value is %d, and the humidity value is %d\n", message.key, message.temp, message.humid); }
/*---------------------------------------------------------------------------*/ void queuebuf_update_from_packetbuf(struct queuebuf *buf) { struct queuebuf_data *buframptr = queuebuf_load_to_ram(buf); packetbuf_attr_copyto(buframptr->attrs, buframptr->addrs); buframptr->len = packetbuf_copyto(buframptr->data); #if WITH_SWAP if(buf->location == IN_CFS) { queuebuf_flush_tmpdata(); } #endif }
static void broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from) /* Print value if broadcast is received */ { static float x; static int dec; static float frac; packetbuf_copyto(&x); dec = x; frac = x - dec; printf("Message from %d: Humidity = %d.%02u %\n", from->u8[0], dec, (unsigned int)(frac * 100)); }
/*---------------------------------------------------------------------------*/ struct queuebuf * queuebuf_new_from_packetbuf(void) { struct queuebuf *buf; struct queuebuf_ref *rbuf; if(packetbuf_is_reference()) { rbuf = memb_alloc(&refbufmem); if(rbuf != NULL) { #if QUEUEBUF_STATS ++queuebuf_ref_len; #if CONTIKI_TARGET_NETSIM /* node_log("%d %d\n", queuebuf_len, queuebuf_ref_len);*/ #endif /* CONTIKI_TARGET_NETSIM */ #endif /* QUEUEBUF_STATS */ rbuf->len = packetbuf_datalen(); rbuf->ref = packetbuf_reference_ptr(); rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n"); } return (struct queuebuf *)rbuf; } else { buf = memb_alloc(&bufmem); if(buf != NULL) { #if QUEUEBUF_STATS ++queuebuf_len; if(queuebuf_len == queuebuf_max_len + 1) { memb_free(&bufmem, buf); queuebuf_len--; return NULL; } #if CONTIKI_TARGET_NETSIM /* node_log("%d %d\n", queuebuf_len, queuebuf_ref_len);*/ #endif /* CONTIKI_TARGET_NETSIM */ #endif /* QUEUEBUF_STATS */ buf->len = packetbuf_copyto(buf->data); packetbuf_attr_copyto(buf->attrs, buf->addrs); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n"); } return buf; } }
/*---------------------------------------------------------------------------*/ static int create_80211(void) { int i; uint16_t _offset; /* The underlying 802.11 driver is delivering the * raw IPv6 packet to us. By using the additional * packet information in the packet buffer, we'll * update the packet header, so the border router * is able to extract all the necessary info for * the received packet. This action is equivalent * to serialization of the input. */ _offset = 0; /* First, we copy the destination and the source * [Rime-8byte] addresses of the incoming packet */ rimeaddr_t* dst_mac_address = packetbuf_addr(PACKETBUF_ADDR_RECEIVER); rimeaddr_t* src_mac_address = packetbuf_addr(PACKETBUF_ADDR_SENDER); if (dst_mac_address == NULL || src_mac_address == NULL) { /* Packet should not be forwarded upwards. */ PRINTF("SLIP-NET ERROR: Addresses of incoming packet are NULL.\n"); return -1; } memcpy(uip_buf, dst_mac_address, RIMEADDR_SIZE); _offset += RIMEADDR_SIZE; memcpy(uip_buf + _offset,src_mac_address, RIMEADDR_SIZE); _offset += RIMEADDR_SIZE; /* Next we extract the PAN ID and attach it * to the header we have constructed at the * front of the UIP header. Since the 80211 * has a BSSID, and not a PAN ID, this step * is redundant. Also, since the packet has * arrived at this stage, it is guaranteed * that it comes from the selected BSSID so * we just copy the default PAN ID and send * it up with the remaining of the packet. */ const uint16_t _pan_id = IEEE802154_PANID; memcpy(uip_buf + _offset, &_pan_id, sizeof(uint16_t)); _offset += sizeof(uint16_t); /* We can now copy the incoming packet data. */ uip_len = packetbuf_datalen() + _offset; i = packetbuf_copyto(uip_buf + _offset); return i; }
/*---------------------------------------------------------------------------*/ #if QUEUEBUF_DEBUG struct queuebuf * queuebuf_new_from_packetbuf_debug(const char *file, int line) #else /* QUEUEBUF_DEBUG */ struct queuebuf * queuebuf_new_from_packetbuf(void) #endif /* QUEUEBUF_DEBUG */ { struct queuebuf *buf; struct queuebuf_ref *rbuf; if(packetbuf_is_reference()) { rbuf = memb_alloc(&refbufmem); if(rbuf != NULL) { #if QUEUEBUF_STATS ++queuebuf_ref_len; #endif /* QUEUEBUF_STATS */ rbuf->len = packetbuf_datalen(); rbuf->ref = packetbuf_reference_ptr(); rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n"); } return (struct queuebuf *)rbuf; } else { buf = memb_alloc(&bufmem); if(buf != NULL) { #if QUEUEBUF_DEBUG list_add(queuebuf_list, buf); buf->file = file; buf->line = line; buf->time = clock_time(); #endif /* QUEUEBUF_DEBUG */ #if QUEUEBUF_STATS ++queuebuf_len; PRINTF("queuebuf len %d\n", queuebuf_len); printf("#A q=%d\n", queuebuf_len); if(queuebuf_len == queuebuf_max_len + 1) { memb_free(&bufmem, buf); queuebuf_len--; return NULL; } #endif /* QUEUEBUF_STATS */ buf->len = packetbuf_copyto(buf->data); packetbuf_attr_copyto(buf->attrs, buf->addrs); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n"); } return buf; } }
recv_relcollect(const rimeaddr_t *originator) #endif /* (WITH_FTSP || GLOSSY) && QUEUING_STATS */ { static struct message msg; packetbuf_copyto(&msg); #if WITH_FTSP || GLOSSY unsigned long time, high, low = 0; high = time_high; low = time_low; time = (high << 16) | low; #endif /* WITH_FTSP */ print_msg.rx = originator->u8[0]; print_msg.seq_no = msg.seq_no; print_msg.t_tx = msg.energest_transmit; print_msg.t_rx = msg.energest_listen; print_msg.t_lpm = msg.energest_lmp; print_msg.t_cpu = msg.energest_cpu; #if WITH_FTSP print_msg.latency = (rtimer_long_clock_t) (ftsp_arrival_time - time) * 1000 / RTIMER_SECOND_LONG; #endif /* WITH_FTSP */ #if QUEUING_STATS print_msg.queuing_delay = queuing_delay; print_msg.dropped_packets_count = dropped_packets_count; print_msg.queue_size = queue_size; #endif /* QUEUING_STATS */ #if GLOSSY print_msg.t_glossy_tx = msg.energest_glossy_transmit; print_msg.t_glossy_rx = msg.energest_glossy_listen; print_msg.t_glossy_cpu = msg.energest_glossy_cpu; if (glossy_arrival_time < time) { // there was a Glossy phase between the first packet transmission and the final reception glossy_arrival_time += GLOSSY_PERIOD; } print_msg.latency = (unsigned long) (glossy_arrival_time - time) * 1000 / RTIMER_SECOND; print_msg.rx_cnt = msg.rx_cnt; print_msg.time_to_rx = msg.time_to_rx; print_msg.period_skew = msg.period_skew; #endif /* GLOSSY */ /* Poll the print process */ process_poll(&print_process); }
static void broadcast_recv(struct broadcast_conn *c, const rimeaddr_t *from) { //printf("broadcast message received from %d.%d: '%s'\n", //from->u8[0], from->u8[1], (char *)packetbuf_dataptr()); static opt_message_t msg; int i; packetbuf_copyto(&msg); // Don't print clock messages printf("%u %u %u", msg.key, msg.iter, from->u8[0]); for( i=0; i<DATA_LEN; i++ ) { printf(" %"PRIi64, msg.data[i]); } printf("\n"); }
/*---------------------------------------------------------------------------*/ static void recv_mag(struct broadcast_conn *c, const rimeaddr_t *from) { struct mag_msg msg; if (packetbuf_datalen() != sizeof(struct mag_msg)) { /* Bad size */ return; } packetbuf_copyto(&msg); /* Copy packet data */ if (msg.type != TYPE_MAG) { /* Bad type */ return; } /* We received a broadcast message from a neighbour. * Update our neighbour list using neighbours LQI */ myneighbours_update(from, (int)packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY)); FLASH_LED(LEDS_GREEN); }
static void recv_runicast(struct runicast_conn *c, rimeaddr_t *from, uint8_t seqno) { printf("runicast message received from %d.%d, seqno %d\n", from->u8[0], from->u8[1], seqno); packetbuf_copyto(&filenameOriginatorRecv); printf("originator: %u\n", filenameOriginatorRecv.originator); printf("filename: %s\n", filenameOriginatorRecv.name); // request file from initiator rucb_open(&rucb, RUCB_CHANNEL, &rucb_call); packetbuf_clear(); *((uint8_t *)packetbuf_dataptr()) = ++req_seq_counter; memcpy(((char *)packetbuf_dataptr()) + 1, filenameOriginatorRecv.name, strlen(filenameOriginatorRecv.name)+1); packetbuf_set_datalen(strlen(filenameOriginatorRecv.name) + 2); rimeaddr_t addr; addr.u8[0] = filenameOriginatorRecv.originator; addr.u8[1] = 0; filename_download = filenameOriginatorRecv.name; fd = cfs_open(filename_download, CFS_WRITE ); runicast_send(&runicast, &addr, MAX_RETRANSMISSIONS); downloading = 1; process_start(&download_and_execute_process, NULL); }
/* this function has been defined to be called when a unicast is being received */ static void recv_uc(struct unicast_conn *c, const rimeaddr_t *from) { // Round-trip time, will be decremented later clock_time_t rtt = clock_time(); printf("unicast message received from %d.%d\n", from->u8[0], from->u8[1]); /* turn on blue led */ leds_on(LEDS_BLUE); /* set the timer "leds_off_timer" to 1/8 second */ ctimer_set(&leds_off_timer_send, CLOCK_SECOND / 8, timerCallback_turnOffLeds, NULL); /* from the packet we have just received, read the data and write it into the * struct tmReceived we have declared and instantiated above (line 16) */ packetbuf_copyto(&tmReceived); /* print the contents of the received packet */ printf("time received = %d clock ticks", (uint16_t)tmReceived.time); printf(" = %d secs ", (uint16_t)tmReceived.time / CLOCK_SECOND); printf("%d millis ", (1000L * ((uint16_t)tmReceived.time % CLOCK_SECOND)) / CLOCK_SECOND); printf("originator = %d\n", tmReceived.originator); // If the packet received is not ours, send it back to the originator if(tmReceived.originator != node_id) { packetbuf_copyfrom(&tmReceived, sizeof(tmSent)); if(!rimeaddr_cmp(&addr, &rimeaddr_node_addr)) { /* when calling unicast_send, we have to specify the address as the second argument (a pointer to the defined rimeaddr_t struct) */ unicast_send(&uc, &addr); } printf("sending packet to %u\n", addr.u8[0]); } else { // Our packet has completed a round-trip rtt -= tmReceived.time; printf("RTT = %d ms\n", (1000L * ((uint16_t)rtt % CLOCK_SECOND)) / CLOCK_SECOND); } }
/*---------------------------------------------------------------------------*/ #if QUEUEBUF_DEBUG struct queuebuf * queuebuf_new_from_packetbuf_debug(const char *file, int line) #else /* QUEUEBUF_DEBUG */ struct queuebuf * queuebuf_new_from_packetbuf(void) #endif /* QUEUEBUF_DEBUG */ { struct queuebuf *buf; struct queuebuf_ref *rbuf; if(packetbuf_is_reference()) { rbuf = memb_alloc(&refbufmem); if(rbuf != NULL) { #if QUEUEBUF_STATS ++queuebuf_ref_len; #endif /* QUEUEBUF_STATS */ rbuf->len = packetbuf_datalen(); rbuf->ref = packetbuf_reference_ptr(); rbuf->hdrlen = packetbuf_copyto_hdr(rbuf->hdr); } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a reference queuebuf\n"); } return (struct queuebuf *)rbuf; } else { struct queuebuf_data *buframptr; buf = memb_alloc(&bufmem); if(buf != NULL) { #if QUEUEBUF_DEBUG list_add(queuebuf_list, buf); buf->file = file; buf->line = line; buf->time = clock_time(); #endif /* QUEUEBUF_DEBUG */ buf->ram_ptr = memb_alloc(&buframmem); #if WITH_SWAP /* If the allocation failed, store the qbuf in swap files */ if(buf->ram_ptr != NULL) { buf->location = IN_RAM; buframptr = buf->ram_ptr; } else { buf->location = IN_CFS; buf->swap_id = -1; tmpdata_qbuf = buf; buframptr = &tmpdata; } #else if(buf->ram_ptr == NULL) { PRINTF("queuebuf_new_from_packetbuf: could not queuebuf data\n"); memb_free(&bufmem, buf); return NULL; } buframptr = buf->ram_ptr; #endif buframptr->len = packetbuf_copyto(buframptr->data); packetbuf_attr_copyto(buframptr->attrs, buframptr->addrs); #if WITH_SWAP if(buf->location == IN_CFS) { if(queuebuf_flush_tmpdata() == -1) { /* We were unable to write the data in the swap */ memb_free(&bufmem, buf); return NULL; } } #endif #if QUEUEBUF_STATS ++queuebuf_len; PRINTF("queuebuf len %d\n", queuebuf_len); printf("#A q=%d\n", queuebuf_len); if(queuebuf_len == queuebuf_max_len + 1) { queuebuf_free(buf); queuebuf_len--; return NULL; } #endif /* QUEUEBUF_STATS */ } else { PRINTF("queuebuf_new_from_packetbuf: could not allocate a queuebuf\n"); } return buf; } }