mw75 0.0.7

Rust client for MW75 Neuro EEG headphones over BLE + RFCOMM using btleplug
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
//! Diagnostic tool for debugging macOS RFCOMM connection to MW75 Neuro.
//!
//! Usage: cargo run --bin rfcomm-debug --features rfcomm
//!
//! This binary runs a series of diagnostic probes against the MW75 headphones:
//!
//! 1. BLE activation (enable EEG + raw mode)
//! 2. Enumerate all paired/recent IOBluetooth devices
//! 3. SDP service record enumeration (list all RFCOMM channels)
//! 4. Try opening RFCOMM on discovered channels
//! 5. Try alternative connection strategies (keep BLE alive, etc.)

use std::sync::Arc;

use anyhow::Result;
use log::info;

use mw75::mw75_client::{Mw75Client, Mw75ClientConfig};
use mw75::types::Mw75Event;

#[tokio::main]
async fn main() -> Result<()> {
    mw75::logging::init("info");

    let config = Mw75ClientConfig {
        scan_timeout_secs: 10,
        name_pattern: "MW75".into(),
        ..Mw75ClientConfig::default()
    };
    let client = Mw75Client::new(config);

    // ── Phase 1: BLE activation ───────────────────────────────────────────────
    info!("═══════════════════════════════════════════════════════════════");
    info!("Phase 1: BLE activation");
    info!("═══════════════════════════════════════════════════════════════");

    let (mut rx, handle) = client.connect().await?;
    let handle = Arc::new(handle);
    handle.start().await?;

    // Drain events
    while let Ok(event) = rx.try_recv() {
        match event {
            Mw75Event::Connected(name) => info!("  Connected: {name}"),
            Mw75Event::Battery(b) => info!("  Battery: {}%", b.level),
            Mw75Event::Activated(s) => info!(
                "  Activated: EEG={}, Raw={}",
                s.eeg_enabled, s.raw_mode_enabled
            ),
            _ => {}
        }
    }

    let device_name = handle.device_name().to_string();
    let peripheral_id = handle.peripheral_id();
    info!("  Device name: {device_name}");
    info!("  Peripheral ID (CoreBluetooth UUID): {peripheral_id}");

    // ── Phase 2: IOBluetooth diagnostics (on a dedicated thread) ──────────────
    info!("");
    info!("═══════════════════════════════════════════════════════════════");
    info!("Phase 2: IOBluetooth device enumeration & SDP probe");
    info!("═══════════════════════════════════════════════════════════════");

    // Strategy A: Try RFCOMM while BLE is still connected
    info!("");
    info!("─── Strategy A: RFCOMM with BLE still connected ───");
    let name_a = device_name.clone();
    let result_a =
        tokio::task::spawn_blocking(move || run_rfcomm_diagnostics(&name_a, "A (BLE connected)"))
            .await?;
    info!("Strategy A result: {result_a:?}");

    // Strategy B: Disconnect BLE first, then try RFCOMM
    info!("");
    info!("─── Strategy B: disconnect BLE, then RFCOMM ───");
    handle.disconnect_ble().await.ok();

    // Various settle times
    for settle_ms in [500, 2000, 5000] {
        info!("");
        info!("  Settle time: {settle_ms} ms …");
        tokio::time::sleep(std::time::Duration::from_millis(settle_ms)).await;

        let name_b = device_name.clone();
        let label = format!("B (BLE disconnected, settle={settle_ms}ms)");
        let result_b =
            tokio::task::spawn_blocking(move || run_rfcomm_diagnostics(&name_b, &label)).await?;
        info!("  Result: {result_b:?}");

        if result_b.is_ok() {
            info!("  ✅ SUCCESS — RFCOMM connected!");
            break;
        }
    }

    info!("");
    info!("═══════════════════════════════════════════════════════════════");
    info!("Diagnostics complete.");
    info!("═══════════════════════════════════════════════════════════════");

    Ok(())
}

#[cfg(target_os = "macos")]
fn run_rfcomm_diagnostics(device_name: &str, label: &str) -> Result<String> {
    use objc2::rc::Retained;
    use objc2::runtime::AnyObject;
    use objc2::{msg_send, ClassType};
    use objc2_foundation::{NSArray, NSDate, NSRunLoop, NSString};
    use objc2_io_bluetooth::IOBluetoothDevice;

    info!("[{label}] Enumerating IOBluetooth devices …");

    // ── List ALL paired devices ───────────────────────────────────────────────
    let mut all_devices: Vec<(String, String, bool)> = Vec::new();

    unsafe {
        let paired: Option<Retained<NSArray<IOBluetoothDevice>>> =
            msg_send![IOBluetoothDevice::class(), pairedDevices];
        if let Some(ref devices) = paired {
            let count: usize = msg_send![devices, count];
            info!("[{label}] Paired devices: {count}");
            for i in 0..count {
                let dev: Retained<IOBluetoothDevice> = msg_send![devices, objectAtIndex: i];
                let name_ptr: *const NSString = msg_send![&*dev, name];
                let addr_ptr: *const NSString = msg_send![&*dev, addressString];
                let connected: bool = msg_send![&*dev, isConnected];
                let name = if !name_ptr.is_null() {
                    (*name_ptr).to_string()
                } else {
                    "<null>".into()
                };
                let addr = if !addr_ptr.is_null() {
                    (*addr_ptr).to_string()
                } else {
                    "<null>".into()
                };
                info!("[{label}]   [{i}] name={name:30} addr={addr}  connected={connected}");
                all_devices.push((name, addr, connected));
            }
        } else {
            info!("[{label}] No paired devices returned");
        }

        let recent: Option<Retained<NSArray<IOBluetoothDevice>>> =
            msg_send![IOBluetoothDevice::class(), recentDevices: 20usize];
        if let Some(ref devices) = recent {
            let count: usize = msg_send![devices, count];
            info!("[{label}] Recent devices: {count}");
            for i in 0..count {
                let dev: Retained<IOBluetoothDevice> = msg_send![devices, objectAtIndex: i];
                let name_ptr: *const NSString = msg_send![&*dev, name];
                let addr_ptr: *const NSString = msg_send![&*dev, addressString];
                let connected: bool = msg_send![&*dev, isConnected];
                let name = if !name_ptr.is_null() {
                    (*name_ptr).to_string()
                } else {
                    "<null>".into()
                };
                let addr = if !addr_ptr.is_null() {
                    (*addr_ptr).to_string()
                } else {
                    "<null>".into()
                };
                info!("[{label}]   [{i}] name={name:30} addr={addr}  connected={connected}");
            }
        }
    }

    // ── Find the MW75 device ──────────────────────────────────────────────────
    let device: Retained<IOBluetoothDevice> = unsafe {
        let mut found: Option<Retained<IOBluetoothDevice>> = None;
        let paired: Option<Retained<NSArray<IOBluetoothDevice>>> =
            msg_send![IOBluetoothDevice::class(), pairedDevices];
        if let Some(ref devices) = paired {
            let count: usize = msg_send![devices, count];
            for i in 0..count {
                let dev: Retained<IOBluetoothDevice> = msg_send![devices, objectAtIndex: i];
                let name_ptr: *const NSString = msg_send![&*dev, name];
                if !name_ptr.is_null() {
                    let name_str = (*name_ptr).to_string();
                    if name_str == device_name {
                        found = Some(dev);
                        break;
                    }
                }
            }
        }
        match found {
            Some(d) => d,
            None => {
                return Err(anyhow::anyhow!(
                    "Device '{device_name}' not found in paired devices"
                ))
            }
        }
    };

    let runloop = NSRunLoop::currentRunLoop();

    // ── Check connection state ────────────────────────────────────────────────
    let is_connected: bool = unsafe { msg_send![&*device, isConnected] };
    info!("[{label}] Device isConnected = {is_connected}");

    // ── Try openConnection ────────────────────────────────────────────────────
    if !is_connected {
        info!("[{label}] Calling openConnection …");
        let result: i32 = unsafe { msg_send![&*device, openConnection] };
        info!(
            "[{label}] openConnection returned: 0x{result:08x} ({})",
            if result == 0 { "success" } else { "error" }
        );

        // Pump run loop up to 8 seconds, checking isConnected
        let deadline = std::time::Instant::now() + std::time::Duration::from_secs(8);
        loop {
            let date = NSDate::dateWithTimeIntervalSinceNow(0.25);
            runloop.runUntilDate(&date);

            let connected: bool = unsafe { msg_send![&*device, isConnected] };
            if connected {
                info!(
                    "[{label}] Baseband connected after {} ms",
                    (std::time::Instant::now() - (deadline - std::time::Duration::from_secs(8)))
                        .as_millis()
                );
                break;
            }
            if std::time::Instant::now() >= deadline {
                info!("[{label}] ⚠ Baseband connection timeout (8 s) — isConnected still false");
                break;
            }
        }
    }

    let is_connected: bool = unsafe { msg_send![&*device, isConnected] };
    info!("[{label}] After openConnection: isConnected = {is_connected}");

    // ── SDP query ─────────────────────────────────────────────────────────────
    info!("[{label}] Performing SDP query …");
    let sdp_result: i32 =
        unsafe { msg_send![&*device, performSDPQuery: std::ptr::null::<AnyObject>()] };
    info!("[{label}] performSDPQuery returned: 0x{sdp_result:08x}");

    // Pump run loop for SDP to complete
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(8);
    loop {
        let date = NSDate::dateWithTimeIntervalSinceNow(0.25);
        runloop.runUntilDate(&date);

        let services: *const AnyObject = unsafe { msg_send![&*device, services] };
        if !services.is_null() {
            let count: usize = unsafe { msg_send![services, count] };
            if count > 0 {
                info!("[{label}] SDP returned {count} service record(s)");
                break;
            }
        }
        if std::time::Instant::now() >= deadline {
            info!("[{label}] ⚠ SDP query timeout (8 s)");
            break;
        }
    }

    // ── Enumerate SDP service records ─────────────────────────────────────────
    let mut rfcomm_channels: Vec<u8> = Vec::new();

    unsafe {
        let services: *const AnyObject = msg_send![&*device, services];
        if !services.is_null() {
            let count: usize = msg_send![services, count];
            info!("[{label}] ── SDP Service Records ({count}) ──");
            for i in 0..count {
                let record: *const AnyObject = msg_send![services, objectAtIndex: i];

                // Get service name
                // IOBluetoothSDPServiceRecord getServiceName
                let svc_name: *const NSString = msg_send![record, getServiceName];
                let name = if !svc_name.is_null() {
                    (*svc_name).to_string()
                } else {
                    "<unnamed>".into()
                };

                // Try to get RFCOMM channel
                // getRFCOMMChannelID: takes a pointer to u8
                let mut ch_id: u8 = 0;
                let ch_result: i32 = msg_send![record, getRFCOMMChannelID: &mut ch_id as *mut u8];

                if ch_result == 0 {
                    info!("[{label}]   [{i}] {name:40}  RFCOMM channel = {ch_id}");
                    rfcomm_channels.push(ch_id);
                } else {
                    // Try L2CAP PSM
                    let mut psm: u16 = 0;
                    let psm_result: i32 = msg_send![record, getL2CAPPSM: &mut psm as *mut u16];
                    if psm_result == 0 {
                        info!("[{label}]   [{i}] {name:40}  L2CAP PSM = {psm}");
                    } else {
                        info!("[{label}]   [{i}] {name:40}  (no RFCOMM/L2CAP)");
                    }
                }
            }
        } else {
            info!("[{label}] No SDP service records available");
        }
    }

    info!("[{label}] Available RFCOMM channels: {rfcomm_channels:?}");

    if rfcomm_channels.is_empty() {
        return Err(anyhow::anyhow!("No RFCOMM channels found in SDP records"));
    }

    // ── Try opening each RFCOMM channel ───────────────────────────────────────
    // Try channel 25 first (if present), then all others
    if !rfcomm_channels.contains(&25) {
        info!("[{label}] ⚠ Channel 25 NOT in SDP records! Will try discovered channels.");
    }

    // Put channel 25 first if it exists, then the rest
    let mut try_channels = Vec::new();
    if rfcomm_channels.contains(&25) {
        try_channels.push(25u8);
    }
    for &ch in &rfcomm_channels {
        if ch != 25 {
            try_channels.push(ch);
        }
    }

    for &channel in &try_channels {
        info!("[{label}] ── Trying RFCOMM channel {channel} ──");

        // Re-check connection state before each attempt
        let is_conn: bool = unsafe { msg_send![&*device, isConnected] };
        info!("[{label}]   isConnected = {is_conn}");

        let mut channel_ptr: *mut AnyObject = std::ptr::null_mut();
        let result: i32 = unsafe {
            msg_send![
                &*device,
                openRFCOMMChannelSync: &mut channel_ptr,
                withChannelID: channel,
                delegate: std::ptr::null::<AnyObject>()
            ]
        };

        if result == 0 && !channel_ptr.is_null() {
            let is_open: bool = unsafe { msg_send![channel_ptr, isOpen] };
            let mtu: u16 = unsafe { msg_send![channel_ptr, getMTU] };
            info!("[{label}]   ✅ Channel {channel} OPENED! isOpen={is_open} MTU={mtu}");

            // Close it. -[IOBluetoothRFCOMMChannel closeChannel] returns
            // IOReturn (i32); binding it as () trips objc2's encoding check.
            let _: i32 = unsafe { msg_send![channel_ptr, closeChannel] };
            info!("[{label}]   Channel {channel} closed.");

            return Ok(format!("Channel {channel} opened successfully"));
        } else {
            let error_name = match result as u32 {
                0xe00002bc => "kIOReturnNotPermitted",
                0xe00002c0 => "kIOReturnNotReady",
                0xe00002c2 => "kIOReturnNoDevice",
                0xe00002d8 => "kIOReturnTimeout",
                0xe00002eb => "kIOReturnNotOpen",
                _ => "unknown",
            };
            info!("[{label}]   ❌ Channel {channel} failed: 0x{result:08x} ({error_name})");
        }

        // Pump runloop between attempts
        let date = NSDate::dateWithTimeIntervalSinceNow(1.0);
        runloop.runUntilDate(&date);
    }

    // ── Try openRFCOMMChannelAsync as alternative ─────────────────────────────
    info!("[{label}] ── Trying openRFCOMMChannelAsync (channel 25) ──");
    unsafe {
        let mut channel_ptr: *mut AnyObject = std::ptr::null_mut();
        let result: i32 = msg_send![
            &*device,
            openRFCOMMChannelAsync: &mut channel_ptr,
            withChannelID: 25u8,
            delegate: std::ptr::null::<AnyObject>()
        ];
        info!("[{label}]   openRFCOMMChannelAsync returned: 0x{result:08x}");

        if result == 0 {
            // Pump run loop to let the async open complete
            let deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
            loop {
                let date = NSDate::dateWithTimeIntervalSinceNow(0.25);
                runloop.runUntilDate(&date);

                if !channel_ptr.is_null() {
                    let is_open: bool = msg_send![channel_ptr, isOpen];
                    if is_open {
                        info!("[{label}]   ✅ Async channel 25 opened!");
                        let _: i32 = msg_send![channel_ptr, closeChannel];
                        return Ok("Async channel 25 opened".into());
                    }
                }
                if std::time::Instant::now() >= deadline {
                    info!("[{label}]   ⚠ Async open timeout (5 s)");
                    break;
                }
            }
        }
    }

    Err(anyhow::anyhow!("All RFCOMM channel open attempts failed"))
}

#[cfg(not(target_os = "macos"))]
fn run_rfcomm_diagnostics(_device_name: &str, label: &str) -> Result<String> {
    info!("[{label}] RFCOMM diagnostics only supported on macOS");
    Ok("skipped (not macOS)".into())
}