在contiki中使用rime时如何管理指针? [英] How do I manage pointers when using rime in contiki?
问题描述
在我的场景中我有2个节点:发送者和接收者。
节点1向接收者发送数据包,请求是一些指向某些字符串的char指针数组:
Hi, In my scenario I have 2 node: sender and receiver.
node 1 sends packets to receiver and request is an array of char pointers to some strings:
packetbuf_copyfrom(request[i], strlen(request[i]));
在接收器中我想将收到的数据包添加到列表中:
但是当我想打印收到的数据包列表时,它只打印最后一个数据包收到,即。所有列表元素都指向最后一个字符串。
如何管理指针来解决这个问题?
我尝试过:
And in receiver I want to add received packets to a list :
but when I want to print the list of received packets it prints only the last packet received, ie. all of list elements points to the last string.
How do I manage pointers to solve this issue?
What I have tried:
#include "contiki.h"
#include "dev/leds.h"
#include <stdio.h>
#define MAX_RECORD 20
struct record{
struct record *next;
char* message;
};
MEMB(record_memb, struct record,MAX_RECORD);
LIST(record_list);
/*---------------------------------------------------------------------------*/
PROCESS(example_unicast_process, "Example unicast");
AUTOSTART_PROCESSES(&example_unicast_process);
/*---------------------------------------------------------------------------*/
static void
recv_uc(struct unicast_conn *c, const linkaddr_t *from)
{
printf("unicast message received from %d.%d : %s\n",
from->u8[0], from->u8[1],(char *)packetbuf_dataptr());
char* temp = (char *)packetbuf_dataptr();
struct record *n;
n = memb_alloc(&record_memb);
n-> message = temp;
list_add(record_list,n);
for(n = list_head(record_list); n != NULL; n = n->next) {
printf(" = %s \n", n->message);
}//end for
}
static const struct unicast_callbacks unicast_callbacks = {recv_uc};
static struct unicast_conn uc;
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(example_unicast_process, ev, data)
{
PROCESS_EXITHANDLER(unicast_close(&uc);)
PROCESS_BEGIN();
unicast_open(&uc, 146, &unicast_callbacks);
static int i = 1;
while(1) {
static struct etimer et;
linkaddr_t addr;
etimer_set(&et, CLOCK_SECOND);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
char * arr= "hello";
if(i == 2 ){
i = 1; packetbuf_copyfrom("hello", 5);
} else { packetbuf_copyfrom("good bye baby", 14); i = 2; }
addr.u8[0] = 1;
addr.u8[1] = 0;
if(!linkaddr_cmp(&addr, &linkaddr_node_addr)) {
unicast_send(&uc, &addr);
}
}
PROCESS_END();
}
/*------------
推荐答案
但是当我想要打印收到的数据包列表,它只打印最后收到的数据包,即。所有列表元素都指向最后一个字符串。
but when I want to print the list of received packets it prints only the last packet received, ie. all of list elements points to the last string.
这是因为记录
结构的消息
成员是一个 char *
指针,对于所有添加的项目,它被设置为 packetbuf_dataptr()
。此数据包缓冲区当然包含最后收到的消息。
您必须为字符串分配内存并复制它们:
That is because the message
member of your record
structure is a char*
pointer which is set to packetbuf_dataptr()
for all added items. This packet buffer contains off course always the last received message.
You have to allocate memory for the strings and copy them:
n = memb_alloc(&record_memb);
n->message = malloc(1 + strlen(temp));
strcpy(n->message, temp);
list_add(record_list,n);
请注意,从列表中删除项目时以及删除列表本身之前,您已删除已分配的内存。
Note that you have delete the allocated memory when removing items from the list and before deleting the list itself.
这篇关于在contiki中使用rime时如何管理指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!