BLE Application to open a Garage door
Dependencies: BLE_API Crypto RNG mbed nRF51822
Fork of BLE_LED by
GaragemService.h@9:329af8cdc923, 2015-08-25 (annotated)
- Committer:
- dgomes
- Date:
- Tue Aug 25 22:18:21 2015 +0000
- Revision:
- 9:329af8cdc923
- Child:
- 10:80850cd6c29e
Mostly Works :)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dgomes | 9:329af8cdc923 | 1 | /* mbed Microcontroller Library |
dgomes | 9:329af8cdc923 | 2 | * Copyright (c) 2006-2013 ARM Limited |
dgomes | 9:329af8cdc923 | 3 | * |
dgomes | 9:329af8cdc923 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
dgomes | 9:329af8cdc923 | 5 | * you may not use this file except in compliance with the License. |
dgomes | 9:329af8cdc923 | 6 | * You may obtain a copy of the License at |
dgomes | 9:329af8cdc923 | 7 | * |
dgomes | 9:329af8cdc923 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
dgomes | 9:329af8cdc923 | 9 | * |
dgomes | 9:329af8cdc923 | 10 | * Unless required by applicable law or agreed to in writing, software |
dgomes | 9:329af8cdc923 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
dgomes | 9:329af8cdc923 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
dgomes | 9:329af8cdc923 | 13 | * See the License for the specific language governing permissions and |
dgomes | 9:329af8cdc923 | 14 | * limitations under the License. |
dgomes | 9:329af8cdc923 | 15 | */ |
dgomes | 9:329af8cdc923 | 16 | |
dgomes | 9:329af8cdc923 | 17 | #ifndef __BLE_GARAGEM_SERVICE_H__ |
dgomes | 9:329af8cdc923 | 18 | #define __BLE_GARAGEM_SERVICE_H__ |
dgomes | 9:329af8cdc923 | 19 | #include "History.h" |
dgomes | 9:329af8cdc923 | 20 | |
dgomes | 9:329af8cdc923 | 21 | #define SHARED_SECRET "ABRE-TE" |
dgomes | 9:329af8cdc923 | 22 | |
dgomes | 9:329af8cdc923 | 23 | #define GARAGEM_OK 0 |
dgomes | 9:329af8cdc923 | 24 | #define GARAGEM_ERROR_REPETITION_ATTACK 1 |
dgomes | 9:329af8cdc923 | 25 | #define GARAGEM_ERROR_WRONG_SHARED_SECRET 2 |
dgomes | 9:329af8cdc923 | 26 | |
dgomes | 9:329af8cdc923 | 27 | |
dgomes | 9:329af8cdc923 | 28 | class GaragemService { |
dgomes | 9:329af8cdc923 | 29 | public: |
dgomes | 9:329af8cdc923 | 30 | const static uint16_t GARAGEM_SERVICE_UUID = 0x2000; |
dgomes | 9:329af8cdc923 | 31 | const static uint16_t GARAGEM_CHALLENGE_CHARACTERISTIC_UUID = 0x2001; |
dgomes | 9:329af8cdc923 | 32 | const static uint16_t GARAGEM_LAST_OPEN_TS_UUID = 0x2002; |
dgomes | 9:329af8cdc923 | 33 | const static uint16_t GARAGEM_LAST_OPEN_ID_UUID = 0x2003; |
dgomes | 9:329af8cdc923 | 34 | |
dgomes | 9:329af8cdc923 | 35 | GaragemService(BLE &_ble) : |
dgomes | 9:329af8cdc923 | 36 | ble(_ble), |
dgomes | 9:329af8cdc923 | 37 | GaragemChallenge(GARAGEM_CHALLENGE_CHARACTERISTIC_UUID, (uint8_t *)"INIT"), |
dgomes | 9:329af8cdc923 | 38 | GaragemLastOpenTS(GARAGEM_LAST_OPEN_TS_UUID, 0), |
dgomes | 9:329af8cdc923 | 39 | GaragemLastOpenID(GARAGEM_LAST_OPEN_ID_UUID, (uint8_t *)"INIT") |
dgomes | 9:329af8cdc923 | 40 | { |
dgomes | 9:329af8cdc923 | 41 | GattCharacteristic *charTable[] = {&GaragemChallenge, &GaragemLastOpenTS, &GaragemLastOpenID}; |
dgomes | 9:329af8cdc923 | 42 | GattService GaragemService(GARAGEM_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); |
dgomes | 9:329af8cdc923 | 43 | ble.gattServer().addService(GaragemService); |
dgomes | 9:329af8cdc923 | 44 | } |
dgomes | 9:329af8cdc923 | 45 | |
dgomes | 9:329af8cdc923 | 46 | GattAttribute::Handle_t getChallengeHandle() const { |
dgomes | 9:329af8cdc923 | 47 | return GaragemChallenge.getValueHandle(); |
dgomes | 9:329af8cdc923 | 48 | } |
dgomes | 9:329af8cdc923 | 49 | |
dgomes | 9:329af8cdc923 | 50 | int checkMessage(uint8_t *msg) { |
dgomes | 9:329af8cdc923 | 51 | DBG("WHAT ? %s\r\n", (char *) msg); |
dgomes | 9:329af8cdc923 | 52 | |
dgomes | 9:329af8cdc923 | 53 | uint64_t token; |
dgomes | 9:329af8cdc923 | 54 | char syskey[8]; |
dgomes | 9:329af8cdc923 | 55 | memcpy(&token, msg, 8); |
dgomes | 9:329af8cdc923 | 56 | memcpy(&syskey, &msg[8], 8); |
dgomes | 9:329af8cdc923 | 57 | 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 | 58 | |
dgomes | 9:329af8cdc923 | 59 | //check we are not a victim of a repetion attack |
dgomes | 9:329af8cdc923 | 60 | if(history.last_ts() >= (uint32_t) msg[0]) { |
dgomes | 9:329af8cdc923 | 61 | DBG("HA HA repetion here...\r\n"); |
dgomes | 9:329af8cdc923 | 62 | return GARAGEM_ERROR_REPETITION_ATTACK; |
dgomes | 9:329af8cdc923 | 63 | } |
dgomes | 9:329af8cdc923 | 64 | |
dgomes | 9:329af8cdc923 | 65 | if (strncmp(syskey,SHARED_SECRET,7)==0) {//TODO MOVE TO 8 CHARS |
dgomes | 9:329af8cdc923 | 66 | //Save our success |
dgomes | 9:329af8cdc923 | 67 | history.save(token); |
dgomes | 9:329af8cdc923 | 68 | ble.gattServer().write(GaragemLastOpenTS.getValueHandle(), (const uint8_t *)&msg[0], 4*sizeof(uint8_t)); |
dgomes | 9:329af8cdc923 | 69 | ble.gattServer().write(GaragemLastOpenID.getValueHandle(), (const uint8_t *)&msg[4], 4*sizeof(uint8_t)); |
dgomes | 9:329af8cdc923 | 70 | |
dgomes | 9:329af8cdc923 | 71 | return GARAGEM_OK; |
dgomes | 9:329af8cdc923 | 72 | } else { |
dgomes | 9:329af8cdc923 | 73 | return GARAGEM_ERROR_WRONG_SHARED_SECRET; |
dgomes | 9:329af8cdc923 | 74 | } |
dgomes | 9:329af8cdc923 | 75 | } |
dgomes | 9:329af8cdc923 | 76 | |
dgomes | 9:329af8cdc923 | 77 | private: |
dgomes | 9:329af8cdc923 | 78 | BLE &ble; |
dgomes | 9:329af8cdc923 | 79 | WriteOnlyArrayGattCharacteristic<uint8_t, 16> GaragemChallenge; |
dgomes | 9:329af8cdc923 | 80 | ReadOnlyGattCharacteristic<uint32_t> GaragemLastOpenTS; |
dgomes | 9:329af8cdc923 | 81 | ReadOnlyArrayGattCharacteristic<uint8_t, 4> GaragemLastOpenID; |
dgomes | 9:329af8cdc923 | 82 | |
dgomes | 9:329af8cdc923 | 83 | History<16> history; |
dgomes | 9:329af8cdc923 | 84 | }; |
dgomes | 9:329af8cdc923 | 85 | |
dgomes | 9:329af8cdc923 | 86 | #endif /* #ifndef __BLE_GARAGEM_SERVICE_H__ */ |