![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
fork to force BLE id to have prefix that is assumed by the app
Dependencies: BLE_API Crypto RNG mbed nRF51822
Fork of Garagem by
main.cpp@10:80850cd6c29e, 2015-08-25 (annotated)
- Committer:
- dgomes
- Date:
- Tue Aug 25 23:26:43 2015 +0000
- Revision:
- 10:80850cd6c29e
- Parent:
- 9:329af8cdc923
- Child:
- 11:3de9b542eeac
Version 1; Moving to SecurityService
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:ed5c53845de7 | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:ed5c53845de7 | 2 | * Copyright (c) 2006-2013 ARM Limited |
rgrover1 | 0:ed5c53845de7 | 3 | * |
rgrover1 | 0:ed5c53845de7 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:ed5c53845de7 | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:ed5c53845de7 | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:ed5c53845de7 | 7 | * |
rgrover1 | 0:ed5c53845de7 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:ed5c53845de7 | 9 | * |
rgrover1 | 0:ed5c53845de7 | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:ed5c53845de7 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:ed5c53845de7 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:ed5c53845de7 | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:ed5c53845de7 | 14 | * limitations under the License. |
rgrover1 | 0:ed5c53845de7 | 15 | */ |
dgomes | 9:329af8cdc923 | 16 | #define DEBUG |
dgomes | 9:329af8cdc923 | 17 | |
dgomes | 9:329af8cdc923 | 18 | #ifndef DEBUG |
dgomes | 9:329af8cdc923 | 19 | #define DEVICE_SERIAL 0 //disable SERIAL so me can use P0_8 (shared with serial) |
dgomes | 9:329af8cdc923 | 20 | #define DBG(...) |
dgomes | 9:329af8cdc923 | 21 | #else |
dgomes | 9:329af8cdc923 | 22 | #define DBG printf |
dgomes | 9:329af8cdc923 | 23 | #endif |
rgrover1 | 0:ed5c53845de7 | 24 | |
rgrover1 | 0:ed5c53845de7 | 25 | #include "mbed.h" |
rgrover1 | 6:fca54d71d10e | 26 | #include "BLE.h" |
dgomes | 9:329af8cdc923 | 27 | #include "GaragemService.h" |
dgomes | 9:329af8cdc923 | 28 | #include "ble/services/iBeacon.h" |
dgomes | 9:329af8cdc923 | 29 | #include "DeviceInformationService.h" |
dgomes | 9:329af8cdc923 | 30 | |
dgomes | 9:329af8cdc923 | 31 | #include "Crypto.h" |
dgomes | 9:329af8cdc923 | 32 | |
dgomes | 9:329af8cdc923 | 33 | //how-to test: |
dgomes | 9:329af8cdc923 | 34 | //create a string to write to the characteristic |
dgomes | 10:80850cd6c29e | 35 | //openssl enc -aes-128-cbc -K 9734062BA852A049CF5D40593B769014 -iv A2685636521871D02306E2EB8F7027B3 -out /dev/stdout -in inputfile |
dgomes | 10:80850cd6c29e | 36 | // TTTTIIIISSSSSSSS |
dgomes | 10:80850cd6c29e | 37 | // T -> 4 bytes Timestamp uint32_t |
dgomes | 10:80850cd6c29e | 38 | // I -> 4 bytes ID (chars) |
dgomes | 10:80850cd6c29e | 39 | // S -> 8 bytes SHARED_SECRET (chars) |
dgomes | 10:80850cd6c29e | 40 | |
dgomes | 9:329af8cdc923 | 41 | #define SHARED_KEY "figueiredo" |
dgomes | 9:329af8cdc923 | 42 | #define DEVICE_NAME "Garagem" |
dgomes | 9:329af8cdc923 | 43 | //openssl enc -aes-128-cbc -pass pass:********** -nosalt -P |
dgomes | 9:329af8cdc923 | 44 | uint8_t myKey[16]; |
dgomes | 9:329af8cdc923 | 45 | uint8_t iv[16] = { 0xA2, 0x68, 0x56, 0x36, 0x52, 0x18, 0x71, 0xD0, 0x23, 0x06, 0xE2, 0xEB, 0x8F, 0x70, 0x27, 0xB3 }; |
rgrover1 | 0:ed5c53845de7 | 46 | |
rgrover1 | 7:c9271599ec5f | 47 | BLE ble; |
dgomes | 9:329af8cdc923 | 48 | DigitalOut actuatedLED(P0_19); |
dgomes | 9:329af8cdc923 | 49 | Timeout offRelay; |
dgomes | 9:329af8cdc923 | 50 | |
dgomes | 9:329af8cdc923 | 51 | #ifndef DEBUG |
dgomes | 9:329af8cdc923 | 52 | DigitalOut relay(P0_8); |
dgomes | 9:329af8cdc923 | 53 | #endif |
dgomes | 9:329af8cdc923 | 54 | |
dgomes | 9:329af8cdc923 | 55 | static const uint16_t uuid16_list[] = {GaragemService::GARAGEM_SERVICE_UUID}; |
rgrover1 | 0:ed5c53845de7 | 56 | |
dgomes | 9:329af8cdc923 | 57 | GaragemService *garagemServicePtr; |
rgrover1 | 0:ed5c53845de7 | 58 | |
dgomes | 9:329af8cdc923 | 59 | void switchOffRelay() |
dgomes | 9:329af8cdc923 | 60 | { |
dgomes | 9:329af8cdc923 | 61 | actuatedLED = !actuatedLED; |
dgomes | 9:329af8cdc923 | 62 | DBG("JA CHEGA...\r\n"); |
dgomes | 9:329af8cdc923 | 63 | #ifndef DEBUG |
dgomes | 9:329af8cdc923 | 64 | relay = 0; |
dgomes | 9:329af8cdc923 | 65 | #endif |
dgomes | 9:329af8cdc923 | 66 | } |
dgomes | 9:329af8cdc923 | 67 | |
rgrover1 | 0:ed5c53845de7 | 68 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
dgomes | 9:329af8cdc923 | 69 | { |
dgomes | 9:329af8cdc923 | 70 | ble.startAdvertising(); |
rgrover1 | 0:ed5c53845de7 | 71 | } |
rgrover1 | 0:ed5c53845de7 | 72 | /** |
rgrover1 | 0:ed5c53845de7 | 73 | * This callback allows the LEDService to receive updates to the ledState Characteristic. |
rgrover1 | 0:ed5c53845de7 | 74 | * |
rgrover1 | 0:ed5c53845de7 | 75 | * @param[in] params |
rgrover1 | 0:ed5c53845de7 | 76 | * Information about the characterisitc being updated. |
rgrover1 | 0:ed5c53845de7 | 77 | */ |
rgrover1 | 7:c9271599ec5f | 78 | void onDataWrittenCallback(const GattWriteCallbackParams *params) { |
dgomes | 9:329af8cdc923 | 79 | DBG("onDataWrittenCallback: handle = %d len = %d\r\n", params->handle, params->len); |
dgomes | 10:80850cd6c29e | 80 | garagemServicePtr->nextLastOpen(params->data, params->len); |
dgomes | 10:80850cd6c29e | 81 | |
dgomes | 9:329af8cdc923 | 82 | if ((params->handle == garagemServicePtr->getChallengeHandle()) && (params->len ==16)) { |
dgomes | 10:80850cd6c29e | 83 | DBG("We have Challenge: data = %s\r\n", (char *) (params->data)); |
dgomes | 9:329af8cdc923 | 84 | |
dgomes | 9:329af8cdc923 | 85 | AES myAES(AES_128, myKey, iv); |
dgomes | 9:329af8cdc923 | 86 | uint8_t msg[16]; |
dgomes | 9:329af8cdc923 | 87 | myAES.decrypt(msg,(uint8_t *) (params->data),16); |
dgomes | 9:329af8cdc923 | 88 | |
dgomes | 9:329af8cdc923 | 89 | if(garagemServicePtr->checkMessage(msg) == GARAGEM_OK) { |
dgomes | 10:80850cd6c29e | 90 | DBG("ABRE-TE SESAMO!\r\n"); |
dgomes | 10:80850cd6c29e | 91 | |
dgomes | 9:329af8cdc923 | 92 | #ifndef DEBUG |
dgomes | 9:329af8cdc923 | 93 | relay = 1; |
dgomes | 9:329af8cdc923 | 94 | #endif |
dgomes | 10:80850cd6c29e | 95 | actuatedLED = !actuatedLED; |
dgomes | 9:329af8cdc923 | 96 | //please cleanup afterwards |
dgomes | 9:329af8cdc923 | 97 | offRelay.attach(&switchOffRelay, 1.0); |
dgomes | 9:329af8cdc923 | 98 | |
dgomes | 9:329af8cdc923 | 99 | } else { |
dgomes | 9:329af8cdc923 | 100 | DBG("NO SUCH LUCK...\r\n"); |
dgomes | 9:329af8cdc923 | 101 | } |
dgomes | 10:80850cd6c29e | 102 | } |
rgrover1 | 0:ed5c53845de7 | 103 | } |
rgrover1 | 0:ed5c53845de7 | 104 | |
rgrover1 | 0:ed5c53845de7 | 105 | int main(void) |
rgrover1 | 0:ed5c53845de7 | 106 | { |
dgomes | 9:329af8cdc923 | 107 | DBG("Garagem v0\r\n"); |
rgrover1 | 0:ed5c53845de7 | 108 | |
dgomes | 9:329af8cdc923 | 109 | //Compute myKey from SHARED_KEY |
dgomes | 9:329af8cdc923 | 110 | MD5::computeHash(myKey, (uint8_t*) SHARED_KEY, strlen(SHARED_KEY)); |
dgomes | 9:329af8cdc923 | 111 | |
dgomes | 9:329af8cdc923 | 112 | actuatedLED = true; |
dgomes | 9:329af8cdc923 | 113 | #ifndef DEBUG |
dgomes | 9:329af8cdc923 | 114 | relay = 0; |
dgomes | 9:329af8cdc923 | 115 | #endif |
rgrover1 | 0:ed5c53845de7 | 116 | |
rgrover1 | 0:ed5c53845de7 | 117 | ble.init(); |
dgomes | 9:329af8cdc923 | 118 | ble.onDisconnection(disconnectionCallback); |
rgrover1 | 7:c9271599ec5f | 119 | ble.gattServer().onDataWritten(onDataWrittenCallback); |
rgrover1 | 0:ed5c53845de7 | 120 | |
dgomes | 9:329af8cdc923 | 121 | /** |
dgomes | 9:329af8cdc923 | 122 | * The Beacon payload has the following composition: |
dgomes | 9:329af8cdc923 | 123 | * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61 |
dgomes | 9:329af8cdc923 | 124 | * Major/Minor = 0x1122 / 0x3344 |
dgomes | 9:329af8cdc923 | 125 | * Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB) |
dgomes | 9:329af8cdc923 | 126 | * |
dgomes | 9:329af8cdc923 | 127 | * Note: please remember to calibrate your beacons TX Power for more accurate results. |
dgomes | 9:329af8cdc923 | 128 | */ |
dgomes | 9:329af8cdc923 | 129 | const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4, |
dgomes | 9:329af8cdc923 | 130 | 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61}; |
dgomes | 9:329af8cdc923 | 131 | uint16_t majorNumber = 3800; |
dgomes | 9:329af8cdc923 | 132 | uint16_t minorNumber = 1423; |
dgomes | 9:329af8cdc923 | 133 | uint16_t txPower = 0xBE; |
dgomes | 9:329af8cdc923 | 134 | iBeacon ibeacon(ble, uuid, majorNumber, minorNumber, txPower); |
dgomes | 9:329af8cdc923 | 135 | |
dgomes | 9:329af8cdc923 | 136 | GaragemService garagemService(ble); |
dgomes | 9:329af8cdc923 | 137 | garagemServicePtr = &garagemService; |
dgomes | 9:329af8cdc923 | 138 | |
dgomes | 9:329af8cdc923 | 139 | DeviceInformationService deviceInfo(ble, "diogogomes.com", DEVICE_NAME, "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
rgrover1 | 0:ed5c53845de7 | 140 | |
rgrover1 | 0:ed5c53845de7 | 141 | /* setup advertising */ |
rgrover1 | 7:c9271599ec5f | 142 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
rgrover1 | 7:c9271599ec5f | 143 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
rgrover1 | 7:c9271599ec5f | 144 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
rgrover1 | 7:c9271599ec5f | 145 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
dgomes | 9:329af8cdc923 | 146 | |
dgomes | 9:329af8cdc923 | 147 | |
rgrover1 | 7:c9271599ec5f | 148 | ble.gap().setAdvertisingInterval(1000); /* 1000ms. */ |
rgrover1 | 7:c9271599ec5f | 149 | ble.gap().startAdvertising(); |
rgrover1 | 0:ed5c53845de7 | 150 | |
rgrover1 | 0:ed5c53845de7 | 151 | while (true) { |
rgrover1 | 0:ed5c53845de7 | 152 | ble.waitForEvent(); |
rgrover1 | 0:ed5c53845de7 | 153 | } |
rgrover1 | 0:ed5c53845de7 | 154 | } |