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
use std;
use collect_slice::CollectSlice;
use bits::{Hexbit, HexbitBytes, Dibit};
use coding::{cyclic, hamming, reed_solomon};
use error::{P25Error, Result};
use stats::{Stats, HasStats};
use voice::frame::VoiceFrame;
use voice::{control, crypto};
use buffer::{
Buffer,
VoiceDataFragStorage,
VoiceExtraStorage,
VoiceExtraWordStorage,
VoiceFrameStorage,
};
use consts::{
CRYPTO_CONTROL_BYTES,
EXTRA_HEXBITS,
EXTRA_PIECE_DIBITS,
LINK_CONTROL_BYTES,
};
use error::P25Error::*;
use self::State::*;
use self::StateChange::*;
pub type VoiceLCFrameGroupReceiver = FrameGroupReceiver<LinkControlExtra>;
pub type VoiceCCFrameGroupReceiver = FrameGroupReceiver<CryptoControlExtra>;
enum State {
DecodeVoiceFrame(VoiceFrameReceiver),
DecodeExtra,
DecodeDataFragment(DataFragmentReceiver),
Done,
}
impl State {
pub fn decode_voice_frame() -> State {
DecodeVoiceFrame(VoiceFrameReceiver::new())
}
pub fn decode_data_frag() -> State {
DecodeDataFragment(DataFragmentReceiver::new())
}
}
enum StateChange<E: Extra> {
NoChange,
Change(State),
EventChange(FrameGroupEvent<E>, State),
Error(P25Error),
}
pub enum FrameGroupEvent<E: Extra> {
VoiceFrame(VoiceFrame),
Extra(E::Fields),
DataFragment(u32),
}
pub struct FrameGroupReceiver<E: Extra> {
state: State,
extra: ExtraReceiver<E>,
frame: usize,
stats: Stats,
}
impl<E: Extra> FrameGroupReceiver<E> {
pub fn new() -> FrameGroupReceiver<E> {
FrameGroupReceiver {
state: State::decode_voice_frame(),
extra: ExtraReceiver::new(),
frame: 0,
stats: Stats::default(),
}
}
pub fn done(&self) -> bool {
if let Done = self.state { true } else { false }
}
fn handle(&mut self, dibit: Dibit) -> StateChange<E> {
let next = match self.state {
DecodeVoiceFrame(ref mut decoder) => match decoder.feed(dibit) {
Some(Ok(vf)) => {
self.frame += 1;
EventChange(FrameGroupEvent::VoiceFrame(vf), match self.frame {
1 => State::decode_voice_frame(),
2...7 => DecodeExtra,
8 => State::decode_data_frag(),
9 => Done,
_ => unreachable!(),
})
},
Some(Err(e)) => Error(e),
None => NoChange,
},
DecodeExtra => match self.extra.feed(dibit) {
Some(Ok(extra)) => EventChange(FrameGroupEvent::Extra(extra),
State::decode_voice_frame()),
Some(Err(err)) => Error(err),
None => if self.extra.piece_done() {
Change(State::decode_voice_frame())
} else {
NoChange
}
},
DecodeDataFragment(ref mut dec) => match dec.feed(dibit) {
Some(Ok(data)) => EventChange(FrameGroupEvent::DataFragment(data),
State::decode_voice_frame()),
Some(Err(err)) => Error(err),
None => NoChange,
},
_ => unreachable!(),
};
match self.state {
DecodeVoiceFrame(ref mut vf) => self.stats.merge(vf),
DecodeExtra => self.stats.merge(&mut self.extra),
DecodeDataFragment(ref mut df) => self.stats.merge(df),
Done => {},
}
next
}
pub fn feed(&mut self, dibit: Dibit) -> Option<Result<FrameGroupEvent<E>>> {
match self.handle(dibit) {
EventChange(event, next) => {
self.state = next;
Some(Ok(event))
},
Change(state) => {
self.state = state;
None
},
Error(e) => Some(Err(e)),
NoChange => None,
}
}
}
impl<E: Extra> HasStats for FrameGroupReceiver<E> {
fn stats(&mut self) -> &mut Stats { &mut self.stats }
}
pub trait Extra {
type Fields;
fn decode_rs<'a>(buf: &'a mut [Hexbit; EXTRA_HEXBITS], s: &mut Stats)
-> Result<&'a [Hexbit]>;
fn decode_extra(buf: &[Hexbit]) -> Self::Fields;
}
pub struct LinkControlExtra;
impl Extra for LinkControlExtra {
type Fields = control::LinkControlFields;
fn decode_rs<'a>(buf: &'a mut [Hexbit; EXTRA_HEXBITS], s: &mut Stats)
-> Result<&'a [Hexbit]>
{
reed_solomon::short::decode(buf).map(|(data, err)| {
s.record_rs_short(err);
data
}).ok_or(RsShortUnrecoverable)
}
fn decode_extra(buf: &[Hexbit]) -> Self::Fields {
let mut bytes = [0; LINK_CONTROL_BYTES];
HexbitBytes::new(buf.iter().cloned()).collect_slice_checked(&mut bytes[..]);
control::LinkControlFields::new(bytes)
}
}
pub struct CryptoControlExtra;
impl Extra for CryptoControlExtra {
type Fields = crypto::CryptoControlFields;
fn decode_rs<'a>(buf: &'a mut [Hexbit; EXTRA_HEXBITS], s: &mut Stats)
-> Result<&'a [Hexbit]>
{
reed_solomon::medium::decode(buf).map(|(data, err)| {
s.record_rs_med(err);
data
}).ok_or(RsMediumUnrecoverable)
}
fn decode_extra(buf: &[Hexbit]) -> Self::Fields {
let mut bytes = [0; CRYPTO_CONTROL_BYTES];
HexbitBytes::new(buf.iter().cloned()).collect_slice_checked(&mut bytes[..]);
crypto::CryptoControlFields::new(bytes)
}
}
struct VoiceFrameReceiver {
dibits: Buffer<VoiceFrameStorage>,
stats: Stats,
}
impl VoiceFrameReceiver {
pub fn new() -> VoiceFrameReceiver {
VoiceFrameReceiver {
dibits: Buffer::new(VoiceFrameStorage::new()),
stats: Stats::default(),
}
}
pub fn feed(&mut self, dibit: Dibit) -> Option<Result<VoiceFrame>> {
let stats = &mut self.stats;
self.dibits.feed(dibit).map(|buf| VoiceFrame::new(buf).map(|vf| {
for idx in 0..4 {
stats.record_golay_std(vf.errors[idx]);
}
for idx in 4..7 {
stats.record_hamming_std(vf.errors[idx]);
}
vf
}))
}
}
impl HasStats for VoiceFrameReceiver {
fn stats(&mut self) -> &mut Stats { &mut self.stats }
}
struct ExtraReceiver<E: Extra> {
extra: std::marker::PhantomData<E>,
dibits: Buffer<VoiceExtraWordStorage>,
hexbits: Buffer<VoiceExtraStorage>,
dibit: usize,
stats: Stats,
}
impl<E: Extra> ExtraReceiver<E> {
pub fn new() -> ExtraReceiver<E> {
ExtraReceiver {
extra: std::marker::PhantomData,
dibits: Buffer::new(VoiceExtraWordStorage::new()),
hexbits: Buffer::new(VoiceExtraStorage::new()),
dibit: 0,
stats: Stats::default(),
}
}
pub fn piece_done(&self) -> bool { self.dibit % EXTRA_PIECE_DIBITS == 0 }
pub fn feed(&mut self, dibit: Dibit) -> Option<Result<E::Fields>> {
self.dibit += 1;
let buf = match self.dibits.feed(dibit) {
Some(buf) => *buf as u16,
None => return None,
};
let bits = match hamming::shortened::decode(buf) {
Some((data, err)) => {
self.stats.record_hamming_short(err);
data
},
None => 0,
};
let hexbits = match self.hexbits.feed(Hexbit::new(bits)) {
Some(buf) => buf,
None => return None,
};
Some(E::decode_rs(hexbits, &mut self.stats).map(|data| {
E::decode_extra(data)
}))
}
}
impl<E: Extra> HasStats for ExtraReceiver<E> {
fn stats(&mut self) -> &mut Stats { &mut self.stats }
}
struct DataFragmentReceiver {
dibits: Buffer<VoiceDataFragStorage>,
byte: u8,
data: u32,
stats: Stats,
}
impl DataFragmentReceiver {
pub fn new() -> DataFragmentReceiver {
DataFragmentReceiver {
dibits: Buffer::new(VoiceDataFragStorage::new()),
byte: 0,
data: 0,
stats: Stats::default(),
}
}
pub fn feed(&mut self, dibit: Dibit) -> Option<Result<u32>> {
let buf = match self.dibits.feed(dibit) {
Some(buf) => *buf as u16,
None => return None,
};
let bits = match cyclic::decode(buf) {
Some((data, err)) => {
self.stats.record_cyclic(err);
data
},
None => return Some(Err(CyclicUnrecoverable)),
};
self.byte += 1;
self.data <<= 8;
self.data |= bits as u32;
if self.byte == 2 {
Some(Ok(self.data))
} else {
None
}
}
}
impl HasStats for DataFragmentReceiver {
fn stats(&mut self) -> &mut Stats { &mut self.stats }
}