BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Files at this revision

API Documentation at this revision

Comitter:
dgomes
Date:
Wed Aug 26 00:59:07 2015 +0000
Parent:
11:3de9b542eeac
Child:
13:bc1be947b497
Commit message:
All works :) ?

Changed in this revision

GaragemService.h Show annotated file Show diff for this revision Revisions of this file
SecurityService.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/GaragemService.h	Wed Aug 26 00:13:10 2015 +0000
+++ b/GaragemService.h	Wed Aug 26 00:59:07 2015 +0000
@@ -41,7 +41,6 @@
 
     }
 
-    
     int checkMessage(uint8_t *msg) {
         DBG("WHAT ? %s\r\n", (char *) msg);
 
--- a/SecurityService.h	Wed Aug 26 00:13:10 2015 +0000
+++ b/SecurityService.h	Wed Aug 26 00:59:07 2015 +0000
@@ -2,26 +2,28 @@
 #define __BLE_SECURITY_SERVICE_H__
 
 #include "Crypto.h"
+
+
 class SecurityService {
 public:
     const static uint16_t SECURITY_SERVICE_UUID              = 0x3000;
     const static uint16_t SECURITY_IV_CHARACTERISTIC_UUID = 0x3001;
     const static uint16_t SECURITY_KEY_CHARACTERISTIC_UUID = 0x3002;
     
-    SecurityService(BLE &_ble, char *shared_key) :
+    SecurityService(BLE &_ble) :
         ble(_ble), 
-        SecurityIV(SECURITY_IV_CHARACTERISTIC_UUID, (uint8_t *) 0),
-        SecurityKey(SECURITY_KEY_CHARACTERISTIC_UUID, (uint8_t *) 0)
+        SecurityIV(SECURITY_IV_CHARACTERISTIC_UUID, (uint8_t *) "IV"),
+        SecurityKey(SECURITY_KEY_CHARACTERISTIC_UUID, (uint8_t *) "KEY")
     {
-        boot = true;
         GattCharacteristic *charTable[] = {&SecurityIV, &SecurityKey};
         
         GattService         SecurityService(SECURITY_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
         ble.gattServer().addService(SecurityService);
-        
+    }
+    void init(char *shared_key) {
         //Initialize AES
         setKey(shared_key);
-        genIV();
+        genIV();    
     }
 
     void decode(uint8_t *out, uint8_t *in, uint32_t len) {
@@ -31,15 +33,9 @@
     }
     
     void setKey(char *shared_key) {
-        if(boot) {
-            DBG("Set shared_key = %s\r\n", shared_key);
-            MD5::computeHash(myKey, (uint8_t*) shared_key, strlen(shared_key));
-        }
-    }
-
-    void bootComplete() {
-        boot = false;
-    }    
+        DBG("Set shared_key = %s\r\n", shared_key);
+        MD5::computeHash(myKey, (uint8_t*) shared_key, strlen(shared_key));
+    } 
 
     void genIV() {
         //TODO RANDOM THIS:
@@ -51,17 +47,14 @@
     GattAttribute::Handle_t getKeyHandle() const {
         return SecurityKey.getValueHandle();
     }
+    
+private:
+    uint8_t iv[16];
+    uint8_t myKey[16];
 
-private:
     BLE   &ble;
     ReadOnlyArrayGattCharacteristic<uint8_t, 16> SecurityIV;
-    WriteOnlyArrayGattCharacteristic<uint8_t, 16> SecurityKey;
-    bool boot;
-
-    uint8_t iv[16];
-    //openssl enc -aes-128-cbc -pass pass:********** -nosalt -P
-    uint8_t myKey[16];
-
+    WriteOnlyArrayGattCharacteristic<uint8_t, 16> SecurityKey;    
 };
 
 #endif /* #ifndef __BLE_SECURITY_SERVICE_H__ */
--- a/main.cpp	Wed Aug 26 00:13:10 2015 +0000
+++ b/main.cpp	Wed Aug 26 00:59:07 2015 +0000
@@ -23,13 +23,16 @@
 // I -> 4 bytes ID (chars)
 // S -> 8 bytes SHARED_SECRET   (chars)
 
+// Generating K and iv:
+//openssl enc -aes-128-cbc -pass pass:********** -nosalt -P
+
 #define SHARED_KEY  "figueiredo"
 #define DEVICE_NAME    "Garagem"
 
 BLE        ble;
 DigitalOut actuatedLED(P0_19);
 Timeout offRelay;
-
+bool boot;
 #ifndef DEBUG
 DigitalOut relay(P0_8);
 #endif
@@ -76,16 +79,17 @@
         } else {
             DBG("NO SUCH LUCK...\r\n");
         }
-    } if((params->handle == securityServicePtr->getKeyHandle()) && (params->len > 0)) {
+    } if(boot && (params->handle == securityServicePtr->getKeyHandle()) && (params->len > 0)) {
         securityServicePtr->setKey((char *) params->data);    
     }
-    securityServicePtr->bootComplete();
+    boot = false;
 }
 
 int main(void)
 {
     DBG("Garagem v0\r\n");
 
+    boot = true;
     actuatedLED  = true;
     #ifndef DEBUG
     relay = 0;
@@ -109,15 +113,16 @@
     uint16_t minorNumber = 1423;
     uint16_t txPower     = 0xBE;
     iBeacon ibeacon(ble, uuid, majorNumber, minorNumber, txPower);
+
+    DeviceInformationService deviceInfo(ble, "diogogomes.com", DEVICE_NAME, "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
     
     GaragemService garagemService(ble);
     garagemServicePtr = &garagemService;
     
-    SecurityService securityService(ble, SHARED_KEY);
+    SecurityService securityService(ble);
+    securityService.init(SHARED_KEY);
     securityServicePtr = &securityService;
 
-    DeviceInformationService deviceInfo(ble, "diogogomes.com", DEVICE_NAME, "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
-
     /* setup advertising */
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));