tauri-plugin-bluetooth-manager 0.1.0

A Tauri plugin to manage Bluetooth adapters and devices in Linux.
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
use futures::StreamExt;
use tauri::{plugin::PluginApi, AppHandle, Emitter, Manager, Runtime};
use zbus::{
    zvariant::{ObjectPath, OwnedValue, Value as ZbusValue},
    Connection, MessageStream, MessageType, Proxy,
};
use std::collections::HashMap;
use std::convert::TryFrom;

use crate::commands::{get_adapter_state, get_device_info};
use crate::models::*;
use crate::Result as CrateResult;

#[derive(Clone)]
pub struct BluetoothManager {
    conn: Connection,
}

pub async fn init<R: Runtime>(app: AppHandle<R>, _api: PluginApi<R, ()>) -> CrateResult<()> {
    let conn = Connection::system().await?;

    let manager = BluetoothManager {
        conn: conn.clone(),
    };

    app.manage(manager);

    // Suscribirse explícitamente a las señales antes de iniciar el listener
    setup_dbus_subscriptions(&conn).await?;

    tauri::async_runtime::spawn(run_signal_listener(conn, app));

    Ok(())
}

async fn setup_dbus_subscriptions(conn: &Connection) -> CrateResult<()> {
    println!("[bluetooth-plugin] Setting up D-Bus subscriptions...");
    
    // Suscribirse a las señales del ObjectManager de BlueZ
    let proxy = Proxy::new(
        conn,
        "org.bluez",
        "/",
        "org.freedesktop.DBus.ObjectManager",
    ).await?;

    // Intentar llamar a GetManagedObjects para verificar la conectividad
    match proxy.call_method("GetManagedObjects", &()).await {
        Ok(_) => println!("[bluetooth-plugin] Successfully connected to BlueZ ObjectManager"),
        Err(e) => {
            eprintln!("[bluetooth-plugin] Failed to connect to BlueZ ObjectManager: {:?}", e);
            return Err(e.into());
        }
    }

    // Configurar filtros de señales más específicos
    let dbus_proxy = Proxy::new(
        conn,
        "org.freedesktop.DBus",
        "/org/freedesktop/DBus",
        "org.freedesktop.DBus",
    ).await?;

    // Agregar reglas de coincidencia para señales específicas
    let rules = vec![
        "type='signal',sender='org.bluez',interface='org.freedesktop.DBus.ObjectManager'",
        "type='signal',sender='org.bluez',interface='org.freedesktop.DBus.Properties'",
        "type='signal',sender='org.bluez',interface='org.bluez.Adapter1'",
        "type='signal',sender='org.bluez',interface='org.bluez.Device1'",
    ];

    for rule in rules {
        match dbus_proxy.call_method("AddMatch", &(rule,)).await {
            Ok(_) => println!("[bluetooth-plugin] Added D-Bus match rule: {}", rule),
            Err(e) => eprintln!("[bluetooth-plugin] Failed to add match rule '{}': {:?}", rule, e),
        }
    }

    println!("[bluetooth-plugin] D-Bus subscriptions setup complete");
    Ok(())
}

fn helper_adapter_info_from_props(
    path: String,
    props: &HashMap<String, OwnedValue>,
) -> AdapterInfo {
    AdapterInfo {
        path,
        address: props.get("Address").and_then(|v| String::try_from(v.clone()).ok()).unwrap_or_default(),
        name: props.get("Name").and_then(|v| String::try_from(v.clone()).ok()).unwrap_or_default(),
        alias: props.get("Alias").and_then(|v| String::try_from(v.clone()).ok()).unwrap_or_default(),
        class: props.get("Class").and_then(|v| u32::try_from(v.clone()).ok()).unwrap_or_default(),
        powered: props.get("Powered").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        discoverable: props.get("Discoverable").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        discoverable_timeout: props.get("DiscoverableTimeout").and_then(|v| u32::try_from(v.clone()).ok()).unwrap_or_default(),
        pairable: props.get("Pairable").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        pairable_timeout: props.get("PairableTimeout").and_then(|v| u32::try_from(v.clone()).ok()).unwrap_or_default(),
        discovering: props.get("Discovering").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        uuids: props.get("UUIDs")
            .and_then(|v| Vec::<String>::try_from(v.clone()).ok())
            .unwrap_or_default(),
        modalias: props.get("Modalias").and_then(|v| String::try_from(v.clone()).ok()),
    }
}

fn helper_device_info_from_props(path: String, props: &HashMap<String, OwnedValue>) -> DeviceInfo {
    DeviceInfo {
        path,
        address: props.get("Address").and_then(|v| String::try_from(v.clone()).ok()).unwrap_or_default(),
        name: props.get("Name").and_then(|v| String::try_from(v.clone()).ok()),
        alias: props.get("Alias").and_then(|v| String::try_from(v.clone()).ok()),
        class: props.get("Class").and_then(|v| u32::try_from(v.clone()).ok()),
        appearance: props.get("Appearance").and_then(|v| u16::try_from(v.clone()).ok()),
        icon: props.get("Icon").and_then(|v| String::try_from(v.clone()).ok()),
        paired: props.get("Paired").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        trusted: props.get("Trusted").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        blocked: props.get("Blocked").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        legacy_pairing: props.get("LegacyPairing").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        rssi: props.get("RSSI").and_then(|v| i16::try_from(v.clone()).ok()),
        tx_power: props.get("TxPower").and_then(|v| i16::try_from(v.clone()).ok()),
        connected: props.get("Connected").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
        uuids: props.get("UUIDs")
            .and_then(|v| Vec::<String>::try_from(v.clone()).ok())
            .unwrap_or_default(),
        adapter: props.get("Adapter")
            .and_then(|v| ObjectPath::try_from(v.clone()).ok())
            .map(|p: ObjectPath| p.to_string())
            .unwrap_or_default(),
        services_resolved: props.get("ServicesResolved").and_then(|v| bool::try_from(v.clone()).ok()).unwrap_or(false),
    }
}

async fn run_signal_listener<R: Runtime>(conn: Connection, app: AppHandle<R>) {
    let mut stream = MessageStream::from(conn.clone());

    // Obtener el nombre único de org.bluez para comparación
    let mut bluez_unique_name: Option<String> = None;
    
    // Intentar obtener el nombre único del servicio org.bluez
    let _dbus_proxy = match Proxy::new(
        &conn,
        "org.freedesktop.DBus",
        "/org/freedesktop/DBus",
        "org.freedesktop.DBus",
    ).await {
        Ok(proxy) => {
            match proxy.call_method("GetNameOwner", &("org.bluez",)).await {
                Ok(reply) => {
                    if let Ok(unique_name) = reply.body::<String>() {
                        bluez_unique_name = Some(unique_name);
                    }
                }
                Err(e) => eprintln!("[bluetooth-plugin] Failed to get org.bluez unique name: {:?}", e),
            }
            Some(proxy)
        }
        Err(e) => {
            eprintln!("[bluetooth-plugin] Failed to create D-Bus proxy: {:?}", e);
            None
        }
    };

    while let Some(msg_res) = stream.next().await {
        match msg_res {
          Ok(msg) => {
            if msg.message_type() == MessageType::Signal {
                let header = match msg.header() {
                    Ok(h) => h,
                    Err(e) => {
                        eprintln!("[bluetooth-plugin] Failed to get message header: {:?}", e);
                        continue;
                    }
                };

                let sender_opt_str = match header.sender() {
                    Ok(Some(unique_name_ref)) => Some(unique_name_ref.as_str()),
                    Ok(None) => None,
                    Err(e) => {
                        eprintln!("[bluetooth-plugin] Error getting sender from header: {:?}", e);
                        None
                    }
                };

                let is_bluez_signal = sender_opt_str == Some("org.bluez") || 
                    (bluez_unique_name.is_some() && sender_opt_str == bluez_unique_name.as_deref());

                if is_bluez_signal {
                    let interface_opt_string = match header.interface() {
                        Ok(Some(i_ref)) => Some(i_ref.as_str().to_string()),
                        Ok(None) => None,
                        Err(e) => {
                            eprintln!("[bluetooth-plugin] Error getting interface from header: {:?}", e);
                            None
                        }
                    };

                    let member_opt_string = match header.member() {
                        Ok(Some(m_ref)) => Some(m_ref.as_str().to_string()),
                        Ok(None) => None,
                        Err(e) => {
                            eprintln!("[bluetooth-plugin] Error getting member from header: {:?}", e);
                            None
                        }
                    };

                    let path_opt_string = match header.path() {
                        Ok(Some(p_ref)) => Some(p_ref.as_str().to_string()),
                        Ok(None) => None,
                        Err(e) => {
                            eprintln!("[bluetooth-plugin] Error getting path from header: {:?}", e);
                            None
                        }
                    };
                    
                    match (interface_opt_string.as_deref(), member_opt_string.as_deref()) {
                        (Some("org.freedesktop.DBus.ObjectManager"), Some("InterfacesAdded")) => {
                            match msg.body::<(ObjectPath<'_>, HashMap<String, HashMap<String, OwnedValue>>)>() {
                                Ok((object_path, interfaces_and_properties)) => {
                                  let path_string = object_path.to_string();
                                  
                                  // Detectar cambios de adaptadores
                                  if let Some(adapter_props) = interfaces_and_properties.get("org.bluez.Adapter1") {
                                    let adapter_info = helper_adapter_info_from_props(path_string.clone(), adapter_props);
                                    
                                    app.emit("bluetooth-change", BluetoothChange {
                                        change_type: "adapter-added".to_string(),
                                        data: serde_json::to_value(adapter_info).unwrap_or_default(),
                                    }).unwrap_or_else(|e| eprintln!("[bluetooth-plugin] Failed to emit adapter-added: {}", e));
                                  }
                                  
                                  // Detectar cambios de dispositivos
                                  if let Some(device_props) = interfaces_and_properties.get("org.bluez.Device1") {
                                    let device_info = helper_device_info_from_props(path_string.clone(), device_props);
                                    
                                    app.emit("bluetooth-change", BluetoothChange {
                                        change_type: "device-added".to_string(),
                                        data: serde_json::to_value(device_info).unwrap_or_default(),
                                    }).unwrap_or_else(|e| eprintln!("[bluetooth-plugin] Failed to emit device-added: {}", e));
                                  }
                                }
                                Err(e) => {
                                  eprintln!("[bluetooth-plugin] Error decoding InterfacesAdded body: {:?}", e);
                                  app.emit("bluetooth-change", BluetoothChange {
                                      change_type: "error".to_string(),
                                      data: serde_json::json!({ "message": format!("Error decoding InterfacesAdded: {:?}", e) }),
                                  }).unwrap_or_else(|err| eprintln!("[bluetooth-plugin] Failed to emit error: {}", err));
                                }
                              }
                        }
                        (Some("org.freedesktop.DBus.ObjectManager"), Some("InterfacesRemoved")) => {
                            println!("[bluetooth-plugin] Processing InterfacesRemoved signal");
                            match msg.body::<(ObjectPath<'_>, Vec<String>)>() {
                                Ok((object_path, interfaces_removed)) => {
                                  let path_string = object_path.to_string();
                                  
                                  // Detectar remoción de adaptadores
                                  if interfaces_removed.contains(&"org.bluez.Adapter1".to_string()) {
                                    println!("[bluetooth-plugin] Adapter removed: {}", path_string);
                                    app.emit("bluetooth-change", BluetoothChange {
                                        change_type: "adapter-removed".to_string(),
                                        data: serde_json::json!({ "path": path_string.clone() }),
                                    }).unwrap_or_else(|e| eprintln!("[bluetooth-plugin] Failed to emit adapter-removed: {}", e));
                                  }
                                  
                                  // Detectar remoción de dispositivos
                                  if interfaces_removed.contains(&"org.bluez.Device1".to_string()) {
                                    
                                    app.emit("bluetooth-change", BluetoothChange {
                                        change_type: "device-removed".to_string(),
                                        data: serde_json::json!({ "path": path_string }),
                                    }).unwrap_or_else(|e| eprintln!("[bluetooth-plugin] Failed to emit device-removed: {}", e));
                                  }
                                }
                                Err(e) => {
                                  eprintln!("[bluetooth-plugin] Error decoding InterfacesRemoved body: {:?}", e);
                                  app.emit("bluetooth-change", BluetoothChange {
                                      change_type: "error".to_string(),
                                      data: serde_json::json!({ "message": format!("Error decoding InterfacesRemoved: {:?}", e) }),
                                  }).unwrap_or_else(|err| eprintln!("[bluetooth-plugin] Failed to emit error: {}", err));
                                }
                              }
                        }
                        (Some("org.freedesktop.DBus.Properties"), Some("PropertiesChanged")) => {
                            
                            if let Some(p_str) = path_opt_string {
                                match msg.body::<(String, HashMap<String, ZbusValue<'_>>, Vec<String>)>() {
                                    Ok((changed_interface_name, _changed_properties, _invalidated_properties)) => {
                                        println!("[bluetooth-plugin] PropertiesChanged for interface: {} on path: {}", changed_interface_name, p_str);
                                        
                                        if changed_interface_name == "org.bluez.Adapter1" {
                                            match get_adapter_state(p_str.clone()).await {
                                                Ok(adapter_info) => {
                                                    println!("[bluetooth-plugin] Adapter property changed: {}", p_str);
                                                    app.emit("bluetooth-change", BluetoothChange {
                                                        change_type: "adapter-property-changed".to_string(),
                                                        data: serde_json::to_value(adapter_info).unwrap_or_default(),
                                                    }).unwrap_or_else(|e| eprintln!("[bluetooth-plugin] Failed to emit adapter-property-changed: {}", e));
                                                }
                                                Err(e) => {
                                                    eprintln!("[bluetooth-plugin] Error getting adapter state for {}: {:?}", p_str, e);
                                                    app.emit("bluetooth-change", BluetoothChange {
                                                        change_type: "error".to_string(),
                                                        data: serde_json::json!({ "message": format!("Error getting adapter state: {:?}", e) }),
                                                    }).unwrap_or_else(|err| eprintln!("[bluetooth-plugin] Failed to emit error: {}", err));
                                                }
                                            }
                                        } 
                                        else if changed_interface_name == "org.bluez.Device1" {
                                            match get_device_info(p_str.clone()).await {
                                                Ok(device_info) => {
                                                    println!("[bluetooth-plugin] Device property changed: {}", p_str);
                                                    app.emit("bluetooth-change", BluetoothChange {
                                                        change_type: "device-property-changed".to_string(),
                                                        data: serde_json::to_value(device_info).unwrap_or_default(),
                                                    }).unwrap_or_else(|e| eprintln!("[bluetooth-plugin] Failed to emit device-property-changed: {}", e));
                                                }
                                                Err(e) => {
                                                    eprintln!("[bluetooth-plugin] Error getting device info for {}: {:?}", p_str, e);
                                                    app.emit("bluetooth-change", BluetoothChange {
                                                        change_type: "error".to_string(),
                                                        data: serde_json::json!({ "message": format!("Error getting device info: {:?}", e) }),
                                                    }).unwrap_or_else(|err| eprintln!("[bluetooth-plugin] Failed to emit error: {}", err));
                                                }
                                            }
                                        }
                                    }
                                    Err(e) => {
                                        eprintln!("[bluetooth-plugin] Error decoding PropertiesChanged body: {:?}", e);
                                        app.emit("bluetooth-change", BluetoothChange {
                                            change_type: "error".to_string(),
                                            data: serde_json::json!({ "message": format!("Error decoding PropertiesChanged: {:?}", e) }),
                                        }).unwrap_or_else(|err| eprintln!("[bluetooth-plugin] Failed to emit error: {}", err));
                                    }
                                }
                            } else {
                                eprintln!("[bluetooth-plugin] PropertiesChanged signal received without a valid path.");
                                app.emit("bluetooth-change", BluetoothChange {
                                    change_type: "error".to_string(),
                                    data: serde_json::json!({ "message": "PropertiesChanged signal without path" }),
                                }).unwrap_or_else(|err| eprintln!("[bluetooth-plugin] Failed to emit error: {}", err));
                            }
                        }
                        (Some("org.bluez.Device1"), Some("Disconnected")) => {
                            println!("[bluetooth-plugin] Processing Device Disconnected signal");
                            if let Some(p_str) = path_opt_string {
                                match get_device_info(p_str.clone()).await {
                                    Ok(device_info) => {
                                        
                                        app.emit("bluetooth-change", BluetoothChange {
                                            change_type: "device-disconnected".to_string(),
                                            data: serde_json::to_value(device_info).unwrap_or_default(),
                                        }).unwrap_or_else(|e| eprintln!("[bluetooth-plugin] Failed to emit device-disconnected: {}", e));
                                    }
                                    Err(e) => {
                                        eprintln!("[bluetooth-plugin] Error getting device info for disconnected device {}: {:?}", p_str, e);
                                    }
                                }
                            }
                        }
                        (Some("org.bluez.Device1"), Some("Connected")) => {
                            
                            if let Some(p_str) = path_opt_string {
                                match get_device_info(p_str.clone()).await {
                                    Ok(device_info) => {
                                        println!("[bluetooth-plugin] Device connected: {}", p_str);
                                        app.emit("bluetooth-change", BluetoothChange {
                                            change_type: "device-connected".to_string(),
                                            data: serde_json::to_value(device_info).unwrap_or_default(),
                                        }).unwrap_or_else(|e| eprintln!("[bluetooth-plugin] Failed to emit device-connected: {}", e));
                                    }
                                    Err(e) => {
                                        eprintln!("[bluetooth-plugin] Error getting device info for connected device {}: {:?}", p_str, e);
                                    }
                                }
                            }
                        }
                        _ => {
                        }
                    }
                }
            }
          }
          Err(e) => {
            eprintln!("[bluetooth-plugin] Error reading from D-Bus message stream: {:?}", e);
            app.emit("bluetooth-change", BluetoothChange {
                change_type: "dbus-error".to_string(),
                data: serde_json::json!({ "message": format!("D-Bus stream error: {:?}", e) }),
            }).unwrap_or_else(|err| eprintln!("[bluetooth-plugin] Failed to emit dbus-error: {}", err));
            break;
          }
        }
    }
}

impl BluetoothManager {
    pub fn ping(
        &self,
        payload: crate::models::PingRequest,
    ) -> CrateResult<crate::models::PingResponse> {
        Ok(crate::models::PingResponse {
            value: payload.value,
        })
    }
}