Austin Blackstone / Mbed 2 deprecated BLE_EddystoneBeaconConfigServiceRelease

Dependencies:   BLE_API mbed nRF51822

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/EddystoneConfigService.h"
00020 #include "ConfigParamsPersistence.h"
00021 
00022 BLE ble;
00023 EddystoneConfigService *EddystoneBeaconConfig;
00024 EddystoneConfigService::Params_t params;
00025 
00026 /**
00027  * URIBeaconConfig service can operate in two modes: a configuration mode which
00028  * allows a user to update settings over a connection; and normal URIBeacon mode
00029  * which involves advertising a URI. Constructing an object from URIBeaconConfig
00030  * service sets up advertisements for the configuration mode. It is then up to
00031  * the application to switch to URIBeacon mode based on some timeout.
00032  *
00033  * The following help with this switch.
00034  */
00035 static const int CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS = 30;  // Duration after power-on that config service is available.
00036 
00037 Ticker configAdvertisementTimeout;
00038 
00039 /**
00040  * Stop advertising the Config Service after a delay; and switch to a non-connectable advertising mode only beacon.
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         configAdvertisementTimeout.detach(); /* disable the callback from the timeout Ticker. */
00048         EddystoneBeaconConfig->setupEddystoneAdvertisements();        
00049     }
00050 }
00051 
00052 /**
00053  * Callback triggered upon a disconnection event.
00054  */
00055 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00056 {
00057     if (true == params.isConfigured){
00058         // end advertising, the beacon is configured
00059         timeout();
00060     }
00061     else{
00062         // eddystone is not configured, continue advertising    
00063         ble.gap().startAdvertising();
00064     }
00065 }
00066 
00067 int main(void)
00068 {
00069     ble.init();
00070     ble.gap().onDisconnection(disconnectionCallback);
00071 
00072     /*
00073      * Load parameters from (platform specific) persistent storage. Parameters
00074      * can be set to non-default values while the URIBeacon is in configuration
00075      * mode (within the first 60 seconds of power-up). Thereafter, parameters
00076      * get copied out to persistent storage before switching to normal URIBeacon
00077      * operation.
00078      */
00079     bool fetchedFromPersistentStorage = loadURIBeaconConfigParams(&params);
00080     
00081     // Set UID and TLM frame data
00082     EddystoneConfigService::UIDNamespaceID_t uidNamespaceID = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99}; // 10Byte Namespace UUID
00083     EddystoneConfigService::UIDInstanceID_t  uidInstanceID = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}; // 6Byte Instance ID
00084     uint8_t tlmVersion = 0x00;
00085     
00086     /* Initialize a EddystoneBeaconConfig service providing config params, default URI, and power levels. */
00087     static EddystoneConfigService::PowerLevels_t defaultAdvPowerLevels = {-47, -33, -21, -13}; // Values for ADV packets related to firmware levels, calibrated based on measured values at 1m
00088     static EddystoneConfigService::PowerLevels_t radioPowerLevels =      {-30, -16, -4, 4};    // Values for radio power levels, provided by manufacturer.
00089     
00090     // Create Eddystone Config Service object
00091     EddystoneBeaconConfig = new EddystoneConfigService(ble, params, defaultAdvPowerLevels, radioPowerLevels);
00092     
00093     // Set default URI, UID and TLM frame data if not initialized through the config service
00094     EddystoneBeaconConfig->setDefaultURIFrameData("http://mbed.org",2);
00095     EddystoneBeaconConfig->setDefaultUIDFrameData(&uidNamespaceID, &uidInstanceID,5);
00096     EddystoneBeaconConfig->setDefaultTLMFrameData(tlmVersion,10);
00097     
00098     // start the config service
00099     EddystoneBeaconConfig->start(!fetchedFromPersistentStorage);
00100     
00101     if (!EddystoneBeaconConfig->initSuccessfully()) {
00102         error("failed to accommodate URI");
00103     }
00104     configAdvertisementTimeout.attach(timeout, CONFIG_ADVERTISEMENT_TIMEOUT_SECONDS);
00105 
00106     ble.gap().startAdvertising(); /* Set the whole thing in motion. After this call a GAP central can scan the EddystoneBeaconConfig
00107                                    * service. This can then be switched to the normal URIBeacon functionality after a timeout. */
00108     while (true) {
00109         ble.waitForEvent();
00110     }
00111 }