hdc-rs 0.1.2

Rust client library for HarmonyOS Device Connector (HDC)
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
//! Blocking (synchronous) API for HDC client
//!
//! This module provides a synchronous wrapper around the async HDC client,
//! suitable for use in synchronous contexts or FFI bindings (like PyO3).
//!
//! # Example
//!
//! ```no_run
//! use hdc_rs::blocking::HdcClient;
//!
//! let mut client = HdcClient::connect("127.0.0.1:8710")?;
//! let devices = client.list_targets()?;
//! println!("Devices: {:?}", devices);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```

use crate::{app::InstallOptions, app::UninstallOptions, file::FileTransferOptions, Result};

/// Blocking HDC client
///
/// This is a synchronous wrapper around the async [`crate::HdcClient`].
/// It creates a tokio runtime internally to execute async operations.
pub struct HdcClient {
    runtime: tokio::runtime::Runtime,
    inner: crate::HdcClient,
}

impl HdcClient {
    /// Connect to HDC server
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let client = HdcClient::connect("127.0.0.1:8710")?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn connect(addr: &str) -> Result<Self> {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .map_err(crate::HdcError::Io)?;

        let inner = runtime.block_on(crate::HdcClient::connect(addr))?;

        Ok(Self { runtime, inner })
    }

    /// List all connected devices
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// for device in devices {
    ///     println!("Device: {}", device);
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn list_targets(&mut self) -> Result<Vec<String>> {
        self.runtime.block_on(self.inner.list_targets())
    }

    /// Connect to a specific device
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// if !devices.is_empty() {
    ///     client.connect_device(&devices[0])?;
    /// }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn connect_device(&mut self, device_id: &str) -> Result<()> {
        self.runtime.block_on(self.inner.connect_device(device_id))
    }

    /// Execute a shell command on the device
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// let output = client.shell("ls -l")?;
    /// println!("Output: {}", output);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn shell(&mut self, command: &str) -> Result<String> {
        self.runtime.block_on(self.inner.shell(command))
    }

    /// Create a forward port mapping (local -> device)
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    /// use hdc_rs::forward::ForwardNode;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// let local = ForwardNode::Tcp(8080);
    /// let remote = ForwardNode::Tcp(8080);
    /// let result = client.fport(local, remote)?;
    /// println!("Forward result: {}", result);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn fport(
        &mut self,
        local: crate::forward::ForwardNode,
        remote: crate::forward::ForwardNode,
    ) -> Result<String> {
        self.runtime.block_on(self.inner.fport(local, remote))
    }

    /// Create a reverse port mapping (device -> local)
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    /// use hdc_rs::forward::ForwardNode;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// let remote = ForwardNode::Tcp(9090);
    /// let local = ForwardNode::Tcp(9090);
    /// let result = client.rport(remote, local)?;
    /// println!("Reverse forward result: {}", result);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn rport(
        &mut self,
        remote: crate::forward::ForwardNode,
        local: crate::forward::ForwardNode,
    ) -> Result<String> {
        self.runtime.block_on(self.inner.rport(remote, local))
    }

    /// Remove a forward port mapping
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// // Remove forward using task string (e.g., "tcp:8080 tcp:8080")
    /// client.fport_remove("tcp:8080 tcp:8080")?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn fport_remove(&mut self, task_str: &str) -> Result<String> {
        self.runtime.block_on(self.inner.fport_remove(task_str))
    }

    /// Install an application on the device
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    /// use hdc_rs::app::InstallOptions;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// let packages = vec!["app.hap"];
    /// let options = InstallOptions::new();
    /// let result = client.install(&packages, options)?;
    /// println!("Install result: {}", result);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn install(&mut self, packages: &[&str], options: InstallOptions) -> Result<String> {
        self.runtime.block_on(self.inner.install(packages, options))
    }

    /// Uninstall an application from the device
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    /// use hdc_rs::app::UninstallOptions;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// let options = UninstallOptions::new();
    /// let result = client.uninstall("com.example.app", options)?;
    /// println!("Uninstall result: {}", result);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn uninstall(&mut self, package: &str, options: UninstallOptions) -> Result<String> {
        self.runtime
            .block_on(self.inner.uninstall(package, options))
    }

    /// Send a file to the device
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    /// use hdc_rs::file::FileTransferOptions;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// let options = FileTransferOptions::default();
    /// let result = client.file_send("local.txt", "/data/local/tmp/remote.txt", options)?;
    /// println!("Transfer result: {}", result);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn file_send(
        &mut self,
        local_path: &str,
        remote_path: &str,
        options: FileTransferOptions,
    ) -> Result<String> {
        self.runtime
            .block_on(self.inner.file_send(local_path, remote_path, options))
    }

    /// Receive a file from the device
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    /// use hdc_rs::file::FileTransferOptions;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// let options = FileTransferOptions::default();
    /// let result = client.file_recv("/data/local/tmp/remote.txt", "local.txt", options)?;
    /// println!("Transfer result: {}", result);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn file_recv(
        &mut self,
        remote_path: &str,
        local_path: &str,
        options: FileTransferOptions,
    ) -> Result<String> {
        self.runtime
            .block_on(self.inner.file_recv(remote_path, local_path, options))
    }

    /// Get device logs (hilog) with buffering
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// // Get all logs
    /// let logs = client.hilog(None)?;
    /// println!("Logs: {}", logs);
    ///
    /// // Get logs with filter
    /// let logs = client.hilog(Some("-t MyTag"))?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn hilog(&mut self, args: Option<&str>) -> Result<String> {
        self.runtime.block_on(self.inner.hilog(args))
    }

    /// Wait for a device to be connected
    ///
    /// This will block until a device is found.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    ///
    /// // Wait for a device
    /// let device = client.wait_for_device()?;
    /// println!("Device found: {}", device);
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn wait_for_device(&mut self) -> Result<String> {
        self.runtime.block_on(self.inner.wait_for_device())
    }

    /// Stream device logs (hilog) with callback
    ///
    /// This method continuously streams logs from the device and calls the callback
    /// function for each log chunk received. The callback should return `true` to
    /// continue streaming or `false` to stop.
    ///
    /// # Arguments
    /// * `args` - Optional hilog command arguments (e.g., "-t MyTag" for filtering)
    /// * `callback` - Function called for each log chunk. Return `true` to continue, `false` to stop.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    /// let devices = client.list_targets()?;
    /// client.connect_device(&devices[0])?;
    ///
    /// // Stream all logs
    /// client.hilog_stream(None, |log_chunk| {
    ///     print!("{}", log_chunk);
    ///     true // Continue streaming
    /// })?;
    ///
    /// // Stream with filter
    /// client.hilog_stream(Some("-t MyTag"), |log_chunk| {
    ///     print!("{}", log_chunk);
    ///     true
    /// })?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn hilog_stream<F>(&mut self, args: Option<&str>, callback: F) -> Result<()>
    where
        F: FnMut(&str) -> bool,
    {
        self.runtime
            .block_on(self.inner.hilog_stream(args, callback))
    }

    /// Monitor device list changes with callback
    ///
    /// This function continuously polls the device list and calls the callback
    /// when changes are detected. The callback should return `true` to continue
    /// monitoring or `false` to stop.
    ///
    /// Note: HDC doesn't have a native "track-devices" command like adb,
    /// so this implementation uses polling to detect changes.
    ///
    /// # Arguments
    /// * `interval` - Polling interval in seconds (recommended: 1-3 seconds)
    /// * `callback` - Function called when device list changes. Return `true` to continue, `false` to stop.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use hdc_rs::blocking::HdcClient;
    ///
    /// let mut client = HdcClient::connect("127.0.0.1:8710")?;
    ///
    /// // Monitor device changes every 2 seconds
    /// client.monitor_devices(2, |devices| {
    ///     println!("Device list changed:");
    ///     for device in devices {
    ///         println!("  - {}", device);
    ///     }
    ///     true // Continue monitoring
    /// })?;
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn monitor_devices<F>(&mut self, interval_secs: u64, callback: F) -> Result<()>
    where
        F: FnMut(&[String]) -> bool,
    {
        let interval = std::time::Duration::from_secs(interval_secs);
        self.runtime
            .block_on(self.inner.monitor_devices(interval, callback))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[ignore] // Requires HDC server running
    fn test_blocking_client_creation() {
        let result = HdcClient::connect("127.0.0.1:8710");
        assert!(result.is_ok());
    }

    #[test]
    #[ignore] // Requires HDC server running
    fn test_blocking_list_targets() {
        let mut client = HdcClient::connect("127.0.0.1:8710").unwrap();
        let result = client.list_targets();
        assert!(result.is_ok());
    }
}