uint16_t packet_received(uint8_t packet[], uint16_t length, uint16_t src_addr, int16_t rssi) { if (packet[0] == DATA) { if (type == SINK) { stat_add(STAT_APP_RX); printf("[APP];SINK_RX;%c%c%c%c;%.4x;%u;%u\n", packet[2], packet[3], packet[4], packet[5],src_addr, packet[6],packet[7]); return 0; } else { routing_txframe[0] = DATA; routing_txframe[1] = level-1; //node level routing_txframe[2] = packet[2]; //node id routing_txframe[3] = packet[3]; routing_txframe[4] = packet[4]; routing_txframe[5] = packet[5]; routing_txframe[6] = packet[6]; // num sequence routing_txframe[7] = packet[7] + 1; // nb hops txlength = 8; printf("[ROUTING];NODE_FORWARD;%.4x;%c%c%c%c;%u;%u;%u-%u\n", nodeaddr, routing_txframe[2], routing_txframe[3], routing_txframe[4], routing_txframe[5], routing_txframe[6],routing_txframe[7],global_clock, timerB_time()/32); stat_add(STAT_FORWARD_CNT); mac_send(routing_txframe, txlength, parent_id); } } return 0; }
static int arp_handle(unsigned char *buf, unsigned int len) { struct arp_header *pRx, *pTx; pRx = (struct arp_header *)buf; pTx = (struct arp_header *)&TxBuf[256]; switch (ntohs(pRx->ar_op)){ case ARP_REQUEST: if (pRx->ar_tpa == htonl(NetOurIp)){ pTx->ar_hrd = htons(0x01); pTx->ar_pro = htons(PROTO_IP); pTx->ar_hln = 0x06; pTx->ar_pln = 0x04; pTx->ar_op = htons(ARP_REPLY); memcpy(pTx->ar_sha, NetOurEther, 6); pTx->ar_spa = htonl(NetOurIp); memcpy(pTx->ar_tha, pRx->ar_sha, 6); pTx->ar_tpa = pRx->ar_spa; mac_send((unsigned char *)pTx, sizeof(struct arp_header), PROTO_ARP); } break; case ARP_REPLY: printf("\n\rGot ARP reply\n"); break; default: printf("\n\rar_op Not Support\n"); break; } return 0; }
static void vSendingTask(void* pvParameters) { uint16_t count = 0; while (1) { count ++; if (mac_addr == sending_node) { if (count & 1) { printf("sending to 0xFFFF\n"); mac_send(0xffff, (uint8_t*) "Test", sizeof("Test")); } else { printf("sending to 0x%.4x\n", receiving_node); mac_send(receiving_node, (uint8_t*) "Test", sizeof("Test")); } } vTaskDelay(20); } }
uint16_t send_packet() { seqnum++; hello.seqnum[0] = seqnum ; hello.seqnum[1] = seqnum >> 8; /* Add data in the hello message here */ hello.crc = crc8_bytes((uint8_t *)hello.raw, (uint16_t)ST_OFFSET(hello_t, crc) ); mac_send(MAC_BROADCAST_ADDR, hello.raw, sizeof(hello_t), 0); return 0; }
int main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer eint(); clock_dco_set(8); // DCO 8MHz clock_mclk_set(CLOCK_SOURCE_DCO, 1); // MCLK 8MHz clock_smclk_set(CLOCK_SOURCE_DCO, 8); // SMCLK 1MHz clock_aclk_set(1); leds_init(); leds_off(LEDS_ALL); mac_init(); mac_set_tx_cb(sent); mac_set_rx_cb(rx); timer_start(TIMER_SOURCE_ACLK, 1); timer_register_cb(TIMER_ALARM_0, send); timer_set_alarm(TIMER_ALARM_0, 12000, 12000, TIMER_MODE_FROM_NOW, 0); leds_on(LEDS_ALL); while (1) { event = 0; LPM0; switch (event) { case EVENT_SEND: mac_send(msg, strlen(msg), MAC_BROADCAST); break; case EVENT_SENT: leds_toggle(LED_GREEN); break; case EVENT_RX: leds_toggle(LED_RED); break; } } return 0; }
uint16_t send_packet(void) { static spt_hello_t pkt_hello; activity_schedule(); if ( num_slot == 0 ) beacon_received = 0; if ( num_slot == 2 && my_state == STATE_ACTIVE ) { #ifdef DEBUG LED_GREEN_ON(); #endif pkt_hello.type = SPT_HELLO; PRINTF("#> Sending HELLO\n"); mac_send((uint8_t *)&pkt_hello, sizeof(pkt_hello), MAC_BROADCAST); #ifdef DEBUG LED_GREEN_OFF(); #endif } return 0; }
int main (void) { WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer set_mcu_speed_xt2_mclk_8MHz_smclk_1MHz(); set_aclk_div(1); LEDS_INIT(); LEDS_OFF(); uart0_init(UART0_CONFIG_1MHZ_115200); printf("\n-----------------------------------\n"); printf("TDMA_NODE test\r\n"); eint(); mac_init(0); mac_set_access_allowed_cb(mac_ready); printf("*** I'm %u ***\n", node_addr); uint8_t dodo = 'a'; while(1) { if (mac_is_access_allowed()) { mac_payload[0] = dodo; mac_send(); dodo++; if (dodo>'z')dodo='a'; LED_RED_TOGGLE(); } LPM3; } return 0; }
static void vADCTask(void* pvParameters) { uint16_t event; vADCInit(); // Start MAC layer mac_send_command(MAC_ASSOCIATE); mac_set_event_handler(MAC_ASSOCIATED, associated); mac_set_event_handler(MAC_LOST, lost); mac_set_beacon_handler(beacon_node); LED_GREEN_OFF(); for (;;) { if (xQueueReceive(xDataQueue, &event, portMAX_DELAY) == pdTRUE) { switch (event) { case ASSOCIATED: vADCInit(); data_frame.length = 0; break; case MEASURE: ADC12CTL0 |= ADC12SC; break; case SEND: if (data_frame.length > 0) { if (mac_send((uint8_t*) &data_frame, 4 + 12 * data_frame.length)) { printf("%.4x: %u: %u\n", mac_addr, data_frame.seq, data_frame.length); } data_frame.length = 0; } break; } } } }
int main(void) { WDTCTL = WDTPW+WDTHOLD; set_mcu_speed_xt2_mclk_8MHz_smclk_1MHz(); set_aclk_div(1); LEDS_INIT(); LEDS_OFF(); ds2411_init(); nodeaddr = (((uint16_t)ds2411_id.serial1)<<8) + (ds2411_id.serial0); uart0_init(UART0_CONFIG_1MHZ_115200); uart0_register_callback(char_rx); eint(); printf("[APP];BOOTING;%.4x\n",nodeaddr); //check if this node is the sink if (nodeaddr == sink_nodes) { type = SINK; level = DEFAULT_LEVEL; } else { //retrieve father for (idx=0; idx<NUMBER_NODES; idx++) { if (list_nodes[idx] == nodeaddr) { if(father_nodes1[idx] != 0x0000) { parent_id = father_nodes1[idx]; level = 12; break; } } } } //hack for mobile /*if(nodeaddr == 0x1f5d) { parent_id = 0x0000; mac_set_mobile(1); level = 12; }*/ mac_init(10); mac_set_rx_cb(packet_received); mac_set_error_cb(packet_error); mac_set_sent_cb(packet_sent); timerB_set_alarm_from_now(TIMERB_ALARM_CCR6, 32768, 32768); timerB_register_cb(TIMERB_ALARM_CCR6, inc_clock); while (1) { LPM1; if (state == SM_TX) { if (level != UNDEF_LEVEL && type != SINK) { seq_max = NUM_SEQ_MAX; delay = rand(); delay &= 0xCFFF; delay += 12000; //(369ms < delay < 1991ms) timerB_set_alarm_from_now(TIMERB_ALARM_CCR5, delay, 0); timerB_register_cb(TIMERB_ALARM_CCR5, next_send); } else { printf("[APP];NOROUTE\n"); } state = SM_IDLE; } else if (state == SM_LOOP_TX) { if (level != UNDEF_LEVEL) { sprintf(sourceaddr,"%.4x",nodeaddr); data_txframe[0] = DATA; data_txframe[1] = level-1; data_txframe[2] = sourceaddr[0]; data_txframe[3] = sourceaddr[1]; data_txframe[4] = sourceaddr[2]; data_txframe[5] = sourceaddr[3]; data_txframe[6] = seq; //sequence data_txframe[7] = 1; //hops txlength = 8; stat_add(STAT_APP_TX); printf("[APP];NODE_TX;%.4x;%.4x;%u;%u-%u\n", nodeaddr, parent_id, seq, global_clock, timerB_time()/32); seq++; mac_send(data_txframe, txlength, parent_id); if (DEBUG_LEDS == 1) { LED_GREEN_ON(); } if (seq < seq_max) { timerB_set_alarm_from_now(TIMERB_ALARM_CCR5, SEND_DATA_PERIOD, 0); timerB_register_cb(TIMERB_ALARM_CCR5, next_send); } } state = SM_IDLE; } } return 0; }