Contiki 3.x
shell-rime-unicast.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 #define MAX_DATALEN 32
61 #define UNICAST_MSG_HDRSIZE 2
62 struct unicast_msg {
63  uint16_t timestamp;
64  uint8_t data[MAX_DATALEN];
65 };
66 
67 static uint8_t is_receiving;
68 static struct unicast_conn uc;
69 /*---------------------------------------------------------------------------*/
70 PROCESS(shell_unicast_send_process, "unicast-send");
71 SHELL_COMMAND(unicast_send_command,
72  "unicast-send",
73  "unicast-send <node addr>: send data to specific neighbor",
74  &shell_unicast_send_process);
75 PROCESS(shell_unicast_recv_process, "unicast-recv");
76 SHELL_COMMAND(unicast_recv_command,
77  "unicast-recv",
78  "unicast-recv: receive data that neighbors send with unicast-send",
79  &shell_unicast_recv_process);
80 /*---------------------------------------------------------------------------*/
81 PROCESS_THREAD(shell_unicast_send_process, ev, data)
82 {
83  struct shell_input *input;
84  static linkaddr_t receiver;
85  int len;
86  const char *nextptr;
87  struct unicast_msg *msg;
88 
89  PROCESS_BEGIN();
90 
91  receiver.u8[0] = shell_strtolong(data, &nextptr);
92  if(nextptr == data || *nextptr != '.') {
93  shell_output_str(&unicast_send_command,
94  "unicast <receiver>: recevier must be specified", "");
95  PROCESS_EXIT();
96  }
97  ++nextptr;
98  receiver.u8[1] = shell_strtolong(nextptr, &nextptr);
99 
100  /* snprintf(buf, sizeof(buf), "%d.%d", receiver.u8[0], receiver.u8[1]);
101  shell_output_str(&unicast_send_command, "Sending unicast packets to ", buf);*/
102 
103  while(1) {
105  input = data;
106 
107  len = input->len1 + input->len2;
108 
109  if(len == 0) {
110  PROCESS_EXIT();
111  }
112 
113  if(len < MAX_DATALEN) {
114  packetbuf_clear();
115  packetbuf_set_datalen(len + UNICAST_MSG_HDRSIZE);
116  msg = packetbuf_dataptr();
117 
118  memcpy(msg->data, input->data1, input->len1);
119  memcpy(msg->data + input->len1, input->data2, input->len2);
120 
121 #if TIMESYNCH_CONF_ENABLED
122  msg->timestamp = timesynch_time();
123 #else
124  msg->timestamp = 0;
125 #endif
126  /* printf("Sending %d bytes\n", len);*/
127  unicast_send(&uc, &receiver);
128  }
129  }
130  PROCESS_END();
131 }
132 /*---------------------------------------------------------------------------*/
133 static void
134 recv_uc(struct unicast_conn *c, const linkaddr_t *from)
135 {
136  struct unicast_msg *msg;
137 #define OUTPUT_BLOB_HDRSIZE 6
138  struct {
139  uint16_t len;
140  uint16_t from;
141  uint16_t latency;
142  uint16_t data[MAX_DATALEN];
143  } output_blob;
144 
145  if(is_receiving == 0) {
146  return;
147  }
148 
149  msg = packetbuf_dataptr();
150 
151 #if TIMESYNCH_CONF_ENABLED
152  output_blob.latency = timesynch_time() - msg->timestamp;
153 #else
154  output_blob.latency = 0;
155 #endif
156  linkaddr_copy((linkaddr_t *)&output_blob.from, from);
157  memcpy(output_blob.data, msg->data, packetbuf_datalen() - UNICAST_MSG_HDRSIZE);
158  output_blob.len = 2 + (packetbuf_datalen() - UNICAST_MSG_HDRSIZE) / 2;
159  shell_output(&unicast_recv_command, &output_blob,
160  OUTPUT_BLOB_HDRSIZE + (packetbuf_datalen() - UNICAST_MSG_HDRSIZE),
161  NULL, 0);
162  /*
163  printf("unicast message received from %d.%d, latency %lu ms, data '%.*s'\n",
164  from->u8[0], from->u8[1],
165  (1000L * latency) / RTIMER_ARCH_SECOND,
166  packetbuf_datalen() - UNICAST_MSG_HDRSIZE,
167  msg->data);*/
168 
169 }
170 static const struct unicast_callbacks unicast_callbacks = {recv_uc};
171 
172 PROCESS_THREAD(shell_unicast_recv_process, ev, data)
173 {
174  PROCESS_EXITHANDLER(is_receiving = 0;);
175 
176  PROCESS_BEGIN();
177 
178  is_receiving = 1;
179 
180  while(1) {
182  }
183 
184  PROCESS_END();
185 }
186 /*---------------------------------------------------------------------------*/
187 void
188 shell_rime_unicast_init(void)
189 {
190  unicast_open(&uc, SHELL_RIME_CHANNEL_UNICAST,
191  &unicast_callbacks);
192  shell_register_command(&unicast_send_command);
193  shell_register_command(&unicast_recv_command);
194 }
195 /*---------------------------------------------------------------------------*/
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
#define NULL
The null pointer.
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.
void shell_output(struct shell_command *c, void *data1, int len1, const void *data2, int len2)
Output data from a shell command.
Definition: shell.c:395
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
Definition: packetbuf.c:239
void linkaddr_copy(linkaddr_t *dest, const linkaddr_t *src)
Copy a Rime address.
Definition: linkaddr.c:60
#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
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Definition: process.h:254
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_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#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
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