diff --git a/apps/shell/Makefile.shell b/apps/shell/Makefile.shell index 4642e3533..e315e65b2 100644 --- a/apps/shell/Makefile.shell +++ b/apps/shell/Makefile.shell @@ -1,2 +1,7 @@ -shell_src = shell-gui.c shell.c ctk-textentry-cmdline.c +shell_src = shell.c serial-shell.c shell-reboot.c \ + shell-vars.c shell-ps.c shell-rime.c shell-sendtest.c \ + shell-blink.c shell-text.c shell-time.c \ + shell-file.c shell-netfile.c shell-exec.c \ + shell-rime-ping.c shell-rime-sniff.c \ + shell-sky.c #shell-rsh.c shell_dsc = shell-dsc.c diff --git a/apps/shell/serial-shell.c b/apps/shell/serial-shell.c new file mode 100644 index 000000000..34f1f0f1d --- /dev/null +++ b/apps/shell/serial-shell.c @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: serial-shell.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A shell driver for the serial port + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell.h" + +#include "dev/serial.h" +#include "net/rime.h" + +#include +#include + + +/*---------------------------------------------------------------------------*/ +PROCESS(serial_shell_process, "Contiki serial shell"); +/*---------------------------------------------------------------------------*/ +void +shell_default_output(const char *text1, int len1, const char *text2, int len2) +{ + printf("%.*s%.*s\r\n", len1, text1, len2, text2); +} +/*---------------------------------------------------------------------------*/ +void +shell_prompt(char *str) +{ + printf("%d.%d: %s", rimeaddr_node_addr.u8[0], rimeaddr_node_addr.u8[1], + str); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(serial_shell_process, ev, data) +{ + PROCESS_BEGIN(); + + shell_init(); + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == serial_event_message && data != NULL); + /* printf("input: '%s'\n", (char *)data);*/ + shell_input(data, strlen(data)); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +serial_shell_init(void) +{ + process_start(&serial_shell_process, NULL); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/serial-shell.h b/apps/shell/serial-shell.h new file mode 100644 index 000000000..3538a7caf --- /dev/null +++ b/apps/shell/serial-shell.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: serial-shell.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SERIAL_SHELL_H__ +#define __SERIAL_SHELL_H__ + +#include "contiki.h" + +void serial_shell_init(void); + +#endif /* __SERIAL_SHELL_H__ */ diff --git a/apps/shell/shell-blink.c b/apps/shell/shell-blink.c new file mode 100644 index 000000000..f6adcd90e --- /dev/null +++ b/apps/shell/shell-blink.c @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-blink.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell.h" + +#include "dev/leds.h" + +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_blink_process, "blink"); +SHELL_COMMAND(blink_command, + "blink", + "blink [num]: blink LEDs ([num] times)", + &shell_blink_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_blink_process, ev, data) +{ + static struct etimer etimer; + static int i, num, on = 0; + const char *nextptr; + + PROCESS_EXITHANDLER(if(on) { leds_toggle(LEDS_ALL); } ); + + PROCESS_BEGIN(); + + if(data != NULL) { + num = shell_strtolong(data, &nextptr); + if(nextptr != data) { + etimer_set(&etimer, CLOCK_SECOND / 2); + for(i = 0; i < num; ++i) { + leds_toggle(LEDS_ALL); + on = 1; + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + etimer_reset(&etimer); + + leds_toggle(LEDS_ALL); + on = 0; + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + etimer_reset(&etimer); + } + PROCESS_EXIT(); + } + } + + while(1) { + struct shell_input *input; + PROCESS_WAIT_EVENT(); + if(ev == PROCESS_EVENT_TIMER && data == &etimer) { + if(on) { + leds_toggle(LEDS_ALL); + on = 0; + } + } else if(ev == shell_event_input) { + input = data; + if(input->len1 + input->len2 == 0) { + if(on) { + leds_toggle(LEDS_ALL); + on = 0; + } + PROCESS_EXIT(); + } + shell_output(&blink_command, + input->data1, input->len1, + input->data2, input->len2); + + if(on == 0) { + leds_toggle(LEDS_ALL); + on = 1; + } + etimer_set(&etimer, CLOCK_SECOND / 16); + } + } + + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_blink_init(void) +{ + shell_register_command(&blink_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-blink.h b/apps/shell/shell-blink.h new file mode 100644 index 000000000..195c8781e --- /dev/null +++ b/apps/shell/shell-blink.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-blink.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_BLINK_H__ +#define __SHELL_BLINK_H__ + +#include "shell.h" + +void shell_blink_init(void); + +#endif /* __SHELL_BLINK_H__ */ diff --git a/apps/shell/shell-exec.c b/apps/shell/shell-exec.c new file mode 100644 index 000000000..8f73c5e92 --- /dev/null +++ b/apps/shell/shell-exec.c @@ -0,0 +1,140 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-exec.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell-exec.h" +#include "loader/elfloader.h" + +#include +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_exec_process, "exec"); +SHELL_COMMAND(exec_command, + "exec", + "exec: load and execute an ELF file", + &shell_exec_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_exec_process, ev, data) +{ + char *name; + int fd; + + PROCESS_BEGIN(); + + name = data; + if(name == NULL || strlen(name) == 0) { + shell_output_str(&exec_command, + "exec : filename must be given", ""); + PROCESS_EXIT(); + } + + /* Kill any old processes. */ + if(elfloader_autostart_processes != NULL) { + autostart_exit(elfloader_autostart_processes); + } + + + fd = cfs_open(name, CFS_READ); + if(fd <= 0) { + shell_output_str(&exec_command, + "exec: could not open ", name); + } else { + int ret; + char *print, *symbol; + + ret = elfloader_load(fd); + cfs_close(fd); + symbol = ""; + + switch(ret) { + case ELFLOADER_OK: + print = "OK"; + break; + case ELFLOADER_BAD_ELF_HEADER: + print = "Bad ELF header"; + break; + case ELFLOADER_NO_SYMTAB: + print = "No symbol table"; + break; + case ELFLOADER_NO_STRTAB: + print = "No string table"; + break; + case ELFLOADER_NO_TEXT: + print = "No text segment"; + break; + case ELFLOADER_SYMBOL_NOT_FOUND: + print = "Symbol not found: "; + symbol = elfloader_unknown; + break; + case ELFLOADER_SEGMENT_NOT_FOUND: + print = "Segment not found: "; + symbol = elfloader_unknown; + break; + case ELFLOADER_NO_STARTPOINT: + print = "No starting point"; + break; + default: + print = "Unknown return code from the ELF loader (internal bug)"; + break; + } + shell_output_str(&exec_command, print, symbol); + + if(ret == ELFLOADER_OK) { + int i; + for(i = 0; elfloader_autostart_processes[i] != NULL; ++i) { + shell_output_str(&exec_command, "exec: starting process ", + elfloader_autostart_processes[i]->name); + } + autostart_start(elfloader_autostart_processes); + } + + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_exec_init(void) +{ + elfloader_init(); + shell_register_command(&exec_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-exec.h b/apps/shell/shell-exec.h new file mode 100644 index 000000000..77212e1b2 --- /dev/null +++ b/apps/shell/shell-exec.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-exec.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_EXEC_H__ +#define __SHELL_EXEC_H__ + +#include "shell.h" + +void shell_exec_init(void); + +#endif /* __SHELL_EXEC_H__ */ diff --git a/apps/shell/shell-file.c b/apps/shell/shell-file.c new file mode 100644 index 000000000..916ab0fd7 --- /dev/null +++ b/apps/shell/shell-file.c @@ -0,0 +1,218 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-file.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell-file.h" +#include "cfs/cfs.h" + +#include +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_ls_process, "ls"); +SHELL_COMMAND(ls_command, + "ls", + "ls: list files", + &shell_ls_process); +PROCESS(shell_append_process, "append"); +SHELL_COMMAND(append_command, + "append", + "append : append to file", + &shell_append_process); +PROCESS(shell_write_process, "write"); +SHELL_COMMAND(write_command, + "write", + "write : write to file", + &shell_write_process); +PROCESS(shell_read_process, "read"); +SHELL_COMMAND(read_command, + "read", + "read : read from file", + &shell_read_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_ls_process, ev, data) +{ + static struct cfs_dir dir; + static int totsize; + struct cfs_dirent dirent; + char buf[32]; + PROCESS_BEGIN(); + + if(cfs_opendir(&dir, ".") != 0) { + shell_output_str(&ls_command, "Cannot open directory", ""); + } else { + totsize = 0; + while(cfs_readdir(&dir, &dirent) == 0) { + totsize += dirent.size; + sprintf(buf, "%d ", dirent.size); + shell_output_str(&ls_command, buf, dirent.name); + } + cfs_closedir(&dir); + sprintf(buf, "%d", totsize); + shell_output_str(&ls_command, "Total size: ", buf); + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_append_process, ev, data) +{ + static int fd = 0; + + PROCESS_EXITHANDLER(cfs_close(fd)); + + PROCESS_BEGIN(); + + fd = cfs_open(data, CFS_WRITE | CFS_APPEND); + + if(fd <= 0) { + shell_output_str(&append_command, + "Could not open file for writing: ", data); + } + + while(1) { + struct shell_input *input; + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + /* printf("cat input %d %d\n", input->len1, input->len2);*/ + if(input->len1 + input->len2 == 0) { + cfs_close(fd); + PROCESS_EXIT(); + } + + cfs_write(fd, input->data1, input->len1); + cfs_write(fd, input->data2, input->len2); + + shell_output(&append_command, + input->data1, input->len1, + input->data2, input->len2); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_write_process, ev, data) +{ + static int fd = 0; + + PROCESS_EXITHANDLER(cfs_close(fd)); + + PROCESS_BEGIN(); + + fd = cfs_open(data, CFS_WRITE); + + if(fd <= 0) { + shell_output_str(&write_command, + "Could not open file for writing: ", data); + } + + while(1) { + struct shell_input *input; + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + /* printf("cat input %d %d\n", input->len1, input->len2);*/ + if(input->len1 + input->len2 == 0) { + cfs_close(fd); + PROCESS_EXIT(); + } + + cfs_write(fd, input->data1, input->len1); + cfs_write(fd, input->data2, input->len2); + + shell_output(&write_command, + input->data1, input->len1, + input->data2, input->len2); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_read_process, ev, data) +{ + static int fd = 0; + PROCESS_EXITHANDLER(cfs_close(fd)); + PROCESS_BEGIN(); + + fd = cfs_open(data, CFS_READ); + + if(fd <= 0) { + shell_output_str(&write_command, + "Could not open file for writing: ", data); + } + + + while(1) { + char buf[40]; + int len; + struct shell_input *input; + + len = cfs_read(fd, buf, sizeof(buf)); + if(len <= 0) { + cfs_close(fd); + PROCESS_EXIT(); + } + shell_output(&read_command, + buf, len, "", 0); + + process_post(&shell_read_process, PROCESS_EVENT_CONTINUE, NULL); + PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_CONTINUE || + ev == shell_event_input); + + if(ev == shell_event_input) { + input = data; + /* printf("cat input %d %d\n", input->len1, input->len2);*/ + if(input->len1 + input->len2 == 0) { + cfs_close(fd); + PROCESS_EXIT(); + } + } + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_file_init(void) +{ + shell_register_command(&ls_command); + shell_register_command(&write_command); + shell_register_command(&append_command); + shell_register_command(&read_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-file.h b/apps/shell/shell-file.h new file mode 100644 index 000000000..b5e8e54df --- /dev/null +++ b/apps/shell/shell-file.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-file.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_FILE_H__ +#define __SHELL_FILE_H__ + +#include "shell.h" + +void shell_file_init(void); + +#endif /* __SHELL_FILE_H__ */ diff --git a/apps/shell/shell-netfile.c b/apps/shell/shell-netfile.c new file mode 100644 index 000000000..fc2722046 --- /dev/null +++ b/apps/shell/shell-netfile.c @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-netfile.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell-netfile.h" +#include "net/rime.h" +#include "net/rime/rudolph0.h" +#include "dev/leds.h" + +#include "cfs/cfs.h" + +#include +#include + +#define FILENAME_LEN 20 + +static struct rudolph0_conn rudolph0_conn; +static char filename[FILENAME_LEN]; +static int receiving_file; +static struct pt recvnetfilept; +/*---------------------------------------------------------------------------*/ +PROCESS(shell_netfile_process, "netfile"); +SHELL_COMMAND(netfile_command, + "netfile", + "netfile: send file to entire network", + &shell_netfile_process); +PROCESS(shell_recvnetfile_process, "recvnetfile"); +SHELL_COMMAND(recvnetfile_command, + "recvnetfile", + "recvnetfile: receive file from network and print to output", + &shell_recvnetfile_process); +/*---------------------------------------------------------------------------*/ +static int +write_chunk(struct rudolph0_conn *c, int offset, int flag, + uint8_t *data, int datalen) +{ + PT_BEGIN(&recvnetfilept); + + PT_WAIT_UNTIL(&recvnetfilept, receiving_file); + leds_on(LEDS_YELLOW); + leds_on(LEDS_RED); + PT_WAIT_UNTIL(&recvnetfilept, flag == RUDOLPH0_FLAG_NEWFILE); + leds_off(LEDS_RED); + + do { + if(datalen > 0) { + shell_output(&recvnetfile_command, data, datalen, "", 0); + printf("write_chunk wrote %d bytes at %d\n", datalen, offset); + } + PT_YIELD(&recvnetfilept); + } while(flag != RUDOLPH0_FLAG_LASTCHUNK); + + shell_output(&recvnetfile_command, data, datalen, "", 0); + printf("write_chunk wrote %d bytes at %d\n", datalen, offset); + shell_output(&recvnetfile_command, "", 0, "", 0); + leds_off(LEDS_YELLOW); + receiving_file = 0; + process_post(&shell_recvnetfile_process, PROCESS_EVENT_CONTINUE, NULL); + + PT_END(&recvnetfilept); +} + +static int +read_chunk(struct rudolph0_conn *c, int offset, uint8_t *to, int maxsize) +{ + int ret; + int fd; + + fd = cfs_open(filename, CFS_READ); + + cfs_seek(fd, offset); + ret = cfs_read(fd, to, maxsize); + printf("read_chunk %d bytes at %d, %d\n", ret, offset, (unsigned char)to[0]); + cfs_close(fd); + return ret; +} +const static struct rudolph0_callbacks rudolph0_callbacks = + {(void (*)(struct rudolph0_conn *, int, int, uint8_t *, int))write_chunk, + read_chunk}; +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_netfile_process, ev, data) +{ + int fd; + + PROCESS_BEGIN(); + + rudolph0_send(&rudolph0_conn, CLOCK_SECOND); + if(data != NULL) { + strcpy(filename, data); + } + fd = cfs_open(filename, CFS_READ); + if(fd <= 0) { + shell_output_str(&netfile_command, "Could not open file ", filename); + } else { + cfs_close(fd); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_recvnetfile_process, ev, data) +{ + PROCESS_BEGIN(); + + PT_INIT(&recvnetfilept); + receiving_file = 1; + while(1) { + struct shell_input *input; + + PROCESS_WAIT_EVENT(); + + if(ev == shell_event_input) { + input = data; + if(input->len1 + input->len2 == 0) { + receiving_file = 0; + PROCESS_EXIT(); + } + } else if(receiving_file == 0) { + PROCESS_EXIT(); + } + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_netfile_init(void) +{ + receiving_file = 0; + shell_register_command(&netfile_command); + shell_register_command(&recvnetfile_command); + + rudolph0_open(&rudolph0_conn, 26, &rudolph0_callbacks); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-netfile.h b/apps/shell/shell-netfile.h new file mode 100644 index 000000000..6e7b39027 --- /dev/null +++ b/apps/shell/shell-netfile.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-netfile.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_NETFILE_H__ +#define __SHELL_NETFILE_H__ + +#include "shell.h" + +void shell_netfile_init(void); + +#endif /* __SHELL_NETFILE_H__ */ diff --git a/apps/shell/shell-profile.c b/apps/shell/shell-profile.c new file mode 100644 index 000000000..46a39b251 --- /dev/null +++ b/apps/shell/shell-profile.c @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-profile.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "contiki-conf.h" +#include "shell-profile.h" + +#include "loader/symbols.h" + +#include +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_profile_process, "Shell 'profile' command"); +SHELL_COMMAND(profile_command, + "profile", + "profile: show aggregate profiling information", + &shell_profile_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_profile_process, ev, data) +{ + int i; + char buf[100]; + PROCESS_BEGIN(); + +#if DETAILED_AGGREGATES + for(i = 0; i < aggregates_list_ptr; ++i) { + sprintf(buf, "-- %s: %lu / %u = %lu", aggregates[i].ptr, + aggregates[i].cycles, + aggregates[i].episodes, + aggregates[i].cycles / aggregates[i].episodes); + shell_output(buf, ""); + } +#else + for(i = 0; i < aggregates_list_ptr; ++i) { + sprintf(buf, "-- %c%c: %lu / %u = %lu", + aggregates[i].ptr[0], aggregates[i].ptr[1], + aggregates[i].cycles, + aggregates[i].episodes, + aggregates[i].cycles / aggregates[i].episodes); + shell_output(buf, ""); + } +#endif + + sprintf(buf, "Memory for aggregates: %d * %d = %d\n", + (int)sizeof(struct aggregate), aggregates_list_ptr, + (int)sizeof(struct aggregate) * aggregates_list_ptr); + shell_output(buf, ""); + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_profile_init(void) +{ + shell_register_command(&profile_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-profile.h b/apps/shell/shell-profile.h new file mode 100644 index 000000000..d0d268918 --- /dev/null +++ b/apps/shell/shell-profile.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-profile.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_PROFILE_H__ +#define __SHELL_PROFILE_H__ + +#include "shell.h" + +void shell_profile_init(void); + +#endif /* __SHELL_PROFILE_H__ */ diff --git a/apps/shell/shell-ps.c b/apps/shell/shell-ps.c new file mode 100644 index 000000000..052b82013 --- /dev/null +++ b/apps/shell/shell-ps.c @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-ps.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Process-related Contiki shell commands + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell-ps.h" + +#include +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_ps_process, "ps"); +SHELL_COMMAND(ps_command, + "ps", + "ps: list all running processes", + &shell_ps_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_ps_process, ev, data) +{ + struct process *p; + PROCESS_BEGIN(); + + shell_output_str(&ps_command, "Processes:", ""); + for(p = PROCESS_LIST(); p != NULL; p = p->next) { + shell_output_str(&ps_command, (char *)p->name, ""); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_ps_init(void) +{ + shell_register_command(&ps_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-ps.h b/apps/shell/shell-ps.h new file mode 100644 index 000000000..d9213e567 --- /dev/null +++ b/apps/shell/shell-ps.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-ps.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Header file for process-related Contiki shell commands + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_PS_H__ +#define __SHELL_PS_H__ + +#include "shell.h" + +void shell_ps_init(void); + +#endif /* __SHELL_PS_H__ */ diff --git a/apps/shell/shell-reboot.c b/apps/shell/shell-reboot.c new file mode 100644 index 000000000..6b8152bd2 --- /dev/null +++ b/apps/shell/shell-reboot.c @@ -0,0 +1,86 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-reboot.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Reboot Contiki shell command + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell.h" +#include "dev/leds.h" +#include "dev/watchdog.h" + +#include +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_reboot_process, "reboot"); +SHELL_COMMAND(reboot_command, + "reboot", + "reboot: reboot the system", + &shell_reboot_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_reboot_process, ev, data) +{ + static struct etimer etimer; + PROCESS_BEGIN(); + + shell_output_str(&reboot_command, + "Rebooting the node in four seconds...", ""); + + etimer_set(&etimer, CLOCK_SECOND); + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + leds_on(LEDS_RED); + etimer_reset(&etimer); + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + leds_on(LEDS_GREEN); + etimer_reset(&etimer); + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + leds_on(LEDS_BLUE); + etimer_reset(&etimer); + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + + watchdog_reboot(); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_reboot_init(void) +{ + shell_register_command(&reboot_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-reboot.h b/apps/shell/shell-reboot.h new file mode 100644 index 000000000..446e4a523 --- /dev/null +++ b/apps/shell/shell-reboot.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-reboot.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Header file for reboot Contik shell command + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_REBOOT_H__ +#define __SHELL_REBOOT_H__ + +#include "shell.h" + +void shell_reboot_init(void); + +#endif /* __SHELL_REBOOT_H__ */ diff --git a/apps/shell/shell-rime-ping.c b/apps/shell/shell-rime-ping.c new file mode 100644 index 000000000..9a8765dfa --- /dev/null +++ b/apps/shell/shell-rime-ping.c @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-rime-ping.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * The Contiki shell Rime ping application + * \author + * Adam Dunkels + */ + + +#include "shell.h" +#include "net/rime.h" + +#include +#ifndef HAVE_SNPRINTF +int snprintf(char *str, size_t size, const char *format, ...); +#endif /* HAVE_SNPRINTF */ + +struct ping_msg { + rtimer_clock_t pingtime; + rtimer_clock_t pongtime; +}; + +static struct mesh_conn mesh; +static int waiting_for_pong = 0; + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_ping_process, "ping"); +SHELL_COMMAND(ping_command, + "ping", + "ping : send a message to a specific node and get a reply", + &shell_ping_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_ping_process, ev, data) +{ + static int i; + static struct etimer timeout, periodic; + static rimeaddr_t receiver; + struct ping_msg *ping; + const char *nextptr; + char buf[32]; + + PROCESS_BEGIN(); + + receiver.u8[0] = shell_strtolong(data, &nextptr); + if(nextptr == data || *nextptr != '.') { + shell_output_str(&ping_command, + "ping : recevier must be specified", ""); + PROCESS_EXIT(); + } + ++nextptr; + receiver.u8[1] = shell_strtolong(nextptr, &nextptr); + + snprintf(buf, sizeof(buf), "%d.%d", receiver.u8[0], receiver.u8[1]); + shell_output_str(&ping_command, "Sending 4 pings to ", buf); + + for(i = 0; i < 4; ++i) { + rimebuf_clear(); + ping = rimebuf_dataptr(); + rimebuf_set_datalen(sizeof(struct ping_msg)); +#if TIMESYNCH_CONF_ENABLED + ping->pingtime = timesynch_time(); +#else + ping->pingtime = 0; +#endif + mesh_send(&mesh, &receiver); + + etimer_set(&timeout, CLOCK_SECOND * 8); + etimer_set(&periodic, CLOCK_SECOND * 1); + waiting_for_pong = 1; + PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timeout) || + waiting_for_pong == 0); + if(waiting_for_pong == 0) { + PROCESS_WAIT_UNTIL(etimer_expired(&periodic)); + } else { + shell_output_str(&ping_command, + "Timed out", ""); + } + waiting_for_pong = 0; + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +static void +timedout_mesh(struct mesh_conn *c) +{ + /* printf("packet timedout\n");*/ +} +static void +recv_mesh(struct mesh_conn *c, rimeaddr_t *from, u8_t hops) +{ + struct ping_msg *ping; + char buf[32]; + rtimer_clock_t pingrecvtime; + + ping = rimebuf_dataptr(); + + if(waiting_for_pong == 0) { +#if TIMESYNCH_CONF_ENABLED + ping->pongtime = timesynch_time(); +#else + ping->pongtime = 0; +#endif + mesh_send(&mesh, from); + } else { +#if TIMESYNCH_CONF_ENABLED + pingrecvtime = timesynch_time(); +#else + pingrecvtime = 0; +#endif + snprintf(buf, sizeof(buf), "%lu, %lu, %lu, %d.", + (1000L * (pingrecvtime - ping->pingtime)) / RTIMER_ARCH_SECOND, + (1000L * (ping->pongtime - ping->pingtime)) / RTIMER_ARCH_SECOND, + (1000L * (pingrecvtime - ping->pongtime)) / RTIMER_ARCH_SECOND, + hops); + + shell_output_str(&ping_command, + "Pong recived; rtt (ms), latency 1 (ms), latency 2 (ms), hops: ", buf); + waiting_for_pong = 0; + process_post(&shell_ping_process, PROCESS_EVENT_CONTINUE, NULL); + } +} +const static struct mesh_callbacks mesh_callbacks = { recv_mesh, + NULL, + timedout_mesh }; +/*---------------------------------------------------------------------------*/ +void +shell_rime_ping_init(void) +{ + mesh_open(&mesh, 19, &mesh_callbacks); + + shell_register_command(&ping_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-rime-ping.h b/apps/shell/shell-rime-ping.h new file mode 100644 index 000000000..913638f44 --- /dev/null +++ b/apps/shell/shell-rime-ping.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-rime-ping.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Header file for the Contiki shell Rime ping application + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_RIME_PING_H__ +#define __SHELL_RIME_PING_H__ + +void shell_rime_ping_init(void); + +#endif /* __SHELL-RIME_-PING_H__ */ diff --git a/apps/shell/shell-rime-sniff.c b/apps/shell/shell-rime-sniff.c new file mode 100644 index 000000000..a35d0b922 --- /dev/null +++ b/apps/shell/shell-rime-sniff.c @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-rime-sniff.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Contiki shell Rime sniffer application + * \author + * Adam Dunkels + */ + +#include "shell.h" +#include "net/rime.h" +/*---------------------------------------------------------------------------*/ +PROCESS(shell_sniff_process, "sniff"); +SHELL_COMMAND(sniff_command, + "sniff", + "sniff: dump incoming packets", + &shell_sniff_process); +/*---------------------------------------------------------------------------*/ +enum { + SNIFFER_INPUT, + SNIFFER_OUTPUT, +}; +/*---------------------------------------------------------------------------*/ +static void +input_sniffer(void) +{ + struct { + uint16_t len; + uint16_t flags; + } msg; + msg.len = rimebuf_totlen() / 2 + 1; + msg.flags = SNIFFER_INPUT; + shell_output(&sniff_command, &msg, sizeof(msg), + rimebuf_dataptr(), (rimebuf_datalen() & 0xfffe) + + 2 * (rimebuf_totlen() & 1)); +} +/*---------------------------------------------------------------------------*/ +static void +output_sniffer(void) +{ + struct { + uint16_t len; + uint16_t flags; + } msg; + msg.len = rimebuf_totlen() / 2 + 1; + msg.flags = SNIFFER_OUTPUT; + shell_output(&sniff_command, &msg, sizeof(msg), + rimebuf_hdrptr(), (rimebuf_totlen() & 0xfffe) + + 2 * (rimebuf_totlen() & 1)); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_sniff_process, ev, data) +{ + RIME_SNIFFER(s, input_sniffer, output_sniffer); + PROCESS_EXITHANDLER(goto exit;); + PROCESS_BEGIN(); + + rime_sniffer_add(&s); + + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + + exit: + rime_sniffer_remove(&s); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_rime_sniff_init(void) +{ + shell_register_command(&sniff_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-rime-sniff.h b/apps/shell/shell-rime-sniff.h new file mode 100644 index 000000000..cc95e656b --- /dev/null +++ b/apps/shell/shell-rime-sniff.h @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-rime-sniff.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Header file for the Contiki shell Rime sniffer application + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_RIME_SNIFF_H__ +#define __SHELL_RIME_SNIFF_H__ + +void shell_rime_sniff_init(void); + +#endif /* __SHELL_RIME_SNIFF_H__ */ diff --git a/apps/shell/shell-rime.c b/apps/shell/shell-rime.c new file mode 100644 index 000000000..19342f35f --- /dev/null +++ b/apps/shell/shell-rime.c @@ -0,0 +1,642 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-rime.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "contiki-conf.h" +#include "shell-rime.h" + +#include "dev/leds.h" + +#include "lib/random.h" + +#include "net/rime.h" +#include "net/rime/neighbor.h" +#include "net/rime/route.h" +#include "net/rime/trickle.h" + +#include "net/rime/timesynch.h" + +#define WITH_DEBUG_COMMANDS 1 + +#if NETSIM +#include "ether.h" +#endif /* NETSIM */ + +#include +#ifndef HAVE_SNPRINTF +int snprintf(char *str, size_t size, const char *format, ...); +#endif /* HAVE_SNPRINTF */ +#include + + +#define COLLECT_REXMITS 4 + +enum { + TRICKLE_TYPE_NODES, + TRICKLE_TYPE_NETCMD, +}; + +struct trickle_msg { + uint8_t type; + char netcmd[1]; +}; + +#define COLLECT_MSG_HDRSIZE 2 +struct collect_msg { + uint16_t timestamp; + uint8_t data[1]; +}; + +#if WITH_DEBUG_COMMANDS +static struct abc_conn abc; +static struct uc_conn uc; +#endif /* WITH_DEBUG_COMMANDS */ + +static struct collect_conn collect; +static struct trickle_conn trickle; +static struct ctimer ctimer; +static int waiting_for_nodes = 0; +static int waiting_for_collect = 0; +static int messages_received = 0; + + +static int is_sink = 0; + +/* XXX ideas not implemented yet: + + * download: download file from specific node. + + * traceroute + +*/ + + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_nodes_process, "nodes"); +SHELL_COMMAND(nodes_command, + "nodes", + "nodes: get a list of nodes in the network", + &shell_nodes_process); +PROCESS(shell_netcmd_process, "netcmd"); +PROCESS(shell_netcmd_server_process, "netcmd server"); +SHELL_COMMAND(netcmd_command, + "netcmd", + "netcmd : run a command on all nodes in the network", + &shell_netcmd_process); +PROCESS(shell_send_process, "send"); +SHELL_COMMAND(send_command, + "send", + "send: send data to the collector node", + &shell_send_process); +PROCESS(shell_collect_process, "collect"); +SHELL_COMMAND(collect_command, + "collect", + "collect: collect data from the network", + &shell_collect_process); +#if WITH_TREEDEPTH +PROCESS(shell_treedepth_process, "treedepth"); +SHELL_COMMAND(treedepth_command, + "treedepth", + "treedepth: print the collection tree depth", + &shell_treedepth_process); +#endif /* WITH_TREEDEPTH */ +PROCESS(shell_neighbors_process, "neighbors"); +SHELL_COMMAND(neighbors_command, + "neighbors", + "neighbors: dump neighbor list in binary format", + &shell_neighbors_process); +PROCESS(shell_routes_process, "routes"); +SHELL_COMMAND(routes_command, + "routes", + "routes: dump route list in binary format", + &shell_routes_process); +PROCESS(shell_packetize_process, "packetize"); +SHELL_COMMAND(packetize_command, + "packetize", + "packetize: put data into one packet", + &shell_packetize_process); +/*---------------------------------------------------------------------------*/ +#if WITH_DEBUG_COMMANDS +PROCESS(shell_broadcast_process, "broadcast"); +SHELL_COMMAND(broadcast_command, + "broadcast", + "broadcast: broadcast data to all neighbors", + &shell_broadcast_process); +PROCESS(shell_unicast_process, "unicast"); +SHELL_COMMAND(unicast_command, + "unicast", + "unicast : unicast data to specific neighbor", + &shell_unicast_process); +#endif /* WITH_DEBUG_COMMANDS */ +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_packetize_process, ev, data) +{ + static struct queuebuf *q = NULL; + static char *ptr; + static int size; + int len; + PROCESS_BEGIN(); + + while(1) { + struct shell_input *input; + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + + if(q == NULL) { + rimebuf_clear(); + q = queuebuf_new_from_rimebuf(); + if(q == NULL) { + shell_output_str(&packetize_command, "packetize: could not allocate packet buffer", ""); + PROCESS_EXIT(); + } + ptr = queuebuf_dataptr(q); + size = 0; + } + + input = data; + + len = input->len1 + input->len2; + + if(len + size >= RIMEBUF_SIZE || + len == 0) { + shell_output(&packetize_command, + ptr, size, + "", 0); + queuebuf_free(q); + q = NULL; + PROCESS_EXIT(); + } + + memcpy(ptr + size, input->data1, input->len1); + size += input->len1; + memcpy(ptr + size, input->data2, input->len2); + size += input->len2; + + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_routes_process, ev, data) +{ + struct { + uint16_t len; + uint16_t dest; + uint16_t nexthop; + uint16_t hop_count; + uint16_t seqno; + } msg; + int i; + struct route_entry *r; + + PROCESS_BEGIN(); + + memset(&msg, 0, sizeof(msg)); + msg.len = 4; + for(i = 0; i < route_num(); ++i) { + r = route_get(i); + rimeaddr_copy((rimeaddr_t *)&msg.dest, &r->dest); + rimeaddr_copy((rimeaddr_t *)&msg.nexthop, &r->nexthop); + msg.hop_count = r->hop_count; + msg.seqno = r->seqno; + shell_output(&routes_command, &msg, sizeof(msg), "", 0); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_neighbors_process, ev, data) +{ + struct { + uint16_t len; + uint16_t addr; + uint16_t rtmetric; + uint16_t etx; + } msg; + int i; + struct neighbor *n; + + PROCESS_BEGIN(); + + for(i = 0; i < neighbor_num(); ++i) { + + n = neighbor_get(i); + + if(!rimeaddr_cmp(&n->addr, &rimeaddr_null)) { + memset(&msg, 0, sizeof(msg)); + msg.len = 3; + rimeaddr_copy((rimeaddr_t *)&msg.addr, &n->addr); + msg.rtmetric = n->rtmetric; + msg.etx = neighbor_etx(n); + shell_output(&neighbors_command, &msg, sizeof(msg), "", 0); + } + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_netcmd_server_process, ev, data) +{ + static struct process *child_command; + int err; + PROCESS_BEGIN(); + + /* XXX: direct output to null. */ + printf("netcmd server got command string '%s'\n", (char *)data); + err = shell_start_command(data, strlen((char * )data), NULL, &child_command); + if(err == SHELL_FOREGROUND && process_is_running(child_command)) { + PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_EXIT || + (ev == PROCESS_EVENT_EXITED && + data == child_command)); + if(ev == PROCESS_EVENT_EXIT) { + process_exit(child_command); + } + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_netcmd_process, ev, data) +{ + struct trickle_msg *msg; + int len; + + PROCESS_BEGIN(); + + len = strlen((char *)data); + if(len > RIMEBUF_SIZE) { + char buf[32]; + snprintf(buf, sizeof(buf), "%d", len); + shell_output_str(&netcmd_command, "command line too large: ", buf); + } else { + + rimebuf_clear(); + msg = rimebuf_dataptr(); + rimebuf_set_datalen(1 + len + 1); + msg->type = TRICKLE_TYPE_NETCMD; + strcpy(msg->netcmd, data); + printf("netcmd sending '%s'\n", msg->netcmd); + trickle_send(&trickle); + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_nodes_process, ev, data) +{ + static struct etimer etimer; + struct trickle_msg *msg; + char buf[10]; + PROCESS_BEGIN(); + + if(!is_sink) { + + shell_output_str(&nodes_command, + "Setting up a collection network...", ""); +#if TIMESYNCH_CONF_ENABLED + timesynch_set_authority_level(0); +#endif + collect_set_sink(&collect, 1); + + etimer_set(&etimer, CLOCK_SECOND * 2); + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + is_sink = 1; + } + + rimebuf_clear(); + msg = rimebuf_dataptr(); + rimebuf_set_datalen(sizeof(struct trickle_msg)); + msg->type = TRICKLE_TYPE_NODES; + trickle_send(&trickle); + + etimer_set(&etimer, CLOCK_SECOND * 10); + waiting_for_nodes = 1; + shell_output_str(&nodes_command, + "Request sent, waiting for replies...", ""); + messages_received = 0; + + PROCESS_WAIT_UNTIL(etimer_expired(&etimer)); + snprintf(buf, sizeof(buf), "%d", messages_received); + shell_output_str(&nodes_command, buf, " nodes heard"); + + waiting_for_nodes = 0; + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +#if WITH_TREEDEPTH +PROCESS_THREAD(shell_treedepth_process, ev, data) +{ + char buf[20]; + + PROCESS_BEGIN(); + + snprintf(buf, sizeof(buf), "%d", collect_depth(&collect)); + + shell_output_str(&treedepth_command, buf, ""); + + PROCESS_END(); +} +#endif /* WITH_TREEDEPTH */ +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_collect_process, ev, data) +{ + + PROCESS_BEGIN(); + +#if TIMESYNCH_CONF_ENABLED + timesynch_set_authority_level(0); +#endif + collect_set_sink(&collect, 1); + + is_sink = 1; + waiting_for_collect = 1; + + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + + waiting_for_collect = 0; + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_send_process, ev, data) +{ + struct shell_input *input; + int len; + struct collect_msg *msg; + + PROCESS_BEGIN(); + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + len = input->len1 + input->len2; + + if(len == 0) { + PROCESS_EXIT(); + } + + if(len < RIMEBUF_SIZE) { + rimebuf_clear(); + rimebuf_set_datalen(len + COLLECT_MSG_HDRSIZE); + msg = rimebuf_dataptr(); + memcpy(msg->data, input->data1, input->len1); + memcpy(msg->data + input->len1, input->data2, input->len2); +#if TIMESYNCH_CONF_ENABLED + msg->timestamp = timesynch_time(); +#else + msg->timestamp = 0; +#endif + /* printf("Sending %d bytes\n", len);*/ + collect_send(&collect, COLLECT_REXMITS); + } + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +#if WITH_DEBUG_COMMANDS +PROCESS_THREAD(shell_broadcast_process, ev, data) +{ + struct shell_input *input; + int len; + struct collect_msg *msg; + + PROCESS_BEGIN(); + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + len = input->len1 + input->len2; + + if(len == 0) { + PROCESS_EXIT(); + } + + if(len < RIMEBUF_SIZE) { + rimebuf_clear(); + rimebuf_set_datalen(len + COLLECT_MSG_HDRSIZE); + msg = rimebuf_dataptr(); + memcpy(msg->data, input->data1, input->len1); + memcpy(msg->data + input->len1, input->data2, input->len2); +#if TIMESYNCH_CONF_ENABLED + msg->timestamp = timesynch_time(); +#else + msg->timestamp = 0; +#endif + /* printf("Sending %d bytes\n", len);*/ + abc_send(&abc); + } + } + PROCESS_END(); +} +static void +recv_abc(struct abc_conn *c) +{ + printf("abc message received\n"); +} +static const struct abc_callbacks abc_callbacks = {recv_abc}; +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_unicast_process, ev, data) +{ + struct shell_input *input; + static rimeaddr_t receiver; + int len; + const char *nextptr; + struct collect_msg *msg; + char buf[30]; + + PROCESS_BEGIN(); + + receiver.u8[0] = shell_strtolong(data, &nextptr); + if(nextptr == data || *nextptr != '.') { + shell_output_str(&unicast_command, + "unicast : recevier must be specified", ""); + PROCESS_EXIT(); + } + ++nextptr; + receiver.u8[1] = shell_strtolong(nextptr, &nextptr); + + snprintf(buf, sizeof(buf), "%d.%d", receiver.u8[0], receiver.u8[1]); + shell_output_str(&unicast_command, "Sending unicast packets to ", buf); + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + len = input->len1 + input->len2; + + if(len == 0) { + PROCESS_EXIT(); + } + + if(len < RIMEBUF_SIZE) { + rimebuf_clear(); + rimebuf_set_datalen(len + COLLECT_MSG_HDRSIZE); + msg = rimebuf_dataptr(); + memcpy(msg->data, input->data1, input->len1); + memcpy(msg->data + input->len1, input->data2, input->len2); +#if TIMESYNCH_CONF_ENABLED + msg->timestamp = timesynch_time(); +#else + msg->timestamp = 0; +#endif + /* printf("Sending %d bytes\n", len);*/ + uc_send(&uc, &receiver); + } + } + PROCESS_END(); +} +static void +recv_uc(struct uc_conn *c, rimeaddr_t *from) +{ + printf("uc message received from %d.%d\n", from->u8[0], from->u8[1]); +} +static const struct uc_callbacks uc_callbacks = {recv_uc}; +#endif /* WITH_DEBUG_COMMANDS */ +/*---------------------------------------------------------------------------*/ +static void +recv_collect(rimeaddr_t *originator, u8_t seqno, u8_t hops) +{ + struct collect_msg *collect_msg; + rtimer_clock_t latency; + + collect_msg = rimebuf_dataptr(); + +#if TIMESYNCH_CONF_ENABLED + latency = timesynch_time() - collect_msg->timestamp; +#else + latency = 0; +#endif + + if(waiting_for_collect) { + struct { + uint16_t len; + uint16_t originator; + uint16_t seqno; + uint16_t hops; + uint16_t latency; + } msg; + + + msg.len = 5 + (rimebuf_datalen() - COLLECT_MSG_HDRSIZE) / 2; + rimeaddr_copy((rimeaddr_t *)&msg.originator, originator); + msg.seqno = seqno; + msg.hops = hops; + msg.latency = latency; + /* printf("recv_collect datalen %d\n", rimebuf_datalen());*/ + + shell_output(&collect_command, + &msg, sizeof(msg), + collect_msg->data, rimebuf_datalen() - COLLECT_MSG_HDRSIZE); + } else if(waiting_for_nodes) { + char buf[40]; + snprintf(buf, sizeof(buf), "%d.%d, %d hops, latency %lu ms", + originator->u8[0], originator->u8[1], + hops, (1000L * latency) / RTIMER_ARCH_SECOND); + shell_output_str(&nodes_command, "Message from node ", buf); + messages_received++; + } + +} +static const struct collect_callbacks collect_callbacks = { recv_collect }; +/*---------------------------------------------------------------------------*/ +static void +send_collect(void *dummy) +{ + struct collect_msg msg; +#if TIMESYNCH_CONF_ENABLED + msg.timestamp = timesynch_time(); +#else + msg.timestamp = 0; +#endif + rimebuf_copyfrom(&msg, COLLECT_MSG_HDRSIZE); + collect_send(&collect, COLLECT_REXMITS); +} +/*---------------------------------------------------------------------------*/ +static void +recv_trickle(struct trickle_conn *c) +{ + struct trickle_msg *msg; + + msg = rimebuf_dataptr(); + if(msg->type == TRICKLE_TYPE_NODES) { + ctimer_set(&ctimer, random_rand() % (CLOCK_SECOND * 2), + send_collect, NULL); + } else if(msg->type == TRICKLE_TYPE_NETCMD) { + /* First ensure that the old process is killed. */ + process_exit(&shell_netcmd_server_process); + + /* Make sure that the incoming command is null-terminated (which + is should be already). */ + + msg->netcmd[rimebuf_datalen() - 2] = 0; + + /* Start the server process with the incoming command. */ + process_start(&shell_netcmd_server_process, msg->netcmd); + } +} +const static struct trickle_callbacks trickle_callbacks = { recv_trickle }; +/*---------------------------------------------------------------------------*/ +void +shell_rime_init(void) +{ + trickle_open(&trickle, CLOCK_SECOND * 4, 16, &trickle_callbacks); + collect_open(&collect, 17, &collect_callbacks); + + shell_register_command(&collect_command); + shell_register_command(&neighbors_command); + shell_register_command(&netcmd_command); + shell_register_command(&nodes_command); + shell_register_command(&packetize_command); + shell_register_command(&routes_command); + shell_register_command(&send_command); + +#if WITH_TREEDEPTH + shell_register_command(&treedepth_command); +#endif /* WITH_TREEDEPTH */ + +#if WITH_DEBUG_COMMANDS + uc_open(&uc, 14, &uc_callbacks); + abc_open(&abc, 15, &abc_callbacks); + shell_register_command(&broadcast_command); + shell_register_command(&unicast_command); +#endif /* WITH_DEBUG_COMMANDS */ +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-rime.h b/apps/shell/shell-rime.h new file mode 100644 index 000000000..efe3049dd --- /dev/null +++ b/apps/shell/shell-rime.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-rime.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_RIME_H__ +#define __SHELL_RIME_H__ + +#include "shell.h" + +void shell_rime_init(void); + +#endif /* __SHELL_RIME_H__ */ diff --git a/apps/shell/shell-rsh.c b/apps/shell/shell-rsh.c new file mode 100644 index 000000000..a28537b9b --- /dev/null +++ b/apps/shell/shell-rsh.c @@ -0,0 +1,215 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-rsh.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell.h" + +#include "net/rime.h" +#include "net/rime/meshconn.h" + +#include +#include + +static struct meshconn_conn meshconn; +static struct process *front_process; + +static int initiator = 0; + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_rsh_process, "rsh"); +SHELL_COMMAND(rsh_command, + "rsh", + "rsh : remote login to another node", + &shell_rsh_process); +PROCESS(shell_rsh_server_process, "rsh server"); +/*SHELL_COMMAND(rsh_server_command, + "rsh server", + "rsh server: never invoked directly", + &shell_rsh_server_process);*/ +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_rsh_process, ev, data) +{ + static rimeaddr_t receiver; + const char *nextptr; + char buf[40]; + + PROCESS_BEGIN(); + + receiver.u8[0] = shell_strtolong(data, &nextptr); + if(nextptr == data || *nextptr != '.') { + shell_output_str(&rsh_command, + "rsh : node address must be specified", ""); + PROCESS_EXIT(); + } + ++nextptr; + receiver.u8[1] = shell_strtolong(nextptr, &nextptr); + + sprintf(buf, "%d.%d", receiver.u8[0], receiver.u8[1]); + shell_output_str(&rsh_command, "Connecting to ", buf); + + initiator = 1; + meshconn_connect(&meshconn, &receiver); + + while(1) { + struct shell_input *input; + PROCESS_WAIT_EVENT(); + if(ev == shell_event_input) { + input = data; + if(input->len1 + input->len2 == 0) { + meshconn_close_connection(&meshconn); + PROCESS_EXIT(); + } + if(input->len1 + input->len2 >= RIMEBUF_SIZE) { + shell_output_str(&rsh_command, "rsh: input too long", ""); + } else { + rimebuf_clear(); + memcpy(rimebuf_dataptr(), input->data1, input->len1); + memcpy((char *)rimebuf_dataptr() + input->len1, + input->data2, input->len2); + rimebuf_set_datalen(input->len1 + input->len2); + meshconn_send(&meshconn); + } + } + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_rsh_server_process, ev, data) +{ + PROCESS_BEGIN(); + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input || + (ev == PROCESS_EVENT_EXITED && + data == front_process)); + if(ev == PROCESS_EVENT_EXITED) { + front_process = NULL; + } else if(ev == shell_event_input) { + struct shell_input *input; + input = data; + rimebuf_clear(); + memcpy(rimebuf_dataptr(), input->data1, input->len1); + memcpy((char *)rimebuf_dataptr() + input->len1, + input->data2, input->len2); + rimebuf_set_datalen(input->len1 + input->len2); + printf("Sending meshconn with %d + %d bytes\n", + input->len1, input->len2); + meshconn_send(&meshconn); + } + /* PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input);*/ + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +static void +connected_meshconn(struct meshconn_conn *c) +{ + printf("connected\n"); +} +static void +recv_meshconn(struct meshconn_conn *c) +{ + /* int ret; + struct process *started_process;*/ + + /* printf("got '%.*s'\n", rimebuf_datalen(), (char *)rimebuf_dataptr());*/ + /* Echo reply */ + if(!initiator) { + rimebuf_copyfrom("abcdefghijklmnopq", 18); + meshconn_send(c); + } + + return; +#if 0 + if(front_process != NULL && + process_is_running(front_process)) { + struct shell_input input; + input.data1 = rimebuf_dataptr(); + input.len1 = rimebuf_datalen(); + input.data2 = ""; + input.len2 = 0; + process_post_synch(front_process, shell_event_input, &input); + } else { + + ret = shell_start_command(rimebuf_dataptr(), rimebuf_datalen(), + &rsh_server_command, &started_process); + + if(started_process != NULL && + ret == SHELL_FOREGROUND && + process_is_running(started_process)) { + front_process = started_process; + } + } + /* shell_input(rimebuf_dataptr(), rimebuf_datalen());*/ + /* shell_output(&rsh_command, rimebuf_dataptr(), rimebuf_datalen(), "", 0);*/ +#endif +} +static void +closed_meshconn(struct meshconn_conn *c) +{ + printf("closed\n"); +} +static void +timedout_meshconn(struct meshconn_conn *c) +{ + printf("timedout\n"); +} +static void +reset_meshconn(struct meshconn_conn *c) +{ + printf("reset\n"); +} +/*---------------------------------------------------------------------------*/ +static struct meshconn_callbacks meshconn_callbacks = { connected_meshconn, + recv_meshconn, + closed_meshconn, + timedout_meshconn, + reset_meshconn }; +/*---------------------------------------------------------------------------*/ +void +shell_rsh_init(void) +{ + meshconn_open(&meshconn, 23, &meshconn_callbacks); + shell_register_command(&rsh_command); + + process_start(&shell_rsh_server_process, NULL); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-rsh.h b/apps/shell/shell-rsh.h new file mode 100644 index 000000000..06704bab5 --- /dev/null +++ b/apps/shell/shell-rsh.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-rsh.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_RSH_H__ +#define __SHELL_RSH_H__ + +#include "shell.h" + +void shell_rsh_init(void); + +#endif /* __SHELL_RSH_H__ */ diff --git a/apps/shell/shell-sendtest.c b/apps/shell/shell-sendtest.c new file mode 100644 index 000000000..9d4a500a0 --- /dev/null +++ b/apps/shell/shell-sendtest.c @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-sendtest.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell-ps.h" +#include "net/rime.h" + +#include +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_sendtest_process, "sendtest"); +SHELL_COMMAND(sendtest_command, + "sendtest", + "sendtest: measure single-hop throughput", + &shell_sendtest_process); +/*---------------------------------------------------------------------------*/ +static clock_time_t start_time_rucb, end_time_rucb; +static unsigned long filesize, bytecount, packetsize; +static int download_complete; +static void +write_chunk(struct rucb_conn *c, int offset, int flag, + char *data, int datalen) +{ +#if NETSIM + { + char buf[100]; + printf("received %d; %d\n", offset, datalen); + sprintf(buf, "%d%%", (100 * (offset + datalen)) / filesize); + ether_set_text(buf); + } +#endif /* NETSIM */ + printf("+"); +} +static int +read_chunk(struct rucb_conn *c, int offset, char *to, int maxsize) +{ + int size; + printf("-"); + size = maxsize; + if(bytecount + maxsize >= filesize) { + size = filesize - bytecount; + } + if(size > packetsize) { + size = packetsize; + } + bytecount += size; + if(bytecount == filesize) { + end_time_rucb = clock_time(); + download_complete = 1; + process_post(&shell_sendtest_process, PROCESS_EVENT_CONTINUE, NULL); + /* profile_aggregates_print(); */ +/* profile_print_stats(); */ +// print_stats(); + } + + /* printf("bytecount %lu\n", bytecount);*/ + return size; +} +const static struct rucb_callbacks rucb_callbacks = {write_chunk, + read_chunk, + NULL}; +static struct rucb_conn rucb; +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_sendtest_process, ev, data) +{ + static rimeaddr_t receiver; + const char *nextptr; + const char *args; + char buf[40]; + + PROCESS_BEGIN(); + + args = data; + receiver.u8[0] = shell_strtolong(args, &nextptr); + if(nextptr == data || *nextptr != '.') { + shell_output_str(&sendtest_command, + "sendtest [packetsize]: recevier must be specified", ""); + PROCESS_EXIT(); + } + args = nextptr + 1; + receiver.u8[1] = shell_strtolong(args, &nextptr); + + + args = nextptr; + while(*args == ' ') { + ++args; + } + filesize = shell_strtolong(args, &nextptr); + if(nextptr == data || filesize == 0) { + shell_output_str(&sendtest_command, + "sendtest [packetsize]: size must be specified and > 0", ""); + PROCESS_EXIT(); + } + + args = nextptr; + while(*args == ' ') { + ++args; + } + packetsize = 64; + packetsize = shell_strtolong(args, &nextptr); + if(packetsize == 0) { + shell_output_str(&sendtest_command, + "sendtest [packetsize]: packetsize must be > 0", ""); + PROCESS_EXIT(); + } + + snprintf(buf, sizeof(buf), "%d.%d, %lu bytes, packetsize %lu", + receiver.u8[0], receiver.u8[1], filesize, packetsize); + shell_output_str(&sendtest_command, "Sending data to ", buf); + + bytecount = 0; + download_complete = 0; + + start_time_rucb = clock_time(); + rucb_send(&rucb, &receiver); + PROCESS_WAIT_UNTIL(download_complete); + sprintf(buf, "%d seconds, %lu bytes/second", + (int)((end_time_rucb - start_time_rucb) / CLOCK_SECOND), + CLOCK_SECOND * filesize / (end_time_rucb - start_time_rucb)); + shell_output_str(&sendtest_command, "Completed in ", buf); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_sendtest_init(void) +{ + rucb_open(&rucb, 22, &rucb_callbacks); + shell_register_command(&sendtest_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-sendtest.h b/apps/shell/shell-sendtest.h new file mode 100644 index 000000000..91c7ccd01 --- /dev/null +++ b/apps/shell/shell-sendtest.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-sendtest.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_SENDTEST_H__ +#define __SHELL_SENDTEST_H__ + +#include "shell.h" + +void shell_sendtest_init(void); + +#endif /* __SHELL_SENDTEST_H__ */ diff --git a/apps/shell/shell-sky.c b/apps/shell/shell-sky.c new file mode 100644 index 000000000..820c9a04f --- /dev/null +++ b/apps/shell/shell-sky.c @@ -0,0 +1,362 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-sky.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Tmote Sky-specific Contiki shell commands + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell-sky.h" + +#include "dev/watchdog.h" + +#include "net/rime.h" +#include "dev/simple-cc2420.h" +#include "dev/leds.h" +#include "dev/light.h" +#include "dev/sht11.h" + +#include "net/rime/timesynch.h" + +#include +#include + +struct power_msg { + uint16_t len; + uint32_t cpu; + uint32_t lpm; + uint32_t transmit; + uint32_t listen; +}; + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_sense_process, "sense"); +SHELL_COMMAND(sense_command, + "sense", + "sense: print out sensor data", + &shell_sense_process); +PROCESS(shell_senseconv_process, "senseconv"); +SHELL_COMMAND(senseconv_command, + "senseconv", + "senseconv: convert 'sense' data to human readable format", + &shell_senseconv_process); +PROCESS(shell_txpower_process, "txpower"); +SHELL_COMMAND(txpower_command, + "txpower", + "txpower : change CC2420 transmission power (0 - 31)", + &shell_txpower_process); +PROCESS(shell_rfchannel_process, "rfchannel"); +SHELL_COMMAND(rfchannel_command, + "rfchannel", + "rfchannel : change CC2420 transmission power (0 - 31)", + &shell_rfchannel_process); +PROCESS(shell_power_process, "power"); +SHELL_COMMAND(power_command, + "power", + "power: print power profile", + &shell_power_process); +PROCESS(shell_energy_process, "energy"); +SHELL_COMMAND(energy_command, + "energy", + "energy: print energy profile", + &shell_energy_process); +PROCESS(shell_powerconv_process, "powerconv"); +SHELL_COMMAND(powerconv_command, + "powerconv", + "powerconv: convert power profile to human readable output", + &shell_powerconv_process); +/*---------------------------------------------------------------------------*/ +#define MAX(a, b) ((a) > (b)? (a): (b)) +#define MIN(a, b) ((a) < (b)? (a): (b)) +struct spectrum { + int channel[16]; +}; +#define NUM_SAMPLES 4 +static struct spectrum rssi_samples[NUM_SAMPLES]; +static int +do_rssi(void) +{ + static int sample; + int channel; + + rime_mac->off(); + + simple_cc2420_on(); + for(channel = 11; channel <= 26; ++channel) { + simple_cc2420_set_channel(channel); + rssi_samples[sample].channel[channel - 11] = simple_cc2420_rssi() + 53; + } + + rime_mac->on(); + + sample = (sample + 1) % NUM_SAMPLES; + + { + int channel, tot; + tot = 0; + for(channel = 0; channel < 16; ++channel) { + int max = -256; + int i; + for(i = 0; i < NUM_SAMPLES; ++i) { + max = MAX(max, rssi_samples[i].channel[channel]); + } + tot += max / 20; + } + return tot; + } +} +/*---------------------------------------------------------------------------*/ +struct sense_msg { + uint16_t len; + uint16_t clock; + uint16_t timesynch_time; + uint16_t light1; + uint16_t light2; + uint16_t temp; + uint16_t humidity; + uint16_t rssi; +}; +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_sense_process, ev, data) +{ + struct sense_msg msg; + PROCESS_BEGIN(); + + msg.len = 7; + msg.clock = clock_time(); + msg.timesynch_time = timesynch_time(); + msg.light1 = sensors_light1(); + msg.light2 = sensors_light2(); + msg.temp = sht11_temp(); + msg.humidity = sht11_humidity(); + msg.rssi = do_rssi(); + + shell_output(&sense_command, &msg, sizeof(msg), "", 0); + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_senseconv_process, ev, data) +{ + struct shell_input *input; + struct sense_msg *msg; + PROCESS_BEGIN(); + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + if(input->len1 + input->len2 == 0) { + PROCESS_EXIT(); + } + msg = (struct sense_msg *)input->data1; + + if(msg != NULL) { + char buf[40]; + snprintf(buf, sizeof(buf), + "l1 %d l2 %d t %d.%d h %d r %d\n", + 10 * msg->light1 / 7, + 46 * msg->light2 / 10, + (msg->temp / 10 - 396) / 10, + (msg->temp / 10 - 396) % 10, + (int)(-4L + 405L * msg->humidity / 10000L), + msg->rssi); + shell_output(&senseconv_command, buf, strlen(buf), "", 0); + } + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_txpower_process, ev, data) +{ + struct { + uint16_t len; + uint16_t txpower; + } msg; + const char *newptr; + PROCESS_BEGIN(); + + msg.txpower = shell_strtolong(data, &newptr); + + /* If no transmission power was given on the command line, we print + out the current txpower. */ + + if(newptr == data) { + msg.txpower = simple_cc2420_get_txpower(); + } else { + simple_cc2420_set_txpower(msg.txpower); + } + + msg.len = 1; + + shell_output(&txpower_command, &msg, sizeof(msg), "", 0); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_rfchannel_process, ev, data) +{ + struct { + uint16_t len; + uint16_t channel; + } msg; + const char *newptr; + PROCESS_BEGIN(); + + msg.channel = shell_strtolong(data, &newptr); + + /* If no channel was given on the command line, we print out the + current channel. */ + + if(newptr == data) { + msg.channel = simple_cc2420_get_channel(); + } else { + simple_cc2420_set_channel(msg.channel); + } + + msg.len = 1; + + shell_output(&txpower_command, &msg, sizeof(msg), "", 0); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_power_process, ev, data) +{ + static unsigned long last_cpu, last_lpm, last_transmit, last_listen; + struct power_msg msg; + + PROCESS_BEGIN(); + + msg.len = 8; + msg.cpu = energest_type_time(ENERGEST_TYPE_CPU) - last_cpu; + msg.lpm = energest_type_time(ENERGEST_TYPE_LPM) - last_lpm; + msg.transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT) - last_transmit; + msg.listen = energest_type_time(ENERGEST_TYPE_LISTEN) - last_listen; + + last_cpu = energest_type_time(ENERGEST_TYPE_CPU); + last_lpm = energest_type_time(ENERGEST_TYPE_LPM); + last_transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT); + last_listen = energest_type_time(ENERGEST_TYPE_LISTEN); + + shell_output(&power_command, &msg, sizeof(msg), "", 0); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_energy_process, ev, data) +{ + struct power_msg msg; + + PROCESS_BEGIN(); + + msg.len = 8; + msg.cpu = energest_type_time(ENERGEST_TYPE_CPU); + msg.lpm = energest_type_time(ENERGEST_TYPE_LPM); + msg.transmit = energest_type_time(ENERGEST_TYPE_TRANSMIT); + msg.listen = energest_type_time(ENERGEST_TYPE_LISTEN); + + shell_output(&energy_command, &msg, sizeof(msg), "", 0); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +#define DEC2FIX(h,d) ((h * 64L) + (unsigned long)((d * 64L) / 1000L)) +static void +printpower(struct power_msg *msg) +{ + char buf[50]; + unsigned long avg_power; + unsigned long time; + + time = msg->cpu + msg->lpm; + + avg_power = (3L * + (msg->cpu * DEC2FIX(1L,800L) + + msg->lpm * DEC2FIX(0L,545L) + + msg->transmit * DEC2FIX(17L,700L) + + msg->listen * DEC2FIX(20L,0))) / ((64L * time) / 1000); + snprintf(buf, sizeof(buf), "CPU %d%% LPM %d%% tx %d%% rx %d%% tot %lu uW", + (int)((100L * (unsigned long)msg->cpu) / time), + (int)((100L * (unsigned long)msg->lpm) / time), + (int)((100L * (unsigned long)msg->transmit) / time), + (int)((100L * (unsigned long)msg->listen) / time), + avg_power); + shell_output_str(&powerconv_command, buf, ""); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_powerconv_process, ev, data) +{ + struct power_msg *msg; + struct shell_input *input; + int len; + + PROCESS_BEGIN(); + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + if(input->len1 + input->len2 == 0) { + PROCESS_EXIT(); + } + len = input->len1; + for(msg = (struct power_msg *)input->data1; + len > 0; + msg++, len -= sizeof(struct power_msg)) { + printpower(msg); + } + len = input->len2; + for(msg = (struct power_msg *)input->data2; + len > 0; + msg++, len -= sizeof(struct power_msg)) { + printpower(msg); + } + + } + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_sky_init(void) +{ + shell_register_command(&power_command); + shell_register_command(&powerconv_command); + shell_register_command(&energy_command); + shell_register_command(&txpower_command); + shell_register_command(&rfchannel_command); + shell_register_command(&sense_command); + shell_register_command(&senseconv_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-sky.h b/apps/shell/shell-sky.h new file mode 100644 index 000000000..da46a04df --- /dev/null +++ b/apps/shell/shell-sky.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-sky.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * Header file for Tmote Sky-specific Contiki shell commands + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_SKY_H__ +#define __SHELL_SKY_H__ + +#include "shell.h" + +void shell_sky_init(void); + +#endif /* __SHELL_SKY_H__ */ diff --git a/apps/shell/shell-text.c b/apps/shell/shell-text.c new file mode 100644 index 000000000..5009a3b36 --- /dev/null +++ b/apps/shell/shell-text.c @@ -0,0 +1,298 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-text.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell.h" + +#include +#include +#ifndef HAVE_SNPRINTF +int snprintf(char *str, size_t size, const char *format, ...); +#endif /* HAVE_SNPRINTF */ + +#include + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_echo_process, "echo"); +SHELL_COMMAND(echo_command, + "echo", + "echo : print ", + &shell_echo_process); +PROCESS(shell_dec64_process, "dec64"); +SHELL_COMMAND(dec64_command, + "dec64", + "dec64: decode base64 input", + &shell_dec64_process); +PROCESS(shell_binprint_process, "binprint"); +SHELL_COMMAND(binprint_command, + "binprint", + "binprint: print binary data in decimal format", + &shell_binprint_process); +PROCESS(shell_hd_process, "hd"); +SHELL_COMMAND(hd_command, + "hd", + "hd: print binary data in hexadecimal format", + &shell_hd_process); +PROCESS(shell_size_process, "size"); +SHELL_COMMAND(size_command, + "size", + "size: print the size of the input", + &shell_size_process); +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_echo_process, ev, data) +{ + PROCESS_BEGIN(); + + shell_output(&echo_command, data, strlen(data), "\n", 1); + + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +#define BASE64_MAX_LINELEN 76 + +struct base64_decoder_state { + uint8_t data[3 * BASE64_MAX_LINELEN / 4]; + int dataptr; + unsigned long tmpdata; + int sextets; + int padding; +}; +/*---------------------------------------------------------------------------*/ +static int +base64_decode_char(char c) +{ + if(c >= 'A' && c <= 'Z') { + return c - 'A'; + } else if(c >= 'a' && c <= 'z') { + return c - 'a' + 26; + } else if(c >= '0' && c <= '9') { + return c - '0' + 52; + } else if(c == '+') { + return 62; + } else if(c == '/') { + return 63; + } else { + return 0; + } +} +/*---------------------------------------------------------------------------*/ +static int +base64_add_char(struct base64_decoder_state *s, char c) +{ + if(isspace(c)) { + return 0; + } + + if(s->dataptr >= sizeof(s->data)) { + return 0; + } + if(c == '=') { + ++s->padding; + } + + s->tmpdata = (s->tmpdata << 6) | base64_decode_char(c); + ++s->sextets; + if(s->sextets == 4) { + s->sextets = 0; + s->data[s->dataptr] = (s->tmpdata >> 16) & 0xff; + s->data[s->dataptr + 1] = (s->tmpdata >> 8) & 0xff; + s->data[s->dataptr + 2] = (s->tmpdata) & 0xff; + s->dataptr += 3; + if(s->dataptr == sizeof(s->data)) { + return 0; + } else { + return 1; + } + } + return 1; +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_dec64_process, ev, data) +{ + PROCESS_BEGIN(); + + while(1) { + struct shell_input *input; + struct base64_decoder_state s; + int i; + + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + if(input->len1 + input->len2 == 0) { + PROCESS_EXIT(); + } + + s.sextets = s.dataptr = s.padding = 0; + + for(i = 0; i < input->len1; ++i) { + base64_add_char(&s, input->data1[i]); + } + for(i = 0; i < input->len2; ++i) { + base64_add_char(&s, input->data2[i]); + } + shell_output(&dec64_command, s.data, s.dataptr - s.padding, "", 0); + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_hd_process, ev, data) +{ + struct shell_input *input; + PROCESS_BEGIN(); + uint16_t *ptr; + int i; + char buf[57], *bufptr; + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + if(input->len1 + input->len2 == 0) { + PROCESS_EXIT(); + } + + bufptr = buf; + ptr = (uint16_t *)input->data1; + for(i = 0; i < input->len1 && i < input->len1 - 1; i += 2) { + bufptr += sprintf(bufptr, "0x%04x ", *ptr); + if(bufptr - buf >= sizeof(buf) - 7) { + shell_output_str(&hd_command, buf, ""); + bufptr = buf; + } + ptr++; + } + + ptr = (uint16_t *)input->data2; + for(i = 0; i < input->len2 && i < input->len2 - 1; i += 2) { + bufptr += sprintf(bufptr, "0x%04x ", *ptr); + if(bufptr - buf >= sizeof(buf) - 7) { + shell_output_str(&hd_command, buf, ""); + bufptr = buf; + } + ptr++; + } + if(bufptr != buf) { + shell_output_str(&hd_command, buf, ""); + } + + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_binprint_process, ev, data) +{ + struct shell_input *input; + PROCESS_BEGIN(); + uint16_t *ptr; + int i; + char buf[2*64], *bufptr; + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + if(input->len1 + input->len2 == 0) { + PROCESS_EXIT(); + } + + bufptr = buf; + ptr = (uint16_t *)input->data1; + for(i = 0; i < input->len1 && i < input->len1 - 1; i += 2) { + bufptr += sprintf(bufptr, "%u ", *ptr); + if(bufptr - buf >= sizeof(buf) - 6) { + shell_output_str(&binprint_command, buf, ""); + bufptr = buf; + } + ptr++; + } + + ptr = (uint16_t *)input->data2; + for(i = 0; i < input->len2 && i < input->len2 - 1; i += 2) { + bufptr += sprintf(bufptr, "%u ", *ptr); + if(bufptr - buf >= sizeof(buf) - 6) { + shell_output_str(&binprint_command, buf, ""); + bufptr = buf; + } + ptr++; + } + + if(bufptr != buf) { + shell_output_str(&binprint_command, buf, ""); + } + + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +PROCESS_THREAD(shell_size_process, ev, data) +{ + struct shell_input *input; + static unsigned long size; + + PROCESS_BEGIN(); + size = 0; + + while(1) { + PROCESS_WAIT_EVENT_UNTIL(ev == shell_event_input); + input = data; + + size += input->len1 + input->len2; + + if(input->len1 + input->len2 == 0) { + char buf[10]; + snprintf(buf, sizeof(buf), "%lu", size); + shell_output_str(&size_command, buf, ""); + PROCESS_EXIT(); + } + } + PROCESS_END(); +} +/*---------------------------------------------------------------------------*/ +void +shell_text_init(void) +{ + shell_register_command(&binprint_command); + shell_register_command(&hd_command); + shell_register_command(&echo_command); + shell_register_command(&dec64_command); + shell_register_command(&size_command); +} +/*---------------------------------------------------------------------------*/ diff --git a/apps/shell/shell-text.h b/apps/shell/shell-text.h new file mode 100644 index 000000000..67571516e --- /dev/null +++ b/apps/shell/shell-text.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-text.h,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#ifndef __SHELL_TEXT_H__ +#define __SHELL_TEXT_H__ + +#include "shell.h" + +void shell_text_init(void); + +#endif /* __SHELL_TEXT_H__ */ diff --git a/apps/shell/shell-time.c b/apps/shell/shell-time.c new file mode 100644 index 000000000..e067c51f9 --- /dev/null +++ b/apps/shell/shell-time.c @@ -0,0 +1,347 @@ +/* + * Copyright (c) 2008, Swedish Institute of Computer Science. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * This file is part of the Contiki operating system. + * + * $Id: shell-time.c,v 1.1 2008/02/04 23:42:17 adamdunkels Exp $ + */ + +/** + * \file + * A brief description of what this file is. + * \author + * Adam Dunkels + */ + +#include "contiki.h" +#include "shell-time.h" + +#include "sys/clock.h" +#include "net/rime/timesynch.h" + +#include "lib/random.h" + +#include +#include + +#define MAX_COMMANDLENGTH 64 + +/*---------------------------------------------------------------------------*/ +PROCESS(shell_time_process, "time"); +SHELL_COMMAND(time_command, + "time", + "time [seconds]: print time or set time in seconds since 1970", + &shell_time_process); +PROCESS(shell_timestamp_process, "timestamp"); +SHELL_COMMAND(timestamp_command, + "timestamp", + "timestamp: prepent a timestamp to data", + &shell_timestamp_process); +PROCESS(shell_repeat_process, "repeat"); +PROCESS(shell_repeat_server_process, "repeat server"); +SHELL_COMMAND(repeat_command, + "repeat", + "repeat