L0_BlueNRG_Test_Client

Dependencies:   BLE_API BLE_BlueNRG mbed

Fork of L0_BlueNRG_Test by Mridupawan Das

Committer:
mridup
Date:
Thu Aug 28 04:47:58 2014 +0000
Revision:
2:cf54be3a9e6a
Parent:
1:3b1c20952274
Child:
4:17e1861df93f
BLE API on Rev 111 and Ble_BlueNRG Rev 42

Who changed what in which revision?

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