Contiki 3.x
shell-rime-debug.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  * A brief description of what this file is.
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 
61 #define COLLECT_MSG_HDRSIZE 2
62 struct collect_msg {
63  uint16_t timestamp;
64  uint8_t data[1];
65 };
66 
67 static struct broadcast_conn broadcast;
68 static struct unicast_conn uc;
69 /*---------------------------------------------------------------------------*/
70 PROCESS(shell_broadcast_process, "broadcast");
71 SHELL_COMMAND(broadcast_command,
72  "broadcast",
73  "broadcast: broadcast data to all neighbors",
74  &shell_broadcast_process);
75 PROCESS(shell_unicast_process, "unicast");
76 SHELL_COMMAND(unicast_command,
77  "unicast",
78  "unicast <node addr>: unicast data to specific neighbor",
79  &shell_unicast_process);
80 /*---------------------------------------------------------------------------*/
81 PROCESS_THREAD(shell_broadcast_process, ev, data)
82 {
83  struct shell_input *input;
84  int len;
85  struct collect_msg *msg;
86 
87  PROCESS_BEGIN();
88 
89  while(1) {
91  input = data;
92 
93  len = input->len1 + input->len2;
94 
95  if(len == 0) {
96  PROCESS_EXIT();
97  }
98 
99  if(len < PACKETBUF_SIZE) {
100  packetbuf_clear();
101  packetbuf_set_datalen(len + COLLECT_MSG_HDRSIZE);
102  msg = packetbuf_dataptr();
103  memcpy(msg->data, input->data1, input->len1);
104  memcpy(msg->data + input->len1, input->data2, input->len2);
105 #if TIMESYNCH_CONF_ENABLED
106  msg->timestamp = timesynch_time();
107 #else
108  msg->timestamp = 0;
109 #endif
110  /* printf("Sending %d bytes\n", len);*/
111  broadcast_send(&broadcast);
112  }
113  }
114  PROCESS_END();
115 }
116 static void
117 recv_broadcast(struct broadcast_conn *c, const linkaddr_t *from)
118 {
119  struct collect_msg *msg;
120  rtimer_clock_t latency;
121 
122  msg = packetbuf_dataptr();
123 
124 #if TIMESYNCH_CONF_ENABLED
125  latency = timesynch_time() - msg->timestamp;
126 #else
127  latency = 0;
128 #endif
129 
130  printf("broadcast message received from %d.%d, latency %lu ms, data '%.*s'\n",
131  from->u8[0], from->u8[1],
132  (1000L * latency) / RTIMER_ARCH_SECOND,
133  packetbuf_datalen() - COLLECT_MSG_HDRSIZE,
134  msg->data);
135 }
136 static const struct broadcast_callbacks broadcast_callbacks = {recv_broadcast};
137 /*---------------------------------------------------------------------------*/
138 PROCESS_THREAD(shell_unicast_process, ev, data)
139 {
140  struct shell_input *input;
141  static linkaddr_t receiver;
142  int len;
143  const char *nextptr;
144  struct collect_msg *msg;
145  char buf[30];
146 
147  PROCESS_BEGIN();
148 
149  receiver.u8[0] = shell_strtolong(data, &nextptr);
150  if(nextptr == data || *nextptr != '.') {
151  shell_output_str(&unicast_command,
152  "unicast <receiver>: recevier must be specified", "");
153  PROCESS_EXIT();
154  }
155  ++nextptr;
156  receiver.u8[1] = shell_strtolong(nextptr, &nextptr);
157 
158  snprintf(buf, sizeof(buf), "%d.%d", receiver.u8[0], receiver.u8[1]);
159  shell_output_str(&unicast_command, "Sending unicast packets to ", buf);
160 
161  while(1) {
163  input = data;
164 
165  len = input->len1 + input->len2;
166 
167  if(len == 0) {
168  PROCESS_EXIT();
169  }
170 
171  if(len < PACKETBUF_SIZE) {
172  packetbuf_clear();
173  packetbuf_set_datalen(len + COLLECT_MSG_HDRSIZE);
174  msg = packetbuf_dataptr();
175  memcpy(msg->data, input->data1, input->len1);
176  memcpy(msg->data + input->len1, input->data2, input->len2);
177 #if TIMESYNCH_CONF_ENABLED
178  msg->timestamp = timesynch_time();
179 #else
180  msg->timestamp = 0;
181 #endif
182  /* printf("Sending %d bytes\n", len);*/
183  unicast_send(&uc, &receiver);
184  }
185  }
186  PROCESS_END();
187 }
188 static void
189 recv_uc(struct unicast_conn *c, const linkaddr_t *from)
190 {
191  struct collect_msg *msg;
192  rtimer_clock_t latency;
193 
194  msg = packetbuf_dataptr();
195 
196 #if TIMESYNCH_CONF_ENABLED
197  latency = timesynch_time() - msg->timestamp;
198 #else
199  latency = 0;
200 #endif
201 
202  printf("unicast message received from %d.%d, latency %lu ms, data '%.*s'\n",
203  from->u8[0], from->u8[1],
204  (1000L * latency) / RTIMER_ARCH_SECOND,
205  packetbuf_datalen() - COLLECT_MSG_HDRSIZE,
206  msg->data);
207 }
208 static const struct unicast_callbacks unicast_callbacks = {recv_uc};
209 /*---------------------------------------------------------------------------*/
210 void
211 shell_rime_debug_init(void)
212 {
213  unicast_open(&uc, SHELL_RIME_CHANNEL_UNICAST,
214  &unicast_callbacks);
215  broadcast_open(&broadcast, SHELL_RIME_CHANNEL_BROADCAST,
216  &broadcast_callbacks);
217  shell_register_command(&broadcast_command);
218  shell_register_command(&unicast_command);
219 }
220 /*---------------------------------------------------------------------------*/
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
Callback structure for broadcast.
Definition: broadcast.h:80
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 broadcast_send(struct broadcast_conn *c)
Send an identified best-effort broadcast packet.
Definition: broadcast.c:111
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
void broadcast_open(struct broadcast_conn *c, uint16_t channel, const struct broadcast_callbacks *u)
Set up an identified best-effort broadcast connection.
Definition: broadcast.c:96
#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