Button initiated config service

Dependencies:   BLE_API_EddystoneConfigService_2 mbed nRF51822

Fork of BLE_EddystoneBeaconConfigService_3 by URIBeacon

Committer:
mbedAustin
Date:
Mon Sep 21 16:40:02 2015 +0000
Revision:
63:2478203aa555
Parent:
59:a89c00180058
Child:
67:345bc62c748d
updated adv and radio power levels per googles instructions

Who changed what in which revision?

UserRevisionLine numberNew contents of line
screamer 0:c04d932e96c9 1 /* mbed Microcontroller Library
screamer 0:c04d932e96c9 2 * Copyright (c) 2006-2013 ARM Limited
screamer 0:c04d932e96c9 3 *
screamer 0:c04d932e96c9 4 * Licensed under the Apache License, Version 2.0 (the "License");
screamer 0:c04d932e96c9 5 * you may not use this file except in compliance with the License.
screamer 0:c04d932e96c9 6 * You may obtain a copy of the License at
screamer 0:c04d932e96c9 7 *
screamer 0:c04d932e96c9 8 * http://www.apache.org/licenses/LICENSE-2.0
screamer 0:c04d932e96c9 9 *
screamer 0:c04d932e96c9 10 * Unless required by applicable law or agreed to in writing, software
screamer 0:c04d932e96c9 11 * distributed under the License is distributed on an "AS IS" BASIS,
screamer 0:c04d932e96c9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
screamer 0:c04d932e96c9 13 * See the License for the specific language governing permissions and
screamer 0:c04d932e96c9 14 * limitations under the License.
screamer 0:c04d932e96c9 15 */
screamer 0:c04d932e96c9 16
screamer 0:c04d932e96c9 17 #include "mbed.h"
rgrover1 7:e9800c45e065 18 #include "BLE.h"
mbedAustin 23:05e9bb3b13af 19 #include "EddystoneConfigService.h"
mbedAustin 34:5876fbb1aa62 20
rgrover1 2:8020d6d4455a 21 #include "ConfigParamsPersistence.h"
screamer 0:c04d932e96c9 22
rgrover1 8:1a21308e5008 23 BLE ble;
mbedAustin 24:8edfe6730cc2 24 EddystoneConfigService *EddystoneBeaconConfig;
mbedAustin 39:2c73bc1ff4f5 25 EddystoneConfigService::Params_t params;
screamer 0:c04d932e96c9 26
screamer 0:c04d932e96c9 27 /**
screamer 0:c04d932e96c9 28 * URIBeaconConfig service can operate in two modes: a configuration mode which
screamer 0:c04d932e96c9 29 * allows a user to update settings over a connection; and normal URIBeacon mode
screamer 0:c04d932e96c9 30 * which involves advertising a URI. Constructing an object from URIBeaconConfig
screamer 0:c04d932e96c9 31 * service sets up advertisements for the configuration mode. It is then up to
screamer 0:c04d932e96c9 32 * the application to switch to URIBeacon mode based on some timeout.
screamer 0:c04d932e96c9 33 *
screamer 0:c04d932e96c9 34 * The following help with this switch.
screamer 0:c04d932e96c9 35 */
mbedAustin 54:4418b24f2506 36 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 30; // Duration after power-on that config service is available.
mbedAustin 55:3818a9fa827d 37
mbedAustin 24:8edfe6730cc2 38 Ticker configAdvertisementTimeout;
screamer 0:c04d932e96c9 39
screamer 0:c04d932e96c9 40 /**
mbedAustin 19:f7c33fa88ca5 41 * Stop advertising the Config Service after a delay; and switch to a non-connectable advertising mode only beacon.
screamer 0:c04d932e96c9 42 */
screamer 0:c04d932e96c9 43 void timeout(void)
screamer 0:c04d932e96c9 44 {
screamer 0:c04d932e96c9 45 Gap::GapState_t state;
screamer 0:c04d932e96c9 46 state = ble.getGapState();
screamer 0:c04d932e96c9 47 if (!state.connected) { /* don't switch if we're in a connected state. */
mbedAustin 24:8edfe6730cc2 48 configAdvertisementTimeout.detach(); /* disable the callback from the timeout Ticker. */
mbedAustin 50:52a8b18d5bf1 49 EddystoneBeaconConfig->setupEddystoneAdvertisements();
screamer 0:c04d932e96c9 50 }
screamer 0:c04d932e96c9 51 }
screamer 0:c04d932e96c9 52
screamer 0:c04d932e96c9 53 /**
mbedAustin 40:6f39aee31205 54 * Callback triggered upon a disconnection event.
screamer 0:c04d932e96c9 55 */
screamer 0:c04d932e96c9 56 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
screamer 0:c04d932e96c9 57 {
mbedAustin 39:2c73bc1ff4f5 58 if (true == params.isConfigured){
mbedAustin 39:2c73bc1ff4f5 59 // end advertising, the beacon is configured
mbedAustin 39:2c73bc1ff4f5 60 timeout();
mbedAustin 39:2c73bc1ff4f5 61 }
mbedAustin 39:2c73bc1ff4f5 62 else{
mbedAustin 40:6f39aee31205 63 // eddystone is not configured, continue advertising
mbedAustin 39:2c73bc1ff4f5 64 ble.gap().startAdvertising();
mbedAustin 39:2c73bc1ff4f5 65 }
screamer 0:c04d932e96c9 66 }
screamer 0:c04d932e96c9 67
screamer 0:c04d932e96c9 68 int main(void)
screamer 0:c04d932e96c9 69 {
screamer 0:c04d932e96c9 70 ble.init();
rgrover1 8:1a21308e5008 71 ble.gap().onDisconnection(disconnectionCallback);
screamer 0:c04d932e96c9 72
screamer 0:c04d932e96c9 73 /*
screamer 0:c04d932e96c9 74 * Load parameters from (platform specific) persistent storage. Parameters
screamer 0:c04d932e96c9 75 * can be set to non-default values while the URIBeacon is in configuration
screamer 0:c04d932e96c9 76 * mode (within the first 60 seconds of power-up). Thereafter, parameters
screamer 0:c04d932e96c9 77 * get copied out to persistent storage before switching to normal URIBeacon
screamer 0:c04d932e96c9 78 * operation.
screamer 0:c04d932e96c9 79 */
rgrover1 2:8020d6d4455a 80 bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(&params);
mbedAustin 44:0e27ee7800b8 81
mbedAustin 44:0e27ee7800b8 82 // Set UID and TLM frame data
mbedAustin 31:11e7a505a4be 83 EddystoneConfigService::UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; // 10Byte Namespace UUID
mbedAustin 31:11e7a505a4be 84 EddystoneConfigService::UIDInstanceID_t uidInstanceID = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // 6Byte Instance ID
mbedAustin 31:11e7a505a4be 85 uint8_t tlmVersion = 0x00;
mbedAustin 31:11e7a505a4be 86
mbedAustin 24:8edfe6730cc2 87 /* Initialize a EddystoneBeaconConfig service providing config params, default URI, and power levels. */
mbedAustin 63:2478203aa555 88 static EddystoneConfigService::PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m
mbedAustin 63:2478203aa555 89 static EddystoneConfigService::PowerLevels_t radioPowerLevels = {-30, -16, -4, 4}; // Values for radio power levels, provided by manufacturer.
mbedAustin 55:3818a9fa827d 90 EddystoneBeaconConfig = new EddystoneConfigService(ble, params, defaultAdvPowerLevels, radioPowerLevels);
mbedAustin 63:2478203aa555 91 EddystoneBeaconConfig->setDefaultURIFrameData("http://mbed.org",2);
mbedAustin 63:2478203aa555 92 EddystoneBeaconConfig->setDefaultUIDFrameData(&uidNamespaceID, &uidInstanceID,5);
mbedAustin 63:2478203aa555 93 EddystoneBeaconConfig->setDefaultTLMFrameData(tlmVersion,10);
mbedAustin 44:0e27ee7800b8 94 EddystoneBeaconConfig->start(!fetchedFromPersistentStorage);
mbedAustin 44:0e27ee7800b8 95
mbedAustin 39:2c73bc1ff4f5 96 if (!EddystoneBeaconConfig->initSuccessfully()) {
screamer 0:c04d932e96c9 97 error("failed to accommodate URI");
screamer 0:c04d932e96c9 98 }
mbedAustin 24:8edfe6730cc2 99 configAdvertisementTimeout.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
screamer 0:c04d932e96c9 100
mbedAustin 24:8edfe6730cc2 101 ble.gap().startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneBeaconConfig
rgrover1 8:1a21308e5008 102 * service. This can then be switched to the normal URIBeacon functionality after a timeout. */
screamer 0:c04d932e96c9 103 while (true) {
screamer 0:c04d932e96c9 104 ble.waitForEvent();
screamer 0:c04d932e96c9 105 }
screamer 0:c04d932e96c9 106 }