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_ct_sub_unit_info.h"
17 #include "avrcp_ct_internal.h"
18
19 namespace OHOS {
20 namespace bluetooth {
AvrcCtSubUnitPacket()21 AvrcCtSubUnitPacket::AvrcCtSubUnitPacket()
22 {
23 HILOGI("enter");
24
25 subunitType_ = AVRC_CT_SUB_UNIT_SUBUNIT_TYPE_UNIT;
26 subunitId_ = AVRC_CT_SUB_UNIT_SUBUNIT_ID_IGNORE;
27 opCode_ = AVRC_CT_OP_CODE_SUB_UNIT_INFO;
28 }
29
AvrcCtSubUnitPacket(Packet * pkt)30 AvrcCtSubUnitPacket::AvrcCtSubUnitPacket(Packet *pkt)
31 {
32 HILOGI("enter");
33
34 subunitType_ = AVRC_CT_SUB_UNIT_SUBUNIT_TYPE_UNIT;
35 subunitId_ = AVRC_CT_SUB_UNIT_SUBUNIT_ID_IGNORE;
36 opCode_ = AVRC_CT_OP_CODE_SUB_UNIT_INFO;
37
38 DisassemblePacket(pkt);
39 }
40
~AvrcCtSubUnitPacket(void)41 AvrcCtSubUnitPacket::~AvrcCtSubUnitPacket(void)
42 {
43 HILOGI("enter");
44
45 if (pkt_ != nullptr) {
46 PacketFree(pkt_);
47 pkt_ = nullptr;
48 }
49 }
50
AssemblePacket(void)51 const Packet *AvrcCtSubUnitPacket::AssemblePacket(void)
52 {
53 HILOGI("enter");
54
55 pkt_ = PacketMalloc(0x00, 0x00, AVRC_CT_SUB_UNIT_COMMAND_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_CT_SUB_UNIT_MOVE_BIT_3) | subunitId_);
61 offset += PushOctets1((buffer + offset), opCode_);
62 offset += PushOctets1(
63 (buffer + offset), ((page_ & 0b00000111) << AVRC_CT_SUB_UNIT_MOVE_BIT_4) | (extentionCode_ & 0b00000111));
64 offset += PushOctets1((buffer + offset), AVRC_CT_SUB_UNIT_OCTET_4);
65 offset += PushOctets1((buffer + offset), AVRC_CT_SUB_UNIT_OCTET_4);
66 offset += PushOctets1((buffer + offset), AVRC_CT_SUB_UNIT_OCTET_4);
67 PushOctets1((buffer + offset), AVRC_CT_SUB_UNIT_OCTET_4);
68
69 return pkt_;
70 }
71
DisassemblePacket(Packet * pkt)72 bool AvrcCtSubUnitPacket::DisassemblePacket(Packet *pkt)
73 {
74 HILOGI("enter");
75
76 bool isValid = false;
77 size_t size = PacketPayloadSize(pkt);
78 if (size >= AVRC_CT_SUB_UNIT_RESPONSE_SIZE) {
79 auto buffer = static_cast<uint8_t *>(BufferPtr(PacketContinuousPayload(pkt)));
80
81 uint16_t offset = AVRC_CT_AVC_COMMON_CTYPE_OFFSET;
82 uint64_t payload = 0x00;
83 PopOctets1((buffer + offset), payload);
84 crCode_ = static_cast<uint8_t>(payload) & 0b00001111;
85 isValid = true;
86 } else {
87 crCode_ = AVRC_CT_RSP_CODE_REJECTED;
88 HILOGI("The size of the packet is invalid! actual size: %{public}zu, valid min size: %{public}d",
89 size, AVRC_CT_SUB_UNIT_RESPONSE_SIZE);
90 }
91
92 return isValid;
93 }
94 } // namespace bluetooth
95 } // namespace OHOS
96