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
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use packet_encoding::{decodable_enum, Decodable, Encodable};

use crate::{Error, Result};

/// An AVCTP Transaction Label
/// Not used outside the library. Public as part of some internal Error variants.
/// See Section 6.1.1
#[derive(Debug, Clone, PartialEq)]
pub struct TxLabel(u8);

// Transaction labels are only 4 bits.
const MAX_TX_LABEL: u8 = 0xF;

impl TryFrom<u8> for TxLabel {
    type Error = Error;
    fn try_from(value: u8) -> Result<Self> {
        if value > MAX_TX_LABEL {
            Err(Error::OutOfRange)
        } else {
            Ok(TxLabel(value))
        }
    }
}

impl From<&TxLabel> for u8 {
    fn from(v: &TxLabel) -> u8 {
        v.0
    }
}

impl From<&TxLabel> for usize {
    fn from(v: &TxLabel) -> usize {
        v.0 as usize
    }
}

/// An AVCTP Profile Identifier
/// The type indicates the how the command/request frame is encoded. It should be identical to the
/// 16bit UUID of the service class for this profile.
/// See Section 6.1.1
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct ProfileId([u8; 2]);

/// 16bit UUID for "A/V Remote Control" assigned by the Bluetooth assigned numbers document
pub(crate) const AV_REMOTE_PROFILE: &ProfileId = &ProfileId([0x11, 0x0e]);

impl From<[u8; 2]> for ProfileId {
    fn from(value: [u8; 2]) -> Self {
        Self(value)
    }
}

decodable_enum! {
    /// Indicates whether this packet is part of a fragmented packet set.
    /// See Section 6.1
    pub enum PacketType<u8, Error, OutOfRange> {
        Single = 0x00,
        Start = 0x01,
        Continue = 0x02,
        End = 0x03,
    }
}

decodable_enum! {
    /// Specifies the type of the packet as being either Command or Response
    /// See Section 6.1.1
    pub enum MessageType<u8, Error, OutOfRange> {
        Command = 0x00,
        Response = 0x01,
    }
}

#[derive(Debug)]
pub struct Header {
    label: TxLabel,            // byte 0, bit 7..4
    packet_type: PacketType,   // byte 0, bit 3..2
    message_type: MessageType, // byte 0, bit 1
    invalid_profile_id: bool,  // byte 0, bit 0
    num_packets: u8,           // byte 1 if packet type == start
    profile_id: ProfileId,     // byte 1..2 (byte 2..3 if packet type is start)
}

impl Header {
    pub(crate) fn new(
        label: TxLabel,
        profile_id: ProfileId,
        message_type: MessageType,
        invalid_profile_id: bool,
    ) -> Header {
        Header {
            label,
            profile_id,
            message_type,
            packet_type: PacketType::Single,
            invalid_profile_id,
            num_packets: 1,
        }
    }

    /// Creates a new header from this header with it's message type set to response.
    pub(crate) fn create_response(&self, packet_type: PacketType) -> Header {
        Header {
            label: self.label.clone(),
            profile_id: self.profile_id.clone(),
            message_type: MessageType::Response,
            packet_type,
            invalid_profile_id: false,
            num_packets: 1,
        }
    }

    /// Creates a new header from this header with it's message type set to response
    /// and with the ipid (invalid profile id) bit set to true.
    pub(crate) fn create_invalid_profile_id_response(&self) -> Header {
        Header {
            label: self.label.clone(),
            profile_id: self.profile_id.clone(),
            message_type: MessageType::Response,
            packet_type: PacketType::Single,
            invalid_profile_id: true,
            num_packets: 1,
        }
    }

    pub(crate) fn label(&self) -> &TxLabel {
        &self.label
    }

    pub(crate) fn profile_id(&self) -> &ProfileId {
        &self.profile_id
    }

    pub fn message_type(&self) -> &MessageType {
        &self.message_type
    }

    pub fn packet_type(&self) -> &PacketType {
        &self.packet_type
    }

    pub fn is_invalid_profile_id(&self) -> bool {
        self.invalid_profile_id
    }

    // convenience helpers
    pub fn is_type(&self, other: &MessageType) -> bool {
        &self.message_type == other
    }

    pub fn is_single(&self) -> bool {
        self.packet_type == PacketType::Single
    }
}

impl Decodable for Header {
    type Error = Error;

    fn decode(bytes: &[u8]) -> Result<Header> {
        if bytes.len() < 3 {
            return Err(Error::OutOfRange);
        }
        let label = TxLabel::try_from(bytes[0] >> 4)?;
        let packet_type = PacketType::try_from((bytes[0] >> 2) & 0x3)?;
        let (id_offset, num_packets) = match packet_type {
            PacketType::Start => {
                if bytes.len() < 4 {
                    return Err(Error::OutOfRange);
                }
                (2, bytes[1])
            }
            _ => (1, 1),
        };

        let profile_id = ProfileId::from([bytes[id_offset], bytes[id_offset + 1]]);
        let invalid_profile_id = bytes[0] & 0x1 == 1;
        let header = Header {
            label,
            profile_id,
            message_type: MessageType::try_from(bytes[0] >> 1 & 0x1)?,
            packet_type,
            invalid_profile_id,
            num_packets,
        };
        Ok(header)
    }
}

impl Encodable for Header {
    type Error = Error;

    fn encoded_len(&self) -> usize {
        match self.packet_type {
            PacketType::Start => 4,
            _ => 3,
        }
    }

    fn encode(&self, buf: &mut [u8]) -> Result<()> {
        if buf.len() < self.encoded_len() {
            return Err(Error::Encoding);
        }
        let invalid_profile_id: u8 = if self.invalid_profile_id { 1 } else { 0 };
        buf[0] = u8::from(&self.label) << 4
            | u8::from(&self.packet_type) << 2
            | u8::from(&self.message_type) << 1
            | invalid_profile_id;
        let mut buf_idx = 1;
        if self.packet_type == PacketType::Start {
            buf[buf_idx] = self.num_packets;
            buf_idx = 2;
        }
        let profile_id = self.profile_id.0;
        buf[buf_idx] = profile_id[0];
        buf[buf_idx + 1] = profile_id[1];
        Ok(())
    }
}

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

    #[test]
    /// Test Header encoding
    fn test_header_encode() {
        let header =
            Header::new(TxLabel(0), AV_REMOTE_PROFILE.clone(), MessageType::Command, false);
        assert!(!header.is_invalid_profile_id());
        assert!(header.is_single());
        assert!(header.is_type(&MessageType::Command));
        assert_eq!(TxLabel(0), *header.label());
        let len = header.encoded_len();
        assert_eq!(3, len);
        let mut buf = vec![0; len];
        assert!(header.encode(&mut buf[..]).is_ok());

        assert_eq!(
            &[
                0x00, // TxLabel 0, Single 0, Command 0, Ipid 0,
                0x11, // AV PROFILE
                0x0e, // AV PROFILE
            ],
            &buf[..]
        );
    }

    #[test]
    /// Test Header encoding
    fn test_header_encode_response() {
        let header =
            Header::new(TxLabel(15), AV_REMOTE_PROFILE.clone(), MessageType::Command, false);
        let header = header.create_response(PacketType::Single);
        assert!(!header.is_invalid_profile_id());
        assert!(header.is_single());
        assert!(header.is_type(&MessageType::Response));
        assert_eq!(TxLabel(15), *header.label());
        let len = header.encoded_len();
        assert_eq!(3, len);
        let mut buf = vec![0; len];
        assert!(header.encode(&mut buf[..]).is_ok());

        assert_eq!(
            &[
                0xf2, // TxLabel 15, Single 0, Response 1, Ipid 0
                0x11, // AV PROFILE
                0x0e, // AV PROFILE
            ],
            &buf[..]
        );
    }

    #[test]
    /// Test Header encoding
    fn test_header_encode_invalid_profile_response() {
        let header =
            Header::new(TxLabel(0), AV_REMOTE_PROFILE.clone(), MessageType::Command, false);
        let header = header.create_invalid_profile_id_response();
        assert!(header.is_invalid_profile_id());
        assert!(header.is_single());
        assert!(header.is_type(&MessageType::Response));
        assert_eq!(TxLabel(0), *header.label());
        let len = header.encoded_len();
        assert_eq!(3, len);
        let mut buf = vec![0; len];
        assert!(header.encode(&mut buf[..]).is_ok());

        assert_eq!(
            &[
                0x03, // TxLabel 0, Single 0, Response 1, Ipid 1
                0x11, // AV PROFILE
                0x0e, // AV PROFILE
            ],
            &buf[..]
        );
    }

    #[test]
    /// Test Header decoding
    fn test_header_decode_invalid_packet_response() {
        let header = Header::decode(&[
            0xf3, // TxLabel 15, Single 0, Response 1, Ipid 1
            0x11, // AV PROFILE
            0x0e, // AV PROFILE
            0x12, // extra ignored
            0x34, // extra ignored
            0x45, // extra ignored
        ])
        .expect("unable to decode header");
        assert!(header.is_invalid_profile_id());
        assert!(header.is_single());
        assert!(header.is_type(&MessageType::Response));
        assert_eq!(TxLabel(15), *header.label());
    }

    #[test]
    /// Test Header decoding
    fn test_header_decode_command() {
        let header = Header::decode(&[
            0x80, // TxLabel 8, Single 0, Command 0, Ipid 0
            0x11, // AV PROFILE
            0x0e, // AV PROFILE
            0x34, // extra ignored
            0x45, // extra ignored
        ])
        .expect("unable to decode header");
        assert!(!header.is_invalid_profile_id());
        assert!(header.is_single());
        assert!(header.is_type(&MessageType::Command));
        assert_eq!(TxLabel(8), *header.label());
    }

    #[test]
    /// Test Header decoding
    fn test_header_decode_invalid() {
        assert_eq!(
            Error::OutOfRange,
            Header::decode(&[
                0x80, // TxLabel 8, Single 0, Command 0, Ipid 0
                0x11, // AV PROFILE
                      // missing fields
            ])
            .unwrap_err()
        );
    }

    #[test]
    fn txlabel_tofrom_u8() {
        let mut label: Result<TxLabel> = TxLabel::try_from(15);
        assert!(label.is_ok());
        assert_eq!(15, u8::from(&label.unwrap()));
        label = TxLabel::try_from(16);
        assert_eq!(Err(Error::OutOfRange), label);
    }

    #[test]
    fn txlabel_to_usize() {
        let label = TxLabel::try_from(1).unwrap();
        assert_eq!(1, usize::from(&label));
    }
}