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
// Copyright (c) 2021 Open Community Project Association https://ocpa.ch
// This software is published under the AGPLv3 license.

//! BLE Connection Module
//!
//! **Connect to other nodes via Bluetooth Low Energy**
//!
//! This mode is currently implemented for the following systems:
//!
//! * Android
//! * iOS

use libp2p::{floodsub::Topic, PeerId};
use prost::Message;
use state::Storage;
use std::{collections::BTreeMap, fmt, sync::RwLock};
use uuid::Uuid;

use crate::connections::ConnectionModule;
use crate::node::Node;
use crate::router::neighbours::Neighbours;
use crate::rpc::{sys::Sys, Rpc};
use crate::services::{feed, messaging};
use crate::utilities::{qaul_id::QaulId, timestamp::Timestamp};

/// Protobuf BLE system communication with BLE module
///
/// Import protobuf message definition generated by
/// the rust module prost-build.
pub mod proto {
    include!("qaul.sys.ble.rs");
}
/// Protobuf BLE network communication
pub mod proto_net {
    include!("qaul.net.ble.rs");
}
/// Protobuf BLE RPC communication
pub mod proto_rpc {
    include!("qaul.rpc.ble.rs");
}

/// Module State
static BLE: Storage<RwLock<Ble>> = Storage::new();
/// List of detected BLE nodes needing ID confirmation
static TO_CONFIRM: Storage<RwLock<BTreeMap<Vec<u8>, ToConfirm>>> = Storage::new();
/// List of discovered and available BLE nodes
///
/// This structure contains a translation table from
/// the BLE ID to the BLE ID
static NODES: Storage<RwLock<BTreeMap<Vec<u8>, BleNode>>> = Storage::new();

/// Detected BLE node, which is not known yet
/// and therefore its ID needs to be confirmed.
#[allow(dead_code)]
pub struct ToConfirm {
    // small id
    small_id: Vec<u8>,
    // detected at
    detected_at: u64,
    // status of it's detection
    status: u8,
}

/// Currently available BLE Nodes
#[derive(Clone)]
pub struct BleNode {
    // node id
    id: Vec<u8>,
    // timestamp of last detection
    #[allow(dead_code)]
    timestamp: u64,
}

/// Ble Module Status
#[allow(dead_code)]
#[derive(Debug)]
pub enum ModuleStatus {
    Uninitalized,
    InfoRequestSent,
    InfoReceived,
    StartRequestSent,
    RightsMissing,
    StartSuccess,
    StopRequestSent,
    Stopped,
}

impl fmt::Display for ModuleStatus {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{:?}", self)
    }
}

/// BLE - Bluetooth Low Energy - Connection Module
pub struct Ble {
    /// qaul BLE id
    ///
    /// 16 Byte short form of qaul node ID
    ///
    /// The BLE id is a smaller representation of the qaul
    /// node id to exchange it in a legacy BLE message with
    /// max 20 bytes
    pub ble_id: Vec<u8>,
    /// Module status
    pub status: ModuleStatus,
    /// BLE Devices
    pub devices: Vec<proto::BleDeviceInfo>,
}

impl Ble {
    /// initialize the BLE module
    pub fn init() {
        // get small BLE ID
        let ble_id = Node::get_small_id();

        // initialize local state
        {
            // create node states
            TO_CONFIRM.set(RwLock::new(BTreeMap::new()));
            NODES.set(RwLock::new(BTreeMap::new()));

            // set it to state
            let ble = Ble {
                ble_id,
                status: ModuleStatus::Uninitalized,
                devices: Vec::new(),
            };
            BLE.set(RwLock::new(ble));
        }

        //#[cfg(target_os = "android")]
        Self::info_send_request();
    }

    /// set module status
    fn status_set(status: ModuleStatus) {
        // get module state
        let mut ble = BLE.get().write().unwrap();

        // set status
        ble.status = status;
    }

    /// send info request
    fn info_send_request() {
        // create message
        let message = proto::Ble {
            message: Some(proto::ble::Message::InfoRequest(proto::BleInfoRequest {})),
        };

        // encode message
        let mut buf = Vec::with_capacity(message.encoded_len());
        message
            .encode(&mut buf)
            .expect("Vec<u8> provides capacity as needed");

        // send the message
        Sys::send_message(buf);

        // update module status
        Self::status_set(ModuleStatus::InfoRequestSent);
    }

    /// info request received
    fn info_received(message: proto::BleInfoResponse) {
        //ble.devices.extend(message.device);
        if let Some(device) = message.device {
            {
                // save to state
                let mut ble = BLE.get().write().unwrap();
                ble.devices.push(device);
            }

            // start module
            Self::module_start();
        } else {
            log::error!("No Bluetooth device available.");
        }
    }

    /// start module
    pub fn module_start() {
        let qaul_id;
        {
            let ble = BLE.get().write().unwrap();
            qaul_id = ble.ble_id.clone();
        }

        // create message
        let start_request = proto::BleStartRequest {
            qaul_id,
            power_setting: proto::BlePowerSetting::LowLatency.into(),
        };
        let message = proto::Ble {
            message: Some(proto::ble::Message::StartRequest(start_request)),
        };

        // encode message
        let mut buf = Vec::with_capacity(message.encoded_len());
        message
            .encode(&mut buf)
            .expect("Vec<u8> provides capacity as needed");

        // send the message
        Sys::send_message(buf);

        // update module status
        Self::status_set(ModuleStatus::StartRequestSent);
    }

    /// check start module result
    fn module_start_result(message: proto::BleStartResult) {
        if message.success {
            Self::status_set(ModuleStatus::StartSuccess);
        } else {
            // TODO: manage rights, etc.
            log::error!("BLE start error: {}", message.error_message);

            match message.error_reason {
                // proto::BleError::UnknownError
                0 => {
                    log::error!("BLE unknown error");
                }
                // proto::BleError::RightsMissing
                1 => {
                    log::error!("BLE rights missing");

                    // request rights from GUI
                    // create rights request message
                    let rights_request = proto_rpc::RightsRequest {};

                    // create BLE RPC message
                    let proto_message = proto_rpc::Ble {
                        message: Some(proto_rpc::ble::Message::RightsRequest(rights_request)),
                    };

                    // encode message
                    let mut buf = Vec::with_capacity(proto_message.encoded_len());
                    proto_message
                        .encode(&mut buf)
                        .expect("Vec<u8> provides capacity as needed");

                    // send message
                    Rpc::send_message(
                        buf,
                        crate::rpc::proto::Modules::Ble.into(),
                        "".to_string(),
                        Vec::new(),
                    );
                }
                // proto::BleError::Timeout
                2 => {
                    log::error!("BLE timeout error");
                }
                _ => {
                    log::error!("BLE undefined error");
                }
            }
        }
    }

    /// start module
    pub fn module_stop() {
        // create stop message
        let message = proto::BleStopRequest {};

        // encode message
        let mut buf = Vec::with_capacity(message.encoded_len());
        message
            .encode(&mut buf)
            .expect("Vec<u8> provides capacity as needed");

        // send the message
        Sys::send_message(buf);

        // update module status
        Self::status_set(ModuleStatus::StopRequestSent);

        // TODO: empty all lists: Neighbours, Nodes, to Confirm
    }

    /// check start module result
    fn module_stop_result(message: proto::BleStopResult) {
        if message.success {
            // update module status
            Self::status_set(ModuleStatus::Stopped);
        } else {
            // TODO: how to handle that?
            log::error!("BLE stop request error: {}", message.error_message);
        }
    }

    /// Add Newly Discovered Node To all Lists
    ///
    /// This function adds a newly discovered and identified
    /// node to all necessary node lists:
    ///
    /// * BLE translation table
    /// * BLE Neighbours list
    fn node_discovered(small_id: Vec<u8>, node_id: Vec<u8>) {
        // create node entry
        let node = BleNode {
            id: node_id,
            timestamp: Timestamp::get_timestamp(),
        };

        // add node to local state
        {
            // get state
            let mut nodes = NODES.get().write().unwrap();

            // add node
            nodes.insert(small_id, node.clone());
        }

        // add it to neighbours table
        match PeerId::from_bytes(&node.id) {
            Ok(node_id) => {
                Neighbours::update_node(ConnectionModule::Ble, node_id, 50);
            }
            Err(e) => {
                log::error!("{}", e);
            }
        }
    }

    /// add a node to the available nodes list
    fn node_to_confirm(small_id: Vec<u8>) {
        // create node entry
        let confirm = ToConfirm {
            small_id: small_id.clone(),
            detected_at: Timestamp::get_timestamp(),
            status: 0,
        };

        // get state
        let mut nodes = TO_CONFIRM.get().write().unwrap();

        // add node
        nodes.insert(small_id.clone(), confirm);

        // send identification message
        Self::identification_send(small_id, true);
    }

    /// find node ID from small id
    fn find_node(small_id: Vec<u8>) -> Option<Vec<u8>> {
        // get nodes table
        let nodes = NODES.get().write().unwrap();

        // search for small id
        if let Some(node) = nodes.get(&small_id) {
            return Some(node.id.clone());
        }

        None
    }

    /// find node ID from small ID
    fn get_node_id(small_id: Vec<u8>) -> Option<PeerId> {
        if let Some(id) = Self::find_node(small_id) {
            match PeerId::from_bytes(&id) {
                Ok(peer) => return Some(peer),
                Err(e) => log::error!("{}", e),
            }
        }

        None
    }

    /// a new device got discovered via bluetooth
    fn device_discovered(message: proto::BleDeviceDiscovered) {
        // check if node is known
        if let Some(node) = Neighbours::node_from_small_id(message.qaul_id.clone()) {
            // add it to translation table
            Self::node_discovered(message.qaul_id, node.id);
        } else {
            // confirm node
            Self::node_to_confirm(message.qaul_id);
        }
    }

    /// a formerly discovered device became unavailable
    fn device_unavailable(message: proto::BleDeviceUnavailable) {
        // get state
        let mut nodes = NODES.get().write().unwrap();

        // remove device from list
        if let Some((_, ble_node)) = nodes.remove_entry(&message.qaul_id) {
            // remove it from neighbours list
            match PeerId::from_bytes(&ble_node.id) {
                Ok(node_id) => {
                    Neighbours::delete(ConnectionModule::Ble, node_id);
                }
                Err(e) => {
                    log::error!("{}", e);
                }
            }
        } else {
            // remove it from TO_CONFIRM list
            let mut to_confirm = TO_CONFIRM.get().write().unwrap();
            if let None = to_confirm.remove_entry(&message.qaul_id) {
                // remove it from neighbours list
                log::error!("node to remove not found");
            }
        }
    }

    /// Identification Received
    ///
    /// Received identity information from another node
    fn identification_received(small_id: Vec<u8>, identification: proto_net::Identification) {
        // add node id
        if let Some(node) = identification.node {
            // remove node from to_confirm
            {
                let mut to_confirm = TO_CONFIRM.get().write().unwrap();
                to_confirm.remove_entry(&small_id);
            }

            // add node to discovered lists
            Self::node_discovered(small_id.clone(), node.id);

            // check if to send a response
            if identification.request {
                Self::identification_send(small_id, false);
            }
        }
    }

    /// Send Identification
    ///
    /// Send identity information to another node
    fn identification_send(_small_id: Vec<u8>, request: bool) {
        // get node ID
        let node_id = Node::get_id();

        // create identification message
        let identification = proto_net::Identification {
            request,
            node: Some(proto_net::NodeIdentification {
                id: node_id.to_bytes(),
            }),
        };

        // create unified message
        let message = proto_net::ble_message::Message::Identification(identification);

        Self::create_send_message(QaulId::to_small(node_id), message);
    }

    /// send message
    ///
    /// * receiver_id: the small qaul id of the receiving node
    /// * sender_id: the small qaul id of the sending node (this node)
    /// * data: the binary data of the message to send
    pub fn message_send(receiver_id: Vec<u8>, sender_id: Vec<u8>, data: Vec<u8>) {
        // create a random UUID as message id
        let message_id = Uuid::new_v4().as_bytes().to_vec();

        // create direct message
        let direct_message = proto::BleDirectSend {
            message_id,
            receiver_id,
            sender_id,
            data,
        };

        // create message
        let message = proto::Ble {
            message: Some(proto::ble::Message::DirectSend(direct_message)),
        };

        // encode message
        let mut buf = Vec::with_capacity(message.encoded_len());
        message
            .encode(&mut buf)
            .expect("Vec<u8> provides capacity as needed");

        // send the message
        Sys::send_message(buf);
    }

    /// result of message sending
    fn message_send_result(result: proto::BleDirectSendResult) {
        if result.success {
        } else {
            log::error!("error sending BLE message");
        }
    }

    /// send routing info message
    pub fn send_routing_info(node_id: PeerId, data: Vec<u8>) {
        let message = proto_net::ble_message::Message::Info(data);

        Self::create_send_message(QaulId::to_small(node_id), message);
    }

    /// send messaging message
    pub fn send_messaging_message(node_id: PeerId, data: Vec<u8>) {
        let message = proto_net::ble_message::Message::Messaging(data);

        Self::create_send_message(QaulId::to_small(node_id), message);
    }

    /// send feed message
    pub fn send_feed_message(_topic: Topic, data: Vec<u8>) {
        // find all nodes, that are only connected through BLE
        let nodes = Neighbours::get_ble_only_nodes();

        // create BLE message
        let message = proto_net::ble_message::Message::Feed(data);

        // send it nodes
        for node_id in nodes {
            Self::create_send_message(QaulId::to_small(node_id), message.clone());
        }
    }

    /// create the message
    fn create_send_message(small_id: Vec<u8>, message: proto_net::ble_message::Message) {
        // get small qaul id of this node
        let sender_id = Node::get_small_id();

        // create message
        let proto_message = proto_net::BleMessage {
            message: Some(message),
        };

        // encode message
        let mut buf = Vec::with_capacity(proto_message.encoded_len());
        proto_message
            .encode(&mut buf)
            .expect("Vec<u8> provides capacity as needed");

        // send the message
        Self::message_send(small_id, sender_id, buf);
    }

    /// BLE message received
    fn message_received(message: proto::BleDirectReceived) {
        // get node ID of sender
        let node_id: PeerId;
        if let Some(id) = Self::get_node_id(message.from.clone()) {
            node_id = id;
        } else {
            // TODO: find a better solution in the future
            //
            // if we don't know the ID of the peer yet,
            // put in our peer ID
            node_id = Node::get_id();
        }

        // decode and distribute messages
        match proto_net::BleMessage::decode(&message.data[..]) {
            Ok(ble_message) => match ble_message.message {
                Some(proto_net::ble_message::Message::Info(data)) => {
                    let received = qaul_info::QaulInfoReceived {
                        received_from: node_id,
                        data,
                    };
                    crate::router::info::RouterInfo::received(received);
                }
                Some(proto_net::ble_message::Message::Feed(data)) => {
                    match feed::proto_net::FeedContainer::decode(&data[..]) {
                        Ok(feed_container) => {
                            feed::Feed::received(ConnectionModule::Ble, node_id, feed_container);
                        }
                        Err(e) => {
                            log::error!("BleMessage feed decoding error: {}", e);
                        }
                    }
                }
                Some(proto_net::ble_message::Message::Messaging(data)) => {
                    let received = qaul_messaging::QaulMessagingReceived {
                        received_from: node_id,
                        data,
                    };
                    messaging::Messaging::received(received);
                }
                Some(proto_net::ble_message::Message::Identification(identification)) => {
                    Self::identification_received(message.from, identification);
                }
                _ => {
                    log::error!("unprocessable BleMessage");
                }
            },
            Err(e) => {
                log::error!("Protobuf BleMessage decoding error: {}", e);
            }
        }
    }

    /// receive sys messages from BLE module
    pub fn sys_received(data: Vec<u8>) {
        match proto::Ble::decode(&data[..]) {
            Ok(ble) => match ble.message {
                Some(proto::ble::Message::InfoResponse(info_response)) => {
                    Self::info_received(info_response);
                }
                Some(proto::ble::Message::StartResult(start_result)) => {
                    Self::module_start_result(start_result);
                }
                Some(proto::ble::Message::StopResult(stop_result)) => {
                    Self::module_stop_result(stop_result);
                }
                Some(proto::ble::Message::DeviceDiscovered(device)) => {
                    Self::device_discovered(device);
                }
                Some(proto::ble::Message::DeviceUnavailable(device)) => {
                    Self::device_unavailable(device);
                }
                Some(proto::ble::Message::DirectReceived(direct_received)) => {
                    Self::message_received(direct_received);
                }
                Some(proto::ble::Message::DirectSendResult(direct_send_result)) => {
                    Self::message_send_result(direct_send_result);
                }
                _ => {
                    log::error!("Unprocessable BLE Sys message received");
                }
            },
            Err(error) => {
                log::error!("{:?}", error);
            }
        }
    }

    /// Process incoming RPC request messages for BLE module
    pub fn rpc(data: Vec<u8>) {
        log::trace!("BLE rpc message received");

        match proto_rpc::Ble::decode(&data[..]) {
            Ok(ble) => {
                match ble.message {
                    Some(proto_rpc::ble::Message::InfoRequest(_)) => {
                        // get module state
                        let ble = BLE.get().read().unwrap();

                        // create binary device info message
                        let mut device_info: Vec<u8> = Vec::new();
                        if let Some(device) = ble.devices.first() {
                            device_info = Vec::with_capacity(device.encoded_len());
                            device
                                .encode(&mut device_info)
                                .expect("Vec<u8> provides capacity as needed");
                        }

                        // create discovered response message
                        let info = proto_rpc::InfoResponse {
                            small_id: ble.ble_id.clone(),
                            status: ble.status.to_string(),
                            device_info,
                        };

                        // create BLE RPC message
                        let proto_message = proto_rpc::Ble {
                            message: Some(proto_rpc::ble::Message::InfoResponse(info)),
                        };

                        // encode message
                        let mut buf = Vec::with_capacity(proto_message.encoded_len());
                        proto_message
                            .encode(&mut buf)
                            .expect("Vec<u8> provides capacity as needed");

                        // send message
                        Rpc::send_message(
                            buf,
                            crate::rpc::proto::Modules::Ble.into(),
                            "".to_string(),
                            Vec::new(),
                        );
                    }
                    Some(proto_rpc::ble::Message::StartRequest(_)) => {
                        // start BLE module
                        Self::module_start();
                    }
                    Some(proto_rpc::ble::Message::StopRequest(_)) => {
                        // stop BLE module
                        Self::module_stop();
                    }
                    Some(proto_rpc::ble::Message::DiscoveredRequest(_)) => {
                        // get nodes state
                        let nodes = NODES.get().read().unwrap();
                        // get to confirm state
                        let to_confirm = TO_CONFIRM.get().read().unwrap();

                        // create discovered response message
                        let discovered = proto_rpc::DiscoveredResponse {
                            nodes_count: nodes.len() as u32,
                            to_confirm_count: to_confirm.len() as u32,
                        };

                        // create BLE RPC message
                        let proto_message = proto_rpc::Ble {
                            message: Some(proto_rpc::ble::Message::DiscoveredResponse(discovered)),
                        };

                        // encode message
                        let mut buf = Vec::with_capacity(proto_message.encoded_len());
                        proto_message
                            .encode(&mut buf)
                            .expect("Vec<u8> provides capacity as needed");

                        // send message
                        Rpc::send_message(
                            buf,
                            crate::rpc::proto::Modules::Ble.into(),
                            "".to_string(),
                            Vec::new(),
                        );
                    }
                    Some(proto_rpc::ble::Message::RightsResult(rights_result)) => {
                        if rights_result.rights_granted {
                            Self::module_start();
                        } else {
                            log::error!("BLE rights not granted");
                        }
                    }
                    _ => {
                        log::error!("BLE rpc message undefined");
                    }
                }
            }
            Err(error) => {
                log::error!("{:?}", error);
            }
        }
    }
}