1/*
2 * Copyright (C) 2021 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#include "cellular_data_service_stub.h"
17
18#include <string_ex.h>
19
20#include "cellular_data_controller.h"
21#include "cellular_data_service.h"
22#include "ipc_skeleton.h"
23#include "sim_account_callback_proxy.h"
24#include "telephony_errors.h"
25#include "telephony_log_wrapper.h"
26
27#ifdef HICOLLIE_ENABLE
28#include "xcollie/xcollie.h"
29#include "xcollie/xcollie_define.h"
30#define XCOLLIE_TIMEOUT_SECONDS 30
31#endif
32
33namespace OHOS {
34namespace Telephony {
35CellularDataServiceStub::CellularDataServiceStub() = default;
36
37CellularDataServiceStub::~CellularDataServiceStub() = default;
38
39int32_t CellularDataServiceStub::OnRemoteRequest(
40    uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
41{
42    std::u16string myDescriptor = CellularDataServiceStub::GetDescriptor();
43    std::u16string remoteDescriptor = data.ReadInterfaceToken();
44    // NetManager has no transport description
45    if (myDescriptor != remoteDescriptor) {
46        TELEPHONY_LOGE("descriptor check fail!");
47        return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
48    }
49    std::map<uint32_t, Fun>::iterator it = eventIdFunMap_.find(code);
50    if (it != eventIdFunMap_.end()) {
51        if (it->second != nullptr) {
52            int32_t idTimer = SetTimer(code);
53            int32_t result = it->second(data, reply);
54            CancelTimer(idTimer);
55            return result;
56        }
57    } else {
58        TELEPHONY_LOGE("event code is not exist");
59    }
60    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
61}
62
63int32_t CellularDataServiceStub::SetTimer(uint32_t code)
64{
65#ifdef HICOLLIE_ENABLE
66    int32_t idTimer = HiviewDFX::INVALID_ID;
67    std::map<uint32_t, std::string>::iterator itCollieId = collieCodeStringMap_.find(code);
68    if (itCollieId != collieCodeStringMap_.end()) {
69        std::string collieStr = itCollieId->second;
70        std::string collieName = "CellularDataServiceStub: " + collieStr;
71        unsigned int flag = HiviewDFX::XCOLLIE_FLAG_NOOP;
72        auto TimerCallback = [collieStr](void *) {
73            TELEPHONY_LOGE("OnRemoteRequest timeout func: %{public}s", collieStr.c_str());
74        };
75        idTimer = HiviewDFX::XCollie::GetInstance().SetTimer(
76            collieName, XCOLLIE_TIMEOUT_SECONDS, TimerCallback, nullptr, flag);
77        TELEPHONY_LOGD("SetTimer id: %{public}d, name: %{public}s.", idTimer, collieStr.c_str());
78    }
79    return idTimer;
80#else
81    TELEPHONY_LOGD("No HICOLLIE_ENABLE");
82    return -1;
83#endif
84}
85
86void CellularDataServiceStub::CancelTimer(int32_t id)
87{
88#ifdef HICOLLIE_ENABLE
89    if (id == HiviewDFX::INVALID_ID) {
90        return;
91    }
92    TELEPHONY_LOGD("CancelTimer id: %{public}d.", id);
93    HiviewDFX::XCollie::GetInstance().CancelTimer(id);
94#else
95    return;
96#endif
97}
98
99int32_t CellularDataServiceStub::OnIsCellularDataEnabled(MessageParcel &data, MessageParcel &reply)
100{
101    bool dataEnabled = false;
102    int32_t result = IsCellularDataEnabled(dataEnabled);
103    if (!reply.WriteInt32(result)) {
104        TELEPHONY_LOGE("write int32 reply failed.");
105        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
106    }
107    if (result != TELEPHONY_ERR_SUCCESS) {
108        return result;
109    }
110    if (!reply.WriteBool(dataEnabled)) {
111        TELEPHONY_LOGE("write bool reply failed.");
112        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
113    }
114    return TELEPHONY_SUCCESS;
115}
116
117int32_t CellularDataServiceStub::OnEnableCellularData(MessageParcel &data, MessageParcel &reply)
118{
119    bool enable = data.ReadBool();
120    int32_t result = EnableCellularData(enable);
121    if (!reply.WriteInt32(result)) {
122        TELEPHONY_LOGE("fail to write parcel");
123        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
124    }
125    return result;
126}
127
128int32_t CellularDataServiceStub::OnEnableIntelligenceSwitch(MessageParcel &data, MessageParcel &reply)
129{
130    bool enable = data.ReadBool();
131    int32_t result = EnableIntelligenceSwitch(enable);
132    if (!reply.WriteInt32(result)) {
133        TELEPHONY_LOGE("fail to write parcel");
134        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
135    }
136    return result;
137}
138
139int32_t CellularDataServiceStub::OnGetCellularDataState(MessageParcel &data, MessageParcel &reply)
140{
141    int32_t result = GetCellularDataState();
142    if (!reply.WriteInt32(result)) {
143        TELEPHONY_LOGE("fail to write parcel");
144        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
145    }
146    return result;
147}
148
149int32_t CellularDataServiceStub::OnIsCellularDataRoamingEnabled(MessageParcel &data, MessageParcel &reply)
150{
151    int32_t slotId = data.ReadInt32();
152    bool dataRoamingEnabled = false;
153    int32_t result = IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
154    if (!reply.WriteInt32(result)) {
155        TELEPHONY_LOGE("write int32 reply failed.");
156        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
157    }
158    if (result != TELEPHONY_ERR_SUCCESS) {
159        return result;
160    }
161    if (!reply.WriteBool(dataRoamingEnabled)) {
162        TELEPHONY_LOGE("write bool reply failed.");
163        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
164    }
165
166    return TELEPHONY_SUCCESS;
167}
168
169int32_t CellularDataServiceStub::OnEnableCellularDataRoaming(MessageParcel &data, MessageParcel &reply)
170{
171    int32_t slotId = data.ReadInt32();
172    bool enable = data.ReadBool();
173    int32_t result = EnableCellularDataRoaming(slotId, enable);
174    if (!reply.WriteInt32(result)) {
175        TELEPHONY_LOGE("fail to write parcel");
176        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
177    }
178    return result;
179}
180
181int32_t CellularDataServiceStub::OnGetIntelligenceSwitchState(MessageParcel &data, MessageParcel &reply)
182{
183    bool switchState = false;
184    int32_t result = GetIntelligenceSwitchState(switchState);
185    if (!reply.WriteInt32(result)) {
186        TELEPHONY_LOGE("write int32 reply failed.");
187        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
188    }
189    if (result != TELEPHONY_ERR_SUCCESS) {
190        return result;
191    }
192    if (!reply.WriteBool(switchState)) {
193        TELEPHONY_LOGE("write bool reply failed.");
194        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
195    }
196    return TELEPHONY_SUCCESS;
197}
198
199int32_t CellularDataServiceStub::OnHandleApnChanged(MessageParcel &data, MessageParcel &reply)
200{
201    int32_t slotId = data.ReadInt32();
202    int32_t result = HandleApnChanged(slotId);
203    if (!reply.WriteInt32(result)) {
204        TELEPHONY_LOGE("fail to write parcel");
205        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
206    }
207    return result;
208}
209
210int32_t CellularDataServiceStub::OnGetDefaultCellularDataSlotId(MessageParcel &data, MessageParcel &reply)
211{
212    int32_t result = GetDefaultCellularDataSlotId();
213    if (!reply.WriteInt32(result)) {
214        TELEPHONY_LOGE("fail to write parcel");
215        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
216    }
217    return result;
218}
219
220int32_t CellularDataServiceStub::OnGetDefaultCellularDataSimId(MessageParcel &data, MessageParcel &reply)
221{
222    int32_t simId = 0;
223    int32_t result = GetDefaultCellularDataSimId(simId);
224    if (!reply.WriteInt32(result)) {
225        TELEPHONY_LOGE("write int32 reply failed.");
226        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
227    }
228    if (result != TELEPHONY_ERR_SUCCESS) {
229        return result;
230    }
231    if (!reply.WriteInt32(simId)) {
232        TELEPHONY_LOGE("write int32 reply failed.");
233        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
234    }
235    return TELEPHONY_SUCCESS;
236}
237
238int32_t CellularDataServiceStub::OnSetDefaultCellularDataSlotId(MessageParcel &data, MessageParcel &reply)
239{
240    int32_t slotId = data.ReadInt32();
241    int32_t result = SetDefaultCellularDataSlotId(slotId);
242    if (!reply.WriteInt32(result)) {
243        TELEPHONY_LOGE("fail to write parcel");
244        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
245    }
246    return result;
247}
248
249int32_t CellularDataServiceStub::OnGetCellularDataFlowType(MessageParcel &data, MessageParcel &reply)
250{
251    int32_t result = GetCellularDataFlowType();
252    if (!reply.WriteInt32(result)) {
253        TELEPHONY_LOGE("fail to write parcel");
254        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
255    }
256    return result;
257}
258
259int32_t CellularDataServiceStub::OnHasInternetCapability(MessageParcel &data, MessageParcel &reply)
260{
261    int32_t slotId = data.ReadInt32();
262    int32_t cid = data.ReadInt32();
263    int32_t result = HasInternetCapability(slotId, cid);
264    if (!reply.WriteInt32(result)) {
265        TELEPHONY_LOGE("fail to write parcel");
266        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
267    }
268    return result;
269}
270
271int32_t CellularDataServiceStub::OnClearCellularDataConnections(MessageParcel &data, MessageParcel &reply)
272{
273    int32_t slotId = data.ReadInt32();
274    int32_t result = ClearCellularDataConnections(slotId);
275    if (!reply.WriteInt32(result)) {
276        TELEPHONY_LOGE("fail to write parcel");
277        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
278    }
279    return result;
280}
281
282int32_t CellularDataServiceStub::OnClearAllConnections(MessageParcel &data, MessageParcel &reply)
283{
284    int32_t slotId = data.ReadInt32();
285    DisConnectionReason reason = static_cast<DisConnectionReason>(data.ReadInt32());
286    int32_t result = ClearAllConnections(slotId, reason);
287    if (!reply.WriteInt32(result)) {
288        TELEPHONY_LOGE("fail to write parcel");
289        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
290    }
291    return result;
292}
293
294int32_t CellularDataServiceStub::OnRegisterSimAccountCallback(MessageParcel &data, MessageParcel &reply)
295{
296    sptr<SimAccountCallback> callback = iface_cast<SimAccountCallback>(data.ReadRemoteObject());
297    int32_t result;
298    if (callback == nullptr) {
299        TELEPHONY_LOGE("callback is nullptr!");
300        result = TELEPHONY_ERR_ARGUMENT_NULL;
301    } else {
302        result = RegisterSimAccountCallback(callback);
303    }
304    reply.WriteInt32(result);
305    return result;
306}
307
308int32_t CellularDataServiceStub::OnUnregisterSimAccountCallback(MessageParcel &data, MessageParcel &reply)
309{
310    int32_t result = UnregisterSimAccountCallback();
311    reply.WriteInt32(result);
312    return result;
313}
314
315int32_t CellularDataServiceStub::OnGetDataConnApnAttr(MessageParcel &data, MessageParcel &reply)
316{
317    int32_t slotId = data.ReadInt32();
318    ApnItem::Attribute apnAttr;
319    int32_t result = GetDataConnApnAttr(slotId, apnAttr);
320    if (!reply.WriteInt32(result)) {
321        TELEPHONY_LOGE("write int32 reply failed.");
322        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
323    }
324    if (!reply.WriteRawData(&apnAttr, sizeof(ApnItem::Attribute))) {
325        TELEPHONY_LOGE("write apnAttr reply failed.");
326        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
327    }
328    return TELEPHONY_SUCCESS;
329}
330
331int32_t CellularDataServiceStub::OnGetDataConnIpType(MessageParcel &data, MessageParcel &reply)
332{
333    int32_t slotId = data.ReadInt32();
334    std::string ipType;
335    int32_t result = GetDataConnIpType(slotId, ipType);
336    if (!reply.WriteInt32(result)) {
337        TELEPHONY_LOGE("write int32 reply failed.");
338        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
339    }
340    if (!reply.WriteString(ipType)) {
341        TELEPHONY_LOGE("write int32 reply failed.");
342        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
343    }
344    return TELEPHONY_SUCCESS;
345}
346
347int32_t CellularDataServiceStub::OnGetApnState(MessageParcel &data, MessageParcel &reply)
348{
349    int32_t slotId = data.ReadInt32();
350    std::string apnType = data.ReadString();
351    int32_t result = GetApnState(slotId, apnType);
352    if (!reply.WriteInt32(result)) {
353        TELEPHONY_LOGE("fail to write parcel");
354        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
355    }
356    return result;
357}
358
359int32_t CellularDataServiceStub::OnGetRecoveryState(MessageParcel &data, MessageParcel &reply)
360{
361    int32_t result = GetDataRecoveryState();
362    if (!reply.WriteInt32(result)) {
363        TELEPHONY_LOGE("fail to write parcel");
364        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
365    }
366    return result;
367}
368
369int32_t CellularDataServiceStub::OnIsNeedDoRecovery(MessageParcel &data, MessageParcel &reply)
370{
371    int32_t slotId = data.ReadInt32();
372    int32_t needDoRecovery = data.ReadBool();
373    int32_t result = IsNeedDoRecovery(slotId, needDoRecovery);
374    if (!reply.WriteInt32(result)) {
375        TELEPHONY_LOGE("write int32 reply failed.");
376        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
377    }
378    return result;
379}
380
381int32_t CellularDataServiceStub::OnInitCellularDataController(MessageParcel &data, MessageParcel &reply)
382{
383    int32_t slotId = data.ReadInt32();
384    int32_t result = InitCellularDataController(slotId);
385    if (!reply.WriteInt32(result)) {
386        TELEPHONY_LOGE("write int32 reply failed.");
387        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
388    }
389    return result;
390}
391
392int32_t CellularDataServiceStub::OnEstablishAllApnsIfConnectable(MessageParcel &data, MessageParcel &reply)
393{
394    int32_t slotId = data.ReadInt32();
395    int32_t result = EstablishAllApnsIfConnectable(slotId);
396    if (!reply.WriteInt32(result)) {
397        TELEPHONY_LOGE("fail to write parcel");
398        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
399    }
400    return result;
401}
402
403int32_t CellularDataServiceStub::OnReleaseCellularDataConnection(MessageParcel &data, MessageParcel &reply)
404{
405    int32_t slotId;
406    if (!data.ReadInt32(slotId)) {
407        TELEPHONY_LOGE("write int32 slotId failed.");
408        return TELEPHONY_ERR_READ_DATA_FAIL;
409    }
410    int32_t result = ReleaseCellularDataConnection(slotId);
411    if (!reply.WriteInt32(result)) {
412        TELEPHONY_LOGE("write int32 reply failed.");
413        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
414    }
415    return result;
416}
417
418int32_t CellularDataServiceStub::OnGetCellularDataSupplierId(MessageParcel &data, MessageParcel &reply)
419{
420    int32_t slotId = data.ReadInt32();
421    uint64_t capability = data.ReadUint64();
422    uint32_t supplierId = 0;
423    int32_t result = GetCellularDataSupplierId(slotId, capability, supplierId);
424    if (!reply.WriteInt32(result)) {
425        TELEPHONY_LOGE("write int32 reply failed.");
426        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
427    }
428    if (!reply.WriteUint32(supplierId)) {
429        TELEPHONY_LOGE("write uint32 reply failed.");
430        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
431    }
432    return result;
433}
434
435int32_t CellularDataServiceStub::OnCorrectNetSupplierNoAvailable(MessageParcel &data, MessageParcel &reply)
436{
437    int32_t slotId = data.ReadInt32();
438    int32_t result = CorrectNetSupplierNoAvailable(slotId);
439    if (!reply.WriteInt32(result)) {
440        TELEPHONY_LOGE("write int32 reply failed.");
441        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
442    }
443    return result;
444}
445
446int32_t CellularDataServiceStub::OnGetSupplierRegisterState(MessageParcel &data, MessageParcel &reply)
447{
448    uint32_t supplierId = data.ReadUint32();
449    int32_t regState = -1;
450    int32_t result = GetSupplierRegisterState(supplierId, regState);
451    if (!reply.WriteInt32(result)) {
452        TELEPHONY_LOGE("write int32 reply failed.");
453        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
454    }
455    if (!reply.WriteInt32(regState)) {
456        TELEPHONY_LOGE("write int32 reply failed.");
457        return TELEPHONY_ERR_WRITE_REPLY_FAIL;
458    }
459    return result;
460}
461} // namespace Telephony
462} // namespace OHOS