Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
main.cpp@8:b816829fb712, 2014-12-03 (annotated)
- Committer:
- rgrover1
- Date:
- Wed Dec 03 14:48:28 2014 +0000
- Revision:
- 8:b816829fb712
- Parent:
- 7:6faec5978fb4
config service remains avaialble for only 30 seconds.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| rgrover1 | 0:790a27ffc99b | 1 | /* mbed Microcontroller Library | 
| rgrover1 | 0:790a27ffc99b | 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 | 0:790a27ffc99b | 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 | 7:6faec5978fb4 | 19 | #include "URIBeaconConfigService.h" | 
| rgrover1 | 6:31b65d4ea67d | 20 | #include "DFUService.h" | 
| rgrover1 | 6:31b65d4ea67d | 21 | #include "DeviceInformationService.h" | 
| rgrover1 | 0:790a27ffc99b | 22 | |
| rgrover1 | 0:790a27ffc99b | 23 | BLEDevice ble; | 
| rgrover1 | 8:b816829fb712 | 24 | URIBeaconConfigService *uriBeaconConfig; | 
| rgrover1 | 0:790a27ffc99b | 25 | |
| rgrover1 | 6:31b65d4ea67d | 26 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) | 
| rgrover1 | 6:31b65d4ea67d | 27 | { | 
| rgrover1 | 6:31b65d4ea67d | 28 | ble.startAdvertising(); | 
| rgrover1 | 6:31b65d4ea67d | 29 | } | 
| rgrover1 | 0:790a27ffc99b | 30 | |
| rgrover1 | 8:b816829fb712 | 31 | void switchCallback(void) | 
| rgrover1 | 8:b816829fb712 | 32 | { | 
| rgrover1 | 8:b816829fb712 | 33 | static bool switched = false; | 
| rgrover1 | 8:b816829fb712 | 34 | if (!switched) { | 
| rgrover1 | 8:b816829fb712 | 35 | printf("executing switch\r\n"); | 
| rgrover1 | 8:b816829fb712 | 36 | delete uriBeaconConfig; | 
| rgrover1 | 8:b816829fb712 | 37 | uriBeaconConfig = NULL; | 
| rgrover1 | 8:b816829fb712 | 38 | |
| rgrover1 | 8:b816829fb712 | 39 | static const uint8_t BEACON_UUID[] = {0xD8, 0xFE}; | 
| rgrover1 | 8:b816829fb712 | 40 | static const uint8_t urldata[] = { | 
| rgrover1 | 8:b816829fb712 | 41 | BEACON_UUID[0], | 
| rgrover1 | 8:b816829fb712 | 42 | BEACON_UUID[1], | 
| rgrover1 | 8:b816829fb712 | 43 | 0x00, // flags | 
| rgrover1 | 8:b816829fb712 | 44 | 0x20, // power | 
| rgrover1 | 8:b816829fb712 | 45 | 0x00, // http://www. | 
| rgrover1 | 8:b816829fb712 | 46 | 'm', | 
| rgrover1 | 8:b816829fb712 | 47 | 'b', | 
| rgrover1 | 8:b816829fb712 | 48 | 'e', | 
| rgrover1 | 8:b816829fb712 | 49 | 'd', | 
| rgrover1 | 8:b816829fb712 | 50 | 0x08, // .".org" | 
| rgrover1 | 8:b816829fb712 | 51 | }; | 
| rgrover1 | 8:b816829fb712 | 52 | |
| rgrover1 | 8:b816829fb712 | 53 | ble.shutdown(); | 
| rgrover1 | 8:b816829fb712 | 54 | ble.init(); | 
| rgrover1 | 8:b816829fb712 | 55 | |
| rgrover1 | 8:b816829fb712 | 56 | ble.clearAdvertisingPayload(); | 
| rgrover1 | 8:b816829fb712 | 57 | ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, BEACON_UUID, sizeof(BEACON_UUID)); | 
| rgrover1 | 8:b816829fb712 | 58 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, urldata, sizeof(urldata)); | 
| rgrover1 | 8:b816829fb712 | 59 | |
| rgrover1 | 8:b816829fb712 | 60 | ble.setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); | 
| rgrover1 | 8:b816829fb712 | 61 | ble.setAdvertisingInterval(1600); /* 1s; in multiples of 0.625ms. */ | 
| rgrover1 | 8:b816829fb712 | 62 | ble.startAdvertising(); | 
| rgrover1 | 8:b816829fb712 | 63 | |
| rgrover1 | 8:b816829fb712 | 64 | switched = true; | 
| rgrover1 | 8:b816829fb712 | 65 | } | 
| rgrover1 | 8:b816829fb712 | 66 | } | 
| rgrover1 | 8:b816829fb712 | 67 | |
| rgrover1 | 0:790a27ffc99b | 68 | int main(void) | 
| rgrover1 | 0:790a27ffc99b | 69 | { | 
| rgrover1 | 8:b816829fb712 | 70 | Ticker ticker; | 
| rgrover1 | 8:b816829fb712 | 71 | ticker.attach(switchCallback, 30); | 
| rgrover1 | 8:b816829fb712 | 72 | |
| rgrover1 | 0:790a27ffc99b | 73 | ble.init(); | 
| rgrover1 | 6:31b65d4ea67d | 74 | ble.onDisconnection(disconnectionCallback); | 
| rgrover1 | 0:790a27ffc99b | 75 | |
| rgrover1 | 8:b816829fb712 | 76 | uriBeaconConfig = new URIBeaconConfigService(ble, "http://www.mbed.org"); | 
| rgrover1 | 8:b816829fb712 | 77 | if (!uriBeaconConfig->configuredSuccessfully()) { | 
| rgrover1 | 6:31b65d4ea67d | 78 | error("failed to accommodate URI"); | 
| rgrover1 | 6:31b65d4ea67d | 79 | } | 
| rgrover1 | 7:6faec5978fb4 | 80 | /* optional use of the API offered by URIBeaconConfigService */ | 
| rgrover1 | 7:6faec5978fb4 | 81 | const int8_t powerLevels[] = {-20, -4, 0, 10}; | 
| rgrover1 | 8:b816829fb712 | 82 | uriBeaconConfig->setTxPowerLevels(powerLevels); | 
| rgrover1 | 8:b816829fb712 | 83 | uriBeaconConfig->setTxPowerMode(URIBeaconConfigService::TX_POWER_MODE_LOW); | 
| rgrover1 | 0:790a27ffc99b | 84 | |
| rgrover1 | 6:31b65d4ea67d | 85 | /* Setup auxiliary services. */ | 
| rgrover1 | 6:31b65d4ea67d | 86 | DFUService dfu(ble); /* To allow over-the-air firmware udpates. optional. */ | 
| rgrover1 | 6:31b65d4ea67d | 87 | DeviceInformationService deviceInfo(ble, "ARM", "URIBeacon2", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); /* optional */ | 
| rgrover1 | 6:31b65d4ea67d | 88 | |
| rgrover1 | 0:790a27ffc99b | 89 | ble.startAdvertising(); | 
| rgrover1 | 0:790a27ffc99b | 90 | |
| rgrover1 | 0:790a27ffc99b | 91 | while (true) { | 
| rgrover1 | 0:790a27ffc99b | 92 | ble.waitForEvent(); | 
| rgrover1 | 0:790a27ffc99b | 93 | } | 
| rgrover1 | 0:790a27ffc99b | 94 | } |