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 Shell
42  The shell enables to inspect and manage the network layer and provides
43  other system functionalities
44  * \ingroup lib
45  * @{
46  */
47 
48 #include "contiki.h"
49 #include "shell.h"
50 #include "shell-commands.h"
51 #include "net/ipv6/uip.h"
52 #include "net/ipv6/ip64-addr.h"
53 #include "net/ipv6/uiplib.h"
54 
55 /*---------------------------------------------------------------------------*/
56 void
57 shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr)
58 {
59  char buf[UIPLIB_IPV6_MAX_STR_LEN];
60  uiplib_ipaddr_snprint(buf, sizeof(buf), ipaddr);
61  SHELL_OUTPUT(output, "%s", buf);
62 }
63 /*---------------------------------------------------------------------------*/
64 void
65 shell_output_lladdr(shell_output_func output, const linkaddr_t *lladdr)
66 {
67  if(lladdr == NULL) {
68  SHELL_OUTPUT(output, "(NULL LL addr)");
69  return;
70  } else {
71  unsigned int i;
72  for(i = 0; i < LINKADDR_SIZE; i++) {
73  if(i > 0 && i % 2 == 0) {
74  SHELL_OUTPUT(output, ".");
75  }
76  SHELL_OUTPUT(output, "%02x", lladdr->u8[i]);
77  }
78  }
79 }
80 /*---------------------------------------------------------------------------*/
81 static void
82 output_prompt(shell_output_func output)
83 {
84  SHELL_OUTPUT(output, "#");
86  SHELL_OUTPUT(output, "> ");
87 }
88 /*---------------------------------------------------------------------------*/
89 PT_THREAD(shell_input(struct pt *pt, shell_output_func output, const char *cmd))
90 {
91  static char *args;
92  static const struct shell_command_t *cmd_descr = NULL;
93 
94  PT_BEGIN(pt);
95 
96  /* Shave off any leading spaces. */
97  while(*cmd == ' ') {
98  cmd++;
99  }
100 
101  /* Look for arguments */
102  args = strchr(cmd, ' ');
103  if(args != NULL) {
104  *args = '\0';
105  args++;
106  }
107 
108  cmd_descr = shell_command_lookup(cmd);
109  if(cmd_descr != NULL) {
110  static struct pt cmd_pt;
111  PT_SPAWN(pt, &cmd_pt, cmd_descr->func(&cmd_pt, output, args));
112  } else {
113  SHELL_OUTPUT(output, "Command not found. Type 'help' for a list of commands\n");
114  }
115 
116  output_prompt(output);
117  PT_END(pt);
118 }
119 /*---------------------------------------------------------------------------*/
120 void
122 {
124 }
125 /** @} */
static uip_ipaddr_t ipaddr
Pointer to prefix information option in uip_buf.
Definition: uip-nd6.c:125
Main header file for the Contiki shell
void shell_commands_init(void)
Initializes Shell-commands module.
void shell_output_lladdr(shell_output_func output, const linkaddr_t *lladdr)
Prints a link-layer address.
Definition: shell.c:65
Main header file for the Contiki shell
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
Definition: pt.h:114
#define PT_SPAWN(pt, child, thread)
Spawn a child protothread and wait until it exits.
Definition: pt.h:205
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:1549
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
Definition: linkaddr.c:48
Header file for the IP address manipulation library.
#define PT_END(pt)
Declare the end of a protothread.
Definition: pt.h:126
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
void shell_init(void)
Initializes Shell module.
Definition: shell.c:121
Header file for the uIP TCP/IP stack.
void shell_output_6addr(shell_output_func output, const uip_ipaddr_t *ipaddr)
Prints an IPv6 address.
Definition: shell.c:57
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:168