1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00

shell: move ccnl commands from example to shell

This commit is contained in:
Oleg Hahm 2015-12-06 17:12:40 +01:00
parent c96679f3d1
commit 13958178f5
6 changed files with 195 additions and 232 deletions

View File

@ -1,81 +0,0 @@
/*
* Copyright (C) 2015 Inria
*
* 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.
*/
#include <stdio.h>
#include <string.h>
#include "ccn-lite-riot.h"
#include "ccnl-pkt-ndntlv.h"
#include "ccnl-defs.h"
#include "ccnl-ext.h"
#include "ccnl-pkt-ndntlv.h"
#define MAX_CONTENT (64)
static char *_default_content = "Start the RIOT!";
static unsigned char _out[CCNL_MAX_PACKET_SIZE];
static void _usage(char *argv)
{
printf("usage: %s <URI> [content]\n"
"%% %s /riot/peter/schmerzl (default content)\n"
"%% %s /riot/peter/schmerzl RIOT\n",
argv, argv, argv);
}
int _ccnl_content(int argc, char **argv)
{
char *body = _default_content;
int arg_len = strlen(_default_content) + 1;
int offs = CCNL_MAX_PACKET_SIZE;
if (argc < 2) {
_usage(argv[0]);
return -1;
}
if (argc > 2) {
char buf[MAX_CONTENT];
memset(buf, ' ', MAX_CONTENT);
char *buf_ptr = buf;
for (int i = 2; (i < argc) && (buf_ptr < (buf + MAX_CONTENT)); i++) {
arg_len = strlen(argv[i]);
if ((buf_ptr + arg_len) > (buf + MAX_CONTENT)) {
arg_len = (buf + MAX_CONTENT) - buf_ptr;
}
strncpy(buf_ptr, argv[i], arg_len);
buf_ptr += arg_len + 1;
}
*buf_ptr = '\0';
body = buf;
arg_len = strlen(body);
}
int suite = CCNL_SUITE_NDNTLV;
struct ccnl_prefix_s *prefix = ccnl_URItoPrefix(argv[1], suite, NULL, NULL);
arg_len = ccnl_ndntlv_prependContent(prefix, (unsigned char*) body, arg_len, NULL, NULL, &offs, _out);
unsigned char *olddata;
unsigned char *data = olddata = _out + offs;
int len;
unsigned typ;
if (ccnl_ndntlv_dehead(&data, &arg_len, (int*) &typ, &len) ||
typ != NDN_TLV_Data) {
return -1;
}
struct ccnl_content_s *c = 0;
struct ccnl_pkt_s *pk = ccnl_ndntlv_bytes2pkt(typ, olddata, &data, &arg_len);
c = ccnl_content_new(&theRelay, &pk);
ccnl_content_add2cache(&theRelay, c);
c->flags |= CCNL_CONTENT_FLAGS_STATIC;
return 0;
}

View File

@ -1,82 +0,0 @@
/*
* Copyright (C) 2013-15, Christian Tschudin, University of Basel
* Copyright (C) 2015, Oliver Hahm, Inria
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* File history:
* 2015-11-09 created (based on ccn-lite-peek.c)
*/
#include <unistd.h>
#include "random.h"
#include "xtimer.h"
#include "timex.h"
#include "arpa/inet.h"
#include "net/packet.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/netif/hdr.h"
#include "net/gnrc/netapi.h"
#include "ccn-lite-riot.h"
#include "ccnl-core.h"
#include "ccnl-headers.h"
#include "ccnl-pkt-ndntlv.h"
#include "ccnl-defs.h"
#include "ccnl-ext.h"
#include "ccnl-pkt-ccntlv.h"
/**
* Maximum number of Interest retransmissions
*/
#define CCNL_INTEREST_RETRIES (3)
#define MAX_ADDR_LEN (8U)
#define BUF_SIZE (128)
static unsigned char _int_buf[BUF_SIZE];
static unsigned char _cont_buf[BUF_SIZE];
static void _usage(char *arg)
{
printf("usage: %s <URI> [relay]\n"
"%% %s /riot/peter/schmerzl (classic lookup)\n",
arg, arg);
}
int _ccnl_interest(int argc, char **argv)
{
if (argc < 2) {
_usage(argv[0]);
return -1;
}
/* XXX: https://xkcd.com/221/ */
genrand_init(0x4);
/* initialize address with 0xFF for broadcast */
size_t addr_len = MAX_ADDR_LEN;
uint8_t relay_addr[MAX_ADDR_LEN];
memset(relay_addr, UINT8_MAX, MAX_ADDR_LEN);
if (argc > 2) {
addr_len = gnrc_netif_addr_from_str(relay_addr, sizeof(relay_addr), argv[2]);
}
for (int cnt = 0; cnt < CCNL_INTEREST_RETRIES; cnt++) {
ccnl_send_interest(CCNL_SUITE_NDNTLV, argv[1], relay_addr, addr_len, NULL, _int_buf, BUF_SIZE);
ccnl_wait_for_chunk(_cont_buf, BUF_SIZE);
}
return 0;
}

View File

@ -20,81 +20,14 @@
#include <stdio.h>
#include "thread.h"
#include "msg.h"
#include "timex.h"
#include "shell_commands.h"
#include "net/packet.h"
#include "net/netopt.h"
#include "net/gnrc/netif.h"
#include "net/gnrc/netapi.h"
#include "shell.h"
#include "ccn-lite-riot.h"
#include "ccnl-core.h"
#include "ccnl-headers.h"
#include "ccnl-pkt-ndntlv.h"
#include "ccnl-defs.h"
#include "net/gnrc/nettype.h"
/* main thread's message queue */
#define MAIN_QUEUE_SIZE (8)
static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
/* check for one-time initialization */
static bool started = false;
/* shell command functions */
static int _ccnl_open(int argc, char **argv);
extern int _ccnl_content(int argc, char **argv);
extern int _ccnl_interest(int argc, char **argv);
static const shell_command_t shell_commands[] = {
{ "open", "opens an interface or socket", _ccnl_open},
{ "interest", "sends an interest", _ccnl_interest},
{ "content", "create content and populated it", _ccnl_content},
{ NULL, NULL, NULL }
};
/* usage for open command */
static void _usage(void)
{
puts("ccnl <interface>");
}
static int _ccnl_open(int argc, char **argv)
{
/* check if already running */
if (started) {
puts("Already opened an interface for CCN!");
return -1;
}
/* check if parameter is given */
if (argc != 2) {
_usage();
return -1;
}
/* check if given number is a valid netif PID */
int pid = atoi(argv[1]);
if (!gnrc_netif_exist(pid)) {
printf("%i is not a valid interface!\n", pid);
return -1;
}
ccnl_start();
/* set the relay's PID, configure the interface to interface to use CCN
* nettype */
if (ccnl_open_netif(pid, GNRC_NETTYPE_CCN) < 0) {
puts("Error registering at network interface!");
return -1;
}
started = true;
return 0;
}
int main(void)
{
msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
@ -104,6 +37,6 @@ int main(void)
ccnl_core_init();
char line_buf[SHELL_DEFAULT_BUFSIZE];
shell_run(shell_commands, line_buf, SHELL_DEFAULT_BUFSIZE);
shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}

View File

@ -60,6 +60,9 @@ endif
ifneq (,$(filter saul_reg,$(USEMODULE)))
SRC += sc_saul_reg.c
endif
ifneq (,$(filter ccn-lite-utils,$(USEMODULE)))
SRC += sc_ccnl.c
endif
# TODO
# Conditional building not possible at the moment due to

View File

@ -0,0 +1,179 @@
/*
* Copyright (C) 2015 INRIA.
*
* 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 sys_shell_commands
* @{
*
* @file
* @brief Shell commands to interact with the CCN-Lite stack
*
* @author Oliver Hahm <oliver.hahm@inria.fr>
*
* @}
*/
#include "random.h"
#include "net/gnrc/netif.h"
#include "ccn-lite-riot.h"
#include "ccnl-pkt-ndntlv.h"
#define MAX_CONTENT (64)
/**
* Maximum number of Interest retransmissions
*/
#define CCNL_INTEREST_RETRIES (3)
#define MAX_ADDR_LEN (8U)
#define BUF_SIZE (128)
static unsigned char _int_buf[BUF_SIZE];
static unsigned char _cont_buf[BUF_SIZE];
static char *_default_content = "Start the RIOT!";
static unsigned char _out[CCNL_MAX_PACKET_SIZE];
/* check for one-time initialization */
static bool started = false;
/* usage for open command */
static void _open_usage(void)
{
puts("ccnl <interface>");
}
int _ccnl_open(int argc, char **argv)
{
/* check if already running */
if (started) {
puts("Already opened an interface for CCN!");
return -1;
}
/* check if parameter is given */
if (argc != 2) {
_open_usage();
return -1;
}
/* check if given number is a valid netif PID */
int pid = atoi(argv[1]);
if (!gnrc_netif_exist(pid)) {
printf("%i is not a valid interface!\n", pid);
return -1;
}
ccnl_start();
/* set the relay's PID, configure the interface to interface to use CCN
* nettype */
if (ccnl_open_netif(pid, GNRC_NETTYPE_CCN) < 0) {
puts("Error registering at network interface!");
return -1;
}
started = true;
return 0;
}
static void _content_usage(char *argv)
{
printf("usage: %s <URI> [content]\n"
"%% %s /riot/peter/schmerzl (default content)\n"
"%% %s /riot/peter/schmerzl RIOT\n",
argv, argv, argv);
}
int _ccnl_content(int argc, char **argv)
{
char *body = _default_content;
int arg_len = strlen(_default_content) + 1;
int offs = CCNL_MAX_PACKET_SIZE;
if (argc < 2) {
_content_usage(argv[0]);
return -1;
}
if (argc > 2) {
char buf[MAX_CONTENT];
memset(buf, ' ', MAX_CONTENT);
char *buf_ptr = buf;
for (int i = 2; (i < argc) && (buf_ptr < (buf + MAX_CONTENT)); i++) {
arg_len = strlen(argv[i]);
if ((buf_ptr + arg_len) > (buf + MAX_CONTENT)) {
arg_len = (buf + MAX_CONTENT) - buf_ptr;
}
strncpy(buf_ptr, argv[i], arg_len);
buf_ptr += arg_len + 1;
}
*buf_ptr = '\0';
body = buf;
arg_len = strlen(body);
}
int suite = CCNL_SUITE_NDNTLV;
struct ccnl_prefix_s *prefix = ccnl_URItoPrefix(argv[1], suite, NULL, NULL);
arg_len = ccnl_ndntlv_prependContent(prefix, (unsigned char*) body, arg_len, NULL, NULL, &offs, _out);
unsigned char *olddata;
unsigned char *data = olddata = _out + offs;
int len;
unsigned typ;
if (ccnl_ndntlv_dehead(&data, &arg_len, (int*) &typ, &len) ||
typ != NDN_TLV_Data) {
return -1;
}
struct ccnl_content_s *c = 0;
struct ccnl_pkt_s *pk = ccnl_ndntlv_bytes2pkt(typ, olddata, &data, &arg_len);
c = ccnl_content_new(&theRelay, &pk);
ccnl_content_add2cache(&theRelay, c);
c->flags |= CCNL_CONTENT_FLAGS_STATIC;
return 0;
}
static void _interest_usage(char *arg)
{
printf("usage: %s <URI> [relay]\n"
"%% %s /riot/peter/schmerzl (classic lookup)\n",
arg, arg);
}
int _ccnl_interest(int argc, char **argv)
{
if (argc < 2) {
_interest_usage(argv[0]);
return -1;
}
/* XXX: https://xkcd.com/221/ */
genrand_init(0x4);
/* initialize address with 0xFF for broadcast */
size_t addr_len = MAX_ADDR_LEN;
uint8_t relay_addr[MAX_ADDR_LEN];
memset(relay_addr, UINT8_MAX, MAX_ADDR_LEN);
if (argc > 2) {
addr_len = gnrc_netif_addr_from_str(relay_addr, sizeof(relay_addr), argv[2]);
}
for (int cnt = 0; cnt < CCNL_INTEREST_RETRIES; cnt++) {
ccnl_send_interest(CCNL_SUITE_NDNTLV, argv[1], relay_addr, addr_len, NULL, _int_buf, BUF_SIZE);
ccnl_wait_for_chunk(_cont_buf, BUF_SIZE);
}
return 0;
}

View File

@ -118,6 +118,12 @@ extern int _gnrc_6ctx(int argc, char **argv);
#endif
#endif
#ifdef MODULE_CCN_LITE_UTILS
extern int _ccnl_open(int argc, char **argv);
extern int _ccnl_content(int argc, char **argv);
extern int _ccnl_interest(int argc, char **argv);
#endif
const shell_command_t _shell_command_list[] = {
{"reboot", "Reboot the node", _reboot_handler},
#ifdef MODULE_CONFIG
@ -193,6 +199,11 @@ const shell_command_t _shell_command_list[] = {
#endif
#ifdef MODULE_SAUL_REG
{"saul", "interact with sensors and actuators using SAUL", _saul },
#endif
#ifdef MODULE_CCN_LITE_UTILS
{ "ccnl_open", "opens an interface or socket", _ccnl_open},
{ "ccnl_int", "sends an interest", _ccnl_interest},
{ "ccnl_cont", "create content and populated it", _ccnl_content},
#endif
{NULL, NULL, NULL}
};