/
main.c
343 lines (277 loc) · 8.79 KB
/
main.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
* Copyright (C) 2013 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup examples
* @{
*
* @file
* @brief CCN Lite interactive shell example application
*
* @author Christian Mehlis <mehlis@inf.fu-berlin.de>
*
* @}
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <inttypes.h>
#include "msg.h"
#include "thread.h"
#include "posix_io.h"
#include "shell.h"
#include "board_uart0.h"
#include "transceiver.h"
#include "vtimer.h"
#include "ps.h"
#include "ltc4150.h"
#define ENABLE_DEBUG (1)
#include "debug.h"
#include "ccn_lite/ccnl-riot.h"
#include "ccn_lite/util/ccnl-riot-client.h"
#define RIOT_CCN_APPSERVER (1)
#define RIOT_CCN_TESTS (0)
static const char DEFAULT_INTEREST[] = "/ccnx/0.7.1/doc/technical/CanonicalOrder.txt";
char relay_stack[KERNEL_CONF_STACKSIZE_MAIN];
#if RIOT_CCN_APPSERVER
char appserver_stack[KERNEL_CONF_STACKSIZE_MAIN];
#endif
static volatile kernel_pid_t _relay_pid = KERNEL_PID_UNDEF, _appserver_pid = KERNEL_PID_UNDEF;
#define SHELL_MSG_BUFFER_SIZE (64)
msg_t msg_buffer_shell[SHELL_MSG_BUFFER_SIZE];
shell_t shell;
unsigned char big_buf[3 * 1024];
char small_buf[PAYLOAD_SIZE];
#if RIOT_CCN_APPSERVER
static void riot_ccn_appserver(int argc, char **argv)
{
(void) argc; /* the function takes no arguments */
(void) argv;
if (_appserver_pid != KERNEL_PID_UNDEF) {
/* already running */
return;
}
_appserver_pid = thread_create(
appserver_stack, sizeof(appserver_stack),
PRIORITY_MAIN - 1, CREATE_STACKTEST,
ccnl_riot_appserver_start, (void *) &_relay_pid, "appserver");
DEBUG("ccn-lite appserver on thread_id %" PRIkernel_pid "...\n", _appserver_pid);
}
#endif
static void riot_ccn_express_interest(int argc, char **argv)
{
if (argc < 2) {
strncpy(small_buf, DEFAULT_INTEREST, sizeof(small_buf));
}
else {
strncpy(small_buf, argv[1], sizeof(small_buf));
}
DEBUG("in='%s'\n", small_buf);
int content_len = ccnl_riot_client_get(_relay_pid, small_buf, (char *) big_buf); // small_buf=name to request
if (content_len == 0) {
puts("riot_get returned 0 bytes...aborting!");
return;
}
puts("####################################################");
big_buf[content_len] = '\0';
printf("data='%s'\n", big_buf);
puts("####################################################");
puts("done");
}
static void riot_ccn_register_prefix(int argc, char **argv)
{
if (argc < 4) {
puts("enter: prefix </path/to/abc> <type> <faceid>");
return;
}
strncpy(small_buf, argv[1], 100);
DEBUG("prefix='%s'\n", small_buf);
char *type = argv[2];
char *faceid = argv[3]; // 0=trans;1=msg
int content_len = ccnl_riot_client_publish(_relay_pid, small_buf, faceid, type, big_buf);
DEBUG("shell received: '%s'\n", big_buf);
DEBUG("received %d bytes.\n", content_len);
puts("done");
}
static void riot_ccn_relay_config(int argc, char **argv)
{
if (_relay_pid == KERNEL_PID_UNDEF) {
puts("ccnl stack not running");
return;
}
if (argc < 2) {
printf("%s: <max_cache_entries>\n", argv[0]);
return;
}
msg_t m;
m.content.value = atoi(argv[1]);
m.type = CCNL_RIOT_CONFIG_CACHE;
msg_send(&m, _relay_pid);
}
static void riot_ccn_transceiver_start(kernel_pid_t _relay_pid)
{
transceiver_init(TRANSCEIVER);
int transceiver_pid = transceiver_start();
DEBUG("transceiver on thread_id %d...\n", transceiver_pid);
/* register for transceiver events */
uint8_t reg = transceiver_register(TRANSCEIVER, _relay_pid);
if (reg != 1) {
DEBUG("transceiver register failed\n");
}
/* set channel to CCNL_CHAN */
msg_t mesg;
transceiver_command_t tcmd;
int32_t c = CCNL_DEFAULT_CHANNEL;
tcmd.transceivers = TRANSCEIVER;
tcmd.data = &c;
mesg.content.ptr = (char *) &tcmd;
mesg.type = SET_CHANNEL;
msg_send_receive(&mesg, &mesg, transceiver_pid);
if (c == -1) {
puts("[transceiver] Error setting/getting channel");
}
else {
printf("[transceiver] Got channel: %" PRIi32 "\n", c);
}
}
static void riot_ccn_relay_start(void)
{
if (_relay_pid != KERNEL_PID_UNDEF) {
DEBUG("ccn-lite relay on thread_id %d...please stop it first!\n", _relay_pid);
/* already running */
return;
}
_relay_pid = thread_create(
relay_stack, sizeof(relay_stack),
PRIORITY_MAIN - 2, CREATE_STACKTEST,
ccnl_riot_relay_start, NULL, "relay");
DEBUG("ccn-lite relay on thread_id %" PRIkernel_pid "...\n", _relay_pid);
riot_ccn_transceiver_start(_relay_pid);
}
static void riot_ccn_relay_stop(int argc, char **argv)
{
(void) argc; /* the function takes no arguments */
(void) argv;
msg_t m;
m.content.value = 0;
m.type = CCNL_RIOT_HALT;
msg_send(&m, _relay_pid);
/* mark relay as not running */
_relay_pid = 0;
}
#if RIOT_CCN_TESTS
static void riot_ccn_pit_test(int argc, char **argv)
{
(void) argc; /* the function takes no arguments */
(void) argv;
char name[] = "/riot/test";
char *prefix[CCNL_MAX_NAME_COMP];
char *cp = strtok(name, "/");
int i = 0;
while (i < (CCNL_MAX_NAME_COMP - 1) && cp) {
prefix[i++] = cp;
cp = strtok(NULL, "/");
}
//prefix[i] = 0; //segment to request
prefix[i + 1] = 0;
msg_t m;
riot_ccnl_msg_t rmsg;
char segment_string[16]; //max=999\0
timex_t now;
int segment;
for (segment = 0; segment < 200; segment++) {
memset(segment_string, 0, 16);
snprintf(segment_string, 16, "%d", segment);
prefix[i] = segment_string;
unsigned int interest_nonce = genrand_uint32();
int interest_len = mkInterest(prefix, &interest_nonce, (unsigned char *) small_buf);
rmsg.payload = &small_buf;
rmsg.size = interest_len;
m.content.ptr = (char *) &rmsg;
m.type = CCNL_RIOT_MSG;
msg_send(&m, _relay_pid);
if ((segment % 50) == 0) {
vtimer_now(&now);
printf("done: %d - %ld.%ld\n", segment, now.tv_sec, now.tv_usec);
}
}
printf("done: tried to send %d interests\n", segment);
}
static void riot_ccn_fib_test(int argc, char **argv)
{
(void) argc; /* the function takes no arguments */
(void) argv;
char type[] = "newTRANSface";
char faceid[] = "42";
riot_new_face(_relay_pid, type, faceid, big_buf);
timex_t now;
int i = -1;
do {
i++;
snprintf(small_buf, sizeof(small_buf), "/riot/test/fib/%d/", i);
riot_register_prefix(_relay_pid, small_buf, faceid, big_buf);
if (i % 50 == 0) {
vtimer_now(&now);
printf("done: %d - %ld.%ld\n", i, now.tv_sec, now.tv_usec);
}
}
while (0 == strcmp((const char *) big_buf, "prefixreg cmd worked"));
DEBUG("%d: '%s'\n", i, big_buf);
printf("done: %d\n", i - 1);
}
#endif
static void riot_ccn_populate(int argc, char **argv)
{
(void) argc; /* the function takes no arguments */
(void) argv;
msg_t m;
m.content.value = 0;
m.type = CCNL_RIOT_POPULATE;
msg_send(&m, _relay_pid);
}
static void riot_ccn_stat(int argc, char **argv)
{
(void) argc; /* the function takes no arguments */
(void) argv;
msg_t m;
m.content.value = 0;
m.type = CCNL_RIOT_PRINT_STAT;
msg_send(&m, _relay_pid);
}
static const shell_command_t sc[] = {
{ "haltccn", "stops ccn relay", riot_ccn_relay_stop },
{ "interest", "express an interest", riot_ccn_express_interest },
{ "populate", "populate the cache of the relay with data", riot_ccn_populate },
{ "prefix", "registers a prefix to a face", riot_ccn_register_prefix },
{ "stat", "prints out forwarding statistics", riot_ccn_stat },
{ "config", "changes the runtime config of the ccn lite relay", riot_ccn_relay_config },
#if RIOT_CCN_APPSERVER
{ "appserver", "starts an application server to reply to interests", riot_ccn_appserver },
#endif
#if RIOT_CCN_TESTS
{ "pittest", "starts a test for the size and speed of pit operations", riot_ccn_pit_test },
{ "fibtest", "starts a test for the size and speed of fib operations", riot_ccn_fib_test },
#endif
{ NULL, NULL, NULL }
};
int main(void)
{
puts("CCN!");
if (msg_init_queue(msg_buffer_shell, SHELL_MSG_BUFFER_SIZE) != 0) {
DEBUG("msg init queue failed...abording\n");
return -1;
}
riot_ccn_relay_start();
puts("starting shell...");
puts(" posix open");
posix_open(uart0_handler_pid, 0);
puts(" shell init");
shell_init(&shell, sc, UART0_BUFSIZE, uart0_readc, uart0_putc);
puts(" shell run");
shell_run(&shell);
return 0;
}