peat-btle 0.3.1

Bluetooth Low Energy mesh transport for Peat Protocol
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
// Copyright (c) 2025-2026 (r)evolve - Revolve Team LLC
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Peat GATT Service Implementation
//!
//! Provides the GATT service structure and handlers for Peat Protocol BLE communication.
//!
//! Note: This module requires the `std` feature for full functionality.

use std::sync::{Arc, RwLock};

use crate::error::Result;
use crate::{HierarchyLevel, NodeId, PEAT_SERVICE_UUID};

use super::characteristics::{
    CharacteristicProperties, Command, CommandType, NodeInfo, PeatCharacteristicUuids, StatusData,
    StatusFlags, SyncDataHeader, SyncDataOp, SyncState, SyncStateData,
};

/// GATT service event handler callback type
pub type GattEventCallback = Box<dyn Fn(GattEvent) + Send + Sync>;

/// Events emitted by the GATT service
#[derive(Debug, Clone)]
pub enum GattEvent {
    /// Client connected
    ClientConnected {
        /// Client address
        address: String,
    },
    /// Client disconnected
    ClientDisconnected {
        /// Client address
        address: String,
    },
    /// Client subscribed to notifications
    NotificationSubscribed {
        /// Characteristic name
        characteristic: String,
    },
    /// Client unsubscribed from notifications
    NotificationUnsubscribed {
        /// Characteristic name
        characteristic: String,
    },
    /// Command received
    CommandReceived {
        /// Command that was received
        command: CommandType,
        /// Command payload
        payload: Vec<u8>,
    },
    /// Sync data received
    SyncDataReceived {
        /// Sync data header
        header: SyncDataHeader,
        /// Sync data payload
        payload: Vec<u8>,
    },
    /// MTU changed
    MtuChanged {
        /// New MTU value
        mtu: u16,
    },
}

/// GATT characteristic descriptor
#[derive(Debug, Clone)]
pub struct CharacteristicDescriptor {
    /// Characteristic UUID
    pub uuid: uuid::Uuid,
    /// Human-readable name
    pub name: &'static str,
    /// Properties (read, write, notify, etc.)
    pub properties: CharacteristicProperties,
    /// Whether encryption is required
    pub encrypted: bool,
}

/// Characteristic definitions for Peat GATT service
pub struct PeatCharacteristics;

impl PeatCharacteristics {
    /// Node Info characteristic descriptor
    pub fn node_info() -> CharacteristicDescriptor {
        CharacteristicDescriptor {
            uuid: PeatCharacteristicUuids::node_info(),
            name: "Node Info",
            properties: CharacteristicProperties::new(CharacteristicProperties::READ),
            encrypted: true,
        }
    }

    /// Sync State characteristic descriptor
    pub fn sync_state() -> CharacteristicDescriptor {
        CharacteristicDescriptor {
            uuid: PeatCharacteristicUuids::sync_state(),
            name: "Sync State",
            properties: CharacteristicProperties::new(
                CharacteristicProperties::READ | CharacteristicProperties::NOTIFY,
            ),
            encrypted: true,
        }
    }

    /// Sync Data characteristic descriptor
    pub fn sync_data() -> CharacteristicDescriptor {
        CharacteristicDescriptor {
            uuid: PeatCharacteristicUuids::sync_data(),
            name: "Sync Data",
            properties: CharacteristicProperties::new(
                CharacteristicProperties::WRITE | CharacteristicProperties::INDICATE,
            ),
            encrypted: true,
        }
    }

    /// Command characteristic descriptor
    pub fn command() -> CharacteristicDescriptor {
        CharacteristicDescriptor {
            uuid: PeatCharacteristicUuids::command(),
            name: "Command",
            properties: CharacteristicProperties::new(CharacteristicProperties::WRITE),
            encrypted: true,
        }
    }

    /// Status characteristic descriptor
    pub fn status() -> CharacteristicDescriptor {
        CharacteristicDescriptor {
            uuid: PeatCharacteristicUuids::status(),
            name: "Status",
            properties: CharacteristicProperties::new(
                CharacteristicProperties::READ | CharacteristicProperties::NOTIFY,
            ),
            encrypted: true,
        }
    }

    /// Get all characteristic descriptors
    pub fn all() -> Vec<CharacteristicDescriptor> {
        vec![
            Self::node_info(),
            Self::sync_state(),
            Self::sync_data(),
            Self::command(),
            Self::status(),
        ]
    }
}

/// Internal state for the GATT service
struct ServiceState {
    /// Node information
    node_info: NodeInfo,
    /// Current sync state
    sync_state: SyncStateData,
    /// Current status
    status: StatusData,
    /// Connected clients (addresses)
    connected_clients: Vec<String>,
    /// Clients subscribed to sync state notifications
    sync_state_subscribers: Vec<String>,
    /// Clients subscribed to status notifications
    status_subscribers: Vec<String>,
    /// Negotiated MTU
    mtu: u16,
}

impl ServiceState {
    fn new(node_id: NodeId, hierarchy_level: HierarchyLevel, capabilities: u16) -> Self {
        Self {
            node_info: NodeInfo::new(node_id, hierarchy_level, capabilities),
            sync_state: SyncStateData::new(SyncState::Idle),
            status: StatusData::new(),
            connected_clients: Vec::new(),
            sync_state_subscribers: Vec::new(),
            status_subscribers: Vec::new(),
            mtu: 23, // Default BLE MTU
        }
    }
}

/// Peat GATT Service
///
/// Manages the GATT service lifecycle and provides handlers for characteristic operations.
pub struct PeatGattService {
    /// Service UUID
    pub uuid: uuid::Uuid,
    /// Internal state
    state: Arc<RwLock<ServiceState>>,
    /// Event callback
    #[allow(dead_code)]
    event_callback: Option<GattEventCallback>,
}

impl PeatGattService {
    /// Create a new Peat GATT service
    pub fn new(node_id: NodeId, hierarchy_level: HierarchyLevel, capabilities: u16) -> Self {
        Self {
            uuid: PEAT_SERVICE_UUID,
            state: Arc::new(RwLock::new(ServiceState::new(
                node_id,
                hierarchy_level,
                capabilities,
            ))),
            event_callback: None,
        }
    }

    /// Set event callback
    pub fn set_event_callback(&mut self, callback: GattEventCallback) {
        self.event_callback = Some(callback);
    }

    /// Get service UUID
    pub fn service_uuid(&self) -> uuid::Uuid {
        self.uuid
    }

    /// Get all characteristic descriptors
    pub fn characteristics(&self) -> Vec<CharacteristicDescriptor> {
        PeatCharacteristics::all()
    }

    // === Read Handlers ===

    /// Handle Node Info read request
    pub fn read_node_info(&self) -> Vec<u8> {
        let state = self.state.read().unwrap();
        state.node_info.encode().to_vec()
    }

    /// Handle Sync State read request
    pub fn read_sync_state(&self) -> Vec<u8> {
        let state = self.state.read().unwrap();
        state.sync_state.encode().to_vec()
    }

    /// Handle Status read request
    pub fn read_status(&self) -> Vec<u8> {
        let state = self.state.read().unwrap();
        state.status.encode().to_vec()
    }

    // === Write Handlers ===

    /// Handle Sync Data write request
    pub fn write_sync_data(&self, data: &[u8]) -> Result<Option<Vec<u8>>> {
        let header = SyncDataHeader::decode(data).ok_or_else(|| {
            crate::error::BleError::GattError("Invalid sync data header".to_string())
        })?;

        let payload = if data.len() > SyncDataHeader::SIZE {
            data[SyncDataHeader::SIZE..].to_vec()
        } else {
            Vec::new()
        };

        // Process based on operation type
        match header.op {
            SyncDataOp::Document => {
                // Update sync state to syncing
                let mut state = self.state.write().unwrap();
                state.sync_state.state = SyncState::Syncing;
                state.status.flags =
                    StatusFlags::new(state.status.flags.flags() | StatusFlags::SYNCING);

                // Return acknowledgement
                let ack = SyncDataHeader::new(SyncDataOp::Ack, header.seq);
                Ok(Some(ack.encode().to_vec()))
            }
            SyncDataOp::Vector => {
                // Sync vector update
                let ack = SyncDataHeader::new(SyncDataOp::Ack, header.seq);
                Ok(Some(ack.encode().to_vec()))
            }
            SyncDataOp::End => {
                // Sync complete
                let mut state = self.state.write().unwrap();
                state.sync_state.state = SyncState::Complete;
                state.sync_state.progress = 100;
                state.status.flags =
                    StatusFlags::new(state.status.flags.flags() & !StatusFlags::SYNCING);

                // Emit event if callback set
                if let Some(ref callback) = self.event_callback {
                    callback(GattEvent::SyncDataReceived { header, payload });
                }

                Ok(None)
            }
            SyncDataOp::Ack => {
                // Acknowledgement received (shouldn't happen on write)
                Ok(None)
            }
        }
    }

    /// Handle Command write request
    pub fn write_command(&self, data: &[u8]) -> Result<()> {
        let command = Command::decode(data)
            .ok_or_else(|| crate::error::BleError::GattError("Invalid command data".to_string()))?;

        match command.cmd_type {
            CommandType::StartSync => {
                let mut state = self.state.write().unwrap();
                state.sync_state.state = SyncState::Syncing;
                state.sync_state.progress = 0;
            }
            CommandType::StopSync => {
                let mut state = self.state.write().unwrap();
                state.sync_state.state = SyncState::Idle;
            }
            CommandType::RefreshInfo => {
                // Trigger info refresh (no-op for now)
            }
            CommandType::SetHierarchy => {
                if !command.payload.is_empty() {
                    let mut state = self.state.write().unwrap();
                    state.node_info.hierarchy_level = HierarchyLevel::from(command.payload[0]);
                }
            }
            CommandType::Ping => {
                // Keepalive - no action needed
            }
            CommandType::Reset => {
                let mut state = self.state.write().unwrap();
                state.sync_state = SyncStateData::new(SyncState::Idle);
            }
        }

        // Emit event if callback set
        if let Some(ref callback) = self.event_callback {
            callback(GattEvent::CommandReceived {
                command: command.cmd_type,
                payload: command.payload,
            });
        }

        Ok(())
    }

    // === State Updates ===

    /// Update battery percentage
    pub fn update_battery(&self, percent: u8) {
        let mut state = self.state.write().unwrap();
        state.node_info.battery_percent = percent.min(100);

        // Update low battery flag
        if percent < 20 {
            state.status.flags =
                StatusFlags::new(state.status.flags.flags() | StatusFlags::LOW_BATTERY);
        } else {
            state.status.flags =
                StatusFlags::new(state.status.flags.flags() & !StatusFlags::LOW_BATTERY);
        }
    }

    /// Update hierarchy level
    pub fn update_hierarchy_level(&self, level: HierarchyLevel) {
        let mut state = self.state.write().unwrap();
        state.node_info.hierarchy_level = level;
    }

    /// Update sync progress
    pub fn update_sync_progress(&self, progress: u8, pending_docs: u16) {
        let mut state = self.state.write().unwrap();
        state.sync_state.progress = progress.min(100);
        state.sync_state.pending_docs = pending_docs;

        if progress >= 100 {
            state.sync_state.state = SyncState::Complete;
        }
    }

    /// Update parent connection status
    pub fn update_parent_status(&self, connected: bool, rssi: Option<i8>) {
        let mut state = self.state.write().unwrap();

        if connected {
            state.status.flags =
                StatusFlags::new(state.status.flags.flags() | StatusFlags::CONNECTED);
            state.status.parent_rssi = rssi.unwrap_or(0);
        } else {
            state.status.flags =
                StatusFlags::new(state.status.flags.flags() & !StatusFlags::CONNECTED);
            state.status.parent_rssi = 127; // No parent
        }
    }

    /// Update child count
    pub fn update_child_count(&self, count: u8) {
        let mut state = self.state.write().unwrap();
        state.status.child_count = count;
    }

    /// Update uptime
    pub fn update_uptime(&self, minutes: u16) {
        let mut state = self.state.write().unwrap();
        state.status.uptime_minutes = minutes;
    }

    // === Connection Management ===

    /// Handle client connection
    pub fn on_client_connected(&self, address: String) {
        let mut state = self.state.write().unwrap();
        if !state.connected_clients.contains(&address) {
            state.connected_clients.push(address.clone());
        }

        if let Some(ref callback) = self.event_callback {
            callback(GattEvent::ClientConnected { address });
        }
    }

    /// Handle client disconnection
    pub fn on_client_disconnected(&self, address: &str) {
        let mut state = self.state.write().unwrap();
        state.connected_clients.retain(|a| a != address);
        state.sync_state_subscribers.retain(|a| a != address);
        state.status_subscribers.retain(|a| a != address);

        if let Some(ref callback) = self.event_callback {
            callback(GattEvent::ClientDisconnected {
                address: address.to_string(),
            });
        }
    }

    /// Handle notification subscription
    pub fn on_subscribe(&self, address: String, characteristic: &str) {
        let mut state = self.state.write().unwrap();

        match characteristic {
            "sync_state" if !state.sync_state_subscribers.contains(&address) => {
                state.sync_state_subscribers.push(address);
            }
            "status" if !state.status_subscribers.contains(&address) => {
                state.status_subscribers.push(address);
            }
            _ => {}
        }

        if let Some(ref callback) = self.event_callback {
            callback(GattEvent::NotificationSubscribed {
                characteristic: characteristic.to_string(),
            });
        }
    }

    /// Handle MTU change
    pub fn on_mtu_changed(&self, mtu: u16) {
        let mut state = self.state.write().unwrap();
        state.mtu = mtu;

        if let Some(ref callback) = self.event_callback {
            callback(GattEvent::MtuChanged { mtu });
        }
    }

    /// Get current MTU
    pub fn mtu(&self) -> u16 {
        let state = self.state.read().unwrap();
        state.mtu
    }

    /// Get connected client count
    pub fn connected_client_count(&self) -> usize {
        let state = self.state.read().unwrap();
        state.connected_clients.len()
    }

    /// Get list of addresses subscribed to sync state
    pub fn sync_state_subscribers(&self) -> Vec<String> {
        let state = self.state.read().unwrap();
        state.sync_state_subscribers.clone()
    }

    /// Get list of addresses subscribed to status
    pub fn status_subscribers(&self) -> Vec<String> {
        let state = self.state.read().unwrap();
        state.status_subscribers.clone()
    }
}

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

    #[test]
    fn test_gatt_service_creation() {
        let service = PeatGattService::new(
            NodeId::new(0x12345678),
            HierarchyLevel::Squad,
            capabilities::CAN_RELAY,
        );

        assert_eq!(service.service_uuid(), PEAT_SERVICE_UUID);
        assert_eq!(service.characteristics().len(), 5);
    }

    #[test]
    fn test_read_node_info() {
        let service = PeatGattService::new(
            NodeId::new(0x12345678),
            HierarchyLevel::Squad,
            capabilities::CAN_RELAY,
        );

        let data = service.read_node_info();
        assert_eq!(data.len(), NodeInfo::ENCODED_SIZE);

        let info = NodeInfo::decode(&data).unwrap();
        assert_eq!(info.node_id, NodeId::new(0x12345678));
        assert_eq!(info.hierarchy_level, HierarchyLevel::Squad);
    }

    #[test]
    fn test_write_command() {
        let service = PeatGattService::new(NodeId::new(0x12345678), HierarchyLevel::Platform, 0);

        // Set hierarchy command
        let cmd = Command::with_payload(CommandType::SetHierarchy, vec![2]); // Platoon
        service.write_command(&cmd.encode()).unwrap();

        let data = service.read_node_info();
        let info = NodeInfo::decode(&data).unwrap();
        assert_eq!(info.hierarchy_level, HierarchyLevel::Platoon);
    }

    #[test]
    fn test_sync_data_flow() {
        let service = PeatGattService::new(NodeId::new(0x12345678), HierarchyLevel::Platform, 0);

        // Start sync
        let cmd = Command::new(CommandType::StartSync);
        service.write_command(&cmd.encode()).unwrap();

        // Check sync state
        let state_data = service.read_sync_state();
        let state = SyncStateData::decode(&state_data).unwrap();
        assert_eq!(state.state, SyncState::Syncing);

        // Send document
        let mut header = SyncDataHeader::new(SyncDataOp::Document, 1);
        let mut data = header.encode().to_vec();
        data.extend_from_slice(b"test document data");

        let response = service.write_sync_data(&data).unwrap();
        assert!(response.is_some()); // Should get ACK

        // End sync
        header = SyncDataHeader::new(SyncDataOp::End, 2);
        service.write_sync_data(&header.encode()).unwrap();

        // Check sync complete
        let state_data = service.read_sync_state();
        let state = SyncStateData::decode(&state_data).unwrap();
        assert_eq!(state.state, SyncState::Complete);
    }

    #[test]
    fn test_battery_update() {
        let service = PeatGattService::new(NodeId::new(0x12345678), HierarchyLevel::Platform, 0);

        service.update_battery(15);

        let data = service.read_node_info();
        let info = NodeInfo::decode(&data).unwrap();
        assert_eq!(info.battery_percent, 15);

        let status_data = service.read_status();
        let status = StatusData::decode(&status_data).unwrap();
        assert!(status.flags.is_low_battery());
    }

    #[test]
    fn test_client_connection() {
        let service = PeatGattService::new(NodeId::new(0x12345678), HierarchyLevel::Platform, 0);

        service.on_client_connected("AA:BB:CC:DD:EE:FF".to_string());
        assert_eq!(service.connected_client_count(), 1);

        service.on_client_disconnected("AA:BB:CC:DD:EE:FF");
        assert_eq!(service.connected_client_count(), 0);
    }

    #[test]
    fn test_mtu_negotiation() {
        let service = PeatGattService::new(NodeId::new(0x12345678), HierarchyLevel::Platform, 0);

        assert_eq!(service.mtu(), 23); // Default

        service.on_mtu_changed(251);
        assert_eq!(service.mtu(), 251);
    }

    #[test]
    fn test_peat_characteristics() {
        let chars = PeatCharacteristics::all();
        assert_eq!(chars.len(), 5);

        let node_info = PeatCharacteristics::node_info();
        assert!(node_info.properties.can_read());
        assert!(!node_info.properties.can_write());

        let sync_data = PeatCharacteristics::sync_data();
        assert!(sync_data.properties.can_write());
        assert!(sync_data.properties.can_indicate());
    }
}