BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Committer:
duststorm
Date:
Mon May 01 00:33:07 2017 +0000
Revision:
15:88ea59cb2b9e
Parent:
12:eaee29bfa1c7
force address for Nano to F2:9B:4B:03:02:01 to match the prefix expected by the app

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgomes9:329af8cdc923 1#ifndef __BLE_GARAGEM_SERVICE_H__
dgomes9:329af8cdc923 2#define __BLE_GARAGEM_SERVICE_H__
dgomes9:329af8cdc923 3#include "History.h"
dgomes9:329af8cdc923 4
dgomes9:329af8cdc923 5#define SHARED_SECRET "ABRE-TE"
dgomes9:329af8cdc923 6
dgomes9:329af8cdc923 7#define GARAGEM_OK 0
dgomes9:329af8cdc923 8#define GARAGEM_ERROR_REPETITION_ATTACK 1
dgomes9:329af8cdc923 9#define GARAGEM_ERROR_WRONG_SHARED_SECRET 2
dgomes9:329af8cdc923 10
dgomes9:329af8cdc923 11class GaragemService {
dgomes9:329af8cdc923 12public:
dgomes9:329af8cdc923 13 const static uint16_t GARAGEM_SERVICE_UUID = 0x2000;
dgomes9:329af8cdc923 14 const static uint16_t GARAGEM_CHALLENGE_CHARACTERISTIC_UUID = 0x2001;
dgomes9:329af8cdc923 15 const static uint16_t GARAGEM_LAST_OPEN_TS_UUID = 0x2002;
dgomes9:329af8cdc923 16 const static uint16_t GARAGEM_LAST_OPEN_ID_UUID = 0x2003;
dgomes9:329af8cdc923 17
dgomes9:329af8cdc923 18 GaragemService(BLE &_ble) :
dgomes9:329af8cdc923 19 ble(_ble),
dgomes9:329af8cdc923 20 GaragemChallenge(GARAGEM_CHALLENGE_CHARACTERISTIC_UUID, (uint8_t *)"INIT"),
dgomes10:80850cd6c29e 21 GaragemLastOpenTS(GARAGEM_LAST_OPEN_TS_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),
dgomes10:80850cd6c29e 22 GaragemLastOpenID(GARAGEM_LAST_OPEN_ID_UUID, (uint8_t *)"INIT", GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY)
dgomes9:329af8cdc923 23 {
dgomes9:329af8cdc923 24 GattCharacteristic *charTable[] = {&GaragemChallenge, &GaragemLastOpenTS, &GaragemLastOpenID};
dgomes9:329af8cdc923 25 GattService GaragemService(GARAGEM_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *));
dgomes9:329af8cdc923 26 ble.gattServer().addService(GaragemService);
dgomes9:329af8cdc923 27 }
dgomes9:329af8cdc923 28
dgomes9:329af8cdc923 29 GattAttribute::Handle_t getChallengeHandle() const {
dgomes9:329af8cdc923 30 return GaragemChallenge.getValueHandle();
dgomes9:329af8cdc923 31 }
dgomes10:80850cd6c29e 32 GattAttribute::Handle_t getLastOpenTSHandle() const {
dgomes10:80850cd6c29e 33 return GaragemLastOpenTS.getValueHandle();
dgomes10:80850cd6c29e 34 }
dgomes10:80850cd6c29e 35 void nextLastOpen(const uint8_t *data, uint16_t len) {
dgomes10:80850cd6c29e 36 uint64_t token = history.getToken();
dgomes10:80850cd6c29e 37 uint8_t *tok = (uint8_t *) &token;
dgomes10:80850cd6c29e 38 DBG("LAST:\tTS=%lu\tID=%c%c%c%c\r\n", *((uint32_t *) tok), tok[4], tok[5], tok[6], tok[7]);
dgomes10:80850cd6c29e 39 ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (uint8_t *)&tok[0], 4*sizeof(uint8_t));
dgomes10:80850cd6c29e 40 ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (uint8_t *)&tok[4], 4*sizeof(uint8_t));
dgomes10:80850cd6c29e 41
dgomes10:80850cd6c29e 42 }
dgomes10:80850cd6c29e 43
dgomes9:329af8cdc923 44 int checkMessage(uint8_t *msg) {
dgomes9:329af8cdc923 45 DBG("WHAT ? %s\r\n", (char *) msg);
dgomes9:329af8cdc923 46
dgomes9:329af8cdc923 47 uint64_t token;
dgomes9:329af8cdc923 48 memcpy(&token, msg, 8);
dgomes10:80850cd6c29e 49
dgomes9:329af8cdc923 50 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]);
dgomes9:329af8cdc923 51
duststorm15:88ea59cb2b9e 52 //check we are not a victim of a repetition attack
dgomes10:80850cd6c29e 53 DBG("%lu\t>=\t%lu\r\n", history.last_ts(), *((uint32_t*) &msg[0]));
dgomes10:80850cd6c29e 54 if(history.last_ts() >= *((uint32_t *) &msg[0])) {
duststorm15:88ea59cb2b9e 55 DBG("HA HA repetition here...\r\n");
dgomes9:329af8cdc923 56 return GARAGEM_ERROR_REPETITION_ATTACK;
dgomes9:329af8cdc923 57 }
dgomes9:329af8cdc923 58
dgomes10:80850cd6c29e 59 if (strncmp((const char *) &msg[8],SHARED_SECRET,7)==0) {//TODO MOVE TO 8 CHARS
dgomes9:329af8cdc923 60 //Save our success
dgomes9:329af8cdc923 61 history.save(token);
dgomes9:329af8cdc923 62 ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (const uint8_t *)&msg[0], 4*sizeof(uint8_t));
dgomes9:329af8cdc923 63 ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (const uint8_t *)&msg[4], 4*sizeof(uint8_t));
dgomes9:329af8cdc923 64
dgomes9:329af8cdc923 65 return GARAGEM_OK;
dgomes9:329af8cdc923 66 } else {
dgomes9:329af8cdc923 67 return GARAGEM_ERROR_WRONG_SHARED_SECRET;
dgomes9:329af8cdc923 68 }
dgomes9:329af8cdc923 69 }
dgomes9:329af8cdc923 70
dgomes9:329af8cdc923 71private:
dgomes9:329af8cdc923 72 BLE &ble;
dgomes9:329af8cdc923 73 WriteOnlyArrayGattCharacteristic<uint8_t, 16> GaragemChallenge;
dgomes9:329af8cdc923 74 ReadOnlyGattCharacteristic<uint32_t> GaragemLastOpenTS;
dgomes9:329af8cdc923 75 ReadOnlyArrayGattCharacteristic<uint8_t, 4> GaragemLastOpenID;
dgomes9:329af8cdc923 76
dgomes9:329af8cdc923 77 History<16> history;
dgomes9:329af8cdc923 78};
dgomes9:329af8cdc923 79
dgomes9:329af8cdc923 80#endif /* #ifndef __BLE_GARAGEM_SERVICE_H__ */