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
23namespace OHOS {
24namespace Telephony {
25int32_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
37int32_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
49int32_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
61int32_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
73int32_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
86int32_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
99int32_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
122int32_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
137int32_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
149int32_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
161std::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
171int32_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
177int32_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        if (ccHandler == nullptr) {
186            TELEPHONY_LOGE("ccHandler is null");
187            return TELEPHONY_ERR_LOCAL_PTR_NULL;
188        }
189        std::shared_ptr<PinPukResponse> pinResData = std::make_shared<PinPukResponse>();
190        pinResData->result = response.result;
191        pinResData->remain = response.remain;
192        AppExecFwk::InnerEvent::Pointer msgEvent =
193            AppExecFwk::InnerEvent::Get(MMIHandlerId::EVENT_SET_UNLOCK_PIN_PUK_ID, pinResData, 0);
194        if (msgEvent == nullptr) {
195            TELEPHONY_LOGE("msgEvent is null");
196            return TELEPHONY_ERR_LOCAL_PTR_NULL;
197        }
198        bool ret = ccHandler->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
199        if (!ret) {
200            TELEPHONY_LOGE("[slot%{public}d] SendEvent failed! status update failed", slotId);
201            return TELEPHONY_ERR_FAIL;
202        }
203        return TELEPHONY_SUCCESS;
204    }
205    return TELEPHONY_ERROR;
206}
207
208int32_t SupplementRequestCs::UnlockPuk(int32_t slotId, std::string newPin, std::string puk)
209{
210    TELEPHONY_LOGD("[slot%{public}d] entry", slotId);
211    LockStatusResponse response = { UNLOCK_FAIL, TELEPHONY_ERROR };
212    int32_t result = CoreManagerInner::GetInstance().UnlockPuk(slotId, newPin, puk, response);
213    TELEPHONY_LOGI("[slot%{public}d] result: %{public}d, remain: %{public}d", slotId, response.result, response.remain);
214    if (result == TELEPHONY_SUCCESS) {
215        std::shared_ptr<CellularCallHandler> ccHandler = GetMMIHandler(slotId);
216        if (ccHandler == nullptr) {
217            TELEPHONY_LOGE("ccHandler is null");
218            return TELEPHONY_ERR_LOCAL_PTR_NULL;
219        }
220        std::shared_ptr<PinPukResponse> pinResData = std::make_shared<PinPukResponse>();
221        pinResData->result = response.result;
222        pinResData->remain = response.remain;
223        AppExecFwk::InnerEvent::Pointer msgEvent =
224            AppExecFwk::InnerEvent::Get(MMIHandlerId::EVENT_SET_UNLOCK_PIN_PUK_ID, pinResData, 0);
225        if (msgEvent == nullptr) {
226            TELEPHONY_LOGE("msgEvent is null");
227            return TELEPHONY_ERR_LOCAL_PTR_NULL;
228        }
229        bool ret = ccHandler->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
230        if (!ret) {
231            TELEPHONY_LOGE("[slot%{public}d] SendEvent failed! status update failed", slotId);
232            return TELEPHONY_ERR_FAIL;
233        }
234        return TELEPHONY_SUCCESS;
235    }
236    return TELEPHONY_ERROR;
237}
238
239int32_t SupplementRequestCs::AlterPin2Password(int32_t slotId, std::string newPin2, std::string oldPin2)
240{
241    TELEPHONY_LOGD("[slot%{public}d] entry", slotId);
242    LockStatusResponse response = { UNLOCK_FAIL, TELEPHONY_ERROR };
243    int32_t result = CoreManagerInner::GetInstance().AlterPin2(slotId, newPin2, oldPin2, response);
244    TELEPHONY_LOGI("[slot%{public}d] result: %{public}d, remain: %{public}d", slotId, response.result, response.remain);
245    if (result == TELEPHONY_SUCCESS) {
246        std::shared_ptr<CellularCallHandler> ccHandler = GetMMIHandler(slotId);
247        if (ccHandler == nullptr) {
248            TELEPHONY_LOGE("ccHandler is null");
249            return TELEPHONY_ERR_LOCAL_PTR_NULL;
250        }
251        std::shared_ptr<PinPukResponse> pinResData = std::make_shared<PinPukResponse>();
252        pinResData->result = response.result;
253        pinResData->remain = response.remain;
254        AppExecFwk::InnerEvent::Pointer msgEvent =
255            AppExecFwk::InnerEvent::Get(MMIHandlerId::EVENT_SET_UNLOCK_PIN_PUK_ID, pinResData, 0);
256        if (msgEvent == nullptr) {
257            TELEPHONY_LOGE("msgEvent is null");
258            return TELEPHONY_ERR_LOCAL_PTR_NULL;
259        }
260        bool ret = ccHandler->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
261        if (!ret) {
262            TELEPHONY_LOGE("[slot%{public}d] SendEvent failed! status update failed", slotId);
263            return TELEPHONY_ERR_FAIL;
264        }
265        return TELEPHONY_SUCCESS;
266    }
267    return TELEPHONY_ERROR;
268}
269
270int32_t SupplementRequestCs::UnlockPuk2(int32_t slotId, std::string newPin2, std::string puk2)
271{
272    TELEPHONY_LOGD("[slot%{public}d] entry", slotId);
273    LockStatusResponse response = { UNLOCK_FAIL, TELEPHONY_ERROR };
274    int32_t result = CoreManagerInner::GetInstance().UnlockPuk2(slotId, newPin2, puk2, response);
275    TELEPHONY_LOGI("[slot%{public}d] result: %{public}d, remain: %{public}d", slotId, response.result, response.remain);
276    if (result == TELEPHONY_SUCCESS) {
277        std::shared_ptr<CellularCallHandler> ccHandler = GetMMIHandler(slotId);
278        if (ccHandler == nullptr) {
279            TELEPHONY_LOGE("ccHandler is null");
280            return TELEPHONY_ERR_LOCAL_PTR_NULL;
281        }
282        std::shared_ptr<PinPukResponse> pinResData = std::make_shared<PinPukResponse>();
283        pinResData->result = response.result;
284        pinResData->remain = response.remain;
285        AppExecFwk::InnerEvent::Pointer msgEvent =
286            AppExecFwk::InnerEvent::Get(MMIHandlerId::EVENT_SET_UNLOCK_PIN_PUK_ID, pinResData, 0);
287        if (msgEvent == nullptr) {
288            TELEPHONY_LOGE("msgEvent is null");
289            return TELEPHONY_ERR_LOCAL_PTR_NULL;
290        }
291        bool ret = ccHandler->SendEvent(msgEvent, 0, AppExecFwk::EventQueue::Priority::IMMEDIATE);
292        if (!ret) {
293            TELEPHONY_LOGE("[slot%{public}d] SendEvent failed! status update failed", slotId);
294            return TELEPHONY_ERR_FAIL;
295        }
296        return TELEPHONY_SUCCESS;
297    }
298    return TELEPHONY_ERROR;
299}
300
301int32_t SupplementRequestCs::CloseUnFinishedUssdRequest(int32_t slotId)
302{
303    TELEPHONY_LOGI("[slot%{public}d] entry", slotId);
304    return CoreManagerInner::GetInstance().CloseUnFinishedUssd(
305        slotId, RadioEvent::RADIO_CLOSE_UNFINISHED_USSD, GetMMIHandler(slotId));
306}
307} // namespace Telephony
308} // namespace OHOS
309