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 #ifndef AVRCP_TG_PASS_THROUGH_H
17 #define AVRCP_TG_PASS_THROUGH_H
18 
19 #include "avrcp_tg_internal.h"
20 #include "avrcp_tg_packet.h"
21 #include "packet.h"
22 
23 namespace OHOS {
24 namespace bluetooth {
25 /**
26  * @brief This enumeration declares the values of the <b>PASS THROUGH</b> command.
27  */
28 enum AvrcTgPass {
29     AVRC_TG_PASS_COMMAND_SIZE = 0x0005,               // The size of the command frame.
30     AVRC_TG_PASS_RESPONSE_SIZE = 0x0005,              // The size of the response frame.
31     AVRC_TG_PASS_OPERATION_DATA_FIELD_LENGTH = 0x00,  // The value of "Operation data field length".
32     AVRC_TG_PASS_OPERATION_ID_OFFSET = 0x03,          // The offset of the "Operation ID".
33 };
34 
35 /**
36  * @brief This class provides a set of methods for assembling / disassembling the frame packet of the <b>PASS
37  * THROUGH</b> command.
38  * @see Audio/Video Remote Control 1.6.2 Section 4.4.1 PASS THROUGH command / 25.3 PASS THROUGH command.
39  */
40 class AvrcTgPassPacket : public AvrcTgPacket {
41 public:
42     /**
43      * @brief A constructor used to create an <b>AvrcTgPassPacket</b> instance.
44      */
45     AvrcTgPassPacket();
46 
47     /**
48      * @brief A constructor used to create an <b>AvrcTgPassPacket</b> instance.
49      *
50      * @details You can use this constructor when wants to initialize the attributes [oper], [state] and [label].
51      */
52     AvrcTgPassPacket(uint8_t oper, uint8_t state, uint8_t label);
53 
54     /**
55      * @brief A constructor used to create an <b>AvrcTgPassPacket</b> instance.
56      *
57      * @details You can use this constructor when wants to disassemble the packet.
58      */
59     AvrcTgPassPacket(Packet *pkt, uint8_t label);
60 
61     /**
62      * @brief A destructor used to delete the <b>AvrcTgPassPacket</b> instance.
63      */
64     ~AvrcTgPassPacket(void);
65 
66     /**
67      * @brief Assembles the frame packet.
68      *
69      * @details Response frame:<br>
70      *                        msb           lsb<br>
71      *                        0 0 0 0 | 0 0 0 0     response                        4 bits<br>
72      * subunit_type 5 bits    0 0 0 0 0 | 0 0 0     subunit_ID                      3 bits<br>
73      *                        0 0 0 0 | 0 0 0 0     opcode                          1 octets<br>
74      * state_flag   1 bit     0 | 0 0 0 0 0 0 0     operation_ID                    7 bits<br>
75      *                        0 0 0 0 | 0 0 0 0     operation_data_field_length     1 octets<br>
76      * @return The frame packet.
77      */
78     const Packet *AssemblePacket(void) override;
79 
80     /**
81      * @brief Disassembles the frame packet.
82      *
83      * @details Command frame:<br>
84      *                        msb           lsb<br>
85      *                        0 0 0 0 | 0 0 0 0     ctype                           4 bits<br>
86      * subunit_type 5 bits    0 0 0 0 0 | 0 0 0     subunit_ID                      3 bits<br>
87      *                        0 0 0 0 | 0 0 0 0     opcode                          1 octets<br>
88      * state_flag   1 bit     0 | 0 0 0 0 0 0 0     operation_ID                    7 bits<br>
89      *                        0 0 0 0 | 0 0 0 0     operation_data_field_length     1 octets<br>
90      * @param[in] pkt The frame packet.
91      * @return The result of the method execution.
92      * @retval true  The packet is valid.
93      * @retval false The packet is invalid.
94      */
95     bool DisassemblePacket(Packet *pkt) override;
96 
97     /**
98      * @brief Gets the "response".
99      *
100      * @return The value of the "response".
101      */
GetCrCode(void)102     uint8_t GetCrCode(void) const
103     {
104         return crCode_;
105     }
106 
107     /**
108      * @brief Sets the "response".
109      *
110      * @param[in] crCode The value of the "response".
111      */
SetCrCode(uint8_t crCode)112     void SetCrCode(uint8_t crCode)
113     {
114         crCode_ = crCode;
115     }
116 
117     /**
118      * @brief Gets the "state_flag".
119      *
120      * @return The value of the "state_flag".
121      */
GetKeyState(void)122     uint8_t GetKeyState(void) const
123     {
124         return stateFlag_;
125     };
126 
127     /**
128      * @brief Gets the "operation_ID".
129      *
130      * @return The value of the "operation_ID".
131      */
GetKeyOperation(void)132     uint8_t GetKeyOperation(void) const
133     {
134         return operationId_;
135     };
136 
137     /**
138      * @brief Gets the label.
139      *
140      * @return The value of the label.
141      */
GetLabel(void)142     uint8_t GetLabel(void) const
143     {
144         return label_;
145     }
146 
147     /**
148      * @brief Checks the frame packet is valid or not.
149      *
150      * @return The result of the method execution.
151      * @retval true  The packet is valid.
152      * @retval false The packet is invalid.
153      */
IsValid(void)154     bool IsValid(void) const
155     {
156         return isValid_;
157     }
158 
159     /**
160      * @brief Checks the "state_flag" is valid or not.
161      *
162      * @return The result of the method execution.
163      * @retval true  The key state is valid.
164      * @retval false The key state is invalid.
165      */
166     bool IsValidKeyState(void);
167 
168     /**
169      * @brief Checks the specified "state_flag" is valid or not.
170      *
171      * @param[in] state value of the "state_flag".
172      * @return The result of the method execution.
173      * @retval true  The key state is valid.
174      * @retval false The key state is invalid.
175      */
176     static bool IsValidKeyState(uint8_t state);
177 
178     /**
179      * @brief Checks the "operation_ID" is supported or not.
180      *
181      * @return The result of the method execution.
182      * @retval true  The key operation is supported.
183      * @retval false The key operation is unsupported.
184      */
185     bool IsSupportedKeyOperation(void);
186 
187     /**
188      * @brief Checks the specified "operation_ID" is supported or not.
189      *
190      * @param key The value of the "operation_ID".
191      * @return The result of the method execution.
192      * @retval true  The key operation is supported.
193      * @retval false The key operation is unsupported.
194      */
195     static bool IsSupportedKeyOperation(uint8_t key);
196 
197 private:
198     uint8_t crCode_ {AVRC_TG_RSP_CODE_ACCEPTED};             // The value of the "ctype" or the "response".
199     uint8_t subunitType_ {AVRC_TG_AVC_COMMON_SUBUNIT_TYPE};  // The value of the "Subunit_type".
200     uint8_t subunitId_ {AVRC_TG_AVC_COMMON_SUBUNIT_ID};      // The value of the "Subunit_ID".
201     uint8_t opCode_ {AVRC_TG_OP_CODE_PASS_THROUGH};          // The value of the "Opcode".
202     uint8_t stateFlag_ {AVRC_KEY_STATE_INVALID};             // The value of the "State flag".
203     uint8_t operationId_ {AVRC_KEY_OPERATION_INVALID};       // The value of the "Operation_ID".
204     uint8_t operationDataFieldLength_ {AVRC_TG_PASS_OPERATION_DATA_FIELD_LENGTH};
205     // The value of the "Operation_data_field_length".
206     Packet *pkt_ {nullptr};                          // The frame packet.
207     uint8_t label_ {AVRC_DEFAULT_LABEL};             // The label which is used to distinguish different call.
208     bool isValid_ {false};                           // The frame packet is valid or not.
209 
210     BT_DISALLOW_COPY_AND_ASSIGN(AvrcTgPassPacket);
211 };
212 }  // namespace bluetooth
213 }  // namespace OHOS
214 
215 #endif  // !AVRCP_TG_PASS_THROUGH_H
216