asd

Dependencies:   BLE_API mbed nRF51822

Fork of Central_1 by -deleted-

Committer:
inbooki
Date:
Tue Aug 18 10:40:55 2015 +0000
Revision:
1:df5573997c91
Parent:
0:51c847e94a29
Child:
2:1f1ab7f55a45
Declare

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DDUdui 0:51c847e94a29 1 /* mbed Microcontroller Library
DDUdui 0:51c847e94a29 2 * Copyright (c) 2006-2015 ARM Limited
DDUdui 0:51c847e94a29 3 *
DDUdui 0:51c847e94a29 4 * Licensed under the Apache License, Version 2.0 (the "License");
DDUdui 0:51c847e94a29 5 * you may not use this file except in compliance with the License.
DDUdui 0:51c847e94a29 6 * You may obtain a copy of the License at
DDUdui 0:51c847e94a29 7 *
DDUdui 0:51c847e94a29 8 * http://www.apache.org/licenses/LICENSE-2.0
DDUdui 0:51c847e94a29 9 *
DDUdui 0:51c847e94a29 10 * Unless required by applicable law or agreed to in writing, software
DDUdui 0:51c847e94a29 11 * distributed under the License is distributed on an "AS IS" BASIS,
DDUdui 0:51c847e94a29 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
DDUdui 0:51c847e94a29 13 * See the License for the specific language governing permissions and
DDUdui 0:51c847e94a29 14 * limitations under the License.
DDUdui 0:51c847e94a29 15 */
DDUdui 0:51c847e94a29 16
DDUdui 0:51c847e94a29 17 #include "mbed.h"
DDUdui 0:51c847e94a29 18 #include "ble/BLE.h"
DDUdui 0:51c847e94a29 19 #include "ble/DiscoveredCharacteristic.h"
DDUdui 0:51c847e94a29 20 #include "ble/DiscoveredService.h"
DDUdui 0:51c847e94a29 21 BLE ble;
DDUdui 0:51c847e94a29 22 DigitalOut led1(LED1);
DDUdui 0:51c847e94a29 23 Serial pc(USBTX, USBRX);
DDUdui 0:51c847e94a29 24
inbooki 1:df5573997c91 25 /* Declare the array */
DDUdui 0:51c847e94a29 26
inbooki 1:df5573997c91 27 static const unsigned ADDR_LEN = 6;
inbooki 1:df5573997c91 28 static const unsigned NUM_LIST = 3;
inbooki 1:df5573997c91 29 typedef uint8_t Address_t[ADDR_LEN]; /* 48-bit address, LSB format. */
inbooki 1:df5573997c91 30 typedef Address_t address_t; /* @Note: deprecated. Use Address_t instead. */
inbooki 1:df5573997c91 31
inbooki 1:df5573997c91 32 /* Declare the pointer */
inbooki 1:df5573997c91 33 uint8_t pt_addr = 0;
inbooki 1:df5573997c91 34 uint8_t list_addr[NUM_LIST][ADDR_LEN]={0,0,0,0,0,0, 0,0,0,0,0,0, 0,0,0,0,0,0};
inbooki 1:df5573997c91 35 int8_t list_rssi[NUM_LIST];
DDUdui 0:51c847e94a29 36 void periodicCallback(void)
DDUdui 0:51c847e94a29 37 {
DDUdui 0:51c847e94a29 38 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
DDUdui 0:51c847e94a29 39 }
DDUdui 0:51c847e94a29 40
DDUdui 0:51c847e94a29 41 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
DDUdui 0:51c847e94a29 42
DDUdui 0:51c847e94a29 43 pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
DDUdui 0:51c847e94a29 44 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
DDUdui 0:51c847e94a29 45 params->rssi, params->isScanResponse, params->type);
inbooki 1:df5573997c91 46
inbooki 1:df5573997c91 47 for(unsigned i = 0; i < NUM_LIST; i++) {
inbooki 1:df5573997c91 48 if(list_addr[i][5] == params->peerAddr[5] && list_addr[i][4] == params->peerAddr[4] && list_addr[i][3] == params->peerAddr[3] && list_addr[i][2] == params->peerAddr[2] && list_addr[i][1] == params->peerAddr[1] && list_addr[i][0] == params->peerAddr[0]) {
inbooki 1:df5573997c91 49 /* Only RSSI Value update */
inbooki 1:df5573997c91 50 list_rssi[i] = params->rssi;
inbooki 1:df5573997c91 51 break;
inbooki 1:df5573997c91 52 }
inbooki 1:df5573997c91 53 if(list_addr[i][5] == 0 && list_addr[i][4] == 0 && list_addr[i][3] == 0 && list_addr[i][2] == 0 && list_addr[i][1] == 0 && list_addr[i][0] == 0) {
inbooki 1:df5573997c91 54 /* Save the address */
inbooki 1:df5573997c91 55 list_addr[i][5] = params->peerAddr[5];
inbooki 1:df5573997c91 56 list_addr[i][4] = params->peerAddr[4];
inbooki 1:df5573997c91 57 list_addr[i][3] = params->peerAddr[3];
inbooki 1:df5573997c91 58 list_addr[i][2] = params->peerAddr[2];
inbooki 1:df5573997c91 59 list_addr[i][1] = params->peerAddr[1];
inbooki 1:df5573997c91 60 list_addr[i][0] = params->peerAddr[0];
inbooki 1:df5573997c91 61 /* Save RSSI value */
inbooki 1:df5573997c91 62 list_rssi[i] = params->rssi;
inbooki 1:df5573997c91 63 if ( i==2 ) {
inbooki 1:df5573997c91 64 /* Find Maximum RSSI value */
inbooki 1:df5573997c91 65 for(unsigned j=0;j<NUM_LIST;j++) {
inbooki 1:df5573997c91 66 for(unsigned k=j ; k<NUM_LIST ; k++) {
inbooki 1:df5573997c91 67 if(list_rssi[j] > list_rssi[j+1]) {
inbooki 1:df5573997c91 68 /* Swap RSSI Value */
inbooki 1:df5573997c91 69 temp = list_rssi[j];
inbooki 1:df5573997c91 70 list_rssi[j] = list_rssi[j+1];
inbooki 1:df5573997c91 71 list_rssi[j+1] = temp;
inbooki 1:df5573997c91 72
inbooki 1:df5573997c91 73 /* Swap the address */
inbooki 1:df5573997c91 74 for(unsigned l = 0; l < 6 ; l++) {
inbooki 1:df5573997c91 75 temp = list_addr[j][l];
inbooki 1:df5573997c91 76 list_addr[j][l] = list_addr[j+1][l];
inbooki 1:df5573997c91 77 list_addr[j+1][l] = temp;
inbooki 1:df5573997c91 78 }
inbooki 1:df5573997c91 79 }
inbooki 1:df5573997c91 80 }
inbooki 1:df5573997c91 81 }
inbooki 1:df5573997c91 82 break;
inbooki 1:df5573997c91 83 }
inbooki 1:df5573997c91 84
inbooki 1:df5573997c91 85 }
DDUdui 0:51c847e94a29 86
inbooki 1:df5573997c91 87
inbooki 1:df5573997c91 88
inbooki 1:df5573997c91 89
inbooki 1:df5573997c91 90 pc.printf("%s", params->advertisingData);
DDUdui 0:51c847e94a29 91
DDUdui 0:51c847e94a29 92 pc.printf("\r\n");
DDUdui 0:51c847e94a29 93
DDUdui 0:51c847e94a29 94 }
DDUdui 0:51c847e94a29 95
DDUdui 0:51c847e94a29 96 void serviceDiscoveryCallback(const DiscoveredService *service) {
DDUdui 0:51c847e94a29 97 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
DDUdui 0:51c847e94a29 98 pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
DDUdui 0:51c847e94a29 99 } else {
DDUdui 0:51c847e94a29 100 printf("S UUID-");
DDUdui 0:51c847e94a29 101 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
DDUdui 0:51c847e94a29 102 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
DDUdui 0:51c847e94a29 103 printf("%02x", longUUIDBytes[i]);
DDUdui 0:51c847e94a29 104 }
DDUdui 0:51c847e94a29 105 pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
DDUdui 0:51c847e94a29 106 }
DDUdui 0:51c847e94a29 107 }
DDUdui 0:51c847e94a29 108
DDUdui 0:51c847e94a29 109 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
DDUdui 0:51c847e94a29 110
DDUdui 0:51c847e94a29 111 }
DDUdui 0:51c847e94a29 112
DDUdui 0:51c847e94a29 113 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
DDUdui 0:51c847e94a29 114 pc.printf("terminated SD for handle %u\r\n", connectionHandle);
DDUdui 0:51c847e94a29 115 }
DDUdui 0:51c847e94a29 116
DDUdui 0:51c847e94a29 117 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
DDUdui 0:51c847e94a29 118 pc.printf("Connected!!\r\n");
DDUdui 0:51c847e94a29 119 ble.stopAdvertising();
DDUdui 0:51c847e94a29 120 wait(1);
DDUdui 0:51c847e94a29 121 if (params->role == Gap::CENTRAL) {
DDUdui 0:51c847e94a29 122 ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
DDUdui 0:51c847e94a29 123 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
DDUdui 0:51c847e94a29 124 }
DDUdui 0:51c847e94a29 125 ble.gap().disconnect(0,Gap::REMOTE_USER_TERMINATED_CONNECTION);
DDUdui 0:51c847e94a29 126 }
DDUdui 0:51c847e94a29 127 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
DDUdui 0:51c847e94a29 128 {
DDUdui 0:51c847e94a29 129 pc.printf("Disconnected\r\n");
DDUdui 0:51c847e94a29 130 pc.printf("Rescan\r\n");
DDUdui 0:51c847e94a29 131 ble.startScan(advertisementCallback);
DDUdui 0:51c847e94a29 132 ble.startAdvertising();
DDUdui 0:51c847e94a29 133 }
DDUdui 0:51c847e94a29 134
DDUdui 0:51c847e94a29 135
DDUdui 0:51c847e94a29 136 int main(void)
DDUdui 0:51c847e94a29 137 {
DDUdui 0:51c847e94a29 138 led1 = 1;
DDUdui 0:51c847e94a29 139 Ticker ticker;
DDUdui 0:51c847e94a29 140 ticker.attach(periodicCallback, 1);
DDUdui 0:51c847e94a29 141 ble.onConnection(connectionCallback);
DDUdui 0:51c847e94a29 142 ble.onDisconnection(disconnectionCallback);
DDUdui 0:51c847e94a29 143
DDUdui 0:51c847e94a29 144 ble.init();
DDUdui 0:51c847e94a29 145
DDUdui 0:51c847e94a29 146 pc.baud(9600);
DDUdui 0:51c847e94a29 147 pc.printf("Observer Init \r\n");
DDUdui 0:51c847e94a29 148
DDUdui 0:51c847e94a29 149
DDUdui 0:51c847e94a29 150 /* */
inbooki 1:df5573997c91 151 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
DDUdui 0:51c847e94a29 152 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
inbooki 1:df5573997c91 153 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
DDUdui 0:51c847e94a29 154 (const uint8_t *)"DDUDDU", sizeof("DDUDDU") - 1);
DDUdui 0:51c847e94a29 155
inbooki 1:df5573997c91 156 /* We don't need any advertising because of its role */
inbooki 1:df5573997c91 157 //ble.gap().setAdvertisingInterval(100); /* 1second. */
inbooki 1:df5573997c91 158 //ble.gap().startAdvertising();
DDUdui 0:51c847e94a29 159
DDUdui 0:51c847e94a29 160 ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */);
DDUdui 0:51c847e94a29 161 ble.gap().startScan(advertisementCallback);
DDUdui 0:51c847e94a29 162
DDUdui 0:51c847e94a29 163 while (true) {
DDUdui 0:51c847e94a29 164 ble.waitForEvent();
DDUdui 0:51c847e94a29 165 }
DDUdui 0:51c847e94a29 166 }