test1

Dependencies:   BLE_API mbed nRF51822

Committer:
bbman30
Date:
Fri May 20 02:36:33 2016 +0000
Revision:
0:91ab5e95f6b0
hkh

Who changed what in which revision?

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