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
//! Header generation for data packets.
//!
//! A header has several user-filled fields followed by a 16-bit checksum over those
//! fields.

use data::crc;
use data::fields;

/// Packet header block for confirmed data packet.
pub type ConfirmedHeader = Header<ConfirmedFields>;

/// Packet header block for unconfirmed data packet.
pub type UnconfirmedHeader = Header<UnconfirmedFields>;

/// Write some bytes into a buffer.
pub trait BufWrite {
    fn write<'a, 'b, T: Iterator<Item = &'a mut u8>>(&self, buf: &'b mut T);
}

/// Field is only a single byte.
pub trait ByteField {
    fn byte(&self) -> u8;
}

/// Just write the single byte.
impl<B: ByteField> BufWrite for B {
    fn write<'a, 'b, T: Iterator<Item = &'a mut u8>>(&self, buf: &'b mut T) {
        *buf.next().unwrap() = self.byte();
    }
}

/// Preamble header field.
pub struct HeaderPreamble {
    /// Whether the packet requires confirmation.
    pub confirmed: bool,
    /// Whether the packet is an outbound message.
    pub outbound: bool,
    /// Packet type.
    pub format: fields::DataPacketOpcode,
}

impl ByteField for HeaderPreamble {
    fn byte(&self) -> u8 {
        bool_to_bit(self.confirmed) << 6 | bool_to_bit(self.outbound) << 5 |
            self.format.to_bits()
    }
}

/// Preamble for confirmed data packet.
pub struct ConfirmedPreamble(HeaderPreamble);

impl ConfirmedPreamble {
    fn new(outbound: bool) -> ConfirmedPreamble {
        ConfirmedPreamble(HeaderPreamble {
            confirmed: true,
            outbound: outbound,
            format: fields::DataPacketOpcode::ConfirmedPacket,
        })
    }

    pub fn outbound() -> Self { Self::new(true) }
    pub fn inbound() -> Self { Self::new(false) }
}

impl ByteField for ConfirmedPreamble {
    fn byte(&self) -> u8 { self.0.byte() }
}

/// Preamble for unconfirmed data packet.
pub struct UnconfirmedPreamble(HeaderPreamble);

impl UnconfirmedPreamble {
    fn new(outbound: bool) -> UnconfirmedPreamble {
        UnconfirmedPreamble(HeaderPreamble {
            confirmed: false,
            outbound: outbound,
            format: fields::DataPacketOpcode::UnconfirmedPacket
        })
    }

    pub fn outbound() -> Self { Self::new(true) }
    pub fn inbound() -> Self { Self::new(false) }
}

impl ByteField for UnconfirmedPreamble {
    fn byte(&self) -> u8 { self.0.byte() }
}

/// Service access point (SAP) field.
pub struct ServiceAccessPoint(pub fields::ServiceAccessPoint);

impl ByteField for ServiceAccessPoint {
    fn byte(&self) -> u8 {
        0b11000000 | self.0.to_bits()
    }
}

/// Manufacturer's ID field.
pub struct Manufacturer(pub u8);

impl ByteField for Manufacturer {
    fn byte(&self) -> u8 { self.0 }
}

/// Logical link ID field for addressing source or destination subscriber.
pub struct LogicalLink(pub u32);

impl BufWrite for LogicalLink {
    fn write<'a, 'b, T: Iterator<Item = &'a mut u8>>(&self, buf: &'b mut T) {
        assert!(self.0 >> 24 == 0);

        *buf.next().unwrap() = (self.0 >> 16) as u8;
        *buf.next().unwrap() = (self.0 >> 8) as u8;
        *buf.next().unwrap() = self.0 as u8;
    }
}

/// FMF and blocks-to-follow fields.
pub struct BlockCount {
    /// Whether the packet is "complete", not being partially retransmitted.
    pub full_pkt: bool,
    /// Number of data blocks in the packet.
    pub count: u8,
}

impl ByteField for BlockCount {
    fn byte(&self) -> u8 {
        assert!(self.count >> 7 == 0);
        bool_to_bit(self.full_pkt) << 7 | self.count
    }
}

/// Number of pad bytes at the end of the data.
pub struct PadCount(pub u8);

impl ByteField for PadCount {
    fn byte(&self) -> u8 {
        assert!(self.0 >> 5 == 0);
        self.0
    }
}

/// Syn, N(S), and FSNF fields.
pub struct Sequencing {
    /// Whether the receiver should resynchronize its sequence numbers using `pkt_seq` and
    /// `frag_seq`.
    pub resync: bool,
    /// Packet sequence number, used for ordering and duplicate removal.
    pub pkt_seq: u8,
    /// Fragment sequence number, used when data is split across multiple fragments.
    pub frag_seq: u8,
}

impl ByteField for Sequencing {
    fn byte(&self) -> u8 {
        assert!(self.pkt_seq >> 3 == 0);
        assert!(self.frag_seq >> 4 == 0);

        bool_to_bit(self.resync) << 7 | self.pkt_seq << 4 | self.frag_seq
    }
}

/// Byte offset into data payload where data header stops and data information begins.
pub struct DataOffset(pub u8);

impl ByteField for DataOffset {
    fn byte(&self) -> u8 {
        assert!(self.0 >> 6 == 0);
        self.0
    }
}

/// Header fields for confirmed packet.
pub struct ConfirmedFields {
    pub preamble: ConfirmedPreamble,
    pub sap: ServiceAccessPoint,
    pub mfg: Manufacturer,
    pub addr: LogicalLink,
    pub blocks: BlockCount,
    pub pads: PadCount,
    pub seq: Sequencing,
    pub data_offset: DataOffset,
}

impl BufWrite for ConfirmedFields {
    fn write<'a, 'b, T: Iterator<Item = &'a mut u8>>(&self, buf: &'b mut T) {
        self.preamble.write(buf);
        self.sap.write(buf);
        self.mfg.write(buf);
        self.addr.write(buf);
        self.blocks.write(buf);
        self.pads.write(buf);
        self.seq.write(buf);
        self.data_offset.write(buf);
    }
}

/// Header fields for unconfirmed packet.
pub struct UnconfirmedFields {
    pub preamble: UnconfirmedPreamble,
    pub sap: ServiceAccessPoint,
    pub mfg: Manufacturer,
    pub addr: LogicalLink,
    pub blocks: BlockCount,
    pub pads: PadCount,
    pub data_offset: DataOffset,
}

impl BufWrite for UnconfirmedFields {
    fn write<'a, 'b, T: Iterator<Item = &'a mut u8>>(&self, buf: &'b mut T) {
        self.preamble.write(buf);
        self.sap.write(buf);
        self.mfg.write(buf);
        self.addr.write(buf);
        self.blocks.write(buf);
        self.pads.write(buf);
        *buf.next().unwrap() = 0;
        self.data_offset.write(buf);
    }
}

/// Builds a checksummed header based on the given fields.
pub struct Header<F: BufWrite>(F);

impl<F: BufWrite> Header<F> {
    /// Construct a new `Header` with the given fields.
    pub fn new(fields: F) -> Header<F> {
        Header(fields)
    }

    /// Get the fields and checksum that make up the header, in that order.
    pub fn build(self) -> ([u8; 10], [u8; 2]) {
        let fields = self.fields();
        let checksum = self.checksum(&fields);

        (fields, checksum)
    }

    /// Build a byte buffer from the header fields.
    fn fields(&self) -> [u8; 10] {
        let mut buf = [0; 10];
        self.0.write(&mut buf.iter_mut());
        buf
    }

    /// Calculate the checksum of the header fields.
    fn checksum(&self, fields: &[u8]) -> [u8; 2] {
        assert!(fields.len() == 10);

        let checksum = crc::CRC16::new()
            .feed_bytes(fields.iter().cloned())
            .finish();

        [(checksum >> 8) as u8, checksum as u8]
    }
}

/// Convert the given Boolean to a single bit.
fn bool_to_bit(b: bool) -> u8 {
    if b { 1 } else { 0 }
}

#[cfg(test)]
mod test {
    use super::*;
    use data::fields;

    #[test]
    fn test_preamble() {
        let p = ConfirmedPreamble::outbound();
        assert_eq!(p.byte(), 0b01110110);
        let p = ConfirmedPreamble::inbound();
        assert_eq!(p.byte(), 0b01010110);
    }

    #[test]
    fn test_sap() {
        let s = ServiceAccessPoint(fields::ServiceAccessPoint::ExtendedAddressing);
        assert_eq!(s.byte(), 0b11011111);
    }

    #[test]
    fn test_mfg() {
        let m = Manufacturer(0b11011011);
        assert_eq!(m.byte(), 0b11011011);
    }

    #[test]
    fn test_ll() {
        let l = LogicalLink(0xABCDEF);
        let mut buf = [0; 3];
        l.write(&mut buf.iter_mut());
        assert_eq!(&buf, &[0xAB, 0xCD, 0xEF]);
    }

    #[test]
    fn test_bc() {
        let b = BlockCount {
            full_pkt: true,
            count: 127,
        };

        assert_eq!(b.byte(), 0b11111111);
    }

    #[test]
    fn test_pc() {
        let p = PadCount(12);
        assert_eq!(p.byte(), 0b00001100);
    }

    #[test]
    fn test_seq() {
        let s = Sequencing {
            resync: false,
            pkt_seq: 6,
            frag_seq: 10,
        };
        assert_eq!(s.byte(), 0b01101010);
    }

    #[test]
    fn test_do() {
        let d = DataOffset(11);
        assert_eq!(d.byte(), 0b00001011);
    }

    #[test]
    fn test_confirmed_fields() {
        let fields = ConfirmedFields {
            preamble: ConfirmedPreamble::outbound(),
            sap: ServiceAccessPoint(fields::ServiceAccessPoint::Paging),
            mfg: Manufacturer(0x12),
            addr: LogicalLink(0x342134),
            blocks: BlockCount {
                full_pkt: true,
                count: 127,
            },
            pads: PadCount(3),
            seq: Sequencing {
                resync: false,
                pkt_seq: 5,
                frag_seq: 2,
            },
            data_offset: DataOffset(0),
        };

        let mut buf = [0; 10];
        fields.write(&mut buf.iter_mut());

        assert_eq!(&buf, &[
            0b01110110,
            0b11100110,
            0b00010010,
            0b00110100,
            0b00100001,
            0b00110100,
            0b11111111,
            0b00000011,
            0b01010010,
            0b00000000,
        ]);
    }

    #[test]
    fn test_confirmed_header() {
        let (fields, checksum) = ConfirmedHeader::new(ConfirmedFields {
            preamble: ConfirmedPreamble::outbound(),
            sap: ServiceAccessPoint(fields::ServiceAccessPoint::PacketData),
            mfg: Manufacturer(0x12),
            addr: LogicalLink(0x342134),
            blocks: BlockCount {
                full_pkt: true,
                count: 127,
            },
            pads: PadCount(3),
            seq: Sequencing {
                resync: false,
                pkt_seq: 5,
                frag_seq: 2,
            },
            data_offset: DataOffset(0),
        }).build();

        assert_eq!(fields, [
            0b01110110,
            0b11000100,
            0b00010010,
            0b00110100,
            0b00100001,
            0b00110100,
            0b11111111,
            0b00000011,
            0b01010010,
            0b00000000,
        ]);

        assert_eq!(checksum, [
            0b10001010,
            0b01110010,
        ]);
    }

    #[test]
    #[should_panic]
    fn test_ll_validate() {
        let mut buf = [0; 4];
        let mut iter = buf.iter_mut();
        LogicalLink(0x20FFFFFF).write(&mut iter);
    }

    #[test]
    #[should_panic]
    fn test_bc_validate() {
        BlockCount {
            full_pkt: true,
            count: 0b11111111,
        }.byte();
    }

    #[test]
    #[should_panic]
    fn test_pc_validate() {
        PadCount(0b11111111).byte();
    }

    #[test]
    #[should_panic]
    fn test_seq_validate() {
        Sequencing {
            resync: true,
            pkt_seq: 0b11111111,
            frag_seq: 0b11111111,
        }.byte();
    }

    #[test]
    #[should_panic]
    fn test_do_validate() {
        DataOffset(0b11111111).byte();
    }
}