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 #include "capabilities_packet.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include "internal_include/bt_trace.h"
22 
23 namespace bluetooth {
24 namespace avrcp {
25 
26 std::unique_ptr<GetCapabilitiesRequestBuilder>
MakeBuilder(Capability capability)27 GetCapabilitiesRequestBuilder::MakeBuilder(Capability capability) {
28   std::unique_ptr<GetCapabilitiesRequestBuilder> builder(
29       new GetCapabilitiesRequestBuilder(capability));
30 
31   return builder;
32 }
33 
size() const34 size_t GetCapabilitiesRequestBuilder::size() const {
35   return GetCapabilitiesRequest::kMinSize();
36 }
37 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)38 bool GetCapabilitiesRequestBuilder::Serialize(
39     const std::shared_ptr<::bluetooth::Packet>& pkt) {
40   ReserveSpace(pkt, size());
41 
42   // Push the standard avrcp headers
43   PacketBuilder::PushHeader(pkt);
44 
45   // Push the avrcp vendor command headers, the parameter length is always 1
46   VendorPacketBuilder::PushHeader(pkt, 1);
47 
48   // Push the capability
49   return AddPayloadOctets1(pkt, static_cast<uint8_t>(capability_));
50 }
51 
GetCapabilityRequested() const52 Capability GetCapabilitiesRequest::GetCapabilityRequested() const {
53   auto value = *(begin() + VendorPacket::kMinSize());
54   return static_cast<Capability>(value);
55 }
56 
IsValid() const57 bool GetCapabilitiesRequest::IsValid() const {
58   if (!VendorPacket::IsValid()) return false;
59   return (size() == VendorPacket::kMinSize() + 1);
60 }
61 
ToString() const62 std::string GetCapabilitiesRequest::ToString() const {
63   std::stringstream ss;
64   ss << "AvrcpCapabilityReqPacket: " << std::endl;
65   ss << "  └ cType = " << GetCType() << std::endl;
66   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
67   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
68   ss << "  └ OpCode = " << GetOpcode() << std::endl;
69   ss << "  └ Company ID = " << loghex(GetCompanyId()) << std::endl;
70   ss << "  └ Command PDU = " << GetCommandPdu() << std::endl;
71   ss << "  └ PacketType = " << GetPacketType() << std::endl;
72   ss << "  └ Parameter Length = " << loghex(GetParameterLength()) << std::endl;
73   ss << "  └ Capabilities Request = " << GetCapabilityRequested() << std::endl;
74   return ss.str();
75 }
76 
77 std::unique_ptr<GetCapabilitiesResponseBuilder>
MakeCompanyIdBuilder(uint32_t company_id_element)78 GetCapabilitiesResponseBuilder::MakeCompanyIdBuilder(
79     uint32_t company_id_element) {
80   std::unique_ptr<GetCapabilitiesResponseBuilder> builder(
81       new GetCapabilitiesResponseBuilder(Capability::COMPANY_ID));
82 
83   company_id_element &= 0x00FFFFFF;
84   builder->elements_.insert(company_id_element);
85 
86   return builder;
87 }
88 
89 std::unique_ptr<GetCapabilitiesResponseBuilder>
MakeEventsSupportedBuilder(Event event)90 GetCapabilitiesResponseBuilder::MakeEventsSupportedBuilder(Event event) {
91   std::unique_ptr<GetCapabilitiesResponseBuilder> builder(
92       new GetCapabilitiesResponseBuilder(Capability::EVENTS_SUPPORTED));
93 
94   builder->elements_.insert(static_cast<uint8_t>(event));
95 
96   return builder;
97 }
98 
AddCompanyId(uint32_t company_id)99 GetCapabilitiesResponseBuilder* GetCapabilitiesResponseBuilder::AddCompanyId(
100     uint32_t company_id) {
101   log::assert_that(capability_ == Capability::COMPANY_ID,
102                    "assert failed: capability_ == Capability::COMPANY_ID");
103   log::assert_that(elements_.size() < size_t(0xFF),
104                    "maximum capability count reached");
105 
106   company_id &= 0x00FFFFFF;
107   elements_.insert(company_id);
108 
109   return this;
110 }
111 
AddEvent(Event event)112 GetCapabilitiesResponseBuilder* GetCapabilitiesResponseBuilder::AddEvent(
113     Event event) {
114   log::assert_that(
115       capability_ == Capability::EVENTS_SUPPORTED,
116       "assert failed: capability_ == Capability::EVENTS_SUPPORTED");
117   log::assert_that(elements_.size() < size_t(0xFF),
118                    "maximum capability count reached");
119 
120   elements_.insert(static_cast<uint8_t>(event));
121 
122   return this;
123 }
124 
size() const125 size_t GetCapabilitiesResponseBuilder::size() const {
126   // Company ID's have a size of 3 while Events have a size of 1
127   size_t capability_count = elements_.size();
128   size_t capability_size = capability_ == Capability::COMPANY_ID ? 3 : 1;
129 
130   return GetCapabilitiesResponse::kMinSize() +
131          (capability_count * capability_size);
132 }
133 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)134 bool GetCapabilitiesResponseBuilder::Serialize(
135     const std::shared_ptr<::bluetooth::Packet>& pkt) {
136   ReserveSpace(pkt, size());
137 
138   // Push the standard avrcp headers
139   PacketBuilder::PushHeader(pkt);
140 
141   // Push the avrcp vendor command headers
142   uint16_t parameter_count = size() - VendorPacket::kMinSize();
143   VendorPacketBuilder::PushHeader(pkt, parameter_count);
144 
145   // Push the capability, capability count, and elements
146   AddPayloadOctets1(pkt, static_cast<uint8_t>(capability_));
147   AddPayloadOctets1(pkt, elements_.size());
148   for (auto it = elements_.begin(); it != elements_.end(); it++) {
149     if (capability_ == Capability::COMPANY_ID)
150       PushCompanyId(pkt, *it);
151     else
152       AddPayloadOctets1(pkt, *it);
153   }
154 
155   return true;
156 }
157 
158 }  // namespace avrcp
159 }  // namespace bluetooth
160