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 extern "C" {
     23 #include "hci/include/hci_hal.h"
     24 }  // extern "C"
     25 
     26 namespace test_vendor_lib {
     27 
     28 // Abstract base class that is subclassed to provide type-specifc accessors on
     29 // data. Manages said data's memory and guarantees the data's persistence for IO
     30 // operations.
     31 class Packet {
     32  public:
     33   virtual ~Packet() = default;
     34 
     35   // Returns the size in octets of the entire packet, which consists of the type
     36   // octet, the header, and the payload.
     37   size_t GetPacketSize() const;
     38 
     39   const std::vector<uint8_t>& GetPayload() const;
     40 
     41   uint8_t GetPayloadSize() const;
     42 
     43   const std::vector<uint8_t>& GetHeader() const;
     44 
     45   uint8_t GetHeaderSize() const;
     46 
     47   serial_data_type_t GetType() const;
     48 
     49   // Validates the packet by checking that the payload size in the header is
     50   // accurate. If the size is not valid, returns false. Otherwise, the data in
     51   // |header| and |payload| is copied into |header_| and |payload_|
     52   // respectively. If an error occurs while the data is being copied, the
     53   // contents of |header| and |payload| are guaranteed to be preserved. The
     54   // packet object will assume ownership of the copied data for its entire
     55   // lifetime.
     56   bool Encode(const std::vector<uint8_t>& header,
     57               const std::vector<uint8_t>& payload);
     58 
     59  protected:
     60   // Constructs an empty packet of type |type|. A call to Encode() shall be made
     61   // to check and fill in the packet's data.
     62   Packet(serial_data_type_t type);
     63 
     64  private:
     65   // Underlying containers for storing the actual packet, broken down into the
     66   // packet header and the packet payload. Data is copied into the vectors
     67   // during the constructor and becomes accessible (read only) to children
     68   // through GetHeader() and GetPayload().
     69   std::vector<uint8_t> header_;
     70 
     71   std::vector<uint8_t> payload_;
     72 
     73   // The packet type is one of DATA_TYPE_ACL, DATA_TYPE_COMMAND,
     74   // DATA_TYPE_EVENT, or DATA_TYPE_SCO.
     75   serial_data_type_t type_;
     76 };
     77 
     78 }  // namespace test_vendor_lib
     79