ccc-cmd/ccc-cmd.c

Date:
2005/08/15
Author:
Mario Strasser <mast@gmx.net>
Example application to show the use of the chipcon radio on the BTnode rev3.

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.5 2006/03/29 01:15:01 olereinhardt Exp $
00032  * 
00033  */
00034 
00067 #include <stdio.h>
00068 #include <string.h>
00069 #include <io.h>
00070 #include <stdlib.h>
00071 #include <dev/usart.h>
00072 #include <dev/usartavr.h>
00073 #include <sys/thread.h>
00074 #include <sys/timer.h>
00075 #include <sys/heap.h>
00076 #include <sys/tracer.h>
00077 #include <led/btn-led.h>
00078 #include <hardware/btn-hardware.h>
00079 #include <terminal/btn-terminal.h>
00080 #include <terminal/btn-cmds.h>
00081 #include <terminal/nut-cmds.h>
00082 #include <cc/bmac.h>
00083 #include <cc/ccc.h>
00084 
00088 static void term_init(void) 
00089 {
00090     FILE *uart_terminal;
00091     u_long baud = 57600;
00092             
00093     /* initialize UART 1 */
00094     NutRegisterDevice(&APP_UART, 0, 0);
00095     uart_terminal = fopen(APP_UART.dev_name, "r+");
00096     _ioctl(_fileno(uart_terminal), UART_SETSPEED, &baud);
00097     btn_terminal_init(uart_terminal, "[bt-cmd@btnode]$");
00098     freopen(APP_UART.dev_name, "w", stdout);    
00099 }
00100 
00104 static void btnode_init(void) 
00105 {    
00106     int res;
00107     /* initialize the btnode hardware */
00108     btn_hardware_init();
00109     btn_led_init(0);
00110     term_init(); 
00111     res = bmac_init(0);
00112     if (res != 0) DEBUGT("bmac_init() failed\n");
00113     bmac_enable_led(1);
00114     res = ccc_init(&bmac_interface);
00115     if (res != 0) DEBUGT("ccc_init() failed\n");        
00116 }
00117 
00121 void test_pkt_handler(ccc_packet_t *pkt)
00122 {
00123     DEBUGT("\033[32mPacket of type %d from %04x to %04x"
00124            "(%d bytes):\033[39m %.80s\n", pkt->type, pkt->src, pkt->dst,
00125            pkt->length, pkt->data);
00126 }
00127 
00128 #define TEST_PACKET_TYPE 0x01
00129 #define TEST_PACKET_SIZE (8*250)
00130 
00134 void ccc_send_echo(char *arg)
00135 {
00136     int i, res;
00137     u_short num, addr = BROADCAST_ADDR;
00138     ccc_packet_t *pkt = new_ccc_packet(TEST_PACKET_SIZE);
00139     if (pkt == NULL || sscanf(arg, "%hu", &num) != 1) {
00140         printf("usage: ccc-echo <#packets>\n");
00141         return;
00142     }
00143     pkt->length = TEST_PACKET_SIZE;
00144     for (i = 0; i < pkt->length; i += 2) {
00145         pkt->data[i] = '-';
00146         pkt->data[i+1] = ':';
00147     }
00148     memcpy(pkt->data, "Echo-Packet: 0123456789", 23);
00149     pkt->data[pkt->length - 1] = 0;
00150     for (i = 0; i < num; i++) {
00151         DEBUGT("ccc_send(%04x) = ", addr);
00152         res = ccc_send(addr, TEST_PACKET_TYPE, pkt);
00153         DEBUGT("%d\n", res);
00154         NutSleep(2000);
00155     }
00156     free(pkt);
00157 }
00158 
00162 static void register_cmds(void)
00163 {
00164     btn_cmds_register_cmds();
00165     nut_cmds_register_cmds();
00166     btn_terminal_register_cmd("ccc-echo", ccc_send_echo);
00167 }
00168 
00174 int main(void) 
00175 {
00176     /* initialize the BTnode hardware and the CC1000 radio */    
00177     btnode_init();    
00178     DEBUGT("btnode initialized.\n");    
00179     /* regiser all commands */
00180     register_cmds();
00181     DEBUGT("commands registered.\n");
00182     /* register packet handler */
00183     ccc_register_packet_handler(TEST_PACKET_TYPE, test_pkt_handler);
00184     /* start terminal */ 
00185     DEBUGT("starting terminal...\n\n");      
00186     btn_terminal_run(BTN_TERMINAL_NOFORK, 256);
00187     DEBUGT("exit\n");
00188     return 0;
00189 }
00190 

Generated on Tue Jan 23 21:12:24 2007 for BTnut System Software by doxygen 1.4.7