Erick / Mbed 2 deprecated ICE_BLE_TEST

Dependencies:   NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed

Fork of ICE by Erick

Committer:
jinu
Date:
Fri Oct 28 18:44:12 2016 +0000
Revision:
284:cc72206ea8e0
Parent:
283:a09013615589
modified for live data testing

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 *****************************************************************************/
jinu 284:cc72206ea8e0 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 {
jinu 284:cc72206ea8e0 49 #if 0 //commented for Demo testing
davidjhoward 281:a362a9450b81 50 memset( BLE_ReceiveString, 0, BLE_RECEIVE_BUFFER_SIZE );
davidjhoward 282:2dc06137f1ec 51 if( len >= BLE_RECEIVE_BUFFER_SIZE ) {
davidjhoward 281:a362a9450b81 52 printf("Received file larger than buffer (len=%d)\r\n", len );
davidjhoward 281:a362a9450b81 53 return;
davidjhoward 266:09ba8e22963d 54 }
davidjhoward 282:2dc06137f1ec 55
davidjhoward 281:a362a9450b81 56 // memcpy( BLE_ReceiveString, rx_data, len ); // comment out and use line below until we receive JSON from APP
davidjhoward 281:a362a9450b81 57 // temporary until we receive actual string from APP
davidjhoward 281:a362a9450b81 58 memcpy( BLE_ReceiveString, fake_string, strlen(fake_string) );
davidjhoward 281:a362a9450b81 59 BLE_StringReceived = true;
davidjhoward 281:a362a9450b81 60 // printf("Data Received: %s\r\n", BLE_ReceiveString);
jinu 284:cc72206ea8e0 61 #endif
jinu 284:cc72206ea8e0 62
jinu 284:cc72206ea8e0 63 printf("Data Received: ");
jinu 284:cc72206ea8e0 64 for(int i=0;i<len;i++)
jinu 284:cc72206ea8e0 65 {
jinu 284:cc72206ea8e0 66 printf("%c",rx_data[i]);
jinu 284:cc72206ea8e0 67
jinu 284:cc72206ea8e0 68 if(rx_data[0] == 0x61)
jinu 284:cc72206ea8e0 69 {
jinu 284:cc72206ea8e0 70 BLE_StringReceived =true;
jinu 284:cc72206ea8e0 71 }
jinu 284:cc72206ea8e0 72 }
jinu 284:cc72206ea8e0 73 printf("\n\r");
davidjhoward 266:09ba8e22963d 74 }
davidjhoward 266:09ba8e22963d 75
jmarkel44 0:65cfa4873284 76 void BLEDataHandler(void const *args)
jmarkel44 0:65cfa4873284 77 {
jinu 284:cc72206ea8e0 78 uint8_t tx_array[500];
jinu 284:cc72206ea8e0 79 uint8_t event_status;
jinu 284:cc72206ea8e0 80
jinu 284:cc72206ea8e0 81 /*TODO
jinu 284:cc72206ea8e0 82 Getting the init status from Nano BLE register and
jinu 284:cc72206ea8e0 83 Proceed based on the status.
jinu 284:cc72206ea8e0 84 */
jinu 284:cc72206ea8e0 85 event_status = BLE.ConfigureBLEDevice(BLE_DEVICE_NAME);
jinu 284:cc72206ea8e0 86 BleDataRxCbRegister(BleRxDataCallback);
jinu 284:cc72206ea8e0 87 while(1) {
jinu 284:cc72206ea8e0 88
jinu 284:cc72206ea8e0 89 event_status = PollBLEEvents();
jinu 284:cc72206ea8e0 90 if(event_status == true && BLE_StringReceived == true) {
jinu 284:cc72206ea8e0 91 GetCurrentReadings( (char *)tx_array, sizeof(tx_array));
jinu 284:cc72206ea8e0 92 BLE.SendFile(tx_array,strlen((char *)tx_array));
jinu 284:cc72206ea8e0 93 BLE_StringReceived = false;
jinu 284:cc72206ea8e0 94 }
jinu 284:cc72206ea8e0 95 Thread::wait(100);
jinu 284:cc72206ea8e0 96 }
jinu 284:cc72206ea8e0 97 }
jinu 284:cc72206ea8e0 98 #if 0
jinu 284:cc72206ea8e0 99 void BLEDataHandler(void const *args)
jinu 284:cc72206ea8e0 100 {
davidjhoward 281:a362a9450b81 101 uint8_t tx_array[BLE_TRANSMIT_BUFFER_SIZE];
davidjhoward 257:e22102d2e079 102 uint8_t event_status;
davidjhoward 257:e22102d2e079 103
davidjhoward 249:68ed571e0002 104 /*TODO
davidjhoward 257:e22102d2e079 105 Getting the init status from Nano BLE register and
davidjhoward 257:e22102d2e079 106 Proceed based on the status.
davidjhoward 257:e22102d2e079 107 */
davidjhoward 266:09ba8e22963d 108 event_status = BLE.ConfigureBLEDevice(BLE_DEVICE_NAME);
davidjhoward 266:09ba8e22963d 109 BleDataRxCbRegister(BleRxDataCallback);
davidjhoward 257:e22102d2e079 110 while(1) {
davidjhoward 257:e22102d2e079 111
davidjhoward 266:09ba8e22963d 112 event_status = PollBLEEvents();
davidjhoward 281:a362a9450b81 113 if(event_status == true && BLE_StringReceived == true) {
davidjhoward 267:fdb40bcd9c12 114
davidjhoward 281:a362a9450b81 115 cJSON * root = cJSON_Parse( BLE_ReceiveString );
davidjhoward 267:fdb40bcd9c12 116 int mType = cJSON_GetObjectItem(root,"mtype")->valueint;
davidjhoward 267:fdb40bcd9c12 117
davidjhoward 281:a362a9450b81 118 printf("mType=%d, str=%s\r\n",mType, BLE_ReceiveString);
davidjhoward 267:fdb40bcd9c12 119 switch( mType ) {
davidjhoward 267:fdb40bcd9c12 120 case BT_GETLIVE_COMMAND_MTYPE: {
davidjhoward 267:fdb40bcd9c12 121
davidjhoward 267:fdb40bcd9c12 122 std::vector<std::string> RequestedTags;
davidjhoward 267:fdb40bcd9c12 123 cJSON *getreadings = cJSON_GetObjectItem(root, "getreadings");
davidjhoward 267:fdb40bcd9c12 124 for ( int i = 0; i < cJSON_GetArraySize(getreadings); ++i ) {
davidjhoward 267:fdb40bcd9c12 125 cJSON *item = cJSON_GetArrayItem(getreadings, i);
davidjhoward 267:fdb40bcd9c12 126 std::string tag = cJSON_GetObjectItem(item, "tag")->valuestring;
davidjhoward 267:fdb40bcd9c12 127 RequestedTags.push_back(tag);
davidjhoward 267:fdb40bcd9c12 128 }
davidjhoward 267:fdb40bcd9c12 129 GetTagReadings( RequestedTags, (char *)tx_array, sizeof(tx_array) );
davidjhoward 279:b60379a9eb1a 130 printf("%s:%d: Sending Reply to APP: %s\r\n",__func__,__LINE__,tx_array);
davidjhoward 267:fdb40bcd9c12 131 BLE.SendFile(tx_array,strlen((char *)tx_array));
davidjhoward 267:fdb40bcd9c12 132 break;
davidjhoward 267:fdb40bcd9c12 133 }
davidjhoward 282:2dc06137f1ec 134 case BT_MODBUS_COMMAND_MTYPE: {
davidjhoward 282:2dc06137f1ec 135 printf("%s:%d: GOT MODBUS COMMAND\r\n",__func__,__LINE__);
davidjhoward 282:2dc06137f1ec 136
davidjhoward 282:2dc06137f1ec 137 cJSON *mbcommand = cJSON_GetObjectItem(root, "mbcommand");
davidjhoward 282:2dc06137f1ec 138 std::string payload_string = cJSON_PrintUnformatted(mbcommand);
davidjhoward 282:2dc06137f1ec 139
davidjhoward 282:2dc06137f1ec 140 printf("%s:%d: Sending Command Request to ModbusMaster: %s\r\n",__func__,__LINE__,payload_string.c_str());
jinu 284:cc72206ea8e0 141
davidjhoward 282:2dc06137f1ec 142 ModbusMasterReq_t *mail = ModbusMasterMailBox.alloc();
davidjhoward 282:2dc06137f1ec 143 mail->action = ACTION_EXEC_CMD;
davidjhoward 282:2dc06137f1ec 144 mail->replyThread = BLE_HANDLER;
davidjhoward 282:2dc06137f1ec 145 strncpy( mail->controlFile, payload_string.c_str(), (sizeof(mail->controlFile)-1));
davidjhoward 282:2dc06137f1ec 146 ModbusMasterMailBox.put(mail);
davidjhoward 282:2dc06137f1ec 147 break;
davidjhoward 282:2dc06137f1ec 148 }
davidjhoward 267:fdb40bcd9c12 149 default:
davidjhoward 267:fdb40bcd9c12 150 printf("unknown mtype received\r\n");
davidjhoward 267:fdb40bcd9c12 151 break;
davidjhoward 267:fdb40bcd9c12 152 }
davidjhoward 281:a362a9450b81 153 BLE_StringReceived = false;
davidjhoward 277:86dbc2483b67 154 cJSON_Delete(root);
davidjhoward 249:68ed571e0002 155 }
davidjhoward 279:b60379a9eb1a 156
davidjhoward 279:b60379a9eb1a 157 // This will wait 100ms for a message
davidjhoward 279:b60379a9eb1a 158 osEvent evt = BLEHandlerMailBox.get(100);
davidjhoward 279:b60379a9eb1a 159 if (evt.status == osEventMail) {
davidjhoward 279:b60379a9eb1a 160 BLEHandlerReq_t *mail = (BLEHandlerReq_t*)evt.value.p;
davidjhoward 279:b60379a9eb1a 161 printf("%s:%d: Sending Reply to APP: %s\r\n",__func__,__LINE__,mail->reply);
davidjhoward 279:b60379a9eb1a 162 BLE.SendFile((uint8_t *)mail->reply,strlen((char *)mail->reply));
davidjhoward 279:b60379a9eb1a 163 BLEHandlerMailBox.free(mail);
davidjhoward 279:b60379a9eb1a 164 }
davidjhoward 257:e22102d2e079 165 }
jinu 284:cc72206ea8e0 166 }
jinu 284:cc72206ea8e0 167
jinu 284:cc72206ea8e0 168 #endif