BLE Application to open a Garage door

Dependencies:   BLE_API Crypto RNG mbed nRF51822

Fork of BLE_LED by Bluetooth Low Energy

Committer:
duststorm
Date:
Mon May 01 00:33:07 2017 +0000
Revision:
15:88ea59cb2b9e
Parent:
13:bc1be947b497
force address for Nano to F2:9B:4B:03:02:01 to match the prefix expected by the app

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dgomes13:bc1be947b497 1//#define DEBUG
dgomes9:329af8cdc923 2
dgomes9:329af8cdc923 3#ifndef DEBUG
dgomes9:329af8cdc923 4 #define DEVICE_SERIAL 0 //disable SERIAL so me can use P0_8 (shared with serial)
dgomes9:329af8cdc923 5 #define DBG(...)
dgomes9:329af8cdc923 6#else
dgomes9:329af8cdc923 7 #define DBG printf
dgomes9:329af8cdc923 8#endif
rgrover10:ed5c53845de7 9
rgrover10:ed5c53845de7 10#include "mbed.h"
rgrover16:fca54d71d10e 11#include "BLE.h"
dgomes9:329af8cdc923 12#include "GaragemService.h"
dgomes11:3de9b542eeac 13#include "SecurityService.h"
dgomes9:329af8cdc923 14#include "ble/services/iBeacon.h"
dgomes9:329af8cdc923 15#include "DeviceInformationService.h"
dgomes9:329af8cdc923 16
dgomes11:3de9b542eeac 17// HOW-TO TEST:
dgomes11:3de9b542eeac 18//create a string to write to the characteristic 2000:2001
dgomes10:80850cd6c29e 19//openssl enc -aes-128-cbc -K 9734062BA852A049CF5D40593B769014 -iv A2685636521871D02306E2EB8F7027B3 -out /dev/stdout -in inputfile
dgomes10:80850cd6c29e 20// TTTTIIIISSSSSSSS
dgomes10:80850cd6c29e 21// T -> 4 bytes Timestamp uint32_t
dgomes10:80850cd6c29e 22// I -> 4 bytes ID (chars)
dgomes10:80850cd6c29e 23// S -> 8 bytes SHARED_SECRET (chars)
dgomes10:80850cd6c29e 24
dgomes12:eaee29bfa1c7 25// Generating K and iv:
dgomes12:eaee29bfa1c7 26//openssl enc -aes-128-cbc -pass pass:********** -nosalt -P
dgomes12:eaee29bfa1c7 27
dgomes9:329af8cdc923 28#define SHARED_KEY "figueiredo"
dgomes9:329af8cdc923 29#define DEVICE_NAME "Garagem"
rgrover10:ed5c53845de7 30
rgrover17:c9271599ec5f 31BLE ble;
dgomes9:329af8cdc923 32DigitalOut actuatedLED(P0_19);
dgomes9:329af8cdc923 33Timeout offRelay;
dgomes12:eaee29bfa1c7 34bool boot;
dgomes9:329af8cdc923 35#ifndef DEBUG
dgomes9:329af8cdc923 36DigitalOut relay(P0_8);
dgomes9:329af8cdc923 37#endif
rgrover10:ed5c53845de7 38
dgomes11:3de9b542eeac 39static const uint16_t uuid16_list[] = {GaragemService::GARAGEM_SERVICE_UUID, SecurityService::SECURITY_SERVICE_UUID};
rgrover10:ed5c53845de7 40
dgomes9:329af8cdc923 41GaragemService *garagemServicePtr;
dgomes11:3de9b542eeac 42SecurityService *securityServicePtr;
dgomes9:329af8cdc923 43
dgomes9:329af8cdc923 44void switchOffRelay()
dgomes9:329af8cdc923 45{
dgomes9:329af8cdc923 46 actuatedLED = !actuatedLED;
dgomes9:329af8cdc923 47 DBG("JA CHEGA...\r\n");
dgomes9:329af8cdc923 48 #ifndef DEBUG
dgomes9:329af8cdc923 49 relay = 0;
dgomes9:329af8cdc923 50 #endif
dgomes9:329af8cdc923 51}
dgomes9:329af8cdc923 52
rgrover10:ed5c53845de7 53void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
dgomes9:329af8cdc923 54{
dgomes9:329af8cdc923 55 ble.startAdvertising();
rgrover10:ed5c53845de7 56}
dgomes11:3de9b542eeac 57
rgrover17:c9271599ec5f 58void onDataWrittenCallback(const GattWriteCallbackParams *params) {
dgomes9:329af8cdc923 59 DBG("onDataWrittenCallback: handle = %d len = %d\r\n", params->handle, params->len);
dgomes10:80850cd6c29e 60 garagemServicePtr->nextLastOpen(params->data, params->len);
dgomes10:80850cd6c29e 61
dgomes9:329af8cdc923 62 if ((params->handle == garagemServicePtr->getChallengeHandle()) && (params->len ==16)) {
dgomes10:80850cd6c29e 63 DBG("We have Challenge: data = %s\r\n", (char *) (params->data));
dgomes9:329af8cdc923 64
dgomes9:329af8cdc923 65 uint8_t msg[16];
dgomes11:3de9b542eeac 66 securityServicePtr->decode(msg, (uint8_t *) params->data, 16);
dgomes9:329af8cdc923 67
dgomes9:329af8cdc923 68 if(garagemServicePtr->checkMessage(msg) == GARAGEM_OK) {
dgomes10:80850cd6c29e 69 DBG("ABRE-TE SESAMO!\r\n");
dgomes10:80850cd6c29e 70
dgomes9:329af8cdc923 71 #ifndef DEBUG
dgomes9:329af8cdc923 72 relay = 1;
dgomes9:329af8cdc923 73 #endif
dgomes10:80850cd6c29e 74 actuatedLED = !actuatedLED;
dgomes9:329af8cdc923 75 //please cleanup afterwards
dgomes9:329af8cdc923 76 offRelay.attach(&switchOffRelay, 1.0);
dgomes9:329af8cdc923 77
dgomes9:329af8cdc923 78 } else {
dgomes9:329af8cdc923 79 DBG("NO SUCH LUCK...\r\n");
dgomes9:329af8cdc923 80 }
dgomes12:eaee29bfa1c7 81 } if(boot && (params->handle == securityServicePtr->getKeyHandle()) && (params->len > 0)) {
dgomes11:3de9b542eeac 82 securityServicePtr->setKey((char *) params->data);
dgomes11:3de9b542eeac 83 }
dgomes12:eaee29bfa1c7 84 boot = false;
rgrover10:ed5c53845de7 85}
rgrover10:ed5c53845de7 86
rgrover10:ed5c53845de7 87int main(void)
rgrover10:ed5c53845de7 88{
dgomes9:329af8cdc923 89 DBG("Garagem v0\r\n");
rgrover10:ed5c53845de7 90
dgomes12:eaee29bfa1c7 91 boot = true;
dgomes9:329af8cdc923 92 actuatedLED = true;
dgomes9:329af8cdc923 93 #ifndef DEBUG
dgomes9:329af8cdc923 94 relay = 0;
dgomes9:329af8cdc923 95 #endif
rgrover10:ed5c53845de7 96
rgrover10:ed5c53845de7 97 ble.init();
duststorm15:88ea59cb2b9e 98
duststorm15:88ea59cb2b9e 99 // Note that the Garagem app that is in the Google Play store has the prefix for the BLE Nano hardcoded as F2:9B:4B, but newer Nanos do not have this prefix
duststorm15:88ea59cb2b9e 100 // The following line changes the address on your Nano to ensure it will work with the app as is.
duststorm15:88ea59cb2b9e 101 const uint8_t address1[] = {0x01,0x02,0x03,0x4B,0x9B,0xF2}; ble.setAddress(Gap::ADDR_TYPE_PUBLIC, address1);
duststorm15:88ea59cb2b9e 102
dgomes9:329af8cdc923 103 ble.onDisconnection(disconnectionCallback);
rgrover17:c9271599ec5f 104 ble.gattServer().onDataWritten(onDataWrittenCallback);
rgrover10:ed5c53845de7 105
dgomes9:329af8cdc923 106 /**
dgomes9:329af8cdc923 107 * The Beacon payload has the following composition:
dgomes9:329af8cdc923 108 * 128-Bit / 16byte UUID = E2 0A 39 F4 73 F5 4B C4 A1 2F 17 D1 AD 07 A9 61
dgomes9:329af8cdc923 109 * Major/Minor = 0x1122 / 0x3344
dgomes9:329af8cdc923 110 * Tx Power = 0xC8 = 200, 2's compliment is 256-200 = (-56dB)
dgomes9:329af8cdc923 111 *
dgomes9:329af8cdc923 112 * Note: please remember to calibrate your beacons TX Power for more accurate results.
dgomes9:329af8cdc923 113 */
dgomes9:329af8cdc923 114 const uint8_t uuid[] = {0xE2, 0x0A, 0x39, 0xF4, 0x73, 0xF5, 0x4B, 0xC4,
dgomes9:329af8cdc923 115 0xA1, 0x2F, 0x17, 0xD1, 0xAD, 0x07, 0xA9, 0x61};
dgomes9:329af8cdc923 116 uint16_t majorNumber = 3800;
dgomes9:329af8cdc923 117 uint16_t minorNumber = 1423;
dgomes9:329af8cdc923 118 uint16_t txPower = 0xBE;
dgomes9:329af8cdc923 119 iBeacon ibeacon(ble, uuid, majorNumber, minorNumber, txPower);
dgomes12:eaee29bfa1c7 120
dgomes12:eaee29bfa1c7 121 DeviceInformationService deviceInfo(ble, "diogogomes.com", DEVICE_NAME, "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
dgomes9:329af8cdc923 122
dgomes9:329af8cdc923 123 GaragemService garagemService(ble);
dgomes9:329af8cdc923 124 garagemServicePtr = &garagemService;
dgomes11:3de9b542eeac 125
dgomes12:eaee29bfa1c7 126 SecurityService securityService(ble);
dgomes12:eaee29bfa1c7 127 securityService.init(SHARED_KEY);
dgomes11:3de9b542eeac 128 securityServicePtr = &securityService;
dgomes9:329af8cdc923 129
rgrover10:ed5c53845de7 130 /* setup advertising */
rgrover17:c9271599ec5f 131 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover17:c9271599ec5f 132 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover17:c9271599ec5f 133 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover17:c9271599ec5f 134 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
dgomes9:329af8cdc923 135
dgomes9:329af8cdc923 136
rgrover17:c9271599ec5f 137 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
rgrover17:c9271599ec5f 138 ble.gap().startAdvertising();
rgrover10:ed5c53845de7 139
rgrover10:ed5c53845de7 140 while (true) {
rgrover10:ed5c53845de7 141 ble.waitForEvent();
rgrover10:ed5c53845de7 142 }
rgrover10:ed5c53845de7 143}