Contiki 3.x
shell-vars.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008, Swedish Institute of Computer Science.
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  * A brief description of what this file is.
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  */
39 
40 #include "contiki.h"
41 #include "contiki-conf.h"
42 #include "shell-vars.h"
43 
44 #include "loader/symbols.h"
45 
46 #include <stdio.h>
47 #include <string.h>
48 
49 #ifdef SHELL_VARS_CONF_RAM_BEGIN
50 #define SHELL_VARS_RAM_BEGIN SHELL_VARS_CONF_RAM_BEGIN
51 #define SHELL_VARS_RAM_END SHELL_VARS_CONF_RAM_END
52 #else /* SHELL_VARS_CONF_RAM_BEGIN */
53 #define SHELL_VARS_RAM_BEGIN 0
54 #define SHELL_VARS_RAM_END (unsigned int)-1
55 #endif /* SHELL_VARS_CONF_RAM_BEGIN */
56 
57 /*---------------------------------------------------------------------------*/
58 PROCESS(shell_vars_process, "vars");
59 SHELL_COMMAND(vars_command,
60  "vars",
61  "vars: list all variables in RAM",
62  &shell_vars_process);
63 /*---------------------------------------------------------------------------*/
64 PROCESS(shell_var_process, "var");
65 SHELL_COMMAND(var_command,
66  "var",
67  "var <variable>: show content of a variable",
68  &shell_var_process);
69 /*---------------------------------------------------------------------------*/
70 PROCESS_THREAD(shell_vars_process, ev, data)
71 {
72  int i;
73 
74  PROCESS_BEGIN();
75 
76  shell_output_str(&vars_command, "Variables in RAM:", "");
77 
78  for(i = 0; i < symbols_nelts; ++i) {
79  if(symbols[i].name != NULL &&
80  (unsigned int)symbols[i].value >= SHELL_VARS_RAM_BEGIN &&
81  (unsigned int)symbols[i].value <= SHELL_VARS_RAM_END) {
82  shell_output_str(&vars_command, (char *)symbols[i].name, "");
83  }
84  }
85 
86  PROCESS_END();
87 }
88 /*---------------------------------------------------------------------------*/
89 PROCESS_THREAD(shell_var_process, ev, data)
90 {
91  int i;
92  int j;
93  char numbuf[32];
94 
95  PROCESS_BEGIN();
96 
97  if(data == NULL) {
98  shell_output_str(&var_command, "syntax: var <variable name>", "");
99  } else {
100  for(i = 0; i < symbols_nelts; ++i) {
101  if(symbols[i].name != NULL &&
102  strncmp(symbols[i].name, data, strlen(symbols[i].name)) == 0) {
103 
104  sprintf(numbuf, " %d", *((int *)symbols[i].value));
105  shell_output_str(&var_command, (char *)symbols[i].name, numbuf);
106 
107  for(j = 0; j < 8 * 8; j += 8) {
108  sprintf(numbuf, "0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x 0x%02x",
109  ((unsigned char *)symbols[i].value)[j],
110  ((unsigned char *)symbols[i].value)[j + 1],
111  ((unsigned char *)symbols[i].value)[j + 2],
112  ((unsigned char *)symbols[i].value)[j + 3],
113  ((unsigned char *)symbols[i].value)[j + 4],
114  ((unsigned char *)symbols[i].value)[j + 5],
115  ((unsigned char *)symbols[i].value)[j + 6],
116  ((unsigned char *)symbols[i].value)[j + 7]);
117  shell_output_str(&var_command, numbuf, "");
118  }
119  PROCESS_EXIT();
120  }
121  }
122  shell_output_str(&var_command, data, ": variable name not found");
123  }
124 
125  PROCESS_END();
126 }
127 /*---------------------------------------------------------------------------*/
128 void
129 shell_vars_init(void)
130 {
131  shell_register_command(&var_command);
132  shell_register_command(&vars_command);
133 }
134 /*---------------------------------------------------------------------------*/
void shell_output_str(struct shell_command *c, char *text1, const char *text2)
Output strings from a shell command.
Definition: shell.c:383
#define PROCESS_EXIT()
Exit the currently running process.
Definition: process.h:200
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define NULL
The null pointer.
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
void shell_register_command(struct shell_command *c)
Register a command with the shell.
Definition: shell.c:413
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
A brief description of what this file is.
#define SHELL_COMMAND(name, command, description, process)
Define a shell command.
Definition: shell.h:219