Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
davidjhoward
Date:
Fri Oct 28 16:28:44 2016 +0000
Revision:
283:a09013615589
Parent:
282:2dc06137f1ec
Child:
284:cc72206ea8e0
More updates

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jmarkel44 0:65cfa4873284 1 /******************************************************************************
davidjhoward 249:68ed571e0002 2 *
jmarkel44 0:65cfa4873284 3 * File: BLEDataHandler.cpp
jmarkel44 0:65cfa4873284 4 * Desciption: source for the ICE Bluetooth Low Energy Data Handler
jmarkel44 0:65cfa4873284 5 *
jmarkel44 0:65cfa4873284 6 *****************************************************************************/
jmarkel44 0:65cfa4873284 7 #include "global.h"
jmarkel44 0:65cfa4873284 8 #include <stdio.h>
davidjhoward 283:a09013615589 9 #include <sstream>
davidjhoward 283:a09013615589 10 #include <iostream>
jmarkel44 0:65cfa4873284 11 #include "BLEDataHandler.h"
davidjhoward 267:fdb40bcd9c12 12 #include "CloudDataHandler.h"
davidjhoward 249:68ed571e0002 13 #include "LoggerApi.h"
davidjhoward 249:68ed571e0002 14 #include "ble_main.h"
davidjhoward 249:68ed571e0002 15 #include "ble_init.h"
davidjhoward 249:68ed571e0002 16 #include "ble_msg_handler.h"
davidjhoward 257:e22102d2e079 17 #include "ble_spi.h"
davidjhoward 267:fdb40bcd9c12 18 #include "cJSON.h"
jmarkel44 0:65cfa4873284 19
jmarkel44 0:65cfa4873284 20 /*****************************************************************************
jmarkel44 0:65cfa4873284 21 * Function: BLEDataHandler
davidjhoward 249:68ed571e0002 22 * Description: entry point for the Analytics Logger
jmarkel44 0:65cfa4873284 23 *
jmarkel44 0:65cfa4873284 24 * @param (IN) args (user-defined arguments)
jmarkel44 0:65cfa4873284 25 * @return none
jmarkel44 0:65cfa4873284 26 *****************************************************************************/
davidjhoward 249:68ed571e0002 27
davidjhoward 249:68ed571e0002 28 BLE_FILE BLE;
davidjhoward 249:68ed571e0002 29 BLE_INIT ble_init;
davidjhoward 249:68ed571e0002 30
davidjhoward 266:09ba8e22963d 31 #define BLE_DEVICE_NAME "Ecolab"
davidjhoward 281:a362a9450b81 32 #define BLE_RECEIVE_BUFFER_SIZE 500
davidjhoward 281:a362a9450b81 33 #define BLE_TRANSMIT_BUFFER_SIZE 500
davidjhoward 266:09ba8e22963d 34
davidjhoward 281:a362a9450b81 35 bool BLE_StringReceived = false;
davidjhoward 281:a362a9450b81 36 char BLE_ReceiveString[BLE_RECEIVE_BUFFER_SIZE];
davidjhoward 266:09ba8e22963d 37
davidjhoward 283:a09013615589 38 void BLE_ReplyToHandler( std::string &id, int status, float value )
davidjhoward 283:a09013615589 39 {
davidjhoward 283:a09013615589 40 std::ostringstream mb_reply;
davidjhoward 283:a09013615589 41
davidjhoward 283:a09013615589 42 BLEHandlerReq_t *mail = BLEHandlerMailBox.alloc();
davidjhoward 283:a09013615589 43 mb_reply << "{ \"mtype\":1001, \"mbreply\":{ \"id\":\"" << id.c_str() << "\"," "\"status\":\"" << status << "\"," "\"value\":\"" << value << "\"} }";
davidjhoward 283:a09013615589 44 std::string string_reply = mb_reply.str();
davidjhoward 283:a09013615589 45 strncpy( mail->reply, string_reply.c_str(), (sizeof(mail->reply)-1));
davidjhoward 283:a09013615589 46 printf("%s:%d: Reply is: %s\r\n",__func__,__LINE__, mail->reply);
davidjhoward 283:a09013615589 47 BLEHandlerMailBox.put(mail);
davidjhoward 283:a09013615589 48 }
davidjhoward 283:a09013615589 49
davidjhoward 267:fdb40bcd9c12 50 /*****************************************************************************
davidjhoward 267:fdb40bcd9c12 51 * Function: BLE data receive callback
davidjhoward 267:fdb40bcd9c12 52 * Description: Process BLE data
davidjhoward 267:fdb40bcd9c12 53 *
davidjhoward 267:fdb40bcd9c12 54 * @param spi_rcv_array
davidjhoward 267:fdb40bcd9c12 55 * @param *rx_data
davidjhoward 267:fdb40bcd9c12 56 * @param length
davidjhoward 267:fdb40bcd9c12 57 * @return none
davidjhoward 267:fdb40bcd9c12 58 *****************************************************************************/
davidjhoward 283:a09013615589 59 char * fake_string = "{ \"mtype\":1200, \"getreadings\":[ { \"tag\":\"i_tra01\" }, { \"tag\":\"i_bdcond01\" }, { \"tag\":\"i_cond_temp01\" } ] }"; // use this to fake a message out requesting i_tra01 readings
davidjhoward 282:2dc06137f1ec 60 //char * fake_string = "{ \"mtype\":1000, \"mbcommand\":{ \"id\":\"READ_TRASAR\"," "\"node\":\"1\"," "\"func\":\"4\"," "\"sreg\":\"9\"," "\"nreg\":\"2\"," "\"dtype\":\"0\"," "\"order\":\"2\"," "\"value\":\"0\" } }"; // use this to fake a message out requesting i_tra01 readings
davidjhoward 283:a09013615589 61 //char * fake_string = "{ \"mtype\":1000, \"mbcommand\":{ \"id\":\"WRITE_MULTIPLE\"," "\"node\":\"21\"," "\"func\":\"16\"," "\"sreg\":\"0\"," "\"nreg\":\"3\"," "\"dtype\":\"5\"," "\"order\":\"2\"," "\"value\":\"0\", \"data\":[ { \"v\":\"20\" }, { \"v\":\"16\" }, { \"v\":\"0\" } ] } }"; // use this to fake a message out requesting i_tra01 readings
davidjhoward 283:a09013615589 62
davidjhoward 266:09ba8e22963d 63 static void BleRxDataCallback(uint8_t *rx_data, uint8_t len)
davidjhoward 266:09ba8e22963d 64 {
davidjhoward 281:a362a9450b81 65 memset( BLE_ReceiveString, 0, BLE_RECEIVE_BUFFER_SIZE );
davidjhoward 282:2dc06137f1ec 66 if( len >= BLE_RECEIVE_BUFFER_SIZE ) {
davidjhoward 281:a362a9450b81 67 printf("Received file larger than buffer (len=%d)\r\n", len );
davidjhoward 281:a362a9450b81 68 return;
davidjhoward 266:09ba8e22963d 69 }
davidjhoward 282:2dc06137f1ec 70
davidjhoward 281:a362a9450b81 71 // memcpy( BLE_ReceiveString, rx_data, len ); // comment out and use line below until we receive JSON from APP
davidjhoward 281:a362a9450b81 72 // temporary until we receive actual string from APP
davidjhoward 281:a362a9450b81 73 memcpy( BLE_ReceiveString, fake_string, strlen(fake_string) );
davidjhoward 281:a362a9450b81 74 BLE_StringReceived = true;
davidjhoward 281:a362a9450b81 75 // printf("Data Received: %s\r\n", BLE_ReceiveString);
davidjhoward 266:09ba8e22963d 76 }
davidjhoward 266:09ba8e22963d 77
jmarkel44 0:65cfa4873284 78 void BLEDataHandler(void const *args)
jmarkel44 0:65cfa4873284 79 {
davidjhoward 281:a362a9450b81 80 uint8_t tx_array[BLE_TRANSMIT_BUFFER_SIZE];
davidjhoward 257:e22102d2e079 81 uint8_t event_status;
davidjhoward 257:e22102d2e079 82
davidjhoward 249:68ed571e0002 83 /*TODO
davidjhoward 257:e22102d2e079 84 Getting the init status from Nano BLE register and
davidjhoward 257:e22102d2e079 85 Proceed based on the status.
davidjhoward 257:e22102d2e079 86 */
davidjhoward 266:09ba8e22963d 87 event_status = BLE.ConfigureBLEDevice(BLE_DEVICE_NAME);
davidjhoward 266:09ba8e22963d 88 BleDataRxCbRegister(BleRxDataCallback);
davidjhoward 257:e22102d2e079 89 while(1) {
davidjhoward 257:e22102d2e079 90
davidjhoward 266:09ba8e22963d 91 event_status = PollBLEEvents();
davidjhoward 281:a362a9450b81 92 if(event_status == true && BLE_StringReceived == true) {
davidjhoward 267:fdb40bcd9c12 93
davidjhoward 281:a362a9450b81 94 cJSON * root = cJSON_Parse( BLE_ReceiveString );
davidjhoward 267:fdb40bcd9c12 95 int mType = cJSON_GetObjectItem(root,"mtype")->valueint;
davidjhoward 267:fdb40bcd9c12 96
davidjhoward 281:a362a9450b81 97 printf("mType=%d, str=%s\r\n",mType, BLE_ReceiveString);
davidjhoward 267:fdb40bcd9c12 98 switch( mType ) {
davidjhoward 267:fdb40bcd9c12 99 case BT_GETLIVE_COMMAND_MTYPE: {
davidjhoward 267:fdb40bcd9c12 100
davidjhoward 267:fdb40bcd9c12 101 std::vector<std::string> RequestedTags;
davidjhoward 267:fdb40bcd9c12 102 cJSON *getreadings = cJSON_GetObjectItem(root, "getreadings");
davidjhoward 267:fdb40bcd9c12 103 for ( int i = 0; i < cJSON_GetArraySize(getreadings); ++i ) {
davidjhoward 267:fdb40bcd9c12 104 cJSON *item = cJSON_GetArrayItem(getreadings, i);
davidjhoward 267:fdb40bcd9c12 105 std::string tag = cJSON_GetObjectItem(item, "tag")->valuestring;
davidjhoward 267:fdb40bcd9c12 106 RequestedTags.push_back(tag);
davidjhoward 267:fdb40bcd9c12 107 }
davidjhoward 267:fdb40bcd9c12 108 GetTagReadings( RequestedTags, (char *)tx_array, sizeof(tx_array) );
davidjhoward 279:b60379a9eb1a 109 printf("%s:%d: Sending Reply to APP: %s\r\n",__func__,__LINE__,tx_array);
davidjhoward 267:fdb40bcd9c12 110 BLE.SendFile(tx_array,strlen((char *)tx_array));
davidjhoward 267:fdb40bcd9c12 111 break;
davidjhoward 267:fdb40bcd9c12 112 }
davidjhoward 282:2dc06137f1ec 113 case BT_MODBUS_COMMAND_MTYPE: {
davidjhoward 282:2dc06137f1ec 114 printf("%s:%d: GOT MODBUS COMMAND\r\n",__func__,__LINE__);
davidjhoward 282:2dc06137f1ec 115
davidjhoward 282:2dc06137f1ec 116 cJSON *mbcommand = cJSON_GetObjectItem(root, "mbcommand");
davidjhoward 282:2dc06137f1ec 117 std::string payload_string = cJSON_PrintUnformatted(mbcommand);
davidjhoward 282:2dc06137f1ec 118
davidjhoward 282:2dc06137f1ec 119 printf("%s:%d: Sending Command Request to ModbusMaster: %s\r\n",__func__,__LINE__,payload_string.c_str());
davidjhoward 283:a09013615589 120
davidjhoward 282:2dc06137f1ec 121 ModbusMasterReq_t *mail = ModbusMasterMailBox.alloc();
davidjhoward 282:2dc06137f1ec 122 mail->action = ACTION_EXEC_CMD;
davidjhoward 282:2dc06137f1ec 123 mail->replyThread = BLE_HANDLER;
davidjhoward 282:2dc06137f1ec 124 strncpy( mail->controlFile, payload_string.c_str(), (sizeof(mail->controlFile)-1));
davidjhoward 282:2dc06137f1ec 125 ModbusMasterMailBox.put(mail);
davidjhoward 282:2dc06137f1ec 126 break;
davidjhoward 282:2dc06137f1ec 127 }
davidjhoward 267:fdb40bcd9c12 128 default:
davidjhoward 267:fdb40bcd9c12 129 printf("unknown mtype received\r\n");
davidjhoward 267:fdb40bcd9c12 130 break;
davidjhoward 267:fdb40bcd9c12 131 }
davidjhoward 281:a362a9450b81 132 BLE_StringReceived = false;
davidjhoward 277:86dbc2483b67 133 cJSON_Delete(root);
davidjhoward 249:68ed571e0002 134 }
davidjhoward 279:b60379a9eb1a 135
davidjhoward 279:b60379a9eb1a 136 // This will wait 100ms for a message
davidjhoward 279:b60379a9eb1a 137 osEvent evt = BLEHandlerMailBox.get(100);
davidjhoward 279:b60379a9eb1a 138 if (evt.status == osEventMail) {
davidjhoward 279:b60379a9eb1a 139 BLEHandlerReq_t *mail = (BLEHandlerReq_t*)evt.value.p;
davidjhoward 279:b60379a9eb1a 140 printf("%s:%d: Sending Reply to APP: %s\r\n",__func__,__LINE__,mail->reply);
davidjhoward 279:b60379a9eb1a 141 BLE.SendFile((uint8_t *)mail->reply,strlen((char *)mail->reply));
davidjhoward 279:b60379a9eb1a 142 BLEHandlerMailBox.free(mail);
davidjhoward 279:b60379a9eb1a 143 }
davidjhoward 257:e22102d2e079 144 }
jmarkel44 0:65cfa4873284 145 }