Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: NaturalTinyShell_ice libmDot-12Sept mbed-rtos mbed
Fork of ICE by
Diff: src/BLEDataHandler/BLEDataHandler.cpp
- Revision:
- 281:a362a9450b81
- Parent:
- 279:b60379a9eb1a
- Child:
- 282:2dc06137f1ec
--- a/src/BLEDataHandler/BLEDataHandler.cpp Wed Oct 26 21:03:53 2016 +0000 +++ b/src/BLEDataHandler/BLEDataHandler.cpp Wed Oct 26 21:26:52 2016 +0000 @@ -27,8 +27,11 @@ BLE_INIT ble_init; #define BLE_DEVICE_NAME "Ecolab" +#define BLE_RECEIVE_BUFFER_SIZE 500 +#define BLE_TRANSMIT_BUFFER_SIZE 500 -bool send_json = false; +bool BLE_StringReceived = false; +char BLE_ReceiveString[BLE_RECEIVE_BUFFER_SIZE]; /***************************************************************************** * Function: BLE data receive callback @@ -39,25 +42,27 @@ * @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) { - printf("Data Received: "); - for(int i=0; i<len; i++) { - printf("%c",rx_data[i]); - - if(rx_data[0] == 0x61) { - send_json =true; - } + 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; } - printf("\n\r"); - + +// 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[500]; + uint8_t tx_array[BLE_TRANSMIT_BUFFER_SIZE]; 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 @@ -68,18 +73,12 @@ 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) { + if(event_status == true && BLE_StringReceived == true) { - cJSON * root = cJSON_Parse( recv_str ); // recv_str is used to generate a fake message, Jinu can remove for testing. + cJSON * root = cJSON_Parse( BLE_ReceiveString ); int mType = cJSON_GetObjectItem(root,"mtype")->valueint; - printf("mType=%d, str=%s\r\n",mType, recv_str); + printf("mType=%d, str=%s\r\n",mType, BLE_ReceiveString); switch( mType ) { case BT_GETLIVE_COMMAND_MTYPE: { @@ -90,18 +89,16 @@ 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; } + BLE_StringReceived = false; cJSON_Delete(root); }