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