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 #include "supplement_request_cs.h"
16 
17 #include "cellular_call_service.h"
18 #include "radio_event.h"
19 #include "securec.h"
20 #include "telephony_log_wrapper.h"
21 #include "telephony_types.h"
22 
23 namespace OHOS {
24 namespace Telephony {
GetClipRequest(int32_t slotId,int32_t index)25 int32_t SupplementRequestCs::GetClipRequest(int32_t slotId, int32_t index)
26 {
27     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
28     AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_GET_CALL_CLIP, index);
29     if (response == nullptr) {
30         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
31         return TELEPHONY_ERR_LOCAL_PTR_NULL;
32     }
33     response->SetOwner(GetMMIHandler(slotId));
34     return CoreManagerInner::GetInstance().GetClip(slotId, response);
35 }
36 
SetClirRequest(int32_t slotId,int32_t action,int32_t index)37 int32_t SupplementRequestCs::SetClirRequest(int32_t slotId, int32_t action, int32_t index)
38 {
39     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
40     AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_CALL_CLIR, index);
41     if (response == nullptr) {
42         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
43         return TELEPHONY_ERR_LOCAL_PTR_NULL;
44     }
45     response->SetOwner(GetMMIHandler(slotId));
46     return CoreManagerInner::GetInstance().SetClir(slotId, action, response);
47 }
48 
GetClirRequest(int32_t slotId,int32_t index)49 int32_t SupplementRequestCs::GetClirRequest(int32_t slotId, int32_t index)
50 {
51     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
52     AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_GET_CALL_CLIR, index);
53     if (response == nullptr) {
54         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
55         return TELEPHONY_ERR_LOCAL_PTR_NULL;
56     }
57     response->SetOwner(GetMMIHandler(slotId));
58     return CoreManagerInner::GetInstance().GetClir(slotId, response);
59 }
60 
GetCallTransferRequest(int32_t slotId,int32_t reason,int32_t index)61 int32_t SupplementRequestCs::GetCallTransferRequest(int32_t slotId, int32_t reason, int32_t index)
62 {
63     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
64     AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_GET_CALL_FORWARD, index);
65     if (response == nullptr) {
66         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
67         return TELEPHONY_ERR_LOCAL_PTR_NULL;
68     }
69     response->SetOwner(GetMMIHandler(slotId));
70     return CoreManagerInner::GetInstance().GetCallTransferInfo(slotId, reason, response);
71 }
72 
SetCallTransferRequest(int32_t slotId,const CallTransferParam & callTransferParam,int32_t index)73 int32_t SupplementRequestCs::SetCallTransferRequest(
74     int32_t slotId, const CallTransferParam &callTransferParam, int32_t index)
75 {
76     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
77     AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_CALL_FORWARD, index);
78     if (response == nullptr) {
79         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
80         return TELEPHONY_ERR_LOCAL_PTR_NULL;
81     }
82     response->SetOwner(GetMMIHandler(slotId));
83     return CoreManagerInner::GetInstance().SetCallTransferInfo(slotId, callTransferParam, response);
84 }
85 
GetCallRestrictionRequest(int32_t slotId,const std::string & fac,int32_t index)86 int32_t SupplementRequestCs::GetCallRestrictionRequest(int32_t slotId, const std::string &fac, int32_t index)
87 {
88     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
89     AppExecFwk::InnerEvent::Pointer response =
90         AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_GET_CALL_RESTRICTION, index);
91     if (response == nullptr) {
92         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
93         return TELEPHONY_ERR_LOCAL_PTR_NULL;
94     }
95     response->SetOwner(GetMMIHandler(slotId));
96     return CoreManagerInner::GetInstance().GetCallRestriction(slotId, fac, response);
97 }
98 
SetCallRestrictionRequest(int32_t slotId,const std::string & fac,int32_t mode,const std::string & pw,int32_t index)99 int32_t SupplementRequestCs::SetCallRestrictionRequest(
100     int32_t slotId, const std::string &fac, int32_t mode, const std::string &pw, int32_t index)
101 {
102     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
103     CallRestrictionParam callRestrictionParam;
104     callRestrictionParam.mode = mode;
105     callRestrictionParam.fac = fac;
106     size_t cpyLen = strlen(pw.c_str()) + 1;
107     size_t maxCpyLen = sizeof(callRestrictionParam.password);
108     if (strcpy_s(callRestrictionParam.password, cpyLen > maxCpyLen ? maxCpyLen : cpyLen, pw.c_str()) != EOK) {
109         TELEPHONY_LOGE("[slot%{public}d] strcpy_s fail.", slotId);
110         return TELEPHONY_ERR_STRCPY_FAIL;
111     }
112     AppExecFwk::InnerEvent::Pointer response =
113         AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_CALL_RESTRICTION, index);
114     if (response == nullptr) {
115         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
116         return TELEPHONY_ERR_LOCAL_PTR_NULL;
117     }
118     response->SetOwner(GetMMIHandler(slotId));
119     return CoreManagerInner::GetInstance().SetCallRestriction(slotId, callRestrictionParam, response);
120 }
121 
SetBarringPasswordRequest(int32_t slotId,const std::string & restrictionType,int32_t index,const char * oldPassword,const char * newPassword)122 int32_t SupplementRequestCs::SetBarringPasswordRequest(int32_t slotId, const std::string &restrictionType,
123     int32_t index, const char *oldPassword, const char *newPassword)
124 {
125     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
126     AppExecFwk::InnerEvent::Pointer response =
127         AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_CALL_RESTRICTION_PWD, index);
128     if (response == nullptr) {
129         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
130         return TELEPHONY_ERR_LOCAL_PTR_NULL;
131     }
132     response->SetOwner(GetMMIHandler(slotId));
133     return CoreManagerInner::GetInstance().SetBarringPassword(
134         slotId, oldPassword, newPassword, restrictionType, response);
135 }
136 
SetCallWaitingRequest(int32_t slotId,bool activate,int32_t classType,int32_t index)137 int32_t SupplementRequestCs::SetCallWaitingRequest(int32_t slotId, bool activate, int32_t classType, int32_t index)
138 {
139     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
140     AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_SET_CALL_WAIT, index);
141     if (response == nullptr) {
142         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
143         return TELEPHONY_ERR_LOCAL_PTR_NULL;
144     }
145     response->SetOwner(GetMMIHandler(slotId));
146     return CoreManagerInner::GetInstance().SetCallWaiting(slotId, activate, response);
147 }
148 
GetCallWaitingRequest(int32_t slotId,int32_t index)149 int32_t SupplementRequestCs::GetCallWaitingRequest(int32_t slotId, int32_t index)
150 {
151     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
152     AppExecFwk::InnerEvent::Pointer response = AppExecFwk::InnerEvent::Get(RadioEvent::RADIO_GET_CALL_WAIT, index);
153     if (response == nullptr) {
154         TELEPHONY_LOGE("[slot%{public}d] response is null!", slotId);
155         return TELEPHONY_ERR_LOCAL_PTR_NULL;
156     }
157     response->SetOwner(GetMMIHandler(slotId));
158     return CoreManagerInner::GetInstance().GetCallWaiting(slotId, response);
159 }
160 
GetMMIHandler(int32_t slotId) const161 std::shared_ptr<CellularCallHandler> SupplementRequestCs::GetMMIHandler(int32_t slotId) const
162 {
163     auto callService = DelayedSingleton<CellularCallService>::GetInstance();
164     if (callService == nullptr) {
165         TELEPHONY_LOGE("[slot%{public}d] error type: callService is nullptr.", slotId);
166         return nullptr;
167     }
168     return callService->GetHandler(slotId);
169 }
170 
SendUssdRequest(int32_t slotId,const std::string & msg)171 int32_t SupplementRequestCs::SendUssdRequest(int32_t slotId, const std::string &msg)
172 {
173     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
174     return CoreManagerInner::GetInstance().SetUssd(slotId, RadioEvent::RADIO_SET_USSD, msg, GetMMIHandler(slotId));
175 }
176 
AlterPinPassword(int32_t slotId,std::string newPin,std::string oldPin)177 int32_t SupplementRequestCs::AlterPinPassword(int32_t slotId, std::string newPin, std::string oldPin)
178 {
179     TELEPHONY_LOGD("[slot%{public}d] entry", slotId);
180     LockStatusResponse response = { UNLOCK_FAIL, TELEPHONY_ERROR };
181     int32_t result = CoreManagerInner::GetInstance().AlterPin(slotId, newPin, oldPin, response);
182     TELEPHONY_LOGI("[slot%{public}d] result: %{public}d, remain: %{public}d", slotId, response.result, response.remain);
183     if (result == TELEPHONY_SUCCESS) {
184         std::shared_ptr<CellularCallHandler> ccHandler = GetMMIHandler(slotId);
185         std::shared_ptr<PinPukResponse> pinResData = std::make_shared<PinPukResponse>();
186         pinResData->result = response.result;
187         pinResData->remain = response.remain;
188         AppExecFwk::InnerEvent::Pointer msgEvent =
189             AppExecFwk::InnerEvent::Get(MMIHandlerId::EVENT_SET_UNLOCK_PIN_PUK_ID, pinResData, 0);
190         if (msgEvent == nullptr) {
191             TELEPHONY_LOGE("msgEvent is null");
192             return TELEPHONY_ERR_LOCAL_PTR_NULL;
193         }
194         bool ret = ccHandler->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
195         if (!ret) {
196             TELEPHONY_LOGE("[slot%{public}d] SendEvent failed! status update failed", slotId);
197             return TELEPHONY_ERR_FAIL;
198         }
199         return TELEPHONY_SUCCESS;
200     }
201     return TELEPHONY_ERROR;
202 }
203 
UnlockPuk(int32_t slotId,std::string newPin,std::string puk)204 int32_t SupplementRequestCs::UnlockPuk(int32_t slotId, std::string newPin, std::string puk)
205 {
206     TELEPHONY_LOGD("[slot%{public}d] entry", slotId);
207     LockStatusResponse response = { UNLOCK_FAIL, TELEPHONY_ERROR };
208     int32_t result = CoreManagerInner::GetInstance().UnlockPuk(slotId, newPin, puk, response);
209     TELEPHONY_LOGI("[slot%{public}d] result: %{public}d, remain: %{public}d", slotId, response.result, response.remain);
210     if (result == TELEPHONY_SUCCESS) {
211         std::shared_ptr<CellularCallHandler> ccHandler = GetMMIHandler(slotId);
212         std::shared_ptr<PinPukResponse> pinResData = std::make_shared<PinPukResponse>();
213         pinResData->result = response.result;
214         pinResData->remain = response.remain;
215         AppExecFwk::InnerEvent::Pointer msgEvent =
216             AppExecFwk::InnerEvent::Get(MMIHandlerId::EVENT_SET_UNLOCK_PIN_PUK_ID, pinResData, 0);
217         if (msgEvent == nullptr) {
218             TELEPHONY_LOGE("msgEvent is null");
219             return TELEPHONY_ERR_LOCAL_PTR_NULL;
220         }
221         bool ret = ccHandler->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
222         if (!ret) {
223             TELEPHONY_LOGE("[slot%{public}d] SendEvent failed! status update failed", slotId);
224             return TELEPHONY_ERR_FAIL;
225         }
226         return TELEPHONY_SUCCESS;
227     }
228     return TELEPHONY_ERROR;
229 }
230 
AlterPin2Password(int32_t slotId,std::string newPin2,std::string oldPin2)231 int32_t SupplementRequestCs::AlterPin2Password(int32_t slotId, std::string newPin2, std::string oldPin2)
232 {
233     TELEPHONY_LOGD("[slot%{public}d] entry", slotId);
234     LockStatusResponse response = { UNLOCK_FAIL, TELEPHONY_ERROR };
235     int32_t result = CoreManagerInner::GetInstance().AlterPin2(slotId, newPin2, oldPin2, response);
236     TELEPHONY_LOGI("[slot%{public}d] result: %{public}d, remain: %{public}d", slotId, response.result, response.remain);
237     if (result == TELEPHONY_SUCCESS) {
238         std::shared_ptr<CellularCallHandler> ccHandler = GetMMIHandler(slotId);
239         std::shared_ptr<PinPukResponse> pinResData = std::make_shared<PinPukResponse>();
240         pinResData->result = response.result;
241         pinResData->remain = response.remain;
242         AppExecFwk::InnerEvent::Pointer msgEvent =
243             AppExecFwk::InnerEvent::Get(MMIHandlerId::EVENT_SET_UNLOCK_PIN_PUK_ID, pinResData, 0);
244         if (msgEvent == nullptr) {
245             TELEPHONY_LOGE("msgEvent is null");
246             return TELEPHONY_ERR_LOCAL_PTR_NULL;
247         }
248         bool ret = ccHandler->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
249         if (!ret) {
250             TELEPHONY_LOGE("[slot%{public}d] SendEvent failed! status update failed", slotId);
251             return TELEPHONY_ERR_FAIL;
252         }
253         return TELEPHONY_SUCCESS;
254     }
255     return TELEPHONY_ERROR;
256 }
257 
UnlockPuk2(int32_t slotId,std::string newPin2,std::string puk2)258 int32_t SupplementRequestCs::UnlockPuk2(int32_t slotId, std::string newPin2, std::string puk2)
259 {
260     TELEPHONY_LOGD("[slot%{public}d] entry", slotId);
261     LockStatusResponse response = { UNLOCK_FAIL, TELEPHONY_ERROR };
262     int32_t result = CoreManagerInner::GetInstance().UnlockPuk2(slotId, newPin2, puk2, response);
263     TELEPHONY_LOGI("[slot%{public}d] result: %{public}d, remain: %{public}d", slotId, response.result, response.remain);
264     if (result == TELEPHONY_SUCCESS) {
265         std::shared_ptr<CellularCallHandler> ccHandler = GetMMIHandler(slotId);
266         std::shared_ptr<PinPukResponse> pinResData = std::make_shared<PinPukResponse>();
267         pinResData->result = response.result;
268         pinResData->remain = response.remain;
269         AppExecFwk::InnerEvent::Pointer msgEvent =
270             AppExecFwk::InnerEvent::Get(MMIHandlerId::EVENT_SET_UNLOCK_PIN_PUK_ID, pinResData, 0);
271         if (msgEvent == nullptr) {
272             TELEPHONY_LOGE("msgEvent is null");
273             return TELEPHONY_ERR_LOCAL_PTR_NULL;
274         }
275         bool ret = ccHandler->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
276         if (!ret) {
277             TELEPHONY_LOGE("[slot%{public}d] SendEvent failed! status update failed", slotId);
278             return TELEPHONY_ERR_FAIL;
279         }
280         return TELEPHONY_SUCCESS;
281     }
282     return TELEPHONY_ERROR;
283 }
284 
CloseUnFinishedUssdRequest(int32_t slotId)285 int32_t SupplementRequestCs::CloseUnFinishedUssdRequest(int32_t slotId)
286 {
287     TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
288     return CoreManagerInner::GetInstance().CloseUnFinishedUssd(
289         slotId, RadioEvent::RADIO_CLOSE_UNFINISHED_USSD, GetMMIHandler(slotId));
290 }
291 } // namespace Telephony
292 } // namespace OHOS
293