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 namespace OHOS {
28 namespace Telephony {
29 CellularDataServiceStub::CellularDataServiceStub() = default;
30
31 CellularDataServiceStub::~CellularDataServiceStub() = default;
32
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t CellularDataServiceStub::OnRemoteRequest(
34 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36 std::u16string myDescriptor = CellularDataServiceStub::GetDescriptor();
37 std::u16string remoteDescriptor = data.ReadInterfaceToken();
38 // NetManager has no transport description
39 if (myDescriptor != remoteDescriptor) {
40 TELEPHONY_LOGE("descriptor check fail!");
41 return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
42 }
43 std::map<uint32_t, Fun>::iterator it = eventIdFunMap_.find(code);
44 if (it != eventIdFunMap_.end()) {
45 if (it->second != nullptr) {
46 return (this->*(it->second))(data, reply);
47 }
48 } else {
49 TELEPHONY_LOGE("event code is not exist");
50 }
51 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
52 }
53
OnIsCellularDataEnabled(MessageParcel & data,MessageParcel & reply)54 int32_t CellularDataServiceStub::OnIsCellularDataEnabled(MessageParcel &data, MessageParcel &reply)
55 {
56 bool dataEnabled = false;
57 int32_t result = IsCellularDataEnabled(dataEnabled);
58 if (!reply.WriteInt32(result)) {
59 TELEPHONY_LOGE("OnIsCellularDataEnabled write int32 reply failed.");
60 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
61 }
62 if (result != TELEPHONY_ERR_SUCCESS) {
63 return result;
64 }
65 if (!reply.WriteBool(dataEnabled)) {
66 TELEPHONY_LOGE("OnIsCellularDataEnabled write bool reply failed.");
67 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
68 }
69 return TELEPHONY_SUCCESS;
70 }
71
OnEnableCellularData(MessageParcel & data,MessageParcel & reply)72 int32_t CellularDataServiceStub::OnEnableCellularData(MessageParcel &data, MessageParcel &reply)
73 {
74 bool enable = data.ReadBool();
75 int32_t result = EnableCellularData(enable);
76 if (!reply.WriteInt32(result)) {
77 TELEPHONY_LOGE("fail to write parcel");
78 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
79 }
80 return result;
81 }
82
OnGetCellularDataState(MessageParcel & data,MessageParcel & reply)83 int32_t CellularDataServiceStub::OnGetCellularDataState(MessageParcel &data, MessageParcel &reply)
84 {
85 int32_t result = GetCellularDataState();
86 if (!reply.WriteInt32(result)) {
87 TELEPHONY_LOGE("fail to write parcel");
88 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
89 }
90 return result;
91 }
92
OnIsCellularDataRoamingEnabled(MessageParcel & data,MessageParcel & reply)93 int32_t CellularDataServiceStub::OnIsCellularDataRoamingEnabled(MessageParcel &data, MessageParcel &reply)
94 {
95 int32_t slotId = data.ReadInt32();
96 bool dataRoamingEnabled = false;
97 int32_t result = IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
98 if (!reply.WriteInt32(result)) {
99 TELEPHONY_LOGE("OnIsCellularDataRoamingEnabled write int32 reply failed.");
100 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
101 }
102 if (result != TELEPHONY_ERR_SUCCESS) {
103 return result;
104 }
105 if (!reply.WriteBool(dataRoamingEnabled)) {
106 TELEPHONY_LOGE("OnIsCellularDataRoamingEnabled write bool reply failed.");
107 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
108 }
109
110 return TELEPHONY_SUCCESS;
111 }
112
OnEnableCellularDataRoaming(MessageParcel & data,MessageParcel & reply)113 int32_t CellularDataServiceStub::OnEnableCellularDataRoaming(MessageParcel &data, MessageParcel &reply)
114 {
115 int32_t slotId = data.ReadInt32();
116 bool enable = data.ReadBool();
117 int32_t result = EnableCellularDataRoaming(slotId, enable);
118 if (!reply.WriteInt32(result)) {
119 TELEPHONY_LOGE("fail to write parcel");
120 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
121 }
122 return result;
123 }
124
OnHandleApnChanged(MessageParcel & data,MessageParcel & reply)125 int32_t CellularDataServiceStub::OnHandleApnChanged(MessageParcel &data, MessageParcel &reply)
126 {
127 int32_t slotId = data.ReadInt32();
128 int32_t result = HandleApnChanged(slotId);
129 if (!reply.WriteInt32(result)) {
130 TELEPHONY_LOGE("fail to write parcel");
131 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
132 }
133 return result;
134 }
135
OnGetDefaultCellularDataSlotId(MessageParcel & data,MessageParcel & reply)136 int32_t CellularDataServiceStub::OnGetDefaultCellularDataSlotId(MessageParcel &data, MessageParcel &reply)
137 {
138 int32_t result = GetDefaultCellularDataSlotId();
139 if (!reply.WriteInt32(result)) {
140 TELEPHONY_LOGE("fail to write parcel");
141 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
142 }
143 return result;
144 }
145
OnSetDefaultCellularDataSlotId(MessageParcel & data,MessageParcel & reply)146 int32_t CellularDataServiceStub::OnSetDefaultCellularDataSlotId(MessageParcel &data, MessageParcel &reply)
147 {
148 int32_t slotId = data.ReadInt32();
149 int32_t result = SetDefaultCellularDataSlotId(slotId);
150 if (!reply.WriteInt32(result)) {
151 TELEPHONY_LOGE("fail to write parcel");
152 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
153 }
154 return result;
155 }
156
OnGetCellularDataFlowType(MessageParcel & data,MessageParcel & reply)157 int32_t CellularDataServiceStub::OnGetCellularDataFlowType(MessageParcel &data, MessageParcel &reply)
158 {
159 int32_t result = GetCellularDataFlowType();
160 if (!reply.WriteInt32(result)) {
161 TELEPHONY_LOGE("fail to write parcel");
162 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
163 }
164 return result;
165 }
166
OnHasInternetCapability(MessageParcel & data,MessageParcel & reply)167 int32_t CellularDataServiceStub::OnHasInternetCapability(MessageParcel &data, MessageParcel &reply)
168 {
169 int32_t slotId = data.ReadInt32();
170 int32_t cid = data.ReadInt32();
171 int32_t result = HasInternetCapability(slotId, cid);
172 if (!reply.WriteInt32(result)) {
173 TELEPHONY_LOGE("fail to write parcel");
174 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
175 }
176 return result;
177 }
178
OnClearCellularDataConnections(MessageParcel & data,MessageParcel & reply)179 int32_t CellularDataServiceStub::OnClearCellularDataConnections(MessageParcel &data, MessageParcel &reply)
180 {
181 int32_t slotId = data.ReadInt32();
182 int32_t result = ClearCellularDataConnections(slotId);
183 if (!reply.WriteInt32(result)) {
184 TELEPHONY_LOGE("fail to write parcel");
185 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
186 }
187 return result;
188 }
189
OnRegisterSimAccountCallback(MessageParcel & data,MessageParcel & reply)190 int32_t CellularDataServiceStub::OnRegisterSimAccountCallback(MessageParcel &data, MessageParcel &reply)
191 {
192 sptr<SimAccountCallback> callback = iface_cast<SimAccountCallback>(data.ReadRemoteObject());
193 int32_t result;
194 if (callback == nullptr) {
195 TELEPHONY_LOGE("callback is nullptr!");
196 result = TELEPHONY_ERR_ARGUMENT_NULL;
197 } else {
198 result = RegisterSimAccountCallback(callback);
199 }
200 reply.WriteInt32(result);
201 return result;
202 }
203
OnUnregisterSimAccountCallback(MessageParcel & data,MessageParcel & reply)204 int32_t CellularDataServiceStub::OnUnregisterSimAccountCallback(MessageParcel &data, MessageParcel &reply)
205 {
206 int32_t result = UnregisterSimAccountCallback();
207 reply.WriteInt32(result);
208 return result;
209 }
210 } // namespace Telephony
211 } // namespace OHOS