BLE Central for Magic Light

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Central_Light by HM_IOT_Demo

Revision:
5:f6b6bf98c686
Parent:
3:d6f80e11a7f4
Child:
6:71e7a446ae6a
diff -r 50f318713140 -r f6b6bf98c686 main.cpp
--- a/main.cpp	Thu Jan 12 06:43:10 2017 +0000
+++ b/main.cpp	Thu Jan 19 12:21:34 2017 +0000
@@ -9,45 +9,80 @@
 BLE ble;
 Serial pc(USBTX, USBRX);
 
+/*const uint8_t MPU6050_service_uuid[] = {
+    0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
+};*/
 const uint8_t MPU6050_service_uuid[] = {
-    0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
+    0x00,0x00,0xFF,0xE5,0x00,0x00,0x10,0x00,0x80,0x00,0x00,0x80,0x5F,0x9B,0x34,0xFB
+};
+/*const uint8_t MPU6050_Accel_Characteristic_uuid[] = {
+    0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
+};*/
+
+const uint8_t MPU6050_Accel_Characteristic_uuid[] = {
+    0x00,0x00,0xFF,0xE6,0x00,0x00,0x10,0x00,0x80,0x00,0x00,0x80,0x5F,0x9B,0x34,0xFB
+};
+#define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
+
+enum
+{
+    UNIT_0_625_MS = 625,                                
+    UNIT_1_25_MS  = 1250,                               
+    UNIT_10_MS    = 10000                               
 };
 
-const uint8_t MPU6050_Accel_Characteristic_uuid[] = {
-    0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
-};
+#define NUMBER_OF_PERIPHERALS 3
 
+#define MIN_CONNECTION_INTERVAL   MSEC_TO_UNITS(7.5, UNIT_1_25_MS)          
+#define MAX_CONNECTION_INTERVAL   MSEC_TO_UNITS(30, UNIT_1_25_MS)           
+#define SLAVE_LATENCY             0                                         
+#define SUPERVISION_TIMEOUT       MSEC_TO_UNITS(4000, UNIT_10_MS) 
+
+//const char Dev_Mac_Addr[15]="5065838DA351";
 DiscoveredCharacteristic accelChar;
 UUID serviceUUID(MPU6050_service_uuid);
 UUID accelUUID(MPU6050_Accel_Characteristic_uuid);
 
-#define NUMBER_OF_PERIPHERALS 3
 
+static const Gap::ConnectionParams_t m_connection_param =
+{
+    (uint16_t)MIN_CONNECTION_INTERVAL,
+    (uint16_t)MAX_CONNECTION_INTERVAL,
+    (uint16_t)SLAVE_LATENCY,
+    (uint16_t)SUPERVISION_TIMEOUT
+};
 typedef struct {
     Gap::Handle_t   handle;
     Gap::Address_t  address;
     bool    connected;
     uint8_t*    deviceName;
 } peripheral_t;
+static const char adv_name[] = "LEDBlue-838DA351";
+static peripheral_t gs_peripheral[NUMBER_OF_PERIPHERALS];
 
-static peripheral_t gs_peripheral[NUMBER_OF_PERIPHERALS];
 
 uint32_t ble_advdata_parser(uint8_t type, uint8_t advdata_len, uint8_t *p_advdata, uint8_t *len, uint8_t *p_field_data)
 {
     uint8_t index=0;
     uint8_t field_length, field_type;
-    
+    pc.printf("advdata_parser1\n\r");
+    pc.printf("type : %d Length : %d\n\r",type, advdata_len);
     while(index<advdata_len)
-    {
+    {pc.printf("advdata_parser loop\n\r");
         field_length = p_advdata[index];
         field_type   = p_advdata[index+1];
+        pc.printf("FieldLen : %d\n\r",p_advdata[index]);
+        pc.printf("Field_Type : %d\n\r",p_advdata[index+1]);
+        pc.printf("Fieldtype : %d\n\r",field_type);
         if(field_type == type)
         {
-            memcpy(p_field_data, &p_advdata[index+2], (field_length-1));
+            pc.printf("Copy the data\n\r");
+            //memcpy(p_field_data, &p_advdata[index+2], (field_length-1));
+            p_field_data = &p_advdata[index+2];
             *len = field_length - 1;
             return NRF_SUCCESS;
         }
-        index += field_length + 1;
+        index += field_length;
     }
     return NRF_ERROR_NOT_FOUND;
 }
@@ -56,23 +91,40 @@
     pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
            params->rssi, params->isScanResponse, params->type);
-    
+           //pc.printf("params->peerAddr : %s \n\r",params->peerAddr);
+      //  char peer_Addr[15];
+         
+   // pc.printf("params->advertisingData : %02x %02x\n\r",params->advertisingData[0],params->advertisingData[1]);
+    pc.printf("Data Lenght = %d\n\r",params->advertisingDataLen);
     uint8_t len;
-    uint8_t adv_name[31];
-    if( NRF_SUCCESS == ble_advdata_parser(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
+    
+    uint8_t status;
+    uint8_t stop_status;
+    pc.printf("Connected1\n\r");
+   /* if( NRF_SUCCESS == ble_advdata_parser(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
                              params->advertisingDataLen,
-                            (uint8_t *)params->advertisingData, &len, adv_name)){
-
-        for(uint8_t i=0; i<3; i++){
-            if(gs_peripheral[i].connected == false){
+                        (uint8_t *)params->advertisingData, &len, adv_name))
+   {*/
+    pc.printf("Connected2\n\r");
+    uint8_t i=0;
+       // for(uint8_t i=0; i<3; i++){
+            //if(gs_peripheral[i].connected == false)
+            if (params->peerAddr[0] == 0x51){
+                pc.printf("Connected3\n\r");
                 memcpy(gs_peripheral[i].address, params->peerAddr, sizeof(params->peerAddr)); 
-                gs_peripheral[i].deviceName = adv_name;
-                ble.connect(params->peerAddr, BLEProtocol::AddressType::RANDOM_STATIC, NULL, NULL);
-                break;
+                gs_peripheral[i].deviceName = (uint8_t*)adv_name;
+                ble.gap().setDeviceName(gs_peripheral[i].deviceName);
+                status = ble.gap().connect(params->peerAddr, BLEProtocol::AddressType::RANDOM_STATIC, &m_connection_param, NULL);
+                pc.printf("Connection Status = %d\n\r",status);
+                pc.printf("stop scan\n\r");
+                stop_status = ble.gap().stopScan();  
+                pc.printf("stop Status = %d\n\r",stop_status);           
+               // break;
             }
-        }
+       // }
+       
         ble.stopScan();
-    }
+   // }
 }
 
 void serviceDiscoveryCallback(const DiscoveredService *service) {
@@ -136,7 +188,7 @@
         }
     }
     wait(8.0);
-    ble.gap().startScan(scanCallback);
+   // ble.gap().startScan(scanCallback);
 }
 
 int main(void) {
@@ -148,13 +200,13 @@
 
     ble.onConnection(connectionCallback);
     ble.onDisconnection(disconnectionCallback);
-    ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
-    ble.gattClient().onDataRead(triggerRead);
+   // ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
+   // ble.gattClient().onDataRead(triggerRead);
     //ble.gattClient().onDataWrite(triggerToggledWrite);
-
-    ble.gap().setScanParams(500, 400);
+    
+    ble.gap().setScanParams(600, 400);
     ble.gap().startScan(scanCallback);
-
+    //ble.startAdvertising();
     while (true) {
         ble.waitForEvent();
     }