Yasushi TAUCHI / Mbed 2 deprecated BLE_HVC_EGAO

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HVC by Yasushi TAUCHI

main.cpp

Committer:
yueee_yt
Date:
2015-07-19
Revision:
1:e4ee755844de
Parent:
0:2dec89c76f68
Child:
2:b94ca9b44b80

File content as of revision 1:e4ee755844de:

/* 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"

char HVC_ADDRESS[]= {0xd5,0x32,0x26,0x3b,0x47,0x3d};
BLE ble;
Serial pc(USBTX, USBRX);

Gap::Handle_t connectionHandle = 0xFFFF;

DigitalOut alivenessLED(LED1, 1);
bool foundHVCWriteCharacteristic = false;
bool foundHVCNotifyCharacteristic = false;
bool foundHVCDeviceCharacteristic = false;
bool WaitSend;
DiscoveredCharacteristic HVCWriteCharacteristic;
DiscoveredCharacteristic HVCNotifyCharacteristic;
DiscoveredCharacteristic HVCDeviceCharacteristic;

void periodicCallback(void)
{
    alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events */
}

void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
{
    if ((params->peerAddr[5] != HVC_ADDRESS[0]) ||
            (params->peerAddr[4] != HVC_ADDRESS[1]) ||
            (params->peerAddr[3] != HVC_ADDRESS[2]) ||
            (params->peerAddr[2] != HVC_ADDRESS[3]) ||
            (params->peerAddr[1] != HVC_ADDRESS[4]) ||
            (params->peerAddr[0] != HVC_ADDRESS[5])) { /* !ALERT! Update this filter to suit your device. */
        //  printf("Not 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);
        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 serviceDiscoveryCallback(const DiscoveredService *service)
{
    if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
 /*       pc.printf("#S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());*/
    } else {
/*        pc.printf("#S UUID-");*/
        const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
        for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
/*            pc.printf("%02x", longUUIDBytes[i]);*/
        }
/*        pc.printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());*/
    }
}

void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP)
{
/*    pc.printf("#  C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());*/
    if (characteristicP->getShortUUID() == 2) { /* !ALERT! Update this filter to suit your device. */
        HVCWriteCharacteristic      = *characteristicP;
        foundHVCWriteCharacteristic = true;
/*        printf("#HVCWriteCharacteristic Found\r\n");*/
    }
    if (characteristicP->getShortUUID() == 3) { /* !ALERT! Update this filter to suit your device. */
        HVCNotifyCharacteristic      = *characteristicP;
        foundHVCNotifyCharacteristic = true;
/*        printf("#HVCNotifyCharacteristic Found\r\n");*/
    }
    if (characteristicP->getShortUUID() == 4) { /* !ALERT! Update this filter to suit your device. */
        HVCDeviceCharacteristic      = *characteristicP;
        foundHVCDeviceCharacteristic = true;
/*        printf("#HVCDeviceCharacteristic Found\r\n");*/
    }
}

void discoveryTerminationCallback(Gap::Handle_t connectionHandle)
{
/*    pc.printf("# discovery terminated CallBack %u\r\n", connectionHandle);*/
}

void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
{
    if (params->role == Gap::CENTRAL) {
        connectionHandle = params->handle;
        ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
        ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback/*, 0xa000, 0xa001*/);
        ble.gap().stopScan();
/*        pc.printf("#SCAN Stop\r\n");*/
    }
}

void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
/*    pc.printf("#Disconnected\r\n");*/
    // ble.gap().startScan(advertisementCallback);
    // pc.printf("#RE SCAN Start\r\n");
}

void hvxCallback(const GattHVXCallbackParams *params)
{
    static bool flag=false; //flag=true no toki tudukiga aru
    static int l;
    static unsigned char buffer[100];
    int k,human_no,hand_no,face_no;
    int a;
/*    pc.printf("#received HVX callback for handle %u; type %s\r\n", params->handle, (params->type == BLE_HVX_NOTIFICATION) ? "notification" : "indication");*/
    if(flag==false)l=0;
    for (unsigned index = 0; index < params->len; index++) {
/*        pc.printf("# %02x", params->data[index]);*/
        buffer[l]=params->data[l];
        l++;
    }
/*    pc.printf("\r\n");*/
    if((buffer[0]==0xfe)||(buffer[1]==0x00)) {
        k=buffer[2]+buffer[3]*0x100+buffer[4]*0x10000+buffer[5]*0x1000000;
        human_no=buffer[6];
        hand_no=buffer[7];
        face_no=buffer[8];
        if(l>2+4+4+8*human_no+8*hand_no+(8+3)*face_no-2)flag=false;
        else flag=true;
        if(flag==false) {
/*            printf("# humanNo=%d HandNo=%d FaceNo=%d\r\n",human_no,hand_no,face_no);*/
            if(face_no>0) {
                a=buffer[9+human_no*8+hand_no*8+9];
                printf("%d\r\n",a);
            }
        }
    }
    WaitSend=true;
}
void DataReadCallback(const GattReadCallbackParams *params)
{
/*    pc.printf("#recive dataread \r\n");*/
    for (unsigned index = 0; index < params->len; index++) {
/*        pc.printf("# %02x", params->data[index]);*/
    }
/*    pc.printf("\r\n");*/
}


void DataWriteCallback(const GattWriteCallbackParams *params)
{
  //  pc.printf("#send data %x\r\n",params->writeOp);
  //  for (unsigned index = 0; index < params->len; index++) {
  //      pc.printf(" %02x", params->data[index]);
  //  }
  //  pc.printf("\r\n");
    //  ble.gattClient().read(connectionHandle, HVCDeviceCharacteristic.getValueHandle(), 0);
//WaitSend=true;
//HVCNotifyCharacteristic.read(0);
}

int main(void)
{
    pc.baud(9600);
/*    pc.printf("#Start\r\n");*/
    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().onDataRead(DataReadCallback);
    ble.gattClient().onDataWrite(DataWriteCallback);
    ble.gattClient().onHVX(hvxCallback);

    uint8_t send[100]= {0xfe,03,03,0,4,0x1,0};
    int state=-1;
    while (true) {
        if(state<0) {
            if (foundHVCWriteCharacteristic && foundHVCNotifyCharacteristic && foundHVCDeviceCharacteristic && !ble.gattClient().isServiceDiscoveryActive()) {
            //    printf("# State 1 \r\n");
                state=1;
            }
        } else if(state==1&&!ble.gattClient().isServiceDiscoveryActive()) {
          //  printf("# State 2 \r\n");
            state=2;
            ble.gattClient().onHVX(hvxCallback);

            uint16_t value = BLE_HVX_NOTIFICATION;
            WaitSend=true;
            ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ,
                                   connectionHandle,
                                   HVCNotifyCharacteristic.getValueHandle() + 1, /* HACK Alert. We're assuming that CCCD descriptor immediately follows the value attribute. */
                                   sizeof(uint16_t),                          /* HACK Alert! size should be made into a BLE_API constant. */
                                   reinterpret_cast<const uint8_t *>(&value));
            // HVCNotifyCharacteristic.write(   sizeof(uint16_t),     reinterpret_cast<const uint8_t *>(&value));                     /* HACK Alert! size should be made into a BLE_API constant. */
            //
            //HVCNotifyCharacteristic.enableNoticiation() ;
        } else if(state==2&&!ble.gattClient().isServiceDiscoveryActive()&&WaitSend) {

            WaitSend=false;
        //    pc.printf("#loop\r\n");
            foundHVCWriteCharacteristic = false; /* need to do the following only once */

            /* Note: Yuckiness alert! The following needs to be encapsulated in a neat API.
             * It isn't clear whether we should provide a DiscoveredCharacteristic::enableNoticiation() or
             * DiscoveredCharacteristic::discoverDescriptors() followed by DiscoveredDescriptor::write(...). */
            // uint16_t value = BLE_HVX_NOTIFICATION;
            // ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ,
            //                        connectionHandle,
            //                        HVCNotifyCharacteristic.getValueHandle() + 1, /* HACK Alert. We're assuming that CCCD descriptor immediately follows the value attribute. */
            //                        sizeof(uint16_t),                          /* HACK Alert! size should be made into a BLE_API constant. */
            //                        reinterpret_cast<const uint8_t *>(&value));
            ble.gattClient().write(GattClient::GATT_OP_WRITE_REQ,
                                   connectionHandle,
                                   HVCWriteCharacteristic.getValueHandle(), /* HACK Alert. We're assuming that CCCD descriptor immediately follows the value attribute. */
                                   7,                          /* HACK Alert! size should be made into a BLE_API constant. */
                                   send);
            //printf ( "snd_error %d \r\n" ,HVCWriteCharacteristic.write(4,send));

        }
        ble.waitForEvent();
    }
}