Button initiated config service
Dependencies: BLE_API_EddystoneConfigService_2 mbed nRF51822
Fork of BLE_EddystoneBeaconConfigService_3 by
main.cpp@37:22175faa76be, 2015-09-03 (annotated)
- Committer:
- mbedAustin
- Date:
- Thu Sep 03 02:55:49 2015 +0000
- Revision:
- 37:22175faa76be
- Parent:
- 36:ccb188bcfef1
- Child:
- 38:8fdb16f67e7c
changed thing to make UID's work correctly;
Who changed what in which revision?
User | Revision | Line number | New 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; |
screamer | 0:c04d932e96c9 | 25 | |
screamer | 0:c04d932e96c9 | 26 | /** |
screamer | 0:c04d932e96c9 | 27 | * URIBeaconConfig service can operate in two modes: a configuration mode which |
screamer | 0:c04d932e96c9 | 28 | * allows a user to update settings over a connection; and normal URIBeacon mode |
screamer | 0:c04d932e96c9 | 29 | * which involves advertising a URI. Constructing an object from URIBeaconConfig |
screamer | 0:c04d932e96c9 | 30 | * service sets up advertisements for the configuration mode. It is then up to |
screamer | 0:c04d932e96c9 | 31 | * the application to switch to URIBeacon mode based on some timeout. |
screamer | 0:c04d932e96c9 | 32 | * |
screamer | 0:c04d932e96c9 | 33 | * The following help with this switch. |
screamer | 0:c04d932e96c9 | 34 | */ |
mbedAustin | 37:22175faa76be | 35 | static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 5; // Duration after power-on that config service is available. |
mbedAustin | 24:8edfe6730cc2 | 36 | Ticker configAdvertisementTimeout; |
screamer | 0:c04d932e96c9 | 37 | |
screamer | 0:c04d932e96c9 | 38 | /** |
mbedAustin | 19:f7c33fa88ca5 | 39 | * Stop advertising the Config Service after a delay; and switch to a non-connectable advertising mode only beacon. |
screamer | 0:c04d932e96c9 | 40 | */ |
screamer | 0:c04d932e96c9 | 41 | void timeout(void) |
screamer | 0:c04d932e96c9 | 42 | { |
screamer | 0:c04d932e96c9 | 43 | Gap::GapState_t state; |
screamer | 0:c04d932e96c9 | 44 | state = ble.getGapState(); |
screamer | 0:c04d932e96c9 | 45 | if (!state.connected) { /* don't switch if we're in a connected state. */ |
mbedAustin | 24:8edfe6730cc2 | 46 | EddystoneBeaconConfig->setupEddystoneAdvertisements(); |
mbedAustin | 24:8edfe6730cc2 | 47 | configAdvertisementTimeout.detach(); /* disable the callback from the timeout Ticker. */ |
mbedAustin | 15:af8c24f34a9f | 48 | printf("removing config service\r\n"); |
screamer | 0:c04d932e96c9 | 49 | } |
screamer | 0:c04d932e96c9 | 50 | } |
screamer | 0:c04d932e96c9 | 51 | |
screamer | 0:c04d932e96c9 | 52 | /** |
screamer | 0:c04d932e96c9 | 53 | * Callback triggered upon a disconnection event. Needs to re-enable advertisements. |
screamer | 0:c04d932e96c9 | 54 | */ |
screamer | 0:c04d932e96c9 | 55 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
screamer | 0:c04d932e96c9 | 56 | { |
rgrover1 | 8:1a21308e5008 | 57 | ble.gap().startAdvertising(); |
mbedAustin | 15:af8c24f34a9f | 58 | printf("disconnect CB\r\n"); |
screamer | 0:c04d932e96c9 | 59 | } |
screamer | 0:c04d932e96c9 | 60 | |
screamer | 0:c04d932e96c9 | 61 | int main(void) |
screamer | 0:c04d932e96c9 | 62 | { |
mbedAustin | 15:af8c24f34a9f | 63 | printf("Starting Example\r\n"); |
screamer | 0:c04d932e96c9 | 64 | ble.init(); |
rgrover1 | 8:1a21308e5008 | 65 | ble.gap().onDisconnection(disconnectionCallback); |
screamer | 0:c04d932e96c9 | 66 | |
screamer | 0:c04d932e96c9 | 67 | /* |
screamer | 0:c04d932e96c9 | 68 | * Load parameters from (platform specific) persistent storage. Parameters |
screamer | 0:c04d932e96c9 | 69 | * can be set to non-default values while the URIBeacon is in configuration |
screamer | 0:c04d932e96c9 | 70 | * mode (within the first 60 seconds of power-up). Thereafter, parameters |
screamer | 0:c04d932e96c9 | 71 | * get copied out to persistent storage before switching to normal URIBeacon |
screamer | 0:c04d932e96c9 | 72 | * operation. |
screamer | 0:c04d932e96c9 | 73 | */ |
mbedAustin | 23:05e9bb3b13af | 74 | EddystoneConfigService::Params_t params; |
rgrover1 | 2:8020d6d4455a | 75 | bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(¶ms); |
mbedAustin | 31:11e7a505a4be | 76 | EddystoneConfigService::UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; // 10Byte Namespace UUID |
mbedAustin | 31:11e7a505a4be | 77 | EddystoneConfigService::UIDInstanceID_t uidInstanceID = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // 6Byte Instance ID |
mbedAustin | 31:11e7a505a4be | 78 | uint8_t tlmVersion = 0x00; |
mbedAustin | 31:11e7a505a4be | 79 | |
mbedAustin | 24:8edfe6730cc2 | 80 | /* Initialize a EddystoneBeaconConfig service providing config params, default URI, and power levels. */ |
mbedAustin | 23:05e9bb3b13af | 81 | static EddystoneConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels |
mbedAustin | 34:5876fbb1aa62 | 82 | EddystoneBeaconConfig = new EddystoneConfigService(ble, params, !fetchedFromPersistentStorage, defaultAdvPowerLevels, "http://mbed.org", &uidNamespaceID, &uidInstanceID, tlmVersion); |
mbedAustin | 24:8edfe6730cc2 | 83 | if (!EddystoneBeaconConfig->configuredSuccessfully()) { |
screamer | 0:c04d932e96c9 | 84 | error("failed to accommodate URI"); |
screamer | 0:c04d932e96c9 | 85 | } |
mbedAustin | 24:8edfe6730cc2 | 86 | configAdvertisementTimeout.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS); |
screamer | 0:c04d932e96c9 | 87 | |
mbedAustin | 24:8edfe6730cc2 | 88 | ble.gap().startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneBeaconConfig |
rgrover1 | 8:1a21308e5008 | 89 | * service. This can then be switched to the normal URIBeacon functionality after a timeout. */ |
mbedAustin | 15:af8c24f34a9f | 90 | printf("Running...\r\n"); |
screamer | 0:c04d932e96c9 | 91 | while (true) { |
screamer | 0:c04d932e96c9 | 92 | ble.waitForEvent(); |
screamer | 0:c04d932e96c9 | 93 | } |
screamer | 0:c04d932e96c9 | 94 | } |