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_AG_COMMAND_PROCESSOR_H
17 #define HFP_AG_COMMAND_PROCESSOR_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "hfp_ag_data_connection.h"
23 
24 namespace OHOS {
25 namespace bluetooth {
26 typedef enum {
27     HFP_AG_CMD_INVALID = 0,
28     HFP_AG_CMD_GET,
29     HFP_AG_CMD_SET,
30     HFP_AG_CMD_TEST,
31     HFP_AG_CMD_EXEC,
32     HFP_AG_CMD_UNKNOWN
33 } HfpAgAtType;
34 
35 /**
36  * @brief Class for process AT command from remote HF device or local AG service.
37  */
38 class HfpAgCommandProcessor {
39 public:
40     /**
41      * @brief Define AG AT command handler functions.
42      */
43     typedef void (HfpAgCommandProcessor::*AtCmdFn)(
44         HfpAgDataConnection &dataConn, const std::string &arg);
45 
46     /**
47      * @brief Struct for define AG AT command handler.
48      */
49     struct HfpAgAtHandler {
50         HfpAgCommandProcessor::AtCmdFn getter;
51         HfpAgCommandProcessor::AtCmdFn setter;
52         HfpAgCommandProcessor::AtCmdFn tester;
53         HfpAgCommandProcessor::AtCmdFn executer;
54     };
55 
56     /**
57      * @brief Get the HfpAgCommandProcessor instance.
58      *
59      * @return Returns the HfpAgCommandProcessor instance.
60      */
61     static HfpAgCommandProcessor &GetInstance();
62 
63     /**
64      * @brief Get the AT command map.
65      *
66      * @return Returns the AT command map.
67      */
GetAtCmdMap()68     std::unordered_map<std::string, HfpAgCommandProcessor::HfpAgAtHandler> &GetAtCmdMap()
69     {
70         return g_atCmdMap;
71     }
72 
73     /**
74      * @brief Send Error command.
75      *
76      * @param dataConn Data connection.
77      * @param errorCode Error code.
78      */
79     static void SendErrorCode(const HfpAgDataConnection &dataConn, int errorCode);
80 
81     /**
82      * @brief Send AT command.
83      *
84      * @param dataConn Data connection.
85      * @param command AT command.
86      */
87     static void SendAtCommand(const HfpAgDataConnection &dataConn, const std::string &command);
88 
89     /**
90      * @brief Empty function.
91      *
92      * @param dataConn Data connection.
93      * @param arg AT command argument
94      */
95     void AtEmptyFn(HfpAgDataConnection &dataConn, const std::string &arg);
96     /**
97      * @brief ATA Setter.
98      *
99      * @param dataConn Data connection.
100      * @param arg AT command argument
101      */
102     void AtaExecuter(HfpAgDataConnection &dataConn, const std::string &arg);
103 
104     /**
105      * @brief ATA Setter.
106      *
107      * @param dataConn Data connection.
108      * @param arg AT command argument
109      */
110     void AtdExecuter(HfpAgDataConnection &dataConn, const std::string &arg);
111 
112     /**
113      * @brief VGS Setter.
114      *
115      * @param dataConn Data connection.
116      * @param arg AT command argument
117      */
118     void VgsSetter(HfpAgDataConnection &dataConn, const std::string &arg);
119 
120     /**
121      * @brief VGM Setter.
122      *
123      * @param dataConn Data connection.
124      * @param arg AT command argument
125      */
126     void VgmSetter(HfpAgDataConnection &dataConn, const std::string &arg);
127 
128     /**
129      * @brief CCWA Setter.
130      *
131      * @param dataConn Data connection.
132      * @param arg AT command argument
133      */
134     void CcwaSetter(HfpAgDataConnection &dataConn, const std::string &arg);
135 
136     /**
137      * @brief CHLD Setter.
138      *
139      * @param dataConn Data connection.
140      * @param arg AT command argument
141      */
142     void ChldSetter(HfpAgDataConnection &dataConn, const std::string &arg);
143 
144     /**
145      * @brief CHLD Tester.
146      *
147      * @param dataConn Data connection.
148      * @param arg AT command argument
149      */
150     void ChldTester(HfpAgDataConnection &dataConn, const std::string &arg);
151 
152     /**
153      * @brief CHUP Executer.
154      *
155      * @param dataConn Data connection.
156      * @param arg AT command argument
157      */
158     void ChupExecuter(HfpAgDataConnection &dataConn, const std::string &arg);
159 
160     /**
161      * @brief CIND Getter.
162      *
163      * @param dataConn Data connection.
164      * @param arg AT command argument
165      */
166     void CindGetter(HfpAgDataConnection &dataConn, const std::string &arg);
167 
168     /**
169      * @brief CIND Tester.
170      *
171      * @param dataConn Data connection.
172      * @param arg AT command argument
173      */
174     void CindTester(HfpAgDataConnection &dataConn, const std::string &arg);
175 
176     /**
177      * @brief CLIP Setter.
178      *
179      * @param dataConn Data connection.
180      * @param arg AT command argument
181      */
182     void ClipSetter(HfpAgDataConnection &dataConn, const std::string &arg);
183 
184     /**
185      * @brief CMER Executer.
186      *
187      * @param dataConn Data connection.
188      * @param arg AT command argument
189      */
190     void CmerSetter(HfpAgDataConnection &dataConn, const std::string &arg);
191 
192     /**
193      * @brief VTS Setter.
194      *
195      * @param dataConn Data connection.
196      * @param arg AT command argument
197      */
198     void VtsSetter(HfpAgDataConnection &dataConn, const std::string &arg);
199 
200     /**
201      * @brief BLDN Executer.
202      *
203      * @param dataConn Data connection.
204      * @param arg AT command argument
205      */
206     void BldnExecuter(HfpAgDataConnection &dataConn, const std::string &arg);
207 
208     /**
209      * @brief BVRA Setter.
210      *
211      * @param dataConn Data connection.
212      * @param arg AT command argument
213      */
214     void BvraSetter(HfpAgDataConnection &dataConn, const std::string &arg);
215 
216     /**
217      * @brief BRSF Setter.
218      *
219      * @param dataConn Data connection.
220      * @param arg AT command argument
221      */
222     void BrsfSetter(HfpAgDataConnection &dataConn, const std::string &arg);
223 
224     /**
225      * @brief NREC Setter.
226      *
227      * @param dataConn Data connection.
228      * @param arg AT command argument
229      */
230     void NrecSetter(HfpAgDataConnection &dataConn, const std::string &arg);
231 
232     /**
233      * @brief CNUM Executer.
234      *
235      * @param dataConn Data connection.
236      * @param arg AT command argument
237      */
238     void CnumExecuter(HfpAgDataConnection &dataConn, const std::string &arg);
239 
240     /**
241      * @brief CLCC Executer.
242      *
243      * @param dataConn Data connection.
244      * @param arg AT command argument
245      */
246     void ClccExecuter(HfpAgDataConnection &dataConn, const std::string &arg);
247 
248     /**
249      * @brief COPS Getter.
250      *
251      * @param dataConn Data connection.
252      * @param arg AT command argument
253      */
254     void CopsGetter(HfpAgDataConnection &dataConn, const std::string &arg);
255 
256     /**
257      * @brief COPS Setter.
258      *
259      * @param dataConn Data connection.
260      * @param arg AT command argument
261      */
262     void CopsSetter(HfpAgDataConnection &dataConn, const std::string &arg);
263 
264     /**
265      * @brief CMEE Setter.
266      *
267      * @param dataConn Data connection.
268      * @param arg AT command argument
269      */
270     void CmeeSetter(HfpAgDataConnection &dataConn, const std::string &arg);
271 
272     /**
273      * @brief BIA Setter.
274      *
275      * @param dataConn Data connection.
276      * @param arg AT command argument
277      */
278     void BiaSetter(HfpAgDataConnection &dataConn, const std::string &arg);
279 
280     /**
281      * @brief BCC Executer.
282      *
283      * @param dataConn Data connection.
284      * @param arg AT command argument
285      */
286     void BccExecuter(HfpAgDataConnection &dataConn, const std::string &arg);
287 
288     /**
289      * @brief BCS Setter.
290      *
291      * @param dataConn Data connection.
292      * @param arg AT command argument
293      */
294     void BcsSetter(HfpAgDataConnection &dataConn, const std::string &arg);
295 
296     /**
297      * @brief BIND Getter.
298      *
299      * @param dataConn Data connection.
300      * @param arg AT command argument
301      */
302     void BindGetter(HfpAgDataConnection &dataConn, const std::string &arg);
303 
304     /**
305      * @brief BIND Setter.
306      *
307      * @param dataConn Data connection.
308      * @param arg AT command argument
309      */
310     void BindSetter(HfpAgDataConnection &dataConn, const std::string &arg);
311 
312     /**
313      * @brief BIND tester.
314      *
315      * @param dataConn Data connection.
316      * @param arg AT command argument
317      */
318     void BindTester(HfpAgDataConnection &dataConn, const std::string &arg);
319 
320     /**
321      * @brief BIEV Setter.
322      *
323      * @param dataConn Data connection.
324      * @param arg AT command argument
325      */
326     void BievSetter(HfpAgDataConnection &dataConn, const std::string &arg);
327 
328     /**
329      * @brief BAC Setter.
330      *
331      * @param dataConn Data connection.
332      * @param arg AT command argument
333      */
334     void BacSetter(HfpAgDataConnection &dataConn, const std::string &arg);
335 
336     /**
337      * @brief BTRH Getter.
338      *
339      * @param dataConn Data connection.
340      * @param arg AT command argument
341      */
342     void BtrhGetter(HfpAgDataConnection &dataConn, const std::string &arg);
343 
344     /**
345      * @brief BTRH Setter.
346      *
347      * @param dataConn Data connection.
348      * @param arg AT command argument
349      */
350     void BtrhSetter(HfpAgDataConnection &dataConn, const std::string &arg);
351 
352     /**
353      * @brief CKPD Setter.
354      *
355      * @param dataConn Data connection.
356      * @param arg AT command argument
357      */
358     void CkpdSetter(HfpAgDataConnection &dataConn, const std::string &arg);
359 
360     /**
361      * @brief BINP Getter.
362      *
363      * @param dataConn Data connection.
364      * @param arg AT command argument
365      */
366     void BinpSetter(HfpAgDataConnection &dataConn, const std::string &arg);
367 
368     /**
369      * @brief Handle specific AT command.
370      *
371      * @param dataConn Data connection.
372      * @param cmd AT command.
373      * @param arg AT command argument.
374      * @param cmdType AT command type.
375      */
376     void Handle(
377         HfpAgDataConnection &dataConn, const std::string &cmd, const std::string &arg, int cmdType);
378 
379 private:
380     HfpAgCommandProcessor() = default;
381     ~HfpAgCommandProcessor() = default;
382     static int StoiTryCatch(HfpAgDataConnection &dataConn, const std::string &arg);
383     BT_DISALLOW_COPY_AND_ASSIGN(HfpAgCommandProcessor);
384 
385     static std::unordered_map<std::string, HfpAgCommandProcessor::HfpAgAtHandler> g_atCmdMap;
386     static inline const std::string HEAD = "\r\n";
387     static inline const std::string TAIL = "\r\n";
388     static inline const std::string OK = "OK";
389     static inline const std::string ERROR = "ERROR";
390     inline static constexpr int ATD_VALID_LENGTH = 2;
391     inline static constexpr int CHLD_RELEASE_ALL_HELD_CALLS = 0;
392     inline static constexpr int CHLD_RELEASE_ACTIVE_ACCPET_OTHER = 1;
393     inline static constexpr int CHLD_RELEASE_HOLD_ACCPET_OTHER = 2;
394     inline static constexpr int CHLD_ADD_CALL_TO_CONVERSATION = 3;
395     inline static constexpr int CHLD_CONNECT_TWO_CALL = 4;
396     inline static constexpr int CHLD_RELEASE_INDEX_ONE = 11;
397     inline static constexpr int CHLD_RELEASE_INDEX_TWO = 12;
398     inline static constexpr int CHLD_CONSULTATION_INDEX_ONE = 21;
399     inline static constexpr int CHLD_CONSULTATION_INDEX_TWO = 22;
400     inline static constexpr int BIEV_ARGS_VALID_LENGTH = 2;
401     inline static constexpr int CMER_ELEMENTS_NUMBER = 4;
402     inline static constexpr unsigned int CMER_MODE_INDEX = 0;
403     inline static constexpr unsigned int CMER_KEYP_INDEX = 1;
404     inline static constexpr unsigned int CMER_DISP_INDEX = 2;
405     inline static constexpr unsigned int CMER_IND_INDEX = 3;
406     inline static constexpr int CMER_MODE_FOR_HFP = 3;
407 
408     // Max Number of supported AG indicators
409     static inline constexpr int MAX_AG_INDICATORS = 20;
410 
411     // Number of supported HF indicators
412     // 1 for Enhanced Safety Status, 2 for Battery Level Status
413     static inline constexpr int LOCAL_HF_IND_NUM = 2;
414 };
415 }  // namespace bluetooth
416 }  // namespace OHOS
417 #endif // HFP_AG_COMMAND_PROCESSOR_H
418