| 96 | } |
| 97 | |
| 98 | int Muse::prepare_session () |
| 99 | { |
| 100 | if (initialized) |
| 101 | { |
| 102 | safe_logger (spdlog::level::info, "Session is already prepared"); |
| 103 | return (int)BrainFlowExitCodes::STATUS_OK; |
| 104 | } |
| 105 | if (params.timeout < 1) |
| 106 | { |
| 107 | params.timeout = 6; |
| 108 | } |
| 109 | muse_preset = "p21"; |
| 110 | bool unused_low_latency = false; |
| 111 | std::string parse_error; |
| 112 | if (!MuseOptions::parse_preset_options (params.other_info, board_id, |
| 113 | MuseOptions::PresetFamily::Legacy, false, muse_preset, unused_low_latency, parse_error)) |
| 114 | { |
| 115 | safe_logger (spdlog::level::err, "Invalid Muse other_info: {}", parse_error); |
| 116 | return (int)BrainFlowExitCodes::INVALID_ARGUMENTS_ERROR; |
| 117 | } |
| 118 | safe_logger (spdlog::level::info, "Use Muse preset {}", muse_preset); |
| 119 | safe_logger (spdlog::level::info, "Use timeout for discovery: {}", params.timeout); |
| 120 | if (!init_dll_loader ()) |
| 121 | { |
| 122 | safe_logger (spdlog::level::err, "Failed to init dll_loader"); |
| 123 | return (int)BrainFlowExitCodes::GENERAL_ERROR; |
| 124 | } |
| 125 | size_t adapter_count = simpleble_adapter_get_count (); |
| 126 | if (adapter_count == 0) |
| 127 | { |
| 128 | safe_logger (spdlog::level::err, "No BLE adapters found"); |
| 129 | return (int)BrainFlowExitCodes::UNABLE_TO_OPEN_PORT_ERROR; |
| 130 | } |
| 131 | |
| 132 | safe_logger (spdlog::level::info, "found {} BLE adapter(s)", adapter_count); |
| 133 | |
| 134 | muse_adapter = simpleble_adapter_get_handle (0); |
| 135 | if (muse_adapter == NULL) |
| 136 | { |
| 137 | safe_logger (spdlog::level::err, "Adapter is NULL"); |
| 138 | return (int)BrainFlowExitCodes::UNABLE_TO_OPEN_PORT_ERROR; |
| 139 | } |
| 140 | |
| 141 | simpleble_adapter_set_callback_on_scan_found ( |
| 142 | muse_adapter, ::adapter_on_scan_found, (void *)this); |
| 143 | |
| 144 | if (!simpleble_adapter_is_bluetooth_enabled ()) |
| 145 | { |
| 146 | safe_logger (spdlog::level::warn, "Probably bluetooth is disabled."); |
| 147 | // dont throw an exception because of this |
| 148 | // https://github.com/OpenBluetoothToolbox/SimpleBLE/issues/115 |
| 149 | } |
| 150 | |
| 151 | simpleble_adapter_scan_start (muse_adapter); |
| 152 | int res = (int)BrainFlowExitCodes::STATUS_OK; |
| 153 | std::unique_lock<std::mutex> lk (m); |
| 154 | auto sec = std::chrono::seconds (1); |
| 155 | if (cv.wait_for (lk, params.timeout * sec, [this] { return this->muse_peripheral != NULL; })) |
nothing calls this directly
no test coverage detected