-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrplinfo.c
212 lines (173 loc) · 4.96 KB
/
rplinfo.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "contiki.h"
#include "contiki-net.h"
#include "net/uip.h"
#include "net/uip-ds6.h"
#include "net/rpl/rpl.h"
#include "erbium.h"
#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"
#elif WITH_COAP == 12
#include "er-coap-12-engine.h"
#elif WITH_COAP == 13
#include "er-coap-13-engine.h"
#else
#error "CoAP version defined by WITH_COAP not implemented"
#endif
#include "rplinfo.h"
/* debug */
#define DEBUG DEBUG_FULL
#include "net/uip-debug.h"
uint16_t
ipaddr_add(const uip_ipaddr_t *addr, char *buf)
{
uint16_t a, n;
int i, f;
n = 0;
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (addr->u8[i] << 8) + addr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
n+= sprintf(&buf[n], "::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
n+= sprintf(&buf[n], ":");
}
n+= sprintf(&buf[n], "%x", a);
}
}
return n;
}
uint16_t create_route_msg(char *buf, uip_ds6_route_t *r)
{
uint8_t n = 0;
n += sprintf(&(buf[n]), "{\"dest\":\"");
n += ipaddr_add(&r->ipaddr, &(buf[n]));
n += sprintf(&(buf[n]), "\",\"next\":\"");
n += ipaddr_add(uip_ds6_route_nexthop(r), &(buf[n]));
n += sprintf(&(buf[n]), "\"}");
buf[n] = 0;
PRINTF("buf: %s\n", buf);
return n;
}
RESOURCE(routes, METHOD_GET, "rplinfo/routes", "title=\"RPL route info\";rt=\"Data\"");
void
routes_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int32_t strpos = 0;
uip_ds6_route_t *r;
volatile uint8_t i;
size_t len = 0;
uint8_t index;
const char *pstr;
uint8_t count;
/* count the number of routes and return the total */
count = uip_ds6_route_num_routes();
if ((len = REST.get_query_variable(request, "index", &pstr))) {
index = (uint8_t)atoi(pstr);
if (index >= count ) {
strpos = snprintf(buffer, preferred_size, "{}");
} else {
/* seek to the route entry and return it */
i = 0;
for(r = uip_ds6_route_head(); r != NULL; r = uip_ds6_route_next(r), i++) {
if (i == index) {
break;
}
}
strpos = create_route_msg(buffer, r);
}
REST.set_header_content_type(response, APPLICATION_JSON);
} else { /* index not provided */
strpos += snprintf((char *)buffer, preferred_size, "%d", count);
}
*offset = -1;
REST.set_response_payload(response, buffer, strpos);
}
/* {"eui":"00050c2a8c9d4ea0","pref":"true","etx":124}*/
/* length of an neighbor entry, must be fixed width */
uint16_t create_parent_msg(char *buf, rpl_parent_t *parent, uint8_t preferred)
{
uint8_t n = 0;
uip_ipaddr_t * addr = rpl_get_parent_ipaddr(parent);
n += sprintf(&(buf[n]), "{\"eui\":\"%04x%04x%04x%04x\",",
UIP_HTONS(addr->u16[4]),
UIP_HTONS(addr->u16[5]),
UIP_HTONS(addr->u16[6]),
UIP_HTONS(addr->u16[7]));
n += sprintf(&(buf[n]), "\"pref\":");
if(preferred == 1) {
n += sprintf(&(buf[n]), "true,");
} else {
n += sprintf(&(buf[n]), "false,");
}
n += sprintf(&(buf[n]), "\"etx\":%d}", parent->link_metric);
buf[n] = 0;
PRINTF("buf: %s\n", buf);
return n;
}
RESOURCE(parents, METHOD_GET, "rplinfo/parents", "title=\"RPL parent info\";rt=\"Data\"");
static volatile uint8_t cur_neigh_entry;
static volatile uint8_t entry_char_skip;
void
parents_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
int32_t strpos = 0;
uip_ds6_route_t *r;
volatile uint8_t i;
rpl_dag_t *dag;
rpl_parent_t *parent;
size_t len = 0;
uint8_t index;
const char *pstr;
uint8_t count;
dag = rpl_get_any_dag();
if(dag != NULL) {
/* count the number of routes and return the total */
count = 0;
for (parent = dag->preferred_parent, i = 0; parent != NULL; parent = parent->next) {
count++;
}
if ((len = REST.get_query_variable(request, "index", &pstr))) {
index = (uint8_t)atoi(pstr);
if (index >= count) {
strpos = snprintf(buffer, preferred_size, "{}");
} else {
/* seek to the route entry and return it */
i = 0;
for (parent = dag->preferred_parent, i = 0; parent != NULL; parent = parent->next, i++) {
if (i == index) {
break;
}
}
if (parent == dag->preferred_parent) {
strpos = create_parent_msg(buffer, parent, 1);
} else {
strpos = create_parent_msg(buffer, parent, 0);
}
}
REST.set_header_content_type(response, APPLICATION_JSON);
} else { /* index not provided */
strpos += snprintf((char *)buffer, preferred_size, "%d", count);
}
} else { /* no DAG */
strpos += snprintf((char *)buffer, preferred_size, "{\"err\": \"no DAG\"}");
REST.set_header_content_type(response, APPLICATION_JSON);
}
*offset = -1;
REST.set_response_payload(response, buffer, strpos);
}
void
rplinfo_activate_resources(void) {
rest_activate_resource(&resource_parents);
rest_activate_resource(&resource_routes);
}