Example for BLE HID scanner

Fork of BLE_HIDScanner_DELTA by Silvia Chen

Revision:
4:fd7572166cbb
Parent:
3:9d6e0ddf3fbe
Child:
5:8f0f8b8a85eb
--- a/main.cpp	Mon Jul 24 02:55:38 2017 +0000
+++ b/main.cpp	Wed Jul 26 03:50:46 2017 +0000
@@ -19,19 +19,30 @@
 #include "ble/services/BatteryService.h"
 #include "ble/services/DeviceInformationService.h"
 #include "HIDService.h"
+#include "ble/services/UARTService.h"
 
 //DigitalOut led1(LED1);
 Serial uart(USBTX, USBRX);
+InterruptIn testHID_btn(BUTTON1);
 
 HIDService *hidService;
 unsigned char keyData;
 const static char     DEVICE_NAME[]        = "HID_Keyboard";
 static const uint16_t uuid16_list[]        = {GattService::UUID_HUMAN_INTERFACE_DEVICE_SERVICE,
+                                              GattService::UUID_BATTERY_SERVICE,
                                               GattService::UUID_DEVICE_INFORMATION_SERVICE};
 static volatile bool  triggerSensorPolling = false;
 bool isConnectionSecured = false;
+bool isSecuritySetup = false;
+unsigned char serial_buf[20];
+uint16_t serialLen = 0;
+static uint8_t key_press_scan_buff[50];
+static uint8_t modifyKey[50];
+int index_b = 0;
 
 DeviceInformationService *deviceInfo;
+BatteryService *batteryService;
+static UARTService *uartServicePtr;
 
 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
 {
@@ -41,6 +52,12 @@
     }
     uart.printf("\r\n");
 }
+
+void securitySetupInitiatedCallback(Gap::Handle_t, bool allowBonding, bool requireMITM, SecurityManager::SecurityIOCapabilities_t iocaps)
+{
+    uart.printf("securitySetupInitiatedCallback\r\n");
+    isSecuritySetup = true;
+}
  
 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
 {
@@ -55,7 +72,15 @@
 void linkSecuredCallback(Gap::Handle_t handle, SecurityManager::SecurityMode_t securityMode)
 {
     uart.printf("linkSecuredCallback\r\n");
+    if (!isSecuritySetup) {
+        isConnectionSecured = true;
+    }
+}
+
+void securityContextStoredCallback(Gap::Handle_t handle) {
+    uart.printf("securityContextStoredCallback\r\n");
     isConnectionSecured = true;
+    isSecuritySetup = false;
 }
 
 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params)
@@ -88,13 +113,68 @@
     }
 }
 
+void serverDataWrittenCallback(const GattWriteCallbackParams *response) {
+    uart.printf("serverDataWrittenCallback\r\n");
+    if (response->handle == uartServicePtr->getTXCharacteristicHandle()) {
+        serialLen = response->len;
+        for(int j=0;j<response->len;j++) {
+            //uart.printf("data: %02X\r\n", response->data[j]);
+            serial_buf[j] = response->data[j];
+        }
+    }
+}
+
 void periodicCallback(void)
 {
     //led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
 
     /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
      * heavy-weight sensor polling from the main thread. */
-    triggerSensorPolling = true;
+    //triggerSensorPolling = true;
+}
+
+void testHIDRiseInterrupt() {
+    triggerSensorPolling = true; 
+}
+
+void transferReportData(unsigned char keyData) {
+    if(keyData <= 0x39 && keyData >= 0x30){             //number
+        if(keyData == 0x30){
+            modifyKey[index_b] = 0x00;
+            key_press_scan_buff[index_b] = 0x27;
+            index_b++;
+            key_press_scan_buff[index_b] = 0x73;
+        } else {
+            modifyKey[index_b] = 0x00;
+            key_press_scan_buff[index_b] = keyData-0x13;
+            index_b++;
+            key_press_scan_buff[index_b] = 0x73;
+            }
+    } else if(keyData <= 0x7a && keyData >= 0x61 ){ //lowercase letters
+        modifyKey[index_b] = 0x00;
+        key_press_scan_buff[index_b] = keyData-0x5d;
+        index_b++;
+        key_press_scan_buff[index_b] = 0x73;
+    } else if(keyData <= 0x5a && keyData >= 0x41){  //uppercase letters
+        modifyKey[index_b] = 0x02;
+        key_press_scan_buff[index_b] = keyData-0x3d;
+        index_b++;
+        key_press_scan_buff[index_b] = 0x73;
+    } else if (keyData == 0x20) {                   //space
+        modifyKey[index_b] = 0x00;
+        key_press_scan_buff[index_b] = 0x2c;
+        index_b++;
+        key_press_scan_buff[index_b] = 0x73;
+    } else {
+        modifyKey[index_b] = 0x00;
+        //key_press_scan_buff[index_b] = 0x73;          //this is dummy data.
+        //msg[index_w+1] = '\0';
+    }
+    index_b++;
+}
+
+void dataSentCallback(const unsigned callback) {
+    //uart.printf("dataSentCallback\r\n");
 }
 
 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
@@ -109,20 +189,26 @@
     bool enableBonding = true;
     bool requireMITM   = false;
 
-    //const uint8_t passkeyValue[6] = {0x00,0x00,0x00,0x00,0x00,0x00};
+    //uint8_t pKey[6] = {'1', '1', '1', '1', '1', '1'}; //set the passkey
     ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_NONE);
     ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
+    ble.securityManager().onSecuritySetupInitiated(securitySetupInitiatedCallback);
     ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
     ble.securityManager().onLinkSecured(linkSecuredCallback);
+    ble.securityManager().onSecurityContextStored(securityContextStoredCallback);
     ble.gap().onDisconnection(disconnectionCallback);
     ble.gap().onConnection(onConnectionCallback);
     ble.gap().onTimeout(onTimeoutCallback);
     //ble.gattServer().onDataRead(onDataReadCallback);
+    ble.gattServer().onDataWritten(serverDataWrittenCallback);
+    ble.gattServer().onDataSent(dataSentCallback);
 
     /* Setup primary service. */
     hidService = new HIDService(ble);
 
     /* Setup auxiliary service. */
+    uartServicePtr = new UARTService(ble);
+    batteryService = new BatteryService(ble, 100);
     deviceInfo = new DeviceInformationService(ble, "DELTA", "NQ620", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
 
     /* Setup advertising. */
@@ -136,17 +222,13 @@
     uart.printf("Start advertising\r\n");
 }
 
-static uint8_t key_press_scan_buff[50];
-static uint8_t modifyKey[50];
-char msg[25] = "";
-int index_b = 0;
-int index_w = 0;
-
 int main(void)
 {
     //led1 = 1;
    // Ticker ticker;
 //    ticker.attach(periodicCallback, 0.1); // blink LED every second
+
+    testHID_btn.rise(&testHIDRiseInterrupt);
     
     uart.baud(115200);
     uart.printf("Srarting HID Service\r\n");
@@ -163,55 +245,42 @@
         if (isConnectionSecured) {
             if (uart.readable() == 1) {
                 keyData = uart.getc();
-                uart.putc(keyData);
-                msg[index_w++] = keyData;
-                if(keyData <= 0x39 && keyData >= 0x30){             //number
-                    if(keyData == 0x30){
-                        modifyKey[index_b] = 0x00;
-                        key_press_scan_buff[index_b] = 0x27;
-                        index_b++;
-                        key_press_scan_buff[index_b] = 0x73;
-                    } else {
-                        modifyKey[index_b] = 0x00;
-                        key_press_scan_buff[index_b] = keyData-0x13;
-                        index_b++;
-                        key_press_scan_buff[index_b] = 0x73;
-                        }
-                } else if(keyData <= 0x7a && keyData >= 0x61 ){ //lowercase letters
-                    modifyKey[index_b] = 0x00;
-                    key_press_scan_buff[index_b] = keyData-0x5d;
-                    index_b++;
-                    key_press_scan_buff[index_b] = 0x73;
-                } else if(keyData <= 0x5a && keyData >= 0x41){  //uppercase letters
-                    modifyKey[index_b] = 0x02;
-                    key_press_scan_buff[index_b] = keyData-0x3d;
-                    index_b++;
-                    key_press_scan_buff[index_b] = 0x73;
-                } else if (keyData == 0x20) {                   //space
-                    modifyKey[index_b] = 0x00;
-                    key_press_scan_buff[index_b] = 0x2c;
-                    index_b++;
-                    key_press_scan_buff[index_b] = 0x73;
-                } else {
-                    modifyKey[index_b] = 0x00;
-                    //key_press_scan_buff[index_b] = 0x73;          //this is dummy data.
-                    //msg[index_w+1] = '\0';
-                }
-                index_b++;
+                //uart.printf("keyData: %c\r\n", keyData);
+                transferReportData(keyData);
                 if(keyData == 0x0a && ble.getGapState().connected){
                     for(int i = 0; i < index_b ; i++){
-                        uart.printf("m[%x] k[%x] ", modifyKey[i], key_press_scan_buff[i]);
+                        //uart.printf("m[%x] k[%x] ", modifyKey[i], key_press_scan_buff[i]);
                         hidService->updateReport(modifyKey[i], key_press_scan_buff[i]);
                         ble.waitForEvent();
                         wait(0.01);
                     }
             
                     index_b = 0;
-                    index_w = 0;
                     memset(modifyKey, 0, 50);
-                    memset(msg, 0, 25);
                     memset(key_press_scan_buff, 0, 50);
                 }
+            } else if(triggerSensorPolling) {
+                //Demo HID/Serial at the same time
+                triggerSensorPolling = false;
+                
+                for(int j=0;j<serialLen;j++) {
+                    keyData = serial_buf[j];
+                    //uart.printf("keyData: %c\r\n", keyData);
+                    transferReportData(keyData);
+                }
+                
+                for(int i = 0; i < index_b ; i++){
+                    //uart.printf("m[%x] k[%x] ", modifyKey[i], key_press_scan_buff[i]);
+                    hidService->updateReport(modifyKey[i], key_press_scan_buff[i]);
+                    ble.waitForEvent();
+                    wait(0.01);
+                }
+            
+                index_b = 0;
+                memset(modifyKey, 0, 50);
+                memset(key_press_scan_buff, 0, 50);
+            } else {
+                ble.waitForEvent();
             }
         } else {
             ble.waitForEvent(); // low power wait for event