您的位置:首页 > 其它

Rime协议学习笔记:(三)匿名广播abc

2016-12-08 23:46 711 查看

三.匿名广播abc

3.1 abc相关定义

abc相关结构体:

struct abc_conn {
/**代表一个abc连接,包含一个通道channel和一个abc_callbacks*/
struct channel channel;
const struct abc_callbacks *u;
};

struct abc_callbacks {
/**Called when a packet has been received by the abc module. */
/**abc回调*/
void (* recv)(struct abc_conn *ptr);//接收数据包
void (* sent)(struct abc_conn *ptr, int status, int num_tx);//发送数据包
};


abc数据包相关属性:

/**abc.h中的定义*/
#define ABC_ATTRIBUTES//空定义,即代表abc通道属性为空

/**abc.c中的定义*/
static const struct packetbuf_attrlist attributes[] =
{ ABC_ATTRIBUTES  PACKETBUF_ATTR_LAST };


3.2 主要操作

3.2.1 abc_open

void abc_open(struct abc_conn *c, uint16_t channelno, const struct abc_callbacks *callbacks){
/**把通道channelno加入链表,再将一个abc连接到回调函数callbacks*/
channel_open(&c->channel, channelno);
c->u = callbacks;
channel_set_attributes(channelno, attributes);
}


3.2.2 abc_close

void abc_close(struct abc_conn *c){
/**把c的通道从链表中删除*/
channel_close(&c->channel);//关闭通道
}


3.2.3 abc_send

int abc_send(struct abc_conn *c){
/**调用rime_output函数(rime.c中),先将要发送的数据存入到packet_buf,再通过abc发送广播,若返回1表示发送成功,反之失败*/
PRINTF("%d.%d: abc: abc_send on channel %d\n",
linkaddr_node_addr.u8[0],linkaddr_node_addr.u8[1],
c->channel.channelno);
return rime_output(&c->channel);
/* int rime_output(struct channel *c){
RIMESTATS_ADD(tx);
if(chameleon_create(c)) {
packetbuf_compact();
NETSTACK_LLSEC.send(packet_sent, c);
return 1;
}
return 0;
}*/
}


3.3 abc广播实例

3.3.1 官方实例源码

#include "contiki.h"
#include "net/rime.h"
#include "random.h"
#include "dev/button-sensor.h"
#include "dev/leds.h"
#include <stdio.h>

PROCESS(example_abc_process, "ABC example");
AUTOSTART_PROCESSES(&example_abc_process);

/*用户自定义abc_recv回调函数:接收abc广播*/
static <
a4a5
span class="hljs-keyword">void abc_recv(struct abc_conn *c){
printf("abc message received '%s'\n",
(char *)packetbuf_dataptr());  //packetbuf_dataptr代表数据包的数据段的
}

static const struct abc_callbacks abc_call = {abc_recv};//调用回调函数
static struct abc_conn abc;

PROCESS_THREAD(example_abc_process, ev, data)
{
static struct etimer et;
PROCESS_EXITHANDLER(abc_close(&abc);)
PROCESS_BEGIN();
abc_open(&abc, 128, &abc_call);//打开abc广播
while(1) {
/* Delay 2-4 seconds */
etimer_set(&et, CLOCK_SECOND * 2 + random_rand() % (CLOCK_SECOND * 2));
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
packetbuf_copyfrom("Hello", 6);//发送的数据存放在数据包缓冲区内
abc_send(&abc);//发送abc广播
printf("abc message sent\n");
}
PROCESS_END();
}


3.3.2 Cooja仿真测试

  关于Cooja仿真器的使用,参考博文http://blog.csdn.net/u012325020/article/details/52965218

   使用Cooja仿真器新建一个仿真项目example_abc,添加两个节点,在Simulation control窗口中将Speed limit选择为100%,点击start开始仿真。



  放大Mote output窗口观察详细的数据传输结果



3.3.3 CC2538实测

  使用两个CC2538节点,分别烧入例程经过编译产生的.bin文件。将USB转串口模块与板子的UART口连接,将两个节点都插在电脑的USB口上。



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: