void _output_frame( TiFrame * frame, TiUartAdapter * uart ) { static TiIEEE802Frame154Descriptor m_desc; TiIEEE802Frame154Descriptor * desc; if (frame_totallength(frame) > 0) { dbc_putchar( '>' ); dbc_n8toa( frame_totallength(frame) ); desc = ieee802frame154_open( &m_desc ); // ? if (ieee802frame154_parse(desc, frame_startptr(frame), frame_length(frame))) if (ieee802frame154_parse(desc, frame_startptr(frame), frame_capacity(frame))) { // if the frame received is parsed successfully, then output it to the // computer through debugging channel //ieee802frame154_set_sequence( desc, seqid ++ ); //ieee802frame154_set_panto( desc, CONFIG_ALOHA_DEFAULT_PANID ); //ieee802frame154_set_shortaddrto( desc, CONFIG_ALOHA_REMOTE_ADDRESS ); //ieee802frame154_set_panfrom( desc, CONFIG_ALOHA_PANID); //ieee802frame154_set_shortaddrfrom( desc, CONFIG_ALOHA_LOCAL_ADDRESS ); // todo: you can output more // reference frame_dump() in rtl_frame.c dbc_n8toa( ieee802frame154_sequence(desc) ); dbc_putchar( ':' ); dbc_write( frame_startptr(frame), frame_capacity(frame) ); } else{ // if the frame received is parsed failed, then output the error frame // to the computer through debugging channel dbc_putchar( 'X' ); dbc_putchar( ':' ); dbc_write( frame_startptr(frame), frame_capacity(frame) ); } dbc_putchar( '\r' ); dbc_putchar( '\n' ); } }
void ieee802frame154_dump( TiFrame * f ) { int8 len; TiIEEE802Frame154Descriptor meta; len = frame_length(f); if (len > 0) { dbc_putchar( 0xFA ); dbc_putchar( 0xFA ); dbc_putchar( frame_curlayer(f) ); dbc_putchar( len ); // if the frame received is parsed successfully, then output it to the // computer through debugging channel // ieee802frame154_open( &meta ); if (ieee802frame154_parse(&meta, frame_startptr(f), frame_length(f))) { // todo: you can output more // reference frame_dump() in rtl_frame.c dbc_putchar( 0xFB ); dbc_write( frame_startptr(f), len ); dbc_putchar( 0xFC ); } else{ // if parsing failed, then we also output the data dbc_putchar( 0xFC ); dbc_write( frame_startptr(f), len ); } } else{ // If the f doesn't contain any data, then still output some flag for indication. dbc_putchar( 0xFB ); dbc_putchar( 0xFB ); } }
void ieee802frame154_dump( TiFrame * frame ) { static TiIEEE802Frame154Descriptor m_desc; TiIEEE802Frame154Descriptor * desc; if (frame_length(frame) > 0) { dbc_putchar( '>' ); dbc_n8toa( frame_length(frame) ); /* desc = ieee802frame154_open( &m_desc ); if (ieee802frame154_parse(desc, frame_startptr(frame), frame_length(frame))) { // if the frame received is parsed successfully, then output it to the // computer through debugging channel //ieee802frame154_sequence( desc ); //ieee802frame154_panto( desc ); //ieee802frame154_shortaddrto( desc ); //ieee802frame154_panfrom( desc ); //ieee802frame154_shortaddrfrom( desc ); // todo: you can output more // reference frame_dump() in rtl_frame.c dbc_n8toa( ieee802frame154_sequence(desc) ); dbc_putchar( ':' ); dbc_write( frame_startptr(frame), frame_length(frame) ); } else{ // if the frame received is parsed failed, then output the error frame // to the computer through debugging channel dbc_putchar( 'X' ); dbc_putchar( ':' ); dbc_write( frame_startptr(frame), frame_length(frame) ); } */ //dbc_write( frame_startptr(frame), frame_length(frame) ); dbc_write( frame_buffer(frame), frame_buffercapacity(frame) ); //dbc_mem( frame_startptr(rxbuf), frame_length(rxbuf) ); //dbc_write( frame_startptr(frame), frame_capacity(frame) ); dbc_putchar( '\n' ); } }
int main(int argc, char **argv) { dbc_t *dbc; char *const inFilename = (argc>1 ? argv[1] : NULL); char *const outFilename = (argc>2 ? argv[2] : NULL); int ret = 0; if(NULL != (dbc = dbc_read_file(inFilename))) { FILE *dbcout; /* open output file */ if(outFilename != NULL) { if(NULL == (dbcout = fopen(outFilename,"w"))) { fprintf(stderr, "can't open output file '%s'\n", outFilename); ret = 1; } } else { dbcout = stdout; } /* perform copy */ if(dbcout != NULL) { dbc_write(dbcout, dbc); /* close output file */ if(outFilename != NULL) { fclose(dbcout); } } dbc_free(dbc); } else { fprintf(stderr, "can't open input file '%s'\n", inFilename); ret = 1; } return ret; }
int main(void) { uint16 value, count; uint8 len; char * request; char * response; char * payload; char * msg = "welcome to node..."; TiTimerAdapter * timeradapter; TiTimerManager * vtm; TiTimer * mac_timer; TiCc2420Adapter * cc; TiFrameRxTxInterface * rxtx; TiNioAcceptor * nac; TiAloha * mac; TiAdcAdapter * adc; TiLumSensor * lum; TiDataTreeNetwork * dtp; TiFrame * rxbuf; TiFrame * txbuf; target_init(); led_open(); led_on( LED_ALL ); hal_delay( 500 ); led_off( LED_ALL ); rtl_init( (void *)dbio_open(38400), (TiFunDebugIoPutChar)dbio_putchar, (TiFunDebugIoGetChar)dbio_getchar, hal_assert_report ); dbc_write( msg, strlen(msg) ); timeradapter = timer_construct( (void *)(&m_timeradapter), sizeof(m_timeradapter) ); vtm = vtm_construct( (void*)&m_vtm, sizeof(m_vtm) ); cc = cc2420_construct( (char *)(&m_cc), sizeof(TiCc2420Adapter) ); nac = nac_construct( &m_nacmem[0], NAC_SIZE ); mac = aloha_construct( (char *)(&m_aloha), sizeof(TiAloha) ); dtp = dtp_construct( (char *)(&m_dtp), sizeof(TiDataTreeNetwork) ); adc = adc_construct( (void *)&m_adc, sizeof(TiAdcAdapter) ); lum = lum_construct( (void *)&m_lum, sizeof(TiLumSensor) ); txbuf = frame_open( (char*)(&m_txbuf), FRAME_HOPESIZE(MAX_IEEE802FRAME154_SIZE), 3, 20, 0 ); rxbuf = frame_open( (char*)(&m_rxbuf), FRAME_HOPESIZE(MAX_IEEE802FRAME154_SIZE), 3, 20, 0 ); // timeradapter is used by the vtm(virtual timer manager). vtm require to enable the // period interrupt modal of vtm //timeradapter = timer_open( timeradapter, 0, NULL, NULL, 0x01 ); vtm = vtm_open( vtm, timeradapter, CONFIG_VTM_RESOLUTION ); cc = cc2420_open(cc, 0, NULL, NULL, 0x00 ); rxtx = cc2420_interface( cc, &m_rxtx ); mac_timer = vtm_apply( vtm ); mac_timer = vti_open( mac_timer, NULL, mac_timer); hal_assert( rxtx != NULL ); nac = nac_open( nac, rxtx, CONFIG_NIOACCEPTOR_RXQUE_CAPACITY, CONFIG_NIOACCEPTOR_TXQUE_CAPACITY); hal_assert( nac != NULL ); mac = aloha_open( mac, rxtx,nac, CONFIG_NODE_CHANNEL, CONFIG_NODE_PANID, CONFIG_NODE_ADDRESS,mac_timer, NULL, NULL,0x01); adc = adc_open( adc, 0, NULL, NULL, 0 ); lum = lum_open( lum, 0, adc ); dtp = dtp_open( dtp, mac, CONFIG_NODE_ADDRESS, NULL, NULL, 0x00 ); //todo cc2420_setchannel( cc, CONFIG_NODE_CHANNEL ); cc2420_setrxmode( cc ); // enable RX mode cc2420_setpanid( cc, CONFIG_NODE_PANID ); // network identifier, seems no use in sniffer mode cc2420_setshortaddress( cc, CONFIG_NODE_ADDRESS ); // in network address, seems no use in sniffer mode cc2420_enable_autoack( cc ); //todo cc2420_settxpower( cc, CC2420_POWER_1);//cc2420_settxpower( cc, CC2420_POWER_2); cc2420_enable_autoack( cc ); // ledtune = ledtune_construct( (void*)(&m_ledtune), sizeof(m_ledtune), vti ); // ledtune = ledtune_open( ledtune ); /* assert: all the above open() functions return non NULL values */ hal_assert((timeradapter != NULL) && (cc != NULL) && (mac != NULL) && (adc != NULL) && (lum != NULL) && (rxbuf != NULL) && (txbuf != NULL) && (dtp != NULL)); hal_enable_interrupts(); dtp->state = DTP_STATE_IDLE;//todo for testing 临时用这一句必须删掉 //todo for testing //dtp->root = 0x01;//todo for testing //dtp->parent = 0x03;//todo for testing /* while ( 1)//todo for testing { response = frame_startptr( txbuf ); value = lum_value( lum ); payload = DTP_PAYLOAD_PTR(response); payload[0] = 0x13; payload[1] = 0x14; if (dtp_send_response(dtp, txbuf, 0x01) > 0) { led_toggle( LED_RED);//todo for testing } dtp_evolve( dtp, NULL ); hal_delay( 2000);//todo for testing } */ while(1) { /* Only the following two kinds of frames will be put into "rxbuf" by dtp_recv() * - broadcast frames. the destination address of these frames are 0xFFFF. * - destination is the current node. */ //dbo_putchar(0x33); len = dtp_recv( dtp, rxbuf, 0x00 ); if (len > 0) { //ieee802frame154_dump( rxbuf); request = frame_startptr( rxbuf ); switch (DTP_CMDTYPE(request)) { /* if the frame is DTP_DATA_REQUEST, then the node will measure the data and * encapsulate the data into the txbuf, which is a TiOpenFrame and sent it back. */ case DTP_DATA_REQUEST: //payload = DTP_PAYLOAD_PTR( frame_startptr(txbuf) ); //ledtune_write( ledtune, MAKE_WORD(payload[1], payload[0]) ); // response frame = PHY Length 1B // + Frame Control 2B // + Sequence No 1B // + Destination Pan & Address 4B // + Source Pan & Address 4B // + DTP Section 15B //opf_cast( txbuf, 50, OPF_DEF_FRAMECONTROL_DATA_ACK ); response = frame_startptr( txbuf ); value = lum_value( lum ); DTP_SET_MAX_HOPCOUNT( response,0x03);//todo for testing payload = DTP_PAYLOAD_PTR(response); //payload[0] = 0x17;//todo 第三个节点数据 //payload[1] = 0x18;//todo 第三个节点数据 //payload[1] = 0x13; //payload[2] = 0x14; payload[1] = 0x15;//todo 另一个节点 payload[2] = 0x16;//todo 另一个节点 /* call dtp_send_response() to send the data in txbuf out. * * modified by zhangwei on 20091230 * - Bug fix. In the past, there's no delay between two adjacent * dtp_send_response() calls. This policy is too ambitious and this * node will occupy the whole time so that the other nodes will lost * chances to send, or encounter much higher frame collision probabilities. * so I add a little time delay here. * Attention the delay time here shouldn't be too large because * we don't want the hal_delay() to occupy all the CPU time. If this * occurs, it may lead to unnecessary frame lossing. */ // try some times until the RESPONSE is successfully sent for (count=0; count<10; count++) { //hal_delay( 500); if (dtp_send_response(dtp, txbuf, 0x03) > 0) { led_toggle( LED_RED);//todo for testing break; } //hal_delay( 50 ); } break; default: //hal_assert(false); break; } } nac_evolve( nac,NULL);//todo for tesitng aloha_evolve( mac,NULL);//todo for testing dtp_evolve( dtp, NULL ); hal_delay( 50 ); } }
//#if (TEST_CHOICE == 1) void recvnode1(void) { TiCc2420Adapter * cc; char * msg = "welcome to recvnode..."; TiFrame * rxbuf; uint8 len; target_init(); HAL_SET_PIN_DIRECTIONS(); wdt_disable(); led_open(); led_on( LED_RED ); hal_delay( 500 ); led_off( LED_ALL ); rtl_init( (void *)dbio_open(38400), (TiFunDebugIoPutChar)dbio_putchar, (TiFunDebugIoGetChar)dbio_getchar, hal_assert_report ); dbc_mem( msg, strlen(msg) ); cc = cc2420_construct( (void *)(&g_cc), sizeof(TiCc2420Adapter) ); cc2420_open( cc, 0, NULL, NULL, 0x00 ); cc2420_setchannel( cc, DEFAULT_CHANNEL ); cc2420_setrxmode( cc ); //Enable RX cc2420_enable_addrdecode( cc ); //使能地址译码 //cc2420_disable_addrdecode(cc); #ifdef TEST_ACK cc2420_enable_autoack(cc); #endif cc2420_setpanid( cc, PANID ); //网络标识 cc2420_setshortaddress( cc, LOCAL_ADDRESS ); //网内标识 rxbuf = frame_open( (char*)(&m_rxbuf), FRAME_HOPESIZE(MAX_IEEE802FRAME154_SIZE), 0, 0, 0 ); hal_enable_interrupts(); // when use this scan mode to receive data, interrupt should be disable; otherwise the data will be // read twice and in the second time there are no data actually which leads to a assert. // Attention: in this scan mode, MCU always try to read and in my test it is faster than the transmission of data. // Thus, after 4 times, there no data at all, and the MCU still want to read, which lead to an assert. So we'd better // not use this scan mode. while(1) { frame_reset( rxbuf,0,0,0); cc2420_evolve( cc ); //len = cc2420_read( cc, frame_startptr( rxbuf), frame_capacity( rxbuf), 0x00 ); len = cc2420_read( cc, frame_startptr(rxbuf), frame_capacity(rxbuf), 0x00 ); if ( len) { frame_setlength( rxbuf,len); dbc_write( frame_startptr( rxbuf), len ); led_toggle( LED_GREEN); } //len = cc2420_read( cc, (char*)(&buf[0]), BUF_SIZE, 0x00 ); /* if (len >= 5) { // output this frame to the computer through uart dbo_write( (char*)(&buf[0]), len ); }*/ } }