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