1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @file avrcp_tg_unit_info.cpp
18  *
19  * @brief Defines the class of the UNIT INFO command, including attributes and methods.
20  */
21 
22 #include "avrcp_tg_unit_info.h"
23 
24 namespace OHOS {
25 namespace bluetooth {
AvrcTgUnitPacket()26 AvrcTgUnitPacket::AvrcTgUnitPacket() : label_(AVRC_DEFAULT_LABEL)
27 {
28     HILOGI("enter");
29 }
30 
AvrcTgUnitPacket(Packet * pkt,uint8_t label)31 AvrcTgUnitPacket::AvrcTgUnitPacket(Packet *pkt, uint8_t label) : label_(label)
32 {
33     HILOGI("label:%{public}d", label);
34 
35     label_ = label;
36 
37     DisassemblePacket(pkt);
38 }
39 
~AvrcTgUnitPacket(void)40 AvrcTgUnitPacket::~AvrcTgUnitPacket(void)
41 {
42     HILOGI("enter");
43 
44     if (pkt_ != nullptr) {
45         PacketFree(pkt_);
46         pkt_ = nullptr;
47     }
48 }
49 
AssemblePacket(void)50 const Packet *AvrcTgUnitPacket::AssemblePacket(void)
51 {
52     HILOGI("enter");
53 
54     pkt_ = PacketMalloc(0x00, 0x00, AVRC_TG_UNIT_RESPONSE_SIZE);
55     auto buffer = static_cast<uint8_t *>(BufferPtr(PacketContinuousPayload(pkt_)));
56     ASSERT(buffer != nullptr);
57     uint16_t offset = 0x00;
58     offset += PushOctets1((buffer + offset), crCode_);
59     offset += PushOctets1((buffer + offset), (subunitType_ << AVRC_TG_OFFSET_THREE_BITS) | subunitId_);
60     offset += PushOctets1((buffer + offset), opCode_);
61     offset += PushOctets1((buffer + offset), AVRC_TG_UNIT_OCTET_3);
62     offset += PushOctets1((buffer + offset), (unit_type_ << AVRC_TG_OFFSET_THREE_BITS) | unit_);
63     PushOctets3((buffer + offset), companyId_);
64 
65     return pkt_;
66 }
67 
DisassemblePacket(Packet * pkt)68 bool AvrcTgUnitPacket::DisassemblePacket(Packet *pkt)
69 {
70     HILOGI("enter");
71 
72     isValid_ = false;
73     size_t size = PacketPayloadSize(pkt);
74     if (size >= AVRC_TG_UNIT_COMMAND_SIZE) {
75         isValid_ = true;
76     } else {
77         crCode_ = AVRC_TG_RSP_CODE_REJECTED;
78         HILOGI("The size of the packet is invalid! actual size: %{public}zu valid min size: %{public}u",
79             size, AVRC_TG_UNIT_COMMAND_SIZE);
80     }
81 
82     return isValid_;
83 }
84 }  // namespace bluetooth
85 }  // namespace OHOS
86