xlink 0.1.0

Unified Multi-Channel Communication SDK
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
<div align="center">

# 📘 API Reference

### Complete API Documentation

[🏠 Home](../README.md) • [📖 User Guide](USER_GUIDE.md) • [🏗️ Architecture](ARCHITECTURE.md)

---

</div>

## 📋 Table of Contents

- [Overview]#overview
- [Core API]#core-api
  - [Initialization]#initialization
  - [Configuration]#configuration
  - [Cipher Operations]#cipher-operations
  - [Key Management]#key-management
- [Algorithms]#algorithms
- [Error Handling]#error-handling
- [Type Definitions]#type-definitions
- [Examples]#examples

---

## Overview

<div align="center">

### 🎯 API Design Principles

</div>

<table>
<tr>
<td width="25%" align="center">
<img src="https://img.icons8.com/fluency/96/000000/easy.png" width="64"><br>
<b>Simple</b><br>
Intuitive and easy to use
</td>
<td width="25%" align="center">
<img src="https://img.icons8.com/fluency/96/000000/security-checked.png" width="64"><br>
<b>Safe</b><br>
Type-safe and secure by default
</td>
<td width="25%" align="center">
<img src="https://img.icons8.com/fluency/96/000000/module.png" width="64"><br>
<b>Composable</b><br>
Build complex workflows easily
</td>
<td width="25%" align="center">
<img src="https://img.icons8.com/fluency/96/000000/documentation.png" width="64"><br>
<b>Well-documented</b><br>
Comprehensive documentation
</td>
</tr>
</table>

---

## Core API

### Initialization

<div align="center">

#### 🚀 Getting Started

</div>

---

#### `XLink::new()`

Create a new xlink SDK instance with device capabilities and channels.

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub async fn new(
    config: DeviceCapabilities,
    channels: Vec<Arc<dyn Channel>>
) -> Result<Self, Error>
```

</td>
</tr>
<tr>
<td><b>Parameters</b></td>
<td>

- `config: DeviceCapabilities` - Device configuration including device ID and supported channels
- `channels: Vec<Arc<dyn Channel>>` - List of communication channels to use

</td>
</tr>
<tr>
<td><b>Returns</b></td>
<td><code>Result&lt;XLink, Error&gt;</code> - SDK instance on success, Error on failure</td>
</tr>
<tr>
<td><b>Errors</b></td>
<td>

- `Error::InvalidConfig` - Invalid device configuration
- `Error::ChannelError` - Channel initialization failed

</td>
</tr>
</table>

**Example:**

```rust
use xlink::XLink;
use xlink::core::types::DeviceCapabilities;
use xlink::core::types::DeviceId;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let device_id = DeviceId::new();
    let capabilities = DeviceCapabilities {
        device_id,
        device_type: xlink::core::types::DeviceType::Smartphone,
        device_name: "My Phone".to_string(),
        supported_channels: HashSet::from([ChannelType::Lan]),
        battery_level: Some(80),
        is_charging: false,
        data_cost_sensitive: false,
    };

    let sdk = XLink::new(capabilities, vec![]).await?;
    println!("✅ SDK created successfully");

    Ok(())
}
```

---

#### `start()`

Start the SDK and begin listening for messages.

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub async fn start(&self) -> Result<(), Error>
```

</td>
</tr>
<tr>
<td><b>Returns</b></td>
<td><code>Result&lt;(), Error&gt;</code></td>
</tr>
<tr>
<td><b>Errors</b></td>
<td>

- `Error::AlreadyRunning` - SDK already started
- `Error::ChannelError` - Failed to start channels

</td>
</tr>
</table>

**Example:**

```rust
let capabilities = DeviceCapabilities {
    device_id,
    device_type: xlink::core::types::DeviceType::Smartphone,
    device_name: "My Phone".to_string(),
    supported_channels: HashSet::from([ChannelType::Lan]),
    battery_level: Some(80),
    is_charging: false,
    data_cost_sensitive: false,
};

let sdk = XLink::new(capabilities, vec![]).await?;
sdk.start().await?;
println!("✅ SDK is now listening for messages");
```

---

#### `stop()`

Stop the SDK and release all resources.

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub async fn stop(&self)
```

</td>
</tr>
<tr>
<td><b>Description</b></td>
<td>Stops all channels, releases resources, and cleans up the SDK state.</td>
</tr>
</table>

**Example:**

```rust
sdk.start().await?;
// ... use SDK ...
sdk.stop().await;
println!("✅ SDK stopped");
```

---

### Configuration

<div align="center">

#### ⚙️ Device Capabilities

</div>

---

#### `DeviceCapabilities`

Device configuration structure for SDK initialization.

<table>
<tr>
<td width="30%"><b>Type</b></td>
<td width="70%">

```rust
pub struct DeviceCapabilities {
    pub device_id: DeviceId,
    pub device_type: DeviceType,
    pub device_name: String,
    pub supported_channels: HashSet<ChannelType>,
    pub battery_level: Option<u8>,
    pub is_charging: bool,
    pub data_cost_sensitive: bool,
}
```

</td>
</tr>
<tr>
<td><b>Fields</b></td>
<td>

- `device_id: DeviceId` - Unique identifier for this device
- `device_type: DeviceType` - Type of device (Smartphone, Laptop, etc.)
- `device_name: String` - Human-readable device name
- `supported_channels: HashSet<ChannelType>` - Set of supported channel types
- `battery_level: Option<u8>` - Current battery percentage (0-100)
- `is_charging: bool` - Whether the device is currently charging
- `data_cost_sensitive: bool` - Whether to optimize for data usage

</td>
</tr>
</table>

**Example:**

```rust
use xlink::core::types::DeviceCapabilities;
use std::collections::HashSet;
use xlink::core::types::ChannelType;

let capabilities = DeviceCapabilities {
    device_id: DeviceId::new(),
    device_type: DeviceType::Smartphone,
    device_name: "My Phone".to_string(),
    supported_channels: HashSet::from([
        ChannelType::Lan,
        ChannelType::BluetoothLE,
    ]),
    battery_level: Some(80),
    is_charging: false,
    data_cost_sensitive: false,
};
```

---

#### `DeviceId`

Unique device identifier type based on UUID.

<table>
<tr>
<td width="30%"><b>Type</b></td>
<td width="70%">

```rust
pub struct DeviceId(pub Uuid);
```

</td>
</tr>
<tr>
<td><b>Methods</b></td>
<td>

- `new() -> Self` - Create a new random device ID
- `from_str(s: &str) -> Result<Self, uuid::Error>` - Create from UUID string
- `to_string(&self) -> String` - Get UUID string representation

</td>
</tr>
</table>

**Example:**

```rust
use xlink::core::types::DeviceId;

// Create new device ID
let device_id = DeviceId::new();

// Parse from string
let device_id: DeviceId = "550e8400-e29b-41d4-a716-446655440000".parse()?;
```

---

### Cipher Operations

<div align="center">

#### 🔐 Encryption and Decryption

</div>

xlink 使用 `CryptoEngine` 提供端到端加密功能,基于 X25519 密钥交换和 ChaCha20Poly1305 加密算法。

---

#### `CryptoEngine`

核心加密引擎,提供密钥交换、消息加密/解密和数字签名功能。

<table>
<tr>
<td width="30%"><b>Type</b></td>
<td width="70%">

```rust
pub struct CryptoEngine {
    static_secret: StaticSecret,
    public_key: PublicKey,
    signing_key: SigningKey,
    sessions: Arc<DashMap<DeviceId, Mutex<SessionState>>>,
}
```

</td>
</tr>
</table>

**主要功能:**
- **密钥交换**:X25519 Diffie-Hellman 密钥交换
- **消息加密**:ChaCha20Poly1305 AEAD 加密
- **数字签名**:Ed25519 签名验证
- **密钥派生**:HKDF-SHA256 密钥派生

---

#### `CryptoEngine::new()`

创建新的加密引擎实例。

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub fn new() -> Self
```

</td>
</tr>
<tr>
<td><b>Returns</b></td>
<td><code>CryptoEngine</code> - 加密引擎实例</td>
</tr>
</table>

**Example:**

```rust
use xlink::crypto::engine::CryptoEngine;

let crypto = CryptoEngine::new();
let public_key = crypto.public_key();
```

---

#### `CryptoEngine::public_key()`

获取本地设备的 X25519 公钥。

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub fn public_key(&self) -> PublicKey
```

</td>
</tr>
<tr>
<td><b>Returns</b></td>
<td><code>PublicKey</code> - X25519 公钥,用于与远程设备建立安全通道</td>
</tr>
</table>

---

#### `CryptoEngine::verifying_key()`

获取本地设备的 Ed25519 验证公钥,用于数字签名验证。

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub fn verifying_key(&self) -> VerifyingKey
```

</td>
</tr>
<tr>
<td><b>Returns</b></td>
<td><code>VerifyingKey</code> - Ed25519 验证公钥</td>
</tr>
</table>

---

#### `CryptoEngine::encrypt_message()`

使用 ChaCha20Poly1305 加密消息。

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub fn encrypt_message(
    &self,
    recipient: &DeviceId,
    plaintext: &[u8]
) -> Result<Vec<u8>, Error>
```

</td>
</tr>
<tr>
<td><b>Parameters</b></td>
<td>

- `recipient: &DeviceId` - 接收方设备 ID
- `plaintext: &[u8]` - 要加密的明文数据

</td>
</tr>
<tr>
<td><b>Returns</b></td>
<td><code>Result&lt;Vec&lt;u8&gt;, Error&gt;</code> - 加密后的密文</td>
</tr>
<tr>
<td><b>Errors</b></td>
<td>

- `Error::SessionNotFound` - 与接收方的会话不存在
- `Error::EncryptionFailed` - 加密操作失败

</td>
</tr>
</table>

**Example:**

```rust
use xlink::crypto::engine::CryptoEngine;

let crypto = CryptoEngine::new();
let recipient: DeviceId = "peer-device".to_string().try_into()?;

let plaintext = b"Secret message";
let ciphertext = crypto.encrypt_message(&recipient, plaintext)?;
```

<details>
<summary><b>📝 Notes</b></summary>

- 每次加密自动生成随机 Nonce
- 使用 Double Ratchet 算法更新密钥
- 密文包含认证标签,篡改检测

</details>

---

#### `CryptoEngine::decrypt_message()`

解密接收到的消息。

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub fn decrypt_message(
    &self,
    sender: &DeviceId,
    ciphertext: &[u8]
) -> Result<Vec<u8>, Error>
```

</td>
</tr>
<tr>
<td><b>Parameters</b></td>
<td>

- `sender: &DeviceId` - 发送方设备 ID
- `ciphertext: &[u8]` - 要解密的密文数据

</td>
</tr>
<tr>
<td><b>Returns</b></td>
<td><code>Result&lt;Vec&lt;u8&gt;, Error&gt;</code> - 解密后的明文</td>
</tr>
<tr>
<td><b>Errors</b></td>
<td>

- `Error::SessionNotFound` - 与发送方的会话不存在
- `Error::DecryptionFailed` - 解密或认证失败
- `Error::InvalidCiphertext` - 密文格式错误

</td>
</tr>
</table>

**Example:**

```rust
use xlink::crypto::engine::CryptoEngine;

let crypto = CryptoEngine::new();
let sender: DeviceId = "peer-device".to_string().try_into()?;

let plaintext = crypto.decrypt_message(&sender, &ciphertext)?;
```

---

#### `CryptoEngine::establish_session()`

与远程设备建立加密会话(执行 X25519 密钥交换)。

<table>
<tr>
<td width="30%"><b>Signature</b></td>
<td width="70%">

```rust
pub async fn establish_session(
    &self,
    peer_device_id: &DeviceId,
    peer_public_key: PublicKey,
    peer_verifying_key: Option<VerifyingKey>
) -> Result<(), Error>
```

</td>
</tr>
<tr>
<td><b>Parameters</b></td>
<td>

- `peer_device_id: &DeviceId` - 远程设备 ID
- `peer_public_key: PublicKey` - 远程设备的 X25519 公钥
- `peer_verifying_key: Option<VerifyingKey>` - 远程设备的 Ed25519 验证公钥(可选)

</td>
</tr>
<tr>
<td><b>Returns</b></td>
<td><code>Result&lt;(), Error&gt;</code></td>
</tr>
<tr>
<td><b>Errors</b></td>
<td>

- `Error::SessionAlreadyExists` - 会话已存在
- `Error::InvalidPeerKey` - 无效的公钥

</td>
</tr>
</table>

**Example:**

```rust
use xlink::crypto::engine::CryptoEngine;

let crypto = CryptoEngine::new();
let peer_device_id: DeviceId = "peer-device".to_string().try_into()?;
let peer_public_key = other_crypto.public_key();

crypto.establish_session(&peer_device_id, peer_public_key, None).await?;
```

<details>
<summary><b>📝 Security Notes</b></summary>

- 密钥交换使用 X25519 DH 算法
- 可选 Ed25519 签名验证防止中间人攻击
- 建立的会话使用 Double Ratchet 算法进行前向保密

</details>

---

### Key Management

<div align="center">

#### 🔑 Session Key Management

</div>

xlink 的密钥管理集成在 `CryptoEngine` 中,自动处理密钥的生命周期。

---

#### 会话状态管理

`CryptoEngine` 自动管理与其他设备的会话密钥:

<table>
<tr>
<td width="30%"><b>方法</b></td>
<td width="70%"><b>描述</b></td>
</tr>
<tr>
<td><code>establish_session()</code></td>
<td>建立与远程设备的安全会话,执行 X25519 密钥交换</td>
</tr>
<tr>
<td><code>encrypt_message()</code></td>
<td>加密消息时自动更新发送链密钥</td>
</tr>
<tr>
<td><code>decrypt_message()</code></td>
<td>解密消息时自动更新接收链密钥</td>
</tr>
<tr>
<td><code>clear_sessions()</code></td>
<td>清除所有会话状态</td>
</tr>
</table>

**Example: 完整的端到端加密流程**

```rust
use xlink::XLink;
use xlink::core::types::{DeviceCapabilities, DeviceId};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let device_id = DeviceId::new();
    let capabilities = DeviceCapabilities {
        device_id,
        device_type: DeviceType::Smartphone,
        device_name: "device-001",
        supported_channels: HashSet::from([ChannelType::Lan]),
        battery_level: Some(80),
        is_charging: false,
        data_cost_sensitive: false,
    };

    let sdk = XLink::new(capabilities, vec![]).await?;

    // 1. 启动 SDK
    sdk.start().await?;

    // 2. 建立与远程设备的加密会话
    let peer_id: DeviceId = "peer-device".to_string().parse()?;
    let peer_public_key = /* 从远程设备获取 */;

    sdk.crypto().establish_session(&peer_id, peer_public_key, None).await?;

    // 3. 发送加密消息(SDK 自动加密)
    let payload = /* 消息载荷 */;
    sdk.send(peer_id, payload).await?;

    // 4. 停止 SDK
    sdk.stop().await;

    Ok(())
}
```

<details>
<summary><b>📝 Key Management Features</b></summary>

- **前向保密**:使用 Double Ratchet 算法,每次通信后更新密钥
- **密钥派生**:使用 HKDF-SHA256 从共享秘密派生会话密钥
- **自动重同步**:处理密钥状态同步问题

</details>

---

## Algorithms

<div align="center">

#### 🔐 Supported Cryptographic Algorithms

</div>

xlink 使用现代密码学算法确保通信安全。

### 密钥交换算法

<table>
<tr>
<th>算法</th>
<th>类型</th>
<th>安全级别</th>
<th>描述</th>
</tr>
<tr>
<td><b>X25519</b></td>
<td>椭圆曲线 DH</td>
<td>🟢 高</td>
<td>现代推荐的密钥交换算法,256 位安全级别</td>
</tr>
</table>

### 消息加密算法

<table>
<tr>
<th>算法</th>
<th>类型</th>
<th>安全级别</th>
<th>性能</th>
<th>描述</th>
</tr>
<tr>
<td><b>ChaCha20-Poly1305</b></td>
<td>AEAD</td>
<td>🟢 高</td>
<td>⚡⚡⚡ 快速</td>
<td>现代认证加密,适用于所有平台</td>
</tr>
</table>

### 数字签名算法

<table>
<tr>
<th>算法</th>
<th>密钥大小</th>
<th>安全级别</th>
<th>签名大小</th>
<th>描述</th>
</tr>
<tr>
<td><b>Ed25519</b></td>
<td>256-bit</td>
<td>🟢 高</td>
<td>64 bytes</td>
<td>现代高效的数字签名算法</td>
</tr>
</table>

### 密钥派生算法

<table>
<tr>
<th>算法</th>
<th>类型</th>
<th>安全级别</th>
<th>描述</th>
</tr>
<tr>
<td><b>HKDF-SHA256</b></td>
<td>密钥派生</td>
<td>🟢 高</td>
<td>从共享秘密派生会话密钥</td>
</tr>
</table>

---

## Error Handling

<div align="center">

#### 🚨 Error Types and Handling

</div>

### `xlinkError` Enum

xlink uses descriptive factory methods for error creation:

```rust
use xlink::core::error::xlinkError;

// Factory methods for common errors:
xlinkError::channel_disconnected(channel_type, reason, location)
xlinkError::encryption_failed(algorithm, reason, location)
xlinkError::group_not_found(group_id, location)
xlinkError::key_derivation_failed(algorithm, reason, location)
xlinkError::resource_exhausted(resource, current, limit, location)
xlinkError::serialization_failed(operation, reason, location)
xlinkError::stream_init_failed(operation, reason, location)
xlinkError::stream_disconnected(stream_id, reason, location)
xlinkError::device_not_found(device_id, location)
xlinkError::invalid_input(field, reason, location)
```

### Error Code Format

Errors use a module-sequence format (XX-YYYY):
- **01xx**: System errors
- **02xx**: Channel errors
- **03xx**: Cryptographic errors
- **04xx**: Group errors
- **05xx**: Device errors
- **06xx**: Stream errors
- **07xx**: Storage errors
- **08xx**: Protocol errors
- **09xx**: Capability errors

### Error Handling Pattern

<table>
<tr>
<td width="50%">

**Pattern Matching**
```rust
match operation() {
    Ok(result) => {
        println!("Success: {:?}", result);
    }
    Err(Error::KeyNotFound) => {
        eprintln!("Key not found");
    }
    Err(Error::EncryptionFailed) => {
        eprintln!("Encryption failed");
    }
    Err(e) => {
        eprintln!("Error: {:?}", e);
    }
}
```

</td>
<td width="50%">

**? Operator**
```rust
fn process_data() -> Result<(), Error> {
    init()?;
    
    let km = KeyManager::new()?;
    let key = km.generate_key(
        Algorithm::AES256GCM
    )?;
    
    let cipher = Cipher::new(
        Algorithm::AES256GCM
    )?;
    
    Ok(())
}
```

</td>
</tr>
</table>

---

## Type Definitions

### Common Types

<table>
<tr>
<td width="50%">

**Key ID**
```rust
pub type KeyId = String;
```

**Algorithm Type**
```rust
pub enum Algorithm { /* ... */ }
```

</td>
<td width="50%">

**Result Type**
```rust
pub type Result<T> = 
    std::result::Result<T, Error>;
```

**Log Level**
```rust
pub enum LogLevel {
    Debug,
    Info,
    Warn,
    Error,
}
```

</td>
</tr>
</table>

---

## Examples

<div align="center">

### 💡 Common Usage Patterns

</div>

### Example 1: Basic Usage

```rust
use xlink::XLink;
use xlink::core::types::{DeviceCapabilities, DeviceId, MessagePayload, ChannelType};
use std::collections::HashSet;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let device_id = DeviceId::new();
    let capabilities = DeviceCapabilities {
        device_id,
        device_type: xlink::core::types::DeviceType::Smartphone,
        device_name: "My Device".to_string(),
        supported_channels: HashSet::from([ChannelType::Lan]),
        battery_level: Some(80),
        is_charging: false,
        data_cost_sensitive: false,
    };

    let sdk = XLink::new(capabilities, vec![]).await?;
    sdk.start().await?;

    let payload = MessagePayload::Text("Hello, World!".to_string());
    sdk.send(recipient_id, payload).await?;
    sdk.stop().await;

    println!("✅ Message sent successfully!");
    Ok(())
}
```

### Example 2: Group Messaging

```rust
use xlink::XLink;
use xlink::core::types::{DeviceCapabilities, DeviceId, MessagePayload, ChannelType};
use std::collections::HashSet;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let device_id = DeviceId::new();
    let capabilities = DeviceCapabilities {
        device_id,
        device_type: xlink::core::types::DeviceType::Smartphone,
        device_name: "My Device".to_string(),
        supported_channels: HashSet::from([ChannelType::Lan]),
        battery_level: Some(80),
        is_charging: false,
        data_cost_sensitive: false,
    };

    let sdk = XLink::new(capabilities, vec![]).await?;
    sdk.start().await?;

    // Create a group with multiple members
    let group_id = sdk.create_group(
        "Team Alpha".to_string(),
        vec![member_1_id, member_2_id, member_3_id]
    ).await?;

    // Broadcast to group
    let payload = MessagePayload::Text("Hello everyone!".to_string());
    sdk.send_to_group(group_id, payload).await?;
    sdk.stop().await;

    println!("✅ Group message sent!");
    Ok(())
}
```

### Example 3: E2E Encrypted Communication

```rust
use xlink::XLink;
use xlink::core::types::{DeviceCapabilities, DeviceId, ChannelType};
use std::collections::HashSet;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let device_id = DeviceId::new();
    let capabilities = DeviceCapabilities {
        device_id,
        device_type: xlink::core::types::DeviceType::Smartphone,
        device_name: "My Device".to_string(),
        supported_channels: HashSet::from([ChannelType::Lan]),
        battery_level: Some(80),
        is_charging: false,
        data_cost_sensitive: false,
    };

    let sdk = XLink::new(capabilities, vec![]).await?;
    sdk.start().await?;

    // Register peer's public key for encrypted group communication
    let peer_id: DeviceId = "550e8400-e29b-41d4-a716-446655440000".parse()?;
    sdk.register_device_key(peer_id, peer_public_key)?;

    // Create an encrypted group
    let group_id = sdk.create_group(
        "Secure Group".to_string(),
        vec![peer_id]
    ).await?;

    println!("✅ Secure session established!");
    Ok(())
}
```

---

<div align="center">

**[📖 User Guide](USER_GUIDE.md)** • **[🏗️ Architecture](ARCHITECTURE.md)** • **[🏠 Home](../README.md)**

Made with ❤️ by the Documentation Team

[⬆ Back to Top](#-api-reference)

</div>