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
use util::{slice_u16, slice_u24, slice_u32};
pub struct ServiceOptions(u8);
impl ServiceOptions {
pub fn new(opts: u8) -> ServiceOptions { ServiceOptions(opts) }
pub fn emergency(&self) -> bool { self.0 >> 7 == 1 }
pub fn protected(&self) -> bool { self.0 >> 6 & 1 == 1 }
pub fn full_duplex(&self) -> bool { self.0 >> 5 & 1 == 1 }
pub fn packet_switched(&self) -> bool { self.0 >> 4 & 1 == 1 }
pub fn prio(&self) -> u8 { self.0 & 0x7 }
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Channel(u16);
impl Channel {
pub fn new(bytes: &[u8]) -> Channel { Channel(slice_u16(bytes)) }
pub fn id(&self) -> u8 { (self.0 >> 12) as u8 }
pub fn number(&self) -> u16 { self.0 & 0xFFF }
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "ser", derive(Serialize))]
pub enum TalkGroup {
Nobody,
Default,
Everbody,
Other(u16),
}
impl TalkGroup {
pub fn new(bytes: &[u8]) -> TalkGroup {
Self::from_bits(slice_u16(bytes))
}
pub fn from_bits(bits: u16) -> TalkGroup {
use self::TalkGroup::*;
match bits {
0x0000 => Nobody,
0x0001 => Default,
0xFFFF => Everbody,
_ => Other(bits),
}
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct SystemServices(u8);
impl SystemServices {
pub fn new(ssc: u8) -> Self { SystemServices(ssc) }
pub fn is_composite(&self) -> bool { self.0 & 0x01 != 0 }
pub fn updates_only(&self) -> bool { self.0 & 0x02 != 0 }
pub fn is_backup(&self) -> bool { self.0 & 0x04 != 0 }
pub fn has_data(&self) -> bool { self.0 & 0x10 != 0 }
pub fn has_voice(&self) -> bool { self.0 & 0x20 != 0 }
pub fn has_registration(&self) -> bool { self.0 & 0x40 != 0 }
pub fn has_auth(&self) -> bool { self.0 & 0x80 != 0 }
}
#[derive(Default)]
pub struct ChannelParamsMap([Option<ChannelParams>; 16]);
impl ChannelParamsMap {
pub fn update(&mut self, upd: &ChannelParamsUpdate) {
self.0[upd.id() as usize] = Some(upd.params());
}
pub fn lookup(&self, id: u8) -> Option<ChannelParams> {
self.0[id as usize]
}
}
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct ChannelParams {
base: u32,
spacing: u32,
offset: i32,
pub bandwidth: u32,
}
impl ChannelParams {
pub fn new(base: u32, bandwidth: u16, offset: u16, spacing: u16) -> ChannelParams {
let off = (offset as i32 & 0xFF) * 250_000;
ChannelParams {
base: base * 5,
spacing: spacing as u32 * 125,
offset: if offset >> 8 == 0 { -off } else { off },
bandwidth: bandwidth as u32 * 125,
}
}
pub fn rx_freq(&self, ch: u16) -> u32 {
self.base + self.spacing * ch as u32
}
pub fn tx_freq(&self, ch: u16) -> u32 {
self.rx_freq(ch) + self.offset as u32
}
}
pub struct SiteOptions(u8);
impl SiteOptions {
pub fn new(opts: u8) -> SiteOptions {
assert!(opts >> 4 == 0);
SiteOptions(opts)
}
pub fn conventional(&self) -> bool { self.0 & 0b1000 != 0 }
pub fn failing(&self) -> bool { self.0 & 0b100 != 0 }
pub fn current(&self) -> bool { self.0 & 0b10 != 0 }
pub fn networked(&self) -> bool { self.0 & 1 != 0 }
}
pub struct GroupTrafficUpdate<'a>(&'a [u8]);
impl<'a> GroupTrafficUpdate<'a> {
pub fn new(payload: &'a [u8]) -> Self { GroupTrafficUpdate(payload) }
pub fn updates(&self) -> [(Channel, TalkGroup); 2] {
[
(Channel::new(&self.0[0..=1]), TalkGroup::new(&self.0[2..=3])),
(Channel::new(&self.0[4..=5]), TalkGroup::new(&self.0[6..=7])),
]
}
}
pub struct AdjacentSite<'a>(&'a [u8]);
impl<'a> AdjacentSite<'a> {
pub fn new(payload: &'a [u8]) -> Self { AdjacentSite(payload) }
pub fn area(&self) -> u8 { self.0[0] }
pub fn opts(&self) -> SiteOptions { SiteOptions::new(self.0[1] >> 4) }
pub fn system(&self) -> u16 { slice_u16(&self.0[1..=2]) & 0xFFF }
pub fn rfss(&self) -> u8 { self.0[3] }
pub fn site(&self) -> u8 { self.0[4] }
pub fn channel(&self) -> Channel { Channel::new(&self.0[5..=6]) }
pub fn services(&self) -> SystemServices { SystemServices::new(self.0[7]) }
}
pub struct ChannelParamsUpdate<'a>(&'a [u8]);
impl<'a> ChannelParamsUpdate<'a> {
pub fn new(payload: &'a [u8]) -> Self { ChannelParamsUpdate(payload) }
pub fn id(&self) -> u8 { self.0[0] >> 4 }
pub fn params(&self) -> ChannelParams {
ChannelParams::new(self.base(), self.bandwidth(), self.offset(), self.spacing())
}
fn bandwidth(&self) -> u16 {
(self.0[0] as u16 & 0xF) << 5 | (self.0[1] >> 3) as u16
}
fn offset(&self) -> u16 {
(self.0[1] as u16 & 0x7) << 6 | (self.0[2] >> 2) as u16
}
fn spacing(&self) -> u16 {
(self.0[2] as u16 & 0x3) << 8 | self.0[3] as u16
}
fn base(&self) -> u32 { slice_u32(&self.0[4..=7]) }
}
pub struct AltControlChannel<'a>(&'a [u8]);
impl<'a> AltControlChannel<'a> {
pub fn new(payload: &'a [u8]) -> Self { AltControlChannel(payload) }
pub fn rfss(&self) -> u8 { self.0[0] }
pub fn site(&self) -> u8 { self.0[1] }
pub fn alts(&self) -> [(Channel, SystemServices); 2] {
[
(Channel::new(&self.0[2..=3]), SystemServices::new(self.0[4])),
(Channel::new(&self.0[5..=6]), SystemServices::new(self.0[7])),
]
}
}
pub struct RfssStatusBroadcast<'a>(&'a [u8]);
impl<'a> RfssStatusBroadcast<'a> {
pub fn new(payload: &'a [u8]) -> Self { RfssStatusBroadcast(payload) }
pub fn area(&self) -> u8 { self.0[0] }
pub fn networked(&self) -> bool { self.0[1] & 0b10000 != 0 }
pub fn system(&self) -> u16 { slice_u16(&self.0[1..=2]) & 0xFFF }
pub fn rfss(&self) -> u8 { self.0[3] }
pub fn site(&self) -> u8 { self.0[4] }
pub fn channel(&self) -> Channel { Channel::new(&self.0[5..=6]) }
pub fn services(&self) -> SystemServices { SystemServices::new(self.0[7]) }
}
pub struct NetworkStatusBroadcast<'a>(&'a [u8]);
impl<'a> NetworkStatusBroadcast<'a> {
pub fn new(payload: &'a [u8]) -> Self { NetworkStatusBroadcast(payload) }
pub fn area(&self) -> u8 { self.0[0] }
pub fn wacn(&self) -> u32 { slice_u24(&self.0[1..=3]) >> 4 }
pub fn system(&self) -> u16 { slice_u16(&self.0[3..=4]) & 0xFFF }
pub fn channel(&self) -> Channel { Channel::new(&self.0[5..=6]) }
pub fn services(&self) -> SystemServices { SystemServices::new(self.0[7]) }
}
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
#[cfg_attr(feature = "ser", derive(Serialize))]
pub enum RegResponse {
Accept,
Fail,
Deny,
Refuse,
}
impl RegResponse {
pub fn from_bits(bits: u8) -> RegResponse {
use self::RegResponse::*;
assert!(bits >> 2 == 0);
match bits {
0b00 => Accept,
0b01 => Fail,
0b10 => Deny,
0b11 => Refuse,
_ => unreachable!(),
}
}
}
pub struct UnitCallAlert<'a>(&'a [u8]);
impl<'a> UnitCallAlert<'a> {
pub fn new(payload: &'a [u8]) -> Self { UnitCallAlert(payload) }
pub fn dest_unit(&self) -> u32 { slice_u24(&self.0[2..=4]) }
pub fn src_unit(&self) -> u32 { slice_u24(&self.0[5..=7]) }
}
pub struct UnitCallRequest<'a>(&'a [u8]);
impl<'a> UnitCallRequest<'a> {
pub fn new(payload: &'a [u8]) -> Self { UnitCallRequest(payload) }
pub fn opts(&self) -> ServiceOptions { ServiceOptions::new(self.0[0]) }
pub fn dest_unit(&self) -> u32 { slice_u24(&self.0[2..=4]) }
pub fn src_unit(&self) -> u32 { slice_u24(&self.0[5..=7]) }
}
pub struct PhoneAlert<'a>(&'a [u8]);
impl<'a> PhoneAlert<'a> {
pub fn new(payload: &'a [u8]) -> Self { PhoneAlert(payload) }
pub fn digits(&self) -> &[u8] { &self.0[0..=4] }
pub fn dest_unit(&self) -> u32 { slice_u24(&self.0[5..=7]) }
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_channel_params() {
let p = ChannelParams::new(170201250, 0x64, 0b010110100, 0x32);
assert_eq!(p.base, 851_006_250);
assert_eq!(p.spacing, 6_250);
assert_eq!(p.offset, -45_000_000);
assert_eq!(p.bandwidth, 12_500);
assert_eq!(p.rx_freq(0b1001), 851_062_500);
}
#[test]
fn test_group_traffic_updates() {
let buf = [
0b10001000,
0b01110111,
0b11111111,
0b11111111,
0b10010001,
0b00000001,
0b10101010,
0b10101010,
];
let u = GroupTrafficUpdate(&buf[..]).updates();
assert_eq!(u[0].0.id(), 0b1000);
assert_eq!(u[0].0.number(), 0b100001110111);
assert_eq!(u[0].1, TalkGroup::Everbody);
assert_eq!(u[1].0.id(), 0b1001);
assert_eq!(u[1].0.number(), 0b000100000001);
assert_eq!(u[1].1, TalkGroup::Other(0b1010101010101010));
}
}