Contiki-NG
shell.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018, Inria.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 */
32
33/**
34 * \file
35 * The shell application
36 * \author
37 * Simon Duquennoy <simon.duquennoy@inria.fr>
38 */
39
40/**
41 * \addtogroup shell
42 * @{
43 */
44
45#include "contiki.h"
46#include "shell.h"
47#include "shell-commands.h"
48#include "net/ipv6/uip.h"
49#include "net/ipv6/ip64-addr.h"
50#include "net/ipv6/uiplib.h"
51
52#if NETSTACK_CONF_WITH_IPV6
53/*---------------------------------------------------------------------------*/
54void
55shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr)
56{
57 char buf[UIPLIB_IPV6_MAX_STR_LEN];
58 uiplib_ipaddr_snprint(buf, sizeof(buf), ipaddr);
59 SHELL_OUTPUT(output, "%s", buf);
60}
61#endif /* NETSTACK_CONF_WITH_IPV6 */
62/*---------------------------------------------------------------------------*/
63void
64shell_output_lladdr(shell_output_func output, const linkaddr_t *lladdr)
65{
66 if(lladdr == NULL) {
67 SHELL_OUTPUT(output, "(NULL LL addr)");
68 return;
69 } else {
70 unsigned int i;
71 for(i = 0; i < LINKADDR_SIZE; i++) {
72 if(i > 0 && i % 2 == 0) {
73 SHELL_OUTPUT(output, ".");
74 }
75 SHELL_OUTPUT(output, "%02x", lladdr->u8[i]);
76 }
77 }
78}
79/*---------------------------------------------------------------------------*/
80static void
81output_prompt(shell_output_func output)
82{
83 SHELL_OUTPUT(output, "#");
85 SHELL_OUTPUT(output, "> ");
86}
87/*---------------------------------------------------------------------------*/
88PT_THREAD(shell_input(struct pt *pt, shell_output_func output, const char *cmd))
89{
90 static char *args;
91 static const struct shell_command_t *cmd_descr = NULL;
92
93 PT_BEGIN(pt);
94
95 /* Shave off any leading spaces. */
96 while(*cmd == ' ') {
97 cmd++;
98 }
99
100 /* Skip empty lines */
101 if(*cmd != '\0') {
102 /* Look for arguments */
103 args = strchr(cmd, ' ');
104 if(args != NULL) {
105 *args = '\0';
106 args++;
107 }
108
109 cmd_descr = shell_command_lookup(cmd);
110 if(cmd_descr != NULL) {
111 static struct pt cmd_pt;
112 PT_SPAWN(pt, &cmd_pt, cmd_descr->func(&cmd_pt, output, args));
113 } else {
114 SHELL_OUTPUT(output, "Command not found. Type 'help' for a list of commands\n");
115 }
116 }
117
118 output_prompt(output);
119 PT_END(pt);
120}
121/*---------------------------------------------------------------------------*/
122void
124{
126}
127/** @} */
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:265
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
Definition: pt.h:280
#define PT_SPAWN(pt, child, thread)
Spawn a child protothread and wait until it exits.
Definition: pt.h:371
#define PT_END(pt)
Declare the end of a protothread.
Definition: pt.h:292
void shell_init(void)
Initializes Shell module.
Definition: shell.c:123
void shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr)
Prints an IPv6 address.
Definition: shell.c:55
void shell_output_lladdr(shell_output_func output, const linkaddr_t *lladdr)
Prints a link-layer address.
Definition: shell.c:64
void shell_commands_init(void)
Initializes Shell-commands module.
static uint8_t output(const linkaddr_t *localdest)
Take an IP packet and format it to be sent on an 802.15.4 network using 6lowpan.
Definition: sicslowpan.c:1606
int uiplib_ipaddr_snprint(char *buf, size_t size, const uip_ipaddr_t *addr)
Write at most size - 1 characters of the IP address to the output string.
Definition: uiplib.c:166
Main header file for the Contiki shell.
Main header file for the Contiki shell.
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:116
Header file for the uIP TCP/IP stack.
Header file for the IP address manipulation library.