Ble Demo with Raspberry PI

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_DEMO_SPI_HARDCODE by HM_IOT_Demo

Committer:
hmiot
Date:
Fri Jan 20 17:59:26 2017 +0000
Revision:
7:66586d2d7cb5
Parent:
6:71e7a446ae6a
Child:
8:285ebd0e83fb
BLE Central for Magic Light

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hmiot 7:66586d2d7cb5 1 /************************************************************************
hmiot 7:66586d2d7cb5 2 * *
hmiot 7:66586d2d7cb5 3 * *
hmiot 7:66586d2d7cb5 4 ************************************************************************/
hmiot 6:71e7a446ae6a 5
hmiot 7:66586d2d7cb5 6 #include "hm_config.h"
hmiot 7:66586d2d7cb5 7
hmiot 6:71e7a446ae6a 8 static const uint16_t UNIT_1_25_MS = 1250;
hmiot 6:71e7a446ae6a 9 static const uint16_t UNIT_10_MS = 10000;
hmiot 5:f6b6bf98c686 10 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
hmiot 6:71e7a446ae6a 11 #define MIN_CONNECTION_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS)
hmiot 6:71e7a446ae6a 12 #define MAX_CONNECTION_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS)
hmiot 6:71e7a446ae6a 13 #define SLAVE_LATENCY 4
hmiot 6:71e7a446ae6a 14 #define SUPERVISION_TIMEOUT MSEC_TO_UNITS(6000, UNIT_10_MS)
hmiot 5:f6b6bf98c686 15
hmiot 6:71e7a446ae6a 16 typedef unsigned char byte_char;
mbed_tw_hoehoe 0:83b5c6efd8d7 17
hmiot 6:71e7a446ae6a 18 DiscoveredCharacteristic lightCharacteristic;
hmiot 5:f6b6bf98c686 19
hmiot 6:71e7a446ae6a 20 static const Gap::ConnectionParams_t m_conn_params ={
hmiot 5:f6b6bf98c686 21 (uint16_t)MIN_CONNECTION_INTERVAL,
hmiot 5:f6b6bf98c686 22 (uint16_t)MAX_CONNECTION_INTERVAL,
hmiot 5:f6b6bf98c686 23 (uint16_t)SLAVE_LATENCY,
hmiot 5:f6b6bf98c686 24 (uint16_t)SUPERVISION_TIMEOUT
hmiot 5:f6b6bf98c686 25 };
mbed_tw_hoehoe 3:d6f80e11a7f4 26
hmiot 6:71e7a446ae6a 27 GapScanningParams m_scan_params;
hmiot 6:71e7a446ae6a 28 Serial pc(USBTX, USBRX);
hmiot 6:71e7a446ae6a 29
hmiot 6:71e7a446ae6a 30 static ble_error_t error_status;
mbed_tw_hoehoe 3:d6f80e11a7f4 31
hmiot 6:71e7a446ae6a 32 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 33 uint8_t con_status =0;
hmiot 6:71e7a446ae6a 34 // uint8_t i;
hmiot 6:71e7a446ae6a 35 Gap::GapState_t con_state;
hmiot 6:71e7a446ae6a 36 //0x51 for Magic light
hmiot 6:71e7a446ae6a 37 //0x5A
hmiot 6:71e7a446ae6a 38 if (params->peerAddr[0] != 0x51) { // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
hmiot 6:71e7a446ae6a 39 return;
mbed_tw_hoehoe 3:d6f80e11a7f4 40 }
mbed_tw_hoehoe 1:2f1203d70643 41 pc.printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
mbed_tw_hoehoe 0:83b5c6efd8d7 42 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
mbed_tw_hoehoe 0:83b5c6efd8d7 43 params->rssi, params->isScanResponse, params->type);
hmiot 6:71e7a446ae6a 44 pc.printf("Data Length : %d\r\n",params->advertisingDataLen);
hmiot 5:f6b6bf98c686 45
hmiot 6:71e7a446ae6a 46 con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL);
hmiot 6:71e7a446ae6a 47 pc.printf("Connection Status : %d\r\n",con_status);
hmiot 6:71e7a446ae6a 48 con_state = BLE::Instance().gap().getState();
hmiot 6:71e7a446ae6a 49 pc.printf("Connection state : %d\r\n",con_state);
hmiot 5:f6b6bf98c686 50
hmiot 6:71e7a446ae6a 51 // }
hmiot 6:71e7a446ae6a 52 BLE::Instance().gap().stopScan();
mbed_tw_hoehoe 0:83b5c6efd8d7 53 }
mbed_tw_hoehoe 0:83b5c6efd8d7 54
mbed_tw_hoehoe 0:83b5c6efd8d7 55 void serviceDiscoveryCallback(const DiscoveredService *service) {
hmiot 6:71e7a446ae6a 56 pc.printf("Service Discovery Callback\r\n");
hmiot 6:71e7a446ae6a 57 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
hmiot 6:71e7a446ae6a 58 pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
hmiot 6:71e7a446ae6a 59 } else {
hmiot 6:71e7a446ae6a 60 pc.printf("S UUID-");
hmiot 6:71e7a446ae6a 61 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
hmiot 6:71e7a446ae6a 62 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
hmiot 6:71e7a446ae6a 63 printf("%02x", longUUIDBytes[i]);
hmiot 6:71e7a446ae6a 64 }
hmiot 6:71e7a446ae6a 65 pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
hmiot 6:71e7a446ae6a 66 }
mbed_tw_hoehoe 0:83b5c6efd8d7 67 }
mbed_tw_hoehoe 0:83b5c6efd8d7 68
mbed_tw_hoehoe 0:83b5c6efd8d7 69 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
hmiot 6:71e7a446ae6a 70 pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
hmiot 6:71e7a446ae6a 71 uint8_t write_data []={0x56,0xff,0x00,0x00,0x00,0xf0,0xaa};
hmiot 6:71e7a446ae6a 72 if (characteristicP->getUUID().getShortUUID() == 0xffe9) { /* !ALERT! Alter this filter to suit your device. */
hmiot 6:71e7a446ae6a 73 lightCharacteristic = *characteristicP;
hmiot 6:71e7a446ae6a 74 pc.printf("Matched char UUID\r\n");
hmiot 6:71e7a446ae6a 75 printf("Value Handle = %d\r\n",characteristicP->getDeclHandle());
hmiot 6:71e7a446ae6a 76 printf("Value Handle = %d and data size : %d\r\n",characteristicP->getValueHandle(),sizeof(write_data));
hmiot 6:71e7a446ae6a 77 lightCharacteristic.writeWoResponse(sizeof(write_data), write_data);
hmiot 6:71e7a446ae6a 78 //error_status = characteristicP->write(sizeof(write_data), write_data);
hmiot 6:71e7a446ae6a 79 printf("Write Status : %d\r\n",error_status);
mbed_tw_hoehoe 2:4b53d13d9851 80 }
mbed_tw_hoehoe 0:83b5c6efd8d7 81 }
mbed_tw_hoehoe 0:83b5c6efd8d7 82
mbed_tw_hoehoe 0:83b5c6efd8d7 83 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
mbed_tw_hoehoe 1:2f1203d70643 84 pc.printf("terminated SD for handle %u\r\n", connectionHandle);
mbed_tw_hoehoe 0:83b5c6efd8d7 85 }
mbed_tw_hoehoe 0:83b5c6efd8d7 86
hmiot 6:71e7a446ae6a 87 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 88 pc.printf("Connection Callback\n\r");
hmiot 6:71e7a446ae6a 89 if (params->role == Gap::CENTRAL) {
hmiot 6:71e7a446ae6a 90 pc.printf("Service and characterstics discovery callback\r\n");
hmiot 6:71e7a446ae6a 91 BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
hmiot 6:71e7a446ae6a 92 BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9);
mbed_tw_hoehoe 0:83b5c6efd8d7 93 }
hmiot 6:71e7a446ae6a 94 }
hmiot 6:71e7a446ae6a 95
hmiot 6:71e7a446ae6a 96 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 97 pc.printf("disconnected\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 98 }
mbed_tw_hoehoe 0:83b5c6efd8d7 99
hmiot 6:71e7a446ae6a 100
hmiot 6:71e7a446ae6a 101 /**
hmiot 6:71e7a446ae6a 102 * Callback triggered when the ble initialization process has finished
hmiot 6:71e7a446ae6a 103 */
hmiot 6:71e7a446ae6a 104 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
hmiot 6:71e7a446ae6a 105 {
hmiot 6:71e7a446ae6a 106 BLE& ble = params->ble;
hmiot 6:71e7a446ae6a 107
hmiot 6:71e7a446ae6a 108 pc.printf("Ble Init\n\r");
hmiot 6:71e7a446ae6a 109 /* Ensure that it is the default instance of BLE */
hmiot 6:71e7a446ae6a 110 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
hmiot 6:71e7a446ae6a 111 return;
hmiot 6:71e7a446ae6a 112 }
hmiot 6:71e7a446ae6a 113
hmiot 6:71e7a446ae6a 114 ble.gap().onConnection(connectionCallback);
hmiot 6:71e7a446ae6a 115 ble.gap().onDisconnection(disconnectionCallback);
mbed_tw_hoehoe 3:d6f80e11a7f4 116
hmiot 6:71e7a446ae6a 117 ble.gap().setScanParams(1000, 800);
hmiot 6:71e7a446ae6a 118 ble.gap().startScan(advertisementCallback);
mbed_tw_hoehoe 2:4b53d13d9851 119 }
mbed_tw_hoehoe 2:4b53d13d9851 120
mbed_tw_hoehoe 0:83b5c6efd8d7 121 int main(void) {
mbed_tw_hoehoe 1:2f1203d70643 122 pc.baud(9600);
mbed_tw_hoehoe 0:83b5c6efd8d7 123 wait(8.0);
hmiot 6:71e7a446ae6a 124 pc.printf("Start\n\r");
hmiot 6:71e7a446ae6a 125 BLE &ble = BLE::Instance();
hmiot 6:71e7a446ae6a 126
hmiot 6:71e7a446ae6a 127 ble.init(bleInitComplete);
hmiot 6:71e7a446ae6a 128
mbed_tw_hoehoe 0:83b5c6efd8d7 129 while (true) {
hmiot 6:71e7a446ae6a 130 ble.waitForEvent();
mbed_tw_hoehoe 0:83b5c6efd8d7 131 }
hmiot 6:71e7a446ae6a 132 }