Contiki 3.x
shell-rime-ping.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  * The Contiki shell Rime ping application
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 
41 #include "shell.h"
42 #include "net/rime/rime.h"
43 
44 #include <string.h>
45 #include <stdio.h>
46 #ifndef HAVE_SNPRINTF
47 int snprintf(char *str, size_t size, const char *format, ...);
48 #endif /* HAVE_SNPRINTF */
49 
50 struct rime_ping_msg {
51  rtimer_clock_t pingtime;
52  rtimer_clock_t pongtime;
53 };
54 
55 static struct mesh_conn mesh;
56 static int waiting_for_pong = 0;
57 
58 /*---------------------------------------------------------------------------*/
59 PROCESS(shell_rime_ping_process, "rime-ping");
60 SHELL_COMMAND(rime_ping_command,
61  "rime-ping",
62  "rime-ping <node addr>: send a message to a specific node and get a reply",
63  &shell_rime_ping_process);
64 /*---------------------------------------------------------------------------*/
65 PROCESS_THREAD(shell_rime_ping_process, ev, data)
66 {
67  static int i;
68  static struct etimer timeout, periodic;
69  static linkaddr_t receiver;
70  struct rime_ping_msg *ping;
71  const char *nextptr;
72  char buf[32];
73 
74  PROCESS_BEGIN();
75 
76  receiver.u8[0] = shell_strtolong(data, &nextptr);
77  if(nextptr == data || *nextptr != '.') {
78  shell_output_str(&rime_ping_command,
79  "ping <receiver>: recevier must be specified", "");
80  PROCESS_EXIT();
81  }
82  ++nextptr;
83  receiver.u8[1] = shell_strtolong(nextptr, &nextptr);
84 
85  snprintf(buf, sizeof(buf), "%d.%d", receiver.u8[0], receiver.u8[1]);
86  shell_output_str(&rime_ping_command, "Sending 4 pings to ", buf);
87 
88  for(i = 0; i < 4; ++i) {
90  ping = packetbuf_dataptr();
91  packetbuf_set_datalen(sizeof(struct rime_ping_msg));
92 #if TIMESYNCH_CONF_ENABLED
93  ping->pingtime = timesynch_time();
94 #else
95  ping->pingtime = rtimer_arch_now();
96 #endif
97  mesh_send(&mesh, &receiver);
98 
99  etimer_set(&timeout, CLOCK_SECOND * 8);
100  etimer_set(&periodic, CLOCK_SECOND * 1);
101  waiting_for_pong = 1;
103  waiting_for_pong == 0);
104  if(waiting_for_pong == 0) {
106  } else {
107  shell_output_str(&rime_ping_command,
108  "Timed out", "");
109  }
110  waiting_for_pong = 0;
111  }
112  PROCESS_END();
113 }
114 /*---------------------------------------------------------------------------*/
115 static void
116 timedout_mesh(struct mesh_conn *c)
117 {
118  /* printf("packet timedout\n");*/
119 }
120 static void
121 sent_mesh(struct mesh_conn *c)
122 {
123 }
124 static void
125 recv_mesh(struct mesh_conn *c, const linkaddr_t *from, uint8_t hops)
126 {
127  struct rime_ping_msg ping;
128  char buf[64];
129  rtimer_clock_t pingrecvtime;
130 
131  memcpy(&ping, packetbuf_dataptr(), sizeof(struct rime_ping_msg));
132 
133  if(waiting_for_pong == 0) {
134 #if TIMESYNCH_CONF_ENABLED
135  ping.pongtime = timesynch_time();
136 #else
137  ping.pongtime = ping.pingtime;
138 #endif
139  memcpy(packetbuf_dataptr(), &ping, sizeof(struct rime_ping_msg));
140  mesh_send(&mesh, from);
141  } else {
142 #if TIMESYNCH_CONF_ENABLED
143  pingrecvtime = timesynch_time();
144 #else
145  pingrecvtime = rtimer_arch_now();
146 #endif
147  snprintf(buf, sizeof(buf), "%lu ms (%lu + %lu), %d hops.",
148  (1000L * (pingrecvtime - ping.pingtime)) / RTIMER_ARCH_SECOND,
149  (1000L * (ping.pongtime - ping.pingtime)) / RTIMER_ARCH_SECOND,
150  (1000L * (pingrecvtime - ping.pongtime)) / RTIMER_ARCH_SECOND,
151  hops);
152 
153  shell_output_str(&rime_ping_command,
154  "Pong recived; rtt ", buf);
155  waiting_for_pong = 0;
156  process_post(&shell_rime_ping_process, PROCESS_EVENT_CONTINUE, NULL);
157  }
158 }
159 CC_CONST_FUNCTION static struct mesh_callbacks mesh_callbacks = { recv_mesh,
160  sent_mesh,
161  timedout_mesh };
162 /*---------------------------------------------------------------------------*/
163 void
164 shell_rime_ping_init(void)
165 {
166  mesh_open(&mesh, SHELL_RIME_CHANNEL_PING, &mesh_callbacks);
167 
168  shell_register_command(&rime_ping_command);
169 }
170 /*---------------------------------------------------------------------------*/
int mesh_send(struct mesh_conn *c, const linkaddr_t *to)
Send a mesh packet.
Definition: mesh.c:184
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:205
#define PROCESS_WAIT_UNTIL(c)
Wait for a condition to occur.
Definition: process.h:192
#define CC_CONST_FUNCTION
Configure if the C compiler have problems with const function pointers.
Definition: cc.h:86
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
Main header file for the Contiki shell
#define NULL
The null pointer.
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
Mesh callbacks.
Definition: mesh.h:73
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
rtimer_clock_t timesynch_time(void)
Get the current time-synchronized time.
#define rtimer_arch_now()
Definition: rtimer-arch.h:40
#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
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
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition: packetbuf.c:77
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177
void mesh_open(struct mesh_conn *c, uint16_t channels, const struct mesh_callbacks *callbacks)
Open a mesh connection.
Definition: mesh.c:164
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition: packetbuf.c:207
A timer.
Definition: etimer.h:76
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82