ok

Dependencies:   BLE_API mbed nRF51822

Committer:
jjowell
Date:
Thu Jul 28 19:40:17 2016 +0000
Revision:
0:148b561040e4
ok

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jjowell 0:148b561040e4 1 /* mbed Microcontroller Library
jjowell 0:148b561040e4 2 * Copyright (c) 2006-2013 ARM Limited
jjowell 0:148b561040e4 3 *
jjowell 0:148b561040e4 4 * Licensed under the Apache License, Version 2.0 (the "License");
jjowell 0:148b561040e4 5 * you may not use this file except in compliance with the License.
jjowell 0:148b561040e4 6 * You may obtain a copy of the License at
jjowell 0:148b561040e4 7 *
jjowell 0:148b561040e4 8 * http://www.apache.org/licenses/LICENSE-2.0
jjowell 0:148b561040e4 9 *
jjowell 0:148b561040e4 10 * Unless required by applicable law or agreed to in writing, software
jjowell 0:148b561040e4 11 * distributed under the License is distributed on an "AS IS" BASIS,
jjowell 0:148b561040e4 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jjowell 0:148b561040e4 13 * See the License for the specific language governing permissions and
jjowell 0:148b561040e4 14 * limitations under the License.
jjowell 0:148b561040e4 15 */
jjowell 0:148b561040e4 16
jjowell 0:148b561040e4 17 #include "mbed.h"
jjowell 0:148b561040e4 18 #include "ble/BLE.h"
jjowell 0:148b561040e4 19 #include "LEDService.h"
jjowell 0:148b561040e4 20
jjowell 0:148b561040e4 21 DigitalOut alivenessLED(LED1, 0);
jjowell 0:148b561040e4 22 DigitalOut actuatedLED(LED2, 0);
jjowell 0:148b561040e4 23
jjowell 0:148b561040e4 24 const static char DEVICE_NAME[] = "LED";
jjowell 0:148b561040e4 25 static const uint16_t uuid16_list[] = {LEDService::LED_SERVICE_UUID};
jjowell 0:148b561040e4 26
jjowell 0:148b561040e4 27 LEDService *ledServicePtr;
jjowell 0:148b561040e4 28
jjowell 0:148b561040e4 29 Ticker ticker;
jjowell 0:148b561040e4 30
jjowell 0:148b561040e4 31 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
jjowell 0:148b561040e4 32 {
jjowell 0:148b561040e4 33 BLE::Instance().gap().startAdvertising();
jjowell 0:148b561040e4 34 }
jjowell 0:148b561040e4 35
jjowell 0:148b561040e4 36 void periodicCallback(void)
jjowell 0:148b561040e4 37 {
jjowell 0:148b561040e4 38 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
jjowell 0:148b561040e4 39 }
jjowell 0:148b561040e4 40
jjowell 0:148b561040e4 41 /**
jjowell 0:148b561040e4 42 * This callback allows the LEDService to receive updates to the ledState Characteristic.
jjowell 0:148b561040e4 43 *
jjowell 0:148b561040e4 44 * @param[in] params
jjowell 0:148b561040e4 45 * Information about the characterisitc being updated.
jjowell 0:148b561040e4 46 */
jjowell 0:148b561040e4 47 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
jjowell 0:148b561040e4 48 if ((params->handle == ledServicePtr->getValueHandle()) && (params->len == 1)) {
jjowell 0:148b561040e4 49 actuatedLED = *(params->data);
jjowell 0:148b561040e4 50 }
jjowell 0:148b561040e4 51 }
jjowell 0:148b561040e4 52
jjowell 0:148b561040e4 53 /**
jjowell 0:148b561040e4 54 * This function is called when the ble initialization process has failed
jjowell 0:148b561040e4 55 */
jjowell 0:148b561040e4 56 void onBleInitError(BLE &ble, ble_error_t error)
jjowell 0:148b561040e4 57 {
jjowell 0:148b561040e4 58 /* Initialization error handling should go here */
jjowell 0:148b561040e4 59 }
jjowell 0:148b561040e4 60
jjowell 0:148b561040e4 61 /**
jjowell 0:148b561040e4 62 * Callback triggered when the ble initialization process has finished
jjowell 0:148b561040e4 63 */
jjowell 0:148b561040e4 64 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
jjowell 0:148b561040e4 65 {
jjowell 0:148b561040e4 66 BLE& ble = params->ble;
jjowell 0:148b561040e4 67 ble_error_t error = params->error;
jjowell 0:148b561040e4 68
jjowell 0:148b561040e4 69 if (error != BLE_ERROR_NONE) {
jjowell 0:148b561040e4 70 /* In case of error, forward the error handling to onBleInitError */
jjowell 0:148b561040e4 71 onBleInitError(ble, error);
jjowell 0:148b561040e4 72 return;
jjowell 0:148b561040e4 73 }
jjowell 0:148b561040e4 74
jjowell 0:148b561040e4 75 /* Ensure that it is the default instance of BLE */
jjowell 0:148b561040e4 76 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
jjowell 0:148b561040e4 77 return;
jjowell 0:148b561040e4 78 }
jjowell 0:148b561040e4 79
jjowell 0:148b561040e4 80 ble.gap().onDisconnection(disconnectionCallback);
jjowell 0:148b561040e4 81 ble.gattServer().onDataWritten(onDataWrittenCallback);
jjowell 0:148b561040e4 82
jjowell 0:148b561040e4 83 bool initialValueForLEDCharacteristic = false;
jjowell 0:148b561040e4 84 ledServicePtr = new LEDService(ble, initialValueForLEDCharacteristic);
jjowell 0:148b561040e4 85
jjowell 0:148b561040e4 86 /* setup advertising */
jjowell 0:148b561040e4 87 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
jjowell 0:148b561040e4 88 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
jjowell 0:148b561040e4 89 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
jjowell 0:148b561040e4 90 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jjowell 0:148b561040e4 91 ble.gap().setAdvertisingInterval(1000); /* 1000ms. */
jjowell 0:148b561040e4 92 ble.gap().startAdvertising();
jjowell 0:148b561040e4 93 }
jjowell 0:148b561040e4 94
jjowell 0:148b561040e4 95 int main(void)
jjowell 0:148b561040e4 96 {
jjowell 0:148b561040e4 97 ticker.attach(periodicCallback, 1); /* Blink LED every second */
jjowell 0:148b561040e4 98
jjowell 0:148b561040e4 99 BLE &ble = BLE::Instance();
jjowell 0:148b561040e4 100 ble.init(bleInitComplete);
jjowell 0:148b561040e4 101
jjowell 0:148b561040e4 102 /* SpinWait for initialization to complete. This is necessary because the
jjowell 0:148b561040e4 103 * BLE object is used in the main loop below. */
jjowell 0:148b561040e4 104 while (ble.hasInitialized() == false) { /* spin loop */ }
jjowell 0:148b561040e4 105
jjowell 0:148b561040e4 106 while (true) {
jjowell 0:148b561040e4 107 ble.waitForEvent();
jjowell 0:148b561040e4 108 }
jjowell 0:148b561040e4 109 }