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 #ifndef AVRCP_CT_PACKET_H
17 #define AVRCP_CT_PACKET_H
18 
19 #include "avrcp_ct_internal.h"
20 
21 namespace OHOS {
22 namespace bluetooth {
23 const uint32_t AVRCP_CT_INVALID_BLUETOOTH_SIG_COMPANY_ID = 0xFFFFFF;
24 
25 /**
26  * @brief This enumeration declares the values of the frame.
27  */
28 enum AvrcCtAvcCommon {
29     AVRC_CT_AVC_COMMON_SUBUNIT_TYPE = 0x09,          // The value of the "Subunit_type".
30     AVRC_CT_AVC_COMMON_SUBUNIT_ID = 0x00,            // The value of the "Subunit_ID".
31     AVRC_CT_AVC_COMMON_PARAMETER_LENGTH = 0x00,      // The value of the "Parameter Length".
32     AVRC_CT_AVC_COMMON_PACKET_TYPE = 0x00,           // The value of the "Packet type".
33     AVRC_CT_AVC_COMMON_PACKET_TYPE_START = 0x01,     // The value of the "Start".
34     AVRC_CT_AVC_COMMON_PACKET_TYPE_CONTINUE = 0x02,  // The value of the "Continue".
35     AVRC_CT_AVC_COMMON_PACKET_TYPE_END = 0x03,       // The value of the "End".
36     AVRC_CT_AVC_COMMON_CTYPE_OFFSET = 0x00,          // The offset of the "ctype".
37     AVRC_CT_AVC_COMMON_OPCODE_OFFSET = 0x02,         // The offset of the "opcode".
38     AVRC_CT_AVC_COMMON_COMPANY_ID_OFFSET = 0x03,     // The offset of the "Company ID".
39     AVRC_CT_AVC_COMMON_VENDOR_PDU_ID_OFFSET = 0x06,  // The offset of the "PDU ID".
40     AVRC_CT_AVC_COMMON_BROWSE_PDU_ID_OFFSET = 0x00,  // The offset of the "PDU ID".
41 };
42 
43 /**
44  * @brief This enumeration declares the values of the "ctype" and "response".
45  */
46 enum AvrcCtCrCode : uint8_t {
47     AVRC_CT_CMD_CODE_CONTROL = 0x00,          // Control command.
48     AVRC_CT_CMD_CODE_STATUS = 0x01,           // Status command.
49     AVRC_CT_CMD_CODE_NOTIFY = 0x03,           // Notify command.
50     AVRC_CT_RSP_CODE_NOT_IMPLEMENTED = 0x08,  // Not implemented command.
51     AVRC_CT_RSP_CODE_ACCEPTED = 0x09,         // Accepted command.
52     AVRC_CT_RSP_CODE_REJECTED = 0x0A,         // Rejected command.
53     AVRC_CT_RSP_CODE_STABLE = 0x0C,           // Status stable.
54     AVRC_CT_RSP_CODE_CHANGED = 0x0D,          // Notify changed.
55     AVRC_CT_RSP_CODE_INTERIM = 0x0F,          // Interim response.
56 };
57 
58 /**
59  * @brief This enumeration declares the values of the "Opcode".
60  */
61 enum AvrcCtOpCode : uint8_t {
62     AVRC_CT_OP_CODE_VENDOR = 0x00,         // Vendor Dependent command.
63     AVRC_CT_OP_CODE_UNIT_INFO = 0x30,      // Unit Info command.
64     AVRC_CT_OP_CODE_SUB_UNIT_INFO = 0x31,  // Subunit Info command.
65     AVRC_CT_OP_CODE_PASS_THROUGH = 0x7C,   // Pass Through command.
66     // @see AV/C Digital Interface Command Set Version 1.0 Section 5.AV/C frames - Operation(opcode).
67     AVRC_CT_OP_CODE_INVALID = 0xFF,
68 };
69 
70 /**
71  * @brief This enumeration declares the values of the "PDU ID".
72  */
73 enum AvrcCtPduId : uint8_t {
74     AVRC_CT_PDU_ID_GET_CAPABILITIES = 0x10,                               // AV/C STATUS
75     AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES = 0x11,     // AV/C STATUS
76     AVRC_CT_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_VALUES = 0x12,         // AV/C STATUS
77     AVRC_CT_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE = 0x13,   // AV/C STATUS
78     AVRC_CT_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE = 0x14,           // AV/C CONTROL
79     AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT = 0x15,  // AV/C STATUS
80     AVRC_CT_PDU_ID_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT = 0x16,      // AV/C STATUS
81     AVRC_CT_PDU_ID_GET_ELEMENT_ATTRIBUTES = 0x20,                         // AV/C STATUS
82     AVRC_CT_PDU_ID_GET_PLAY_STATUS = 0x30,                                // AV/C STATUS
83     AVRC_CT_PDU_ID_REGISTER_NOTIFICATION = 0x31,                          // AV/C NOTIFY
84     AVRC_CT_PDU_ID_REQUEST_CONTINUING_RESPONSE = 0x40,                    // AV/C CONTROL
85     AVRC_CT_PDU_ID_ABORT_CONTINUING_RESPONSE,                             // AV/C CONTROL
86     AVRC_CT_PDU_ID_SET_ABSOLUTE_VOLUME = 0x50,                            // AV/C CONTROL
87     AVRC_CT_PDU_ID_SET_ADDRESSED_PLAYER = 0x60,                           // AV/C CONTROL
88     AVRC_CT_PDU_ID_SET_BROWSED_PLAYER = 0x70,                             // Browsing
89     AVRC_CT_PDU_ID_GET_FOLDER_ITEMS = 0x71,                               // Browsing
90     AVRC_CT_PDU_ID_CHANGE_PATH = 0x72,                                    // Browsing
91     AVRC_CT_PDU_ID_GET_ITEM_ATTRIBUTES = 0x73,                            // Browsing
92     AVRC_CT_PDU_ID_PLAY_ITEM = 0x74,                                      // AV/C CONTROL
93     AVRC_CT_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS = 0x75,                      // Browsing
94     AVRC_CT_PDU_ID_ADD_TO_NOW_PLAYING = 0x90,                             // AV/C CONTROL
95     AVRC_CT_PDU_ID_GENERAL_REJECT = 0xa0,                                 // Browsing
96     AVRC_CT_PDU_ID_INVALID = 0xFF,
97 };
98 
99 /**
100  * @brief This enumeration declares a set of values associated with the <b>GetCapability<b>
101  */
102 enum AvrcCtGcCapabilityId : uint8_t {
103     AVRC_CT_GC_CAPABILITY_INVALID = 0x00,
104     AVRC_CT_GC_CAPABILITY_COMPANY = 0x02,
105     AVRC_CT_GC_CAPABILITY_EVENT = 0x03,
106 };
107 
108 /**
109  * @brief This enumeration declares the value of the uint8_t number
110  * frame.
111  */
112 enum AvrcCtUnit8Num : uint8_t {
113     AVRC_CT_UNIT8_NUM_1 = 0x01,
114     AVRC_CT_UNIT8_NUM_2 = 0x02,
115     AVRC_CT_UNIT8_NUM_3 = 0x03,
116     AVRC_CT_UNIT8_NUM_4 = 0x04,
117     AVRC_CT_UNIT8_NUM_8 = 0x08,
118 };
119 /**
120  * @brief The class provides a set of methods for pushing / popping the payload of the frame.
121  */
122 class AvrcCtPacket {
123 public:
124     /**
125      * @brief A constructor used to create an <b>AvrcCtPacket</b> instance.
126      *
127      * @since 6.0
128      */
129     AvrcCtPacket(void) = default;
130 
131     /**
132      * @brief A destructor used to delete the <b>AvrcCtPacket</b> instance.
133      */
134     virtual ~AvrcCtPacket(void) = default;
135 
136     /**
137      * @brief Pushes one octet into the specified memory.
138      *
139      * @param[out] pkt     The memory that need to push the payload.
140      * @param[in]  payload The payload that need to be pushed.
141      * @return The number of octets that is pushed into the specified memory.
142      */
143     virtual uint16_t PushOctets1(uint8_t *pkt, uint8_t payload) const final;
144 
145     /**
146      * @brief Pushes two octets into the specified memory.
147      *
148      * @param[out] pkt     The memory that need to push the payload.
149      * @param[in]  payload The payload that need to be pushed.
150      * @return The number of octets that is pushed into the specified memory.
151      */
152     virtual uint16_t PushOctets2(uint8_t *pkt, uint16_t payload) const final;
153 
154     /**
155      * @brief Pushes three octets into the specified memory.
156      *
157      * @param[out] pkt     The memory that need to push the payload.
158      * @param[in]  payload The payload that need to be pushed.
159      * @return The number of octets that is pushed into the specified memory.
160      */
161     virtual uint16_t PushOctets3(uint8_t *pkt, uint32_t payload) const final;
162 
163     /**
164      * @brief Pushes four octets into the specified memory.
165      *
166      * @param[out] pkt     The memory that need to push the payload.
167      * @param[in]  payload The payload that need to be pushed.
168      * @return The number of octets that is pushed into the specified memory.
169      */
170     virtual uint16_t PushOctets4(uint8_t *pkt, uint32_t payload) const final;
171 
172     /**
173      * @brief Pushes eight octets into the specified memory.
174      *
175      * @param[out] pkt     The memory that need to push the payload.
176      * @param[in]  payload The payload that need to be pushed.
177      * @return The number of octets that is pushed into the specified memory.
178      */
179     virtual uint16_t PushOctets8(uint8_t *pkt, uint64_t payload) const final;
180 
181     /**
182      * @brief Pushes the octets into the specified memory.
183      *
184      * @param[out] pkt     The memory that need to push the payload.
185      * @param[in]  payload The payload that need to be pushed.
186      * @return The number of octets that is pushed into the specified memory.
187      */
188     virtual uint16_t PushOctets(uint8_t *pkt, uint8_t *playload, uint16_t length) final;
189 
190     /**
191      * @brief Pops one octet from the specified memory.
192      *
193      * @param[in]  pkt     The memory that need to pop the payload.
194      * @param[out] payload The store that saves the payload.
195      * @return The number of octets that is popped from the specified memory.
196      */
197     virtual uint16_t PopOctets1(uint8_t *pkt, uint64_t &payload) const final;
198 
199     /**
200      * @brief Pops two octets from the specified memory.
201      *
202      * @param[in]  pkt     The memory that need to pop the payload.
203      * @param[out] payload The store that saves the payload.
204      * @return The number of octets that is popped from the specified memory.
205      */
206     virtual uint16_t PopOctets2(uint8_t *pkt, uint64_t &payload) const final;
207 
208     /**
209      * @brief Pops three octets from the specified memory.
210      *
211      * @param[in]  pkt     The memory that need to pop the payload.
212      * @param[out] payload The store that saves the payload.
213      * @return The number of octets that is popped from the specified memory.
214      */
215     virtual uint16_t PopOctets3(uint8_t *pkt, uint64_t &payload) const final;
216 
217     /**
218      * @brief Pops four octets from the specified memory.
219      *
220      * @param[in]  pkt     The memory that need to pop the payload.
221      * @param[out] payload The store that saves the payload.
222      * @return The number of octets that is popped from the specified memory.
223      */
224     virtual uint16_t PopOctets4(uint8_t *pkt, uint64_t &payload) const final;
225 
226     /**
227      * @brief Pops eight octets from the specified memory.
228      *
229      * @param[in]  pkt     The memory that need to pop the payload.
230      * @param[out] payload The store that saves the payload.
231      * @return The number of octets that is popped from the specified memory.
232      */
233     virtual uint16_t PopOctets8(uint8_t *pkt, uint64_t &payload) const final;
234 
235     /**
236      * @brief Pops octets from the specified memory.
237      *
238      * @param[in]  pkt     The memory that need to pop the payload.
239      * @param[out] payload The store that saves the payload.
240      * @return The number of octets that is popped from the specified memory.
241      */
242     virtual uint16_t PopOctets(uint8_t *pkt, uint8_t *playload, uint16_t length) final;
243 
244     /**
245      * @brief Gets the operation code of the specified command frame.
246      *
247      * @param[in] pkt The packet of the specified command frame.
248      * @return The operation code of the specified command frame.
249      */
250     static uint8_t GetOpCode(Packet *pkt);
251 
252     /**
253      * @brief Gets the PDU ID of the VENDOR DEPENDENT command frame.
254      *
255      * @param[in] pkt he packet of the VENDOR DEPENDENT command frame.
256      * @return The PDU ID of the VENDOR DEPENDENT command frame.
257      */
258     static uint8_t GetVendorPdu(Packet *pkt);
259 
260     /**
261      * @brief Gets the PDU ID of the BROWSING command frame.
262      *
263      * @param[in] pkt The packet of the BROWSING command frame.
264      * @return The PDU ID of the BROWSING command frame.
265      */
266     static uint8_t GetBrowsePdu(Packet *pkt);
267 
268     /**
269      * @brief Checks the opcode is valid or not.
270      *
271      * @param[in] code The operation code of the specified command frame
272      * @return @c true  The opcode is valid.
273      *         @c false The opcode is invalid.
274      */
275     static bool IsValidOpCode(uint8_t code);
276 
277     /**
278      * @brief Assembles the payload into the specified response frame.
279      *
280      * @param[in] pkt The memory that need to push the payload.
281      * @return The packet of the specified response frame.
282      */
283     virtual const Packet *AssemblePacket(void) = 0;
284 
285     /**
286      * @brief Disassembles the payload from the specified command frame.
287      *
288      * @param[in] pkt The memory that need to pop the payload.
289      * @return @c true  The packet is valid.
290      *         @c false The packet is invalid.
291      */
292     virtual bool DisassemblePacket(Packet *pkt) = 0;
293 
294 private:
295     /**
296      * @brief Pushes specific octets into specific memory.
297      *
298      * @param[out] pkt     The memory that need to push the payload.
299      * @param[in]  size    The size of the payload need to be pushed.
300      * @param[in]  payload The store that saves the payload.
301      * @return The number of octets that is pushed into the specified memory.
302      */
303     static uint16_t PushOctetsUpto8(uint8_t *pkt, size_t size, uint64_t payload);
304 
305     /**
306      * @brief Pops specific octets into specific memory.
307      *
308      * @param[out] pkt     The memory that need to pop the payload.
309      * @param[in]  size    The size of the payload need to be popped.
310      * @param[in]  payload The store that saves the payload.
311      * @return The number of octets that is popped into the specified memory.
312      */
313     static uint16_t PopOctetsUpto8(uint8_t *pkt, size_t size, uint64_t &payload);
314 };
315 }  // namespace bluetooth
316 }  // namespace OHOS
317 
318 #endif  // !AVRCP_CT_PACKET_H
319