forked from contiki-os/contiki
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathslip-radio.c
239 lines (219 loc) · 6.96 KB
/
slip-radio.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* Copyright (c) 2011, Swedish Institute of Computer Science.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/**
* \file
* Slip-radio driver
* \author
* Niclas Finne <[email protected]>
* Joakim Eriksson <[email protected]>
*/
#include "contiki.h"
#include "net/uip.h"
#include "net/uip-ds6.h"
#include "dev/slip.h"
#include <string.h>
#include "net/netstack.h"
#include "net/packetbuf.h"
#define DEBUG DEBUG_NONE
#include "net/uip-debug.h"
#include "cmd.h"
#include "slip-radio.h"
#include "packetutils.h"
#ifdef SLIP_RADIO_CONF_SENSORS
extern const struct slip_radio_sensors SLIP_RADIO_CONF_SENSORS;
#endif
void slip_send_packet(const uint8_t *ptr, int len);
/* max 16 packets at the same time??? */
uint8_t packet_ids[16];
int packet_pos;
static int slip_radio_cmd_handler(const uint8_t *data, int len);
#if CONTIKI_TARGET_NOOLIBERRY
int cmd_handler_rf230(const uint8_t *data, int len);
#elif CONTIKI_TARGET_ECONOTAG
int cmd_handler_mc1322x(const uint8_t *data, int len);
#else /* Leave CC2420 as default */
int cmd_handler_cc2420(const uint8_t *data, int len);
#endif /* CONTIKI_TARGET */
/*---------------------------------------------------------------------------*/
#ifdef CMD_CONF_HANDLERS
CMD_HANDLERS(CMD_CONF_HANDLERS);
#else
CMD_HANDLERS(slip_radio_cmd_handler);
#endif
/*---------------------------------------------------------------------------*/
static void
packet_sent(void *ptr, int status, int transmissions)
{
uint8_t buf[20];
uint8_t sid;
int pos;
sid = *((uint8_t *)ptr);
PRINTF("Slip-radio: packet sent! sid: %d, status: %d, tx: %d\n",
sid, status, transmissions);
/* packet callback from lower layers */
/* neighbor_info_packet_sent(status, transmissions); */
pos = 0;
buf[pos++] = '!';
buf[pos++] = 'R';
buf[pos++] = sid;
buf[pos++] = status; /* one byte ? */
buf[pos++] = transmissions;
cmd_send(buf, pos);
}
/*---------------------------------------------------------------------------*/
static int
slip_radio_cmd_handler(const uint8_t *data, int len)
{
int i;
if(data[0] == '!') {
/* should send out stuff to the radio - ignore it as IP */
/* --- s e n d --- */
if(data[1] == 'S') {
int pos;
packet_ids[packet_pos] = data[2];
packetbuf_clear();
pos = packetutils_deserialize_atts(&data[3], len - 3);
if(pos < 0) {
PRINTF("slip-radio: illegal packet attributes\n");
return 1;
}
pos += 3;
len -= pos;
if(len > PACKETBUF_SIZE) {
len = PACKETBUF_SIZE;
}
memcpy(packetbuf_dataptr(), &data[pos], len);
packetbuf_set_datalen(len);
PRINTF("slip-radio: sending %u (%d bytes)\n",
data[2], packetbuf_datalen());
/* parse frame before sending to get addresses, etc. */
no_framer.parse();
NETSTACK_MAC.send(packet_sent, &packet_ids[packet_pos]);
packet_pos++;
if(packet_pos >= sizeof(packet_ids)) {
packet_pos = 0;
}
return 1;
}
} else if(uip_buf[0] == '?') {
PRINTF("Got request message of type %c\n", uip_buf[1]);
if(data[1] == 'M') {
/* this is just a test so far... just to see if it works */
uip_buf[0] = '!';
uip_buf[1] = 'M';
for(i = 0; i < 8; i++) {
uip_buf[2 + i] = uip_lladdr.addr[i];
}
uip_len = 10;
cmd_send(uip_buf, uip_len);
return 1;
}
}
return 0;
}
/*---------------------------------------------------------------------------*/
void
slip_radio_cmd_output(const uint8_t *data, int data_len)
{
slip_send_packet(data, data_len);
}
/*---------------------------------------------------------------------------*/
static void
slip_input_callback(void)
{
PRINTF("SR-SIN: %u '%c%c'\n", uip_len, uip_buf[0], uip_buf[1]);
cmd_input(uip_buf, uip_len);
uip_len = 0;
}
/*---------------------------------------------------------------------------*/
static void
init(void)
{
#ifndef BAUD2UBR
#define BAUD2UBR(baud) baud
#endif
slip_arch_init(BAUD2UBR(115200));
process_start(&slip_process, NULL);
slip_set_input_callback(slip_input_callback);
packet_pos = 0;
}
/*---------------------------------------------------------------------------*/
#if !SLIP_RADIO_CONF_NO_PUTCHAR
#undef putchar
int
putchar(int c)
{
#define SLIP_END 0300
static char debug_frame = 0;
if(!debug_frame) { /* Start of debug output */
slip_arch_writeb(SLIP_END);
slip_arch_writeb('\r'); /* Type debug line == '\r' */
debug_frame = 1;
}
/* Need to also print '\n' because for example COOJA will not show
any output before line end */
slip_arch_writeb((char)c);
/*
* Line buffered output, a newline marks the end of debug output and
* implicitly flushes debug output.
*/
if(c == '\n') {
slip_arch_writeb(SLIP_END);
debug_frame = 0;
}
return c;
}
#endif
/*---------------------------------------------------------------------------*/
PROCESS(slip_radio_process, "Slip radio process");
AUTOSTART_PROCESSES(&slip_radio_process);
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(slip_radio_process, ev, data)
{
static struct etimer et;
PROCESS_BEGIN();
init();
NETSTACK_RDC.off(1);
#ifdef SLIP_RADIO_CONF_SENSORS
SLIP_RADIO_CONF_SENSORS.init();
#endif
printf("Slip Radio started...\n");
etimer_set(&et, CLOCK_SECOND * 3);
while(1) {
PROCESS_YIELD();
if(etimer_expired(&et)) {
etimer_reset(&et);
#ifdef SLIP_RADIO_CONF_SENSORS
SLIP_RADIO_CONF_SENSORS.send();
#endif
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/