AdventCalendar2015

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_ButtonSense by Bluetooth Low Energy

main.cpp

Committer:
yueee_yt
Date:
2015-12-19
Revision:
1:f66e5ed21c90
Parent:
0:2dec89c76f68

File content as of revision 1:f66e5ed21c90:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2015 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "ble/BLE.h"
#include "ble/DiscoveredCharacteristic.h"
#include "ble/DiscoveredService.h"

#include "ble_gatt.h"

BLE ble;

int mode=0;

const uint8_t WriteCharacteristicAddr[]=  {0x71, 0x3D, 0, 3, 0x50, 0x3E, 0x4C, 0x75, 0xBA, 0x94, 0x31, 0x48, 0xF1, 0x8D, 0x94, 0x1E};

uint8_t peripheral1_ADDRESS[]= {0x3b ,0x72 ,0x34 ,0x88 ,0xe2 ,0xd4};
uint8_t peripheral2_ADDRESS[]= {0x5c ,0xde ,0xa7 ,0xdd ,0xec ,0xe1};
uint8_t peripheral3_ADDRESS[]= {0xf5 ,0x5a ,0x21 ,0xa5 ,0xc0 ,0xde};
uint8_t peripheral4_ADDRESS[]= {0xe0 ,0x15 ,0xdc ,0x65 ,0x1b ,0xd0};
uint8_t peripheral5_ADDRESS[]= {0x01 ,0xc6 ,0x18 ,0x2f ,0x66 ,0xdd};

Gap::Handle_t peripheral1_connectionHandle = 0xFFFF;
Gap::Handle_t peripheral2_connectionHandle = 0xFFFF;
Gap::Handle_t peripheral3_connectionHandle = 0xFFFF;
Gap::Handle_t peripheral4_connectionHandle = 0xFFFF;
Gap::Handle_t peripheral5_connectionHandle = 0xFFFF;

DiscoveredCharacteristic peripheral1_WriteCharacteristic;
DiscoveredCharacteristic peripheral2_WriteCharacteristic;
DiscoveredCharacteristic peripheral3_WriteCharacteristic;
DiscoveredCharacteristic peripheral4_WriteCharacteristic;
DiscoveredCharacteristic peripheral5_WriteCharacteristic;

DigitalOut alivenessLED(LED1, 1);

void periodicCallback(void) {
    alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
    int er;
    if(mode==0){
       er= ble.gap().connect(peripheral1_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection
        mode=1;printf("mode1 \r\n");
      if(er!=0)printf("connection error:%x \r\n",er);
        }
    if(mode==4){
      er=  ble.gap().connect(peripheral2_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection
        mode=11;printf("mode11 \r\n");
      if(er!=0)printf("connection error:%x \r\n",er);
        }
    if(mode==14){
       er= ble.gap().connect(peripheral3_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection
        mode=21;printf("mode21 \r\n");
      if(er!=0)printf("connection error:%x \r\n",er);
        }
    if(mode==24){
       er= ble.gap().connect(peripheral4_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection
        mode=31;printf("mode31 \r\n");
      if(er!=0)printf("connection error:%x \r\n",er);
        }
    if(mode==34){
       er= ble.gap().connect(peripheral5_ADDRESS, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL); //Connection
        mode=41;printf("mode41 \r\n");
      if(er!=0)printf("connection error:%x \r\n",er);
        }
}

void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
    //if (params->peerAddr[0] != 0x29) { /* !ALERT! Update this filter to suit your device. */
    //    return;
    //}
    printf("adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
           params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
           params->rssi, params->isScanResponse, params->type);

    //ble.gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
}

void peripheral1_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP)
{
    bool flag;
    if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) {
        const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID();
        flag=true;
        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++)
            if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false;
        if(flag==true) {
            printf("#peripheral1_Write characteristic found\r\n");
            peripheral1_WriteCharacteristic      = *characteristicP;
            mode=3;printf("mode3 \r\n");
       }
    }
}

void peripheral2_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP)
{
    bool flag;
    if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) {
        const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID();
        flag=true;
        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++)
            if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false;
        if(flag==true) {
            printf("#peripheral2_Write characteristic found\r\n");
            peripheral2_WriteCharacteristic      = *characteristicP;
            mode=13;printf("mode13 \r\n");
       }
    }
}

void peripheral3_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP)
{
    bool flag;
    if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) {
        const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID();
        flag=true;
        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++)
            if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false;
        if(flag==true) {
            printf("#peripheral3_Write characteristic found\r\n");
            peripheral3_WriteCharacteristic      = *characteristicP;
            mode=23;printf("mode23 \r\n");
       }
    }
}

void peripheral4_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP)
{
    bool flag;
    if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) {
        const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID();
        flag=true;
        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++)
            if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false;
        if(flag==true) {
            printf("#peripheral4_Write characteristic found\r\n");
            peripheral4_WriteCharacteristic      = *characteristicP;
            mode=33;printf("mode33 \r\n");
       }
    }
}

void peripheral5_characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP)
{
    bool flag;
    if (characteristicP->getUUID().shortOrLong() == UUID::UUID_TYPE_LONG) {
        const uint8_t *longUUIDBytes = characteristicP->getUUID().getBaseUUID();
        flag=true;
        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++)
            if( longUUIDBytes[UUID::LENGTH_OF_LONG_UUID-i-1]!=WriteCharacteristicAddr[i])flag=false;
        if(flag==true) {
            printf("#peripheral5_Write characteristic found\r\n");
            peripheral5_WriteCharacteristic      = *characteristicP;
            mode=43;printf("mode43 \r\n");
       }
    }
}

void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
    if(peripheral1_connectionHandle==connectionHandle) {
        printf("peripheral1_terminated SD\r\n");
        mode=4;printf("mode4 \r\n");
    }
    if(peripheral2_connectionHandle==connectionHandle) {
        printf("peripheral2_terminated SD\r\n");
        mode=14;printf("mode14 \r\n");
    }
    if(peripheral3_connectionHandle==connectionHandle) {
        printf("peripheral3_terminated SD\r\n");
        mode=24;printf("mode24 \r\n");
    }
    if(peripheral4_connectionHandle==connectionHandle) {
        printf("peripheral4_terminated SD\r\n");
        mode=34;printf("mode34 \r\n");
    }
    if(peripheral5_connectionHandle==connectionHandle) {
        printf("peripheral5_terminated SD\r\n");
        mode=44;printf("mode44 \r\n");
    }
}

void peripheral1_serviceDiscoveryCallback(const DiscoveredService *service)
{
}
void peripheral2_serviceDiscoveryCallback(const DiscoveredService *service)
{
}
void peripheral3_serviceDiscoveryCallback(const DiscoveredService *service)
{
}
void peripheral4_serviceDiscoveryCallback(const DiscoveredService *service)
{
}
void peripheral5_serviceDiscoveryCallback(const DiscoveredService *service)
{
}

void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
    bool f;
    int i;
    printf("connection callback\r\n");
    if (params->role == Gap::CENTRAL) {

        f=true;
        for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral1_ADDRESS[i])f=false;
        if(f==true) {
            peripheral1_connectionHandle=params->handle;
            ble.gattClient().launchServiceDiscovery(params->handle, peripheral1_serviceDiscoveryCallback, peripheral1_characteristicDiscoveryCallback);
            ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
            mode=2;printf("mode2 \r\n");
        }

        f=true;
        for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral2_ADDRESS[i])f=false;
        if(f==true) {
            peripheral2_connectionHandle=params->handle;
            ble.gattClient().launchServiceDiscovery(params->handle, peripheral2_serviceDiscoveryCallback, peripheral2_characteristicDiscoveryCallback);
            ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
            mode=12;printf("mode12 \r\n");
        }

        f=true;
        for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral3_ADDRESS[i])f=false;
        if(f==true) {
            peripheral3_connectionHandle=params->handle;
            ble.gattClient().launchServiceDiscovery(params->handle, peripheral3_serviceDiscoveryCallback, peripheral3_characteristicDiscoveryCallback);
            ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
            mode=22;printf("mode22 \r\n");
        }

        f=true;
        for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral4_ADDRESS[i])f=false;
        if(f==true) {
            peripheral4_connectionHandle=params->handle;
            ble.gattClient().launchServiceDiscovery(params->handle, peripheral4_serviceDiscoveryCallback, peripheral4_characteristicDiscoveryCallback);
            ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
            mode=32;printf("mode32 \r\n");
        }

        f=true;
        for(i=0; i<6; i++)if(params->peerAddr[i]!=peripheral5_ADDRESS[i])f=false;
        if(f==true) {
            peripheral5_connectionHandle=params->handle;
            ble.gattClient().launchServiceDiscovery(params->handle, peripheral5_serviceDiscoveryCallback, peripheral5_characteristicDiscoveryCallback);
            ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
            mode=42;printf("mode42 \r\n");
        }
    }
}

//void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 
void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params){
    printf("disconnect\r\n");
    if(peripheral1_connectionHandle==params->handle) {
        printf("peripheral1:Disconnected\r\n");
    }
    if(peripheral2_connectionHandle==params->handle) {
        printf("peripheral2:Disconnected\r\n");
    }
    if(peripheral3_connectionHandle==params->handle) {
        printf("peripheral3:Disconnected\r\n");
    }
    if(peripheral4_connectionHandle==params->handle) {
        printf("peripheral4:Disconnected\r\n");
    }
    if(peripheral5_connectionHandle==params->handle) {
        printf("peripheral5:Disconnected\r\n");
    }
}

void hvxCallback(const GattHVXCallbackParams *params) {
    printf("received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication");
    for (unsigned index = 0; index < params->len; index++) {
        printf(" %02x", params->data[index]);
    }
    printf("\r\n");
}

int main(void) {
    Ticker ticker;
    ticker.attach(periodicCallback, 1);

    ble.init();
    ble.gap().onConnection(connectionCallback);
    ble.gap().onDisconnection(disconnectionCallback);

   // ble.gap().setScanParams(500, 400);
   // ble.gap().startScan(advertisementCallback);

   // ble.gattClient().onHVX(hvxCallback);

    while (true) {
        ble.waitForEvent();
    }
}