Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

src/BLEDataHandler/BLEDataHandler.cpp

Committer:
davidjhoward
Date:
2016-10-26
Revision:
281:a362a9450b81
Parent:
279:b60379a9eb1a
Child:
282:2dc06137f1ec

File content as of revision 281:a362a9450b81:

/******************************************************************************
 *
 * File:                BLEDataHandler.cpp
 * Desciption:          source for the ICE Bluetooth Low Energy Data Handler
 *
 *****************************************************************************/
#include "global.h"
#include <stdio.h>
#include "BLEDataHandler.h"
#include "CloudDataHandler.h"
#include "LoggerApi.h"
#include "ble_main.h"
#include "ble_init.h"
#include "ble_msg_handler.h"
#include "ble_spi.h"
#include "cJSON.h"

/*****************************************************************************
 * Function:             BLEDataHandler
 * Description:          entry point for the Analytics Logger
 *
 * @param                (IN) args (user-defined arguments)
 * @return               none
 *****************************************************************************/

BLE_FILE BLE;
BLE_INIT ble_init;

#define BLE_DEVICE_NAME "Ecolab"
#define BLE_RECEIVE_BUFFER_SIZE 500
#define BLE_TRANSMIT_BUFFER_SIZE 500

bool BLE_StringReceived = false;
char BLE_ReceiveString[BLE_RECEIVE_BUFFER_SIZE];

/*****************************************************************************
* Function:            BLE data receive callback
* Description:         Process BLE data
*
* @param               spi_rcv_array
* @param               *rx_data
* @param               length
* @return              none
*****************************************************************************/
char * fake_string = "{  \"mtype\":1200, \"getreadings\":[ { \"tag\":\"i_tra01\" } ] }"; // use this to fake a message out requesting i_tra01 readings
static void BleRxDataCallback(uint8_t *rx_data, uint8_t len)
{
    memset( BLE_ReceiveString, 0, BLE_RECEIVE_BUFFER_SIZE );
    if( len >= BLE_RECEIVE_BUFFER_SIZE )
    {
        printf("Received file larger than buffer (len=%d)\r\n", len );
        return;
    }
    
//    memcpy( BLE_ReceiveString, rx_data, len ); // comment out and use line below until we receive JSON from APP
    // temporary until we receive actual string from APP
    memcpy( BLE_ReceiveString, fake_string, strlen(fake_string) );
    BLE_StringReceived = true;
//    printf("Data Received:  %s\r\n", BLE_ReceiveString);
}

void BLEDataHandler(void const *args)
{
    uint8_t tx_array[BLE_TRANSMIT_BUFFER_SIZE];
    uint8_t event_status;

    /*TODO
        Getting the init status from Nano BLE register and
        Proceed based on the status.
    */
    event_status = BLE.ConfigureBLEDevice(BLE_DEVICE_NAME);
    BleDataRxCbRegister(BleRxDataCallback);
    while(1) {

        event_status = PollBLEEvents();
        if(event_status == true && BLE_StringReceived == true) {

            cJSON * root = cJSON_Parse( BLE_ReceiveString );
            int mType = cJSON_GetObjectItem(root,"mtype")->valueint;

            printf("mType=%d, str=%s\r\n",mType, BLE_ReceiveString);
            switch( mType ) {
                case BT_GETLIVE_COMMAND_MTYPE: {

                    std::vector<std::string>  RequestedTags;
                    cJSON *getreadings    = cJSON_GetObjectItem(root, "getreadings");
                    for ( int i = 0; i < cJSON_GetArraySize(getreadings); ++i ) {
                        cJSON *item = cJSON_GetArrayItem(getreadings, i);
                        std::string tag      = cJSON_GetObjectItem(item, "tag")->valuestring;
                        RequestedTags.push_back(tag);
                    }
                    GetTagReadings( RequestedTags, (char *)tx_array, sizeof(tx_array) );
                    printf("%s:%d: Sending Reply to APP: %s\r\n",__func__,__LINE__,tx_array);
                    BLE.SendFile(tx_array,strlen((char *)tx_array));
                    break;
                }
                default:
                    printf("unknown mtype received\r\n");
                    break;
            }
            BLE_StringReceived = false;
            cJSON_Delete(root);
        }

        // This will wait 100ms for a message
        osEvent evt = BLEHandlerMailBox.get(100);
        if (evt.status == osEventMail) {
            BLEHandlerReq_t *mail = (BLEHandlerReq_t*)evt.value.p;
            printf("%s:%d: Sending Reply to APP: %s\r\n",__func__,__LINE__,mail->reply);
            BLE.SendFile((uint8_t *)mail->reply,strlen((char *)mail->reply));
            BLEHandlerMailBox.free(mail);
        }
    }
}