BLE Application to open a Garage door
Dependencies: BLE_API Crypto RNG mbed nRF51822
Fork of BLE_LED by
GaragemService.h
00001 #ifndef __BLE_GARAGEM_SERVICE_H__ 00002 #define __BLE_GARAGEM_SERVICE_H__ 00003 #include "History.h" 00004 00005 #define SHARED_SECRET "ABRE-TE" 00006 00007 #define GARAGEM_OK 0 00008 #define GARAGEM_ERROR_REPETITION_ATTACK 1 00009 #define GARAGEM_ERROR_WRONG_SHARED_SECRET 2 00010 00011 class GaragemService { 00012 public: 00013 const static uint16_t GARAGEM_SERVICE_UUID = 0x2000; 00014 const static uint16_t GARAGEM_CHALLENGE_CHARACTERISTIC_UUID = 0x2001; 00015 const static uint16_t GARAGEM_LAST_OPEN_TS_UUID = 0x2002; 00016 const static uint16_t GARAGEM_LAST_OPEN_ID_UUID = 0x2003; 00017 00018 GaragemService(BLE &_ble) : 00019 ble(_ble), 00020 GaragemChallenge(GARAGEM_CHALLENGE_CHARACTERISTIC_UUID, (uint8_t *)"INIT"), 00021 GaragemLastOpenTS(GARAGEM_LAST_OPEN_TS_UUID, 0, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY), 00022 GaragemLastOpenID(GARAGEM_LAST_OPEN_ID_UUID, (uint8_t *)"INIT", GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY) 00023 { 00024 GattCharacteristic *charTable[] = {&GaragemChallenge, &GaragemLastOpenTS, &GaragemLastOpenID}; 00025 GattService GaragemService(GARAGEM_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); 00026 ble.gattServer().addService(GaragemService); 00027 } 00028 00029 GattAttribute::Handle_t getChallengeHandle() const { 00030 return GaragemChallenge.getValueHandle(); 00031 } 00032 GattAttribute::Handle_t getLastOpenTSHandle() const { 00033 return GaragemLastOpenTS.getValueHandle(); 00034 } 00035 void nextLastOpen(const uint8_t *data, uint16_t len) { 00036 uint64_t token = history.getToken(); 00037 uint8_t *tok = (uint8_t *) &token; 00038 DBG("LAST:\tTS=%lu\tID=%c%c%c%c\r\n", *((uint32_t *) tok), tok[4], tok[5], tok[6], tok[7]); 00039 ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (uint8_t *)&tok[0], 4*sizeof(uint8_t)); 00040 ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (uint8_t *)&tok[4], 4*sizeof(uint8_t)); 00041 00042 } 00043 00044 int checkMessage(uint8_t *msg) { 00045 DBG("WHAT ? %s\r\n", (char *) msg); 00046 00047 uint64_t token; 00048 memcpy(&token, msg, 8); 00049 00050 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]); 00051 00052 //check we are not a victim of a repetition attack 00053 DBG("%lu\t>=\t%lu\r\n", history.last_ts(), *((uint32_t*) &msg[0])); 00054 if(history.last_ts() >= *((uint32_t *) &msg[0])) { 00055 DBG("HA HA repetition here...\r\n"); 00056 return GARAGEM_ERROR_REPETITION_ATTACK; 00057 } 00058 00059 if (strncmp((const char *) &msg[8],SHARED_SECRET,7)==0) {//TODO MOVE TO 8 CHARS 00060 //Save our success 00061 history.save(token); 00062 ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (const uint8_t *)&msg[0], 4*sizeof(uint8_t)); 00063 ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (const uint8_t *)&msg[4], 4*sizeof(uint8_t)); 00064 00065 return GARAGEM_OK; 00066 } else { 00067 return GARAGEM_ERROR_WRONG_SHARED_SECRET; 00068 } 00069 } 00070 00071 private: 00072 BLE &ble; 00073 WriteOnlyArrayGattCharacteristic<uint8_t, 16> GaragemChallenge; 00074 ReadOnlyGattCharacteristic<uint32_t> GaragemLastOpenTS; 00075 ReadOnlyArrayGattCharacteristic<uint8_t, 4> GaragemLastOpenID; 00076 00077 History<16> history; 00078 }; 00079 00080 #endif /* #ifndef __BLE_GARAGEM_SERVICE_H__ */
Generated on Thu Jul 14 2022 08:19:32 by 1.7.2