• 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_proxy.h"
17 
18 #include "iremote_object.h"
19 #include "message_option.h"
20 #include "message_parcel.h"
21 #include "telephony_errors.h"
22 #include "telephony_log_wrapper.h"
23 
24 namespace OHOS {
25 namespace Telephony {
IsCellularDataEnabled(bool & dataEnabled)26 int32_t CellularDataServiceProxy::IsCellularDataEnabled(bool &dataEnabled)
27 {
28     MessageParcel data;
29     MessageParcel reply;
30     MessageOption option;
31     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
32     if (Remote() == nullptr) {
33         TELEPHONY_LOGE("remote is null");
34         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
35     }
36     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::IS_CELLULAR_DATA_ENABLED, data, reply, option);
37     if (error != TELEPHONY_SUCCESS) {
38         TELEPHONY_LOGE("function IsCellularDataEnabled call failed! errCode:%{public}d", error);
39         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
40     }
41     int32_t result = reply.ReadInt32();
42     if (result == TELEPHONY_SUCCESS) {
43         dataEnabled = reply.ReadBool();
44     }
45 
46     return result;
47 }
48 
EnableCellularData(bool enable)49 int32_t CellularDataServiceProxy::EnableCellularData(bool enable)
50 {
51     MessageParcel data;
52     MessageParcel reply;
53     MessageOption option;
54     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
55     data.WriteBool(enable);
56     if (Remote() == nullptr) {
57         TELEPHONY_LOGE("remote is null");
58         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
59     }
60     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::ENABLE_CELLULAR_DATA, data, reply, option);
61     if (error != TELEPHONY_SUCCESS) {
62         TELEPHONY_LOGE("function EnableCellularData call failed! errCode:%{public}d", error);
63         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
64     }
65     int32_t result = reply.ReadInt32();
66     return result;
67 }
68 
GetCellularDataState()69 int32_t CellularDataServiceProxy::GetCellularDataState()
70 {
71     MessageParcel data;
72     MessageParcel reply;
73     MessageOption option;
74     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
75     if (Remote() == nullptr) {
76         TELEPHONY_LOGE("remote is null");
77         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
78     }
79     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::GET_CELLULAR_DATA_STATE, data, reply, option);
80     if (error != TELEPHONY_SUCCESS) {
81         TELEPHONY_LOGE("function GetCellularDataState call failed! errCode:%{public}d", error);
82         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
83     }
84     int32_t result = reply.ReadInt32();
85     return result;
86 }
87 
IsCellularDataRoamingEnabled(int32_t slotId,bool & dataRoamingEnabled)88 int32_t CellularDataServiceProxy::IsCellularDataRoamingEnabled(int32_t slotId, bool &dataRoamingEnabled)
89 {
90     MessageParcel data;
91     MessageParcel reply;
92     MessageOption option;
93     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
94     data.WriteInt32(slotId);
95     if (Remote() == nullptr) {
96         TELEPHONY_LOGE("remote is null");
97         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
98     }
99     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::IS_DATA_ROAMING_ENABLED, data, reply, option);
100     if (error != TELEPHONY_SUCCESS) {
101         TELEPHONY_LOGE("function IsCellularDataRoamingEnabled call failed! errCode:%{public}d", error);
102         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
103     }
104     int32_t result = reply.ReadInt32();
105     if (result == TELEPHONY_SUCCESS) {
106         dataRoamingEnabled = reply.ReadBool();
107     }
108 
109     return result;
110 }
111 
EnableCellularDataRoaming(int32_t slotId,bool enable)112 int32_t CellularDataServiceProxy::EnableCellularDataRoaming(int32_t slotId, bool enable)
113 {
114     MessageParcel data;
115     MessageParcel reply;
116     MessageOption option;
117     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
118     data.WriteInt32(slotId);
119     data.WriteBool(enable);
120     if (Remote() == nullptr) {
121         TELEPHONY_LOGE("remote is null");
122         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
123     }
124     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::ENABLE_DATA_ROAMING, data, reply, option);
125     if (error != TELEPHONY_SUCCESS) {
126         TELEPHONY_LOGE("function EnableCellularDataRoaming call failed! errCode:%{public}d", error);
127         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
128     }
129     int32_t result = reply.ReadInt32();
130     return result;
131 }
132 
HandleApnChanged(int32_t slotId)133 int32_t CellularDataServiceProxy::HandleApnChanged(int32_t slotId)
134 {
135     MessageParcel data;
136     MessageParcel reply;
137     MessageOption option;
138     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
139     data.WriteInt32(slotId);
140     if (Remote() == nullptr) {
141         TELEPHONY_LOGE("remote is null");
142         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
143     }
144     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::APN_DATA_CHANGED, data, reply, option);
145     if (error != TELEPHONY_SUCCESS) {
146         TELEPHONY_LOGE("HandleApnChanged call failed! errCode:%{public}d", error);
147         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
148     }
149     int32_t result = reply.ReadInt32();
150     return result;
151 }
152 
GetDefaultCellularDataSlotId()153 int32_t CellularDataServiceProxy::GetDefaultCellularDataSlotId()
154 {
155     MessageParcel data;
156     MessageParcel reply;
157     MessageOption option;
158     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
159     if (Remote() == nullptr) {
160         TELEPHONY_LOGE("remote is null");
161         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
162     }
163     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::GET_DEFAULT_SLOT_ID, data, reply, option);
164     if (error != TELEPHONY_SUCCESS) {
165         TELEPHONY_LOGE("function GetDefaultCellularDataSlotId call failed! errCode:%{public}d", error);
166         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
167     }
168     int32_t result = reply.ReadInt32();
169     return result;
170 }
171 
SetDefaultCellularDataSlotId(int32_t slotId)172 int32_t CellularDataServiceProxy::SetDefaultCellularDataSlotId(int32_t slotId)
173 {
174     MessageParcel data;
175     MessageParcel reply;
176     MessageOption option;
177     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
178     data.WriteInt32(slotId);
179     if (Remote() == nullptr) {
180         TELEPHONY_LOGE("remote is null");
181         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
182     }
183     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::SET_DEFAULT_SLOT_ID, data, reply, option);
184     if (error != TELEPHONY_SUCCESS) {
185         TELEPHONY_LOGE("function SetDefaultCellularDataSlotId call failed! errCode:%{public}d", error);
186         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
187     }
188     int32_t result = reply.ReadInt32();
189     return result;
190 }
191 
GetCellularDataFlowType()192 int32_t CellularDataServiceProxy::GetCellularDataFlowType()
193 {
194     MessageParcel data;
195     MessageParcel reply;
196     MessageOption option;
197     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
198     if (Remote() == nullptr) {
199         TELEPHONY_LOGE("remote is null");
200         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
201     }
202     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::GET_FLOW_TYPE_ID, data, reply, option);
203     if (error != TELEPHONY_SUCCESS) {
204         TELEPHONY_LOGE("function GetCellularDataFlowType call failed! errCode:%{public}d", error);
205         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
206     }
207     int32_t result = reply.ReadInt32();
208     return result;
209 }
210 
HasInternetCapability(int32_t slotId,int32_t cid)211 int32_t CellularDataServiceProxy::HasInternetCapability(int32_t slotId, int32_t cid)
212 {
213     MessageParcel data;
214     MessageParcel reply;
215     MessageOption option;
216     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
217     data.WriteInt32(slotId);
218     data.WriteInt32(cid);
219     if (Remote() == nullptr) {
220         TELEPHONY_LOGE("remote is null");
221         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
222     }
223     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::HAS_CAPABILITY, data, reply, option);
224     if (error != TELEPHONY_SUCCESS) {
225         TELEPHONY_LOGE("Strategy switch fail! errCode:%{public}d", error);
226         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
227     }
228     int32_t result = reply.ReadInt32();
229     return result;
230 }
231 
ClearCellularDataConnections(int32_t slotId)232 int32_t CellularDataServiceProxy::ClearCellularDataConnections(int32_t slotId)
233 {
234     MessageParcel data;
235     MessageParcel reply;
236     MessageOption option;
237     data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor());
238     data.WriteInt32(slotId);
239     if (Remote() == nullptr) {
240         TELEPHONY_LOGE("remote is null");
241         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
242     }
243     int32_t error = Remote()->SendRequest((uint32_t)FuncCode::CLEAR_ALL_CONNECTIONS, data, reply, option);
244     if (error != TELEPHONY_SUCCESS) {
245         TELEPHONY_LOGE("Strategy switch fail! errCode:%{public}d", error);
246         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
247     }
248     int32_t result = reply.ReadInt32();
249     return result;
250 }
251 
RegisterSimAccountCallback(const sptr<SimAccountCallback> & callback)252 int32_t CellularDataServiceProxy::RegisterSimAccountCallback(const sptr<SimAccountCallback> &callback)
253 {
254     if (callback == nullptr) {
255         TELEPHONY_LOGE("callback is nullptr!");
256         return TELEPHONY_ERR_ARGUMENT_NULL;
257     }
258     MessageParcel data;
259     MessageParcel reply;
260     MessageOption option;
261     if (!data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor())) {
262         TELEPHONY_LOGE("write interface token failed!");
263         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
264     }
265     if (!data.WriteRemoteObject(callback->AsObject().GetRefPtr())) {
266         TELEPHONY_LOGE("write remote object failed!");
267         return TELEPHONY_ERR_WRITE_DATA_FAIL;
268     }
269     sptr<OHOS::IRemoteObject> remote = Remote();
270     if (remote == nullptr) {
271         TELEPHONY_LOGE("remote is nullptr!");
272         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
273     }
274     int32_t error = remote->SendRequest(static_cast<uint32_t>(FuncCode::REG_SIM_ACCOUNT_CALLBACK), data, reply, option);
275     if (error != TELEPHONY_SUCCESS) {
276         TELEPHONY_LOGE("error! errCode:%{public}d", error);
277         return error;
278     }
279     return reply.ReadInt32();
280 }
281 
UnregisterSimAccountCallback()282 int32_t CellularDataServiceProxy::UnregisterSimAccountCallback()
283 {
284     MessageParcel data;
285     MessageParcel reply;
286     MessageOption option;
287     if (!data.WriteInterfaceToken(CellularDataServiceProxy::GetDescriptor())) {
288         TELEPHONY_LOGE("write interface token failed!");
289         return TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL;
290     }
291     sptr<OHOS::IRemoteObject> remote = Remote();
292     if (remote == nullptr) {
293         TELEPHONY_LOGE("remote is nullptr!");
294         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
295     }
296     int32_t error =
297         remote->SendRequest(static_cast<uint32_t>(FuncCode::UN_REG_SIM_ACCOUNT_CALLBACK), data, reply, option);
298     if (error != TELEPHONY_SUCCESS) {
299         TELEPHONY_LOGE("error! errCode:%{public}d", error);
300         return error;
301     }
302     return reply.ReadInt32();
303 }
304 } // namespace Telephony
305 } // namespace OHOS