BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Revision:
10:80850cd6c29e
Parent:
9:329af8cdc923
Child:
12:eaee29bfa1c7
--- a/GaragemService.h	Tue Aug 25 22:18:21 2015 +0000
+++ b/GaragemService.h	Tue Aug 25 23:26:43 2015 +0000
@@ -1,19 +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.
- */
-
 #ifndef __BLE_GARAGEM_SERVICE_H__
 #define __BLE_GARAGEM_SERVICE_H__
 #include "History.h"
@@ -24,7 +8,6 @@
 #define GARAGEM_ERROR_REPETITION_ATTACK     1
 #define GARAGEM_ERROR_WRONG_SHARED_SECRET   2
 
-
 class GaragemService {
 public:
     const static uint16_t GARAGEM_SERVICE_UUID              = 0x2000;
@@ -35,8 +18,8 @@
     GaragemService(BLE &_ble) :
         ble(_ble), 
         GaragemChallenge(GARAGEM_CHALLENGE_CHARACTERISTIC_UUID, (uint8_t *)"INIT"),
-        GaragemLastOpenTS(GARAGEM_LAST_OPEN_TS_UUID, 0),
-        GaragemLastOpenID(GARAGEM_LAST_OPEN_ID_UUID, (uint8_t *)"INIT")
+        GaragemLastOpenTS(GARAGEM_LAST_OPEN_TS_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
+        GaragemLastOpenID(GARAGEM_LAST_OPEN_ID_UUID, (uint8_t *)"INIT", GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
     {
         GattCharacteristic *charTable[] = {&GaragemChallenge, &GaragemLastOpenTS, &GaragemLastOpenID};
         GattService         GaragemService(GARAGEM_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
@@ -46,23 +29,35 @@
     GattAttribute::Handle_t getChallengeHandle() const {
         return GaragemChallenge.getValueHandle();
     }
+    GattAttribute::Handle_t getLastOpenTSHandle() const {
+        return GaragemLastOpenTS.getValueHandle();
+    }
+    void nextLastOpen(const uint8_t *data, uint16_t len) {
+        uint64_t token = history.getToken();
+        uint8_t *tok = (uint8_t *) &token;
+        DBG("LAST:\tTS=%lu\tID=%c%c%c%c\r\n", *((uint32_t *) tok), tok[4], tok[5], tok[6], tok[7]); 
+        ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (uint8_t *)&tok[0], 4*sizeof(uint8_t));
+        ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (uint8_t *)&tok[4], 4*sizeof(uint8_t));
+
+    }
+
     
     int checkMessage(uint8_t *msg) {
         DBG("WHAT ? %s\r\n", (char *) msg);
 
         uint64_t token;
-        char syskey[8];
         memcpy(&token, msg, 8);
-        memcpy(&syskey, &msg[8], 8);
+
         DBG("TS=%lu\tID=%c%c%c%c\tSYSKEY=%s\r\n", *((uint32_t *) msg), msg[4], msg[5], msg[6], msg[7], &msg[8]); 
 
         //check we are not a victim of a repetion attack
-        if(history.last_ts() >= (uint32_t) msg[0]) {
+        DBG("%lu\t>=\t%lu\r\n", history.last_ts(), *((uint32_t*) &msg[0]));
+        if(history.last_ts() >= *((uint32_t *) &msg[0])) {
             DBG("HA HA repetion here...\r\n");
             return GARAGEM_ERROR_REPETITION_ATTACK;
         }
         
-        if (strncmp(syskey,SHARED_SECRET,7)==0) {//TODO MOVE TO 8 CHARS
+        if (strncmp((const char *) &msg[8],SHARED_SECRET,7)==0) {//TODO MOVE TO 8 CHARS
             //Save our success
             history.save(token);
             ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (const uint8_t *)&msg[0], 4*sizeof(uint8_t));