Home | History | Annotate | Download | only in include
      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 < |kMaxPacketOctets|
     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 < |kMaxPacketOctets|
     59   bool AddPayloadOctets(size_t octets, uint64_t value);
     60 
     61  public:
     62   // Add type-checking versions
     63   bool AddPayloadOctets1(uint8_t value) { return AddPayloadOctets(1, value); }
     64   bool AddPayloadOctets2(uint16_t value) { return AddPayloadOctets(2, value); }
     65   bool AddPayloadOctets3(uint32_t value) { return AddPayloadOctets(3, value); }
     66   bool AddPayloadOctets4(uint32_t value) { return AddPayloadOctets(4, value); }
     67   bool AddPayloadOctets6(uint64_t value) { return AddPayloadOctets(6, 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 < |kMaxPacketOctets|
     72   bool AddPayloadBtAddress(const BtAddress& address);
     73 
     74  protected:
     75   // Constructs an empty packet of type |type| and header |header|
     76   Packet(serial_data_type_t type, std::vector<uint8_t> header);
     77 
     78   bool IncrementPayloadCounter(size_t index);
     79   bool IncrementPayloadCounter(size_t index, uint8_t max_val);
     80 
     81  private:
     82   const size_t kMaxPacketOctets = 256;  // Includes the Octet count
     83 
     84   // Underlying containers for storing the actual packet
     85 
     86   // The packet type is one of DATA_TYPE_ACL, DATA_TYPE_COMMAND,
     87   // DATA_TYPE_EVENT, or DATA_TYPE_SCO.
     88   serial_data_type_t type_;
     89 
     90   std::vector<uint8_t> header_;
     91 
     92   std::vector<uint8_t> payload_;
     93 };
     94 
     95 }  // namespace test_vendor_lib
     96