1 //! Utility for packet manipulation on top of the codegen from PDL
2
3 use crate::packets::{
4 AttAttributeDataBuilder, AttAttributeDataChild, AttBuilder, AttChild, AttOpcode, Builder,
5 OwnedAttView, OwnedPacket, Serializable,
6 };
7
8 /// Convert an ATT builder child into an owned AttView, for use in test
build_att_view_or_crash(child: impl Into<AttChild>) -> OwnedAttView9 pub fn build_att_view_or_crash(child: impl Into<AttChild>) -> OwnedAttView {
10 let child = child.into();
11 let opcode = HACK_child_to_opcode(&child);
12 let serialized = AttBuilder { _child_: child, opcode }.to_vec().unwrap();
13 OwnedAttView::try_parse(serialized.into_boxed_slice()).unwrap()
14 }
15
16 /// Convert an arbitrary packet builder into an OwnedView, for use in test
build_view_or_crash<T: Builder>(builder: T) -> T::OwnedPacket17 pub fn build_view_or_crash<T: Builder>(builder: T) -> T::OwnedPacket {
18 let buf = builder.to_vec().unwrap();
19 T::OwnedPacket::try_parse(buf.into_boxed_slice()).unwrap()
20 }
21
22 /// Hack to workaround PDL limitations where constraints are ignored in builders
23 /// TODO(aryarahul) - get rid of this, PDL should deal with it!
24 #[allow(non_snake_case)]
HACK_child_to_opcode(child: &AttChild) -> AttOpcode25 pub fn HACK_child_to_opcode(child: &AttChild) -> AttOpcode {
26 match child {
27 AttChild::RawData(_vec) => unreachable!(),
28 AttChild::AttFindInformationRequest(_) => AttOpcode::FIND_INFORMATION_REQUEST,
29 AttChild::AttReadByGroupTypeRequest(_) => AttOpcode::READ_BY_GROUP_TYPE_REQUEST,
30 AttChild::AttReadByTypeRequest(_) => AttOpcode::READ_BY_TYPE_REQUEST,
31 AttChild::AttReadRequest(_) => AttOpcode::READ_REQUEST,
32 AttChild::AttReadResponse(_) => AttOpcode::READ_RESPONSE,
33 AttChild::AttErrorResponse(_) => AttOpcode::ERROR_RESPONSE,
34 AttChild::AttReadByGroupTypeResponse(_) => AttOpcode::READ_BY_GROUP_TYPE_RESPONSE,
35 AttChild::AttReadByTypeResponse(_) => AttOpcode::READ_BY_TYPE_RESPONSE,
36 AttChild::AttFindInformationResponse(_) => AttOpcode::FIND_INFORMATION_RESPONSE,
37 AttChild::AttFindByTypeValueRequest(_) => AttOpcode::FIND_BY_TYPE_VALUE_REQUEST,
38 AttChild::AttFindByTypeValueResponse(_) => AttOpcode::FIND_BY_TYPE_VALUE_RESPONSE,
39 AttChild::AttWriteRequest(_) => AttOpcode::WRITE_REQUEST,
40 AttChild::AttWriteResponse(_) => AttOpcode::WRITE_RESPONSE,
41 AttChild::AttHandleValueIndication(_) => AttOpcode::HANDLE_VALUE_INDICATION,
42 AttChild::AttHandleValueConfirmation(_) => AttOpcode::HANDLE_VALUE_CONFIRMATION,
43 AttChild::AttExchangeMtuRequest(_) => AttOpcode::EXCHANGE_MTU_REQUEST,
44 AttChild::AttExchangeMtuResponse(_) => AttOpcode::EXCHANGE_MTU_RESPONSE,
45 AttChild::AttWriteCommand(_) => AttOpcode::WRITE_COMMAND,
46 }
47 }
48
49 /// Utility to simplify assembly of AttData by reducing boilerplate
build_att_data(child: impl Into<AttAttributeDataChild>) -> AttAttributeDataBuilder50 pub fn build_att_data(child: impl Into<AttAttributeDataChild>) -> AttAttributeDataBuilder {
51 AttAttributeDataBuilder { _child_: child.into() }
52 }
53