sdasd

Dependencies:   BLE_API mbed nRF51822

Fork of MEDRON_SNIFFER_ABCD-KIMLIKLI-OLANLARI-TARAMA-YAPAR_30062016 by HAKAN AYDIN

Committer:
andresag
Date:
Tue Jan 12 10:20:26 2016 +0000
Revision:
11:16f67d5752e1
Parent:
9:69a2ad0bcdb7
Child:
12:f4b869da449c
Update example to latest BLE API.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rgrover1 0:332983584a9c 1 /* mbed Microcontroller Library
rgrover1 0:332983584a9c 2 * Copyright (c) 2006-2015 ARM Limited
rgrover1 0:332983584a9c 3 *
rgrover1 0:332983584a9c 4 * Licensed under the Apache License, Version 2.0 (the "License");
rgrover1 0:332983584a9c 5 * you may not use this file except in compliance with the License.
rgrover1 0:332983584a9c 6 * You may obtain a copy of the License at
rgrover1 0:332983584a9c 7 *
rgrover1 0:332983584a9c 8 * http://www.apache.org/licenses/LICENSE-2.0
rgrover1 0:332983584a9c 9 *
rgrover1 0:332983584a9c 10 * Unless required by applicable law or agreed to in writing, software
rgrover1 0:332983584a9c 11 * distributed under the License is distributed on an "AS IS" BASIS,
rgrover1 0:332983584a9c 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rgrover1 0:332983584a9c 13 * See the License for the specific language governing permissions and
rgrover1 0:332983584a9c 14 * limitations under the License.
rgrover1 0:332983584a9c 15 */
rgrover1 0:332983584a9c 16
rgrover1 0:332983584a9c 17 #include "mbed.h"
rgrover1 9:69a2ad0bcdb7 18 #include "toolchain.h"
rgrover1 9:69a2ad0bcdb7 19 #include "ble/BLE.h"
sunsmile2015 7:91324daa3bfa 20 #include "TMP_nrf51/TMP_nrf51.h"
sunsmile2015 7:91324daa3bfa 21
rgrover1 9:69a2ad0bcdb7 22 DigitalOut alivenessLED(LED1, 1);
andresag 11:16f67d5752e1 23 Ticker ticker;
rgrover1 0:332983584a9c 24
rgrover1 0:332983584a9c 25 void periodicCallback(void)
rgrover1 0:332983584a9c 26 {
rgrover1 9:69a2ad0bcdb7 27 alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. This is optional. */
rgrover1 0:332983584a9c 28 }
rgrover1 0:332983584a9c 29
rgrover1 9:69a2ad0bcdb7 30 /*
rgrover1 9:69a2ad0bcdb7 31 * This function is called every time we scan an advertisement.
rgrover1 9:69a2ad0bcdb7 32 */
sunsmile2015 6:850f44146c9f 33 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
sunsmile2015 6:850f44146c9f 34 {
rgrover1 9:69a2ad0bcdb7 35 struct AdvertisingData_t {
rgrover1 9:69a2ad0bcdb7 36 uint8_t length; /* doesn't include itself */
rgrover1 9:69a2ad0bcdb7 37 GapAdvertisingData::DataType_t dataType;
rgrover1 9:69a2ad0bcdb7 38 uint8_t data[0];
rgrover1 9:69a2ad0bcdb7 39 } PACKED;
rgrover1 9:69a2ad0bcdb7 40
rgrover1 9:69a2ad0bcdb7 41 struct ApplicationData_t {
rgrover1 9:69a2ad0bcdb7 42 uint16_t applicationSpecificId; /* An ID used to identify temperature value
rgrover1 9:69a2ad0bcdb7 43 in the manufacture specific AD data field */
rgrover1 9:69a2ad0bcdb7 44 TMP_nrf51::TempSensorValue_t tmpSensorValue; /* User defined application data */
rgrover1 9:69a2ad0bcdb7 45 } PACKED;
rgrover1 9:69a2ad0bcdb7 46
rgrover1 9:69a2ad0bcdb7 47 static const uint16_t APP_SPECIFIC_ID_TEST = 0xFEFE;
rgrover1 9:69a2ad0bcdb7 48
rgrover1 9:69a2ad0bcdb7 49 /* Search for the manufacturer specific data with matching application-ID */
rgrover1 9:69a2ad0bcdb7 50 AdvertisingData_t *pAdvData;
rgrover1 9:69a2ad0bcdb7 51 size_t index = 0;
rgrover1 9:69a2ad0bcdb7 52 while (index < params->advertisingDataLen) {
rgrover1 9:69a2ad0bcdb7 53 pAdvData = (AdvertisingData_t *)&params->advertisingData[index];
rgrover1 9:69a2ad0bcdb7 54 if (pAdvData->dataType == GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA) {
sunsmile2015 8:649bd171929e 55 ApplicationData_t *pAppData = (ApplicationData_t *)pAdvData->data;
rgrover1 9:69a2ad0bcdb7 56 if (pAppData->applicationSpecificId == APP_SPECIFIC_ID_TEST) {
rgrover1 9:69a2ad0bcdb7 57 /* dump information on the console. */
sunsmile2015 8:649bd171929e 58 printf("From [%02x %02x %02x], ", params->peerAddr[2], params->peerAddr[1], params->peerAddr[0]);
rgrover1 9:69a2ad0bcdb7 59 printf("Temp is %f\r\n", (TMP_nrf51::TempSensorValue_t)pAppData->tmpSensorValue);
sunsmile2015 7:91324daa3bfa 60 break;
sunsmile2015 7:91324daa3bfa 61 }
sunsmile2015 6:850f44146c9f 62 }
rgrover1 9:69a2ad0bcdb7 63 index += (pAdvData->length + 1);
rgrover1 0:332983584a9c 64 }
rgrover1 0:332983584a9c 65 }
rgrover1 0:332983584a9c 66
andresag 11:16f67d5752e1 67 /**
andresag 11:16f67d5752e1 68 * This function is called when the ble initialization process has failed
andresag 11:16f67d5752e1 69 */
andresag 11:16f67d5752e1 70 void onBleInitError(BLE &ble, ble_error_t error)
andresag 11:16f67d5752e1 71 {
andresag 11:16f67d5752e1 72 /* Initialization error handling should go here */
andresag 11:16f67d5752e1 73 }
andresag 11:16f67d5752e1 74
andresag 11:16f67d5752e1 75 /**
andresag 11:16f67d5752e1 76 * Callback triggered when the ble initialization process has finished
andresag 11:16f67d5752e1 77 */
andresag 11:16f67d5752e1 78 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 11:16f67d5752e1 79 {
andresag 11:16f67d5752e1 80 BLE& ble = params->ble;
andresag 11:16f67d5752e1 81 ble_error_t error = params->error;
andresag 11:16f67d5752e1 82
andresag 11:16f67d5752e1 83 if (error != BLE_ERROR_NONE) {
andresag 11:16f67d5752e1 84 /* In case of error, forward the error handling to onBleInitError */
andresag 11:16f67d5752e1 85 onBleInitError(ble, error);
andresag 11:16f67d5752e1 86 return;
andresag 11:16f67d5752e1 87 }
andresag 11:16f67d5752e1 88
andresag 11:16f67d5752e1 89 /* Ensure that it is the default instance of BLE */
andresag 11:16f67d5752e1 90 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 11:16f67d5752e1 91 return;
andresag 11:16f67d5752e1 92 }
andresag 11:16f67d5752e1 93
andresag 11:16f67d5752e1 94 /* Setup and start scanning */
andresag 11:16f67d5752e1 95 ble.gap().setScanParams(1800 /* scan interval */, 1500 /* scan window */);
andresag 11:16f67d5752e1 96 ble.gap().startScan(advertisementCallback);
andresag 11:16f67d5752e1 97 }
andresag 11:16f67d5752e1 98
rgrover1 0:332983584a9c 99 int main(void)
rgrover1 0:332983584a9c 100 {
andresag 11:16f67d5752e1 101 ticker.attach(periodicCallback, 1); /* trigger sensor polling every 2 seconds */
rgrover1 0:332983584a9c 102
andresag 11:16f67d5752e1 103 BLE &ble = BLE::Instance();
andresag 11:16f67d5752e1 104 ble.init(bleInitComplete);
rgrover1 0:332983584a9c 105
rgrover1 0:332983584a9c 106 while (true) {
rgrover1 0:332983584a9c 107 ble.waitForEvent();
rgrover1 0:332983584a9c 108 }
rgrover1 0:332983584a9c 109 }