BLE mbed Endpoint network stack for mbedConnectorInterface. The stack makes use of a special BLE Socket abstraction to create socket() semantics over BLE.

Dependencies:   libnsdl_m0 BLE_API Base64 nRF51822 SplitterAssembler

Files at this revision

API Documentation at this revision

Comitter:
ansond
Date:
Tue Nov 03 17:03:51 2015 +0000
Parent:
37:64cab752e428
Parent:
36:aa73681951ad
Commit message:
merged with IoS and android tweaks

Changed in this revision

diff -r 64cab752e428 -r 30e71f1206b1 bt_network/BleUartRPC/Dispatcher.cpp
--- a/bt_network/BleUartRPC/Dispatcher.cpp	Tue Nov 03 17:02:34 2015 +0000
+++ b/bt_network/BleUartRPC/Dispatcher.cpp	Tue Nov 03 17:03:51 2015 +0000
@@ -109,12 +109,6 @@
  }
  
  int Dispatcher::uart_write(uint8_t *data,int data_length) {
- // 9/4/15: Android 5.x appears to have some sort of poor-mans DDoS detector in its BLE stack so we have to slow things down a bit.. 
- #ifdef ANDROID_BLE_DELAY
-     wait_ms(ANDROID_BLE_DELAY_MS);
- #endif
-     
-     // OK... now write to UART... 
      return this->m_uart.write(data,data_length);
  }
  
diff -r 64cab752e428 -r 30e71f1206b1 bt_network/BleUartRPC/Dispatcher.h
--- a/bt_network/BleUartRPC/Dispatcher.h	Tue Nov 03 17:02:34 2015 +0000
+++ b/bt_network/BleUartRPC/Dispatcher.h	Tue Nov 03 17:03:51 2015 +0000
@@ -34,11 +34,7 @@
  
  // This is aligned with the value of DEF_FRAGMENT_LENGTH in SplitterAssembler.h and is also the max UART MTU for BLE
  #define UART_SEGMENT_LENGTH        20             // BLE: max UART MTU (will trigger send immediately...)
- 
- // 9/4/15: Android 5.x appears to have some sort of poor-mans DDoS detector in its BLE stack so we have to slow things down a bit.. 
- #define ANDROID_BLE_DELAY          true           // true to enable slow down, false to disable 
- #define ANDROID_BLE_DELAY_MS       500            // delay time (in ms), if enabled, to allow android 5.x to catch up... 
- 
+  
  class Dispatcher {
      public:
         /**
diff -r 64cab752e428 -r 30e71f1206b1 bt_network/BleUartRPC/UartRPC.cpp
--- a/bt_network/BleUartRPC/UartRPC.cpp	Tue Nov 03 17:02:34 2015 +0000
+++ b/bt_network/BleUartRPC/UartRPC.cpp	Tue Nov 03 17:03:51 2015 +0000
@@ -100,7 +100,7 @@
          case SOCKET_OPEN_FN: {
              // socket open has been attempted... call the CB if it exists and process the status
              //DBG("UartRPC: dispatch(local): SOCKET_OPEN_FN length=%d\r\n",base64_buffer_length);
-             if (this->m_cb != NULL) {
+             if (this->m_cb != NULL) { 
                  // cast the callback
                  ble_dispatch_callback_fn cb = (ble_dispatch_callback_fn)this->m_cb;
                  
diff -r 64cab752e428 -r 30e71f1206b1 bt_network/BleUartRPC/UartRPC.h
--- a/bt_network/BleUartRPC/UartRPC.h	Tue Nov 03 17:02:34 2015 +0000
+++ b/bt_network/BleUartRPC/UartRPC.h	Tue Nov 03 17:03:51 2015 +0000
@@ -45,7 +45,7 @@
         bool              localDispatchAvailable(void);
         int               retrieveLocalDispatch(uint8_t *buffer,int buffer_length);
         void              setLocationInstance(BLELocation *location);
-   
+        
     private:       
         void              resetLocalDispatch(void);
         
diff -r 64cab752e428 -r 30e71f1206b1 bt_network/BleUartRPC/UartRPCFunctions.cpp
--- a/bt_network/BleUartRPC/UartRPCFunctions.cpp	Tue Nov 03 17:02:34 2015 +0000
+++ b/bt_network/BleUartRPC/UartRPCFunctions.cpp	Tue Nov 03 17:03:51 2015 +0000
@@ -31,7 +31,7 @@
  #endif
  #define DBG  std::printf
  
- UartRPC *__rpc = NULL;
+ static UartRPC *__rpc = NULL;
  
  void ble_rpc_init(BLEDevice &ble) 
  {
@@ -40,32 +40,36 @@
  
  bool ble_rpc_open_udp_socket(char *ip_address,int port,ble_dispatch_callback_fn cb) 
  {
+     bool status = false;
      uint8_t response[2];
      memset(response,0,2);
+     
      //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatching...\r\n");
      if (__rpc->dispatch(SOCKET_OPEN_FN,(void *)cb,response,2,"%s %d",ip_address,port) > 0) {
          //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatched SUCCESSFULLY\r\n");
-         return true;
+         status = true;
      }
      
      // failure
      //DBG("ble_rpc_open_udp_socket: UDP Socket open() dispatch failed\r\n");
-     return false;
+     return status;
  }
  
  bool ble_rpc_close_udp_socket(void) 
  {
+     bool status = false;
      uint8_t response[2];
      memset(response,0,2);
+     
      //DBG("ble_rpc_open_udp_socket: UDP Socket close() dispatched successfully. Waiting on response...\r\n");
      if (__rpc->dispatch(SOCKET_CLOSE_FN,NULL,response,2,"%s","loc") > 0) {
          //DBG("ble_rpc_close_udp_socket: UDP Socket close() dispatched SUCCESSFULLY\r\n");
-         return true;
+         status = true;
      }
      
      // failure
      //DBG("ble_rpc_close_udp_socket: UDP Socket close() dispatch failed\r\n");
-     return false;
+     return status;
  }
  
  int ble_rpc_send_data(uint8_t *data,int data_length) 
@@ -92,13 +96,14 @@
  }
  
  int ble_rpc_recv_packet(uint8_t *buffer,int buffer_length)
- {
+ {  
+     int n = 0;
      //DBG("ble_rpc_recv_packet: checking for a local dispatch...\r\n");
      if (__rpc->localDispatchAvailable()) { 
         //DBG("ble_rpc_recv_packet: local dispatch is ready. Retrieving...\r\n");
-        return __rpc->retrieveLocalDispatch(buffer,buffer_length);
+        n =  __rpc->retrieveLocalDispatch(buffer,buffer_length);
      }
-     return 0;
+     return n;
  }
  
  int ble_rpc_recv_data(uint8_t *buffer,int buffer_length)
diff -r 64cab752e428 -r 30e71f1206b1 bt_network/Location/BLELocation.cpp
--- a/bt_network/Location/BLELocation.cpp	Tue Nov 03 17:02:34 2015 +0000
+++ b/bt_network/Location/BLELocation.cpp	Tue Nov 03 17:03:51 2015 +0000
@@ -44,30 +44,53 @@
     ble_rpc_get_location(this);
 }
 
+// set the default location
+void BLELocation::setDefault(float latitude,float longitude,float altitude,float speed) {
+    sprintf(this->m_latitude,"%.5f",latitude);
+    sprintf(this->m_longitude,"%.5f",longitude);
+    sprintf(this->m_msl_altitude_m,"%.1f",altitude);
+    sprintf(this->m_speed,"%.1f",speed);
+}
+
 // process the location update response
 void BLELocation::processLocationUpdateResponse(char *response,int response_length) {
-    if (response_length > 0) {
+    float latitude = 0.0;
+    float longitude = 0.0;
+    float altitude = 0.0;
+    float speed = 0.0;
+    bool present = false;
+            
+    if (response != NULL && response_length > 0) {
         //DBG("BLELocation:processLocationUpdateResponse: data(%d): %s\r\n",response_length,response);
         for(int i=0;i<response_length;++i) {
-            if (response[i] == ':') response[i] = ' ';
+            if (response[i] == ':') {
+                response[i] = ' ';
+                present = true;
+            }
         }
-        float latitude = 0.0;
-        float longitude=0.0;
-        float altitude=0.0;
-        float speed = 0.0;
+        
+        // fill with defaults if not presented with new ones...
+        if (!present) {
+            sprintf(response,"%s %s %s %s",this->m_latitude,this->m_longitude,this->m_msl_altitude_m,this->m_speed);
+        }
+        
+        // parse
         sscanf((char *)response,"%f%f%f%f",&latitude,&longitude,&altitude,&speed);
-        sprintf(this->m_latitude,"%.5f",latitude);
-        sprintf(this->m_longitude,"%.5f",longitude);
-        sprintf(this->m_msl_altitude_m,"%.1f",altitude);
-        sprintf(this->m_speed,"%.1f",speed);
+        this->setDefault(latitude,longitude,altitude,speed);
         
         // check for disabled location 
         if (latitude < 1.0 && longitude < 1.0 && speed < 0.0 && altitude < 0) {
             DBG("BLELocation: Location reporting is DISABLED in UART_PROXY...\r\n");
             ::Connector::Location::initBuffers();
         }
+        else if (!present) {
+            // using cached values
+            DBG("BLELocation (CACHED): latitude=%s longtiude=%s altitude=%s speed=%s\r\n",
+                this->m_latitude,this->m_longitude,this->m_msl_altitude_m,this->m_speed);
+        }
         else {
-            DBG("BLELocation: latitude=%s longtiude=%s altitude=%s speed=%s\r\n",
+            // latest values
+            DBG("BLELocation (CURRENT): latitude=%s longtiude=%s altitude=%s speed=%s\r\n",
                 this->m_latitude,this->m_longitude,this->m_msl_altitude_m,this->m_speed);
         }
     }
diff -r 64cab752e428 -r 30e71f1206b1 bt_network/Location/BLELocation.h
--- a/bt_network/Location/BLELocation.h	Tue Nov 03 17:02:34 2015 +0000
+++ b/bt_network/Location/BLELocation.h	Tue Nov 03 17:03:51 2015 +0000
@@ -53,6 +53,11 @@
         */
         void processLocationUpdateResponse(char *response,int response_length);
         
+        /**
+        Set the default location
+        */
+        void setDefault(float latitude,float longitude,float altitude,float speed);
+        
     private:
         void dispatchUpdateRequest();
 };
diff -r 64cab752e428 -r 30e71f1206b1 network_stubs/network_stubs.cpp
--- a/network_stubs/network_stubs.cpp	Tue Nov 03 17:02:34 2015 +0000
+++ b/network_stubs/network_stubs.cpp	Tue Nov 03 17:03:51 2015 +0000
@@ -84,6 +84,12 @@
 #endif
 {
     DBG("ble_connect_callback: BLE connected!...\r\n");    
+    
+    // 9/4/15: Android 5.x appears to have some sort of poor-mans DDoS detector in its BLE stack so we have to slow things down a bit.. 
+ #ifdef ANDROID_BLE_DELAY
+    wait_ms(ANDROID_BLE_DELAY_MS);
+ #endif
+ 
     if (__registered == false) {
         DBG("ble_connect_callback: Opening remote UDP socket to: %s@%d...\r\n",_ipAddress,_ipPort);
         ble_rpc_open_udp_socket(_ipAddress,_ipPort,&ble_rpc_completed_open_udp_socket);   
diff -r 64cab752e428 -r 30e71f1206b1 network_stubs/network_stubs.h
--- a/network_stubs/network_stubs.h	Tue Nov 03 17:02:34 2015 +0000
+++ b/network_stubs/network_stubs.h	Tue Nov 03 17:03:51 2015 +0000
@@ -28,6 +28,10 @@
 // NSP support
 #include "nsdl_support.h"
 
+// 9/4/15: Android 5.x appears to have some sort of poor-mans DDoS detector in its BLE stack so we have to slow things down a bit.. 
+#define ANDROID_BLE_DELAY          true           // true to enable slow down, comment out to disable 
+#define ANDROID_BLE_DELAY_MS       200            // delay time (in ms), if enabled, to allow android 5.x to catch up... 
+
 // configure the endpoint
 extern void configure_endpoint();
 
@@ -47,7 +51,6 @@
     uint32_t tv_usec;
 };
 
-
 // socket length is simply an unsigned int.
 typedef uint32_t socklen_t;