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