Central Test.

Dependencies:   BLE_API mbed nRF51822

Committer:
hmiot
Date:
Thu Jan 19 12:21:34 2017 +0000
Revision:
5:f6b6bf98c686
Parent:
3:d6f80e11a7f4
Child:
6:71e7a446ae6a
Updated

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
hmiot 5:f6b6bf98c686 12 /*const uint8_t MPU6050_service_uuid[] = {
hmiot 5:f6b6bf98c686 13 0x45,0x35,0x56,0x80,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
hmiot 5:f6b6bf98c686 14 };*/
mbed_tw_hoehoe 0:83b5c6efd8d7 15 const uint8_t MPU6050_service_uuid[] = {
hmiot 5:f6b6bf98c686 16 0x00,0x00,0xFF,0xE5,0x00,0x00,0x10,0x00,0x80,0x00,0x00,0x80,0x5F,0x9B,0x34,0xFB
hmiot 5:f6b6bf98c686 17 };
hmiot 5:f6b6bf98c686 18 /*const uint8_t MPU6050_Accel_Characteristic_uuid[] = {
hmiot 5:f6b6bf98c686 19 0x45,0x35,0x56,0x81,0x0F,0xD8,0x5F,0xB5,0x51,0x48,0x30,0x27,0x06,0x9B,0x3F,0xD9
hmiot 5:f6b6bf98c686 20 };*/
hmiot 5:f6b6bf98c686 21
hmiot 5:f6b6bf98c686 22 const uint8_t MPU6050_Accel_Characteristic_uuid[] = {
hmiot 5:f6b6bf98c686 23 0x00,0x00,0xFF,0xE6,0x00,0x00,0x10,0x00,0x80,0x00,0x00,0x80,0x5F,0x9B,0x34,0xFB
hmiot 5:f6b6bf98c686 24 };
hmiot 5:f6b6bf98c686 25 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
hmiot 5:f6b6bf98c686 26
hmiot 5:f6b6bf98c686 27 enum
hmiot 5:f6b6bf98c686 28 {
hmiot 5:f6b6bf98c686 29 UNIT_0_625_MS = 625,
hmiot 5:f6b6bf98c686 30 UNIT_1_25_MS = 1250,
hmiot 5:f6b6bf98c686 31 UNIT_10_MS = 10000
mbed_tw_hoehoe 0:83b5c6efd8d7 32 };
mbed_tw_hoehoe 0:83b5c6efd8d7 33
hmiot 5:f6b6bf98c686 34 #define NUMBER_OF_PERIPHERALS 3
mbed_tw_hoehoe 0:83b5c6efd8d7 35
hmiot 5:f6b6bf98c686 36 #define MIN_CONNECTION_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS)
hmiot 5:f6b6bf98c686 37 #define MAX_CONNECTION_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS)
hmiot 5:f6b6bf98c686 38 #define SLAVE_LATENCY 0
hmiot 5:f6b6bf98c686 39 #define SUPERVISION_TIMEOUT MSEC_TO_UNITS(4000, UNIT_10_MS)
hmiot 5:f6b6bf98c686 40
hmiot 5:f6b6bf98c686 41 //const char Dev_Mac_Addr[15]="5065838DA351";
mbed_tw_hoehoe 3:d6f80e11a7f4 42 DiscoveredCharacteristic accelChar;
mbed_tw_hoehoe 0:83b5c6efd8d7 43 UUID serviceUUID(MPU6050_service_uuid);
mbed_tw_hoehoe 0:83b5c6efd8d7 44 UUID accelUUID(MPU6050_Accel_Characteristic_uuid);
mbed_tw_hoehoe 0:83b5c6efd8d7 45
mbed_tw_hoehoe 3:d6f80e11a7f4 46
hmiot 5:f6b6bf98c686 47 static const Gap::ConnectionParams_t m_connection_param =
hmiot 5:f6b6bf98c686 48 {
hmiot 5:f6b6bf98c686 49 (uint16_t)MIN_CONNECTION_INTERVAL,
hmiot 5:f6b6bf98c686 50 (uint16_t)MAX_CONNECTION_INTERVAL,
hmiot 5:f6b6bf98c686 51 (uint16_t)SLAVE_LATENCY,
hmiot 5:f6b6bf98c686 52 (uint16_t)SUPERVISION_TIMEOUT
hmiot 5:f6b6bf98c686 53 };
mbed_tw_hoehoe 3:d6f80e11a7f4 54 typedef struct {
mbed_tw_hoehoe 3:d6f80e11a7f4 55 Gap::Handle_t handle;
mbed_tw_hoehoe 3:d6f80e11a7f4 56 Gap::Address_t address;
mbed_tw_hoehoe 3:d6f80e11a7f4 57 bool connected;
mbed_tw_hoehoe 3:d6f80e11a7f4 58 uint8_t* deviceName;
mbed_tw_hoehoe 3:d6f80e11a7f4 59 } peripheral_t;
hmiot 5:f6b6bf98c686 60 static const char adv_name[] = "LEDBlue-838DA351";
hmiot 5:f6b6bf98c686 61 static peripheral_t gs_peripheral[NUMBER_OF_PERIPHERALS];
mbed_tw_hoehoe 3:d6f80e11a7f4 62
mbed_tw_hoehoe 3:d6f80e11a7f4 63
mbed_tw_hoehoe 3:d6f80e11a7f4 64 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 65 {
mbed_tw_hoehoe 3:d6f80e11a7f4 66 uint8_t index=0;
mbed_tw_hoehoe 3:d6f80e11a7f4 67 uint8_t field_length, field_type;
hmiot 5:f6b6bf98c686 68 pc.printf("advdata_parser1\n\r");
hmiot 5:f6b6bf98c686 69 pc.printf("type : %d Length : %d\n\r",type, advdata_len);
mbed_tw_hoehoe 3:d6f80e11a7f4 70 while(index<advdata_len)
hmiot 5:f6b6bf98c686 71 {pc.printf("advdata_parser loop\n\r");
mbed_tw_hoehoe 3:d6f80e11a7f4 72 field_length = p_advdata[index];
mbed_tw_hoehoe 3:d6f80e11a7f4 73 field_type = p_advdata[index+1];
hmiot 5:f6b6bf98c686 74 pc.printf("FieldLen : %d\n\r",p_advdata[index]);
hmiot 5:f6b6bf98c686 75 pc.printf("Field_Type : %d\n\r",p_advdata[index+1]);
hmiot 5:f6b6bf98c686 76 pc.printf("Fieldtype : %d\n\r",field_type);
mbed_tw_hoehoe 3:d6f80e11a7f4 77 if(field_type == type)
mbed_tw_hoehoe 3:d6f80e11a7f4 78 {
hmiot 5:f6b6bf98c686 79 pc.printf("Copy the data\n\r");
hmiot 5:f6b6bf98c686 80 //memcpy(p_field_data, &p_advdata[index+2], (field_length-1));
hmiot 5:f6b6bf98c686 81 p_field_data = &p_advdata[index+2];
mbed_tw_hoehoe 3:d6f80e11a7f4 82 *len = field_length - 1;
mbed_tw_hoehoe 3:d6f80e11a7f4 83 return NRF_SUCCESS;
mbed_tw_hoehoe 3:d6f80e11a7f4 84 }
hmiot 5:f6b6bf98c686 85 index += field_length;
mbed_tw_hoehoe 3:d6f80e11a7f4 86 }
mbed_tw_hoehoe 3:d6f80e11a7f4 87 return NRF_ERROR_NOT_FOUND;
mbed_tw_hoehoe 3:d6f80e11a7f4 88 }
mbed_tw_hoehoe 0:83b5c6efd8d7 89
mbed_tw_hoehoe 0:83b5c6efd8d7 90 void scanCallback(const Gap::AdvertisementCallbackParams_t *params) {
mbed_tw_hoehoe 1:2f1203d70643 91 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
mbed_tw_hoehoe 0:83b5c6efd8d7 92 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
mbed_tw_hoehoe 0:83b5c6efd8d7 93 params->rssi, params->isScanResponse, params->type);
hmiot 5:f6b6bf98c686 94 //pc.printf("params->peerAddr : %s \n\r",params->peerAddr);
hmiot 5:f6b6bf98c686 95 // char peer_Addr[15];
hmiot 5:f6b6bf98c686 96
hmiot 5:f6b6bf98c686 97 // pc.printf("params->advertisingData : %02x %02x\n\r",params->advertisingData[0],params->advertisingData[1]);
hmiot 5:f6b6bf98c686 98 pc.printf("Data Lenght = %d\n\r",params->advertisingDataLen);
mbed_tw_hoehoe 3:d6f80e11a7f4 99 uint8_t len;
hmiot 5:f6b6bf98c686 100
hmiot 5:f6b6bf98c686 101 uint8_t status;
hmiot 5:f6b6bf98c686 102 uint8_t stop_status;
hmiot 5:f6b6bf98c686 103 pc.printf("Connected1\n\r");
hmiot 5:f6b6bf98c686 104 /* if( NRF_SUCCESS == ble_advdata_parser(BLE_GAP_AD_TYPE_COMPLETE_LOCAL_NAME,
mbed_tw_hoehoe 3:d6f80e11a7f4 105 params->advertisingDataLen,
hmiot 5:f6b6bf98c686 106 (uint8_t *)params->advertisingData, &len, adv_name))
hmiot 5:f6b6bf98c686 107 {*/
hmiot 5:f6b6bf98c686 108 pc.printf("Connected2\n\r");
hmiot 5:f6b6bf98c686 109 uint8_t i=0;
hmiot 5:f6b6bf98c686 110 // for(uint8_t i=0; i<3; i++){
hmiot 5:f6b6bf98c686 111 //if(gs_peripheral[i].connected == false)
hmiot 5:f6b6bf98c686 112 if (params->peerAddr[0] == 0x51){
hmiot 5:f6b6bf98c686 113 pc.printf("Connected3\n\r");
mbed_tw_hoehoe 3:d6f80e11a7f4 114 memcpy(gs_peripheral[i].address, params->peerAddr, sizeof(params->peerAddr));
hmiot 5:f6b6bf98c686 115 gs_peripheral[i].deviceName = (uint8_t*)adv_name;
hmiot 5:f6b6bf98c686 116 ble.gap().setDeviceName(gs_peripheral[i].deviceName);
hmiot 5:f6b6bf98c686 117 status = ble.gap().connect(params->peerAddr, BLEProtocol::AddressType::RANDOM_STATIC, &m_connection_param, NULL);
hmiot 5:f6b6bf98c686 118 pc.printf("Connection Status = %d\n\r",status);
hmiot 5:f6b6bf98c686 119 pc.printf("stop scan\n\r");
hmiot 5:f6b6bf98c686 120 stop_status = ble.gap().stopScan();
hmiot 5:f6b6bf98c686 121 pc.printf("stop Status = %d\n\r",stop_status);
hmiot 5:f6b6bf98c686 122 // break;
mbed_tw_hoehoe 3:d6f80e11a7f4 123 }
hmiot 5:f6b6bf98c686 124 // }
hmiot 5:f6b6bf98c686 125
mbed_tw_hoehoe 3:d6f80e11a7f4 126 ble.stopScan();
hmiot 5:f6b6bf98c686 127 // }
mbed_tw_hoehoe 0:83b5c6efd8d7 128 }
mbed_tw_hoehoe 0:83b5c6efd8d7 129
mbed_tw_hoehoe 0:83b5c6efd8d7 130 void serviceDiscoveryCallback(const DiscoveredService *service) {
mbed_tw_hoehoe 1:2f1203d70643 131 pc.printf("service found...\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 132 }
mbed_tw_hoehoe 0:83b5c6efd8d7 133
mbed_tw_hoehoe 0:83b5c6efd8d7 134 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
mbed_tw_hoehoe 1:2f1203d70643 135 pc.printf("characteristicDiscoveryCallback\r\n");
mbed_tw_hoehoe 2:4b53d13d9851 136
mbed_tw_hoehoe 3:d6f80e11a7f4 137 accelChar = *characteristicP;
mbed_tw_hoehoe 3:d6f80e11a7f4 138
mbed_tw_hoehoe 3:d6f80e11a7f4 139 for(uint8_t i=0; i<3; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 140 if(gs_peripheral[i].connected){
mbed_tw_hoehoe 3:d6f80e11a7f4 141 ble.gattClient().read(characteristicP->getConnectionHandle(), characteristicP->getValueHandle(), 0);
mbed_tw_hoehoe 2:4b53d13d9851 142 }
mbed_tw_hoehoe 2:4b53d13d9851 143 }
mbed_tw_hoehoe 2:4b53d13d9851 144
mbed_tw_hoehoe 0:83b5c6efd8d7 145 }
mbed_tw_hoehoe 0:83b5c6efd8d7 146
mbed_tw_hoehoe 0:83b5c6efd8d7 147 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
mbed_tw_hoehoe 3:d6f80e11a7f4 148 pc.printf("GAP_EVT_CONNECTED\r\n");
mbed_tw_hoehoe 2:4b53d13d9851 149 if (params->role == Gap::CENTRAL) {
mbed_tw_hoehoe 3:d6f80e11a7f4 150
mbed_tw_hoehoe 3:d6f80e11a7f4 151 for(uint8_t i=0; i<3; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 152 if(gs_peripheral[i].connected == false){
mbed_tw_hoehoe 3:d6f80e11a7f4 153 gs_peripheral[i].handle = params->handle;
mbed_tw_hoehoe 3:d6f80e11a7f4 154 gs_peripheral[i].connected = true;
mbed_tw_hoehoe 3:d6f80e11a7f4 155 break;
mbed_tw_hoehoe 3:d6f80e11a7f4 156 }
mbed_tw_hoehoe 3:d6f80e11a7f4 157 }
mbed_tw_hoehoe 3:d6f80e11a7f4 158
mbed_tw_hoehoe 2:4b53d13d9851 159 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, serviceUUID, accelUUID);
mbed_tw_hoehoe 3:d6f80e11a7f4 160
mbed_tw_hoehoe 2:4b53d13d9851 161 }
mbed_tw_hoehoe 0:83b5c6efd8d7 162 }
mbed_tw_hoehoe 0:83b5c6efd8d7 163
mbed_tw_hoehoe 0:83b5c6efd8d7 164 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
mbed_tw_hoehoe 1:2f1203d70643 165 pc.printf("terminated SD for handle %u\r\n", connectionHandle);
mbed_tw_hoehoe 0:83b5c6efd8d7 166 }
mbed_tw_hoehoe 0:83b5c6efd8d7 167
mbed_tw_hoehoe 0:83b5c6efd8d7 168 void triggerRead(const GattReadCallbackParams *response) {
mbed_tw_hoehoe 3:d6f80e11a7f4 169 pc.printf("triggerRead.....\r\n");
mbed_tw_hoehoe 3:d6f80e11a7f4 170
mbed_tw_hoehoe 3:d6f80e11a7f4 171 pc.printf("len: %d\r\n", response->len);
mbed_tw_hoehoe 3:d6f80e11a7f4 172 const uint8_t *data = response->data;
mbed_tw_hoehoe 3:d6f80e11a7f4 173 pc.printf("data ");
mbed_tw_hoehoe 0:83b5c6efd8d7 174 for(int i=0; i < response->len; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 175 pc.printf("%f ", (float)data[i]);
mbed_tw_hoehoe 0:83b5c6efd8d7 176 }
mbed_tw_hoehoe 1:2f1203d70643 177 pc.printf("\r\n");
mbed_tw_hoehoe 2:4b53d13d9851 178 accelChar.read();
mbed_tw_hoehoe 0:83b5c6efd8d7 179 }
mbed_tw_hoehoe 0:83b5c6efd8d7 180
mbed_tw_hoehoe 3:d6f80e11a7f4 181 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
mbed_tw_hoehoe 1:2f1203d70643 182 pc.printf("disconnected\r\n");
mbed_tw_hoehoe 3:d6f80e11a7f4 183
mbed_tw_hoehoe 3:d6f80e11a7f4 184 for(uint8_t i=0; i<3; i++){
mbed_tw_hoehoe 3:d6f80e11a7f4 185 if(gs_peripheral[i].handle == params->handle){
mbed_tw_hoehoe 3:d6f80e11a7f4 186 gs_peripheral[i].connected = false;
mbed_tw_hoehoe 3:d6f80e11a7f4 187 gs_peripheral[i].handle = 0xFFFF;
mbed_tw_hoehoe 3:d6f80e11a7f4 188 }
mbed_tw_hoehoe 3:d6f80e11a7f4 189 }
mbed_tw_hoehoe 2:4b53d13d9851 190 wait(8.0);
hmiot 5:f6b6bf98c686 191 // ble.gap().startScan(scanCallback);
mbed_tw_hoehoe 2:4b53d13d9851 192 }
mbed_tw_hoehoe 2:4b53d13d9851 193
mbed_tw_hoehoe 0:83b5c6efd8d7 194 int main(void) {
mbed_tw_hoehoe 1:2f1203d70643 195 pc.baud(9600);
mbed_tw_hoehoe 0:83b5c6efd8d7 196 wait(8.0);
mbed_tw_hoehoe 1:2f1203d70643 197 pc.printf("start\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 198
mbed_tw_hoehoe 0:83b5c6efd8d7 199 ble.init();
mbed_tw_hoehoe 3:d6f80e11a7f4 200
mbed_tw_hoehoe 0:83b5c6efd8d7 201 ble.onConnection(connectionCallback);
mbed_tw_hoehoe 0:83b5c6efd8d7 202 ble.onDisconnection(disconnectionCallback);
hmiot 5:f6b6bf98c686 203 // ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
hmiot 5:f6b6bf98c686 204 // ble.gattClient().onDataRead(triggerRead);
mbed_tw_hoehoe 0:83b5c6efd8d7 205 //ble.gattClient().onDataWrite(triggerToggledWrite);
hmiot 5:f6b6bf98c686 206
hmiot 5:f6b6bf98c686 207 ble.gap().setScanParams(600, 400);
mbed_tw_hoehoe 0:83b5c6efd8d7 208 ble.gap().startScan(scanCallback);
hmiot 5:f6b6bf98c686 209 //ble.startAdvertising();
mbed_tw_hoehoe 0:83b5c6efd8d7 210 while (true) {
mbed_tw_hoehoe 3:d6f80e11a7f4 211 ble.waitForEvent();
mbed_tw_hoehoe 0:83b5c6efd8d7 212 }
mbed_tw_hoehoe 0:83b5c6efd8d7 213 }