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_DATA_CONNECTION_H
17 #define HFP_AG_DATA_CONNECTION_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "base_def.h"
25 #include "hfp_ag_defines.h"
26 #include "hfp_ag_rfcomm_connection.h"
27 #include "raw_address.h"
28 #include "timer.h"
29 
30 namespace OHOS {
31 namespace bluetooth {
32 /**
33  * @brief Class for HFP AG data connection.
34  */
35 class HfpAgDataConnection {
36     typedef struct {
37         uint16_t anum {0};
38         uint32_t value {0};
39         uint32_t minVal {0};
40         uint32_t maxVal {0};
41         bool isSupported {false};
42         bool isEnabled {false};
43     } HfIndicator;
44 
45 public:
46     /**
47      * @brief Data connection callback function.
48      *
49      * @param handle Handle.
50      * @param eventId Event id.
51      * @param eventData Event data.
52      * @param context Context.
53      */
54     static void DataConnectionCallback(uint16_t handle, uint32_t eventId, const void *eventData, void *context);
55 
56     /**
57      * @brief Init data connection.
58      */
59     static int Init();
60 
61     /**
62      * @brief Clean up data connection.
63      */
64     static void CleanUp();
65 
66     /**
67      * @brief Get the AG local features.
68      *
69      * @return Returns the local AG support features.
70      */
71     static uint32_t GetLocalFeatures();
72 
73     /**
74      * @brief  This function used to get support of codec negotiation.
75      *
76      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
77      */
78     bool IsCodecNegotiationSupport() const;
79 
80     /**
81      * @brief This function used to get support of ESCO.
82      *
83      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
84      */
85     static bool IsEscoSupport();
86 
87     /**
88      * @brief This function used to get support of ESCO S4.
89      *
90      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
91      */
92     bool IsEscoS4Support() const;
93 
94     /**
95      * @brief Construct a new HfpAgDataConnection object.
96      */
97     HfpAgDataConnection() = default;
98 
99     /**
100      * @brief Destroy the HfpAgDataConnection object.
101      */
102     ~HfpAgDataConnection() = default;
103 
104     /**
105      * @brief  This function used to connect remote device rfcomm channel after SDP discovery OK (Initiator).
106      *
107      * @return Returns the error code of the connect result.
108      */
109     int Connect();
110 
111     /**
112      * @brief This function used to disconnect remote device rfcomm channel.
113      *
114      * @return Returns the error code of the disconnect result.
115      */
116     int Disconnect() const;
117 
118     /**
119      * @brief  This function used to read data from remote device.
120      *
121      * @param pkt The address of data packet pointer.
122      * @return Returns the error code of the read data result.
123      */
124     int ReadData(Packet **pkt) const;
125 
126     /**
127      * @brief This function used to write data to remote device.
128      *
129      * @param pkt Data packet pointer.
130      * @return Returns the error code of the write data result.
131      */
132     int WriteData(Packet &pkt) const;
133 
134     /**
135      * @brief Set the remote address.
136      *
137      * @param addr Remote device address.
138      */
139     void SetRemoteAddr(const std::string &addr);
140 
141     /**
142      * @brief Set the SDP info.
143      *
144      * @param sdpInfo Remote device SDP info.
145      */
146     void SetSdpInfo(HfpAgRemoteSdpInfo sdpInfo);
147 
148     /**
149      * @brief Get the connection role.
150      *
151      * @return Returns the connection role.
152      */
153     int GetRole() const;
154 
155     /**
156      * @brief Set the connection role.
157      *
158      * @param role Role in connection.
159      */
160     void SetRole(int role);
161 
162     /**
163      * @brief Get the connection handle.
164      *
165      * @return Returns the connection handle.
166      */
167     uint16_t GetConnectionHandle() const;
168 
169     /**
170      * @brief Set the connection handle.
171      *
172      * @param handle Connection handle.
173      */
174     void SetConnectionHandle(uint16_t handle);
175 
176     /**
177      * @brief Set the SLC connect state.
178      *
179      * @param state SLC connect state.
180      */
181     void SetSlcConnectState(bool state);
182 
183     /**
184      * @brief This function get remote server channel number.
185      *
186      * @return Remote server channel number
187      */
188     uint8_t GetRemoteScn() const;
189 
190 private:
191     /**
192      * @brief Process Data connection callback function in hfp thread.
193      *
194      * @param handle Handle.
195      * @param eventId Event id.
196      */
197     static void ProcessDataConnectionCallback(uint16_t handle, uint32_t eventId);
198 
199     friend class HfpAgProfile;
200     friend class HfpAgCommandProcessor;
201 
202     static uint32_t g_localFeatures;
203     static inline int g_localSupportCodecs {HFP_AG_CODEC_NONE};
204     static constexpr uint8_t ANUM_CVSD = 0x02;               // Coding Format(1 Octet) Assign Number CVSD
205     static constexpr uint8_t ANUM_MSBC = 0x05;               // Coding Format(1 Octet) Assign Number MSBC
206     static inline const std::string CHLD_SETTINGS = "(1,2)"; /* now only support +CHLD 1&2 */
207     static inline const std::string CHLD_ECC_SETTINGS = "(0,1,1x,2,2x,3,4)";
208     static inline const std::string CIND_SETTINGS = "(\"service\",(0-1)),(\"call\",(0,1)),(\"callsetup\",(0-3)),"
209                                                     "(\"callheld\",(0-2)),(\"signal\",(0-5)),(\"roam\",(0,1)),"
210                                                     "(\"battchg\",(0-5)))";
211     int role_ {HFP_AG_ACCEPTOR};
212     uint32_t remoteFeatures_ {0};
213 
214     int localDemandCodec_ {HFP_AG_CODEC_MSBC};
215     int localSelectedCodec_ {HFP_AG_CODEC_MSBC};
216     int remoteSelectedCodec_ {HFP_AG_CODEC_MSBC};
217     int remoteSupportCodecs_ {HFP_AG_CODEC_NONE};
218     int inUseCodec_ {HFP_AG_CODEC_CVSD};
219     int inBandRingTone_ {HFP_AG_INBAND_RING_DISABLE};
220     bool remoteSupportCodecsUpdated_ {false};
221     bool codecNegotiating_ {false};
222     bool slcConnected_ {false};
223     bool clipEnabled_ {false};
224     bool ccwaEnabled_ {true};
225     bool cmerEnabled_ {true};
226     bool cmeeEnabled_ {false};
227     bool serviceIndicatorEnabled_ {true};
228     bool signalStrengthIndicatorEnabled_ {true};
229     bool batteryIndicatorEnabled_ {true};
230     bool roamIndicatorEnabled_ {true};
231     uint32_t serviceInd_ {0};
232     uint32_t signalInd_ {0};
233     uint32_t batteryInd_ {0};
234     uint32_t roamInd_ {0};
235     uint32_t callInd_ {0};
236     uint32_t callsetupInd_ {0};
237     uint32_t callheldInd_ {0};
238     int biaMaskOut_ {0};
239     std::string remoteAddr_ {""};
240     std::string clipNumber_ {""};
241     uint16_t clipType_ {0};
242     std::string subscriberNumber_ {""};
243     std::string operatorName_ {""};
244     HfpAgRemoteSdpInfo sdpInfo_ {};
245     std::vector<HfIndicator> localHfIndicators_ = {
246         {1, 0, 0, 1, true, true},   /* Enhanced Driver Status */
247         {2, 0, 0, 100, true, true}  /* Battery Level Status */
248     };
249     std::vector<HfIndicator> remoteHfIndicators_ {};
250     HfpAgRfcommConnection rfcommConnection_ {&HfpAgDataConnection::DataConnectionCallback};
251 
252     // Ring Timeout
253     static inline constexpr int RING_TIMEOUT_MS = 3000;
254 
255     // Ring timer object
256     std::unique_ptr<utility::Timer> ringTimer_ {nullptr};
257 
258     BT_DISALLOW_COPY_AND_ASSIGN(HfpAgDataConnection);
259 };
260 }  // namespace bluetooth
261 }  // namespace OHOS
262 #endif // HFP_AG_DATA_CONNECTION_H