Contiki 3.x
shell-sky.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  * Tmote Sky-specific Contiki shell commands
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "contiki.h"
41 #include "sys/cc.h"
42 #include "shell-sky.h"
43 
44 #include "dev/watchdog.h"
45 
46 #include "net/rime/rime.h"
47 #include "net/netstack.h"
48 #include "cc2420.h"
49 #include "dev/leds.h"
50 #include "dev/sht11/sht11.h"
51 #include "dev/light-sensor.h"
52 #include "dev/battery-sensor.h"
53 #include "dev/sht11/sht11-sensor.h"
54 #include "net/rime/timesynch.h"
55 
56 #include "sys/node-id.h"
57 
58 #include <stdio.h>
59 #include <string.h>
60 
61 /*---------------------------------------------------------------------------*/
62 PROCESS(shell_nodeid_process, "nodeid");
63 SHELL_COMMAND(nodeid_command,
64  "nodeid",
65  "nodeid: set node ID",
66  &shell_nodeid_process);
67 PROCESS(shell_sense_process, "sense");
68 SHELL_COMMAND(sense_command,
69  "sense",
70  "sense: print out sensor data",
71  &shell_sense_process);
72 PROCESS(shell_senseconv_process, "senseconv");
73 SHELL_COMMAND(senseconv_command,
74  "senseconv",
75  "senseconv: convert 'sense' data to human readable format",
76  &shell_senseconv_process);
77 PROCESS(shell_txpower_process, "txpower");
78 SHELL_COMMAND(txpower_command,
79  "txpower",
80  "txpower <power>: change CC2420 transmission power (0 - 31)",
81  &shell_txpower_process);
82 PROCESS(shell_rfchannel_process, "rfchannel");
83 SHELL_COMMAND(rfchannel_command,
84  "rfchannel",
85  "rfchannel <channel>: change CC2420 radio channel (11 - 26)",
86  &shell_rfchannel_process);
87 /*---------------------------------------------------------------------------*/
88 struct spectrum {
89  int channel[16];
90 };
91 #define NUM_SAMPLES 4
92 static struct spectrum rssi_samples[NUM_SAMPLES];
93 static int
94 do_rssi(void)
95 {
96  static int sample;
97  int channel;
98 
99  NETSTACK_MAC.off(0);
100 
101  cc2420_on();
102  for(channel = 11; channel <= 26; ++channel) {
103  cc2420_set_channel(channel);
104  rssi_samples[sample].channel[channel - 11] = cc2420_rssi() + 53;
105  }
106 
107  NETSTACK_MAC.on();
108 
109  sample = (sample + 1) % NUM_SAMPLES;
110 
111  {
112  int channel, tot;
113  tot = 0;
114  for(channel = 0; channel < 16; ++channel) {
115  int max = -256;
116  int i;
117  for(i = 0; i < NUM_SAMPLES; ++i) {
118  max = MAX(max, rssi_samples[i].channel[channel]);
119  }
120  tot += max / 20;
121  }
122  return tot;
123  }
124 }
125 /*---------------------------------------------------------------------------*/
126 struct sense_msg {
127  uint16_t len;
128  uint16_t clock;
129  uint16_t timesynch_time;
130  uint16_t light1;
131  uint16_t light2;
132  uint16_t temp;
133  uint16_t humidity;
134  uint16_t rssi;
135  uint16_t voltage;
136 };
137 /*---------------------------------------------------------------------------*/
138 PROCESS_THREAD(shell_sense_process, ev, data)
139 {
140  struct sense_msg msg;
141  PROCESS_BEGIN();
142 
143  SENSORS_ACTIVATE(light_sensor);
144  SENSORS_ACTIVATE(battery_sensor);
145  SENSORS_ACTIVATE(sht11_sensor);
146 
147  msg.len = 7;
148  msg.clock = clock_time();
149 #if TIMESYNCH_CONF_ENABLED
150  msg.timesynch_time = timesynch_time();
151 #else /* TIMESYNCH_CONF_ENABLED */
152  msg.timesynch_time = 0;
153 #endif /* TIMESYNCH_CONF_ENABLED */
154  msg.light1 = light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC);
155  msg.light2 = light_sensor.value(LIGHT_SENSOR_TOTAL_SOLAR);
156  msg.temp = sht11_sensor.value(SHT11_SENSOR_TEMP);
157  msg.humidity = sht11_sensor.value(SHT11_SENSOR_HUMIDITY);
158  msg.rssi = do_rssi();
159  msg.voltage = battery_sensor.value(0);
160 
161  msg.rssi = do_rssi();
162 
163  SENSORS_DEACTIVATE(light_sensor);
164  SENSORS_DEACTIVATE(battery_sensor);
165  SENSORS_DEACTIVATE(sht11_sensor);
166 
167  shell_output(&sense_command, &msg, sizeof(msg), "", 0);
168  PROCESS_END();
169 }
170 /*---------------------------------------------------------------------------*/
171 PROCESS_THREAD(shell_senseconv_process, ev, data)
172 {
173  struct shell_input *input;
174  struct sense_msg *msg;
175  PROCESS_BEGIN();
176  while(1) {
178  input = data;
179 
180  if(input->len1 + input->len2 == 0) {
181  PROCESS_EXIT();
182  }
183  msg = (struct sense_msg *)input->data1;
184 
185  if(msg != NULL) {
186  char buf[40];
187  snprintf(buf, sizeof(buf),
188  "%d", 10 * msg->light1 / 7);
189  shell_output_str(&senseconv_command, "Light 1 ", buf);
190  snprintf(buf, sizeof(buf),
191  "%d", 46 * msg->light2 / 10);
192  shell_output_str(&senseconv_command, "Light 2 ", buf);
193  snprintf(buf, sizeof(buf),
194  "%d.%d", (msg->temp / 10 - 396) / 10,
195  (msg->temp / 10 - 396) % 10);
196  shell_output_str(&senseconv_command, "Temperature ", buf);
197  snprintf(buf, sizeof(buf),
198  "%d", (int)(-4L + 405L * msg->humidity / 10000L));
199  shell_output_str(&senseconv_command, "Relative humidity ", buf);
200  snprintf(buf, sizeof(buf),
201  "%d", msg->rssi);
202  shell_output_str(&senseconv_command, "RSSI ", buf);
203  snprintf(buf, sizeof(buf), /* 819 = 4096 / 5 */
204  "%d.%d", (msg->voltage / 819), (10 * msg->voltage / 819) % 10);
205  shell_output_str(&senseconv_command, "Voltage ", buf);
206  }
207  }
208  PROCESS_END();
209 }
210 /*---------------------------------------------------------------------------*/
211 PROCESS_THREAD(shell_txpower_process, ev, data)
212 {
213  struct {
214  uint16_t len;
215  uint16_t txpower;
216  } msg;
217  const char *newptr;
218  PROCESS_BEGIN();
219 
220  msg.txpower = shell_strtolong(data, &newptr);
221 
222  /* If no transmission power was given on the command line, we print
223  out the current txpower. */
224 
225  if(newptr == data) {
226  msg.txpower = cc2420_get_txpower();
227  } else {
228  cc2420_set_txpower(msg.txpower);
229  }
230 
231  msg.len = 1;
232 
233  shell_output(&txpower_command, &msg, sizeof(msg), "", 0);
234 
235  PROCESS_END();
236 }
237 /*---------------------------------------------------------------------------*/
238 PROCESS_THREAD(shell_rfchannel_process, ev, data)
239 {
240  struct {
241  uint16_t len;
242  uint16_t channel;
243  } msg;
244  const char *newptr;
245  PROCESS_BEGIN();
246 
247  msg.channel = shell_strtolong(data, &newptr);
248 
249  /* If no channel was given on the command line, we print out the
250  current channel. */
251  if(newptr == data) {
252  msg.channel = cc2420_get_channel();
253  } else {
254  cc2420_set_channel(msg.channel);
255  }
256 
257  msg.len = 1;
258 
259  shell_output(&rfchannel_command, &msg, sizeof(msg), "", 0);
260 
261  PROCESS_END();
262 }
263 /*---------------------------------------------------------------------------*/
264 PROCESS_THREAD(shell_nodeid_process, ev, data)
265 {
266  uint16_t nodeid;
267  char buf[20];
268  const char *newptr;
269  PROCESS_BEGIN();
270 
271  nodeid = shell_strtolong(data, &newptr);
272 
273  /* If no node ID was given on the command line, we print out the
274  current channel. Else we burn the new node ID. */
275  if(newptr == data) {
276  nodeid = node_id;
277  } else {
278  nodeid = shell_strtolong(data, &newptr);
279  watchdog_stop();
280  leds_on(LEDS_RED);
281  node_id_burn(nodeid);
282  leds_on(LEDS_BLUE);
283  node_id_restore();
284  leds_off(LEDS_RED + LEDS_BLUE);
285  watchdog_start();
286  }
287 
288  snprintf(buf, sizeof(buf), "%d", nodeid);
289  shell_output_str(&nodeid_command, "Node ID: ", buf);
290 
291  PROCESS_END();
292 }
293 /*---------------------------------------------------------------------------*/
294 void
295 shell_sky_init(void)
296 {
297  shell_register_command(&txpower_command);
298  shell_register_command(&rfchannel_command);
299  shell_register_command(&sense_command);
300  shell_register_command(&senseconv_command);
301  shell_register_command(&nodeid_command);
302 
303 }
304 /*---------------------------------------------------------------------------*/
rtimer_clock_t timesynch_time(void)
Get the current time-synchronized time.
CCIF clock_time_t clock_time(void)
Get the current clock time.
Definition: clock.c:41
Default definitions of C compiler quirk work-arounds.
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_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
static void input(void)
Process a received 6lowpan packet.
Definition: sicslowpan.c:1503
unsigned long shell_strtolong(const char *str, const char **retstr)
Convert a string to a number.
Definition: shell.c:521
Header file for Tmote Sky-specific Contiki shell commands
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
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
void watchdog_stop(void)
Stops the WDT such that it won't timeout and cause MCU reset.
Definition: watchdog.c:58
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219
int shell_event_input
The event number for shell input data.
Definition: shell.c:70
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
Header file for the Rime stack
#define NULL
The null pointer.
Structure for shell input data.
Definition: shell.h:365
void watchdog_start(void)
Starts the WDT in watchdog mode if enabled by user configuration, maximum interval.
Definition: watchdog.c:49
Header file for a simple time synchronization mechanism
Include file for the Contiki low-layer network stack (NETSTACK)
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120