在 simple_udp_connection 内得到回应

Getting response in simple_udp_connection

我在 contiki cooja 中有一个代码。如图所示,我有 2 个发送节点和 2 个接收节点1。广播发送者和接收者如下。

#include "contiki.h"
#include "lib/random.h"
#include "sys/etimer.h"
#include "net/ip/uip.h"
#include "net/ipv6/uip-ds6.h"
#include "net/ip/uip-debug.h"


#include "simple-udp.h"


#include <stdio.h>
#include <string.h>



#define SEND_INTERVAL       (20 * CLOCK_SECOND)
#define SEND_TIME       (random_rand() % (SEND_INTERVAL))

static struct simple_udp_connection broadcast_connection;


/*---------------------------------------------------------------------------*/
PROCESS(broadcast_example_process, "UDP broadcast example process");
AUTOSTART_PROCESSES(&broadcast_example_process);
/*---------------------------------------------------------------------------*/
static void
receiver(struct simple_udp_connection *c,
         const uip_ipaddr_t *sender_addr,
         uint16_t sender_port,
         const uip_ipaddr_t *receiver_addr,
         uint16_t receiver_port,
         const uint8_t *data,
         uint16_t datalen)
{
 printf("Data received from ");
  uip_debug_ipaddr_print(sender_addr);
  printf(" on port %d from port %d with length %d:  %s \n",
         receiver_port, sender_port, datalen, data);

}
/*---------------------------------------------------------------------------*/

PROCESS_THREAD(broadcast_example_process, ev, data)
{



  static struct etimer periodic_timer;
  static struct etimer send_timer;
  uip_ipaddr_t addr;
  char message[20];
  sprintf(message," hello ");

  PROCESS_BEGIN();

  simple_udp_register(&broadcast_connection, 1234,
                      NULL, 1900,
                      receiver);
  etimer_set(&periodic_timer, SEND_INTERVAL);
  while(1) {



    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&periodic_timer));
    etimer_reset(&periodic_timer);
    etimer_set(&send_timer, SEND_TIME);

    PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&send_timer));

    printf("Sending broadcast\n");
    uip_create_linklocal_allnodes_mcast(&addr);
    simple_udp_sendto(&broadcast_connection, message , strlen(message) , &addr);

  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

和接收者:

#include "contiki.h"
#include "lib/random.h"
#include "sys/ctimer.h"
#include "sys/etimer.h"
#include "net/ip/uip.h"
#include "net/ipv6/uip-ds6.h"

#include "simple-udp.h"
#include "net/ip/uip-debug.h"

#include <stdio.h>
#include <string.h>

#define UDP_PORT 1900



static struct simple_udp_connection broadcast_connection;


/*---------------------------------------------------------------------------*/
PROCESS(broadcast_example_process, "UDP broadcast example process");
AUTOSTART_PROCESSES(&broadcast_example_process);
/*---------------------------------------------------------------------------*/
static void
receiver(struct simple_udp_connection *c,
         const uip_ipaddr_t *sender_addr,
         uint16_t sender_port,
         const uip_ipaddr_t *receiver_addr,
         uint16_t receiver_port,
         const uint8_t *data,
         uint16_t datalen)
{
  printf("Data received from ");
  uip_debug_ipaddr_print(sender_addr);
  printf(" on port %d from port %d with length %d: %s \n",
         receiver_port, sender_port, datalen, data);
 /* this line should work,right?*/
   simple_udp_sendto(&broadcast_connection, "hello",5, sender_addr);

}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(broadcast_example_process, ev, data)
{


  PROCESS_BEGIN();
  simple_udp_register(&broadcast_connection, 1900,
                      NULL, 1234,
                      receiver);


  while(1) {
    PROCESS_WAIT_EVENT();




  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/

现在问题是回复发件人。我希望每个接收方节点回复向其发送该消息的发送方。 我不知道该怎么做? simple_udp_sendto() 不 work.Can 我用这个函数做这个工作?我应该如何使用它?

您通常有一个 RDC 函数:packet_received。当一个 IT 触发器时,packet_received 被调用并使用 ptr 中的数据。您只需要读取发件人的地址重新发送即可。

编辑:您可能在网络层上有一个 packet_received,具有相同的功能。如果你不这样做,你应该实施一个。