This library includes use cases which can be used for replicating BLE use caes.

Committer:
hemddabral
Date:
Thu Sep 25 07:02:26 2014 +0000
Revision:
0:935006092f33
Child:
1:857e59bbf707
Initial version implementing multiple service use-case

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hemddabral 0:935006092f33 1 #include "UseCases.h"
hemddabral 0:935006092f33 2
hemddabral 0:935006092f33 3 #include "mbed.h"
hemddabral 0:935006092f33 4
hemddabral 0:935006092f33 5 DigitalOut myled(LED1);
hemddabral 0:935006092f33 6
hemddabral 0:935006092f33 7 #include "debug.h"
hemddabral 0:935006092f33 8 #include "btle.h"
hemddabral 0:935006092f33 9 #include "BLEDevice.h"
hemddabral 0:935006092f33 10 #include "UUID.h"
hemddabral 0:935006092f33 11 #include "Utils.h"
hemddabral 0:935006092f33 12
hemddabral 0:935006092f33 13 BLEDevice dev;
hemddabral 0:935006092f33 14
hemddabral 0:935006092f33 15 const static char DEVICE_NAME[] = "HEALTH_DEVICE";
hemddabral 0:935006092f33 16 //const uint8_t device_address[6] = { 0x12, 0x34, 0x00, 0xe1, 0x80, 0x02 }; //Peripheral address
hemddabral 0:935006092f33 17
hemddabral 0:935006092f33 18 void Append128bitUUID(uint8_t *uuid128_list, const LongUUID_t HRM_SERVICE_UUID_128);
hemddabral 0:935006092f33 19 void print_array(uint8_t *array);
hemddabral 0:935006092f33 20
hemddabral 0:935006092f33 21 /*********128 bit UUIDs. Not defined in GattCharacteristic.h and GattService.h*************/
hemddabral 0:935006092f33 22
hemddabral 0:935006092f33 23
hemddabral 0:935006092f33 24 #define MAX_SERVICES_NOS 1
hemddabral 0:935006092f33 25 //typedef uint8_t UUID_128_BIT[16];
hemddabral 0:935006092f33 26 const LongUUID_t HEART_RATE_CHAR_UUID_128 = {0x42,0x82,0x1a,0x40, 0xe4,0x77, 0x11,0xe2, 0x82,0xd0, 0x00,0x02,0xa5,0xd5,0xc5,0x1a};
hemddabral 0:935006092f33 27 const LongUUID_t HRM_SERVICE_UUID_128 = {0x42,0x82,0x1a,0x40, 0xe4,0x77, 0x11,0xe2, 0x82,0xd0, 0x00,0x02,0xa5,0xd5,0xc5,0x1b};
hemddabral 0:935006092f33 28
hemddabral 0:935006092f33 29 uint8_t UUID_Count=0;
hemddabral 0:935006092f33 30
hemddabral 0:935006092f33 31 static uint8_t hrmCounter = 100;
hemddabral 0:935006092f33 32 static uint8_t bpm[2] = {0x00, hrmCounter};
hemddabral 0:935006092f33 33 //static uint8_t hcpCounter = 99;
hemddabral 0:935006092f33 34 static uint8_t hcp[2] = {0x00, hrmCounter};
hemddabral 0:935006092f33 35
hemddabral 0:935006092f33 36 GattCharacteristic hrmRate(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR, bpm, sizeof(bpm), sizeof(bpm),
hemddabral 0:935006092f33 37 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
hemddabral 0:935006092f33 38
hemddabral 0:935006092f33 39 GattCharacteristic hcpControlPoint(GattCharacteristic::UUID_HEART_RATE_CONTROL_POINT_CHAR, hcp, sizeof(hcp), sizeof(hcp),
hemddabral 0:935006092f33 40 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
hemddabral 0:935006092f33 41 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
hemddabral 0:935006092f33 42
hemddabral 0:935006092f33 43
hemddabral 0:935006092f33 44 GattCharacteristic *hrmChars[] = {&hrmRate, &hcpControlPoint};
hemddabral 0:935006092f33 45 UUID HRMSERVICE(GattService::UUID_HEART_RATE_SERVICE);
hemddabral 0:935006092f33 46 GattService hrmService(HRMSERVICE/*GattService::UUID_HEART_RATE_SERVICE*/, hrmChars, sizeof(hrmChars) / sizeof(GattCharacteristic *));
hemddabral 0:935006092f33 47
hemddabral 0:935006092f33 48 //uint8_t uuid128_list[16*MAX_SERVICES_NOS];// = {HRM_SERVICE_UUID_128[0], HRM_SERVICE_UUID_128[1]};
hemddabral 0:935006092f33 49 //static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
hemddabral 0:935006092f33 50
hemddabral 0:935006092f33 51 /* second service for testing the device */
hemddabral 0:935006092f33 52 #if 1
hemddabral 0:935006092f33 53 static uint8_t bpValue = 110;
hemddabral 0:935006092f33 54 static uint8_t bloodPressure[2] = {bpValue, 0x00};
hemddabral 0:935006092f33 55 static uint8_t bpDevFeature[2] = {0x00, bpValue};
hemddabral 0:935006092f33 56
hemddabral 0:935006092f33 57 GattCharacteristic bpMeasurment(GattCharacteristic::UUID_BLOOD_PRESSURE_MEASUREMENT_CHAR, bloodPressure, sizeof(bloodPressure), sizeof(bloodPressure),
hemddabral 0:935006092f33 58 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY|GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ);
hemddabral 0:935006092f33 59
hemddabral 0:935006092f33 60 GattCharacteristic bpFeature(GattCharacteristic::UUID_BLOOD_PRESSURE_FEATURE_CHAR, bpDevFeature, sizeof(bpDevFeature), sizeof(bpDevFeature),
hemddabral 0:935006092f33 61 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE_WITHOUT_RESPONSE|
hemddabral 0:935006092f33 62 GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_WRITE);
hemddabral 0:935006092f33 63
hemddabral 0:935006092f33 64
hemddabral 0:935006092f33 65 GattCharacteristic *bpChars[] = {&bpMeasurment, &bpFeature};
hemddabral 0:935006092f33 66 UUID BP_SERVICE(GattService::UUID_BLOOD_PRESSURE_SERVICE);
hemddabral 0:935006092f33 67 GattService bpService(BP_SERVICE, bpChars, sizeof(bpChars) / sizeof(GattCharacteristic *));
hemddabral 0:935006092f33 68
hemddabral 0:935006092f33 69 #endif
hemddabral 0:935006092f33 70 /****************************************/
hemddabral 0:935006092f33 71
hemddabral 0:935006092f33 72
hemddabral 0:935006092f33 73 void disconnectionCallback(Gap::Handle_t handle)
hemddabral 0:935006092f33 74 {
hemddabral 0:935006092f33 75 DEBUG("Disconnected!\n\r");
hemddabral 0:935006092f33 76 DEBUG("Restarting the advertising process\n\r");
hemddabral 0:935006092f33 77 dev.startAdvertising();
hemddabral 0:935006092f33 78 }
hemddabral 0:935006092f33 79
hemddabral 0:935006092f33 80 void onWriteCallback(uint16_t attributeHandle) {
hemddabral 0:935006092f33 81 DEBUG("Write Callback!!\n\r");
hemddabral 0:935006092f33 82 }
hemddabral 0:935006092f33 83
hemddabral 0:935006092f33 84 void onConnectionCallback(Gap::Handle_t handle) {
hemddabral 0:935006092f33 85 //myled = 1; // LED is ON
hemddabral 0:935006092f33 86 DEBUG("Connected BlueNRG!!\n\r");
hemddabral 0:935006092f33 87 }
hemddabral 0:935006092f33 88
hemddabral 0:935006092f33 89 void Append128bitUUID(uint8_t *array, const LongUUID_t SERVICE_UUID_128)
hemddabral 0:935006092f33 90 {
hemddabral 0:935006092f33 91 for(int x=0;x<16; x++)
hemddabral 0:935006092f33 92 {
hemddabral 0:935006092f33 93 array[x+UUID_Count*16]=SERVICE_UUID_128[x];
hemddabral 0:935006092f33 94 }
hemddabral 0:935006092f33 95 UUID_Count++;
hemddabral 0:935006092f33 96 return;
hemddabral 0:935006092f33 97 }
hemddabral 0:935006092f33 98
hemddabral 0:935006092f33 99 /**
hemddabral 0:935006092f33 100 * Triggered periodically by the 'ticker' interrupt; updates hrmCounter.
hemddabral 0:935006092f33 101 */
hemddabral 0:935006092f33 102 void periodicCallback(void)
hemddabral 0:935006092f33 103 {
hemddabral 0:935006092f33 104 //myled = !myled; /* Do blinky on LED1 while we're waiting for BLE events */
hemddabral 0:935006092f33 105
hemddabral 0:935006092f33 106 if (dev.getGapState().connected) {
hemddabral 0:935006092f33 107 hrmCounter++;
hemddabral 0:935006092f33 108 if (hrmCounter == 175) {
hemddabral 0:935006092f33 109 hrmCounter = 100;
hemddabral 0:935006092f33 110 }
hemddabral 0:935006092f33 111 bpm[1] = hrmCounter;
hemddabral 0:935006092f33 112 //uint16_t t = sizeof(bpm);
hemddabral 0:935006092f33 113 //DEBUG("Char Handle 0x%x OK.",hrmRate.getHandle());
hemddabral 0:935006092f33 114 //dev.readCharacteristicValue(hrmRate.getHandle(), bpm, (uint16_t *const)t);
hemddabral 0:935006092f33 115 dev.updateCharacteristicValue(hrmRate.getHandle(), bpm, sizeof(bpm));
hemddabral 0:935006092f33 116
hemddabral 0:935006092f33 117 bloodPressure[0] = hrmCounter;
hemddabral 0:935006092f33 118 dev.updateCharacteristicValue(bpMeasurment.getHandle(), bloodPressure, sizeof(bloodPressure));
hemddabral 0:935006092f33 119 //DEBUG("Ticker CB..\n\r");
hemddabral 0:935006092f33 120 } //else DEBUG("Not Connected..\n\r");
hemddabral 0:935006092f33 121 }
hemddabral 0:935006092f33 122
hemddabral 0:935006092f33 123 void multipleServiceUseCase() {
hemddabral 0:935006092f33 124 Ticker ticker; //For Tick interrupt if used later on (periodic data updates?)
hemddabral 0:935006092f33 125
hemddabral 0:935006092f33 126
hemddabral 0:935006092f33 127 //LongUUID_t HEART_RATE_CHAR_UUID_128, HRM_SERVICE_UUID_128;
hemddabral 0:935006092f33 128 //COPY_HRM_SERVICE_UUID(HRM_SERVICE_UUID_128);
hemddabral 0:935006092f33 129 //COPY_HRM_CHAR_UUID(HEART_RATE_CHAR_UUID_128);
hemddabral 0:935006092f33 130 UUID heart_rate_char_UUID = UUID(HEART_RATE_CHAR_UUID_128);
hemddabral 0:935006092f33 131 UUID hrm_service_UUID = UUID(HRM_SERVICE_UUID_128);
hemddabral 0:935006092f33 132
hemddabral 0:935006092f33 133 myled = 0;//Switch OFF LED1
hemddabral 0:935006092f33 134
hemddabral 0:935006092f33 135 DEBUG("Initializing BlueNRG...\n\r");
hemddabral 0:935006092f33 136 #if 0
hemddabral 0:935006092f33 137 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
hemddabral 0:935006092f33 138 DigitalOut cs(PB_6);
hemddabral 0:935006092f33 139 cs = 0;
hemddabral 0:935006092f33 140 #endif
hemddabral 0:935006092f33 141 dev.init();
hemddabral 0:935006092f33 142
hemddabral 0:935006092f33 143 dev.onConnection(onConnectionCallback);
hemddabral 0:935006092f33 144 dev.onDisconnection(disconnectionCallback);
hemddabral 0:935006092f33 145 dev.onDataWritten(onWriteCallback);
hemddabral 0:935006092f33 146
hemddabral 0:935006092f33 147 //TODO.
hemddabral 0:935006092f33 148 //dev.setAddress(Gap::ADDR_TYPE_PUBLIC, device_address);//Does not work after gap/gatt init.
hemddabral 0:935006092f33 149 //TODO.
hemddabral 0:935006092f33 150
hemddabral 0:935006092f33 151 //Append128bitUUID(uuid128_list, HRM_SERVICE_UUID_128);
hemddabral 0:935006092f33 152
hemddabral 0:935006092f33 153 /* setup advertising */
hemddabral 0:935006092f33 154 dev.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
hemddabral 0:935006092f33 155
hemddabral 0:935006092f33 156 //TODO:IMP STUFF: 128bit list is basically a uint8_t list. User should know how many services he supports and define the number in MAX_SERVICES_NOS
hemddabral 0:935006092f33 157 //dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list));
hemddabral 0:935006092f33 158
hemddabral 0:935006092f33 159 dev.accumulateAdvertisingPayload(GapAdvertisingData::HEART_RATE_SENSOR_HEART_RATE_BELT);
hemddabral 0:935006092f33 160
hemddabral 0:935006092f33 161 dev.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
hemddabral 0:935006092f33 162 dev.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
hemddabral 0:935006092f33 163 dev.setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */
hemddabral 0:935006092f33 164 DEBUG("Starting Advertising...\n\r");
hemddabral 0:935006092f33 165 dev.startAdvertising();
hemddabral 0:935006092f33 166
hemddabral 0:935006092f33 167
hemddabral 0:935006092f33 168 dev.addService(bpService);
hemddabral 0:935006092f33 169 dev.addService(hrmService);
hemddabral 0:935006092f33 170
hemddabral 0:935006092f33 171
hemddabral 0:935006092f33 172
hemddabral 0:935006092f33 173 //ticker.attach(periodicCallback, 1); Multi threading and called from ISR context does not work!
hemddabral 0:935006092f33 174
hemddabral 0:935006092f33 175 while(1) {
hemddabral 0:935006092f33 176 myled = 1; // LED is ON
hemddabral 0:935006092f33 177 wait(0.5); // 500 ms
hemddabral 0:935006092f33 178 myled = 0; // LED is OFF
hemddabral 0:935006092f33 179 wait(0.5); // 500 ms
hemddabral 0:935006092f33 180 //DEBUG("tic!\n\r");
hemddabral 0:935006092f33 181 periodicCallback();//Works from here!!
hemddabral 0:935006092f33 182
hemddabral 0:935006092f33 183 dev.waitForEvent();
hemddabral 0:935006092f33 184 }
hemddabral 0:935006092f33 185 }
hemddabral 0:935006092f33 186