00001 /* 00002 * Copyright (C) 2005 Mario Strasser <mast@gmx.net>, 00003 * Swiss Federal Institute of Technology (ETH) Zurich 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions 00007 * are met: 00008 * 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 3. Neither the name of the copyright holders nor the names of 00015 * contributors may be used to endorse or promote products derived 00016 * from this software without specific prior written permission. 00017 * 00018 * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS 00019 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH 00022 * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00023 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00024 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 00025 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 00026 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00027 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF 00028 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00029 * SUCH DAMAGE. 00030 * 00031 * $Id: ccc-cmd.c,v 1.6 2007/11/02 15:10:14 freckle Exp $ 00032 * 00033 */ 00034 00070 #include <stdio.h> 00071 #include <string.h> 00072 #include <io.h> 00073 #include <stdlib.h> 00074 #include <dev/usart.h> 00075 #include <dev/usartavr.h> 00076 #include <sys/thread.h> 00077 #include <sys/timer.h> 00078 #include <sys/heap.h> 00079 #include <sys/tracer.h> 00080 #include <led/btn-led.h> 00081 #include <hardware/btn-hardware.h> 00082 #include <terminal/btn-terminal.h> 00083 #include <terminal/btn-cmds.h> 00084 #include <terminal/nut-cmds.h> 00085 #include <cc/bmac.h> 00086 #include <cc/ccc.h> 00087 00091 static void term_init(void) 00092 { 00093 FILE *uart_terminal; 00094 u_long baud = 57600; 00095 00096 /* initialize UART 1 */ 00097 NutRegisterDevice(&APP_UART, 0, 0); 00098 uart_terminal = fopen(APP_UART.dev_name, "r+"); 00099 _ioctl(_fileno(uart_terminal), UART_SETSPEED, &baud); 00100 btn_terminal_init(uart_terminal, "[bt-cmd@btnode]$"); 00101 freopen(APP_UART.dev_name, "w", stdout); 00102 } 00103 00107 static void btnode_init(void) 00108 { 00109 int res; 00110 /* initialize the btnode hardware */ 00111 btn_hardware_init(); 00112 btn_led_init(0); 00113 term_init(); 00114 res = bmac_init(0); 00115 if (res != 0) DEBUGT("bmac_init() failed\n"); 00116 bmac_enable_led(1); 00117 res = ccc_init(&bmac_interface); 00118 if (res != 0) DEBUGT("ccc_init() failed\n"); 00119 } 00120 00124 void test_pkt_handler(ccc_packet_t *pkt) 00125 { 00126 DEBUGT("\033[32mPacket of type %u, flags %u from %04x to %04x" 00127 "(%u bytes):\033[39m %.80s\n", pkt->type, pkt->flags, pkt->src, pkt->dst, 00128 pkt->length, pkt->data); 00129 } 00130 00131 #define TEST_PACKET_TYPE 0x01 00132 #define TEST_PACKET_SIZE (30) 00133 00137 void ccc_send_echo(char *arg) 00138 { 00139 int i, res; 00140 u_short num, addr = BROADCAST_ADDR; 00141 ccc_packet_t *pkt = new_ccc_packet(TEST_PACKET_SIZE); 00142 if (pkt == NULL || sscanf(arg, "%hu", &num) != 1) { 00143 printf("usage: ccc-echo <#packets>\n"); 00144 return; 00145 } 00146 pkt->length = TEST_PACKET_SIZE; 00147 for (i = 0; i < pkt->length; i += 2) { 00148 pkt->data[i] = '-'; 00149 pkt->data[i+1] = ':'; 00150 } 00151 memcpy(pkt->data, "Echo-Packet: 0123456789", 23); 00152 pkt->data[pkt->length - 1] = 0; 00153 pkt->flags = 0; 00154 for (i = 0; i < num; i++) { 00155 DEBUGT("ccc_send(%04x-%02x) = ", addr, pkt->flags); 00156 res = ccc_send(addr, TEST_PACKET_TYPE, pkt); 00157 DEBUGT("%d\n", res); 00158 NutSleep(2000); 00159 pkt->flags ^= CCC_FLAG_TIMESTAMP; 00160 } 00161 free(pkt); 00162 } 00163 00167 static void register_cmds(void) 00168 { 00169 btn_cmds_register_cmds(); 00170 nut_cmds_register_cmds(); 00171 btn_terminal_register_cmd("ccc-echo", ccc_send_echo); 00172 } 00173 00179 int main(void) 00180 { 00181 /* initialize the BTnode hardware and the CC1000 radio */ 00182 btnode_init(); 00183 DEBUGT("btnode initialized.\n"); 00184 /* regiser all commands */ 00185 register_cmds(); 00186 DEBUGT("commands registered.\n"); 00187 /* register packet handler */ 00188 ccc_register_packet_handler(TEST_PACKET_TYPE, test_pkt_handler); 00189 /* start terminal */ 00190 DEBUGT("starting terminal...\n\n"); 00191 btn_terminal_run(BTN_TERMINAL_NOFORK, 256); 00192 DEBUGT("exit\n"); 00193 return 0; 00194 } 00195