BLE demo for mbed Ported RunningElectronics's SBDBT firmware for BLE. It can communicate with iOS

Dependencies:   FatFileSystem mbed

Fork of BTstack by Norimasa Okamoto

Revision:
3:4bc438c6fc52
Parent:
2:24b33d92e086
Child:
4:3e8cd77ee303
--- a/BLE_demo.cpp	Thu Feb 07 14:23:22 2013 +0000
+++ b/BLE_demo.cpp	Sun Feb 10 07:53:46 2013 +0000
@@ -43,11 +43,6 @@
 
 // NOTE: Supports only a single connection
 
-
-/*
- * BLE_demo
- */
-
 // mbed specific
 #include "mbed.h"
 Serial pc(USBTX, USBRX);
@@ -61,6 +56,7 @@
 #include "btstack/btstack.h"
 #include "btstack/hci_cmds.h"
 #include "btstack/run_loop.h"
+#include "btstack/hal_tick.h"
 
 #include "hci.h"
 #include "l2cap.h"
@@ -70,10 +66,6 @@
 
 #include "att.h"
 
-unsigned    timer_counter=0;
-uint8_t     startup_state=0;
-
-
 hci_transport_t * hci_transport_picusb_instance();
 
 static att_connection_t att_connection;
@@ -81,8 +73,6 @@
 static uint16_t         att_response_size   = 0;
 static uint8_t          att_response_buffer[28];
 
-uint8_t connection_status=0;
-
 void hexdump2(void *data, int size){
     int i;
     for (i=0; i<size;i++){
@@ -115,8 +105,9 @@
 // enable LE, setup ADV data
 static void packet_handler (void * connection, uint8_t packet_type, uint16_t channel, uint8_t *packet, uint16_t size){
     static bd_addr_t addr;
-//    uint8_t adv_data[] = { 02, 01, 05,   03, 02, 0xf0, 0xff }; 
-    const uint8_t adv_data[31]="\x02\x01\x05" "\x06\x09SBDBT";
+    //uint8_t adv_data[] = { 02, 01, 05, 03, 02, 0xf0, 0xff }; 
+    // Data for advertisement and scan response
+    const uint8_t adv_data[31]="\x02\x01\x05" "\x06\x09SBDBT" "\x03\x02\xf0\xff";
     switch (packet_type) {
             
         case HCI_EVENT_PACKET:
@@ -231,9 +222,10 @@
 
     if(buffer){
         log_info("READ Callback, handle %04x\n", handle);
-        led1 = 1;
+        led2 = 1;
     }
     switch(handle){
+        // Correspond to Characteristics 0xFFF1
        case 0x000b:
             ret=strlen((char*)strbuf);
             if(buffer && ret<=buffer_size){
@@ -241,6 +233,8 @@
                 memcpy(buffer,strbuf,ret);
             }
             break;
+
+        // Correspond to Characteristics 0xFFF2
         case 0x000d:
             if(buffer && buffer_size){
                 log_info("Read value: %u\n", buffer[0]);
@@ -248,6 +242,8 @@
             }
             ret=1;
             break;
+
+        // Correspond to Characteristics 0x00001234-0000-1000-8000-00805F9B34FB
         case 0x000f:
             if(buffer && buffer_size>=2){
                 val=timer_counter;
@@ -264,8 +260,9 @@
 // write requests
 static void att_write_callback(uint16_t handle, uint16_t transaction_mode, uint16_t offset, uint8_t *buffer, uint16_t buffer_size, signature_t * signature){
     log_info("WRITE Callback, handle %04x\n", handle);
-    led1 = 1;
+    led2 = 0;
     switch(handle){
+        // Correspond to Characteristics 0xFFF1
         case 0x000b:
             buffer[buffer_size]=0;
             if(sizeof(strbuf)>buffer_size){
@@ -273,6 +270,8 @@
             }
             log_info("New text: %s\n", buffer);
             break;
+        
+        // Correspond to Characteristics 0xFFF2
         case 0x000d:
             log_info("New value: %u\n", buffer[0]);
             ledvalue=buffer[0];
@@ -286,12 +285,16 @@
 }
 
 
-static void  heartbeat_handler(struct timer *ts){
-    run_loop_set_timer(ts, HEARTBEAT_PERIOD_MS);
-    run_loop_add_timer(ts);
-    led2 = !led2;
-} 
+void hal_tick_event(void)
+{
+    timer_counter++;
 
+    if(connection_status){
+        led3 = !led3;
+    }else{
+        led3 = 1;
+    }
+}
 
 // main
 int main(void)
@@ -305,6 +308,7 @@
    /// GET STARTED with BTstack ///
     btstack_memory_init();
     run_loop_init(RUN_LOOP_EMBEDDED);
+    hal_tick_set_handler(&hal_tick_event);
     
     // init HCI
     // use BlueUSB
@@ -328,12 +332,6 @@
     att_set_read_callback(att_read_callback);
     att_dump_attributes();
     att_connection.mtu = 27;
-
-    // set one-shot timer
-    timer_source_t heartbeat;
-    heartbeat.process = &heartbeat_handler;
-    run_loop_set_timer(&heartbeat, HEARTBEAT_PERIOD_MS);
-    run_loop_add_timer(&heartbeat);
     
     log_info("Run...\n\r");