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_TG_PACKET_H
17 #define AVRCP_TG_PACKET_H
18 
19 #include "avrcp_tg_internal.h"
20 #include "packet.h"
21 
22 namespace OHOS {
23 namespace bluetooth {
24 const uint32_t AVRC_TG_INVALID_BLUETOOTH_SIG_COMPANY_ID = 0xFFFFFF;
25 
26 /**
27  * @brief This enumeration declares the values of the frame.
28  */
29 enum AvrcTgAvcCommon {
30     AVRC_TG_AVC_COMMON_SUBUNIT_TYPE = 0x09,          // The value of the "Subunit_type".
31     AVRC_TG_AVC_COMMON_SUBUNIT_ID = 0x00,            // The value of the "Subunit_ID".
32     AVRC_TG_AVC_COMMON_PARAMETER_LENGTH = 0x00,      // The value of the "Parameter Length".
33     AVRC_TG_AVC_COMMON_PACKET_TYPE = 0x00,           // The value of the "Packet type".
34     AVRC_TG_AVC_COMMON_PACKET_TYPE_START = 0x01,     // The value of the "Start".
35     AVRC_TG_AVC_COMMON_PACKET_TYPE_CONTINUE = 0x02,  // The value of the "Continue".
36     AVRC_TG_AVC_COMMON_PACKET_TYPE_END = 0x03,       // The value of the "End".
37     AVRC_TG_AVC_COMMON_CTYPE_OFFSET = 0x00,          // The offset of the "ctype".
38     AVRC_TG_AVC_COMMON_OPCODE_OFFSET = 0x02,         // The offset of the "opcode".
39     AVRC_TG_AVC_COMMON_COMPANY_ID_OFFSET = 0x03,     // The offset of the "Company ID".
40     AVRC_TG_AVC_COMMON_VENDOR_PDU_ID_OFFSET = 0x06,  // The offset of the "PDU ID".
41     AVRC_TG_AVC_COMMON_BROWSE_PDU_ID_OFFSET = 0x00,  // The offset of the "PDU ID".
42 };
43 
44 /**
45  * @brief This enumeration declares the values of the "ctype" and "response".
46  */
47 enum AvrcTgCrCode {
48     AVRC_TG_CMD_CODE_CONTROL = 0x00,          // Control command.
49     AVRC_TG_CMD_CODE_STATUS = 0x01,           // Status command.
50     AVRC_TG_CMD_CODE_NOTIFY = 0x03,           // Notify command.
51     AVRC_TG_RSP_CODE_NOT_IMPLEMENTED = 0x08,  // Not implemented command.
52     AVRC_TG_RSP_CODE_ACCEPTED = 0x09,         // Accepted command.
53     AVRC_TG_RSP_CODE_REJECTED = 0x0A,         // Rejected command.
54     AVRC_TG_RSP_CODE_STABLE = 0x0C,           // Status stable.
55     AVRC_TG_RSP_CODE_CHANGED = 0x0D,          // Status changed.
56     AVRC_TG_RSP_CODE_INTERIM = 0x0F,          // Interim response.
57 };
58 
59 /**
60  * @brief This enumeration declares the values of the "Opcode".
61  */
62 enum AvrcTgOpCode {
63     AVRC_TG_OP_CODE_VENDOR = 0x00,         // Vendor Dependent command.
64     AVRC_TG_OP_CODE_UNIT_INFO = 0x30,      // Unit Info command.
65     AVRC_TG_OP_CODE_SUB_UNIT_INFO = 0x31,  // Subunit Info command.
66     AVRC_TG_OP_CODE_PASS_THROUGH = 0x7C,   // Pass Through command.
67     // @see AV/C Digital Interface Command Set Version 1.0 Section 5.AV/C frames - Operation(opcode).
68     AVRC_TG_OP_CODE_INVALID = 0xFF,
69 };
70 
71 /**
72  * @brief This enumeration declares the values of the "PDU ID".
73  */
74 enum AvrcTgPduId {
75     AVRC_TG_PDU_ID_GET_CAPABILITIES = 0x10,                               // AV/C STATUS
76     AVRC_TG_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_ATTRIBUTES = 0x11,     // AV/C STATUS
77     AVRC_TG_PDU_ID_LIST_PLAYER_APPLICATION_SETTING_VALUES = 0x12,         // AV/C STATUS
78     AVRC_TG_PDU_ID_GET_CURRENT_PLAYER_APPLICATION_SETTING_VALUE = 0x13,   // AV/C STATUS
79     AVRC_TG_PDU_ID_SET_PLAYER_APPLICATION_SETTING_VALUE = 0x14,           // AV/C CONTROL
80     AVRC_TG_PDU_ID_GET_PLAYER_APPLICATION_SETTING_ATTRIBUTE_TEXT = 0x15,  // AV/C STATUS
81     AVRC_TG_PDU_ID_GET_PLAYER_APPLICATION_SETTING_VALUE_TEXT = 0x16,      // AV/C STATUS
82     AVRC_TG_PDU_ID_GET_ELEMENT_ATTRIBUTES = 0x20,                         // AV/C STATUS
83     AVRC_TG_PDU_ID_GET_PLAY_STATUS = 0x30,                                // AV/C STATUS
84     AVRC_TG_PDU_ID_REGISTER_NOTIFICATION = 0x31,                          // AV/C NOTIFY
85     AVRC_TG_PDU_ID_REQUEST_CONTINUING_RESPONSE = 0x40,                    // AV/C CONTROL
86     AVRC_TG_PDU_ID_ABORT_CONTINUING_RESPONSE,                             // AV/C CONTROL
87     AVRC_TG_PDU_ID_SET_ABSOLUTE_VOLUME = 0x50,                            // AV/C CONTROL
88     AVRC_TG_PDU_ID_SET_ADDRESSED_PLAYER = 0x60,                           // AV/C CONTROL
89     AVRC_TG_PDU_ID_SET_BROWSED_PLAYER = 0x70,                             // Browsing
90     AVRC_TG_PDU_ID_GET_FOLDER_ITEMS = 0x71,                               // Browsing
91     AVRC_TG_PDU_ID_CHANGE_PATH = 0x72,                                    // Browsing
92     AVRC_TG_PDU_ID_GET_ITEM_ATTRIBUTES = 0x73,                            // Browsing
93     AVRC_TG_PDU_ID_PLAY_ITEM = 0x74,                                      // AV/C CONTROL
94     AVRC_TG_PDU_ID_GET_TOTAL_NUMBER_OF_ITEMS = 0x75,                      // Browsing
95     AVRC_TG_PDU_ID_ADD_TO_NOW_PLAYING = 0x90,                             // AV/C CONTROL
96     AVRC_TG_PDU_ID_GENERAL_REJECT = 0xa0,                                 // Browsing
97     AVRC_TG_PDU_ID_INVALID = 0xFF,
98 };
99 
100 /**
101  * @brief This enumeration declares a set of values associated with the <b>GetCapability<b>
102  * frame.
103  */
104 enum AvrcTgGcCapabilityId : uint8_t {
105     AVRC_TG_GC_CAPABILITY_INVALID = 0x00,
106     AVRC_TG_GC_CAPABILITY_COMPANY = 0x02,
107     AVRC_TG_GC_CAPABILITY_EVENT = 0x03,
108 };
109 
110 /**
111  * @brief The class provides a set of methods for pushing / popping the payload of the frame.
112  */
113 class AvrcTgPacket {
114 public:
115     /**
116      * @brief A constructor used to create an <b>AvrcTgPacket</b> instance.
117      */
118     AvrcTgPacket(void) = default;
119 
120     /**
121      * @brief A destructor used to delete the <b>AvrcTgPacket</b> instance.
122      */
123     virtual ~AvrcTgPacket(void) = default;
124 
125     /**
126      * @brief Pushes one octet into the specified memory.
127      *
128      * @param[out] pkt     The memory that need to push the payload.
129      * @param[in]  payload The payload that need to be pushed.
130      * @return The number of octets that is pushed into the specified memory.
131      */
132     virtual uint16_t PushOctets1(uint8_t *pkt, uint8_t payload) final;
133 
134     /**
135      * @brief Pushes two octets into the specified memory.
136      *
137      * @param[out] pkt     The memory that need to push the payload.
138      * @param[in]  payload The payload that need to be pushed.
139      * @return The number of octets that is pushed into the specified memory.
140      */
141     virtual uint16_t PushOctets2(uint8_t *pkt, uint16_t payload) final;
142 
143     /**
144      * @brief Pushes three octets into the specified memory.
145      *
146      * @param[out] pkt     The memory that need to push the payload.
147      * @param[in]  payload The payload that need to be pushed.
148      * @return The number of octets that is pushed into the specified memory.
149      */
150     virtual uint16_t PushOctets3(uint8_t *pkt, uint32_t payload) final;
151 
152     /**
153      * @brief Pushes four octets into the specified memory.
154      *
155      * @param[out] pkt     The memory that need to push the payload.
156      * @param[in]  payload The payload that need to be pushed.
157      * @return The number of octets that is pushed into the specified memory.
158      */
159     virtual uint16_t PushOctets4(uint8_t *pkt, uint32_t payload) final;
160 
161     /**
162      * @brief Pushes eight octets into the specified memory.
163      *
164      * @param[out] pkt     The memory that need to push the payload.
165      * @param[in]  payload The payload that need to be pushed.
166      * @return The number of octets that is pushed into the specified memory.
167      */
168     virtual uint16_t PushOctets8(uint8_t *pkt, uint64_t payload) final;
169 
170     /**
171      * @brief Pushes the octets into the specified memory.
172      *
173      * @param[out] pkt     The memory that need to push the payload.
174      * @param[in]  payload The payload that need to be pushed.
175      * @return The number of octets that is pushed into the specified memory.
176      */
177     virtual uint16_t PushOctets(uint8_t *pkt, uint8_t *playload, uint16_t length) final;
178 
179     /**
180      * @brief Pops one octet from the specified memory.
181      *
182      * @param[in]  pkt     The memory that need to pop the payload.
183      * @param[out] payload The store that saves the payload.
184      * @return The number of octets that is popped from the specified memory.
185      */
186     virtual uint16_t PopOctets1(uint8_t *pkt, uint64_t &payload) final;
187 
188     /**
189      * @brief Pops two octets from the specified memory.
190      *
191      * @param[in]  pkt     The memory that need to pop the payload.
192      * @param[out] payload The store that saves the payload.
193      * @return The number of octets that is popped from the specified memory.
194      */
195     virtual uint16_t PopOctets2(uint8_t *pkt, uint64_t &payload) final;
196 
197     /**
198      * @brief Pops three octets from the specified memory.
199      *
200      * @param[in]  pkt     The memory that need to pop the payload.
201      * @param[out] payload The store that saves the payload.
202      * @return The number of octets that is popped from the specified memory.
203      */
204     virtual uint16_t PopOctets3(uint8_t *pkt, uint64_t &payload) final;
205 
206     /**
207      * @brief Pops four octets from the specified memory.
208      *
209      * @param[in]  pkt     The memory that need to pop the payload.
210      * @param[out] payload The store that saves the payload.
211      * @return The number of octets that is popped from the specified memory.
212      */
213     virtual uint16_t PopOctets4(uint8_t *pkt, uint64_t &payload) final;
214 
215     /**
216      * @brief Pops eight octets from the specified memory.
217      *
218      * @param[in]  pkt     The memory that need to pop the payload.
219      * @param[out] payload The store that saves the payload.
220      * @return The number of octets that is popped from the specified memory.
221      */
222     virtual uint16_t PopOctets8(uint8_t *pkt, uint64_t &payload) final;
223 
224     /**
225      * @brief Pops octets from the specified memory.
226      *
227      * @param[in]  pkt     The memory that need to pop the payload.
228      * @param[out] payload The store that saves the payload.
229      * @return The number of octets that is popped from the specified memory.
230      */
231     virtual uint16_t PopOctets(uint8_t *pkt, uint8_t *playload, uint16_t length) final;
232 
233     /**
234      * @brief Gets the operation code of the specified command frame.
235      *
236      * @param[in] pkt The packet of the specified command frame.
237      * @return The operation code of the specified command frame.
238      */
239     static uint8_t GetOpCode(Packet *pkt);
240 
241     /**
242      * @brief Gets the PDU ID of the VENDOR DEPENDENT command frame.
243      *
244      * @param[in] pkt he packet of the VENDOR DEPENDENT command frame.
245      * @return The PDU ID of the VENDOR DEPENDENT command frame.
246      */
247     static uint8_t GetVendorPdu(Packet *pkt);
248 
249     /**
250      * @brief Gets the PDU ID of the BROWSING command frame.
251      *
252      * @param[in] pkt The packet of the BROWSING command frame.
253      * @return The PDU ID of the BROWSING command frame.
254      */
255     static uint8_t GetBrowsePdu(Packet *pkt);
256 
257     /**
258      * @brief Checks the opcode is valid or not.
259      *
260      * @param[in] code The operation code of the specified command frame
261      * @return @c true  The opcode is valid.
262      *         @c false The opcode is invalid.
263      */
264     static bool IsValidOpCode(uint8_t code);
265 
266     /**
267      * @brief Assembles the payload into the specified response frame.
268      *
269      * @param[in] pkt The memory that need to push the payload.
270      * @return The packet of the specified response frame.
271      */
272     virtual const Packet *AssemblePacket(void) = 0;
273 
274     /**
275      * @brief Disassembles the payload from the specified command frame.
276      *
277      * @param[in] pkt The memory that need to pop the payload.
278      * @return @c true  The packet is valid.
279      *         @c false The packet is invalid.
280      */
281     virtual bool DisassemblePacket(Packet *pkt) = 0;
282 
283 private:
284     /**
285      * @brief Pushes specific octets into specific memory.
286      *
287      * @param[out] pkt     The memory that need to push the payload.
288      * @param[in]  size    The size of the payload need to be pushed.
289      * @param[in]  payload The store that saves the payload.
290      * @return The number of octets that is pushed into the specified memory.
291      */
292     static uint16_t PushOctetsUpto8(uint8_t *pkt, size_t size, uint64_t payload);
293 
294     /**
295      * @brief Pops specific octets into specific memory.
296      *
297      * @param[out] pkt     The memory that need to pop the payload.
298      * @param[in]  size    The size of the payload need to be popped.
299      * @param[in]  payload The store that saves the payload.
300      * @return The number of octets that is popped into the specified memory.
301      */
302     static uint16_t PopOctetsUpto8(uint8_t *pkt, size_t size, uint64_t &payload);
303 };
304 }  // namespace bluetooth
305 }  // namespace OHOS
306 #endif  // !AVRCP_TG_PACKET_H
307