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_PROFILE_H
17 #define HFP_AG_PROFILE_H
18 
19 #include <cstdint>
20 #include <string>
21 
22 #include "hfp_ag_audio_connection.h"
23 #include "hfp_ag_command_parser.h"
24 #include "hfp_ag_data_connection.h"
25 #include "hfp_ag_data_connection_server.h"
26 #include "hfp_ag_defines.h"
27 #include "hfp_ag_sdp_client.h"
28 
29 namespace OHOS {
30 namespace bluetooth {
31 /**
32  * @brief Class for providing functions to relate to other underlying layers.
33  *        The functions of this layer are called by AG state machine.
34  */
35 class HfpAgProfile {
36 public:
37     typedef struct {
38         int status {0};
39         int call {0};
40         int callsetup {0};
41         int callheld {0};
42         int signal {0};
43         int roam {0};
44         int battchg {0};
45     } CindIndicators;
46 
47     /**
48      * @brief Register SDP service and data connection server for local device only once.
49      */
50     static int RegisterService();
51 
52     /**
53      * @brief Deregister SDP service and data connection server for local device only once.
54      */
55     static int DeregisterService();
56 
57     /**
58      * @brief Check if audio connection established;
59      *
60      * @return Returns <b>true</b> if audio connected; returns <b>false</b> if audio not connected.
61      */
62     static bool IsAudioConnected(const std::string &address);
63 
64     /**
65      * @brief Construct a new HfpAgProfile object.
66      *
67      * @param address Remote device address.
68      */
69     explicit HfpAgProfile(const std::string &address);
70 
71     /**
72      * @brief Destroy the HfpAgProfile object.
73      */
74     ~HfpAgProfile() = default;
75 
76     /**
77      * @brief Initialise the profile object.
78      */
79     void Init();
80 
81     /**
82      * @brief Start a SDP discovery job of remote SDP server.
83      *
84      * @param role Role in connection.
85      * @return Returns the sdp discovery result.
86      */
87     int DoServiceDiscovery(int role, int handle = -1);
88 
89     /**
90      * @brief  Find out attributes required by AG and set the SDP result to data connection.
91      *
92      * @return Returns the find sdp discovery result.
93      */
94     int ServiceDiscoveryResult();
95 
96     /**
97      * @brief Process service level connection established event.
98      */
99     void ProcessSlcEstablished();
100 
101     /**
102      * @brief Init inband ring tone according to features.
103      */
104     void InitInbandRingTone();
105 
106     /**
107      * @brief Periodically send ring & clip to remote device.
108      */
109     void RingTimeout();
110 
111     /**
112      * @brief Establish data connection by AG.
113      *
114      * @return Returns the result of establish data connection.
115      */
116     int EstablishDataConnection();
117 
118     /**
119      * @brief Release the existed data connection by AG.
120      *
121      * @return Returns the result of release data connection.
122      */
123     int ReleaseDataConnection() const;
124 
125     /**
126      * @brief Accept data connection from remote HF device.
127      *
128      * @param handle Data connection handle.
129      * @return Returns the result of accept data connection.
130      */
131     int AcceptDataConnection(uint16_t handle);
132 
133     /**
134      * @brief Read data from data connection.
135      *
136      * @return Returns the result of read data from data connection.
137      */
138     int ReadData();
139 
140     /**
141      * @brief Initiate the establishment of an audio connection to remote HF device by AG.
142      *
143      * @return Returns the result of establish audio data connection.
144      */
145     int EstablishAudioConnection();
146 
147     /**
148      * @brief Select codec between AG and HF.
149      *
150      * @return Returns the result of select codec.
151      */
152     int SetupCodecConnection();
153 
154     /**
155      * @brief Setup codec connection to CVSD.
156      *
157      * @return Returns the result of setup codec.
158      */
159     int SetupCodecCvsd();
160 
161     /**
162      * @brief Release the existed audio connection between AG and HF by AG.
163      *
164      * @return Returns the result of release audio connection.
165      */
166     int ReleaseAudioConnection() const;
167 
168     /**
169      * @brief Process audio connect request.
170      *
171      * @return Returns <b>true</b> if accept audio connection; returns <b>false</b> if reject audio connection.
172      */
173     bool ProcessAudioConnectRequest();
174 
175     /**
176      * @brief Accept audio connection request from HF.
177      *
178      * @return Returns the result of accept audio connection.
179      */
180     int AcceptAudioConnection();
181 
182     /**
183      * @brief Reject audio connection request from HF.
184      *
185      * @return Returns the result of reject audio connection.
186      */
187     int RejectAudioConnection() const;
188 
189     /**
190      * @brief AG shall initiate the audio connection establishment if audio connection is not present.
191      *
192      * @return Returns the result of active voice recognition.
193      */
194     int ActivateVoiceRecognition();
195 
196     /**
197      * @brief Disable Voice recognition in AG.
198      *
199      * @return Returns the result of deactivate voice recognition.
200      */
201     int DeactivateVoiceRecognition() const;
202 
203     /**
204      * @brief Set microphone gain to remote HF device.
205      *
206      * @param val Microphone gain value.
207      * @return Returns the result of set mic value.
208      */
209     int SetMicrophoneGain(int val) const;
210 
211     /**
212      * @brief Set speaker volume to remote HF device.
213      *
214      * @param val Speaker volume value.
215      * @return Returns the result of set speaker value.
216      */
217     int SetSpeakerVolume(int val) const;
218 
219     /**
220      * @brief Set the on/off action of in-band ring tone to remote HF device.
221      *
222      * @param action In-band ring tone on/off action.
223      * @return Returns the result of set in-band ring.
224      */
225     int SetInbandRingTone(int action);
226 
227     /**
228      * @brief Set the active device for audio connection.
229      *
230      * @param address Device address.
231      */
232     static void SetActiveDevice(const std::string &address);
233 
234     /**
235      * @brief Get the active device for audio connection.
236      *
237      * @return Returns the active device.
238      */
239     static std::string GetActiveDevice();
240 
241     /**
242      * @brief AG response to the query of subscriber number from HF.
243      *
244      * @param type Format type of the subscriber number.
245      * @param number Subscriber number.
246      * @param service Service the subscriber number relates to.
247      * @return Returns the result of send subscriber number.
248      */
249     int SendSubscriberNumberInformation(
250         uint16_t type, const std::string &number, int service = HFP_AG_CNUM_SERVICE_VOICE);
251 
252     /**
253      * @brief Send the general and unsolicited AG results codes response.
254      *
255      * @param result Result code type.
256      * @return Returns the result of send result code.
257      */
258     int SendResultCode(int result) const;
259 
260     void startMock(int state);
261 
262     /**
263      * @brief Reports the change of the call status to remote HF device.
264      *
265      * @param call Call status.
266      * @return Returns the result of report call status.
267      */
268     int ReportCallStatus(uint32_t call);
269 
270     /**
271      * @brief Reports the change of the callsetup status to remote HF device.
272      *
273      * @param callsetup Callsetup status.
274      * @return Returns the result of report callsetup status.
275      */
276     int ReportCallsetupStatus(uint32_t callsetup);
277 
278     /**
279      * @brief Reports the change of the callheld status to remote HF device.
280      *
281      * @param callheld Callheld status.
282      * @return Returns the result of report callheld status.
283      */
284     int ReportCallheldStatus(uint32_t callheld);
285 
286     /**
287      * @brief Reports the change of the response hold status to remote HF device.
288      *
289      * @param state Response status.
290      * @return Returns the result of report callheld status.
291      */
292     int ReportResponseHoldStatus(uint32_t state, int test);
293 
294     /**
295      * @brief Reports the change of the registration status to remote HF device.
296      *
297      * @param status Registration status.
298      * @return Returns the result of report registration status.
299      */
300     int ReportRegistrationStatus(uint32_t status);
301 
302     /**
303      * @brief Reports the change of the signal strength to remote HF device.
304      *
305      * @param signal Signal strength.
306      * @return Returns the result of report signal strength.
307      */
308     int ReportSignalStrength(uint32_t signal);
309 
310     /**
311      * @brief Reports the change of the roaming status to remote HF device.
312      *
313      * @param state Roaming status.
314      * @return Returns the result of report roaming status.
315      */
316     int ReportRoamingState(uint32_t state);
317 
318     /**
319      * @brief Reports the change of the battery level to remote HF device.
320      *
321      * @param battery Battery level.
322      * @return Returns the result of report battery level.
323      */
324     int ReportBatteryLevel(uint32_t battery);
325 
326     /**
327      * @brief AG response to the query of network operator name from HF.
328      *
329      * @param operatorName Network operator name.
330      * @return Returns the result of report network operator.
331      */
332     int ReportCurrentNetworkOperator(const std::string &operatorName);
333 
334     /**
335      * @brief Reports the changes of current phone state to remote HF device.
336      *
337      * @param phoneState Current phone state.
338      * @return Returns the result of phone state change.
339      */
340     int PhoneStateChange(const HfpAgPhoneState &phoneState);
341 
342     /**
343      * @brief Reports the list of current calls to remote HF device.
344      *
345      * @param clcc Single current call.
346      * @return Returns the result of report single call list.
347      */
348     int ReportCurrentCallList(const HfpAgCallList &clcc) const;
349 
350     /**
351      * @brief respones ok to hf.
352      *
353      */
354     void ResponesOK() const;
355 
356     /**
357      * @brief Remote the state machine.
358      */
359     void RemoveStateMachine();
360 
361     /**
362      * @brief Send RING and CLIP command to remote HF device.
363      *
364      * @return Returns the result of send ring.
365      */
366     void SendRingAndClip();
367 
368     /**
369      * @brief Set the active device for audio connection.
370      *
371      * @param address Device address.
372      * @return Returns the result of set active device.
373      */
374     void PostAudioConnectionEstablishment();
375 
376     /**
377      * @brief Report indicators to hf.
378      *
379      * @param indicators Audio Gateway indicators.
380      * @return Returns the result report indicators.
381      */
382     int ReportIndicators(const CindIndicators &indicators);
383 
384     /**
385      * @brief Activate/Deactivate indicators notifications.
386      *
387      * @param service service indicator.
388      * @param roam roam indicator.
389      * @param signal signal indicator.
390      * @param battery battery indicator.
391      */
392     void ProcessBia(bool service, bool roam, bool signal, bool battery);
393 
394     /**
395      * @brief Remove scn loging.
396      */
397     void RemoveRemoteScnLoging() const;
398 
399     /**
400      * @brief Reports the presence of new call waiting to remote HF device when there are ongoing calls.
401      *
402      * @param type Format type of the incoming call number.
403      * @param number Incoming call number.
404      * @return Returns the result of notify incoming call waiting.
405      */
406     int NotifyIncomingCallWaiting(uint16_t type, const std::string &number) const;
407 
408     /**
409      * @brief send binp to hf
410      *
411      * @param number voice number
412      */
413     void SendBinp(std::string number) const;
414 
415     /**
416      * @brief Reports the incoming call number to remote HF device after the new call coming.
417      *
418      * @param type Format type of the incoming call number.
419      * @param number Incoming call number.
420      * @return Returns the result of notify calling line identification.
421      */
422     int NotifyCallingLineIdentification(uint16_t type, const std::string &number) const;
423 
424 private:
425     /**
426      * @brief Reports the change of the call status to remote HF device by active call number
427      *        and held call number.
428      *
429      * @param numActive Active call number.
430      * @param numHeld Held call number.
431      * @return Returns the result of report call status by call nums.
432      */
433     int ReportCallStatusByCallNums(int numActive, int numHeld);
434 
435     /**
436      * @brief Reports the change of the callheld status to remote HF device by active call number
437      *        and held call number.
438      *
439      * @param numActive Active call number.
440      * @param numHeld Held call number.
441      * @return Returns the result of report callheld status by call nums.
442      */
443     int ReportCallheldStatusByCallNums(int numActive, int numHeld);
444 
445     /**
446      * @brief Reports the specific indicator to remote HF device.
447      *
448      * @param indicator Indicator type.
449      * @param value Indicator value.
450      * @return Returns the result of report AG indicator.
451      */
452     int ReportAgIndicator(int indicator, int value) const;
453 
454     /**
455      * @brief Send RING command to remote HF device.
456      *
457      * @return Returns the result of send ring.
458      */
459     int SendRing() const;
460 
461     /**
462      * @brief Process phone change when current call state is dialing.
463      *
464      * @param numActive Active call number.
465      * @param numHeld Held call number.
466      * @return Returns the result of process current call state dialing.
467      */
468     int ProcessCurrentCallStateDialing(int numActive, int numHeld);
469 
470     /**
471      * @brief Process phone change when current call state is alerting.
472      *
473      * @return Returns the result of process current call state alerting.
474      */
475     int ProcessCurrentCallStateAlerting();
476 
477     /**
478      * @brief Process phone change when current call state is incoming.
479      *
480      * @param numActive Active call number.
481      * @param numHeld Held call number.
482      * @param type Call type.
483      * @param number Call number.
484      * @return Returns the result of process current call state incoming.
485      */
486     int ProcessCurrentCallStateIncominging(int numActive, int numHeld, uint16_t type, const std::string &number);
487 
488     /**
489      * @brief Process phone change when current call state is idle.
490      *
491      * @param numActive Active call number.
492      * @param numHeld Held call number.
493      * @return Returns the result of process current call state idle.
494      */
495     int ProcessCurrentCallStateIdle(int numActive, int numHeld);
496 
497     /**
498      * @brief Process phone change when current call state is idle and previous call state is dialing or alerting.
499      *
500      * @param numActive Active call number.
501      * @param numHeld Held call number.
502      * @return Returns the result of process current call state idle and previous dialing or alerting.
503      */
504     int ProcessPreviousCallStateDialingAlerting(int numActive, int numHeld);
505 
506     /**
507      * @brief Process phone change when current call state is idle and previous call state is incoming.
508      *
509      * @param numActive Active call number.
510      * @param numHeld Held call number.
511      * @return Returns the result of process current call state idle and previous incoming.
512      */
513     int ProcessPreviousCallStateIncoming(int numActive, int numHeld);
514 
515     /**
516      * @brief Process phone change when current call state is idle and previous call state is idle.
517      *
518      * @param numActive Active call number.
519      * @param numHeld Held call number.
520      * @return Returns the result of process current call state idle and previous idle.
521      */
522     int ProcessPreviousCallStateIdle(int numActive, int numHeld);
523 
524     /**
525      * @brief Set the call states, including held calls number, active calls number and current call state.
526      *
527      * @param numHeld Held calls number.
528      * @param numActive Active calls number.
529      * @param callState Current call state.
530      */
531     void SetCallStates(int numHeld, int numActive, int callState);
532 
533     /**
534      * @brief Get the call state name.
535      *
536      * @param state Call state number.
537      * @return Returns the name of the call state.
538      */
539     static std::string GetCallState(int state);
540 
541     inline static const uint16_t HFP_AG_COPS_MODE_SET_FORMAT = 3;
542     inline static const uint16_t HFP_AG_COPS_FORMAT_LONG = 0;
543 
544     // Remote device address
545     std::string address_;
546 
547     int tempRfcommHandle_ {-1};
548 
549     // Last held calls number
550     int preNumHeldCalls_ {0};
551 
552     // Last active calls number
553     int preNumActiveCalls_ {0};
554 
555     // Last call state
556     int preCallState_ {HFP_AG_CALL_STATE_IDLE};
557 
558     // Need Send ring after sco connected
559     bool scoPostProcess_ {false};
560 
561     // HfpAgSdpClient object
562     HfpAgSdpClient sdpClient_ {};
563 
564     // HfpAgDataConnection object
565     HfpAgDataConnection dataConn_ {};
566 
567     // HfpAgAudioConnection object
568     HfpAgAudioConnection audioConn_ {};
569 
570     // HfpAgDataConnectionServer instance
571     HfpAgDataConnectionServer &dataServer_ {HfpAgDataConnectionServer::GetInstance()};
572 
573     // HfpAgCommandProcessor instance
574     HfpAgCommandProcessor &commandProcessor_ {HfpAgCommandProcessor::GetInstance()};
575 
576     int mockState_ {0};
577     BT_DISALLOW_COPY_AND_ASSIGN(HfpAgProfile);
578 };
579 }  // namespace bluetooth
580 }  // namespace OHOS
581 #endif // HFP_AG_PROFILE_H