Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
davidjhoward
Date:
Thu Oct 27 21:44:25 2016 +0000
Revision:
282:2dc06137f1ec
Parent:
281:a362a9450b81
Child:
283:a09013615589
Added Modbus Command Queries to BLE Handler

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