• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
OnGetIntelligenceSwitchState(MessageParcel & data,MessageParcel & reply)136 int32_t CellularDataServiceStub::OnGetIntelligenceSwitchState(MessageParcel &data, MessageParcel &reply)
137 {
138     bool switchState = false;
139     int32_t result = GetIntelligenceSwitchState(switchState);
140     if (!reply.WriteInt32(result)) {
141         TELEPHONY_LOGE("write int32 reply failed.");
142         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
143     }
144     if (result != TELEPHONY_ERR_SUCCESS) {
145         return result;
146     }
147     if (!reply.WriteBool(switchState)) {
148         TELEPHONY_LOGE("write bool reply failed.");
149         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
150     }
151     return TELEPHONY_SUCCESS;
152 }
153 
OnHandleApnChanged(MessageParcel & data,MessageParcel & reply)154 int32_t CellularDataServiceStub::OnHandleApnChanged(MessageParcel &data, MessageParcel &reply)
155 {
156     int32_t slotId = data.ReadInt32();
157     int32_t result = HandleApnChanged(slotId);
158     if (!reply.WriteInt32(result)) {
159         TELEPHONY_LOGE("fail to write parcel");
160         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
161     }
162     return result;
163 }
164 
OnGetDefaultCellularDataSlotId(MessageParcel & data,MessageParcel & reply)165 int32_t CellularDataServiceStub::OnGetDefaultCellularDataSlotId(MessageParcel &data, MessageParcel &reply)
166 {
167     int32_t result = GetDefaultCellularDataSlotId();
168     if (!reply.WriteInt32(result)) {
169         TELEPHONY_LOGE("fail to write parcel");
170         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
171     }
172     return result;
173 }
174 
OnGetDefaultCellularDataSimId(MessageParcel & data,MessageParcel & reply)175 int32_t CellularDataServiceStub::OnGetDefaultCellularDataSimId(MessageParcel &data, MessageParcel &reply)
176 {
177     int32_t simId = 0;
178     int32_t result = GetDefaultCellularDataSimId(simId);
179     if (!reply.WriteInt32(result)) {
180         TELEPHONY_LOGE("write int32 reply failed.");
181         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
182     }
183     if (result != TELEPHONY_ERR_SUCCESS) {
184         return result;
185     }
186     if (!reply.WriteInt32(simId)) {
187         TELEPHONY_LOGE("write int32 reply failed.");
188         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
189     }
190     return TELEPHONY_SUCCESS;
191 }
192 
OnSetDefaultCellularDataSlotId(MessageParcel & data,MessageParcel & reply)193 int32_t CellularDataServiceStub::OnSetDefaultCellularDataSlotId(MessageParcel &data, MessageParcel &reply)
194 {
195     int32_t slotId = data.ReadInt32();
196     int32_t result = SetDefaultCellularDataSlotId(slotId);
197     if (!reply.WriteInt32(result)) {
198         TELEPHONY_LOGE("fail to write parcel");
199         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
200     }
201     return result;
202 }
203 
OnGetCellularDataFlowType(MessageParcel & data,MessageParcel & reply)204 int32_t CellularDataServiceStub::OnGetCellularDataFlowType(MessageParcel &data, MessageParcel &reply)
205 {
206     int32_t result = GetCellularDataFlowType();
207     if (!reply.WriteInt32(result)) {
208         TELEPHONY_LOGE("fail to write parcel");
209         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
210     }
211     return result;
212 }
213 
OnHasInternetCapability(MessageParcel & data,MessageParcel & reply)214 int32_t CellularDataServiceStub::OnHasInternetCapability(MessageParcel &data, MessageParcel &reply)
215 {
216     int32_t slotId = data.ReadInt32();
217     int32_t cid = data.ReadInt32();
218     int32_t result = HasInternetCapability(slotId, cid);
219     if (!reply.WriteInt32(result)) {
220         TELEPHONY_LOGE("fail to write parcel");
221         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
222     }
223     return result;
224 }
225 
OnClearCellularDataConnections(MessageParcel & data,MessageParcel & reply)226 int32_t CellularDataServiceStub::OnClearCellularDataConnections(MessageParcel &data, MessageParcel &reply)
227 {
228     int32_t slotId = data.ReadInt32();
229     int32_t result = ClearCellularDataConnections(slotId);
230     if (!reply.WriteInt32(result)) {
231         TELEPHONY_LOGE("fail to write parcel");
232         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
233     }
234     return result;
235 }
236 
OnClearAllConnections(MessageParcel & data,MessageParcel & reply)237 int32_t CellularDataServiceStub::OnClearAllConnections(MessageParcel &data, MessageParcel &reply)
238 {
239     int32_t slotId = data.ReadInt32();
240     DisConnectionReason reason = static_cast<DisConnectionReason>(data.ReadInt32());
241     int32_t result = ClearAllConnections(slotId, reason);
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 
OnRegisterSimAccountCallback(MessageParcel & data,MessageParcel & reply)249 int32_t CellularDataServiceStub::OnRegisterSimAccountCallback(MessageParcel &data, MessageParcel &reply)
250 {
251     sptr<SimAccountCallback> callback = iface_cast<SimAccountCallback>(data.ReadRemoteObject());
252     int32_t result;
253     if (callback == nullptr) {
254         TELEPHONY_LOGE("callback is nullptr!");
255         result = TELEPHONY_ERR_ARGUMENT_NULL;
256     } else {
257         result = RegisterSimAccountCallback(callback);
258     }
259     reply.WriteInt32(result);
260     return result;
261 }
262 
OnUnregisterSimAccountCallback(MessageParcel & data,MessageParcel & reply)263 int32_t CellularDataServiceStub::OnUnregisterSimAccountCallback(MessageParcel &data, MessageParcel &reply)
264 {
265     int32_t result = UnregisterSimAccountCallback();
266     reply.WriteInt32(result);
267     return result;
268 }
269 
OnGetDataConnApnAttr(MessageParcel & data,MessageParcel & reply)270 int32_t CellularDataServiceStub::OnGetDataConnApnAttr(MessageParcel &data, MessageParcel &reply)
271 {
272     int32_t slotId = data.ReadInt32();
273     ApnItem::Attribute apnAttr;
274     int32_t result = GetDataConnApnAttr(slotId, apnAttr);
275     if (!reply.WriteInt32(result)) {
276         TELEPHONY_LOGE("write int32 reply failed.");
277         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
278     }
279     if (!reply.WriteRawData(&apnAttr, sizeof(ApnItem::Attribute))) {
280         TELEPHONY_LOGE("write apnAttr reply failed.");
281         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
282     }
283     return TELEPHONY_SUCCESS;
284 }
285 
OnGetDataConnIpType(MessageParcel & data,MessageParcel & reply)286 int32_t CellularDataServiceStub::OnGetDataConnIpType(MessageParcel &data, MessageParcel &reply)
287 {
288     int32_t slotId = data.ReadInt32();
289     std::string ipType;
290     int32_t result = GetDataConnIpType(slotId, ipType);
291     if (!reply.WriteInt32(result)) {
292         TELEPHONY_LOGE("write int32 reply failed.");
293         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
294     }
295     if (!reply.WriteString(ipType)) {
296         TELEPHONY_LOGE("write int32 reply failed.");
297         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
298     }
299     return TELEPHONY_SUCCESS;
300 }
301 
OnGetApnState(MessageParcel & data,MessageParcel & reply)302 int32_t CellularDataServiceStub::OnGetApnState(MessageParcel &data, MessageParcel &reply)
303 {
304     int32_t slotId = data.ReadInt32();
305     std::string apnType = data.ReadString();
306     int32_t result = GetApnState(slotId, apnType);
307     if (!reply.WriteInt32(result)) {
308         TELEPHONY_LOGE("fail to write parcel");
309         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
310     }
311     return result;
312 }
313 
OnGetRecoveryState(MessageParcel & data,MessageParcel & reply)314 int32_t CellularDataServiceStub::OnGetRecoveryState(MessageParcel &data, MessageParcel &reply)
315 {
316     int32_t result = GetDataRecoveryState();
317     if (!reply.WriteInt32(result)) {
318         TELEPHONY_LOGE("fail to write parcel");
319         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
320     }
321     return result;
322 }
323 
OnIsNeedDoRecovery(MessageParcel & data,MessageParcel & reply)324 int32_t CellularDataServiceStub::OnIsNeedDoRecovery(MessageParcel &data, MessageParcel &reply)
325 {
326     int32_t slotId = data.ReadInt32();
327     int32_t needDoRecovery = data.ReadBool();
328     int32_t result = IsNeedDoRecovery(slotId, needDoRecovery);
329     if (!reply.WriteInt32(result)) {
330         TELEPHONY_LOGE("write int32 reply failed.");
331         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
332     }
333     return result;
334 }
335 
OnInitCellularDataController(MessageParcel & data,MessageParcel & reply)336 int32_t CellularDataServiceStub::OnInitCellularDataController(MessageParcel &data, MessageParcel &reply)
337 {
338     int32_t slotId = data.ReadInt32();
339     int32_t result = InitCellularDataController(slotId);
340     if (!reply.WriteInt32(result)) {
341         TELEPHONY_LOGE("write int32 reply failed.");
342         return TELEPHONY_ERR_WRITE_REPLY_FAIL;
343     }
344     return result;
345 }
346 } // namespace Telephony
347 } // namespace OHOS