Home | History | Annotate | Download | only in include
      1 /*
      2  * Copyright 2017 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 <string>
     21 #include <vector>
     22 
     23 namespace test_vendor_lib {
     24 
     25 // ACL data packets are specified in the Bluetooth Core Specification Version
     26 // 4.2, Volume 2, Part E, Section 5.4.2
     27 class AclPacket {
     28  public:
     29   typedef enum {
     30     FirstNonAutomaticallyFlushable,
     31     Continuing,
     32     FirstAutomaticallyFlushable,
     33     Complete
     34   } PacketBoundaryFlags;
     35   typedef enum {
     36     PointToPoint,
     37     ActiveSlaveBroadcast,
     38     ParkedSlaveBroadcast,
     39     Reserved
     40   } BroadcastFlags;
     41 
     42   virtual ~AclPacket() = default;
     43 
     44   uint16_t GetChannel() const {
     45     return (raw_packet_[0] | (raw_packet_[1] << 8)) & 0xfff;
     46   }
     47 
     48   PacketBoundaryFlags GetPacketBoundaryFlags() const {
     49     return static_cast<PacketBoundaryFlags>((raw_packet_[1] & 0x30) >> 4);
     50   }
     51 
     52   BroadcastFlags GetBroadcastFlags() const {
     53     return static_cast<BroadcastFlags>((raw_packet_[1] & 0xC0) >> 6);
     54   }
     55 
     56   explicit AclPacket(uint16_t channel,
     57                      AclPacket::PacketBoundaryFlags boundary_flags,
     58                      AclPacket::BroadcastFlags broadcast);
     59 
     60   size_t GetPacketSize() const;
     61 
     62   const std::vector<uint8_t>& GetPacket() const;
     63 
     64   void AddPayloadOctets(size_t octets, const std::vector<uint8_t>& bytes);
     65 
     66  private:
     67   // Add |octets| bytes to the payload.
     68   void AddPayloadOctets(size_t octets, uint64_t value);
     69 
     70   static const size_t kHeaderSize = 4;
     71 
     72  public:
     73   // Add type-checking versions
     74   void AddPayloadOctets1(uint8_t value) { AddPayloadOctets(1, value); }
     75   void AddPayloadOctets2(uint16_t value) { AddPayloadOctets(2, value); }
     76   void AddPayloadOctets3(uint32_t value) { AddPayloadOctets(3, value); }
     77   void AddPayloadOctets4(uint32_t value) { AddPayloadOctets(4, value); }
     78   void AddPayloadOctets6(uint64_t value) { AddPayloadOctets(6, value); }
     79   void AddPayloadOctets8(uint64_t value) { AddPayloadOctets(8, value); }
     80 
     81  private:
     82   std::vector<uint8_t> raw_packet_;
     83 };
     84 
     85 }  // namespace test_vendor_lib
     86