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_PROFILE_H
17 #define HFP_HF_PROFILE_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include "hfp_hf_audio_connection.h"
23 #include "hfp_hf_command_parser.h"
24 #include "hfp_hf_data_connection.h"
25 #include "hfp_hf_data_connection_server.h"
26 #include "hfp_hf_sdp_client.h"
27 
28 namespace OHOS {
29 namespace bluetooth {
30 /**
31  * @brief Class for providing functions to relate to other underlying layers.
32  *        The functions of this layer are called by HF state machine.
33  */
34 class HfpHfProfile {
35 public:
36     /**
37      * @brief Register SDP service and data connection server for local device only once.
38      */
39     static int RegisterService();
40 
41     /**
42      * @brief Deregister SDP service and data connection server for local device only once.
43      */
44     static int DeregisterService();
45 
46     /**
47      * @brief Construct a new HfpHfProfile object.
48      *
49      * @param address Remote device address
50      */
51     explicit HfpHfProfile(std::string address);
52 
53     /**
54      * @brief Destroy the HfpHfProfile object.
55      */
56     ~HfpHfProfile() = default;
57 
58     /**
59      * @brief Initialise the profile object.
60      */
61     void Init();
62 
63     /**
64      * @brief Start a SDP discovery job of remote SDP server.
65      *
66      * @param role Role in connection.
67      * @return Returns the sdp discovery result.
68      */
69     int DoServiceDiscovery(int role);
70 
71     /**
72      * @brief Find out attributes required by HF and set the SDP result to data connection.
73      *
74      * @return Returns the find sdp discovery result.
75      */
76     int ServiceDiscoveryResult();
77 
78     /**
79      * @brief Process service level connection established event.
80      */
81     void ProcessSlcEstablished();
82 
83     /**
84      * @brief Establish data connection by HF.
85      *
86      * @return Returns the result of establish data connection.
87      */
88     int EstablishDataConnection();
89 
90     /**
91      * @brief Release the existed data connection by HF.
92      *
93      * @return Returns the result of release data connection.
94      */
95     int ReleaseDataConnection() const;
96 
97     /**
98      * @brief Accept data connection from remote HF device.
99      *
100      * @param handle Data connection handle.
101      * @return Returns the result of accept data connection.
102      */
103     int AcceptDataConnection(uint16_t handle);
104 
105     /**
106      * @brief Establish service level connection between Audio Gateway and HandsFree Uint.
107      *
108      * @return Returns the result of establish service level connection.
109      */
110     int EstablishServiceLevelConnection();
111 
112     /**
113      * @brief Read data from data connection.
114      *
115      * @return Returns the result of read data from data connection.
116      */
117     int ReadData();
118 
119     /**
120      * @brief Select codec between AG and HF.
121      *
122      * @return Returns the result of select codec.
123      */
124     int SetupCodecConnection();
125 
126     /**
127      * @brief Initiate the establishment of an audio connection to remote HF device by AG.
128      *
129      * @return Returns the result of establish audio data connection.
130      */
131     int EstablishAudioConnection();
132 
133     /**
134      * @brief Release the existed audio connection between AG and HF by AG.
135      *
136      * @return Returns the result of release audio connection.
137      */
138     int ReleaseAudioConnection() const;
139 
140     /**
141      * @brief Accept audio connection request from HF.
142      *
143      * @return Returns the result of accept audio connection.
144      */
145     int AcceptAudioConnection();
146 
147     /**
148      * @brief Reject audio connection request from HF.
149      *
150      * @return Returns the result of reject audio connection.
151      */
152     int RejectAudioConnection() const;
153 
154     /**
155      * @brief Restore codec setup role.
156      *
157      * @return Returns the result of restore codec setup role.
158      */
159     int RestorCodecSetupRole();
160 
161     /**
162      * @brief Process audio connect request in audio connecting state.
163      *
164      * @return Returns the result of process result.
165      */
166     int ProcessAudioConnReqInAudioConnecting();
167 
168     /**
169      * @brief Query the current call list.
170      *
171      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
172      */
173     bool QueryCurrentCalls();
174 
175     /**
176      * @brief Query the current operator name.
177      *
178      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
179      */
180     bool QueryOperatorName(bool isQuery);
181 
182     /**
183      * @brief Query the subscriber information.
184      *
185      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
186      */
187     bool QuerySubscriberInfo();
188 
189     /**
190      * @brief Send the DTMF.
191      *
192      * @param code The code of the DTMF.
193      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
194      */
195     bool SendDtmf(uint8_t code);
196 
197     /**
198      * @brief Set the Volume.
199      *
200      * @param volume The value of the volume.
201      * @param type The type of the volume.
202      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
203      */
204     bool SetHfVolume(int volume, int type);
205 
206     /**
207      * @brief Open the voice recognition.
208      *
209      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
210      */
211     bool OpenVoiceRecognition();
212 
213     /**
214      * @brief Close the voice recognition.
215      *
216      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
217      */
218     bool CloseVoiceRecognition();
219 
220     /**
221      * @brief Dial out the call by the number.
222      *
223      * @param number The number of the call.
224      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
225      */
226     bool DialOutCall(const std::string &number);
227 
228     /**
229      * @brief Send key pressed event.
230      *
231      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
232      */
233     bool SendKeyPressed();
234 
235     /**
236      * @brief Dial out the call by the memory.
237      *
238      * @param number The index of the memory.
239      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
240      */
241     bool DialMemory(int memory);
242 
243     /**
244      * @brief Send voice tag.
245      *
246      * @param index The index of voice tag.
247      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
248      */
249     bool SendVoiceTag(int index);
250 
251     /**
252      * @brief Send ATA command.
253      *
254      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
255      */
256     bool SendAta();
257 
258     /**
259      * @brief Send CHLD command.
260      *
261      * @param action Action of CHLD.
262      * @param idx Index to specific, default 0(mean no index).
263      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
264      */
265     bool SendChld(int action, int idx = 0);
266 
267     /**
268      * @brief Send CHUP command.
269      *
270      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
271      */
272     bool SendChup();
273 
274     /**
275      * @brief Send BTRH command.
276      *
277      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
278      */
279     bool SendBtrh(int action);
280 
281     /**
282      * @brief Send BCC command.
283      *
284      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
285      */
286     bool SendBcc();
287 
288     /**
289      * @brief Send BLDN command, which used for calling the last phone number dialed.
290      *
291      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
292      */
293     bool CallLastDialedNumber();
294 
295     /**
296      * @brief Send NREC command to disable NREC of the remote AG device.
297      *
298      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
299      */
300     bool CloseRemoteNrec();
301 
302     /**
303      * @brief Reports the change of the battery level to remote HF device.
304      *
305      * @param battery Battery level.
306      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
307      */
308     bool ReportBatteryLevel(uint32_t battery);
309 
310     /**
311      * @brief Reports the change of the driver safety value to remote HF device.
312      *
313      * @param safety Driver safety value.
314      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
315      */
316     bool ReportDriverSafety(uint32_t safety);
317 
318     /**
319      * @brief Remove the hf state machine.
320      */
321     void RemoveStateMachine();
322 
323     /**
324      * @brief Send the AT command.
325      * @param cmdId : The id of the at command
326      * @param arg : The string of the at command
327      */
328     void SendAtCommand(int cmdId, const std::string &arg);
329 
330     /**
331      * @brief Process audio connect request.
332      */
333     void ProcessAudioConnectRequest();
334 
335     /**
336      * @brief Remove scn loging.
337      */
338     void RemoveRemoteScnLoging() const;
339 
340 private:
341     /**
342      * @brief Reports the specific indicator to remote AG device.
343      *
344      * @param indicator Indicator type.
345      * @param value Indicator value.
346      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
347      */
348     bool ReportHfIndicator(int indicator, int value);
349 
350     // Codec setup initiator
351     bool isCodecSetupInitiator_ {false};
352 
353     // Remote device address
354     std::string address_;
355     // HfpHfSdpClient object
356     HfpHfSdpClient sdpClient_ {};
357     // HfpHFDataConnection object
358     HfpHfDataConnection dataConn_ {};
359     // HfpAgAudioConnection object
360     HfpHfAudioConnection audioConn_ {};
361     // HfpHfDataConnectionServer instance
362     HfpHfDataConnectionServer &dataServer_ {HfpHfDataConnectionServer::GetInstance()};
363     // HfpHfCommandProcessor
364     HfpHfCommandProcessor commandProcessor_ {};
365 
366     BT_DISALLOW_COPY_AND_ASSIGN(HfpHfProfile);
367 };
368 }  // namespace bluetooth
369 }  // namespace OHOS
370 #endif // HFP_HF_PROFILE_H