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 "pass_through_packet.h"
18 
19 namespace bluetooth {
20 namespace avrcp {
21 
MakeBuilder(bool response,bool pushed,uint8_t opperation_id)22 std::unique_ptr<PassThroughPacketBuilder> PassThroughPacketBuilder::MakeBuilder(
23     bool response, bool pushed, uint8_t opperation_id) {
24   auto builder = std::unique_ptr<PassThroughPacketBuilder>(
25       new PassThroughPacketBuilder(response, pushed, opperation_id));
26 
27   return builder;
28 }
29 
size() const30 size_t PassThroughPacketBuilder::size() const {
31   return PassThroughPacket::kMinSize();
32 }
33 
Serialize(const std::shared_ptr<::bluetooth::Packet> & pkt)34 bool PassThroughPacketBuilder::Serialize(
35     const std::shared_ptr<::bluetooth::Packet>& pkt) {
36   ReserveSpace(pkt, size());
37 
38   PacketBuilder::PushHeader(pkt);
39 
40   uint8_t byte = opperation_id_ & 0b01111111;
41   if (!pushed_) byte |= 0b10000000;
42   AddPayloadOctets1(pkt, byte);
43   // Data length, for this packet it's always 0;
44   AddPayloadOctets1(pkt, 0x00);
45 
46   return true;
47 }
48 
GetKeyState() const49 KeyState PassThroughPacket::GetKeyState() const {
50   auto it = begin() + Packet::kMinSize();
51   return static_cast<KeyState>(((*it) & 0b10000000) >> 7);
52 }
53 
GetOperationId() const54 uint8_t PassThroughPacket::GetOperationId() const {
55   return *(begin() + Packet::kMinSize()) & 0b01111111;
56 }
57 
IsValid() const58 bool PassThroughPacket::IsValid() const { return size() == kMinSize(); }
59 
ToString() const60 std::string PassThroughPacket::ToString() const {
61   std::stringstream ss;
62   ss << "Avrcp::AvrcpPacket: " << std::endl;
63   ss << "  └ cType = " << GetCType() << std::endl;
64   ss << "  └ Subunit Type = " << loghex(GetSubunitType()) << std::endl;
65   ss << "  └ Subunit ID = " << loghex(GetSubunitId()) << std::endl;
66   ss << "  └ OpCode = " << GetOpcode() << std::endl;
67   ss << "  └ Pushed = " << GetKeyState() << std::endl;
68   ss << "  └ Opperation ID = " << loghex(GetOperationId()) << std::endl;
69 
70   return ss.str();
71 }
72 
73 }  // namespace avrcp
74 }  // namespace bluetooth