Central Test.

Dependencies:   BLE_API mbed nRF51822

Committer:
hmiot
Date:
Fri Jan 20 14:22:56 2017 +0000
Revision:
6:71e7a446ae6a
Parent:
5:f6b6bf98c686
Working with BLE Light..

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hmiot 6:71e7a446ae6a 1 /* mbed Microcontroller Library
hmiot 6:71e7a446ae6a 2 * Copyright (c) 2006-2015 ARM Limited
hmiot 6:71e7a446ae6a 3 *
hmiot 6:71e7a446ae6a 4 * Licensed under the Apache License, Version 2.0 (the "License");
hmiot 6:71e7a446ae6a 5 * you may not use this file except in compliance with the License.
hmiot 6:71e7a446ae6a 6 * You may obtain a copy of the License at
hmiot 6:71e7a446ae6a 7 *
hmiot 6:71e7a446ae6a 8 * http://www.apache.org/licenses/LICENSE-2.0
hmiot 6:71e7a446ae6a 9 *
hmiot 6:71e7a446ae6a 10 * Unless required by applicable law or agreed to in writing, software
hmiot 6:71e7a446ae6a 11 * distributed under the License is distributed on an "AS IS" BASIS,
hmiot 6:71e7a446ae6a 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
hmiot 6:71e7a446ae6a 13 * See the License for the specific language governing permissions and
hmiot 6:71e7a446ae6a 14 * limitations under the License.
hmiot 6:71e7a446ae6a 15 */
hmiot 6:71e7a446ae6a 16
mbed_tw_hoehoe 0:83b5c6efd8d7 17 #include "mbed.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 18 #include "ble/BLE.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 19 #include "ble/DiscoveredCharacteristic.h"
mbed_tw_hoehoe 0:83b5c6efd8d7 20 #include "ble/DiscoveredService.h"
hmiot 6:71e7a446ae6a 21 #include "ble/Gap.h"
hmiot 6:71e7a446ae6a 22 #include "nRF5xGap.h"
hmiot 6:71e7a446ae6a 23 static const uint16_t UNIT_1_25_MS = 1250;
hmiot 6:71e7a446ae6a 24 static const uint16_t UNIT_10_MS = 10000;
hmiot 5:f6b6bf98c686 25 #define MSEC_TO_UNITS(TIME, RESOLUTION) (((TIME) * 1000) / (RESOLUTION))
hmiot 6:71e7a446ae6a 26 #define MIN_CONNECTION_INTERVAL MSEC_TO_UNITS(7.5, UNIT_1_25_MS)
hmiot 6:71e7a446ae6a 27 #define MAX_CONNECTION_INTERVAL MSEC_TO_UNITS(30, UNIT_1_25_MS)
hmiot 6:71e7a446ae6a 28 #define SLAVE_LATENCY 4
hmiot 6:71e7a446ae6a 29 #define SUPERVISION_TIMEOUT MSEC_TO_UNITS(6000, UNIT_10_MS)
hmiot 5:f6b6bf98c686 30
hmiot 6:71e7a446ae6a 31 typedef unsigned char byte_char;
mbed_tw_hoehoe 0:83b5c6efd8d7 32
hmiot 6:71e7a446ae6a 33 DiscoveredCharacteristic lightCharacteristic;
hmiot 5:f6b6bf98c686 34
hmiot 6:71e7a446ae6a 35 static const Gap::ConnectionParams_t m_conn_params ={
hmiot 5:f6b6bf98c686 36 (uint16_t)MIN_CONNECTION_INTERVAL,
hmiot 5:f6b6bf98c686 37 (uint16_t)MAX_CONNECTION_INTERVAL,
hmiot 5:f6b6bf98c686 38 (uint16_t)SLAVE_LATENCY,
hmiot 5:f6b6bf98c686 39 (uint16_t)SUPERVISION_TIMEOUT
hmiot 5:f6b6bf98c686 40 };
mbed_tw_hoehoe 3:d6f80e11a7f4 41
hmiot 6:71e7a446ae6a 42 GapScanningParams m_scan_params;
hmiot 6:71e7a446ae6a 43 Serial pc(USBTX, USBRX);
hmiot 6:71e7a446ae6a 44
hmiot 6:71e7a446ae6a 45 static ble_error_t error_status;
mbed_tw_hoehoe 3:d6f80e11a7f4 46
hmiot 6:71e7a446ae6a 47 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 48 uint8_t con_status =0;
hmiot 6:71e7a446ae6a 49 // uint8_t i;
hmiot 6:71e7a446ae6a 50 Gap::GapState_t con_state;
hmiot 6:71e7a446ae6a 51 //0x51 for Magic light
hmiot 6:71e7a446ae6a 52 //0x5A
hmiot 6:71e7a446ae6a 53 if (params->peerAddr[0] != 0x51) { // 0x2F for red bear1.5 /* !ALERT! Alter this filter to suit your device. */
hmiot 6:71e7a446ae6a 54 return;
mbed_tw_hoehoe 3:d6f80e11a7f4 55 }
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);
hmiot 6:71e7a446ae6a 59 pc.printf("Data Length : %d\r\n",params->advertisingDataLen);
hmiot 5:f6b6bf98c686 60
hmiot 6:71e7a446ae6a 61 con_status = BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_PUBLIC, NULL, NULL);
hmiot 6:71e7a446ae6a 62 pc.printf("Connection Status : %d\r\n",con_status);
hmiot 6:71e7a446ae6a 63 con_state = BLE::Instance().gap().getState();
hmiot 6:71e7a446ae6a 64 pc.printf("Connection state : %d\r\n",con_state);
hmiot 5:f6b6bf98c686 65
hmiot 6:71e7a446ae6a 66 // }
hmiot 6:71e7a446ae6a 67 BLE::Instance().gap().stopScan();
mbed_tw_hoehoe 0:83b5c6efd8d7 68 }
mbed_tw_hoehoe 0:83b5c6efd8d7 69
mbed_tw_hoehoe 0:83b5c6efd8d7 70 void serviceDiscoveryCallback(const DiscoveredService *service) {
hmiot 6:71e7a446ae6a 71 pc.printf("Service Discovery Callback\r\n");
hmiot 6:71e7a446ae6a 72 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
hmiot 6:71e7a446ae6a 73 pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
hmiot 6:71e7a446ae6a 74 } else {
hmiot 6:71e7a446ae6a 75 pc.printf("S UUID-");
hmiot 6:71e7a446ae6a 76 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
hmiot 6:71e7a446ae6a 77 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
hmiot 6:71e7a446ae6a 78 printf("%02x", longUUIDBytes[i]);
hmiot 6:71e7a446ae6a 79 }
hmiot 6:71e7a446ae6a 80 pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
hmiot 6:71e7a446ae6a 81 }
mbed_tw_hoehoe 0:83b5c6efd8d7 82 }
mbed_tw_hoehoe 0:83b5c6efd8d7 83
mbed_tw_hoehoe 0:83b5c6efd8d7 84 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
hmiot 6:71e7a446ae6a 85 pc.printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
hmiot 6:71e7a446ae6a 86 uint8_t write_data []={0x56,0xff,0x00,0x00,0x00,0xf0,0xaa};
hmiot 6:71e7a446ae6a 87 if (characteristicP->getUUID().getShortUUID() == 0xffe9) { /* !ALERT! Alter this filter to suit your device. */
hmiot 6:71e7a446ae6a 88 lightCharacteristic = *characteristicP;
hmiot 6:71e7a446ae6a 89 pc.printf("Matched char UUID\r\n");
hmiot 6:71e7a446ae6a 90 printf("Value Handle = %d\r\n",characteristicP->getDeclHandle());
hmiot 6:71e7a446ae6a 91 printf("Value Handle = %d and data size : %d\r\n",characteristicP->getValueHandle(),sizeof(write_data));
hmiot 6:71e7a446ae6a 92 lightCharacteristic.writeWoResponse(sizeof(write_data), write_data);
hmiot 6:71e7a446ae6a 93 //error_status = characteristicP->write(sizeof(write_data), write_data);
hmiot 6:71e7a446ae6a 94 printf("Write Status : %d\r\n",error_status);
mbed_tw_hoehoe 2:4b53d13d9851 95 }
mbed_tw_hoehoe 0:83b5c6efd8d7 96 }
mbed_tw_hoehoe 0:83b5c6efd8d7 97
mbed_tw_hoehoe 0:83b5c6efd8d7 98 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
mbed_tw_hoehoe 1:2f1203d70643 99 pc.printf("terminated SD for handle %u\r\n", connectionHandle);
mbed_tw_hoehoe 0:83b5c6efd8d7 100 }
mbed_tw_hoehoe 0:83b5c6efd8d7 101
hmiot 6:71e7a446ae6a 102 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 103 pc.printf("Connection Callback\n\r");
hmiot 6:71e7a446ae6a 104 if (params->role == Gap::CENTRAL) {
hmiot 6:71e7a446ae6a 105 pc.printf("Service and characterstics discovery callback\r\n");
hmiot 6:71e7a446ae6a 106 BLE::Instance().gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
hmiot 6:71e7a446ae6a 107 BLE::Instance().gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xffe5, 0xffe9);
mbed_tw_hoehoe 0:83b5c6efd8d7 108 }
hmiot 6:71e7a446ae6a 109 }
hmiot 6:71e7a446ae6a 110
hmiot 6:71e7a446ae6a 111 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) {
hmiot 6:71e7a446ae6a 112 pc.printf("disconnected\r\n");
mbed_tw_hoehoe 0:83b5c6efd8d7 113 }
mbed_tw_hoehoe 0:83b5c6efd8d7 114
hmiot 6:71e7a446ae6a 115
hmiot 6:71e7a446ae6a 116 /**
hmiot 6:71e7a446ae6a 117 * Callback triggered when the ble initialization process has finished
hmiot 6:71e7a446ae6a 118 */
hmiot 6:71e7a446ae6a 119 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
hmiot 6:71e7a446ae6a 120 {
hmiot 6:71e7a446ae6a 121 BLE& ble = params->ble;
hmiot 6:71e7a446ae6a 122
hmiot 6:71e7a446ae6a 123 pc.printf("Ble Init\n\r");
hmiot 6:71e7a446ae6a 124 /* Ensure that it is the default instance of BLE */
hmiot 6:71e7a446ae6a 125 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
hmiot 6:71e7a446ae6a 126 return;
hmiot 6:71e7a446ae6a 127 }
hmiot 6:71e7a446ae6a 128
hmiot 6:71e7a446ae6a 129 ble.gap().onConnection(connectionCallback);
hmiot 6:71e7a446ae6a 130 ble.gap().onDisconnection(disconnectionCallback);
mbed_tw_hoehoe 3:d6f80e11a7f4 131
hmiot 6:71e7a446ae6a 132 ble.gap().setScanParams(1000, 800);
hmiot 6:71e7a446ae6a 133 ble.gap().startScan(advertisementCallback);
mbed_tw_hoehoe 2:4b53d13d9851 134 }
mbed_tw_hoehoe 2:4b53d13d9851 135
mbed_tw_hoehoe 0:83b5c6efd8d7 136 int main(void) {
mbed_tw_hoehoe 1:2f1203d70643 137 pc.baud(9600);
mbed_tw_hoehoe 0:83b5c6efd8d7 138 wait(8.0);
hmiot 6:71e7a446ae6a 139 pc.printf("Start\n\r");
hmiot 6:71e7a446ae6a 140 BLE &ble = BLE::Instance();
hmiot 6:71e7a446ae6a 141
hmiot 6:71e7a446ae6a 142 ble.init(bleInitComplete);
hmiot 6:71e7a446ae6a 143
mbed_tw_hoehoe 0:83b5c6efd8d7 144 while (true) {
hmiot 6:71e7a446ae6a 145 ble.waitForEvent();
mbed_tw_hoehoe 0:83b5c6efd8d7 146 }
hmiot 6:71e7a446ae6a 147 }