Skip to content

Commit

Permalink
Merge pull request #112 from daniellasry/fix-ganglion
Browse files Browse the repository at this point in the history
Fixing segfault when streaming ganglion on ubuntu
  • Loading branch information
Andrey1994 authored Jan 28, 2020
2 parents edbff40 + 300630b commit 54d3d66
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/board_controller/openbci/ganglion_bglib/callbacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 9 additions & 3 deletions src/board_controller/openbci/ganglion_bglib/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#define FIRST_HANDLE 0x0001
#define LAST_HANDLE 0xffff


namespace GanglionLib
{
extern volatile int exit_code;
Expand All @@ -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)
{
Expand All @@ -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<std::mutex> lock (mutex);

unsigned char *data = NULL;
struct ble_header hdr;
int r;

Expand All @@ -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
}
}
Expand All @@ -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;
}

Expand Down
4 changes: 1 addition & 3 deletions src/board_controller/openbci/ganglion_bglib/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <chrono>
#include <ctype.h>
#include <mutex>
#include <queue>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -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 ()
{
Expand Down
10 changes: 5 additions & 5 deletions src/board_controller/openbci/ganglion_bglib/uart.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Bluegigas Bluetooth Smart Demo Application
// Bluegigas Bluetooth Smart Demo Application
// Contact: [email protected].
//
// This is free software distributed under the terms of the MIT license reproduced below.
Expand Down Expand Up @@ -179,7 +179,7 @@ int uart_tx (int len, unsigned char *data)
return -1;
}
len -= written;
data += len;
data += written;
}

return 0;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -296,7 +296,7 @@ int uart_tx (int len, unsigned char *data)
return -1;
}
len -= written;
data += len;
data += written;
}

return 0;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 54d3d66

Please sign in to comment.