注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
dan158185的个人空间 https://home.eeworld.com.cn/space-uid-349284.html [收藏] [复制] [分享] [RSS]
日志

CC2538之TinyOS例程实验:8-RPL(roll)路由实验

已有 1904 次阅读2016-1-5 16:10 |个人分类:CC2538之TinyOS例程

上一篇文章使用了BLIP,本次的例程正是需要依赖BLIP栈,后面的网络实验也都需要BLIP

视频第十四部也做了RPL实验,关于RPL路由不做概念讲解,可以去百度网盘文档区或者IETF官网进行学习


例程目录:

tinyos-main-release_tinyos_2_1_2\apps\cc2538_Test\TestRPL\udp

源码还是官方的例程源码


Makefile文件:

  1. COMPONENT=TestRPLAppC  
  2.   
  3. CFLAGS += -DUSE_TIMER_HANDLER  
  4. #CFLAGS += -DUSE_UART_HANDLER  
  5. CFLAGS += -DUSE_RF_HANDLER  
  6. CFLAGS += -DNOT_USE_PRINTFC_BUT_USE_PRINT  
  7.   
  8. # NB :  
  9. # DEFAULT_LOCAL_GROUP=0xabcd  
  10.   
  11. # radio settings  
  12. CFLAGS+=-DCC2420_DEF_CHANNEL=25  
  13. CFLAGS+=-DCC2520_DEF_CHANNEL=25  
  14.   
  15. ################################################################################  
  16. ### Set the addressing scheme  
  17. ################################################################################  
  18.   
  19. # Use IN6_PREFIX with static addressing modes  
  20. PFLAGS += -DIN6_PREFIX=\"aaaa::/64\"  
  21. # Use BLIP Neighbor Discovery to autoconfigure an address  
  22. PFLAGS += -DBLIP_ADDR_AUTOCONF=0  
  23. # Use RPL and prefix information in DIO messages to autoconfigure an address  
  24. PFLAGS += -DRPL_ADDR_AUTOCONF=0  
  25.   
  26. ################################################################################  
  27. ### Configure BLIP  
  28. ################################################################################  
  29.   
  30. # Configure the Neighbor Discovery mechanism  
  31. PFLAGS += -DBLIP_SEND_ROUTER_SOLICITATIONS=0  
  32. PFLAGS += -DBLIP_SEND_ROUTER_ADVERTISEMENTS=0  
  33.   
  34. # Configure the number of times BLIP tries to send a packet and how long it  
  35. # waits between attempts  
  36. PFLAGS += -DBLIP_L2_RETRIES=3  
  37. PFLAGS += -DBLIP_L2_DELAY=103  
  38.   
  39. # Configure how many of the 6LoWPAN headers we support  
  40. #PFLAGS += -DLIB6LOWPAN_FULL=1  
  41.   
  42. # Configure the header compression for 6LoWPAN  
  43. PFLAGS += -DLIB6LOWPAN_HC_VERSION=6  
  44.   
  45. # Keep statistics about various BLIP/IPv6 parameters. See BlipStatistics.h  
  46. #PFLAGS += -DBLIP_STATS  
  47. #PFLAGS += -DBLIP_STATS_IP_MEM  
  48.   
  49. ################################################################################  
  50. ### Configure RPL  
  51. ################################################################################  
  52.   
  53. # Include the RPL layer if set to 1  
  54. PFLAGS += -DRPL_ROUTING=1  
  55.   
  56. # If set keep routing information in each node. If not the root must keep all  
  57. # routing information.  
  58. PFLAGS += -DRPL_STORING_MODE=1  
  59.   
  60. # Choose the objective function RPL should use  
  61. PFLAGS += -DRPL_OF_0=1  
  62. PFLAGS += -DRPL_OF_MRHOF=0  
  63.   
  64. ################################################################################  
  65. ### Configure LPL  
  66. ################################################################################  
  67.   
  68. #PFLAGS += -DLOW_POWER_LISTENING  
  69. #PFLAGS += -DLPL_SLEEP_INTERVAL=512  
  70. #PFLAGS += -DLPL_DEF_LOCAL_WAKEUP=512  
  71. #PFLAGS += -DLPL_DEF_REMOTE_WAKEUP=512  
  72.   
  73. ################################################################################  
  74. ### Configure printf() output  
  75. ################################################################################  
  76.   
  77. PFLAGS += -DNEW_PRINTF_SEMANTICS -DPRINTFUART_ENABLED -DPRINTF_BUFFER_SIZE=1024  
  78.   
  79. ################################################################################  
  80. ### Configure this application  
  81. ################################################################################  
  82.   
  83. # 5 second packet generation interval  
  84. CFLAGS+=-DPACKET_INTERVAL=5120UL  
  85.   
  86. CFLAGS+=-DRPL_ROOT_ADDR=11  
  87.   
  88.   
  89. # enable printf  
  90. CFLAGS += -DNEW_PRINTF_SEMANTICS -DPRINTFUART_ENABLED -DPRINTF_BUFFER_SIZE=1024  
  91.   
  92. include $(MAKERULES)  

突然发现Makefile内容多了不少,本质都是一些网络参数;需要注意的是

CFLAGS+=-DRPL_ROOT_ADDR=11  设定RPL路由的root节点地址是11;回忆上一部帖子介绍;至少我们需要编译这么一个节点了make cc2538cb blip id.11;


TestRPLAppC.nc文件:

  1. #include "TestRPL.h"  
  2. #include "printf.h"  
  3.   
  4. /**  
  5.  * Configuration for the RadioCountToLeds application. RadioCountToLeds  
  6.  * maintains a 4Hz counter, broadcasting its value in an AM packet  
  7.  * every time it gets updated. A RadioCountToLeds node that hears a counter  
  8.  * displays the bottom three bits on its LEDs. This application is a useful  
  9.  * test to show that basic AM communication and timers work.  
  10.  *  
  11.  * @author Philip Levis  
  12.  * @date   June 6 2005  
  13.  */  
  14.   
  15. configuration TestRPLAppC {  
  1. }  
  2. implementation {  
  3.   components MainC, TestRPLC as App, LedsC;  
  4.   components new TimerMilliC();  
  5.   components new TimerMilliC() as Timer;  
  6.   components RandomC;  
  7.   components RPLRankC;  
  8.   components RPLRoutingEngineC;  
  9.   components IPDispatchC;  
  10.   //components RPLForwardingEngineC;  
  11.   components RPLDAORoutingEngineC;  
  12.   components IPStackC;  
  13.   components IPProtocolsP;  
  14.   
  15.   App.Boot -> MainC.Boot;  
  16.   App.SplitControl -> IPStackC;//IPDispatchC;  
  17.   App.Leds -> LedsC;  
  18.   App.MilliTimer -> TimerMilliC;  
  19.   App.RPLRoute -> RPLRoutingEngineC;  
  20.   App.RootControl -> RPLRoutingEngineC;  
  21.   App.RoutingControl -> RPLRoutingEngineC;  
  22.   
  23.   components new UdpSocketC() as RPLUDP;  
  24.   App.RPLUDP -> RPLUDP;  
  25.   
  26.   App.RPLDAO -> RPLDAORoutingEngineC;  
  27.   App.Timer -> Timer;  
  28.   App.Random -> RandomC;  
  29.   
  30.   components StaticIPAddressC;  
  31.   
  32. #ifdef RPL_ROUTING  
  33.   components RPLRoutingC;  
  34. #endif  
  35.   
  36. #ifdef PRINTFUART_ENABLED  
  37.   
  38. #endif  
  39.   
  40. }  

哇,看着好复杂的样子,呵呵;咱们有神器YETI2;大家可以去图形化组件查看分析他


TestRPLC.nc文件:

  1. /*******************************************************************  
  2.  *实验6----zigbee roll路由实验  
  3.  *节点需求数 >= 2  
  4.  *编译命令make cc2538cb blip id.xx (xx为1~65533)  
  5.  ********************************************************************/  
  6.    
  7. #include "Timer.h"  
  8. #include "TestRPL.h"  
  9. #include "lib6lowpan/ip.h"  
  10. #include "blip_printf.h"  
  11. /**  
  12.  * Implementation of the RadioCountToLeds application. RadioCountToLeds   
  13.  * maintains a 4Hz counter, broadcasting its value in an AM packet   
  14.  * every time it gets updated. A RadioCountToLeds node that hears a counter   
  15.  * displays the bottom three bits on its LEDs. This application is a useful   
  16.  * test to show that basic AM communication and timers work.  
  17.  *  
  18.  * @author Philip Levis  
  19.  * @date   June 6 2005  
  20.  */  
  21.   
  22. module TestRPLC @safe() {  
  23.   uses {  
  24.     interface Leds;  
  25.     interface Boot;  
  26.     interface Timer<TMilli> as MilliTimer;  
  27.     interface Timer<TMilli> as Timer;  
  28.     interface RPLRoutingEngine as RPLRoute;  
  29.     interface RootControl;  
  30.     interface StdControl as RoutingControl;  
  31.     interface SplitControl;  
  32.     //interface IP as RPL;  
  33.     interface UDP as RPLUDP;  
  34.     //interface RPLForwardingEngine;  
  35.     interface RPLDAORoutingEngine as RPLDAO;  
  36.     interface Random;  
  37.   }  
  38. }  
  39. implementation {  
  40.   
  41. #ifndef RPL_ROOT_ADDR  
  42. #define RPL_ROOT_ADDR 1  
  43. #endif  
  44.   
  45. #define UDP_PORT 5678  
  46.   
  47.   //uint8_t payload[10];  
  48.   //struct in6_addr dest;  
  49.   struct in6_addr MULTICAST_ADDR;  
  50.   
  51.   bool locked;  
  52.   uint16_t counter = 0;  
  53.     
  54.   event void Boot.booted() {  
  55.     memset(MULTICAST_ADDR.s6_addr, 0, 16);  
  56.     MULTICAST_ADDR.s6_addr[0] = 0xFF;  
  57.     MULTICAST_ADDR.s6_addr[1] = 0x2;  
  58.     MULTICAST_ADDR.s6_addr[15] = 0x1A;  
  59.   
  60.   
  61.     if(TOS_NODE_ID == RPL_ROOT_ADDR){  
  62.       call RootControl.setRoot();  
  63.     }  
  64.     call RoutingControl.start();  
  65.     call SplitControl.start();  
  66.   
  67.     call RPLUDP.bind(UDP_PORT);  
  68.   }  
  69.   
  70.   
  71.   uint32_t countrx = 0;  
  72.   uint32_t counttx = 0;  
  73.   
  74.   event void RPLUDP.recvfrom(struct sockaddr_in6 *from, void *payload, uint16_t len, struct ip6_metadata *meta){  
  75.   
  76.     nx_uint16_t temp[10];  
  77.     memcpy(temp, (uint8_t*)payload, len);  
  78.     call Leds.led2Toggle();  
  79.       
  80.     printf(">>>> RX %d %d %d %lu \n", TOS_NODE_ID, temp[0], temp[9], ++countrx);  
  81.     printfflush();  
  82.   }  
  83.     
  84.   event void SplitControl.startDone(error_t err){  
  85.     while( call RPLDAO.startDAO() != SUCCESS );  
  86.       
  87.     if(TOS_NODE_ID != RPL_ROOT_ADDR){  
  88.       call Timer.startOneShot((call Random.rand16()%2)*2048U);  
  89.     }  
  90.   }  
  91.   
  92.   event void Timer.fired(){  
  93.     call MilliTimer.startOneShot(PACKET_INTERVAL + (call Random.rand16() % 100));  
  94.   }  
  95.   
  96.   task void sendTask(){  
  97.     struct sockaddr_in6 dest;  
  98.   
  99.     nx_uint16_t temp[10];  
  100.     uint8_t i;  
  101.   
  102.     for(i=0;i<10;i++){  
  103.       temp[i] = 0xABCD;  
  104.     }  
  105.   
  106.     temp[0] = TOS_NODE_ID;  
  107.     temp[9] = counttx;  
  108.   
  109.     memcpy(dest.sin6_addr.s6_addr, call RPLRoute.getDodagId(), sizeof(struct in6_addr));  
  110.   
  111.     if(dest.sin6_addr.s6_addr[15] != 0) // destination is set as root!  
  112.       ++counttx;  
  113.   
  114.     //if(dest.sin6_addr.s6_addr[0] == 0xAA)  
  115.     call Leds.led0Toggle();  
  116.   
  117.     dest.sin6_port = htons(UDP_PORT);  
  118.   
  119.     printf("Generate Packet at %d \n", TOS_NODE_ID);  
  120.     /**可以自行修改负载测试,假设是你采集的传感器数据呢******************/  
  121.     call RPLUDP.sendto(&dest, temp, 20);  
  122.   }  
  123.   
  124.   event void MilliTimer.fired(){  
  125.     //call Leds.led1Toggle();  
  126.     call MilliTimer.startOneShot(PACKET_INTERVAL + (call Random.rand16() % 100));  
  127.     post sendTask();  
  128.   }  
  129.   
  130.   event void SplitControl.stopDone(error_t err){}  
  131.   
  132. }  


if(TOS_NODE_ID != RPL_ROOT_ADDR){
      call Timer.startOneShot((call Random.rand16()%2)*2048U);

 }

需要注意一下,这是判断root节点,根据地址,大家也应该清楚如果我要改成别的id的方法了;

代码部分不多做介绍;注意

call RPLUDP.sendto(&dest, temp, 20);部分在前面取了地址,可以自己去看看如何取的;

这个例程因为是TinyOS的官方例程,大家实验一下就行了;在以后的例程RPL路由是基础;


但是我不推荐这种写法,一般来说的写法会有组网成功事件(event),也就是路由表添加事件

推荐那种写法;


调用发包的时候如果目的地址在路由表不存在将会怎么动作呢?交给聪明的你来分析;懒死我算了,呵呵!


现在大家可以自己参考这个例程编写RPL路由的应用,使用其实本质是很简单的,不要被这一堆代码吓到!


测试的时候两个节点,假设一个是11号(root),一个是12号

输入命令make cc2538cb blip id.11烧写

输入命令make cc2538cb blip id.12烧写

至于实验结果,大家可以插上cc2538cb到PC;默默的打开串口助手进行查看

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章