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