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 #include "avrcp_tg_sub_unit_info.h"
17
18 namespace OHOS {
19 namespace bluetooth {
AvrcTgSubUnitPacket(void)20 AvrcTgSubUnitPacket::AvrcTgSubUnitPacket(void) : AvrcTgUnitPacket()
21 {
22 HILOGI("enter");
23
24 subunitType_ = AVRC_TG_SUB_UNIT_SUBUNIT_TYPE_UNIT;
25 subunitId_ = AVRC_TG_SUB_UNIT_SUBUNIT_ID_IGNORE;
26 opCode_ = AVRC_TG_OP_CODE_SUB_UNIT_INFO;
27 }
28
AvrcTgSubUnitPacket(Packet * pkt,uint8_t label)29 AvrcTgSubUnitPacket::AvrcTgSubUnitPacket(Packet *pkt, uint8_t label) : AvrcTgUnitPacket()
30 {
31 HILOGI("label:%{public}d", label);
32
33 subunitType_ = AVRC_TG_SUB_UNIT_SUBUNIT_TYPE_UNIT;
34 subunitId_ = AVRC_TG_SUB_UNIT_SUBUNIT_ID_IGNORE;
35 opCode_ = AVRC_TG_OP_CODE_SUB_UNIT_INFO;
36 label_ = label;
37
38 DisassemblePacket(pkt);
39 }
40
~AvrcTgSubUnitPacket(void)41 AvrcTgSubUnitPacket::~AvrcTgSubUnitPacket(void)
42 {
43 HILOGI("enter");
44
45 if (pkt_ != nullptr) {
46 PacketFree(pkt_);
47 pkt_ = nullptr;
48 }
49 }
50
AssemblePacket(void)51 const Packet *AvrcTgSubUnitPacket::AssemblePacket(void)
52 {
53 HILOGI("enter");
54
55 pkt_ = PacketMalloc(0x00, 0x00, AVRC_TG_SUB_UNIT_RESPONSE_SIZE);
56 auto buffer = static_cast<uint8_t *>(BufferPtr(PacketContinuousPayload(pkt_)));
57
58 uint16_t offset = 0x00;
59 offset += PushOctets1((buffer + offset), crCode_);
60 offset += PushOctets1((buffer + offset), (subunitType_ << AVRC_TG_OFFSET_THREE_BITS) | subunitId_);
61 offset += PushOctets1((buffer + offset), opCode_);
62 offset += PushOctets1(
63 (buffer + offset), ((page_ & 0b00000111) << AVRC_TG_OFFSET_FOUR_BITS) | (extentionCode_ & 0b00000111));
64 offset += PushOctets1((buffer + offset), AVRC_TG_SUB_UNIT_OCTET_4);
65 offset += PushOctets1((buffer + offset), AVRC_TG_SUB_UNIT_OCTET_4);
66 offset += PushOctets1((buffer + offset), AVRC_TG_SUB_UNIT_OCTET_4);
67 PushOctets1((buffer + offset), AVRC_TG_SUB_UNIT_OCTET_4);
68
69 return pkt_;
70 }
71
DisassemblePacket(Packet * pkt)72 bool AvrcTgSubUnitPacket::DisassemblePacket(Packet *pkt)
73 {
74 HILOGI("enter");
75
76 bool isValid = false;
77 size_t size = PacketPayloadSize(pkt);
78 if (size >= AVRC_TG_SUB_UNIT_COMMAND_SIZE) {
79 isValid = true;
80 } else {
81 crCode_ = AVRC_TG_RSP_CODE_REJECTED;
82 HILOGI("The size of the packet is invalid! actual size: %{public}zu valid min size: %{public}u",
83 size, AVRC_TG_SUB_UNIT_COMMAND_SIZE);
84 }
85
86 return isValid;
87 }
88 } // namespace bluetooth
89 } // namespace OHOS
90