Contiki 3.x
shell-rime-debug-runicast.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Debugging command for testing reliable unicast (runicast)
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "contiki.h"
41 #include "contiki-conf.h"
42 #include "shell-rime-debug.h"
43 
44 #include "dev/leds.h"
45 
46 #include "lib/random.h"
47 
48 #include "net/rime/rime.h"
49 #include "net/rime/route.h"
50 #include "net/rime/trickle.h"
51 
52 #include "net/rime/timesynch.h"
53 
54 #include <stdio.h>
55 #ifndef HAVE_SNPRINTF
56 int snprintf(char *str, size_t size, const char *format, ...);
57 #endif /* HAVE_SNPRINTF */
58 #include <string.h>
59 
60 #define COLLECT_MSG_HDRSIZE 2
61 struct collect_msg {
62  uint16_t timestamp;
63  uint8_t data[1];
64 };
65 
66 static struct runicast_conn ruc;
67 /*---------------------------------------------------------------------------*/
68 PROCESS(shell_runicast_process, "runicast");
69 SHELL_COMMAND(runicast_command,
70  "runicast",
71  "runicast <node addr>: reliably unicast data to specific neighbor",
72  &shell_runicast_process);
73 /*---------------------------------------------------------------------------*/
74 PROCESS_THREAD(shell_runicast_process, ev, data)
75 {
76  struct shell_input *input;
77  static linkaddr_t receiver;
78  int len;
79  const char *nextptr;
80  struct collect_msg *msg;
81  char buf[30];
82 
83  PROCESS_BEGIN();
84 
85  receiver.u8[0] = shell_strtolong(data, &nextptr);
86  if(nextptr == data || *nextptr != '.') {
87  shell_output_str(&runicast_command,
88  "runicast <receiver>: recevier must be specified", "");
89  PROCESS_EXIT();
90  }
91  ++nextptr;
92  receiver.u8[1] = shell_strtolong(nextptr, &nextptr);
93 
94  snprintf(buf, sizeof(buf), "%d.%d", receiver.u8[0], receiver.u8[1]);
95  shell_output_str(&runicast_command, "Sending runicast packets to ", buf);
96 
97  while(1) {
99  input = data;
100 
101  len = input->len1 + input->len2;
102 
103  if(len == 0) {
104  PROCESS_EXIT();
105  }
106 
107  if(len < PACKETBUF_SIZE) {
108  packetbuf_clear();
109  packetbuf_set_datalen(len + COLLECT_MSG_HDRSIZE);
110  msg = packetbuf_dataptr();
111  memcpy(msg->data, input->data1, input->len1);
112  memcpy(msg->data + input->len1, input->data2, input->len2);
113 #if TIMESYNCH_CONF_ENABLED
114  msg->timestamp = timesynch_time();
115 #else
116  msg->timestamp = 0;
117 #endif
118  /* printf("Sending %d bytes\n", len);*/
119  runicast_send(&ruc, &receiver, 4);
120  }
121  }
122  PROCESS_END();
123 }
124 static void
125 recv_ruc(struct runicast_conn *c, const linkaddr_t *from, uint8_t seqno)
126 {
127  struct collect_msg *msg;
128  rtimer_clock_t latency;
129 #if TIMESYNCH_CONF_ENABLED
130  rtimer_clock_t timestamp;
131 #endif /* TIMESYNCH_CONF_ENABLED */
132 
133  msg = packetbuf_dataptr();
134 
135 #if TIMESYNCH_CONF_ENABLED
136  memcpy(&timestamp, &msg->timestamp, sizeof(timestamp));
137  latency = timesynch_time() - timestamp;
138 #else
139  latency = 0;
140 #endif
141 
142  printf("runicast message received from %d.%d, latency %lu ms, seqno %d, data '%.*s'\n",
143  from->u8[0], from->u8[1],
144  (1000L * latency) / RTIMER_ARCH_SECOND,
145  seqno,
146  packetbuf_datalen() - COLLECT_MSG_HDRSIZE,
147  msg->data);
148 }
149 static void
150 sent_ruc(struct runicast_conn *c, const linkaddr_t *to, uint8_t tx)
151 {
152  printf("runicast message sent to %d.%d, %d transmissions\n",
153  to->u8[0], to->u8[1],
154  tx);
155 }
156 static void
157 timedout_ruc(struct runicast_conn *c, const linkaddr_t *to, uint8_t tx)
158 {
159  printf("runicast message to %d.%d timed out after %d transmissions\n",
160  to->u8[0], to->u8[1],
161  tx);
162 }
163 static const struct runicast_callbacks runicast_callbacks = {recv_ruc,
164  sent_ruc,
165  timedout_ruc};
166 /*---------------------------------------------------------------------------*/
167 void
168 shell_rime_debug_runicast_init(void)
169 {
170  runicast_open(&ruc, SHELL_RIME_CHANNEL_RUNICAST,
171  &runicast_callbacks);
172  shell_register_command(&runicast_command);
173 }
174 /*---------------------------------------------------------------------------*/
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
Header file for a simple time synchronization mechanism
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition: packetbuf.c:200
Header file for the Rime stack
rtimer_clock_t timesynch_time(void)
Get the current time-synchronized time.
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
Definition: shell.c:521
Structure for shell input data.
Definition: shell.h:365
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:77
Header file for Trickle (reliable single source flooding) for Rime
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition: packetbuf.h:65
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
A brief description of what this file is.
Header file for the Rime route table