Demo for the LinkLoss Service.

Dependencies:   BLE_API mbed nRF51822 X_NUCLEO_IDB0XA1

Committer:
Vincent Coubard
Date:
Tue Sep 20 13:28:49 2016 +0100
Revision:
7:66b2dfe95c75
Parent:
5:04920b552c37
Update all libraries.
Add support of ST shield via the config system.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:440ee5e8595f 1 /* mbed Microcontroller Library
rgrover1 0:440ee5e8595f 2 * Copyright (c) 2006-2013 ARM Limited
rgrover1 0:440ee5e8595f 3 *
rgrover1 0:440ee5e8595f 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:440ee5e8595f 5 * you may not use this file except in compliance with the License.
rgrover1 0:440ee5e8595f 6 * You may obtain a copy of the License at
rgrover1 0:440ee5e8595f 7 *
rgrover1 0:440ee5e8595f 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:440ee5e8595f 9 *
rgrover1 0:440ee5e8595f 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:440ee5e8595f 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:440ee5e8595f 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:440ee5e8595f 13 * See the License for the specific language governing permissions and
rgrover1 0:440ee5e8595f 14 * limitations under the License.
rgrover1 0:440ee5e8595f 15 */
rgrover1 0:440ee5e8595f 16
rgrover1 0:440ee5e8595f 17 #include "mbed.h"
andresag 5:04920b552c37 18 #include "ble/BLE.h"
andresag 5:04920b552c37 19 #include "ble/services/LinkLossService.h"
rgrover1 0:440ee5e8595f 20
andresag 5:04920b552c37 21 static LinkLossService* linkLossPtr;
rgrover1 0:440ee5e8595f 22
andresag 5:04920b552c37 23 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
rgrover1 0:440ee5e8595f 24 {
andresag 5:04920b552c37 25 BLE::Instance().gap().startAdvertising();
rgrover1 0:440ee5e8595f 26 }
rgrover1 0:440ee5e8595f 27
rgrover1 0:440ee5e8595f 28 void linkLossCallback(LinkLossService::AlertLevel_t level)
rgrover1 0:440ee5e8595f 29 {
rgrover1 0:440ee5e8595f 30 printf("received link loss alert\r\n");
rgrover1 0:440ee5e8595f 31 }
rgrover1 0:440ee5e8595f 32
andresag 5:04920b552c37 33 /**
andresag 5:04920b552c37 34 * This function is called when the ble initialization process has failed
andresag 5:04920b552c37 35 */
andresag 5:04920b552c37 36 void onBleInitError(BLE &ble, ble_error_t error)
rgrover1 0:440ee5e8595f 37 {
andresag 5:04920b552c37 38 /* Avoid compiler warnings */
andresag 5:04920b552c37 39 (void) ble;
andresag 5:04920b552c37 40 (void) error;
andresag 5:04920b552c37 41
andresag 5:04920b552c37 42 /* Initialization error handling should go here */
andresag 5:04920b552c37 43 }
andresag 5:04920b552c37 44
andresag 5:04920b552c37 45 /**
andresag 5:04920b552c37 46 * Callback triggered when the ble initialization process has finished
andresag 5:04920b552c37 47 */
andresag 5:04920b552c37 48 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 5:04920b552c37 49 {
andresag 5:04920b552c37 50 BLE& ble = params->ble;
andresag 5:04920b552c37 51 ble_error_t error = params->error;
andresag 5:04920b552c37 52
andresag 5:04920b552c37 53 if (error != BLE_ERROR_NONE) {
andresag 5:04920b552c37 54 /* In case of error, forward the error handling to onBleInitError */
andresag 5:04920b552c37 55 onBleInitError(ble, error);
andresag 5:04920b552c37 56 return;
andresag 5:04920b552c37 57 }
andresag 5:04920b552c37 58
andresag 5:04920b552c37 59 /* Ensure that it is the default instance of BLE */
andresag 5:04920b552c37 60 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 5:04920b552c37 61 return;
andresag 5:04920b552c37 62 }
andresag 5:04920b552c37 63
rgrover1 4:88dca48b2397 64 ble.gap().onDisconnection(disconnectionCallback);
rgrover1 0:440ee5e8595f 65
andresag 5:04920b552c37 66 /* Setup primary service */
andresag 5:04920b552c37 67 linkLossPtr = new LinkLossService(ble, linkLossCallback, LinkLossService::HIGH_ALERT);
rgrover1 0:440ee5e8595f 68
rgrover1 4:88dca48b2397 69 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 4:88dca48b2397 70 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
rgrover1 4:88dca48b2397 71 ble.gap().setAdvertisingInterval(1000); /* 1second. */
rgrover1 4:88dca48b2397 72 ble.gap().startAdvertising();
andresag 5:04920b552c37 73 }
rgrover1 0:440ee5e8595f 74
andresag 5:04920b552c37 75 int main(void)
andresag 5:04920b552c37 76 {
andresag 5:04920b552c37 77 BLE &ble = BLE::Instance();
andresag 5:04920b552c37 78 ble.init(bleInitComplete);
andresag 5:04920b552c37 79
rgrover1 0:440ee5e8595f 80 while (true) {
rgrover1 0:440ee5e8595f 81 ble.waitForEvent();
rgrover1 0:440ee5e8595f 82 }
rgrover1 0:440ee5e8595f 83 }