| 171 | } |
| 172 | |
| 173 | int MuseAthena::prepare_session () |
| 174 | { |
| 175 | if (initialized) |
| 176 | { |
| 177 | safe_logger (spdlog::level::info, "Session is already prepared"); |
| 178 | return (int)BrainFlowExitCodes::STATUS_OK; |
| 179 | } |
| 180 | int res = parse_muse_options (); |
| 181 | if (res != (int)BrainFlowExitCodes::STATUS_OK) |
| 182 | { |
| 183 | return res; |
| 184 | } |
| 185 | safe_logger (spdlog::level::info, "Use MuseAthena preset {} and low_latency {}", muse_preset, |
| 186 | enable_low_latency); |
| 187 | if (params.timeout < 1) |
| 188 | { |
| 189 | params.timeout = 6; |
| 190 | } |
| 191 | safe_logger (spdlog::level::info, "Use timeout for discovery: {}", params.timeout); |
| 192 | if (!init_dll_loader ()) |
| 193 | { |
| 194 | safe_logger (spdlog::level::err, "Failed to init dll_loader"); |
| 195 | return (int)BrainFlowExitCodes::GENERAL_ERROR; |
| 196 | } |
| 197 | size_t adapter_count = simpleble_adapter_get_count (); |
| 198 | if (adapter_count == 0) |
| 199 | { |
| 200 | safe_logger (spdlog::level::err, "No BLE adapters found"); |
| 201 | return (int)BrainFlowExitCodes::UNABLE_TO_OPEN_PORT_ERROR; |
| 202 | } |
| 203 | |
| 204 | safe_logger (spdlog::level::info, "found {} BLE adapter(s)", adapter_count); |
| 205 | |
| 206 | muse_adapter = simpleble_adapter_get_handle (0); |
| 207 | if (muse_adapter == NULL) |
| 208 | { |
| 209 | safe_logger (spdlog::level::err, "Adapter is NULL"); |
| 210 | return (int)BrainFlowExitCodes::UNABLE_TO_OPEN_PORT_ERROR; |
| 211 | } |
| 212 | |
| 213 | simpleble_adapter_set_callback_on_scan_found ( |
| 214 | muse_adapter, ::athena_adapter_on_scan_found, (void *)this); |
| 215 | |
| 216 | if (!simpleble_adapter_is_bluetooth_enabled ()) |
| 217 | { |
| 218 | safe_logger (spdlog::level::warn, "Probably bluetooth is disabled."); |
| 219 | // dont throw an exception because of this |
| 220 | // https://github.com/OpenBluetoothToolbox/SimpleBLE/issues/115 |
| 221 | } |
| 222 | |
| 223 | { |
| 224 | // Release m before stopping the scan; macOS scan callbacks also take it. |
| 225 | simpleble_adapter_scan_start (muse_adapter); |
| 226 | std::unique_lock<std::mutex> lk (m); |
| 227 | auto sec = std::chrono::seconds (1); |
| 228 | if (cv.wait_for ( |
| 229 | lk, params.timeout * sec, [this] { return this->muse_peripheral != NULL; })) |
| 230 | { |
nothing calls this directly
no test coverage detected