Simple example to demonstrate custom made BLE service and characteristics.

Fork of BLE_GATT_Example by Bluetooth Low Energy

Committer:
jurica238814
Date:
Thu Nov 09 16:08:04 2017 +0000
Revision:
23:924d0ef8f1cb
Parent:
22:406127954d1f
0xBABE and 0xDEAD works.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jurica238814 23:924d0ef8f1cb 1 /* mbed Microcontroller Library
jurica238814 23:924d0ef8f1cb 2 * Copyright (c) 2006-2013 ARM Limited
jurica238814 23:924d0ef8f1cb 3 *
jurica238814 23:924d0ef8f1cb 4 * Licensed under the Apache License, Version 2.0 (the "License");
jurica238814 23:924d0ef8f1cb 5 * you may not use this file except in compliance with the License.
jurica238814 23:924d0ef8f1cb 6 * You may obtain a copy of the License at
jurica238814 23:924d0ef8f1cb 7 *
jurica238814 23:924d0ef8f1cb 8 * http://www.apache.org/licenses/LICENSE-2.0
jurica238814 23:924d0ef8f1cb 9 *
jurica238814 23:924d0ef8f1cb 10 * Unless required by applicable law or agreed to in writing, software
jurica238814 23:924d0ef8f1cb 11 * distributed under the License is distributed on an "AS IS" BASIS,
jurica238814 23:924d0ef8f1cb 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
jurica238814 23:924d0ef8f1cb 13 * See the License for the specific language governing permissions and
jurica238814 23:924d0ef8f1cb 14 * limitations under the License.
jurica238814 23:924d0ef8f1cb 15 */
mbedAustin 1:94152e7d8b5c 16
jurica238814 23:924d0ef8f1cb 17 #include <mbed-events/events.h>
jurica238814 23:924d0ef8f1cb 18 #include <mbed.h>
jurica238814 23:924d0ef8f1cb 19 #include "ble/BLE.h"
jurica238814 23:924d0ef8f1cb 20 #include "AckService.h"
mbedAustin 1:94152e7d8b5c 21
jurica238814 23:924d0ef8f1cb 22 #define DEBUG (0)
jurica238814 23:924d0ef8f1cb 23 #define MSD_SIZE (18)
mbedAustin 1:94152e7d8b5c 24
jurica238814 23:924d0ef8f1cb 25 #if DEBUG
jurica238814 23:924d0ef8f1cb 26 #define RX (p26) // SERIAL STILL DOES NOT WORK!!!
jurica238814 23:924d0ef8f1cb 27 #define TX (p25)
jurica238814 23:924d0ef8f1cb 28 #endif
mbedAustin 2:e84c13abc479 29
jurica238814 23:924d0ef8f1cb 30 ACKService<4> *ackServicePtr;
jurica238814 23:924d0ef8f1cb 31 #if DEBUG
jurica238814 23:924d0ef8f1cb 32 Serial pc(TX, RX);
jurica238814 23:924d0ef8f1cb 33 #endif
mbedAustin 2:e84c13abc479 34
jurica238814 23:924d0ef8f1cb 35 BLE &ble = BLE::Instance();
jurica238814 23:924d0ef8f1cb 36
jurica238814 23:924d0ef8f1cb 37 DigitalOut redLed(p22);
jurica238814 23:924d0ef8f1cb 38 DigitalOut greenLed(p24);
jurica238814 23:924d0ef8f1cb 39 DigitalOut blueLed(p23);
jurica238814 23:924d0ef8f1cb 40 DigitalOut alivenessLED(p26);
mbedAustin 2:e84c13abc479 41
jurica238814 23:924d0ef8f1cb 42 const static char DEVICE_NAME[] = "CRP_ACK";
jurica238814 23:924d0ef8f1cb 43 const static uint16_t ACK_CHARACTERISTIC_UUID = 0xA001;
jurica238814 23:924d0ef8f1cb 44 const static uint16_t ACK_SERVICE_UUID = 0xA000;
jurica238814 23:924d0ef8f1cb 45 uint8_t MSD[MSD_SIZE] = {0x59, 0x00, 0xE1, 0x61, 0x35, 0xBA, 0xC0, 0xEC, 0x47, 0x2A, 0x98, 0x00, 0xAF, 0x18, 0x43, 0xFF, 0x05, 0x00};
jurica238814 23:924d0ef8f1cb 46 uint8_t macAddress[6] = {0x23, 0x24, 0x35, 0x22, 0x24, 0x45};
mbedAustin 2:e84c13abc479 47
jurica238814 23:924d0ef8f1cb 48 static EventQueue eventQueue(
jurica238814 23:924d0ef8f1cb 49 /* event count */ 10 * /* event size */ 32
jurica238814 23:924d0ef8f1cb 50 );
jurica238814 23:924d0ef8f1cb 51
jurica238814 23:924d0ef8f1cb 52 void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params){
jurica238814 23:924d0ef8f1cb 53 #if DEBUG
jurica238814 23:924d0ef8f1cb 54 pc.printf("BLE device is connected.\n");
jurica238814 23:924d0ef8f1cb 55 #endif
jurica238814 23:924d0ef8f1cb 56 redLed = 1;
jurica238814 23:924d0ef8f1cb 57 greenLed = 1;
jurica238814 23:924d0ef8f1cb 58 blueLed = 0;
mbedAustin 1:94152e7d8b5c 59 }
mbedAustin 0:cd5b6733aeb1 60
jurica238814 23:924d0ef8f1cb 61 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
jurica238814 23:924d0ef8f1cb 62 (void) params;
jurica238814 23:924d0ef8f1cb 63 redLed = 0;
jurica238814 23:924d0ef8f1cb 64 greenLed = 1;
jurica238814 23:924d0ef8f1cb 65 blueLed = 1;
jurica238814 23:924d0ef8f1cb 66 ble.disconnect(Gap::LOCAL_HOST_TERMINATED_CONNECTION);
jurica238814 23:924d0ef8f1cb 67 BLE::Instance().gap().startAdvertising();
jurica238814 23:924d0ef8f1cb 68 }
jurica238814 23:924d0ef8f1cb 69
jurica238814 23:924d0ef8f1cb 70 void blinkCallback(void){
jurica238814 23:924d0ef8f1cb 71 alivenessLED = !alivenessLED; /* Do blinky on LED1 to indicate system aliveness. */
jurica238814 23:924d0ef8f1cb 72 }
jurica238814 23:924d0ef8f1cb 73
jurica238814 23:924d0ef8f1cb 74 void updateMac(const GattReadCallbackParams *response){
jurica238814 23:924d0ef8f1cb 75 ackServicePtr->updateMacAddress(macAddress); // Update MAC address
jurica238814 23:924d0ef8f1cb 76 }
jurica238814 23:924d0ef8f1cb 77
jurica238814 23:924d0ef8f1cb 78
jurica238814 23:924d0ef8f1cb 79 /**
jurica238814 23:924d0ef8f1cb 80 * This callback allows the LEDService to receive updates to the ledState Characteristic.
jurica238814 23:924d0ef8f1cb 81 *
jurica238814 23:924d0ef8f1cb 82 * @param[in] params
jurica238814 23:924d0ef8f1cb 83 * Information about the characterisitc being updated.
jurica238814 23:924d0ef8f1cb 84 */
jurica238814 23:924d0ef8f1cb 85 void onDataWrittenCallback(const GattWriteCallbackParams *params) {
jurica238814 23:924d0ef8f1cb 86 #if DEBUG
jurica238814 23:924d0ef8f1cb 87 pc.printf("Data written into characteristic.\n");
jurica238814 23:924d0ef8f1cb 88 #endif
jurica238814 23:924d0ef8f1cb 89
jurica238814 23:924d0ef8f1cb 90
jurica238814 23:924d0ef8f1cb 91 if(params->data[0] == 0xBA)
jurica238814 23:924d0ef8f1cb 92 if(params->data[1] == 0xBE)
jurica238814 23:924d0ef8f1cb 93 greenLed = 0;
jurica238814 23:924d0ef8f1cb 94
jurica238814 23:924d0ef8f1cb 95 if(params->data[0] == 0xDE)
jurica238814 23:924d0ef8f1cb 96 if(params->data[1] == 0xAD)
jurica238814 23:924d0ef8f1cb 97 greenLed = 1;
jurica238814 23:924d0ef8f1cb 98 }
jurica238814 23:924d0ef8f1cb 99
jurica238814 23:924d0ef8f1cb 100 /**
jurica238814 23:924d0ef8f1cb 101 * This function is called when the ble initialization process has failled
jurica238814 23:924d0ef8f1cb 102 */
jurica238814 23:924d0ef8f1cb 103 void onBleInitError(BLE &ble, ble_error_t error){
jurica238814 23:924d0ef8f1cb 104 /* Initialization error handling should go here */
jurica238814 23:924d0ef8f1cb 105 }
jurica238814 23:924d0ef8f1cb 106
jurica238814 23:924d0ef8f1cb 107 /**
jurica238814 23:924d0ef8f1cb 108 * Callback triggered when the ble initialization process has finished
jurica238814 23:924d0ef8f1cb 109 */
jurica238814 23:924d0ef8f1cb 110 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params){
jurica238814 23:924d0ef8f1cb 111 BLE& ble = params->ble;
jurica238814 23:924d0ef8f1cb 112 ble_error_t error = params->error;
jurica238814 23:924d0ef8f1cb 113
jurica238814 23:924d0ef8f1cb 114 if (error != BLE_ERROR_NONE) {
jurica238814 23:924d0ef8f1cb 115 /* In case of error, forward the error handling to onBleInitError */
jurica238814 23:924d0ef8f1cb 116 onBleInitError(ble, error);
jurica238814 23:924d0ef8f1cb 117 return;
mbedAustin 8:60ede963dfe2 118 }
jurica238814 23:924d0ef8f1cb 119
jurica238814 23:924d0ef8f1cb 120 /* Ensure that it is the default instance of BLE */
jurica238814 23:924d0ef8f1cb 121 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 22:406127954d1f 122 return;
andresag 22:406127954d1f 123 }
mbedAustin 0:cd5b6733aeb1 124
andresag 19:477567297aac 125 ble.gap().onDisconnection(disconnectionCallback);
jurica238814 23:924d0ef8f1cb 126 ble.gap().onConnection(onConnectionCallback);
jurica238814 23:924d0ef8f1cb 127 ble.gattServer().onDataWritten(onDataWrittenCallback);
jurica238814 23:924d0ef8f1cb 128 ble.gattClient().onDataRead(updateMac);
mbedAustin 2:e84c13abc479 129
jurica238814 23:924d0ef8f1cb 130 uint8_t init_values[4] = {0,0,0,0};
jurica238814 23:924d0ef8f1cb 131 /* Get my MAC address */
jurica238814 23:924d0ef8f1cb 132 BLEProtocol::AddressType_t macAddressType;
jurica238814 23:924d0ef8f1cb 133 ble.gap().getAddress(&macAddressType, macAddress);
jurica238814 23:924d0ef8f1cb 134 ackServicePtr = new ACKService<4>(ble, init_values);
jurica238814 23:924d0ef8f1cb 135 ackServicePtr->updateMacAddress(macAddress); // Update MAC address
jurica238814 23:924d0ef8f1cb 136
jurica238814 23:924d0ef8f1cb 137 /* setup advertising */
jurica238814 23:924d0ef8f1cb 138 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
jurica238814 23:924d0ef8f1cb 139 //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
jurica238814 23:924d0ef8f1cb 140 //ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
jurica238814 23:924d0ef8f1cb 141 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, (uint8_t *)MSD, MSD_SIZE);
jurica238814 23:924d0ef8f1cb 142 ble.gap().setAdvertisingInterval(500); /* 1000ms. */
andresag 19:477567297aac 143 ble.gap().startAdvertising();
andresag 22:406127954d1f 144 }
andresag 19:477567297aac 145
jurica238814 23:924d0ef8f1cb 146 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
jurica238814 23:924d0ef8f1cb 147 BLE &ble = BLE::Instance();
jurica238814 23:924d0ef8f1cb 148 eventQueue.post(Callback<void()>(&ble, &BLE::processEvents));
jurica238814 23:924d0ef8f1cb 149 }
jurica238814 23:924d0ef8f1cb 150
jurica238814 23:924d0ef8f1cb 151
jurica238814 23:924d0ef8f1cb 152
jurica238814 23:924d0ef8f1cb 153 int main(){
jurica238814 23:924d0ef8f1cb 154 redLed = 1;
jurica238814 23:924d0ef8f1cb 155 greenLed = 1;
jurica238814 23:924d0ef8f1cb 156 blueLed = 1;
andresag 22:406127954d1f 157
jurica238814 23:924d0ef8f1cb 158 eventQueue.post_every(500, blinkCallback);
jurica238814 23:924d0ef8f1cb 159
jurica238814 23:924d0ef8f1cb 160 ble.onEventsToProcess(scheduleBleEventsProcessing);
andresag 22:406127954d1f 161 ble.init(bleInitComplete);
andresag 22:406127954d1f 162
mbedAustin 2:e84c13abc479 163 while (true) {
jurica238814 23:924d0ef8f1cb 164 eventQueue.dispatch();
mbedAustin 2:e84c13abc479 165 }
jurica238814 23:924d0ef8f1cb 166 }