asd
Dependencies: BLE_API mbed nRF51822
Fork of Central_1 by
main.cpp@2:1f1ab7f55a45, 2015-08-18 (annotated)
- Committer:
- inbooki
- Date:
- Tue Aug 18 12:37:28 2015 +0000
- Revision:
- 2:1f1ab7f55a45
- Parent:
- 1:df5573997c91
new
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:1f1ab7f55a45 | 27 | static const unsigned NUM_LIST = 3; |
inbooki | 1:df5573997c91 | 28 | static const unsigned ADDR_LEN = 6; |
inbooki | 1:df5573997c91 | 29 | |
inbooki | 1:df5573997c91 | 30 | /* Declare the pointer */ |
inbooki | 2:1f1ab7f55a45 | 31 | |
inbooki | 2:1f1ab7f55a45 | 32 | int8_t temp; |
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]; |
inbooki | 2:1f1ab7f55a45 | 36 | |
DDUdui | 0:51c847e94a29 | 37 | void periodicCallback(void) |
DDUdui | 0:51c847e94a29 | 38 | { |
DDUdui | 0:51c847e94a29 | 39 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
DDUdui | 0:51c847e94a29 | 40 | } |
DDUdui | 0:51c847e94a29 | 41 | |
DDUdui | 0:51c847e94a29 | 42 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
inbooki | 2:1f1ab7f55a45 | 43 | unsigned i; |
inbooki | 2:1f1ab7f55a45 | 44 | |
inbooki | 2:1f1ab7f55a45 | 45 | for(i = 0; i < NUM_LIST; i++) |
inbooki | 2:1f1ab7f55a45 | 46 | { |
inbooki | 2:1f1ab7f55a45 | 47 | 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 | 2:1f1ab7f55a45 | 48 | { |
inbooki | 2:1f1ab7f55a45 | 49 | /* Only RSSI Value update */ |
inbooki | 2:1f1ab7f55a45 | 50 | list_rssi[i] = params->rssi; |
inbooki | 2:1f1ab7f55a45 | 51 | //pc.printf("%dth RSSI Update!\r\n",i+1); |
inbooki | 2:1f1ab7f55a45 | 52 | break; |
inbooki | 2:1f1ab7f55a45 | 53 | } |
inbooki | 2:1f1ab7f55a45 | 54 | 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 | 2:1f1ab7f55a45 | 55 | { |
inbooki | 2:1f1ab7f55a45 | 56 | pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n", |
DDUdui | 0:51c847e94a29 | 57 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
DDUdui | 0:51c847e94a29 | 58 | params->rssi, params->isScanResponse, params->type); |
inbooki | 1:df5573997c91 | 59 | /* Save the address */ |
inbooki | 1:df5573997c91 | 60 | list_addr[i][5] = params->peerAddr[5]; |
inbooki | 1:df5573997c91 | 61 | list_addr[i][4] = params->peerAddr[4]; |
inbooki | 1:df5573997c91 | 62 | list_addr[i][3] = params->peerAddr[3]; |
inbooki | 1:df5573997c91 | 63 | list_addr[i][2] = params->peerAddr[2]; |
inbooki | 1:df5573997c91 | 64 | list_addr[i][1] = params->peerAddr[1]; |
inbooki | 1:df5573997c91 | 65 | list_addr[i][0] = params->peerAddr[0]; |
inbooki | 1:df5573997c91 | 66 | /* Save RSSI value */ |
inbooki | 1:df5573997c91 | 67 | list_rssi[i] = params->rssi; |
inbooki | 2:1f1ab7f55a45 | 68 | pc.printf("Address & RSSI Save!\r\n"); |
inbooki | 2:1f1ab7f55a45 | 69 | if ( i==NUM_LIST-1 ) |
inbooki | 2:1f1ab7f55a45 | 70 | { |
inbooki | 2:1f1ab7f55a45 | 71 | pc.printf("Find Maximum value!\r\n"); |
inbooki | 1:df5573997c91 | 72 | /* Find Maximum RSSI value */ |
inbooki | 2:1f1ab7f55a45 | 73 | for(unsigned j=0;j<NUM_LIST;j++) |
inbooki | 2:1f1ab7f55a45 | 74 | { |
inbooki | 2:1f1ab7f55a45 | 75 | for(unsigned k=j ; k<NUM_LIST ; k++) |
inbooki | 2:1f1ab7f55a45 | 76 | { |
inbooki | 2:1f1ab7f55a45 | 77 | if(list_rssi[j] < list_rssi[k]) |
inbooki | 2:1f1ab7f55a45 | 78 | { |
inbooki | 1:df5573997c91 | 79 | /* Swap RSSI Value */ |
inbooki | 1:df5573997c91 | 80 | temp = list_rssi[j]; |
inbooki | 2:1f1ab7f55a45 | 81 | list_rssi[j] = list_rssi[k]; |
inbooki | 2:1f1ab7f55a45 | 82 | list_rssi[k] = temp; |
inbooki | 1:df5573997c91 | 83 | |
inbooki | 1:df5573997c91 | 84 | /* Swap the address */ |
inbooki | 2:1f1ab7f55a45 | 85 | for(unsigned l = 0; l < 6 ; l++) |
inbooki | 2:1f1ab7f55a45 | 86 | { |
inbooki | 2:1f1ab7f55a45 | 87 | temp = list_addr[j][l]; |
inbooki | 2:1f1ab7f55a45 | 88 | list_addr[j][l] = list_addr[k][l]; |
inbooki | 2:1f1ab7f55a45 | 89 | list_addr[k][l] = temp; |
inbooki | 1:df5573997c91 | 90 | } |
inbooki | 1:df5573997c91 | 91 | } |
inbooki | 1:df5573997c91 | 92 | } |
inbooki | 2:1f1ab7f55a45 | 93 | } |
inbooki | 2:1f1ab7f55a45 | 94 | for(unsigned j=0; j<NUM_LIST ; j++) |
inbooki | 2:1f1ab7f55a45 | 95 | pc.printf("Address %d : %02x %02x %02x %02x %02x %02x RSSI : %d\r\n", j, list_addr[j][5], list_addr[j][4], list_addr[j][3], list_addr[j][2], list_addr[j][1], list_addr[j][0], list_rssi[j]); |
inbooki | 2:1f1ab7f55a45 | 96 | } |
inbooki | 1:df5573997c91 | 97 | break; |
inbooki | 1:df5573997c91 | 98 | } |
inbooki | 1:df5573997c91 | 99 | } |
inbooki | 2:1f1ab7f55a45 | 100 | if( i == NUM_LIST-1) |
inbooki | 2:1f1ab7f55a45 | 101 | { |
inbooki | 2:1f1ab7f55a45 | 102 | ble.gap().stopScan(); |
inbooki | 2:1f1ab7f55a45 | 103 | ble.gap().connect(list_addr[0], Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); |
inbooki | 2:1f1ab7f55a45 | 104 | } |
DDUdui | 0:51c847e94a29 | 105 | } |
DDUdui | 0:51c847e94a29 | 106 | |
DDUdui | 0:51c847e94a29 | 107 | void serviceDiscoveryCallback(const DiscoveredService *service) { |
DDUdui | 0:51c847e94a29 | 108 | if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) { |
DDUdui | 0:51c847e94a29 | 109 | pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle()); |
DDUdui | 0:51c847e94a29 | 110 | } else { |
DDUdui | 0:51c847e94a29 | 111 | printf("S UUID-"); |
DDUdui | 0:51c847e94a29 | 112 | const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID(); |
DDUdui | 0:51c847e94a29 | 113 | for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) { |
DDUdui | 0:51c847e94a29 | 114 | printf("%02x", longUUIDBytes[i]); |
DDUdui | 0:51c847e94a29 | 115 | } |
DDUdui | 0:51c847e94a29 | 116 | pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle()); |
DDUdui | 0:51c847e94a29 | 117 | } |
DDUdui | 0:51c847e94a29 | 118 | } |
DDUdui | 0:51c847e94a29 | 119 | |
DDUdui | 0:51c847e94a29 | 120 | void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) { |
DDUdui | 0:51c847e94a29 | 121 | |
DDUdui | 0:51c847e94a29 | 122 | } |
DDUdui | 0:51c847e94a29 | 123 | |
DDUdui | 0:51c847e94a29 | 124 | void discoveryTerminationCallback(Gap::Handle_t connectionHandle) { |
DDUdui | 0:51c847e94a29 | 125 | pc.printf("terminated SD for handle %u\r\n", connectionHandle); |
DDUdui | 0:51c847e94a29 | 126 | } |
DDUdui | 0:51c847e94a29 | 127 | |
DDUdui | 0:51c847e94a29 | 128 | void connectionCallback(const Gap::ConnectionCallbackParams_t *params) { |
inbooki | 2:1f1ab7f55a45 | 129 | pc.printf("Connection success to %02x %02x %02x %02x %02x %02x\r\n", list_addr[0][5], list_addr[0][4], list_addr[0][3], list_addr[0][2], list_addr[0][1], list_addr[0][0]); |
DDUdui | 0:51c847e94a29 | 130 | ble.stopAdvertising(); |
DDUdui | 0:51c847e94a29 | 131 | wait(1); |
DDUdui | 0:51c847e94a29 | 132 | if (params->role == Gap::CENTRAL) { |
DDUdui | 0:51c847e94a29 | 133 | ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback); |
DDUdui | 0:51c847e94a29 | 134 | ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001); |
DDUdui | 0:51c847e94a29 | 135 | } |
DDUdui | 0:51c847e94a29 | 136 | ble.gap().disconnect(0,Gap::REMOTE_USER_TERMINATED_CONNECTION); |
DDUdui | 0:51c847e94a29 | 137 | } |
DDUdui | 0:51c847e94a29 | 138 | void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) |
DDUdui | 0:51c847e94a29 | 139 | { |
inbooki | 2:1f1ab7f55a45 | 140 | /* Initialize address list */ |
inbooki | 2:1f1ab7f55a45 | 141 | for(unsigned i=0; i<NUM_LIST ; i++) { |
inbooki | 2:1f1ab7f55a45 | 142 | list_rssi[i] = 0; |
inbooki | 2:1f1ab7f55a45 | 143 | for(unsigned j=0; j<ADDR_LEN ; j++) |
inbooki | 2:1f1ab7f55a45 | 144 | list_addr[i][j] = 0; |
inbooki | 2:1f1ab7f55a45 | 145 | } |
DDUdui | 0:51c847e94a29 | 146 | pc.printf("Disconnected\r\n"); |
DDUdui | 0:51c847e94a29 | 147 | pc.printf("Rescan\r\n"); |
DDUdui | 0:51c847e94a29 | 148 | ble.startScan(advertisementCallback); |
DDUdui | 0:51c847e94a29 | 149 | } |
DDUdui | 0:51c847e94a29 | 150 | |
DDUdui | 0:51c847e94a29 | 151 | |
DDUdui | 0:51c847e94a29 | 152 | int main(void) |
DDUdui | 0:51c847e94a29 | 153 | { |
DDUdui | 0:51c847e94a29 | 154 | led1 = 1; |
DDUdui | 0:51c847e94a29 | 155 | Ticker ticker; |
DDUdui | 0:51c847e94a29 | 156 | ticker.attach(periodicCallback, 1); |
DDUdui | 0:51c847e94a29 | 157 | ble.onConnection(connectionCallback); |
DDUdui | 0:51c847e94a29 | 158 | ble.onDisconnection(disconnectionCallback); |
DDUdui | 0:51c847e94a29 | 159 | |
DDUdui | 0:51c847e94a29 | 160 | ble.init(); |
DDUdui | 0:51c847e94a29 | 161 | |
DDUdui | 0:51c847e94a29 | 162 | pc.baud(9600); |
DDUdui | 0:51c847e94a29 | 163 | pc.printf("Observer Init \r\n"); |
DDUdui | 0:51c847e94a29 | 164 | |
DDUdui | 0:51c847e94a29 | 165 | |
DDUdui | 0:51c847e94a29 | 166 | /* */ |
inbooki | 1:df5573997c91 | 167 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED); |
DDUdui | 0:51c847e94a29 | 168 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
inbooki | 1:df5573997c91 | 169 | ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, |
DDUdui | 0:51c847e94a29 | 170 | (const uint8_t *)"DDUDDU", sizeof("DDUDDU") - 1); |
DDUdui | 0:51c847e94a29 | 171 | |
inbooki | 1:df5573997c91 | 172 | /* We don't need any advertising because of its role */ |
inbooki | 1:df5573997c91 | 173 | //ble.gap().setAdvertisingInterval(100); /* 1second. */ |
inbooki | 1:df5573997c91 | 174 | //ble.gap().startAdvertising(); |
DDUdui | 0:51c847e94a29 | 175 | |
inbooki | 2:1f1ab7f55a45 | 176 | ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */); |
DDUdui | 0:51c847e94a29 | 177 | ble.gap().startScan(advertisementCallback); |
DDUdui | 0:51c847e94a29 | 178 | |
DDUdui | 0:51c847e94a29 | 179 | while (true) { |
DDUdui | 0:51c847e94a29 | 180 | ble.waitForEvent(); |
DDUdui | 0:51c847e94a29 | 181 | } |
DDUdui | 0:51c847e94a29 | 182 | } |