blob: b422bb893deb1e50ff7ff3e29f07787e19ef7629 [file] [log] [blame]
Myles Watsone22dde22019-01-18 11:42:33 -08001/*
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 "packets/hci/command_packet_builder.h"
18
Myles Watson1a0a0da2019-10-17 11:36:33 -070019#include "os/log.h"
Myles Watsone22dde22019-01-18 11:42:33 -080020
21using std::vector;
22using test_vendor_lib::hci::OpCode;
23
24namespace test_vendor_lib {
25namespace packets {
26
27CommandPacketBuilder::CommandPacketBuilder(OpCode opcode, std::unique_ptr<BasePacketBuilder> payload)
28 : opcode_(opcode), payload_(std::move(payload)) {}
29
30size_t CommandPacketBuilder::size() const {
31 return sizeof(uint16_t) + sizeof(uint8_t) + payload_->size();
32}
33
34void CommandPacketBuilder::Serialize(std::back_insert_iterator<std::vector<uint8_t>> it) const {
35 insert(static_cast<uint16_t>(opcode_), it);
36 uint8_t payload_size = static_cast<uint8_t>(payload_->size());
37
Myles Watson1a0a0da2019-10-17 11:36:33 -070038 ASSERT_LOG(static_cast<size_t>(payload_size) == payload_->size(), "Payload too large for a command packet: %d",
39 static_cast<int>(payload_->size()));
Myles Watsone22dde22019-01-18 11:42:33 -080040 insert(payload_size, it);
41 payload_->Serialize(it);
42}
43
44} // namespace packets
45} // namespace test_vendor_lib