/ Mbed 2 deprecated Central_1_new

Dependencies:   BLE_API mbed nRF51822

Fork of Central_1 by Changmo Yang

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2006-2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "ble/BLE.h"
00019 #include "ble/DiscoveredCharacteristic.h"
00020 #include "ble/DiscoveredService.h"
00021 BLE        ble;
00022 DigitalOut led1(LED1);
00023 Serial pc(USBTX, USBRX);
00024 
00025 /* Declare the array */
00026 
00027 static const unsigned NUM_LIST = 3;
00028 static const unsigned ADDR_LEN = 6;
00029 
00030 /* Declare the pointer */
00031 
00032 int8_t temp;
00033 uint8_t pt_addr = 0;
00034 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};
00035 int8_t list_rssi[NUM_LIST];
00036 
00037 void periodicCallback(void)
00038 {
00039     led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
00040 }
00041 
00042 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
00043     unsigned i;
00044            
00045     for(i = 0; i < NUM_LIST; i++) 
00046     {
00047         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]) 
00048         {          
00049             /* Only RSSI Value update */
00050             list_rssi[i] = params->rssi;
00051             //pc.printf("%dth RSSI Update!\r\n",i+1);
00052             break;
00053         }
00054         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) 
00055         {
00056             pc.printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
00057            params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
00058            params->rssi, params->isScanResponse, params->type);
00059             /* Save the address */
00060             list_addr[i][5] = params->peerAddr[5];
00061             list_addr[i][4] = params->peerAddr[4];
00062             list_addr[i][3] = params->peerAddr[3];
00063             list_addr[i][2] = params->peerAddr[2];
00064             list_addr[i][1] = params->peerAddr[1];
00065             list_addr[i][0] = params->peerAddr[0];
00066             /* Save RSSI value */
00067             list_rssi[i] = params->rssi;
00068             pc.printf("Address & RSSI Save!\r\n");
00069             if ( i==NUM_LIST-1 ) 
00070             {
00071                 pc.printf("Find Maximum value!\r\n");
00072                 /* Find Maximum RSSI value */
00073                 for(unsigned j=0;j<NUM_LIST;j++) 
00074                 {
00075                     for(unsigned k=j ; k<NUM_LIST ; k++) 
00076                     {
00077                         if(list_rssi[j] < list_rssi[k]) 
00078                         {
00079                             /* Swap RSSI Value */
00080                             temp = list_rssi[j];
00081                             list_rssi[j] = list_rssi[k];
00082                             list_rssi[k] = temp;
00083                             
00084                             /* Swap the address */
00085                             for(unsigned l = 0; l < 6 ; l++) 
00086                             {
00087                                 temp = list_addr[j][l];
00088                                 list_addr[j][l] = list_addr[k][l];
00089                                 list_addr[k][l] = temp;
00090                             }
00091                         }
00092                     }
00093                 }
00094                 for(unsigned j=0; j<NUM_LIST ; j++)
00095                     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]);                        
00096             }
00097             break;
00098         }
00099     }
00100     if( i == NUM_LIST-1)
00101     {
00102         ble.gap().stopScan();
00103         ble.gap().connect(list_addr[0], Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
00104     }
00105 }
00106 
00107 void serviceDiscoveryCallback(const DiscoveredService *service) {
00108     if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
00109         pc.printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
00110     } else {
00111         printf("S UUID-");
00112         const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
00113         for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
00114             printf("%02x", longUUIDBytes[i]);
00115         }
00116         pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
00117     }
00118 }
00119  
00120 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
00121  
00122 }
00123  
00124 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
00125     pc.printf("terminated SD for handle %u\r\n", connectionHandle);
00126 }
00127  
00128 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
00129     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]);
00130     ble.stopAdvertising();
00131     wait(1);
00132     if (params->role == Gap::CENTRAL) {
00133         ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
00134         ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback, 0xa000, 0xa001);
00135     }
00136     ble.gap().disconnect(0,Gap::REMOTE_USER_TERMINATED_CONNECTION);
00137 }
00138 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
00139 {
00140     /* Initialize address list */
00141     for(unsigned i=0; i<NUM_LIST ; i++) {
00142         list_rssi[i] = 0;
00143         for(unsigned j=0; j<ADDR_LEN ; j++)
00144             list_addr[i][j] = 0;
00145     }
00146     pc.printf("Disconnected\r\n");
00147     pc.printf("Rescan\r\n");
00148     ble.startScan(advertisementCallback);
00149 }
00150     
00151 
00152 int main(void)
00153 {
00154     led1 = 1;
00155     Ticker ticker;
00156     ticker.attach(periodicCallback, 1);
00157     ble.onConnection(connectionCallback);
00158     ble.onDisconnection(disconnectionCallback);
00159 
00160     ble.init();
00161     
00162     pc.baud(9600);
00163     pc.printf("Observer Init \r\n");
00164 
00165 
00166     /* */
00167     ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00168     ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00169     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00170                                     (const uint8_t *)"DDUDDU", sizeof("DDUDDU") - 1);
00171    
00172     /* We don't need any advertising because of its role */
00173     //ble.gap().setAdvertisingInterval(100); /* 1second. */
00174     //ble.gap().startAdvertising();
00175 
00176     ble.gap().setScanParams(500 /* scan interval */, 500 /* scan window */);
00177     ble.gap().startScan(advertisementCallback);
00178     
00179     while (true) {
00180         ble.waitForEvent();
00181     }
00182 }