mhop_blink/mhop_blink.c

Simple example application using the connectionless multi-hop transport layer and a connection manager.
Date:
2005/10/27
Author:
Kevin Martin <kevmarti@tik.ee.ethz.ch>
With this application it is possible to send "blink" commands to an arbitrary node in a connected network. Network establishment is done automatically by the connection manger.

After sending a "blink" cmd pkt to a destination node, receiving intermediate nodes flash their blue and red LED's, while the destination node flashes all LED's.

When receiving a "blink" cmd, the receiving node stores the source of the packet in its forwarding table (i.e. the packets sent have set the "source recording" flag). Thus, being lucky (depending on the network topology the connection manager formed) it is possible to gain some inside into the underlying routing protocol.

00001 /*
00002  * Copyright (C) 2000-2005 by ETH Zurich
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
00021  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.btnode.ethz.ch/
00031  */
00032 
00057 #include <stdio.h>
00058 #include <dev/usartavr.h>
00059 #include <sys/heap.h>
00060 #include <sys/event.h>
00061 #include <sys/thread.h>
00062 #include <sys/timer.h>
00063 #include <hardware/btn-hardware.h>
00064 #include <led/btn-led.h>
00065 
00066 #include <bt/bt_hci_defs.h>
00067 #include <bt/bt_hci_cmds.h>
00068 #include <bt/bt_acl_defs.h>
00069 #include <bt/bt_psm.h>
00070 #include <bt/l2cap_cl.h>
00071 
00072 #include <mhop/mhop_cl.h>
00073 #include <mhop/mhop_init.h>
00074 
00075 #include <terminal/btn-terminal.h>
00076 #include <terminal/bt_psm-cmds.h>
00077 #include <terminal/bt-cmds.h>
00078 #include <terminal/log-cmds.h>
00079 #include <terminal/mhop_cl-cmds.h>
00080 
00081 #include <debug/logging.h>
00082 
00083 // choose your connection manger here!
00084 #include <cm/cm_tree.h>
00085 
00086 #include "program_version.h"
00087 
00088 
00089 #define MAX_NR_SERVICES     16
00090 
00091 #define FWD_BUF_SIZE        5
00092 
00093 #define CM_PSM              0x1003
00094 #define MHOP_PSM            0x1005
00095 #define BLINK_PSM           0x1007
00096 
00097 #define CM_COD              933
00098 
00099 
00116 bt_acl_pkt_buf* _mhop_blink_data_cb(bt_acl_pkt_buf* pkt_buf,
00117                                     u_char* data,
00118                                     u_short data_len,
00119                                     u_short service_nr,
00120                                     void* cb_arg)
00121 {
00122     u_char* target;
00123     target = mhop_cl_get_target_addr(pkt_buf->pkt);
00124     
00125     // let all LED's blink
00126     btn_led_add_pattern(BTN_LED_PATTERN_ON_OFF, 15, 3, 5);
00127     
00128     // free the received message
00129     return pkt_buf;
00130 }
00131 
00141 void _mhop_blink_cmd_send_blink_rqst(char* arg) {
00142     u_char i;
00143     unsigned int addr[BD_ADDR_LEN];
00144     bt_addr_t dest_addr;
00145     
00146     // parse user input
00147     if (sscanf(arg, "%2x:%2x:%2x:%2x:%2x:%2x", &addr[5], &addr[4], &addr[3], &addr[2], &addr[1], &addr[0]) == 6) {
00148         // store bt addr
00149         for (i = 0; i < BD_ADDR_LEN; i++) {
00150             dest_addr[i] = (u_char) addr[i];
00151         }
00152         // send the packet
00153         mhop_cl_send_pkt(NULL, 0, dest_addr, BLINK_PSM, MHOP_CL_BROADCAST, MHOP_CL_TTL_INFINITE);
00154         tprintf("blink rqst sent.\n");
00155     }
00156     else tprintf("error usage: blink <addr>\n");
00157 }
00158 
00159 
00160 void _mhop_blink_cmd_send_blink_reply(char* arg) {
00161     u_char i;
00162     unsigned int addr[BD_ADDR_LEN];
00163     bt_addr_t dest_addr;
00164     long retval;
00165     
00166     // parse user input
00167     if (sscanf(arg, "%2x:%2x:%2x:%2x:%2x:%2x", &addr[5], &addr[4], &addr[3], &addr[2], &addr[1], &addr[0]) == 6) {
00168         // store bt addr
00169         for (i = 0; i < BD_ADDR_LEN; i++) {
00170             dest_addr[i] = (u_char) addr[i];
00171         }
00172         // send the packet
00173         retval = mhop_cl_send_pkt(NULL, 0, dest_addr, BLINK_PSM, MHOP_CL_UNICAST, MHOP_CL_TTL_INFINITE);
00174         tprintf("blink reply sent: %d\n", retval);
00175     }
00176     else tprintf("error usage: blink <addr>\n");
00177 }
00178 
00191 long mhop_blink_service_register(bt_psm_t* psmux, u_short psm)
00192 {
00193     long blink_service_nr;
00194     
00195     // register "blink" service at psmux
00196     blink_service_nr =
00197         bt_psm_service_register(psmux, BLINK_PSM, _mhop_blink_data_cb, NULL);
00198     
00199     // return if the "blink" service could has not been registered properly
00200     if (blink_service_nr < 0) {
00201         printf("error registering blink service!\n");
00202         return blink_service_nr;
00203     }
00204     // else, set packet buffers for the "blink" service
00205     else {
00206         // we don't need any buffers for the "blink" service!
00207         bt_psm_service_set_buffers(psmux, blink_service_nr, NULL);
00208     }
00209     return blink_service_nr;
00210 }
00211 
00223 int main(void)
00224 {
00225 //  FILE* terminal;
00226     // pointer to the bt_stack
00227     struct btstack* bt_stack;
00228     
00229     // pointer to the protocol/service multiplexor
00230     bt_psm_t* psmux;
00231     
00232     // serial baud rate
00233     u_long baud = 57600;
00234 
00235     // hardware init
00236     btn_hardware_init();
00237     btn_led_init(1);
00238     
00239     // init terminal app uart
00240     NutRegisterDevice(&APP_UART, 0, 0);
00241     freopen(APP_UART.dev_name, "r+", stdout);
00242     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00243         
00244     // init event logger
00245     log_init();
00246     
00247     // hello world!
00248     printf("\n# --------------------------------------------");
00249     printf("\n# Welcome to BTnut (c) 2006 ETH Zurich\n");
00250     printf("# program version: %s\n", PROGRAM_VERSION);
00251     printf("# --------------------------------------------");
00252     printf("\nbooting bluetooth module... ");
00253 
00254     // bluetooth module on (takes a while)
00255     btn_hardware_bt_on();
00256     printf("ok.\n\r");
00257     
00258     // Start bt-stack and let the initialization begin
00259     printf("init bt-stack... ");
00260     bt_stack = bt_hci_init(&BT_UART);
00261     printf("done.\n");
00262 
00263     // Initialize connection-less multi-hop layer
00264     // Set ACL packet types
00265     printf("setting acl pkt types... ");
00266     bt_acl_init(bt_stack, BT_HCI_PACKET_TYPE_DM3);
00267     printf("done.\n");
00268     
00269     // Init protocol/service multiplexor
00270     printf("init protcol/service mux... ");
00271     psmux = bt_psm_init(bt_stack, MAX_NR_SERVICES, 4);
00272     printf("done.\n");
00273     
00274     // Init connectionless l2cap stack
00275     printf("init connectionless l2cap... ");
00276     l2cap_cl_init(bt_stack, psmux);
00277     printf("done.\n");
00278     
00279     // Init connection manager
00280     printf("init connection manager... ");
00281     con_mgr_init(bt_stack, psmux, CM_PSM, bt_hci_register_con_table_cb, CM_COD);
00282     printf("done.\n");
00283     
00284     // Init connectionless multihop protocol
00285     printf("init connectionless multi-hop protocol... ");
00286     mhop_cl_init(bt_stack,
00287                     psmux,
00288                     MHOP_PSM,
00289                     6,
00290                     con_mgr_register_con_table_cb);
00291     printf("done.\n");
00292        
00293     // register the "blink" service at the service / protocol multiplexor
00294     mhop_blink_service_register(psmux, BLINK_PSM);
00295 
00296     // init terminal & give hint
00297     btn_terminal_init(stdout, "[mblink@btnode]$");
00298     printf("hit tab twice for a list of commands\n\r");
00299     
00300     // register "blink" command at terminal
00301     btn_terminal_register_cmd("blink", _mhop_blink_cmd_send_blink_rqst);
00302     
00303     // register "reply" command at terminal
00304     btn_terminal_register_cmd("reply", _mhop_blink_cmd_send_blink_reply);
00305         
00306     // terminal mode
00307     btn_terminal_run(BTN_TERMINAL_NOFORK, 0);
00308 
00309     return 0;
00310 }

Generated on Wed Apr 29 11:12:28 2009 for BTnut System Software by doxygen 1.5.1