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 <base/macros.h>
21 #include <iostream>
22 
23 #include "hardware/avrcp/avrcp_common.h"
24 #include "hardware/avrcp/avrcp_logging_helper.h"
25 #include "packet/base/iterator.h"
26 #include "packet/base/packet.h"
27 #include "packet/base/packet_builder.h"
28 
29 namespace bluetooth {
30 namespace avrcp {
31 
32 class BrowsePacketBuilder : public ::bluetooth::PacketBuilder {
33  public:
34   virtual ~BrowsePacketBuilder() = default;
35 
36   static std::unique_ptr<BrowsePacketBuilder> MakeBuilder(
37       BrowsePdu pdu, std::unique_ptr<::bluetooth::PacketBuilder> payload);
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   BrowsePdu pdu_;
45   std::unique_ptr<::bluetooth::PacketBuilder> payload_;
46 
47   void PushHeader(const std::shared_ptr<::bluetooth::Packet>& pkt,
48                   uint16_t length);
49 
BrowsePacketBuilder(BrowsePdu pdu)50   BrowsePacketBuilder(BrowsePdu pdu) : pdu_(pdu){};
51 };
52 
53 class BrowsePacket : public ::bluetooth::Packet {
54  public:
55   virtual ~BrowsePacket() = default;
56 
57   static std::shared_ptr<BrowsePacket> Parse(
58       std::shared_ptr<::bluetooth::Packet> pkt);
59 
60   /**
61    * Avrcp Browse Packet Layout
62    *   uint8_t pdu_;
63    *   uint16_t length_;
64    *   uint8_t[] payload_;
65    */
kMinSize()66   static constexpr size_t kMinSize() { return 3; }
67 
68   BrowsePdu GetPdu() const;
69   uint16_t GetLength() const;
70 
71   virtual bool IsValid() const override;
72   virtual std::string ToString() const override;
73 
74  protected:
75   using ::bluetooth::Packet::Packet;
76 
77  private:
78   virtual std::pair<size_t, size_t> GetPayloadIndecies() const override;
79   DISALLOW_COPY_AND_ASSIGN(BrowsePacket);
80 };
81 
82 }  // namespace avrcp
83 }  // namespace bluetooth