Skip to content

Commit cb15bb1

Browse files
committed
Merge remote-tracking branch 'openbci/master'
2 parents e413c87 + 5926e46 commit cb15bb1

14 files changed

Lines changed: 192 additions & 90 deletions

File tree

cpp-package/src/board_shim.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ void BoardShim::prepare_session ()
9999
}
100100
}
101101

102+
bool BoardShim::is_prepared ()
103+
{
104+
int prepared = 0;
105+
int res = ::is_prepared (&prepared, board_id, const_cast<char *> (serialized_params.c_str ()));
106+
if (res != STATUS_OK)
107+
{
108+
throw BrainFlowException ("failed to check session", res);
109+
}
110+
return (bool)prepared;
111+
}
112+
102113
void BoardShim::start_stream (int buffer_size, char *streamer_params)
103114
{
104115
int res = ::start_stream (

cpp-package/src/inc/board_shim.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ class BoardShim
162162
Range for multicast addresses is from "224.0.0.0" to "239.255.255.255"
163163
*/
164164
void start_stream (int buffer_size = 450000, char *streamer_params = NULL);
165+
/// check if session is ready or not
166+
bool is_prepared ();
165167
/// stop streaming thread, doesnt release other resources
166168
void stop_stream ();
167169
/// release streaming session

csharp-package/brainflow/brainflow/board_controller_library.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ public static class BoardControllerLibrary64
112112
public static extern int get_other_channels (int board_id, int[] channels, int[] len);
113113
[DllImport ("BoardController.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
114114
public static extern int get_temperature_channels (int board_id, int[] channels, int[] len);
115+
[DllImport("BoardController.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
116+
public static extern int is_prepared(int[] prepared, int board_id, string input_json);
115117
}
116118

117119
public static class BoardControllerLibrary32
@@ -170,6 +172,8 @@ public static class BoardControllerLibrary32
170172
public static extern int get_other_channels (int board_id, int[] channels, int[] len);
171173
[DllImport ("BoardController32.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
172174
public static extern int get_temperature_channels (int board_id, int[] channels, int[] len);
175+
[DllImport("BoardController32.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
176+
public static extern int is_prepared(int[] prepared, int board_id, string input_json);
173177
}
174178

175179
public static class BoardControllerLibrary
@@ -222,6 +226,14 @@ public static int get_board_data_count (int[] result, int board_id, string input
222226
return BoardControllerLibrary32.get_board_data_count (result, board_id, input_json);
223227
}
224228

229+
public static int is_prepared(int[] result, int board_id, string input_json)
230+
{
231+
if (System.Environment.Is64BitProcess)
232+
return BoardControllerLibrary64.is_prepared(result, board_id, input_json);
233+
else
234+
return BoardControllerLibrary32.is_prepared(result, board_id, input_json);
235+
}
236+
225237
public static int get_board_data (int data_count, double[] data_buf, int board_id, string input_json)
226238
{
227239
if (System.Environment.Is64BitProcess)

csharp-package/brainflow/brainflow/board_shim.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,21 @@ public void release_session ()
505505
}
506506
}
507507

508+
/// <summary>
509+
/// check session status
510+
/// </summary>
511+
/// <returns>session status</returns>
512+
public bool is_prepared ()
513+
{
514+
int[] res = new int[1];
515+
int ec = BoardControllerLibrary.is_prepared(res, board_id, input_json);
516+
if (ec != (int)CustomExitCodes.STATUS_OK)
517+
{
518+
throw new BrainFlowException(ec);
519+
}
520+
return res[0] != 0;
521+
}
522+
508523
/// <summary>
509524
/// get number of packages in ringbuffer
510525
/// </summary>

java-package/brainflow/src/main/java/brainflow/BoardShim.java

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@ public class BoardShim
1919

2020
private interface DllInterface extends Library
2121
{
22-
int prepare_session (int board_id, String port_name);
22+
int prepare_session (int board_id, String params);
2323

24-
int config_board (String config, int board_id, String port_name);
24+
int config_board (String config, int board_id, String params);
2525

26-
int start_stream (int buffer_size, String streamer_params, int board_id, String port_name);
26+
int start_stream (int buffer_size, String streamer_params, int board_id, String params);
2727

28-
int stop_stream (int board_id, String port_name);
28+
int stop_stream (int board_id, String params);
2929

30-
int release_session (int board_id, String port_name);
30+
int release_session (int board_id, String params);
3131

3232
int get_current_board_data (int num_samples, double[] data_buf, int[] returned_samples, int board_id,
33-
String port_name);
33+
String params);
3434

35-
int get_board_data_count (int[] result, int board_id, String port_name);
35+
int get_board_data_count (int[] result, int board_id, String params);
3636

37-
int get_board_data (int data_count, double[] data_buf, int board_id, String port_name);
37+
int get_board_data (int data_count, double[] data_buf, int board_id, String params);
3838

3939
int set_log_level (int log_level);
4040

@@ -73,6 +73,8 @@ int get_current_board_data (int num_samples, double[] data_buf, int[] returned_s
7373
int get_other_channels (int board_id, int[] other_channels, int[] len);
7474

7575
int get_temperature_channels (int board_id, int[] temperature_channels, int[] len);
76+
77+
int is_prepared (int[] prepared, int board_id, String params);
7678
}
7779

7880
private static DllInterface instance;
@@ -560,6 +562,20 @@ public int get_board_data_count () throws BrainFlowError
560562
return res[0];
561563
}
562564

565+
/**
566+
* check session status
567+
*/
568+
public boolean is_prepared () throws BrainFlowError
569+
{
570+
int[] res = new int[1];
571+
int ec = instance.is_prepared (res, board_id, input_json);
572+
if (ec != ExitCode.STATUS_OK.get_code ())
573+
{
574+
throw new BrainFlowError ("Error in is_prepared", ec);
575+
}
576+
return res[0] != 0;
577+
}
578+
563579
/**
564580
* get latest collected data, can return less than "num_samples", doesnt flush
565581
* it from ringbuffer

python-package/brainflow/board_shim.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,14 @@ def __init__ (self):
121121
ctypes.c_char_p
122122
]
123123

124+
self.is_prepared = self.lib.is_prepared
125+
self.is_prepared.restype = ctypes.c_int
126+
self.is_prepared.argtypes = [
127+
ndpointer (ctypes.c_int32),
128+
ctypes.c_int,
129+
ctypes.c_char_p
130+
]
131+
124132
self.start_stream = self.lib.start_stream
125133
self.start_stream.restype = ctypes.c_int
126134
self.start_stream.argtypes = [
@@ -774,6 +782,19 @@ def get_board_data_count (self):
774782
raise BrainFlowError ('unable to obtain buffer size', res)
775783
return data_size[0]
776784

785+
def is_prepared (self):
786+
"""Check if session is ready or not
787+
788+
:return: session status
789+
:rtype: bool
790+
"""
791+
prepared = numpy.zeros (1).astype (numpy.int32)
792+
793+
res = BoardControllerDLL.get_instance ().is_prepared (prepared, self.board_id, self.input_json)
794+
if res != BrainflowExitCodes.STATUS_OK.value:
795+
raise BrainFlowError ('unable to check session status', res)
796+
return bool(prepared[0])
797+
777798
def get_board_data (self):
778799
"""Get all board data and remove them from ringbuffer
779800

src/board_controller/board_controller.cpp

Lines changed: 53 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ std::mutex mutex;
3333

3434
std::pair<int, struct BrainFlowInputParams> get_key (
3535
int board_id, struct BrainFlowInputParams params);
36-
static int check_board_session (std::pair<int, struct BrainFlowInputParams> key);
36+
static int check_board_session (int board_id, char *json_brainflow_input_params,
37+
std::pair<int, struct BrainFlowInputParams> &key, bool log_error = true);
3738
static int string_to_brainflow_input_params (
3839
const char *json_brainflow_input_params, struct BrainFlowInputParams *params);
3940

@@ -96,24 +97,38 @@ int prepare_session (int board_id, char *json_brainflow_input_params)
9697
{
9798
board = NULL;
9899
}
99-
boards[key] = board;
100+
else
101+
{
102+
boards[key] = board;
103+
}
100104
return res;
101105
}
102106

103-
int start_stream (
104-
int buffer_size, char *streamer_params, int board_id, char *json_brainflow_input_params)
107+
int is_prepared (int *prepared, int board_id, char *json_brainflow_input_params)
105108
{
106109
std::lock_guard<std::mutex> lock (mutex);
107110

108-
struct BrainFlowInputParams params;
109-
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
110-
if (res != STATUS_OK)
111+
std::pair<int, struct BrainFlowInputParams> key;
112+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
113+
if (res == STATUS_OK)
111114
{
112-
return res;
115+
*prepared = 1;
113116
}
117+
if (res == BOARD_NOT_CREATED_ERROR)
118+
{
119+
*prepared = 0;
120+
res = STATUS_OK;
121+
}
122+
return res;
123+
}
114124

115-
std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
116-
res = check_board_session (key);
125+
int start_stream (
126+
int buffer_size, char *streamer_params, int board_id, char *json_brainflow_input_params)
127+
{
128+
std::lock_guard<std::mutex> lock (mutex);
129+
130+
std::pair<int, struct BrainFlowInputParams> key;
131+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
117132
if (res != STATUS_OK)
118133
{
119134
return res;
@@ -126,15 +141,8 @@ int stop_stream (int board_id, char *json_brainflow_input_params)
126141
{
127142
std::lock_guard<std::mutex> lock (mutex);
128143

129-
struct BrainFlowInputParams params;
130-
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
131-
if (res != STATUS_OK)
132-
{
133-
return res;
134-
}
135-
136-
std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
137-
res = check_board_session (key);
144+
std::pair<int, struct BrainFlowInputParams> key;
145+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
138146
if (res != STATUS_OK)
139147
{
140148
return res;
@@ -147,15 +155,8 @@ int release_session (int board_id, char *json_brainflow_input_params)
147155
{
148156
std::lock_guard<std::mutex> lock (mutex);
149157

150-
struct BrainFlowInputParams params;
151-
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
152-
if (res != STATUS_OK)
153-
{
154-
return res;
155-
}
156-
157-
std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
158-
res = check_board_session (key);
158+
std::pair<int, struct BrainFlowInputParams> key;
159+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
159160
if (res != STATUS_OK)
160161
{
161162
return res;
@@ -171,15 +172,8 @@ int get_current_board_data (int num_samples, double *data_buf, int *returned_sam
171172
{
172173
std::lock_guard<std::mutex> lock (mutex);
173174

174-
struct BrainFlowInputParams params;
175-
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
176-
if (res != STATUS_OK)
177-
{
178-
return res;
179-
}
180-
181-
std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
182-
res = check_board_session (key);
175+
std::pair<int, struct BrainFlowInputParams> key;
176+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
183177
if (res != STATUS_OK)
184178
{
185179
return res;
@@ -192,15 +186,8 @@ int get_board_data_count (int *result, int board_id, char *json_brainflow_input_
192186
{
193187
std::lock_guard<std::mutex> lock (mutex);
194188

195-
struct BrainFlowInputParams params;
196-
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
197-
if (res != STATUS_OK)
198-
{
199-
return res;
200-
}
201-
202-
std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
203-
res = check_board_session (key);
189+
std::pair<int, struct BrainFlowInputParams> key;
190+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
204191
if (res != STATUS_OK)
205192
{
206193
return res;
@@ -214,15 +201,8 @@ int get_board_data (
214201
{
215202
std::lock_guard<std::mutex> lock (mutex);
216203

217-
struct BrainFlowInputParams params;
218-
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
219-
if (res != STATUS_OK)
220-
{
221-
return res;
222-
}
223-
224-
std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
225-
res = check_board_session (key);
204+
std::pair<int, struct BrainFlowInputParams> key;
205+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
226206
if (res != STATUS_OK)
227207
{
228208
return res;
@@ -273,15 +253,8 @@ int config_board (char *config, int board_id, char *json_brainflow_input_params)
273253
{
274254
std::lock_guard<std::mutex> lock (mutex);
275255

276-
struct BrainFlowInputParams params;
277-
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
278-
if (res != STATUS_OK)
279-
{
280-
return res;
281-
}
282-
283-
std::pair<int, struct BrainFlowInputParams> key = get_key (board_id, params);
284-
res = check_board_session (key);
256+
std::pair<int, struct BrainFlowInputParams> key;
257+
int res = check_board_session (board_id, json_brainflow_input_params, key, false);
285258
if (res != STATUS_OK)
286259
{
287260
return res;
@@ -301,12 +274,25 @@ std::pair<int, struct BrainFlowInputParams> get_key (
301274
return key;
302275
}
303276

304-
int check_board_session (std::pair<int, struct BrainFlowInputParams> key)
277+
int check_board_session (int board_id, char *json_brainflow_input_params,
278+
std::pair<int, struct BrainFlowInputParams> &key, bool log_error)
305279
{
280+
struct BrainFlowInputParams params;
281+
int res = string_to_brainflow_input_params (json_brainflow_input_params, &params);
282+
if (res != STATUS_OK)
283+
{
284+
return res;
285+
}
286+
287+
key = get_key (board_id, params);
288+
306289
if (boards.find (key) == boards.end ())
307290
{
308-
Board::board_logger->error (
309-
"Board with id {} and port provided config is not created", key.first);
291+
if (log_error)
292+
{
293+
Board::board_logger->error (
294+
"Board with id {} and port provided config is not created", key.first);
295+
}
310296
return BOARD_NOT_CREATED_ERROR;
311297
}
312298
return STATUS_OK;

src/board_controller/inc/board_controller.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ extern "C"
7474
int data_count, double *data_buf, int board_id, char *json_brainflow_input_params);
7575
SHARED_EXPORT int CALLING_CONVENTION config_board (
7676
char *config, int board_id, char *json_brainflow_input_params);
77+
SHARED_EXPORT int CALLING_CONVENTION is_prepared (
78+
int *prepared, int board_id, char *json_brainflow_input_params);
7779

7880
// logging methods
7981
SHARED_EXPORT int CALLING_CONVENTION set_log_level (int log_level);

0 commit comments

Comments
 (0)