1 /*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "link_layer_packet_builder.h"
18 #include "link_layer_packet_view.h"
19
20 #include "base/logging.h"
21
22 using std::vector;
23
24 namespace test_vendor_lib {
25
26 namespace packets {
27
LinkLayerPacketBuilder(Link::PacketType type,const Address & source,const Address & dest)28 LinkLayerPacketBuilder::LinkLayerPacketBuilder(Link::PacketType type, const Address& source, const Address& dest)
29 : type_(type), source_addr_(source), dest_addr_(dest) {}
30
LinkLayerPacketBuilder(Link::PacketType type,std::unique_ptr<PacketBuilder> packet,const Address & source)31 LinkLayerPacketBuilder::LinkLayerPacketBuilder(Link::PacketType type, std::unique_ptr<PacketBuilder> packet,
32 const Address& source)
33 : type_(type), source_addr_(source), dest_addr_(Address::kEmpty), builder_(std::move(packet)) {}
34
LinkLayerPacketBuilder(Link::PacketType type,std::unique_ptr<PacketBuilder> packet,const Address & source,const Address & dest)35 LinkLayerPacketBuilder::LinkLayerPacketBuilder(Link::PacketType type, std::unique_ptr<PacketBuilder> packet,
36 const Address& source, const Address& dest)
37 : type_(type), source_addr_(source), dest_addr_(dest), builder_(std::move(packet)) {}
38
WrapAcl(std::unique_ptr<ViewForwarderBuilder> acl,const Address & source,const Address & dest)39 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapAcl(std::unique_ptr<ViewForwarderBuilder> acl,
40 const Address& source, const Address& dest) {
41 return std::shared_ptr<LinkLayerPacketBuilder>(
42 new LinkLayerPacketBuilder(Link::PacketType::ACL, std::move(acl), source, dest));
43 }
44
WrapCommand(std::unique_ptr<CommandBuilder> command,const Address & source,const Address & dest)45 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapCommand(std::unique_ptr<CommandBuilder> command,
46 const Address& source,
47 const Address& dest) {
48 return std::shared_ptr<LinkLayerPacketBuilder>(
49 new LinkLayerPacketBuilder(Link::PacketType::COMMAND, std::move(command), source, dest));
50 }
51
WrapDisconnect(std::unique_ptr<DisconnectBuilder> disconnect,const Address & source,const Address & dest)52 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapDisconnect(
53 std::unique_ptr<DisconnectBuilder> disconnect, const Address& source, const Address& dest) {
54 return std::shared_ptr<LinkLayerPacketBuilder>(
55 new LinkLayerPacketBuilder(Link::PacketType::DISCONNECT, std::move(disconnect), source, dest));
56 }
57
WrapEncryptConnection(std::unique_ptr<EncryptConnectionBuilder> encrypt_connection,const Address & source,const Address & dest)58 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapEncryptConnection(
59 std::unique_ptr<EncryptConnectionBuilder> encrypt_connection, const Address& source, const Address& dest) {
60 return std::shared_ptr<LinkLayerPacketBuilder>(
61 new LinkLayerPacketBuilder(Link::PacketType::ENCRYPT_CONNECTION, std::move(encrypt_connection), source, dest));
62 }
63
WrapEncryptConnectionResponse(std::unique_ptr<EncryptConnectionBuilder> encrypt_connection,const Address & source,const Address & dest)64 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapEncryptConnectionResponse(
65 std::unique_ptr<EncryptConnectionBuilder> encrypt_connection, const Address& source, const Address& dest) {
66 return std::shared_ptr<LinkLayerPacketBuilder>(new LinkLayerPacketBuilder(
67 Link::PacketType::ENCRYPT_CONNECTION_RESPONSE, std::move(encrypt_connection), source, dest));
68 }
69
WrapInquiry(std::unique_ptr<InquiryBuilder> inquiry,const Address & source)70 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapInquiry(std::unique_ptr<InquiryBuilder> inquiry,
71 const Address& source) {
72 return std::shared_ptr<LinkLayerPacketBuilder>(
73 new LinkLayerPacketBuilder(Link::PacketType::INQUIRY, std::move(inquiry), source));
74 }
75
WrapInquiryResponse(std::unique_ptr<InquiryResponseBuilder> inquiry_response,const Address & source,const Address & dest)76 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapInquiryResponse(
77 std::unique_ptr<InquiryResponseBuilder> inquiry_response, const Address& source, const Address& dest) {
78 return std::shared_ptr<LinkLayerPacketBuilder>(
79 new LinkLayerPacketBuilder(Link::PacketType::INQUIRY_RESPONSE, std::move(inquiry_response), source, dest));
80 }
81
WrapIoCapabilityRequest(std::unique_ptr<IoCapabilityBuilder> io_capability,const Address & source,const Address & dest)82 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapIoCapabilityRequest(
83 std::unique_ptr<IoCapabilityBuilder> io_capability, const Address& source, const Address& dest) {
84 return std::shared_ptr<LinkLayerPacketBuilder>(
85 new LinkLayerPacketBuilder(Link::PacketType::IO_CAPABILITY_REQUEST, std::move(io_capability), source, dest));
86 }
87
WrapIoCapabilityResponse(std::unique_ptr<IoCapabilityBuilder> io_capability,const Address & source,const Address & dest)88 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapIoCapabilityResponse(
89 std::unique_ptr<IoCapabilityBuilder> io_capability, const Address& source, const Address& dest) {
90 return std::shared_ptr<LinkLayerPacketBuilder>(
91 new LinkLayerPacketBuilder(Link::PacketType::IO_CAPABILITY_RESPONSE, std::move(io_capability), source, dest));
92 }
93
WrapIoCapabilityNegativeResponse(std::unique_ptr<IoCapabilityNegativeResponseBuilder> io_capability_negative_response,const Address & source,const Address & dest)94 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapIoCapabilityNegativeResponse(
95 std::unique_ptr<IoCapabilityNegativeResponseBuilder> io_capability_negative_response, const Address& source,
96 const Address& dest) {
97 return std::shared_ptr<LinkLayerPacketBuilder>(new LinkLayerPacketBuilder(
98 Link::PacketType::IO_CAPABILITY_NEGATIVE_RESPONSE, std::move(io_capability_negative_response), source, dest));
99 }
100
WrapLeAdvertisement(std::unique_ptr<LeAdvertisementBuilder> advertisement,const Address & source)101 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapLeAdvertisement(
102 std::unique_ptr<LeAdvertisementBuilder> advertisement, const Address& source) {
103 return std::shared_ptr<LinkLayerPacketBuilder>(
104 new LinkLayerPacketBuilder(Link::PacketType::LE_ADVERTISEMENT, std::move(advertisement), source));
105 }
106
WrapLeScan(const Address & source,const Address & dest)107 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapLeScan(const Address& source, const Address& dest) {
108 return std::shared_ptr<LinkLayerPacketBuilder>(new LinkLayerPacketBuilder(Link::PacketType::LE_SCAN, source, dest));
109 }
110
WrapLeScanResponse(std::unique_ptr<LeAdvertisementBuilder> scan_response,const Address & source,const Address & dest)111 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapLeScanResponse(
112 std::unique_ptr<LeAdvertisementBuilder> scan_response, const Address& source, const Address& dest) {
113 return std::shared_ptr<LinkLayerPacketBuilder>(
114 new LinkLayerPacketBuilder(Link::PacketType::LE_SCAN_RESPONSE, std ::move(scan_response), source, dest));
115 }
116
WrapPage(std::unique_ptr<PageBuilder> page,const Address & source,const Address & dest)117 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapPage(std::unique_ptr<PageBuilder> page,
118 const Address& source, const Address& dest) {
119 return std::shared_ptr<LinkLayerPacketBuilder>(
120 new LinkLayerPacketBuilder(Link::PacketType::PAGE, std::move(page), source, dest));
121 }
122
WrapPageResponse(std::unique_ptr<PageResponseBuilder> page_response,const Address & source,const Address & dest)123 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapPageResponse(
124 std::unique_ptr<PageResponseBuilder> page_response, const Address& source, const Address& dest) {
125 return std::shared_ptr<LinkLayerPacketBuilder>(
126 new LinkLayerPacketBuilder(Link::PacketType::PAGE_RESPONSE, std::move(page_response), source, dest));
127 }
128
WrapResponse(std::unique_ptr<ResponseBuilder> response,const Address & source,const Address & dest)129 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapResponse(std::unique_ptr<ResponseBuilder> response,
130 const Address& source,
131 const Address& dest) {
132 return std::shared_ptr<LinkLayerPacketBuilder>(
133 new LinkLayerPacketBuilder(Link::PacketType::RESPONSE, std::move(response), source, dest));
134 }
135
WrapSco(std::unique_ptr<ViewForwarderBuilder> sco,const Address & source,const Address & dest)136 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::WrapSco(std::unique_ptr<ViewForwarderBuilder> sco,
137 const Address& source, const Address& dest) {
138 return std::shared_ptr<LinkLayerPacketBuilder>(
139 new LinkLayerPacketBuilder(Link::PacketType::SCO, std::move(sco), source, dest));
140 }
141
ReWrap(const std::shared_ptr<std::vector<uint8_t>> raw_packet)142 std::shared_ptr<LinkLayerPacketBuilder> LinkLayerPacketBuilder::ReWrap(
143 const std::shared_ptr<std::vector<uint8_t>> raw_packet) {
144 LinkLayerPacketView received = LinkLayerPacketView::Create(raw_packet);
145 Link::PacketType packet_type = received.GetType();
146 Address source = received.GetSourceAddress();
147 Address dest = received.GetDestinationAddress();
148 PacketView<true> payload = received.GetPayload();
149 std::unique_ptr<PacketBuilder> builder = ViewForwarderBuilder::Create(payload);
150 return std::shared_ptr<LinkLayerPacketBuilder>(
151 new LinkLayerPacketBuilder(packet_type, std::move(builder), source, dest));
152 }
153
size() const154 size_t LinkLayerPacketBuilder::size() const {
155 size_t builder_size = (builder_ ? builder_->size() : 0);
156 return Link::kTypeBytes + Link::kSizeBytes + 2 * Address::kLength + builder_size;
157 }
158
Serialize(std::back_insert_iterator<std::vector<uint8_t>> it) const159 void LinkLayerPacketBuilder::Serialize(std::back_insert_iterator<std::vector<uint8_t>> it) const {
160 insert(static_cast<uint32_t>(size() - Link::kSizeBytes), it);
161 insert(static_cast<uint8_t>(type_), it);
162 insert_address(source_addr_.address, it);
163 insert_address(dest_addr_.address, it);
164 if (builder_) {
165 builder_->Serialize(it);
166 }
167 }
168
169 } // namespace packets
170 } // namespace test_vendor_lib
171