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_SYSTEM_INTERFACE_H
17 #define HFP_AG_SYSTEM_INTERFACE_H
18 
19 #include <string>
20 #include "base_def.h"
21 #include "hfp_ag_defines.h"
22 #include "telephony_observer.h"
23 #include "hfp_ag_message.h"
24 #include "system_ability_status_change_stub.h"
25 
26 namespace OHOS {
27 namespace bluetooth {
28 /**
29  * @brief  Defines the interfaces that is called by HFP.
30  */
31 class SystemAbilityStatusChange : public SystemAbilityStatusChangeStub {
32 public:
33     void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
34     void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
35 };
36 
37 class HfpAgSystemInterface {
38 public:
39     /**
40      * @brief Get the Instance of the HfpAgSystemInterface.
41      *
42      * @return Returns the Instance of the HfpAgSystemInterface.
43      */
44     static HfpAgSystemInterface &GetInstance();
45 
46     /**
47      * @brief Starts the module of the System interface.
48      */
49     void Start();
50 
51     /**
52      * @brief Stops the module of the System interface.
53      */
54     void Stop();
55 
56     /**
57      * @brief Receive the command from HF and reject the incoming call.
58      *
59      * @param address The address of the bluetooth device.
60      */
61     void RejectCall(const std::string &address) const;
62 
63     /**
64      * @brief Receive the command from HF and dial out the call with the number.
65      *
66      * @param address The address of the bluetooth Device
67      * @param number The number of the call.
68      */
69     void DialOutCall(const std::string &address, const std::string &number) const;
70 
71     /**
72      * @brief Receive the command from HF and hangup the call.
73      *
74      * @param address The address of the bluetooth Device
75      */
76     void HangupCall(const std::string &address) const;
77 
78     /**
79      * @brief Receive the command from HF and answer the incoming call.
80      *
81      * @param address The address of the bluetooth Device
82      */
83     void AnswerCall(const std::string &address) const;
84 
85     /**
86      * @brief Instructs Telecom to play the specified DTMF tone for the current foreground call.
87      *
88      * @param dtmf The code of the DTMF.
89      * @param address The address of the bluetooth device.
90      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
91      */
92     bool SendDtmf(int dtmf, const std::string &address) const;
93 
94     /**
95      * @brief Receives the command of HF and instructs the telecom to hold the current call.
96      *
97      * @param chld The value of the chld.
98      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
99      */
100     bool HoldCall(int chld) const;
101 
102     /**
103      * @brief Receives the command of HF and get the name of the network operator from the telecom.
104      *
105      * @return Returns the name of the network operator.
106      */
107     std::string GetNetworkOperator();
108 
109     /**
110      * @brief Receives the command of HF and get the number of the phone from the telecom.
111      *
112      * @return Returns the number of the phone.
113      */
114     std::string GetSubscriberNumber();
115 
116     /**
117      * @brief Receives the command of HF and asks the telecom to respone the current list through the CLCC.
118      *
119      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
120      */
121     bool QueryCurrentCallsList(const std::string &address);
122 
123     /**
124      * @brief Receives the command of HF and asks the telecom to respone the current call state.
125      */
126     void QueryPhoneState() const;
127 
128     /**
129      * @brief Receives the command of HF and starts the voice recognition.
130      *
131      * @param address The address of the bluetooth Device
132      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
133      */
134     bool StartVoiceRecognition(const std::string &address) const;
135 
136     /**
137      * @brief Receives the command of HF and stops the voice recognition.
138      *
139      * @param address The address of the bluetooth Device
140      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
141      */
142     bool StopVoiceRecogition(const std::string &address) const;
143 
144     /**
145      * @brief Set the parameter to the audio.
146      *
147      * @param parameters Audio parameters.
148      * @return Returns <b>true</b> if the operation is successful; returns <b>false</b> if the operation fails.
149      */
150     bool SetAudioParameters(const std::string &parameters) const;
151 
152     /**
153      * @brief Set the stream volume.
154      *
155      * @param streamType The type of the stream.
156      * @param volume The value of the volume.
157      * @param flag The flag of volume.
158      */
159     void SetStreamVolume(int streamType, int volume, int flag) const;
160 
161     /**
162      * @brief Get the last dialed number.
163      *
164      * @return Returns the last dialed number.
165      */
166     std::string GetLastDialNumber();
167 
168     /**
169      * @brief Get the service state object.
170      *
171      * @return Returns the value of the state.
172      */
173     int GetServiceState();
174 
175     /**
176      * @brief Get the signal strength.
177      *
178      * @return Returns the value of the signal.
179      */
180     int GetSignalStrength();
181 
182     /**
183      * @brief Get the roaming state.
184      *
185      * @return Returns the value of the roaming.
186      */
187     int GetRoamState();
188 
189     /**
190      * @brief Get the battery level.
191      *
192      * @return Returns the value of the battery level.
193      */
194     int GetBatteryLevel();
195 
196     /**
197      * @brief Notify ag indicator from the phone.
198      */
199     void QueryAgIndicator();
200 
201     /**
202      * @brief Update ag indicator from the phone.
203      */
204     void UpdateAgIndicator() const;
205 
206     /**
207      * @brief update vluetooth call list
208      *
209      */
210     void UpdateCallList();
211 
212     /**
213      * @brief Get the Call With State object
214      *
215      * @param state call state
216      * @return int call id
217      */
218     int GetCallWithState(int state) const;
219 
220     /**
221      * @brief Get voice numner.
222      */
223     void GetVoiceNumber();
224 
225     /**
226      * @brief Get response hold state.
227      */
228     void GetResponseHoldState(std::string address);
229 
230     /**
231      * @brief Get response hold state.
232      */
233     void SetResponseHoldState(std::string address, int btrh);
234 
235     /**
236      * @brief Get response hold state.
237      */
238     void HandlePhoneStateMock(std::string number, int state, int type);
239 
240     /**
241      * @brief Notify the subscription state changed.
242      *
243      * @param state The network availabel flag.
244      */
245     void OnSubscriptionStateChanged(int state);
246 
247     /**
248      * @brief Notify the signal strength.
249      *
250      * @param signal The signal strength.(Always between 0 and 5)
251      */
252     void OnSignalStrengthChanged(int signal);
253 
254     /**
255      * @brief Notify the roam state.
256      *
257      * @param state The phone roaming flag.
258      */
259     void OnRoamStateChanged(int state);
260 
261     /**
262      * @brief Notify the battery level.
263      *
264      * @param level Battery level.(Ranges from 0 to 5)
265      */
266     void OnBatteryLevel(int level);
267 
268     /**
269      * @brief Send the hf indicator.
270      *
271      * @param address The bluetooth device address.
272      * @param indId The id of the indicator.
273      * @param indValue The value of the indicator.
274      */
275     void SendHfIndicator(const std::string &address, int indId, int indValue) const;
276 
277     /**
278      * @brief Set the active calls Number.
279      *
280      * @param number The number of active calls.
281      */
282     void SetActiveCallNumber(int number);
283 
284     /**
285      * @brief Get the active calls number.
286      *
287      * @return Returns the number of active calls.
288      */
289     int GetActiveCallNumber() const;
290 
291     /**
292      * @brief Set the held calls number.
293      *
294      * @param number The number of held calls.
295      */
296     void SetHeldCallNumber(int number);
297 
298     /**
299      * @brief Get the Held Call Number.
300      *
301      * @return Returns the number of held call.
302      */
303     int GetHeldCallNumber() const;
304 
305     /**
306      * @brief Set the call state.
307      *
308      * @param state The status of the call.
309      */
310     void SetCallState(int state);
311 
312     /**
313      * @brief Get the call state.
314      *
315      * @return Returns the status of the call.
316      */
317     int GetCallState() const;
318 
319     /**
320      * @brief Check if it is in a call.
321      *
322      * @return Returns if is in a call flag.
323      */
324     bool IsInCall() const;
325 
326     /**
327      * @brief Check if there is an incoming call.
328      *
329      * @return Returns is incoming call flag.
330      */
331     bool IsRinging() const;
332 
333     /**
334      * @brief handle chld command
335      *
336      * @param chld chld operation
337      * @return true operation success
338      * @return false operation failed
339      */
340     bool HandleChld(int chld) const;
341 
342 private:
343     /**
344      * @brief Construct a new HfpAgSystemInterface object.
345      */
346     HfpAgSystemInterface();
347 
348     /**
349      * @brief Destroy the HfpAgSystemInterface object.
350      */
351     ~HfpAgSystemInterface() = default;
352 
353     /**
354      * @brief Send the service state to the service.
355      */
356     void SendServiceStateToService() const;
357 
358     /**
359      * @brief Send the service state to the service.
360      */
361     void SendIncomingCallToService(std::string number, int type) const;
362 
363     /**
364      * @brief send cmd in mock state
365      *
366      * @param evt ag event
367      */
368     void SendMockEvent(HfpAgMessage evt) const;
369 
370     /**
371      * @brief send mock clip command
372      *
373      * @param call number
374      */
375     void SendClip(std::string number) const;
376 
377     /**
378      * @brief Send the service state to the service.
379      */
380     void SendCallSetupToService(int state) const;
381 
382     /**
383      * @brief Send the call held state to the service.
384      */
385     void SendCallHeldToService(int state) const;
386 
387     /**
388      * @brief Send the calle state to the service.
389      */
390     void SendCallStateToService(int state) const;
391 
392     /**
393      * @brief Send response hold state to the service.
394      */
395     void SendResponseHoldStateToService(int state) const;
396 
397     /**
398      * @brief Send the roam state to the service.
399      */
400     void SendRoamStateToService() const;
401 
402     /**
403      * @brief Send the signal strength to the service.
404      */
405     void SendSignalStrengthToService() const;
406 
407     /**
408      * @brief Send the battery level to the service.
409      */
410     void SendBatteryLevelToService() const;
411 
412     /**
413      * @brief Register a TelephonyObserver to the telephony subsystem.
414      */
415     void RegisterObserver();
416 
417     /**
418      * @brief Unregister a TelephonyObserver to the telephony subsystem.
419      */
420     void UnregisterObserver();
421 
422     /**
423      * @brief add for pts chld command
424      *
425      * @param chld  chle operation
426      * @return true success
427      * @return false  failed
428      */
429     bool HandleChldMock(int chld) const;
430 
431     /**
432      * @brief add for pts get call list
433      *
434      * @param address device address
435      * @return true success
436      * @return false failed
437      */
438     bool HandleClccMock(std::string address) const;
439 
440     /**
441      * @brief Convert from string to u16string
442      */
443 
444     inline std::u16string Str8ToStr16(const std::string& str) const;
445 
446     /**
447      * @brief Convert from u16string to string
448      */
449     inline std::string Str16ToStr8(const std::u16string& str) const;
450 
451     // The state of the service.
452     int serviceState_ {0};
453 
454     // The signal strength.
455     int signalStrength_ {0};
456 
457     // The roam state.
458     int roamState_ {0};
459 
460     // The battery level.
461     int batteryLevel_ {0};
462 
463     // The number of active call.
464     int activeNum_ {0};
465 
466     // The number of held call.
467     int heldNum_ {0};
468 
469     // The status of the call.
470     int callState_ {HFP_AG_CALL_STATE_DISCONNECTED};
471 
472     // The primary slot Id
473     int slotId_ {0};
474 
475     // The number of the sim card
476     std::string subscriberNumber_ {""};
477 
478     // The vendor name
479     std::string operatorName_ {""};
480 
481     class AgTelephonyObserver;
482 
483     // For pts mock call list
484     std::vector<MockCall> callList_;
485 
486     // bluetooth call list
487     std::vector<BluetoothCall> bluetoothCallList_;
488 
489     sptr<SystemAbilityStatusChange> statusChangeListener_;
490 
491      // The observer that implements TelephonyObserver
492     OHOS::sptr<AgTelephonyObserver> observer_ {nullptr};
493 };
494 class HfpAgSystemInterface::AgTelephonyObserver : public OHOS::Telephony::TelephonyObserver {
495 public:
AgTelephonyObserver(HfpAgSystemInterface & interface)496     explicit AgTelephonyObserver(HfpAgSystemInterface &interface) : interface_(interface)
497     {}
~AgTelephonyObserver()498     ~AgTelephonyObserver() override
499     {}
500 
OnCellularDataConnectStateUpdated(int32_t slotId,int32_t dataState,int32_t networkType)501     void OnCellularDataConnectStateUpdated(int32_t slotId, int32_t dataState, int32_t networkType) override
502     {}
OnCallStateUpdated(int32_t slotId,int32_t callState,const std::u16string & phoneNumber)503     void OnCallStateUpdated(int32_t slotId, int32_t callState, const std::u16string &phoneNumber) override
504     {}
505     void OnSignalInfoUpdated(int32_t slotId,
506         const std::vector<OHOS::sptr<OHOS::Telephony::SignalInformation>> &vec) override;
507 
508     void OnNetworkStateUpdated(int32_t slotId, const OHOS::sptr<OHOS::Telephony::NetworkState> &networkState) override;
509 
OnCellInfoUpdated(int32_t slotId,const std::vector<OHOS::sptr<OHOS::Telephony::CellInformation>> & vec)510     void OnCellInfoUpdated(int32_t slotId,
511         const std::vector<OHOS::sptr<OHOS::Telephony::CellInformation>> &vec) override
512     {}
OnSimStateUpdated(int32_t slotId,OHOS::Telephony::CardType type,OHOS::Telephony::SimState state,OHOS::Telephony::LockReason reason)513     void OnSimStateUpdated(int32_t slotId, OHOS::Telephony::CardType type, OHOS::Telephony::SimState state,
514         OHOS::Telephony::LockReason reason) override
515     {}
OnCellularDataFlowUpdated(int32_t slotId,int32_t dataFlowType)516     void OnCellularDataFlowUpdated(int32_t slotId, int32_t dataFlowType) override
517     {}
518 
519 private:
520     HfpAgSystemInterface &interface_;
521     BT_DISALLOW_COPY_AND_ASSIGN(AgTelephonyObserver);
522 };
523 }  // namespace bluetooth
524 }  // namespace OHOS
525 #endif // HFP_AG_SYSTEM_INTERFACE_H