Changed to work with St Board

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Fork of BLE_URIBeacon by Bluetooth Low Energy

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2013 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "ble/BLE.h"
00019 #include "ble/services/URIBeaconConfigService.h"
00020 #include "ble/services/DFUService.h"
00021 #include "ble/services/DeviceInformationService.h"
00022 #include "ConfigParamsPersistence.h"
00023 
00024 BLE ble;
00025 URIBeaconConfigService *uriBeaconConfig;
00026 
00027 /**
00028  * URIBeaconConfig service can operate in two modes: a configuration mode which
00029  * allows a user to update settings over a connection; and normal URIBeacon mode
00030  * which involves advertising a URI. Constructing an object from URIBeaconConfig
00031  * service sets up advertisements for the configuration mode. It is then up to
00032  * the application to switch to URIBeacon mode based on some timeout.
00033  *
00034  * The following help with this switch.
00035  */
00036 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 60;  // Duration after power-on that config service is available.
00037 Ticker configAdvertisementTimeoutTicker;
00038 
00039 /**
00040  * Stop advertising the UriBeaconConfig Service after a delay; and switch to normal URIBeacon.
00041  */
00042 void timeout(void)
00043 {
00044     Gap::GapState_t state;
00045     state = ble.getGapState();
00046     if (!state.connected) { /* don't switch if we're in a connected state. */
00047         uriBeaconConfig->setupURIBeaconAdvertisements();
00048         ble.startAdvertising();
00049 
00050         configAdvertisementTimeoutTicker.detach(); /* disable the callback from the timeout Ticker. */
00051     }
00052 }
00053 
00054 /**
00055  * Callback triggered upon a disconnection event. Needs to re-enable advertisements.
00056  */
00057 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00058 {
00059     ble.startAdvertising();
00060 }
00061 
00062 int main(void)
00063 {
00064     ble.init();
00065     ble.onDisconnection(disconnectionCallback);
00066 
00067     /*
00068      * Load parameters from (platform specific) persistent storage. Parameters
00069      * can be set to non-default values while the URIBeacon is in configuration
00070      * mode (within the first 60 seconds of power-up). Thereafter, parameters
00071      * get copied out to persistent storage before switching to normal URIBeacon
00072      * operation.
00073      */
00074     URIBeaconConfigService::Params_t params;
00075     bool fetchedFromPersistentStorage = false;
00076 
00077     /* Initialize a URIBeaconConfig service providing config params, default URI, and power levels. */
00078     static URIBeaconConfigService::PowerLevels_t defaultAdvPowerLevels = {-20, -4, 0, 10}; // Values for ADV packets related to firmware levels
00079     uriBeaconConfig = new URIBeaconConfigService(ble, params, !fetchedFromPersistentStorage, "http://st.com", defaultAdvPowerLevels);
00080     if (!uriBeaconConfig->configuredSuccessfully()) {
00081         error("failed to accommodate URI");
00082     }
00083     //configAdvertisementTimeoutTicker.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
00084 
00085     // Setup auxiliary services to allow over-the-air firmware updates, etc
00086     //DFUService dfu(ble);
00087     DeviceInformationService deviceInfo(ble, "ARM", "UriBeacon", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
00088 
00089     ble.startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the URIBeaconConfig
00090                              * service. This can then be switched to the normal URIBeacon functionality after a timeout. */
00091 
00092     while (true) {
00093         ble.waitForEvent();
00094     }
00095 }