Skip to main content

kdeconnect_proto/packet/
pair.rs

1//! Define the [`PairPacket`] which is used during pairing of two devices.
2use serde::{Deserialize, Serialize};
3
4use crate::packet::{NetworkPacket, NetworkPacketBody};
5
6/// The KDE Connect pair packet is used to negotiate pairing between devices.
7///
8/// <https://invent.kde.org/network/kdeconnect-meta/blob/master/protocol.md#kdeconnectpair>
9#[derive(Serialize, Deserialize, Debug, Clone)]
10#[serde(rename_all = "camelCase")]
11pub struct PairPacket {
12    /// If true a pairing request is being accepted or a new one started. If false a pairing request
13    /// is being rejected or a paired device is unpairing.
14    pub pair: bool,
15
16    /// Required if this is a pairing request. The current time in seconds since epoch. Used in the
17    /// calculation of the pair verification code since protocol version 8.
18    #[serde(deserialize_with = "super::deserialize_number_or_string_in_option")]
19    #[serde(skip_serializing_if = "Option::is_none")]
20    #[serde(default)]
21    pub timestamp: Option<u64>,
22}
23
24impl NetworkPacket {
25    /// Make a [`NetworkPacket`] with a [`PairPacket`] payload ready to make a pair request.
26    pub fn pair_request(timestamp: u64) -> Self {
27        NetworkPacket::new(NetworkPacketBody::Pair(PairPacket {
28            pair: true,
29            timestamp: Some(timestamp),
30        }))
31    }
32
33    /// Make a [`NetworkPacket`] with a [`PairPacket`] payload ready to make a pair response.
34    pub fn pair_response() -> Self {
35        NetworkPacket::new(NetworkPacketBody::Pair(PairPacket {
36            pair: true,
37            timestamp: None,
38        }))
39    }
40
41    /// Make a [`NetworkPacket`] with a [`PairPacket`] payload ready to make an unpair request.
42    pub fn unpair_request() -> Self {
43        NetworkPacket::new(NetworkPacketBody::Pair(PairPacket {
44            pair: false,
45            timestamp: None,
46        }))
47    }
48
49    /// Make a [`NetworkPacket`] with a [`PairPacket`] payload ready to make an unpair responsee.
50    pub fn unpair_response() -> Self {
51        NetworkPacket::new(NetworkPacketBody::Pair(PairPacket {
52            pair: false,
53            timestamp: None,
54        }))
55    }
56}