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_PARSER_H
17 #define HFP_AG_COMMAND_PARSER_H
18 
19 #include <cstdint>
20 #include <string>
21 #include <unordered_map>
22 #include <vector>
23 
24 #include "base_def.h"
25 #include "hfp_ag_command_processor.h"
26 #include "hfp_ag_data_connection.h"
27 
28 namespace OHOS {
29 namespace bluetooth {
30 /**
31  * @brief Class for parse AT command from remote HF device.
32  */
33 class HfpAgCommandParser {
34 public:
35     /**
36      * @brief Get the HfpAgCommandParser instance.
37      *
38      * @return Returns HfpAgCommandParser instance.
39      */
40     static HfpAgCommandParser &GetInstance();
41 
42     /**
43      * @brief Struct for store command position.
44      */
45     struct CommandPosition {
46         uint16_t pos;
47         uint16_t endPos;
48         uint16_t setPos;
49         uint16_t getPos;
50         uint16_t startPos;
51         bool headValid;
52         bool tailValid;
53     };
54 
55     /**
56      * @brief Read data from data link.
57      *
58      * @param dataConn Data connection.
59      */
60     void Read(HfpAgDataConnection &dataConn) const;
61 
62     /**
63      * @brief Parse data bufffer.
64      *
65      * @param dataConn Data connection.
66      * @param data Data buffer pointer.
67      * @param len Data buffer length.
68      */
69     void Parse(HfpAgDataConnection &dataConn, std::vector<uint8_t> &data, size_t len) const;
70 
71     /**
72      * @brief Extract At command from data buffer.
73      *
74      * @param data Data buffer pointer.
75      * @param cmd AT command.
76      * @param arg AT command argument.
77      * @param cmdLen AT command length include command & argument.
78      * @param len Data buffer length.
79      * @return Returns error code of the result.
80      */
81     int Extract(std::vector<uint8_t> &data, std::string &cmd, std::string &arg, size_t &cmdLen, size_t len) const;
82 
83     /**
84      * @brief Extract command argument for AT Setter command.
85      *
86      * @param cmd AT command.
87      * @param arg AT command argument.
88      */
89     void ExtractArg(std::string &cmd, std::string &arg) const;
90 
91     /**
92      * @brief Get the Command Position object.
93      *
94      * @param data Data buffer pointer.
95      * @param len Data buffer length.
96      * @param pos Data buffer position.
97      */
98     void GetCommandPosition(const std::vector<uint8_t> &data, size_t len,
99                             HfpAgCommandParser::CommandPosition &pos) const;
100 
101 private:
102     HfpAgCommandParser() = default;
103     ~HfpAgCommandParser() = default;
104     BT_DISALLOW_COPY_AND_ASSIGN(HfpAgCommandParser);
105 
106     inline static constexpr int HFP_AG_AT_HEAD_SIZE = 2;
107     inline static constexpr int ATA_LENGTH = 3;
108     inline static constexpr int ATD_LENGTH = 3;
109     inline static constexpr int AT_EXEC_OPERATOR_LENGTH = 0;    // operator ""
110     inline static constexpr int AT_GET_OPERATOR_LENGTH = 1;     // operator "?"
111     inline static constexpr int AT_SET_OPERATOR_LENGTH = 1;     // operator "="
112     inline static constexpr int AT_TEST_OPERATOR_LENGTH = 2;    // operator "=?"
113 
114     std::unordered_map<std::string, HfpAgCommandProcessor::HfpAgAtHandler> &g_atCmdMap {
115         HfpAgCommandProcessor::GetInstance().GetAtCmdMap()
116     };
117 };
118 }  // namespace bluetooth
119 }  // namespace OHOS
120 #endif // HFP_AG_COMMAND_PARSER_H