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 TEL_RIL_MANAGER_H
17 #define TEL_RIL_MANAGER_H
18 
19 #include <singleton.h>
20 #include <v1_3/iril.h>
21 
22 #include "hdf_service_status_listener.h"
23 #include "i_tel_ril_manager.h"
24 #include "tel_ril_call.h"
25 #include "tel_ril_data.h"
26 #include "tel_ril_handler.h"
27 #include "tel_ril_modem.h"
28 #include "tel_ril_network.h"
29 #include "tel_ril_sim.h"
30 #include "tel_ril_sms.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 class TelRilManager : public ITelRilManager, public std::enable_shared_from_this<TelRilManager> {
35 public:
36     TelRilManager();
37     ~TelRilManager() override = default;
38 
39     bool OnInit() override;
40     bool DeInit();
41 
42     int32_t RegisterCoreNotify(
43         int32_t slotId, const std::shared_ptr<AppExecFwk::EventHandler> &handler, int32_t what, int32_t *obj) override;
44     int32_t UnRegisterCoreNotify(
45         int32_t slotId, const std::shared_ptr<AppExecFwk::EventHandler> &observerCallBack, int32_t what) override;
46 
47     int32_t SetRadioState(
48         int32_t slotId, int32_t fun, int32_t rst, const AppExecFwk::InnerEvent::Pointer &response) override;
49     int32_t GetRadioState(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
50 
51     int32_t ShutDown(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
52 
53     int32_t GetCallList(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
54 
55     int32_t Dial(
56         int32_t slotId, std::string address, int32_t clirMode, const AppExecFwk::InnerEvent::Pointer &result) override;
57 
58     int32_t Reject(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
59 
60     int32_t Hangup(int32_t slotId, int32_t gsmIndex, const AppExecFwk::InnerEvent::Pointer &result) override;
61 
62     int32_t Answer(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
63 
64     int32_t HoldCall(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
65 
66     int32_t UnHoldCall(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
67 
68     int32_t SwitchCall(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
69 
70     int32_t CombineConference(int32_t slotId, int32_t callType, const AppExecFwk::InnerEvent::Pointer &result) override;
71 
72     int32_t SeparateConference(
73         int32_t slotId, int32_t callIndex, int32_t callType, const AppExecFwk::InnerEvent::Pointer &result) override;
74 
75     int32_t CallSupplement(int32_t slotId, int32_t type, const AppExecFwk::InnerEvent::Pointer &result) override;
76 
77     int32_t GetCallWaiting(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
78 
79     int32_t SetCallWaiting(
80         int32_t slotId, const int32_t activate, const AppExecFwk::InnerEvent::Pointer &result) override;
81 
82     int32_t GetCallTransferInfo(
83         int32_t slotId, const int32_t reason, const AppExecFwk::InnerEvent::Pointer &result) override;
84 
85     int32_t SetCallTransferInfo(
86         int32_t slotId, const CallTransferParam &callTransfer, const AppExecFwk::InnerEvent::Pointer &result) override;
87 
88     int32_t GetClip(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
89 
90     int32_t SetClip(int32_t slotId, const int32_t action, const AppExecFwk::InnerEvent::Pointer &result) override;
91 
92     int32_t GetClir(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
93 
94     int32_t SetClir(int32_t slotId, const int32_t action, const AppExecFwk::InnerEvent::Pointer &result) override;
95 
96     int32_t GetCallRestriction(int32_t slotId, std::string fac, const AppExecFwk::InnerEvent::Pointer &result) override;
97 
98     int32_t SetCallRestriction(int32_t slotId, const CallRestrictionParam &callRestriction,
99         const AppExecFwk::InnerEvent::Pointer &result) override;
100 
101     int32_t SetBarringPassword(int32_t slotId, const char *oldPassword, const char *newPassword,
102         const std::string &restrictionType, const AppExecFwk::InnerEvent::Pointer &response) override;
103 
104     int32_t SetVoNRSwitch(int32_t slotId, int32_t state, const AppExecFwk::InnerEvent::Pointer &result) override;
105 
106     int32_t SendDtmf(
107         int32_t slotId, const DtmfParam &dtmfParam, const AppExecFwk::InnerEvent::Pointer &result) override;
108     int32_t SendDtmf(
109         int32_t slotId, char cDTMFCode, int32_t index, const AppExecFwk::InnerEvent::Pointer &result) override;
110     int32_t StartDtmf(
111         int32_t slotId, char cDTMFCode, int32_t index, const AppExecFwk::InnerEvent::Pointer &result) override;
112     int32_t StopDtmf(int32_t slotId, int32_t index, const AppExecFwk::InnerEvent::Pointer &result) override;
113 
114     int32_t GetSignalStrength(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
115 
116     int32_t GetCsRegStatus(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
117 
118     int32_t GetPsRegStatus(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
119 
120     int32_t GetOperatorInfo(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
121 
122     int32_t GetCellInfoList(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
123 
124     int32_t GetCurrentCellInfo(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
125 
126     int32_t GetImei(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
127 
128     int32_t GetImeiSv(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
129 
130     int32_t GetMeid(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
131 
132     int32_t GetVoiceRadioTechnology(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
133 
134     int32_t GetPhysicalChannelConfig(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
135 
136     int32_t SetLocateUpdates(
137         int32_t slotId, RegNotifyMode mode, const AppExecFwk::InnerEvent::Pointer &response) override;
138 
139     /**
140      * @param slotId is the card slot index number
141      * @param newFilter is the notification filter with bits in NotificationFilter
142      * @param response is the feedback info after setting notification filter
143      * @return int32_t Indicates if notification filter is set successfully
144      */
145     int32_t SetNotificationFilter(
146         int32_t slotId, int32_t newFilter, const AppExecFwk::InnerEvent::Pointer &response) override;
147 
148     /**
149      * @param slotId is the card slot index number
150      * @param deviceStateType is the device state type in DeviceStateType
151      * @param deviceStateOn Indicates the specific device state is on
152      * @param response is the feedback info after setting device state
153      * @return int32_t Indicates if device state is set successfully
154      */
155     int32_t SetDeviceState(int32_t slotId, int32_t deviceStateType, bool deviceStateOn,
156         const AppExecFwk::InnerEvent::Pointer &response) override;
157 
158     int32_t GetBasebandVersion(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
159 
160     int32_t SetNrOptionMode(int32_t slotId, int32_t mode, const AppExecFwk::InnerEvent::Pointer &response) override;
161 
162     int32_t GetNrOptionMode(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
163 
164     int32_t GetRrcConnectionState(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
165 
166     int32_t GetNrSsbId(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
167 
168     int32_t SendGsmSms(
169         int32_t slotId, std::string smscPdu, std::string pdu, const AppExecFwk::InnerEvent::Pointer &response) override;
170 
171     int32_t SendCdmaSms(int32_t slotId, std::string pdu, const AppExecFwk::InnerEvent::Pointer &response) override;
172 
173     int32_t AddSimMessage(
174         int32_t slotId, const SimMessageParam &simMessage, const AppExecFwk::InnerEvent::Pointer &response) override;
175 
176     int32_t DelSimMessage(int32_t slotId, int32_t gsmIndex, const AppExecFwk::InnerEvent::Pointer &response) override;
177 
178     int32_t UpdateSimMessage(
179         int32_t slotId, const SimMessageParam &simMessage, const AppExecFwk::InnerEvent::Pointer &response) override;
180 
181     int32_t GetSmscAddr(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
182 
183     int32_t GetCdmaCBConfig(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
184 
185     int32_t SetSmscAddr(
186         int32_t slotId, int32_t tosca, std::string address, const AppExecFwk::InnerEvent::Pointer &response) override;
187 
188     int32_t SetCBConfig(
189         int32_t slotId, const CBConfigParam &cbConfig, const AppExecFwk::InnerEvent::Pointer &response) override;
190 
191     int32_t SetCdmaCBConfig(int32_t slotId, CdmaCBConfigInfoList &cdmaCBConfigInfoList,
192         const AppExecFwk::InnerEvent::Pointer &response) override;
193 
194     int32_t GetCBConfig(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
195 
196     int32_t SendSmsMoreMode(
197         int32_t slotId, std::string smscPdu, std::string pdu, const AppExecFwk::InnerEvent::Pointer &response) override;
198 
199     int32_t SendSmsAck(
200         int32_t slotId, bool success, int32_t cause, const AppExecFwk::InnerEvent::Pointer &response) override;
201     int32_t AddCdmaSimMessage(
202         int32_t slotId, int32_t status, std::string pdu, const AppExecFwk::InnerEvent::Pointer &response) override;
203     int32_t DelCdmaSimMessage(
204         int32_t slotId, int32_t cdmaIndex, const AppExecFwk::InnerEvent::Pointer &response) override;
205     int32_t UpdateCdmaSimMessage(int32_t slotId, const CdmaSimMessageParam &cdmaSimMsg,
206         const AppExecFwk::InnerEvent::Pointer &response) override;
207 
208     int32_t SetInitApnInfo(
209         int32_t slotId, const DataProfile &dataProfile, const AppExecFwk::InnerEvent::Pointer &response) override;
210     int32_t ActivatePdpContext(
211         int32_t slotId, const ActivateDataParam &activeData, const AppExecFwk::InnerEvent::Pointer &response) override;
212     int32_t DeactivatePdpContext(
213         int32_t slotId, int32_t cid, int32_t reason, const AppExecFwk::InnerEvent::Pointer &response) override;
214     int32_t GetPdpContextList(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
215     int32_t GetLinkBandwidthInfo(
216         int32_t slotId, const int32_t cid, const AppExecFwk::InnerEvent::Pointer &response) override;
217     int32_t SetLinkBandwidthReportingRule(
218         int32_t slotId, LinkBandwidthRule linkBandwidth, const AppExecFwk::InnerEvent::Pointer &response) override;
219     int32_t SetDataPermitted(
220         int32_t slotId, int32_t dataPermitted, const AppExecFwk::InnerEvent::Pointer &response) override;
221     int32_t GetLinkCapability(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
222     int32_t CleanAllConnections(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
223 
224     int32_t GetSimStatus(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
225     int32_t GetSimIO(int32_t slotId, SimIoRequestInfo data, const AppExecFwk::InnerEvent::Pointer &response) override;
226     int32_t GetImsi(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
227     int32_t GetSimLockStatus(int32_t slotId, std::string fac, const AppExecFwk::InnerEvent::Pointer &result) override;
228     int32_t SetSimLock(
229         int32_t slotId, const SimLockParam &simLock, const AppExecFwk::InnerEvent::Pointer &result) override;
230     int32_t ChangeSimPassword(
231         int32_t slotId, const SimPasswordParam &simPassword, const AppExecFwk::InnerEvent::Pointer &result) override;
232     int32_t UnlockPin(int32_t slotId, const std::string &pin, const AppExecFwk::InnerEvent::Pointer &result) override;
233     int32_t UnlockPuk(int32_t slotId, const std::string &puk, const std::string &pin,
234         const AppExecFwk::InnerEvent::Pointer &result) override;
235     int32_t UnlockPin2(int32_t slotId, const std::string &pin2, const AppExecFwk::InnerEvent::Pointer &result) override;
236     int32_t UnlockPuk2(int32_t slotId, const std::string &puk2, const std::string &pin2,
237         const AppExecFwk::InnerEvent::Pointer &result) override;
238     int32_t SetActiveSim(
239         int32_t slotId, int32_t index, int32_t enable, const AppExecFwk::InnerEvent::Pointer &result) override;
240     int32_t SendTerminalResponseCmd(
241         int32_t slotId, const std::string &strCmd, const AppExecFwk::InnerEvent::Pointer &response) override;
242     int32_t SendEnvelopeCmd(
243         int32_t slotId, const std::string &strCmd, const AppExecFwk::InnerEvent::Pointer &response) override;
244     int32_t SendCallSetupRequestResult(
245         int32_t slotId, bool accept, const AppExecFwk::InnerEvent::Pointer &response) override;
246     int32_t SimStkIsReady(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
247     int32_t UnlockSimLock(int32_t slotId, int32_t lockType, std::string password,
248         const AppExecFwk::InnerEvent::Pointer &response) override;
249     int32_t GetRadioProtocol(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
250     int32_t SetRadioProtocol(
251         int32_t slotId, RadioProtocol radioProtocol, const AppExecFwk::InnerEvent::Pointer &response) override;
252 
253     int32_t GetNetworkSearchInformation(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
254     int32_t GetNetworkSelectionMode(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &result) override;
255     int32_t SetNetworkSelectionMode(int32_t slotId, int32_t automaticFlag, std::string oper,
256         const AppExecFwk::InnerEvent::Pointer &result) override;
257     int32_t SetPreferredNetwork(
258         int32_t slotId, int32_t preferredNetworkType, const AppExecFwk::InnerEvent::Pointer &response) override;
259     int32_t GetPreferredNetwork(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
260 
261     int32_t SetCallPreferenceMode(
262         int32_t slotId, const int32_t mode, const AppExecFwk::InnerEvent::Pointer &response) override;
263     int32_t GetCallPreferenceMode(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
264     int32_t SetUssd(int32_t slotId, const std::string str, const AppExecFwk::InnerEvent::Pointer &response) override;
265     int32_t GetUssd(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
266     int32_t SetMute(int32_t slotId, const int32_t mute, const AppExecFwk::InnerEvent::Pointer &response) override;
267     int32_t GetMute(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
268     int32_t GetEmergencyCallList(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
269     int32_t SetEmergencyCallList(int32_t slotId, const std::vector<EmergencyCall> &eccVec,
270         const AppExecFwk::InnerEvent::Pointer &response) override;
271     int32_t GetCallFailReason(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
272     int32_t SimOpenLogicalChannel(int32_t slotId, const std::string &appID, const int32_t p2,
273         const AppExecFwk::InnerEvent::Pointer &response) override;
274     int32_t SimCloseLogicalChannel(
275         int32_t slotId, const int32_t channelId, const AppExecFwk::InnerEvent::Pointer &response) override;
276     int32_t SimTransmitApduLogicalChannel(
277         int32_t slotId, const ApduSimIORequestInfo &reqInfo, const AppExecFwk::InnerEvent::Pointer &response) override;
278     int32_t SimTransmitApduBasicChannel(
279         int32_t slotId, const ApduSimIORequestInfo &reqInfo, const AppExecFwk::InnerEvent::Pointer &response) override;
280     int32_t SimAuthentication(int32_t slotId, const SimAuthenticationRequestInfo &reqInfo,
281         const AppExecFwk::InnerEvent::Pointer &response) override;
282     int32_t SendSimMatchedOperatorInfo(int32_t slotId, const NcfgOperatorInfo &reqInfo,
283         const AppExecFwk::InnerEvent::Pointer &response) override;
284     int32_t CloseUnFinishedUssd(int32_t slotId, const AppExecFwk::InnerEvent::Pointer &response) override;
285     int32_t InitTelExtraModule(int32_t slotId) override;
286 
287     /**
288      * Register hdf status listener
289      *
290      * @return:True is Register HdfStatusListener success, false is fail
291      */
292     bool RegisterHdfStatusListener();
293 
294     /**
295      * UnRegister hdf status listener
296      *
297      * @return:True is UnRegister HdfStatusListener success, false is fail
298      */
299     bool UnRegisterHdfStatusListener();
300 
301     std::shared_ptr<TelRilSms> GetTelRilSms(int32_t slotId);
302     std::shared_ptr<TelRilSim> GetTelRilSim(int32_t slotId);
303     std::shared_ptr<TelRilCall> GetTelRilCall(int32_t slotId);
304     std::shared_ptr<TelRilData> GetTelRilData(int32_t slotId);
305     std::shared_ptr<TelRilNetwork> GetTelRilNetwork(int32_t slotId);
306     std::shared_ptr<TelRilModem> GetTelRilModem(int32_t slotId);
307     void SendAckAndLock(void);
308     void ReduceRunningLock();
309     void ReleaseRunningLock();
310     int32_t GetTelRilSimSize();
311     int32_t GetTelRilModemSize();
312 
313 public:
314     static const int32_t INVALID_WAKELOCK = -1;
315     static const int32_t FOR_WAKELOCK = 0;
316     static const int32_t FOR_ACK_WAKELOCK = 1;
317 
318 private:
319     void CreatTelRilHandler(void);
320     int32_t SendResponseAck(void);
321     void InitTelModule(int32_t slotId);
322     bool ConnectRilInterface();
323     bool ResetRilInterface(void);
324     void HandleRilInterfaceStatusCallback(const OHOS::HDI::ServiceManager::V1_0::ServiceStatus &status);
325     bool ReConnectRilInterface();
326     bool DisConnectRilInterface();
327     void ResetRilInterfaceBySlotId(int32_t slotId);
328 
329     std::shared_ptr<ObserverHandler> GetObserverHandler(int32_t slotId);
330 
331     /**
332      * @brief Task schedule. The function of this function is to unify the input interface.
333      * @param _result response handler
334      * @param _module Sub module identification, used to print logs.
335      *   This header file cannot use the typeid() keyword, so the module name is passed in.
336      * @param template: _obj - Object type (this)pointer.
337      * @param template: _func - Class member function address.
338      * @param template: _args - The parameter list of the class member function except the response parameter.
339      *    The number can vary.
340      * @return true/false - success/fail
341      */
342     template<typename ResponsePtr, typename ClassTypePtr, typename FuncType, typename... ParamTypes>
TaskSchedule(ResponsePtr & _result,const std::string _module,ClassTypePtr _obj,FuncType && _func,ParamTypes &&..._args)343     inline int32_t TaskSchedule(ResponsePtr &_result, const std::string _module, ClassTypePtr _obj, FuncType &&_func,
344         ParamTypes &&... _args) const
345     {
346         if (_func != nullptr && _obj != nullptr) {
347             // The reason for using native member function access here is to
348             //   remove std::unique_ptr to prevent copying.
349             // The reason for not directly using pointers to access member functions is:
350             //   _obj is a smart pointer, not a native pointer.
351             return (_obj.get()->*(_func))(std::forward<ParamTypes>(_args)..., _result);
352         } else {
353             TELEPHONY_LOGE("%{public}s - func: %{public}s", _module.c_str(), "null pointer");
354             return TELEPHONY_ERR_LOCAL_PTR_NULL;
355         }
356     }
357 
358 private:
359     std::mutex mutex_;
360     std::mutex telRilMutex_;
361     std::vector<std::shared_ptr<TelRilSim>> telRilSim_;
362     std::vector<std::shared_ptr<TelRilSms>> telRilSms_;
363     std::vector<std::shared_ptr<TelRilCall>> telRilCall_;
364     std::vector<std::shared_ptr<TelRilData>> telRilData_;
365     std::vector<std::shared_ptr<TelRilModem>> telRilModem_;
366     std::vector<std::shared_ptr<TelRilNetwork>> telRilNetwork_;
367     std::vector<std::shared_ptr<ObserverHandler>> observerHandler_;
368     std::shared_ptr<TelRilHandler> handler_ = nullptr;
369     sptr<OHOS::HDI::ServiceManager::V1_0::IServiceManager> servMgr_ = nullptr;
370     sptr<HdfServiceStatusListener::IServStatListener> hdfListener_ = nullptr;
371     sptr<HDI::Ril::V1_3::IRil> rilInterface_ = nullptr;
372 };
373 } // namespace Telephony
374 } // namespace OHOS
375 #endif // TEL_RIL_MANAGER_H
376