Total Number of Packets Sent and Received in Contiki

459 Views Asked by At

I'm trying to get the total number of packets transmitted and packets received in RPL. I'm using upd-client.c and udp-server.c and I added printf lines in both files as the following:

PROCESS(udp_client_process, "UDP client process");
AUTOSTART_PROCESSES(&udp_client_process);
/*---------------------------------------------------------------------------*/
static void
tcpip_handler(void)
{
  char *str;

  if(uip_newdata()) {
    str = uip_appdata;
    str[uip_datalen()] = '\0';
    printf("Data recv");
    //printf("DATA recv '%s'\n", str);
  }
}
/*---------------------------------------------------------------------------*/
static void
send_packet(void *ptr)
{
  static int seq_id;
  char buf[MAX_PAYLOAD_LEN];

  seq_id++;
  printf("Data send");
  PRINTF("DATA send to %d 'Hello %d'\n",
         server_ipaddr.u8[sizeof(server_ipaddr.u8) - 1], seq_id);
  sprintf(buf, "Hello %d from the client", seq_id);
  uip_udp_packet_sendto(client_conn, buf, strlen(buf),
                        &server_ipaddr, UIP_HTONS(UDP_SERVER_PORT));
}
PROCESS(udp_server_process, "UDP server process");
AUTOSTART_PROCESSES(&udp_server_process);
/*---------------------------------------------------------------------------*/
static void
tcpip_handler(void)
{
  char *appdata;

  if(uip_newdata()) {
    appdata = (char *)uip_appdata;
    appdata[uip_datalen()] = 0;
    printf("Data recv");
   // PRINTF("DATA recv '%s' from ", appdata);
    PRINTF("%d",
           UIP_IP_BUF->srcipaddr.u8[sizeof(UIP_IP_BUF->srcipaddr.u8) - 1]);
    PRINTF("\n");
#if SERVER_REPLY
    PRINTF("DATA sending reply\n");
    uip_ipaddr_copy(&server_conn->ripaddr, &UIP_IP_BUF->srcipaddr);
    uip_udp_packet_send(server_conn, "Reply", sizeof("Reply"));
    uip_create_unspecified(&server_conn->ripaddr);
#endif
  }
}

Then in the simulation script editor, I counted the printed messages:

while (true) {
if (msg) {
    if(msg.startsWith("Data send")) {
        num_messages_tx += 1;
    }
    if(msg.startsWith("Data recv")) {
        num_messages_rx += 1;
    }
}

I'm not sure what I'm doing wrong but it seems that I always have equal numbers of transmitted and received packets.

I appreciate your help.. Hanin

1

There are 1 best solutions below

0
On BEST ANSWER

in the project-conf.h define below macros and the contiki link-stats.c count them for you.

#define LOG_CONF_LEVEL_MAC LOG_LEVEL_INFO
#define LINK_STATS_CONF_PACKET_COUNTERS 1