Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 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 "packets/hci/command_packet_builder.h" |
| 18 | |
Myles Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 19 | #include "os/log.h" |
Myles Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 20 | |
| 21 | using std::vector; |
| 22 | using test_vendor_lib::hci::OpCode; |
| 23 | |
| 24 | namespace test_vendor_lib { |
| 25 | namespace packets { |
| 26 | |
| 27 | CommandPacketBuilder::CommandPacketBuilder(OpCode opcode, std::unique_ptr<BasePacketBuilder> payload) |
| 28 | : opcode_(opcode), payload_(std::move(payload)) {} |
| 29 | |
| 30 | size_t CommandPacketBuilder::size() const { |
| 31 | return sizeof(uint16_t) + sizeof(uint8_t) + payload_->size(); |
| 32 | } |
| 33 | |
| 34 | void 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 Watson | 1a0a0da | 2019-10-17 11:36:33 -0700 | [diff] [blame] | 38 | 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 Watson | e22dde2 | 2019-01-18 11:42:33 -0800 | [diff] [blame] | 40 | insert(payload_size, it); |
| 41 | payload_->Serialize(it); |
| 42 | } |
| 43 | |
| 44 | } // namespace packets |
| 45 | } // namespace test_vendor_lib |