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
use super::{
tx_ack, write_preamble, CodingRate, DataRate, Error as PktError, Identifier, MacAddress,
Modulation, SerializablePacket, StringOrNum,
};
use serde::{Deserialize, Serialize};
use std::io::{Cursor, Write};
#[derive(Debug, Clone)]
pub struct Packet {
pub random_token: u16,
pub data: Data,
}
impl Packet {
pub fn into_ack_for_gateway(self, gateway_mac: MacAddress) -> tx_ack::Packet {
tx_ack::Packet {
gateway_mac,
random_token: self.random_token,
data: tx_ack::TxPkNack::default(),
}
}
pub fn into_nack_with_error_for_gateway(
self,
error: super::tx_ack::Error,
gateway_mac: MacAddress,
) -> tx_ack::Packet {
tx_ack::Packet {
gateway_mac,
random_token: self.random_token,
data: super::tx_ack::TxPkNack::new_with_error(error),
}
}
pub fn into_nack_with_error(self, e: super::tx_ack::Error) -> tx_ack::Packet {
self.into_nack_with_error_for_gateway(e, MacAddress::from([0; 8]))
}
pub fn into_ack(self) -> tx_ack::Packet {
self.into_ack_for_gateway(MacAddress::from([0; 8]))
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Data {
pub txpk: TxPk,
}
impl Data {
pub fn from_txpk(txpk: TxPk) -> Data {
Data { txpk }
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct TxPk {
pub imme: bool,
pub tmst: StringOrNum,
#[serde(skip_serializing_if = "Option::is_none")]
pub tmms: Option<StringOrNum>,
pub freq: f64,
pub rfch: u64,
pub powe: u64,
pub modu: Modulation,
pub datr: DataRate,
pub codr: CodingRate,
#[serde(skip_serializing_if = "Option::is_none")]
pub fdev: Option<u64>,
pub ipol: bool,
pub prea: Option<u64>,
pub size: u64,
#[serde(with = "crate::packet::types::base64")]
pub data: Vec<u8>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ncrc: Option<bool>,
}
use std::fmt;
impl fmt::Display for TxPk {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
"{}, {:.2} MHz, {:?}, len: {}",
if let StringOrNum::N(time) = self.tmst {
format!("@{} us", time)
} else {
"immediately".into()
},
self.freq,
self.datr,
self.size
)
}
}
impl SerializablePacket for Packet {
fn serialize(&self, buffer: &mut [u8]) -> std::result::Result<u64, PktError> {
let mut w = Cursor::new(buffer);
write_preamble(&mut w, self.random_token)?;
w.write_all(&[Identifier::PullResp as u8])?;
w.write_all(serde_json::to_string(&self.data)?.as_bytes())?;
Ok(w.position())
}
}
impl From<Packet> for super::Packet {
fn from(packet: Packet) -> super::Packet {
super::Packet::Down(super::Down::PullResp(Box::new(packet)))
}
}
impl From<Box<Packet>> for super::Packet {
fn from(packet: Box<Packet>) -> super::Packet {
super::Packet::Down(super::Down::PullResp(packet))
}
}