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:
279:b60379a9eb1a
Parent:
277:86dbc2483b67
Child:
281:a362a9450b81

File content as of revision 279:b60379a9eb1a:

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

bool send_json = false;

/*****************************************************************************
* Function:            BLE data receive callback
* Description:         Process BLE data
*
* @param               spi_rcv_array
* @param               *rx_data
* @param               length
* @return              none
*****************************************************************************/
static void BleRxDataCallback(uint8_t *rx_data, uint8_t len)
{
    printf("Data Received:  ");
    for(int i=0; i<len; i++) {
        printf("%c",rx_data[i]);

        if(rx_data[0] == 0x61) {
            send_json =true;
        }
    }
    printf("\n\r");

}

void BLEDataHandler(void const *args)
{
    uint8_t tx_array[500];
    uint8_t event_status;
    char * recv_str = "{  \"mtype\":1200, \"getreadings\":[ { \"tag\":\"i_tra01\" } ] }"; // use this to fake a message out requesting i_tra01 readings

    /*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 ) {
            printf("event_status=true\r\n");
        }
        if(event_status == true ) {
            printf("send_json=true\r\n");
        }
        if(event_status == true && send_json == true) {

            cJSON * root = cJSON_Parse( recv_str ); // recv_str is used to generate a fake message, Jinu can remove for testing.
            int mType = cJSON_GetObjectItem(root,"mtype")->valueint;

            printf("mType=%d, str=%s\r\n",mType, recv_str);
            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));
                    send_json = false;
                    break;
                }
                default:
                    printf("unknown mtype received\r\n");
                    break;
            }
            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);
        }
    }
}