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