Working Code

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_Central_Light_Demo by HM_IOT_Demo

Committer:
mbed_tw_hoehoe
Date:
Wed Mar 30 11:21:50 2016 +0000
Revision:
3:d6f80e11a7f4
Parent:
2:4b53d13d9851
Child:
5:f6b6bf98c686
commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_tw_hoehoe 0:83b5c6efd8d7 1 #include "mbed.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 2 #include "ble/BLE.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 3 #include "ble/DiscoveredCharacteristic.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 4 #include "ble/DiscoveredService.h"
mbed_tw_hoehoe 3:d6f80e11a7f4 5 #include "ble/GapScanningParams.h"
mbed_tw_hoehoe 2:4b53d13d9851 6 #include "ble_radio_notification.h"
mbed_tw_hoehoe 3:d6f80e11a7f4 7 #include "ble_gap.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 8
mbed_tw_hoehoe 0:83b5c6efd8d7 9 BLE ble;
mbed_tw_hoehoe 1:2f1203d70643 10 Serial pc(USBTX, USBRX);
mbed_tw_hoehoe 0:83b5c6efd8d7 11
mbed_tw_hoehoe 0:83b5c6efd8d7 12 const uint8_t MPU6050_service_uuid[] = {
mbed_tw_hoehoe 0:83b5c6efd8d7 13 0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
mbed_tw_hoehoe 0:83b5c6efd8d7 14 };
mbed_tw_hoehoe 0:83b5c6efd8d7 15
mbed_tw_hoehoe 0:83b5c6efd8d7 16 const uint8_t MPU6050_Accel_Characteristic_uuid[] = {
mbed_tw_hoehoe 0:83b5c6efd8d7 17 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
mbed_tw_hoehoe 0:83b5c6efd8d7 18 };
mbed_tw_hoehoe 0:83b5c6efd8d7 19
mbed_tw_hoehoe 3:d6f80e11a7f4 20 DiscoveredCharacteristic accelChar;
mbed_tw_hoehoe 0:83b5c6efd8d7 21 UUID serviceUUID(MPU6050_service_uuid);
mbed_tw_hoehoe 0:83b5c6efd8d7 22 UUID accelUUID(MPU6050_Accel_Characteristic_uuid);
mbed_tw_hoehoe 0:83b5c6efd8d7 23
mbed_tw_hoehoe 3:d6f80e11a7f4 24 #define NUMBER_OF_PERIPHERALS 3
mbed_tw_hoehoe 3:d6f80e11a7f4 25
mbed_tw_hoehoe 3:d6f80e11a7f4 26 typedef struct {
mbed_tw_hoehoe 3:d6f80e11a7f4 27 Gap::Handle_t handle;
mbed_tw_hoehoe 3:d6f80e11a7f4 28 Gap::Address_t address;
mbed_tw_hoehoe 3:d6f80e11a7f4 29 bool connected;
mbed_tw_hoehoe 3:d6f80e11a7f4 30 uint8_t* deviceName;
mbed_tw_hoehoe 3:d6f80e11a7f4 31 } peripheral_t;
mbed_tw_hoehoe 3:d6f80e11a7f4 32
mbed_tw_hoehoe 3:d6f80e11a7f4 33 static peripheral_t gs_peripheral[NUMBER_OF_PERIPHERALS];
mbed_tw_hoehoe 3:d6f80e11a7f4 34
mbed_tw_hoehoe 3:d6f80e11a7f4 35 uint32_t ble_advdata_parser(uint8_t type, uint8_t advdata_len, uint8_t *p_advdata, uint8_t *len, uint8_t *p_field_data)
mbed_tw_hoehoe 3:d6f80e11a7f4 36 {
mbed_tw_hoehoe 3:d6f80e11a7f4 37 uint8_t index=0;
mbed_tw_hoehoe 3:d6f80e11a7f4 38 uint8_t field_length, field_type;
mbed_tw_hoehoe 3:d6f80e11a7f4 39
mbed_tw_hoehoe 3:d6f80e11a7f4 40 while(index<advdata_len)
mbed_tw_hoehoe 3:d6f80e11a7f4 41 {
mbed_tw_hoehoe 3:d6f80e11a7f4 42 field_length = p_advdata[index];
mbed_tw_hoehoe 3:d6f80e11a7f4 43 field_type = p_advdata[index+1];
mbed_tw_hoehoe 3:d6f80e11a7f4 44 if(field_type == type)
mbed_tw_hoehoe 3:d6f80e11a7f4 45 {
mbed_tw_hoehoe 3:d6f80e11a7f4 46 memcpy(p_field_data, &p_advdata[index+2], (field_length-1));
mbed_tw_hoehoe 3:d6f80e11a7f4 47 *len = field_length - 1;
mbed_tw_hoehoe 3:d6f80e11a7f4 48 return NRF_SUCCESS;
mbed_tw_hoehoe 3:d6f80e11a7f4 49 }
mbed_tw_hoehoe 3:d6f80e11a7f4 50 index += field_length + 1;
mbed_tw_hoehoe 3:d6f80e11a7f4 51 }
mbed_tw_hoehoe 3:d6f80e11a7f4 52 return NRF_ERROR_NOT_FOUND;
mbed_tw_hoehoe 3:d6f80e11a7f4 53 }
mbed_tw_hoehoe 0:83b5c6efd8d7 54
mbed_tw_hoehoe 0:83b5c6efd8d7 55 void scanCallback(const Gap::AdvertisementCallbackParams_t *params) {
mbed_tw_hoehoe 1:2f1203d70643 56 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
mbed_tw_hoehoe 0:83b5c6efd8d7 57 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
mbed_tw_hoehoe 0:83b5c6efd8d7 58 params->rssi, params->isScanResponse, params->type);
mbed_tw_hoehoe 0:83b5c6efd8d7 59
mbed_tw_hoehoe 3:d6f80e11a7f4 60 uint8_t len;
mbed_tw_hoehoe 3:d6f80e11a7f4 61 uint8_t adv_name[31];
mbed_tw_hoehoe 3:d6f80e11a7f4 62 if( NRF_SUCCESS == ble_advdata_parser(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
mbed_tw_hoehoe 3:d6f80e11a7f4 63 params->advertisingDataLen,
mbed_tw_hoehoe 3:d6f80e11a7f4 64 (uint8_t *)params->advertisingData, &len, adv_name)){
mbed_tw_hoehoe 3:d6f80e11a7f4 65
mbed_tw_hoehoe 3:d6f80e11a7f4 66 for(uint8_t i=0; i<3; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 67 if(gs_peripheral[i].connected == false){
mbed_tw_hoehoe 3:d6f80e11a7f4 68 memcpy(gs_peripheral[i].address, params->peerAddr, sizeof(params->peerAddr));
mbed_tw_hoehoe 3:d6f80e11a7f4 69 gs_peripheral[i].deviceName = adv_name;
mbed_tw_hoehoe 3:d6f80e11a7f4 70 ble.connect(params->peerAddr, BLEProtocol::AddressType::RANDOM_STATIC, NULL, NULL);
mbed_tw_hoehoe 3:d6f80e11a7f4 71 break;
mbed_tw_hoehoe 3:d6f80e11a7f4 72 }
mbed_tw_hoehoe 3:d6f80e11a7f4 73 }
mbed_tw_hoehoe 3:d6f80e11a7f4 74 ble.stopScan();
mbed_tw_hoehoe 3:d6f80e11a7f4 75 }
mbed_tw_hoehoe 0:83b5c6efd8d7 76 }
mbed_tw_hoehoe 0:83b5c6efd8d7 77
mbed_tw_hoehoe 0:83b5c6efd8d7 78 void serviceDiscoveryCallback(const DiscoveredService *service) {
mbed_tw_hoehoe 1:2f1203d70643 79 pc.printf("service found...\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 80 }
mbed_tw_hoehoe 0:83b5c6efd8d7 81
mbed_tw_hoehoe 0:83b5c6efd8d7 82 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
mbed_tw_hoehoe 1:2f1203d70643 83 pc.printf("characteristicDiscoveryCallback\r\n");
mbed_tw_hoehoe 2:4b53d13d9851 84
mbed_tw_hoehoe 3:d6f80e11a7f4 85 accelChar = *characteristicP;
mbed_tw_hoehoe 3:d6f80e11a7f4 86
mbed_tw_hoehoe 3:d6f80e11a7f4 87 for(uint8_t i=0; i<3; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 88 if(gs_peripheral[i].connected){
mbed_tw_hoehoe 3:d6f80e11a7f4 89 ble.gattClient().read(characteristicP->getConnectionHandle(), characteristicP->getValueHandle(), 0);
mbed_tw_hoehoe 2:4b53d13d9851 90 }
mbed_tw_hoehoe 2:4b53d13d9851 91 }
mbed_tw_hoehoe 2:4b53d13d9851 92
mbed_tw_hoehoe 0:83b5c6efd8d7 93 }
mbed_tw_hoehoe 0:83b5c6efd8d7 94
mbed_tw_hoehoe 0:83b5c6efd8d7 95 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
mbed_tw_hoehoe 3:d6f80e11a7f4 96 pc.printf("GAP_EVT_CONNECTED\r\n");
mbed_tw_hoehoe 2:4b53d13d9851 97 if (params->role == Gap::CENTRAL) {
mbed_tw_hoehoe 3:d6f80e11a7f4 98
mbed_tw_hoehoe 3:d6f80e11a7f4 99 for(uint8_t i=0; i<3; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 100 if(gs_peripheral[i].connected == false){
mbed_tw_hoehoe 3:d6f80e11a7f4 101 gs_peripheral[i].handle = params->handle;
mbed_tw_hoehoe 3:d6f80e11a7f4 102 gs_peripheral[i].connected = true;
mbed_tw_hoehoe 3:d6f80e11a7f4 103 break;
mbed_tw_hoehoe 3:d6f80e11a7f4 104 }
mbed_tw_hoehoe 3:d6f80e11a7f4 105 }
mbed_tw_hoehoe 3:d6f80e11a7f4 106
mbed_tw_hoehoe 2:4b53d13d9851 107 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, serviceUUID, accelUUID);
mbed_tw_hoehoe 3:d6f80e11a7f4 108
mbed_tw_hoehoe 2:4b53d13d9851 109 }
mbed_tw_hoehoe 0:83b5c6efd8d7 110 }
mbed_tw_hoehoe 0:83b5c6efd8d7 111
mbed_tw_hoehoe 0:83b5c6efd8d7 112 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
mbed_tw_hoehoe 1:2f1203d70643 113 pc.printf("terminated SD for handle %u\r\n", connectionHandle);
mbed_tw_hoehoe 0:83b5c6efd8d7 114 }
mbed_tw_hoehoe 0:83b5c6efd8d7 115
mbed_tw_hoehoe 0:83b5c6efd8d7 116 void triggerRead(const GattReadCallbackParams *response) {
mbed_tw_hoehoe 3:d6f80e11a7f4 117 pc.printf("triggerRead.....\r\n");
mbed_tw_hoehoe 3:d6f80e11a7f4 118
mbed_tw_hoehoe 3:d6f80e11a7f4 119 pc.printf("len: %d\r\n", response->len);
mbed_tw_hoehoe 3:d6f80e11a7f4 120 const uint8_t *data = response->data;
mbed_tw_hoehoe 3:d6f80e11a7f4 121 pc.printf("data ");
mbed_tw_hoehoe 0:83b5c6efd8d7 122 for(int i=0; i < response->len; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 123 pc.printf("%f ", (float)data[i]);
mbed_tw_hoehoe 0:83b5c6efd8d7 124 }
mbed_tw_hoehoe 1:2f1203d70643 125 pc.printf("\r\n");
mbed_tw_hoehoe 2:4b53d13d9851 126 accelChar.read();
mbed_tw_hoehoe 0:83b5c6efd8d7 127 }
mbed_tw_hoehoe 0:83b5c6efd8d7 128
mbed_tw_hoehoe 3:d6f80e11a7f4 129 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
mbed_tw_hoehoe 1:2f1203d70643 130 pc.printf("disconnected\r\n");
mbed_tw_hoehoe 3:d6f80e11a7f4 131
mbed_tw_hoehoe 3:d6f80e11a7f4 132 for(uint8_t i=0; i<3; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 133 if(gs_peripheral[i].handle == params->handle){
mbed_tw_hoehoe 3:d6f80e11a7f4 134 gs_peripheral[i].connected = false;
mbed_tw_hoehoe 3:d6f80e11a7f4 135 gs_peripheral[i].handle = 0xFFFF;
mbed_tw_hoehoe 3:d6f80e11a7f4 136 }
mbed_tw_hoehoe 3:d6f80e11a7f4 137 }
mbed_tw_hoehoe 2:4b53d13d9851 138 wait(8.0);
mbed_tw_hoehoe 2:4b53d13d9851 139 ble.gap().startScan(scanCallback);
mbed_tw_hoehoe 2:4b53d13d9851 140 }
mbed_tw_hoehoe 2:4b53d13d9851 141
mbed_tw_hoehoe 0:83b5c6efd8d7 142 int main(void) {
mbed_tw_hoehoe 1:2f1203d70643 143 pc.baud(9600);
mbed_tw_hoehoe 0:83b5c6efd8d7 144 wait(8.0);
mbed_tw_hoehoe 1:2f1203d70643 145 pc.printf("start\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 146
mbed_tw_hoehoe 0:83b5c6efd8d7 147 ble.init();
mbed_tw_hoehoe 3:d6f80e11a7f4 148
mbed_tw_hoehoe 0:83b5c6efd8d7 149 ble.onConnection(connectionCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 150 ble.onDisconnection(disconnectionCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 151 ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 152 ble.gattClient().onDataRead(triggerRead);
mbed_tw_hoehoe 0:83b5c6efd8d7 153 //ble.gattClient().onDataWrite(triggerToggledWrite);
mbed_tw_hoehoe 0:83b5c6efd8d7 154
mbed_tw_hoehoe 0:83b5c6efd8d7 155 ble.gap().setScanParams(500, 400);
mbed_tw_hoehoe 0:83b5c6efd8d7 156 ble.gap().startScan(scanCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 157
mbed_tw_hoehoe 0:83b5c6efd8d7 158 while (true) {
mbed_tw_hoehoe 3:d6f80e11a7f4 159 ble.waitForEvent();
mbed_tw_hoehoe 0:83b5c6efd8d7 160 }
mbed_tw_hoehoe 0:83b5c6efd8d7 161 }