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 HFP_HF_COMMAND_PROCESSOR_H
17 #define HFP_HF_COMMAND_PROCESSOR_H
18 
19 #include <cstdint>
20 #include <queue>
21 #include <string>
22 #include <tuple>
23 #include <unordered_map>
24 
25 #include "hfp_hf_data_connection.h"
26 #include "timer.h"
27 
28 namespace OHOS {
29 namespace bluetooth {
30 /**
31  * @brief Class for process AT command.
32  */
33 class HfpHfCommandProcessor {
34 public:
35     enum {
36         AT_COMMAND_NONE,
37         AT_BRSF_SETTER,
38         AT_BAC_SETTER,
39         AT_CIND_GETTER,
40         AT_CIND_TESTER,
41         AT_CMER_EXECUTER,
42         AT_CHLD_GETTER,
43         AT_CHLD_TESTER,
44         AT_BIND_SETTER,
45         AT_BIND_GETTER,
46         AT_BIND_TESTER,
47         AT_CLCC_EXECUTER,
48         AT_COPS_GETTER,
49         AT_COPS_SETTER,
50         AT_CNUM_EXECUTER,
51         AT_VTS_SETTER,
52         AT_VGS_SETTER,
53         AT_VGM_SETTER,
54         AT_BVRA_1_SETTER,
55         AT_BVRA_0_SETTER,
56         AT_ATD_EXECUTER,
57         AT_ATA_EXECUTER,
58         AT_CHLD_SETTER,
59         AT_BTRH_GETTER,
60         AT_BTRH_SETTER,
61         AT_CHUP_EXECUTER,
62         AT_BCC_EXECUTER,
63         AT_BINP_SETTER,
64         AT_BLDN_EXECUTER,
65         AT_NREC_SETTER,
66         AT_CMEE_SETTER,
67         AT_BIA_SETTER,
68         AT_CLIP_SETTER,
69         AT_CCWA_SETTER,
70         AT_CCME_SETTER,
71         AT_BCS_SETTER,
72         AT_BIEV_SETTER,
73         AT_CKPD_SETTER
74     };
75 
76     /**
77      * @brief Define HF AT command handler functions.
78      */
79     typedef void (HfpHfCommandProcessor::*AtCmdFn)(
80         HfpHfDataConnection &dataConn, const std::string &arg);
81 
82     /**
83      * @brief Struct for define HF AT command handler.
84      */
85     struct HfpHfAtHandler {
86         HfpHfCommandProcessor::AtCmdFn fn;
87     };
88 
89     /**
90      * @brief Get the AT command map.
91      *
92      * @return Returns the AT command map.
93      */
94     static std::unordered_map<std::string, HfpHfCommandProcessor::HfpHfAtHandler> &GetAtCmdMap();
95 
96     /**
97      * @brief Construct a new HfpHfCommandProcessor object.
98      */
99     HfpHfCommandProcessor() = default;
100 
101     /**
102      * @brief Destroy the HfpHfCommandProcessor object.
103      */
104     ~HfpHfCommandProcessor() = default;
105 
106     /**
107      * @brief Init command processor.
108      *
109      * @param address Remote device address.
110      */
111     void Init(const std::string &address);
112 
113     /**
114      * @brief Connect service level Connection in profile level.
115      *
116      * @param dataConn Data connection.
117      */
118     void ConnectSlc(HfpHfDataConnection &dataConn);
119 
120     /**
121      * @brief Send At command.
122      *
123      * @param dataConn Data connection.
124      * @param command command string.
125      * @param commandId command id.
126      */
127     void SendAtCommand(HfpHfDataConnection &dataConn, const std::string &command, int commandId);
128 
129     /**
130      * @brief Handle specific AT command.
131      *
132      * @param dataConn Data connection.
133      * @param cmd AT command.
134      * @param arg AT command argument.
135      */
136     void ProcessCommand(HfpHfDataConnection &dataConn, const std::string &cmd, const std::string &arg);
137 
138     /**
139      * @brief Clear up after disconnection.
140      */
141     void CleanUp();
142 
143 private:
144     // Remote device address
145     std::string address_ {""};
146 
147     // Respond timer object
148     std::unique_ptr<utility::Timer> respTimer_ {nullptr};
149 
150     int hspState_ = 1;
151 
152     static inline constexpr int HF_INDICATOR_MAX = 20;
153     static inline constexpr int INDICATOR_NAME_MAX_LEN = 16;
154     static inline constexpr int OPERATOR_NAME_MAX_LEN = 16;
155     static inline constexpr int PHONE_NUMBER_MAX_LEN = 32;
156 
157     // Respond Timeout
158     static inline constexpr int RESPOND_TIMEOUT_MS = 30000;
159     int currentCommand_ {AT_COMMAND_NONE};
160     std::queue<std::tuple<std::string, int>> commandQueue_ {};
161     inline static const std::string COMMAND_NONE = "";
162     inline static const std::string COMMAND_TYPE_EXECUTER = "";
163     inline static const std::string COMMAND_TYPE_GETTER = "?";
164     inline static const std::string COMMAND_TYPE_SETTER = "=";
165     inline static const std::string COMMAND_TYPE_TESTER = "=?";
166 
167     // 0 Releases all held calls or sets User Determined User Busy (UDUB) for a waiting call.
168     static inline constexpr int CHLD_RELEASE_ALL = 0x00000001;
169     // 1 Releases all active calls (if any exist) and accepts the other (held or waiting) call.
170     static inline constexpr int CHLD_RELEASE_ALL_AND_ACCPET =
171         0x00000002;  // 1x Releases specified active call only (<idx>).
172     static inline constexpr int CHLD_RELEASE_SPECIFIC = 0x00000004;
173     // 2 Places all active calls (if any exist) on hold and accepts the other (held or waiting) call.
174     static inline constexpr int CHLD_HOLD_ALL_AND_ACCPET = 0x00000008;
175     // 2x Request private consultation mode with specified call (<idx>). (Place all calls on hold EXCEPT the call
176     // indicated by <idx>.)
177     static inline constexpr int CHLD_HOLD_ALL_AND_PRIVATE = 0x00000010;
178     // 3 Adds a held call to the conversation.
179     static inline constexpr int CHLD_MERGE = 0x00000020;
180     // 4  Connects the two calls and disconnects the subscriber from both calls (Explicit Call Transfer).
181     static inline constexpr int CHLD_EXPLICT_CALL_TRANSFER = 0x00000040;
182 
183     static inline constexpr int INVALID_CME_ERROR_CODE = 0xFFFFFFFF;
184 
185     inline static constexpr int COPS_ARGS_NUMBER = 2;
186     inline static constexpr int CIEV_ARGS_NUMBER = 2;
187     inline static constexpr int CCWA_ARGS_NUMBER = 2;
188     inline static constexpr int CNUM_ARGS_NUMBER = 3;
189     inline static constexpr int CLCC_ARGS_NUMBER = 5;
190     inline static constexpr int CHLD_ARGS_MIN_LENGTH = 3;
191     inline static constexpr int CHLD_TEST_ARGS_NUMBER = 3;
192     inline static constexpr int CHLD_MAIN_ARGS_NUMBER = 5;
193     inline static constexpr int CHLD_SUB_ARGS_NUMBER = 2;
194     inline static constexpr int BIND_SET_ARGS_NUMBER = 2;
195 
196     static std::unordered_map<std::string, HfpHfCommandProcessor::HfpHfAtHandler> g_atCmdMap;
197     static int StoiTryCatch(const std::string &arg);
198     void RespondTimeout();
199     void SendQueuedAtCommand(HfpHfDataConnection &dataConn);
200     void ProcessOK(HfpHfDataConnection &dataConn, const std::string &arg);
201     void ProcessErrorCode(HfpHfDataConnection &dataConn, int errorCode);
202     void ProcessErrorCmd(HfpHfDataConnection &dataConn, const std::string &arg);
203     void ProcessBusy(HfpHfDataConnection &dataConn, const std::string &arg);
204     void ProcessDelayed(HfpHfDataConnection &dataConn, const std::string &arg);
205     void ProcessNoCarrier(HfpHfDataConnection &dataConn, const std::string &arg);
206     void ProcessNoAnswer(HfpHfDataConnection &dataConn, const std::string &arg);
207     void ProcessBlocklisted(HfpHfDataConnection &dataConn, const std::string &arg);
208     void ProcessCmeError(HfpHfDataConnection &dataConn, const std::string &arg);
209     void ProcessCops(HfpHfDataConnection &dataConn, const std::string &arg);
210     void ProcessBtrh(HfpHfDataConnection &dataConn, const std::string &arg);
211     void ProcessRing(HfpHfDataConnection &dataConn, const std::string &arg);
212     void ProcessClip(HfpHfDataConnection &dataConn, const std::string &arg);
213     void ProcessBrsf(HfpHfDataConnection &dataConn, const std::string &arg);
214     void ProcessCind(HfpHfDataConnection &dataConn, const std::string &arg);
215     void ProcessCindGetter(HfpHfDataConnection &dataConn, std::string arg);
216     void ProcessCindTester(HfpHfDataConnection &dataConn, std::string arg);
217     void ProcessChld(HfpHfDataConnection &dataConn, const std::string &arg);
218     void ProcessBind(HfpHfDataConnection &dataConn, const std::string &arg);
219     void ProcessListHfIndicators(HfpHfDataConnection &dataConn, const std::string &arg);
220     void ProcessChangeIndicatorState(HfpHfDataConnection &dataConn, const std::string &arg);
221     void ProcessCiev(HfpHfDataConnection &dataConn, const std::string &arg);
222     void ProcessClcc(HfpHfDataConnection &dataConn, const std::string &arg);
223     void ProcessBsir(HfpHfDataConnection &dataConn, const std::string &arg);
224     void ProcessCcwa(HfpHfDataConnection &dataConn, const std::string &arg);
225     void ProcessBcs(HfpHfDataConnection &dataConn, const std::string &arg);
226     void ProcessBvra(HfpHfDataConnection &dataConn, const std::string &arg);
227     void ProcessCnum(HfpHfDataConnection &dataConn, const std::string &arg);
228     void ProcessVgm(HfpHfDataConnection &dataConn, const std::string &arg);
229     void ProcessVgs(HfpHfDataConnection &dataConn, const std::string &arg);
230     void ProcessIndicator(
231         HfpHfDataConnection &dataConn, HfpHfDataConnection::AgIndicator &it, uint32_t value);
232     void ProcessSlcBrsf(HfpHfDataConnection &dataConn);
233     void ProcessSlcCmer(HfpHfDataConnection &dataConn);
234     void ProcessSlcChld(HfpHfDataConnection &dataConn);
235     void ProcessSlcConnected(HfpHfDataConnection &dataConn);
236     void SendAtBrsfSetter(HfpHfDataConnection &dataConn);
237     void SendAtBacSetter(HfpHfDataConnection &dataConn);
238     void SendAtCindTester(HfpHfDataConnection &dataConn);
239     void SendAtCindGetter(HfpHfDataConnection &dataConn);
240     void SendAtCmerSetter(HfpHfDataConnection &dataConn);
241     void SendAtChldTester(HfpHfDataConnection &dataConn);
242     void SendAtBindGetter(HfpHfDataConnection &dataConn);
243     void SendAtBindSetter(HfpHfDataConnection &dataConn);
244     void SendAtBindTester(HfpHfDataConnection &dataConn);
245     void SendAtBiaSetter(HfpHfDataConnection &dataConn);
246     BT_DISALLOW_COPY_AND_ASSIGN(HfpHfCommandProcessor);
247 };
248 }  // namespace bluetooth
249 }  // namespace OHOS
250 #endif // HFP_HF_COMMAND_PROCESSOR_H