BLE Application to open a Garage door
Dependencies: BLE_API Crypto RNG mbed nRF51822
Fork of BLE_LED by
Diff: GaragemService.h
- Revision:
- 9:329af8cdc923
- Child:
- 10:80850cd6c29e
diff -r 0f145b25ab0b -r 329af8cdc923 GaragemService.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GaragemService.h Tue Aug 25 22:18:21 2015 +0000 @@ -0,0 +1,86 @@ +/* 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" + +#define SHARED_SECRET "ABRE-TE" + +#define GARAGEM_OK 0 +#define GARAGEM_ERROR_REPETITION_ATTACK 1 +#define GARAGEM_ERROR_WRONG_SHARED_SECRET 2 + + +class GaragemService { +public: + const static uint16_t GARAGEM_SERVICE_UUID = 0x2000; + const static uint16_t GARAGEM_CHALLENGE_CHARACTERISTIC_UUID = 0x2001; + const static uint16_t GARAGEM_LAST_OPEN_TS_UUID = 0x2002; + const static uint16_t GARAGEM_LAST_OPEN_ID_UUID = 0x2003; + + 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") + { + GattCharacteristic *charTable[] = {&GaragemChallenge, &GaragemLastOpenTS, &GaragemLastOpenID}; + GattService GaragemService(GARAGEM_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); + ble.gattServer().addService(GaragemService); + } + + GattAttribute::Handle_t getChallengeHandle() const { + return GaragemChallenge.getValueHandle(); + } + + 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("HA HA repetion here...\r\n"); + return GARAGEM_ERROR_REPETITION_ATTACK; + } + + if (strncmp(syskey,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)); + ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (const uint8_t *)&msg[4], 4*sizeof(uint8_t)); + + return GARAGEM_OK; + } else { + return GARAGEM_ERROR_WRONG_SHARED_SECRET; + } + } + +private: + BLE &ble; + WriteOnlyArrayGattCharacteristic<uint8_t, 16> GaragemChallenge; + ReadOnlyGattCharacteristic<uint32_t> GaragemLastOpenTS; + ReadOnlyArrayGattCharacteristic<uint8_t, 4> GaragemLastOpenID; + + History<16> history; +}; + +#endif /* #ifndef __BLE_GARAGEM_SERVICE_H__ */