Sample program
Dependencies: BLE_API mbed nRF51822
Fork of mbed_EddystoneURL_Beacon_ssci by
main.cpp@0:094734b4e8bd, 2015-08-17 (annotated)
- Committer:
- roywant
- Date:
- Mon Aug 17 04:41:01 2015 +0000
- Revision:
- 0:094734b4e8bd
- Child:
- 1:0ad7573b5918
Original example taking from BLE_URIBeacon
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
roywant | 0:094734b4e8bd | 1 | /* mbed Microcontroller Library |
roywant | 0:094734b4e8bd | 2 | * Copyright (c) 2006-2013 ARM Limited |
roywant | 0:094734b4e8bd | 3 | * |
roywant | 0:094734b4e8bd | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
roywant | 0:094734b4e8bd | 5 | * you may not use this file except in compliance with the License. |
roywant | 0:094734b4e8bd | 6 | * You may obtain a copy of the License at |
roywant | 0:094734b4e8bd | 7 | * |
roywant | 0:094734b4e8bd | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
roywant | 0:094734b4e8bd | 9 | * |
roywant | 0:094734b4e8bd | 10 | * Unless required by applicable law or agreed to in writing, software |
roywant | 0:094734b4e8bd | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
roywant | 0:094734b4e8bd | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
roywant | 0:094734b4e8bd | 13 | * See the License for the specific language governing permissions and |
roywant | 0:094734b4e8bd | 14 | * limitations under the License. |
roywant | 0:094734b4e8bd | 15 | */ |
roywant | 0:094734b4e8bd | 16 | |
roywant | 0:094734b4e8bd | 17 | #include "mbed.h" |
roywant | 0:094734b4e8bd | 18 | #include "ble/BLE.h" |
roywant | 0:094734b4e8bd | 19 | #include "ble/services/URIBeaconConfigService.h" |
roywant | 0:094734b4e8bd | 20 | #include "ble/services/DFUService.h" |
roywant | 0:094734b4e8bd | 21 | #include "ble/services/DeviceInformationService.h" |
roywant | 0:094734b4e8bd | 22 | #include "ConfigParamsPersistence.h" |
roywant | 0:094734b4e8bd | 23 | |
roywant | 0:094734b4e8bd | 24 | BLE ble; |
roywant | 0:094734b4e8bd | 25 | URIBeaconConfigService *uriBeaconConfig; |
roywant | 0:094734b4e8bd | 26 | |
roywant | 0:094734b4e8bd | 27 | /** |
roywant | 0:094734b4e8bd | 28 | * URIBeaconConfig service can operate in two modes: a configuration mode which |
roywant | 0:094734b4e8bd | 29 | * allows a user to update settings over a connection; and normal URIBeacon mode |
roywant | 0:094734b4e8bd | 30 | * which involves advertising a URI. Constructing an object from URIBeaconConfig |
roywant | 0:094734b4e8bd | 31 | * service sets up advertisements for the configuration mode. It is then up to |
roywant | 0:094734b4e8bd | 32 | * the application to switch to URIBeacon mode based on some timeout. |
roywant | 0:094734b4e8bd | 33 | * |
roywant | 0:094734b4e8bd | 34 | * The following help with this switch. |
roywant | 0:094734b4e8bd | 35 | */ |
roywant | 0:094734b4e8bd | 36 | static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 60; // Duration after power-on that config service is available. |
roywant | 0:094734b4e8bd | 37 | Ticker configAdvertisementTimeoutTicker; |
roywant | 0:094734b4e8bd | 38 | |
roywant | 0:094734b4e8bd | 39 | /** |
roywant | 0:094734b4e8bd | 40 | * Stop advertising the UriBeaconConfig Service after a delay; and switch to normal URIBeacon. |
roywant | 0:094734b4e8bd | 41 | */ |
roywant | 0:094734b4e8bd | 42 | void timeout(void) |
roywant | 0:094734b4e8bd | 43 | { |
roywant | 0:094734b4e8bd | 44 | Gap::GapState_t state; |
roywant | 0:094734b4e8bd | 45 | state = ble.getGapState(); |
roywant | 0:094734b4e8bd | 46 | if (!state.connected) { /* don't switch if we're in a connected state. */ |
roywant | 0:094734b4e8bd | 47 | uriBeaconConfig->setupURIBeaconAdvertisements(); |
roywant | 0:094734b4e8bd | 48 | ble.startAdvertising(); |
roywant | 0:094734b4e8bd | 49 | |
roywant | 0:094734b4e8bd | 50 | configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */ |
roywant | 0:094734b4e8bd | 51 | } |
roywant | 0:094734b4e8bd | 52 | } |
roywant | 0:094734b4e8bd | 53 | |
roywant | 0:094734b4e8bd | 54 | /** |
roywant | 0:094734b4e8bd | 55 | * Callback triggered upon a disconnection event. Needs to re-enable advertisements. |
roywant | 0:094734b4e8bd | 56 | */ |
roywant | 0:094734b4e8bd | 57 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
roywant | 0:094734b4e8bd | 58 | { |
roywant | 0:094734b4e8bd | 59 | ble.startAdvertising(); |
roywant | 0:094734b4e8bd | 60 | } |
roywant | 0:094734b4e8bd | 61 | |
roywant | 0:094734b4e8bd | 62 | int main(void) |
roywant | 0:094734b4e8bd | 63 | { |
roywant | 0:094734b4e8bd | 64 | ble.init(); |
roywant | 0:094734b4e8bd | 65 | ble.onDisconnection(disconnectionCallback); |
roywant | 0:094734b4e8bd | 66 | |
roywant | 0:094734b4e8bd | 67 | /* |
roywant | 0:094734b4e8bd | 68 | * Load parameters from (platform specific) persistent storage. Parameters |
roywant | 0:094734b4e8bd | 69 | * can be set to non-default values while the URIBeacon is in configuration |
roywant | 0:094734b4e8bd | 70 | * mode (within the first 60 seconds of power-up). Thereafter, parameters |
roywant | 0:094734b4e8bd | 71 | * get copied out to persistent storage before switching to normal URIBeacon |
roywant | 0:094734b4e8bd | 72 | * operation. |
roywant | 0:094734b4e8bd | 73 | */ |
roywant | 0:094734b4e8bd | 74 | URIBeaconConfigService::Params_t params; |
roywant | 0:094734b4e8bd | 75 | bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(¶ms); |
roywant | 0:094734b4e8bd | 76 | |
roywant | 0:094734b4e8bd | 77 | /* Initialize a URIBeaconConfig service providing config params, default URI, and power levels. */ |
roywant | 0:094734b4e8bd | 78 | static URIBeaconConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels |
roywant | 0:094734b4e8bd | 79 | uriBeaconConfig = new URIBeaconConfigService(ble, params, !fetchedFromPersistentStorage, "http://uribeacon.org", defaultAdvPowerLevels); |
roywant | 0:094734b4e8bd | 80 | if (!uriBeaconConfig->configuredSuccessfully()) { |
roywant | 0:094734b4e8bd | 81 | error("failed to accommodate URI"); |
roywant | 0:094734b4e8bd | 82 | } |
roywant | 0:094734b4e8bd | 83 | configAdvertisementTimeoutTicker.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS); |
roywant | 0:094734b4e8bd | 84 | |
roywant | 0:094734b4e8bd | 85 | // Setup auxiliary services to allow over-the-air firmware updates, etc |
roywant | 0:094734b4e8bd | 86 | DFUService dfu(ble); |
roywant | 0:094734b4e8bd | 87 | DeviceInformationService deviceInfo(ble, "ARM", "UriBeacon", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); |
roywant | 0:094734b4e8bd | 88 | |
roywant | 0:094734b4e8bd | 89 | ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the URIBeaconConfig |
roywant | 0:094734b4e8bd | 90 | * service. This can then be switched to the normal URIBeacon functionality after a timeout. */ |
roywant | 0:094734b4e8bd | 91 | |
roywant | 0:094734b4e8bd | 92 | while (true) { |
roywant | 0:094734b4e8bd | 93 | ble.waitForEvent(); |
roywant | 0:094734b4e8bd | 94 | } |
roywant | 0:094734b4e8bd | 95 | } |