BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Revision:
11:3de9b542eeac
Parent:
10:80850cd6c29e
Child:
12:eaee29bfa1c7
--- a/main.cpp	Tue Aug 25 23:26:43 2015 +0000
+++ b/main.cpp	Wed Aug 26 00:13:10 2015 +0000
@@ -1,18 +1,3 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2006-2013 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
 #define DEBUG
 
 #ifndef DEBUG
@@ -25,13 +10,13 @@
 #include "mbed.h"
 #include "BLE.h"
 #include "GaragemService.h"
+#include "SecurityService.h"
 #include "ble/services/iBeacon.h"
 #include "DeviceInformationService.h"
 
-#include "Crypto.h"
 
-//how-to test:
-//create a string to write to the characteristic
+// HOW-TO TEST:
+//create a string to write to the characteristic 2000:2001
 //openssl enc -aes-128-cbc -K 9734062BA852A049CF5D40593B769014 -iv A2685636521871D02306E2EB8F7027B3 -out /dev/stdout -in inputfile
 // TTTTIIIISSSSSSSS
 // T -> 4 bytes Timestamp uint32_t
@@ -40,9 +25,6 @@
 
 #define SHARED_KEY  "figueiredo"
 #define DEVICE_NAME    "Garagem"
-//openssl enc -aes-128-cbc -pass pass:********** -nosalt -P
-uint8_t myKey[16];
-uint8_t iv[16] = { 0xA2, 0x68, 0x56, 0x36, 0x52, 0x18, 0x71, 0xD0, 0x23, 0x06, 0xE2, 0xEB, 0x8F, 0x70, 0x27, 0xB3 };
 
 BLE        ble;
 DigitalOut actuatedLED(P0_19);
@@ -52,9 +34,10 @@
 DigitalOut relay(P0_8);
 #endif
 
-static const uint16_t uuid16_list[] = {GaragemService::GARAGEM_SERVICE_UUID};
+static const uint16_t uuid16_list[] = {GaragemService::GARAGEM_SERVICE_UUID, SecurityService::SECURITY_SERVICE_UUID};
 
 GaragemService *garagemServicePtr;
+SecurityService *securityServicePtr;
 
 void switchOffRelay()
 {
@@ -69,12 +52,7 @@
 {   
     ble.startAdvertising(); 
 }
-/**
- * This callback allows the LEDService to receive updates to the ledState Characteristic.
- *
- * @param[in] params
- *     Information about the characterisitc being updated.
- */
+
 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
     DBG("onDataWrittenCallback: handle = %d      len = %d\r\n", params->handle, params->len);
     garagemServicePtr->nextLastOpen(params->data, params->len);
@@ -82,9 +60,8 @@
     if ((params->handle == garagemServicePtr->getChallengeHandle()) && (params->len ==16)) {
         DBG("We have Challenge: data = %s\r\n", (char *) (params->data));
         
-        AES myAES(AES_128, myKey, iv);
         uint8_t msg[16];
-        myAES.decrypt(msg,(uint8_t *) (params->data),16);
+        securityServicePtr->decode(msg, (uint8_t *) params->data, 16);
         
         if(garagemServicePtr->checkMessage(msg) == GARAGEM_OK) {  
             DBG("ABRE-TE SESAMO!\r\n");
@@ -99,16 +76,16 @@
         } else {
             DBG("NO SUCH LUCK...\r\n");
         }
-    } 
+    } if((params->handle == securityServicePtr->getKeyHandle()) && (params->len > 0)) {
+        securityServicePtr->setKey((char *) params->data);    
+    }
+    securityServicePtr->bootComplete();
 }
 
 int main(void)
 {
     DBG("Garagem v0\r\n");
 
-    //Compute myKey from SHARED_KEY
-    MD5::computeHash(myKey, (uint8_t*) SHARED_KEY, strlen(SHARED_KEY));
-    
     actuatedLED  = true;
     #ifndef DEBUG
     relay = 0;
@@ -135,6 +112,9 @@
     
     GaragemService garagemService(ble);
     garagemServicePtr = &garagemService;
+    
+    SecurityService securityService(ble, SHARED_KEY);
+    securityServicePtr = &securityService;
 
     DeviceInformationService deviceInfo(ble, "diogogomes.com", DEVICE_NAME, "SN1", "hw-rev1", "fw-rev1", "soft-rev1");