• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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_unit_info.h"
17 
18 namespace OHOS {
19 namespace bluetooth {
AvrcCtUnitPacket(void)20 AvrcCtUnitPacket::AvrcCtUnitPacket(void)
21 {
22     HILOGI("enter");
23 }
24 
AvrcCtUnitPacket(Packet * pkt)25 AvrcCtUnitPacket::AvrcCtUnitPacket(Packet *pkt)
26 {
27     HILOGI("enter");
28 
29     DisassemblePacket(pkt);
30 }
31 
~AvrcCtUnitPacket(void)32 AvrcCtUnitPacket::~AvrcCtUnitPacket(void)
33 {
34     HILOGI("enter");
35 
36     if (pkt_ != nullptr) {
37         PacketFree(pkt_);
38         pkt_ = nullptr;
39     }
40 }
41 
AssemblePacket(void)42 const Packet *AvrcCtUnitPacket::AssemblePacket(void)
43 {
44     HILOGI("enter");
45 
46     pkt_ = PacketMalloc(0x00, 0x00, AVRC_CT_UNIT_COMMAND_SIZE);
47     auto buffer = static_cast<uint8_t *>(BufferPtr(PacketContinuousPayload(pkt_)));
48 
49     uint16_t offset = 0x00;
50     offset += PushOctets1((buffer + offset), crCode_);
51     offset += PushOctets1((buffer + offset), (subunitType_ << AVRC_CT_UNIT_MOVE_BIT_3) | subunitId_);
52     offset += PushOctets1((buffer + offset), opCode_);
53     offset += PushOctets1((buffer + offset), AVRC_CT_UNIT_OCTET_3);
54     offset += PushOctets1((buffer + offset), AVRC_CT_UNIT_OCTET_3);
55     offset += PushOctets1((buffer + offset), AVRC_CT_UNIT_OCTET_3);
56     offset += PushOctets1((buffer + offset), AVRC_CT_UNIT_OCTET_3);
57     PushOctets1((buffer + offset), AVRC_CT_UNIT_OCTET_3);
58 
59     return pkt_;
60 }
61 
DisassemblePacket(Packet * pkt)62 bool AvrcCtUnitPacket::DisassemblePacket(Packet *pkt)
63 {
64     HILOGI("enter");
65 
66     isValid_ = false;
67     size_t size = PacketPayloadSize(pkt);
68     if (size >= AVRC_CT_UNIT_RESPONSE_SIZE) {
69         auto buffer = static_cast<uint8_t *>(BufferPtr(PacketContinuousPayload(pkt)));
70 
71         uint16_t offset = AVRC_CT_AVC_COMMON_CTYPE_OFFSET;
72         uint64_t payload = 0x00;
73         PopOctets1((buffer + offset), payload);
74         crCode_ = static_cast<uint8_t>(payload) & 0b00001111;
75 
76         offset = AVRC_CT_UNIT_COMPANY_ID_OFFSET;
77         PopOctets3((buffer + offset), payload);
78         companyId_ = static_cast<uint32_t>(payload);
79         isValid_ = true;
80     } else {
81         crCode_ = AVRC_CT_RSP_CODE_REJECTED;
82         HILOGI("The size of the packet is invalid!");
83     }
84 
85     return isValid_;
86 }
87 }  // namespace bluetooth
88 }  // namespace OHOS
89