diff --git a/src/board_controller/openbci/ganglion_bglib/callbacks.cpp b/src/board_controller/openbci/ganglion_bglib/callbacks.cpp index f584dfe6e..7e0360ce8 100644 --- a/src/board_controller/openbci/ganglion_bglib/callbacks.cpp +++ b/src/board_controller/openbci/ganglion_bglib/callbacks.cpp @@ -188,7 +188,7 @@ void ble_evt_attclient_find_information_found ( void ble_evt_attclient_attribute_value (const struct ble_msg_attclient_attribute_value_evt_t *msg) { - if ((int)msg->value.len >= 18) + if (((int)msg->value.len >= 18) && ((int)msg->value.len <= 20)) { unsigned char values[20] = {0}; memcpy (values, msg->value.data, msg->value.len * sizeof (unsigned char)); diff --git a/src/board_controller/openbci/ganglion_bglib/helpers.cpp b/src/board_controller/openbci/ganglion_bglib/helpers.cpp index 9473d8593..569d8dfde 100644 --- a/src/board_controller/openbci/ganglion_bglib/helpers.cpp +++ b/src/board_controller/openbci/ganglion_bglib/helpers.cpp @@ -19,6 +19,7 @@ #define FIRST_HANDLE 0x0001 #define LAST_HANDLE 0xffff + namespace GanglionLib { extern volatile int exit_code; @@ -27,8 +28,8 @@ namespace GanglionLib extern volatile uint16 client_char_handle; extern char uart_port[1024]; extern volatile State state; - extern std::mutex m; extern std::condition_variable cv; + std::mutex mutex; void output (uint8 len1, uint8 *data1, uint16 len2, uint8 *data2) { @@ -41,7 +42,9 @@ namespace GanglionLib // reads messages and calls required callbacks (copypaste from sample) int read_message (int timeout_ms) { - unsigned char data[256]; // enough for BLE + std::lock_guard lock (mutex); + + unsigned char *data = NULL; struct ble_header hdr; int r; @@ -57,10 +60,12 @@ namespace GanglionLib } if (hdr.lolen) { + data = new unsigned char[hdr.lolen]; r = uart_rx (hdr.lolen, data, UART_TIMEOUT); if (r <= 0) { exit_code = (int)GanglionLib::PORT_OPEN_ERROR; + delete[] data; return 1; // fails to read } } @@ -70,11 +75,12 @@ namespace GanglionLib if (!msg) { exit_code = (int)GanglionLib::GENERAL_ERROR; + delete[] data; return 1; } msg->handler (data); - + delete[] data; return 0; } diff --git a/src/board_controller/openbci/ganglion_bglib/main.cpp b/src/board_controller/openbci/ganglion_bglib/main.cpp index f413cbb5e..981d1a636 100644 --- a/src/board_controller/openbci/ganglion_bglib/main.cpp +++ b/src/board_controller/openbci/ganglion_bglib/main.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -28,11 +27,10 @@ namespace GanglionLib volatile uint16 client_char_handle = 0; volatile State state = State::NONE; // same callbacks are triggered by different methods we need to differ them + volatile bool should_stop_stream = true; bool initialized = false; - std::mutex mutex; std::thread read_characteristic_thread; - bool should_stop_stream = true; void read_characteristic_worker () { diff --git a/src/board_controller/openbci/ganglion_bglib/uart.cpp b/src/board_controller/openbci/ganglion_bglib/uart.cpp index e94423a78..9cf03b1db 100644 --- a/src/board_controller/openbci/ganglion_bglib/uart.cpp +++ b/src/board_controller/openbci/ganglion_bglib/uart.cpp @@ -1,5 +1,5 @@ // -// Bluegiga’s Bluetooth Smart Demo Application +// Bluegiga�s Bluetooth Smart Demo Application // Contact: support@bluegiga.com. // // This is free software distributed under the terms of the MIT license reproduced below. @@ -179,7 +179,7 @@ int uart_tx (int len, unsigned char *data) return -1; } len -= written; - data += len; + data += written; } return 0; @@ -213,7 +213,7 @@ int uart_rx (int len, unsigned char *data, int timeout_ms) return 0; } len -= rread; - data += len; + data += rread; } return l; @@ -296,7 +296,7 @@ int uart_tx (int len, unsigned char *data) return -1; } len -= written; - data += len; + data += written; } return 0; @@ -325,7 +325,7 @@ int uart_rx (int len, unsigned char *data, int timeout_ms) return -1; } len -= rread; - data += len; + data += rread; } return l;