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("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("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
OnEnableIntelligenceSwitch(MessageParcel & data,MessageParcel & reply)83 int32_t CellularDataServiceStub::OnEnableIntelligenceSwitch(MessageParcel &data, MessageParcel &reply)
84 {
85 bool enable = data.ReadBool();
86 int32_t result = EnableIntelligenceSwitch(enable);
87 if (!reply.WriteInt32(result)) {
88 TELEPHONY_LOGE("fail to write parcel");
89 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
90 }
91 return result;
92 }
93
OnGetCellularDataState(MessageParcel & data,MessageParcel & reply)94 int32_t CellularDataServiceStub::OnGetCellularDataState(MessageParcel &data, MessageParcel &reply)
95 {
96 int32_t result = GetCellularDataState();
97 if (!reply.WriteInt32(result)) {
98 TELEPHONY_LOGE("fail to write parcel");
99 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
100 }
101 return result;
102 }
103
OnIsCellularDataRoamingEnabled(MessageParcel & data,MessageParcel & reply)104 int32_t CellularDataServiceStub::OnIsCellularDataRoamingEnabled(MessageParcel &data, MessageParcel &reply)
105 {
106 int32_t slotId = data.ReadInt32();
107 bool dataRoamingEnabled = false;
108 int32_t result = IsCellularDataRoamingEnabled(slotId, dataRoamingEnabled);
109 if (!reply.WriteInt32(result)) {
110 TELEPHONY_LOGE("write int32 reply failed.");
111 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
112 }
113 if (result != TELEPHONY_ERR_SUCCESS) {
114 return result;
115 }
116 if (!reply.WriteBool(dataRoamingEnabled)) {
117 TELEPHONY_LOGE("write bool reply failed.");
118 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
119 }
120
121 return TELEPHONY_SUCCESS;
122 }
123
OnEnableCellularDataRoaming(MessageParcel & data,MessageParcel & reply)124 int32_t CellularDataServiceStub::OnEnableCellularDataRoaming(MessageParcel &data, MessageParcel &reply)
125 {
126 int32_t slotId = data.ReadInt32();
127 bool enable = data.ReadBool();
128 int32_t result = EnableCellularDataRoaming(slotId, enable);
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
OnHandleApnChanged(MessageParcel & data,MessageParcel & reply)136 int32_t CellularDataServiceStub::OnHandleApnChanged(MessageParcel &data, MessageParcel &reply)
137 {
138 int32_t slotId = data.ReadInt32();
139 int32_t result = HandleApnChanged(slotId);
140 if (!reply.WriteInt32(result)) {
141 TELEPHONY_LOGE("fail to write parcel");
142 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
143 }
144 return result;
145 }
146
OnGetDefaultCellularDataSlotId(MessageParcel & data,MessageParcel & reply)147 int32_t CellularDataServiceStub::OnGetDefaultCellularDataSlotId(MessageParcel &data, MessageParcel &reply)
148 {
149 int32_t result = GetDefaultCellularDataSlotId();
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
OnGetDefaultCellularDataSimId(MessageParcel & data,MessageParcel & reply)157 int32_t CellularDataServiceStub::OnGetDefaultCellularDataSimId(MessageParcel &data, MessageParcel &reply)
158 {
159 int32_t simId = 0;
160 int32_t result = GetDefaultCellularDataSimId(simId);
161 if (!reply.WriteInt32(result)) {
162 TELEPHONY_LOGE("write int32 reply failed.");
163 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
164 }
165 if (result != TELEPHONY_ERR_SUCCESS) {
166 return result;
167 }
168 if (!reply.WriteInt32(simId)) {
169 TELEPHONY_LOGE("write int32 reply failed.");
170 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
171 }
172 return TELEPHONY_SUCCESS;
173 }
174
OnSetDefaultCellularDataSlotId(MessageParcel & data,MessageParcel & reply)175 int32_t CellularDataServiceStub::OnSetDefaultCellularDataSlotId(MessageParcel &data, MessageParcel &reply)
176 {
177 int32_t slotId = data.ReadInt32();
178 int32_t result = SetDefaultCellularDataSlotId(slotId);
179 if (!reply.WriteInt32(result)) {
180 TELEPHONY_LOGE("fail to write parcel");
181 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
182 }
183 return result;
184 }
185
OnGetCellularDataFlowType(MessageParcel & data,MessageParcel & reply)186 int32_t CellularDataServiceStub::OnGetCellularDataFlowType(MessageParcel &data, MessageParcel &reply)
187 {
188 int32_t result = GetCellularDataFlowType();
189 if (!reply.WriteInt32(result)) {
190 TELEPHONY_LOGE("fail to write parcel");
191 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
192 }
193 return result;
194 }
195
OnHasInternetCapability(MessageParcel & data,MessageParcel & reply)196 int32_t CellularDataServiceStub::OnHasInternetCapability(MessageParcel &data, MessageParcel &reply)
197 {
198 int32_t slotId = data.ReadInt32();
199 int32_t cid = data.ReadInt32();
200 int32_t result = HasInternetCapability(slotId, cid);
201 if (!reply.WriteInt32(result)) {
202 TELEPHONY_LOGE("fail to write parcel");
203 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
204 }
205 return result;
206 }
207
OnClearCellularDataConnections(MessageParcel & data,MessageParcel & reply)208 int32_t CellularDataServiceStub::OnClearCellularDataConnections(MessageParcel &data, MessageParcel &reply)
209 {
210 int32_t slotId = data.ReadInt32();
211 int32_t result = ClearCellularDataConnections(slotId);
212 if (!reply.WriteInt32(result)) {
213 TELEPHONY_LOGE("fail to write parcel");
214 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
215 }
216 return result;
217 }
218
OnClearAllConnections(MessageParcel & data,MessageParcel & reply)219 int32_t CellularDataServiceStub::OnClearAllConnections(MessageParcel &data, MessageParcel &reply)
220 {
221 int32_t slotId = data.ReadInt32();
222 DisConnectionReason reason = static_cast<DisConnectionReason>(data.ReadInt32());
223 int32_t result = ClearAllConnections(slotId, reason);
224 if (!reply.WriteInt32(result)) {
225 TELEPHONY_LOGE("fail to write parcel");
226 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
227 }
228 return result;
229 }
230
OnRegisterSimAccountCallback(MessageParcel & data,MessageParcel & reply)231 int32_t CellularDataServiceStub::OnRegisterSimAccountCallback(MessageParcel &data, MessageParcel &reply)
232 {
233 sptr<SimAccountCallback> callback = iface_cast<SimAccountCallback>(data.ReadRemoteObject());
234 int32_t result;
235 if (callback == nullptr) {
236 TELEPHONY_LOGE("callback is nullptr!");
237 result = TELEPHONY_ERR_ARGUMENT_NULL;
238 } else {
239 result = RegisterSimAccountCallback(callback);
240 }
241 reply.WriteInt32(result);
242 return result;
243 }
244
OnUnregisterSimAccountCallback(MessageParcel & data,MessageParcel & reply)245 int32_t CellularDataServiceStub::OnUnregisterSimAccountCallback(MessageParcel &data, MessageParcel &reply)
246 {
247 int32_t result = UnregisterSimAccountCallback();
248 reply.WriteInt32(result);
249 return result;
250 }
251
OnGetDataConnApnAttr(MessageParcel & data,MessageParcel & reply)252 int32_t CellularDataServiceStub::OnGetDataConnApnAttr(MessageParcel &data, MessageParcel &reply)
253 {
254 int32_t slotId = data.ReadInt32();
255 ApnItem::Attribute apnAttr;
256 int32_t result = GetDataConnApnAttr(slotId, apnAttr);
257 if (!reply.WriteInt32(result)) {
258 TELEPHONY_LOGE("write int32 reply failed.");
259 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
260 }
261 if (!reply.WriteRawData(&apnAttr, sizeof(ApnItem::Attribute))) {
262 TELEPHONY_LOGE("write apnAttr reply failed.");
263 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
264 }
265 return TELEPHONY_SUCCESS;
266 }
267
OnGetDataConnIpType(MessageParcel & data,MessageParcel & reply)268 int32_t CellularDataServiceStub::OnGetDataConnIpType(MessageParcel &data, MessageParcel &reply)
269 {
270 int32_t slotId = data.ReadInt32();
271 std::string ipType;
272 int32_t result = GetDataConnIpType(slotId, ipType);
273 if (!reply.WriteInt32(result)) {
274 TELEPHONY_LOGE("write int32 reply failed.");
275 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
276 }
277 if (!reply.WriteString(ipType)) {
278 TELEPHONY_LOGE("write int32 reply failed.");
279 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
280 }
281 return TELEPHONY_SUCCESS;
282 }
283
OnGetApnState(MessageParcel & data,MessageParcel & reply)284 int32_t CellularDataServiceStub::OnGetApnState(MessageParcel &data, MessageParcel &reply)
285 {
286 int32_t slotId = data.ReadInt32();
287 std::string apnType = data.ReadString();
288 int32_t result = GetApnState(slotId, apnType);
289 if (!reply.WriteInt32(result)) {
290 TELEPHONY_LOGE("fail to write parcel");
291 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
292 }
293 return result;
294 }
295
OnGetRecoveryState(MessageParcel & data,MessageParcel & reply)296 int32_t CellularDataServiceStub::OnGetRecoveryState(MessageParcel &data, MessageParcel &reply)
297 {
298 int32_t result = GetDataRecoveryState();
299 if (!reply.WriteInt32(result)) {
300 TELEPHONY_LOGE("fail to write parcel");
301 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
302 }
303 return result;
304 }
305
OnIsNeedDoRecovery(MessageParcel & data,MessageParcel & reply)306 int32_t CellularDataServiceStub::OnIsNeedDoRecovery(MessageParcel &data, MessageParcel &reply)
307 {
308 int32_t slotId = data.ReadInt32();
309 int32_t needDoRecovery = data.ReadBool();
310 int32_t result = IsNeedDoRecovery(slotId, needDoRecovery);
311 if (!reply.WriteInt32(result)) {
312 TELEPHONY_LOGE("write int32 reply failed.");
313 return TELEPHONY_ERR_WRITE_REPLY_FAIL;
314 }
315 return result;
316 }
317
318 } // namespace Telephony
319 } // namespace OHOS