1 /* 2 * Copyright 2015 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 #pragma once 18 19 #include <cstdint> 20 #include <vector> 21 22 #include "bt_address.h" 23 #include "hci/include/hci_hal.h" 24 25 namespace test_vendor_lib { 26 27 const size_t kReservedZero = 0; 28 29 // Abstract base class that is subclassed to provide type-specifc accessors on 30 // data. Manages said data's memory and guarantees the data's persistence for IO 31 // operations. 32 class Packet { 33 public: 34 virtual ~Packet() = default; 35 36 // Returns the size in octets of the entire packet, which consists of the type 37 // octet, the header, and the payload. 38 size_t GetPacketSize() const; 39 40 const std::vector<uint8_t>& GetPayload() const; 41 42 size_t GetPayloadSize() const; 43 44 const std::vector<uint8_t>& GetHeader() const; 45 46 uint8_t GetHeaderSize() const; 47 48 serial_data_type_t GetType() const; 49 50 // Add |octets| bytes to the payload. Return true if: 51 // - the size of |bytes| is equal to |octets| and 52 // - the new size of the payload is still < |kMaxPayloadOctets| 53 bool AddPayloadOctets(size_t octets, const std::vector<uint8_t>& bytes); 54 55 private: 56 // Add |octets| bytes to the payload. Return true if: 57 // - the value of |value| fits in |octets| bytes and 58 // - the new size of the payload is still < |kMaxPayloadOctets| 59 bool AddPayloadOctets(size_t octets, uint64_t value); 60 61 public: 62 // Add type-checking versions AddPayloadOctets1(uint8_t value)63 bool AddPayloadOctets1(uint8_t value) { return AddPayloadOctets(1, value); } AddPayloadOctets2(uint16_t value)64 bool AddPayloadOctets2(uint16_t value) { return AddPayloadOctets(2, value); } AddPayloadOctets3(uint32_t value)65 bool AddPayloadOctets3(uint32_t value) { return AddPayloadOctets(3, value); } AddPayloadOctets4(uint32_t value)66 bool AddPayloadOctets4(uint32_t value) { return AddPayloadOctets(4, value); } AddPayloadOctets6(uint64_t value)67 bool AddPayloadOctets6(uint64_t value) { return AddPayloadOctets(6, value); } AddPayloadOctets8(uint64_t value)68 bool AddPayloadOctets8(uint64_t value) { return AddPayloadOctets(8, value); } 69 70 // Add |address| to the payload. Return true if: 71 // - the new size of the payload is still < |kMaxPayloadOctets| 72 bool AddPayloadBtAddress(const BtAddress& address); 73 74 // Return true if |num_bytes| can be added to the payload. CanAddPayloadOctets(size_t num_bytes)75 bool CanAddPayloadOctets(size_t num_bytes) const { 76 return GetPayloadSize() + num_bytes <= kMaxPayloadOctets; 77 } 78 79 protected: 80 // Constructs an empty packet of type |type| and header |header| 81 Packet(serial_data_type_t type, std::vector<uint8_t> header); 82 83 bool IncrementPayloadCounter(size_t index); 84 bool IncrementPayloadCounter(size_t index, uint8_t max_val); 85 86 private: 87 const size_t kMaxPayloadOctets = 256; // Includes the size byte. 88 89 // Underlying containers for storing the actual packet 90 91 // The packet type is one of DATA_TYPE_ACL, DATA_TYPE_COMMAND, 92 // DATA_TYPE_EVENT, or DATA_TYPE_SCO. 93 serial_data_type_t type_; 94 95 std::vector<uint8_t> header_; 96 97 std::vector<uint8_t> payload_; 98 }; 99 100 } // namespace test_vendor_lib 101