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(const std::shared_ptr<::bluetooth::Packet>& pkt) override;
39 
40 protected:
41   BrowsePdu pdu_;
42   std::unique_ptr<::bluetooth::PacketBuilder> payload_;
43 
44   void PushHeader(const std::shared_ptr<::bluetooth::Packet>& pkt, uint16_t length);
45 
BrowsePacketBuilder(BrowsePdu pdu)46   BrowsePacketBuilder(BrowsePdu pdu) : pdu_(pdu) {}
47 };
48 
49 class BrowsePacket : public ::bluetooth::Packet {
50 public:
51   BrowsePacket(const BrowsePacket&) = delete;
52   BrowsePacket& operator=(const BrowsePacket&) = delete;
53 
54   virtual ~BrowsePacket() = default;
55 
56   static std::shared_ptr<BrowsePacket> Parse(std::shared_ptr<::bluetooth::Packet> pkt);
57 
58   /**
59    * Avrcp Browse Packet Layout
60    *   uint8_t pdu_;
61    *   uint16_t length_;
62    *   uint8_t[] payload_;
63    */
kMinSize()64   static constexpr size_t kMinSize() { return 3; }
65 
66   BrowsePdu GetPdu() const;
67   uint16_t GetLength() const;
68 
69   virtual bool IsValid() const override;
70   virtual std::string ToString() const override;
71 
72 protected:
73   using ::bluetooth::Packet::Packet;
74 
75 private:
76   virtual std::pair<size_t, size_t> GetPayloadIndecies() const override;
77 };
78 
79 }  // namespace avrcp
80 }  // namespace bluetooth
81