• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
18 
19 #include <base/logging.h>
20 #include <iostream>
21 
22 #include "hardware/avrcp/avrcp_common.h"
23 #include "hardware/avrcp/avrcp_logging_helper.h"
24 #include "packet/base/iterator.h"
25 #include "packet/base/packet.h"
26 #include "packet/base/packet_builder.h"
27 
28 namespace bluetooth {
29 namespace avrcp {
30 
31 class PacketBuilder : public ::bluetooth::PacketBuilder {
32  public:
33   virtual ~PacketBuilder() = default;
34 
35   static std::unique_ptr<PacketBuilder> MakeBuilder(
36       CType cType, uint8_t subunit_type, uint8_t subunit_id, Opcode opcode,
37       std::unique_ptr<::bluetooth::PacketBuilder> packet);
38 
39   virtual size_t size() const override;
40   virtual bool Serialize(
41       const std::shared_ptr<::bluetooth::Packet>& pkt) override;
42 
43  protected:
44   CType c_type_;
45   uint8_t subunit_type_ : 5;
46   uint8_t subunit_id_ : 3;
47   Opcode opcode_;
48   std::unique_ptr<::bluetooth::PacketBuilder> payload_;
49 
50   void PushHeader(const std::shared_ptr<::bluetooth::Packet>& pkt);
51   bool PushCompanyId(const std::shared_ptr<::bluetooth::Packet>& pkt,
52                      uint32_t company_id);
53 
PacketBuilder(CType type,uint8_t subunit_type,uint8_t subunit_id,Opcode opcode)54   PacketBuilder(CType type, uint8_t subunit_type, uint8_t subunit_id,
55                 Opcode opcode)
56       : c_type_(type),
57         subunit_type_(subunit_type),
58         subunit_id_(subunit_id),
59         opcode_(opcode){};
60 };
61 
62 class Packet : public ::bluetooth::Packet {
63  public:
64   Packet(const Packet&) = delete;
65   Packet& operator=(const Packet&) = delete;
66 
67   virtual ~Packet() = default;
68 
69   // TODO (apanicke): Right now we can use this to build an AvrcpPacket from
70   // another packet type. In the future, we can remove this in favor of
71   // getting an AVRCP Packet directly from an AVCTP Packet
72   static std::shared_ptr<Packet> Parse(
73       std::shared_ptr<::bluetooth::Packet> pkt);
74 
75   /**
76    * Avrcp Packet Layout
77    *   CType c_type_;
78    *   uint8_t subunit_type_ : 5;
79    *   uint8_t subunit_id_ : 3;
80    *   Opcode opcode_;
81    *   uint8_t[] payload_;
82    */
kMinSize()83   static constexpr size_t kMinSize() { return 3; };
84 
85   // Getter Functions
86   CType GetCType() const;
87   uint8_t GetSubunitType() const;
88   uint8_t GetSubunitId() const;
89   Opcode GetOpcode() const;
90 
91   // Overloaded Functions
92   virtual bool IsValid() const override;
93   virtual std::string ToString() const override;
94 
95  protected:
96   using ::bluetooth::Packet::Packet;
97 
PullCompanyId(Iterator it)98   static inline uint32_t PullCompanyId(Iterator it) {
99     uint32_t value = 0;
100     for (int i = 0; i < 3; i++) {
101       value <<= 8;
102       value |= *it++;
103     }
104     return value;
105   }
106 
107  private:
108   virtual std::pair<size_t, size_t> GetPayloadIndecies() const override;
109 };
110 
111 }  // namespace avrcp
112 }  // namespace bluetooth
113