-
Notifications
You must be signed in to change notification settings - Fork 1
/
erbr.c
271 lines (236 loc) · 7.66 KB
/
erbr.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
* 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.
*
* This file is part of the Contiki operating system.
*
*/
/**
* \file
* border-router
* \author
* Niclas Finne <nfi@sics.se>
* Joakim Eriksson <joakime@sics.se>
* Nicolas Tsiftes <nvt@sics.se>
*/
#include "contiki.h"
#include "contiki-lib.h"
#include "contiki-net.h"
#include "net/uip.h"
#include "net/uip-ds6.h"
#include "net/rpl/rpl.h"
#include "net/netstack.h"
#include "dev/button-sensor.h"
#include "dev/slip.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define DEBUG DEBUG_NONE
#include "net/uip-debug.h"
/* coap */
#if WITH_COAP == 3
#include "er-coap-03-engine.h"
#elif WITH_COAP == 6
#include "er-coap-06-engine.h"
#elif WITH_COAP == 7
#include "er-coap-07-engine.h"
#else
#error "CoAP version defined by WITH_COAP not implemented"
#endif
/* mc1322x */
#include "mc1322x.h"
#include "config.h"
RESOURCE(config, METHOD_GET | METHOD_POST , "config", "title=\"Config parameters\";rt=\"Data\"");
void
config_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
void *param;
uip_ipaddr_t *new_addr;
const char *pstr;
size_t len = 0;
if ((len = REST.get_query_variable(request, "param", &pstr))) {
if(strncmp(pstr, "channel", len) == 0) {
param = &mc1322x_config.channel;
} else if (strncmp(pstr, "serial", len) == 0) {
param = &mc1322x_config.eui;
} else if(strncmp(pstr, "eui", len) == 0) {
param = &mc1322x_config.eui;
} else {
goto bad;
}
} else {
goto bad;
}
if (REST.get_method_type(request) == METHOD_POST) {
const uint8_t *new;
uint32_t serial;
REST.get_request_payload(request, &new);
if(strncmp(pstr, "channel", len) == 0) {
*(uint8_t *)param = (uint8_t)atoi(new) - 11;
} else if(strncmp(pstr, "serial", len) == 0) {
serial = (uint32_t)atoi(new);
*(uint64_t *)param = (0xEC473C4D12ull << 24) | serial;
} else if(strncmp(pstr, "eui", len) == 0) {
*(uint64_t *)param = strtoull(new, NULL, 16);
} else {
goto bad;
}
/* do clean-up actions */
/* save the config and reset ourself */
mc1322x_config_save(&mc1322x_config);
} else { /* GET */
uint8_t n;
if (strncmp(pstr, "channel", len) == 0) {
n = sprintf(buffer, "%d", *(uint8_t *)param + 11);
} else if (strncmp(pstr, "eui", len) == 0) {
n = sprintf(buffer, "%08X%08X", (uint32_t)(*(uint64_t *)param >> 32), (uint32_t)(*(uint64_t *)param));
} else if (strncmp(pstr, "serial", len) == 0) {
uint32_t serial;
serial = (uint32_t)(*(uint64_t *)param) & 0xffffff;
n = sprintf(buffer, "%d", serial);
} else {
goto bad;
}
REST.set_response_payload(response, buffer, n);
}
return;
bad:
REST.set_response_status(response, REST.status.BAD_REQUEST);
}
extern uip_ds6_nbr_t uip_ds6_nbr_cache[];
static uip_ipaddr_t prefix;
static uint8_t prefix_set;
PROCESS(border_router_process, "Border router process");
AUTOSTART_PROCESSES(&border_router_process);
/*---------------------------------------------------------------------------*/
static void
print_local_addresses(void)
{
int i;
uint8_t state;
char addr[64];
PRINTA("{\"addrs\":[");
for(i = 0; i < UIP_DS6_ADDR_NB; i++) {
state = uip_ds6_if.addr_list[i].state;
if(uip_ds6_if.addr_list[i].isused &&
(state == ADDR_TENTATIVE || state == ADDR_PREFERRED)) {
PRINTA("\"");
uip_debug_ipaddr_print(&uip_ds6_if.addr_list[i].ipaddr);
PRINTA("\",");
}
}
PRINTA("]}\n\r");
}
/*---------------------------------------------------------------------------*/
void
request_prefix(void)
{
/* mess up uip_buf with a dirty request... */
uip_buf[0] = '?';
uip_buf[1] = 'P';
uip_len = 2;
slip_send();
uip_len = 0;
}
/*---------------------------------------------------------------------------*/
void
set_prefix_64(uip_ipaddr_t *prefix_64)
{
uip_ipaddr_t ipaddr;
memcpy(&prefix, prefix_64, 16);
memcpy(&ipaddr, prefix_64, 16);
prefix_set = 1;
uip_ds6_set_addr_iid(&ipaddr, &uip_lladdr);
uip_ds6_addr_add(&ipaddr, 0, ADDR_AUTOCONF);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(border_router_process, ev, data)
{
static struct etimer et;
rpl_dag_t *dag;
uip_ds6_addr_t *addr;
static uint8_t led;
GPIO->FUNC_SEL.ADC0 = 3;
GPIO->PAD_DIR.ADC0 = 1;
gpio_reset(ADC0);
led = 1;
PROCESS_BEGIN();
/* While waiting for the prefix to be sent through the SLIP connection, the future
* border router can join an existing DAG as a parent or child, or acquire a default
* router that will later take precedence over the SLIP fallback interface.
* Prevent that by turning the radio off until we are initialized as a DAG root.
*/
prefix_set = 0;
NETSTACK_MAC.off(0);
PROCESS_PAUSE();
SENSORS_ACTIVATE(button_sensor);
PRINTF("RPL-Border router started\n");
#if 0
/* The border router runs with a 100% duty cycle in order to ensure high
packet reception rates.
Note if the MAC RDC is not turned off now, aggressive power management of the
cpu will interfere with establishing the SLIP connection */
NETSTACK_MAC.off(1);
#endif
/* Request prefix until it has been received */
while(!prefix_set) {
etimer_set(&et, 0.5 * CLOCK_SECOND);
request_prefix();
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
if(led) {
led = 0;
gpio_set(ADC0);
} else {
led = 1;
gpio_reset(ADC0);
}
}
addr = uip_ds6_get_global(-1);
dag = rpl_set_root(RPL_DEFAULT_INSTANCE, &(addr->ipaddr));
if(dag != NULL) {
rpl_set_prefix(dag, &prefix, 64);
PRINTF("created a new RPL dag\n");
}
gpio_reset(ADC0);
/* Now turn the radio on, but disable radio duty cycling.
* Since we are the DAG root, reception delays would constrain mesh throughbut.
*/
NETSTACK_MAC.off(1);
#if DEBUG || 1
print_local_addresses();
#endif
rest_init_engine();
rplinfo_activate_resources();
rest_activate_resource(&resource_config);
while(1) {
PROCESS_YIELD();
if (ev == sensors_event && data == &button_sensor) {
PRINTF("Initiating global repair\n");
rpl_repair_root(RPL_DEFAULT_INSTANCE);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/