Contiki 3.x
shell-rime-sniff.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  * Contiki shell Rime sniffer application
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "shell.h"
41 #include "net/rime/rime.h"
42 
43 static uint8_t sniff_for_attributes;
44 
45 /*---------------------------------------------------------------------------*/
46 PROCESS(shell_sniff_process, "sniff");
47 SHELL_COMMAND(sniff_command,
48  "sniff",
49  "sniff: dump incoming packets",
50  &shell_sniff_process);
51 /*---------------------------------------------------------------------------*/
52 enum {
53  SNIFFER_PACKET_INPUT,
54  SNIFFER_PACKET_OUTPUT,
55  SNIFFER_ATTRIBUTES_INPUT,
56  SNIFFER_ATTRIBUTES_OUTPUT,
57 };
58 /*---------------------------------------------------------------------------*/
59 struct sniff_attributes_blob {
60  uint16_t len;
61  uint16_t type;
62  uint16_t rssi;
63  uint16_t lqi;
64  uint16_t timestamp;
65  uint16_t listen_time;
66  uint16_t transmit_time;
67  uint16_t channel;
68  linkaddr_t src, dest;
69 };
70 struct sniff_packet_blob {
71  uint16_t len;
72  uint16_t type;
73 };
74 /*---------------------------------------------------------------------------*/
75 static void
76 sniff_attributes_output(int type)
77 {
78  struct sniff_attributes_blob msg;
79  msg.len = 10;
80  msg.type = type;
81  msg.rssi = packetbuf_attr(PACKETBUF_ATTR_RSSI);
82  msg.lqi = packetbuf_attr(PACKETBUF_ATTR_LINK_QUALITY);
83  msg.timestamp = packetbuf_attr(PACKETBUF_ATTR_TIMESTAMP);
84  msg.listen_time = packetbuf_attr(PACKETBUF_ATTR_LISTEN_TIME);
85  msg.transmit_time = packetbuf_attr(PACKETBUF_ATTR_TRANSMIT_TIME);
86  msg.channel = packetbuf_attr(PACKETBUF_ATTR_CHANNEL);
87  linkaddr_copy(&msg.src, packetbuf_addr(PACKETBUF_ADDR_SENDER));
88  linkaddr_copy(&msg.dest, packetbuf_addr(PACKETBUF_ADDR_RECEIVER));
89 
90  shell_output(&sniff_command, &msg, sizeof(msg), NULL, 0);
91 }
92 /*---------------------------------------------------------------------------*/
93 static void
94 sniff_packet_output(int type)
95 {
96  struct sniff_packet_blob msg;
97 
98  msg.len = packetbuf_totlen() / 2 + 1;
99  msg.type = type;
100  shell_output(&sniff_command, &msg, sizeof(msg),
101  packetbuf_dataptr(), (packetbuf_datalen() & 0xfffe) +
102  2 * (packetbuf_totlen() & 1));
103 }
104 /*---------------------------------------------------------------------------*/
105 static void
106 input_sniffer(void)
107 {
108  if(sniff_for_attributes) {
109  sniff_attributes_output(SNIFFER_ATTRIBUTES_INPUT);
110  } else {
111  sniff_packet_output(SNIFFER_PACKET_INPUT);
112  }
113 }
114 /*---------------------------------------------------------------------------*/
115 static void
116 output_sniffer(int mac_status)
117 {
118  if(sniff_for_attributes) {
119  sniff_attributes_output(SNIFFER_ATTRIBUTES_OUTPUT);
120  } else {
121  sniff_packet_output(SNIFFER_PACKET_OUTPUT);
122  }
123 }
124 /*---------------------------------------------------------------------------*/
125 PROCESS_THREAD(shell_sniff_process, ev, data)
126 {
127  RIME_SNIFFER(s, input_sniffer, output_sniffer);
128  PROCESS_EXITHANDLER(goto exit;);
129  PROCESS_BEGIN();
130 
131  sniff_for_attributes = 0;
132 
133  if(data != NULL) {
134  char *arg = data;
135  if(arg[0] == '-' &&
136  arg[1] == 'a') {
137  sniff_for_attributes = 1;
138  }
139  }
140 
141  rime_sniffer_add(&s);
142 
144 
145  exit:
146  rime_sniffer_remove(&s);
147 
148  PROCESS_END();
149 }
150 /*---------------------------------------------------------------------------*/
151 void
152 shell_rime_sniff_init(void)
153 {
154  shell_register_command(&sniff_command);
155 }
156 /*---------------------------------------------------------------------------*/
#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.
uint16_t packetbuf_totlen(void)
Get the total length of the header and data in the packetbuf.
Definition: packetbuf.c:260
Header file for the Rime stack
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
#define PROCESS_EXITHANDLER(handler)
Specify an action when a process exits.
Definition: process.h:254
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_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