• 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 "telephony_state_registry_stub.h"
17 
18 #include "ipc_skeleton.h"
19 #include "string_ex.h"
20 
21 #include "sim_state_type.h"
22 #include "state_registry_errors.h"
23 #include "telephony_permission.h"
24 
25 namespace OHOS {
26 namespace Telephony {
TelephonyStateRegistryStub()27 TelephonyStateRegistryStub::TelephonyStateRegistryStub()
28 {
29     memberFuncMap_[StateNotifyInterfaceCode::CELL_INFO] = &TelephonyStateRegistryStub::OnUpdateCellInfo;
30     memberFuncMap_[StateNotifyInterfaceCode::SIM_STATE] = &TelephonyStateRegistryStub::OnUpdateSimState;
31     memberFuncMap_[StateNotifyInterfaceCode::SIGNAL_INFO] = &TelephonyStateRegistryStub::OnUpdateSignalInfo;
32     memberFuncMap_[StateNotifyInterfaceCode::NET_WORK_STATE] = &TelephonyStateRegistryStub::OnUpdateNetworkState;
33     memberFuncMap_[StateNotifyInterfaceCode::CALL_STATE] = &TelephonyStateRegistryStub::OnUpdateCallState;
34     memberFuncMap_[StateNotifyInterfaceCode::CALL_STATE_FOR_ID] =
35         &TelephonyStateRegistryStub::OnUpdateCallStateForSlotId;
36     memberFuncMap_[StateNotifyInterfaceCode::CELLULAR_DATA_STATE] =
37         &TelephonyStateRegistryStub::OnUpdateCellularDataConnectState;
38     memberFuncMap_[StateNotifyInterfaceCode::CELLULAR_DATA_FLOW] =
39         &TelephonyStateRegistryStub::OnUpdateCellularDataFlow;
40     memberFuncMap_[StateNotifyInterfaceCode::ADD_OBSERVER] = &TelephonyStateRegistryStub::OnRegisterStateChange;
41     memberFuncMap_[StateNotifyInterfaceCode::REMOVE_OBSERVER] = &TelephonyStateRegistryStub::OnUnregisterStateChange;
42     memberFuncMap_[StateNotifyInterfaceCode::CFU_INDICATOR] = &TelephonyStateRegistryStub::OnUpdateCfuIndicator;
43     memberFuncMap_[StateNotifyInterfaceCode::VOICE_MAIL_MSG_INDICATOR] =
44         &TelephonyStateRegistryStub::OnUpdateVoiceMailMsgIndicator;
45     memberFuncMap_[StateNotifyInterfaceCode::ICC_ACCOUNT_CHANGE] = &TelephonyStateRegistryStub::OnIccAccountUpdated;
46 }
47 
~TelephonyStateRegistryStub()48 TelephonyStateRegistryStub::~TelephonyStateRegistryStub()
49 {
50     memberFuncMap_.clear();
51 }
52 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)53 int32_t TelephonyStateRegistryStub::OnRemoteRequest(
54     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
55 {
56     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnRemoteRequest start##code = %{public}u", code);
57     std::u16string myToken = TelephonyStateRegistryStub::GetDescriptor();
58     std::u16string remoteToken = data.ReadInterfaceToken();
59     if (myToken != remoteToken) {
60         TELEPHONY_LOGE("TelephonyStateRegistryStub::OnRemoteRequest end##descriptor checked fail");
61         return TELEPHONY_ERR_DESCRIPTOR_MISMATCH;
62     }
63     auto itFunc = memberFuncMap_.find(static_cast<StateNotifyInterfaceCode>(code));
64     if (itFunc != memberFuncMap_.end()) {
65         auto memberFunc = itFunc->second;
66         if (memberFunc != nullptr) {
67             return (this->*memberFunc)(data, reply);
68         }
69     }
70     int ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
71     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnRemoteRequest end##ret=%{public}d", ret);
72     return ret;
73 }
74 
OnUpdateCallState(MessageParcel & data,MessageParcel & reply)75 int32_t TelephonyStateRegistryStub::OnUpdateCallState(MessageParcel &data, MessageParcel &reply)
76 {
77     int32_t slotId = data.ReadInt32();
78     int32_t callState = data.ReadInt32();
79     std::u16string phoneNumber = data.ReadString16();
80     int32_t ret = UpdateCallState(slotId, callState, phoneNumber);
81     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateCallState end##ret=%{public}d", ret);
82     reply.WriteInt32(ret);
83     return ret;
84 }
85 
OnUpdateSimState(MessageParcel & data,MessageParcel & reply)86 int32_t TelephonyStateRegistryStub::OnUpdateSimState(MessageParcel &data, MessageParcel &reply)
87 {
88     int32_t slotId = data.ReadInt32();
89     CardType type = static_cast<CardType>(data.ReadInt32());
90     SimState state = static_cast<SimState>(data.ReadInt32());
91     LockReason reason = static_cast<LockReason>(data.ReadInt32());
92     int32_t ret = UpdateSimState(slotId, type, state, reason);
93     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateSimState end##ret=%{public}d", ret);
94     reply.WriteInt32(ret);
95     return ret;
96 }
97 
OnUpdateCallStateForSlotId(MessageParcel & data,MessageParcel & reply)98 int32_t TelephonyStateRegistryStub::OnUpdateCallStateForSlotId(MessageParcel &data, MessageParcel &reply)
99 {
100     int32_t slotId = data.ReadInt32();
101     int32_t callId = data.ReadInt32();
102     int32_t callState = data.ReadInt32();
103     std::u16string incomingNumber = data.ReadString16();
104     int32_t ret = UpdateCallStateForSlotId(slotId, callId, callState, incomingNumber);
105     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateCallStateForSlotId end##ret=%{public}d", ret);
106     reply.WriteInt32(ret);
107     return ret;
108 }
109 
OnUpdateCellularDataConnectState(MessageParcel & data,MessageParcel & reply)110 int32_t TelephonyStateRegistryStub::OnUpdateCellularDataConnectState(MessageParcel &data, MessageParcel &reply)
111 {
112     int32_t slotId = data.ReadInt32();
113     int32_t dataState = data.ReadInt32();
114     int32_t networkType = data.ReadInt32();
115     int32_t ret = UpdateCellularDataConnectState(slotId, dataState, networkType);
116     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateCellularDataConnectState end##ret=%{public}d", ret);
117     reply.WriteInt32(ret);
118     return ret;
119 }
120 
OnUpdateCellularDataFlow(MessageParcel & data,MessageParcel & reply)121 int32_t TelephonyStateRegistryStub::OnUpdateCellularDataFlow(MessageParcel &data, MessageParcel &reply)
122 {
123     int32_t slotId = data.ReadInt32();
124     int32_t flowData = data.ReadInt32();
125     int32_t ret = UpdateCellularDataFlow(slotId, flowData);
126     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateCellularDataFlow end##ret=%{public}d", ret);
127     reply.WriteInt32(ret);
128     return ret;
129 }
130 
OnUpdateSignalInfo(MessageParcel & data,MessageParcel & reply)131 int32_t TelephonyStateRegistryStub::OnUpdateSignalInfo(MessageParcel &data, MessageParcel &reply)
132 {
133     int32_t ret = TELEPHONY_SUCCESS;
134     int32_t slotId = data.ReadInt32();
135     int32_t size = data.ReadInt32();
136     TELEPHONY_LOGD("TelephonyStateRegistryStub::OnUpdateSignalInfo size=%{public}d", size);
137     size = ((size > SignalInformation::MAX_SIGNAL_NUM) ? 0 : size);
138     if (size <= 0) {
139         ret = TELEPHONY_ERR_FAIL;
140         TELEPHONY_LOGE("TelephonyStateRegistryStub::OnUpdateSignalInfo size <= 0");
141         return ret;
142     }
143     std::vector<sptr<SignalInformation>> result;
144     parseSignalInfos(data, size, result);
145     ret = UpdateSignalInfo(slotId, result);
146     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateSignalInfo end##ret=%{public}d", ret);
147     reply.WriteInt32(ret);
148     return ret;
149 }
150 
parseSignalInfos(MessageParcel & data,const int32_t size,std::vector<sptr<SignalInformation>> & result)151 void TelephonyStateRegistryStub::parseSignalInfos(
152     MessageParcel &data, const int32_t size, std::vector<sptr<SignalInformation>> &result)
153 {
154     SignalInformation::NetworkType type;
155     for (int i = 0; i < size; ++i) {
156         type = static_cast<SignalInformation::NetworkType>(data.ReadInt32());
157         switch (type) {
158             case SignalInformation::NetworkType::GSM: {
159                 TELEPHONY_LOGI("TelephonyStateRegistryStub::parseSignalInfos NetworkType::GSM");
160                 std::unique_ptr<GsmSignalInformation> signal = std::make_unique<GsmSignalInformation>();
161                 if (signal != nullptr) {
162                     signal->ReadFromParcel(data);
163                     result.emplace_back(signal.release());
164                 }
165                 break;
166             }
167             case SignalInformation::NetworkType::CDMA: {
168                 TELEPHONY_LOGI("TelephonyStateRegistryStub::parseSignalInfos NetworkType::CDMA");
169                 std::unique_ptr<CdmaSignalInformation> signal = std::make_unique<CdmaSignalInformation>();
170                 if (signal != nullptr) {
171                     signal->ReadFromParcel(data);
172                     result.emplace_back(signal.release());
173                 }
174                 break;
175             }
176             case SignalInformation::NetworkType::LTE:
177                 [[fallthrough]]; // fall_through
178             case SignalInformation::NetworkType::NR: {
179                 ParseLteNrSignalInfos(data, result, type);
180                 break;
181             }
182             case SignalInformation::NetworkType::WCDMA: {
183                 TELEPHONY_LOGI("TelephonyStateRegistryStub::parseSignalInfos NetworkType::Wcdma");
184                 std::unique_ptr<WcdmaSignalInformation> signal = std::make_unique<WcdmaSignalInformation>();
185                 if (signal != nullptr) {
186                     signal->ReadFromParcel(data);
187                     result.emplace_back(signal.release());
188                 }
189                 break;
190             }
191             default:
192                 break;
193         }
194     }
195 }
196 
ParseLteNrSignalInfos(MessageParcel & data,std::vector<sptr<SignalInformation>> & result,SignalInformation::NetworkType type)197 void TelephonyStateRegistryStub::ParseLteNrSignalInfos(
198     MessageParcel &data, std::vector<sptr<SignalInformation>> &result, SignalInformation::NetworkType type)
199 {
200     switch (type) {
201         case SignalInformation::NetworkType::LTE: {
202             TELEPHONY_LOGD("TelephonyStateRegistryStub::ParseLteNrSignalInfos NetworkType::LTE");
203             std::unique_ptr<LteSignalInformation> signal = std::make_unique<LteSignalInformation>();
204             if (signal != nullptr) {
205                 signal->ReadFromParcel(data);
206                 result.emplace_back(signal.release());
207             }
208             break;
209         }
210         case SignalInformation::NetworkType::NR: {
211             TELEPHONY_LOGD("TelephonyStateRegistryStub::ParseSignalInfos");
212             std::unique_ptr<NrSignalInformation> signal = std::make_unique<NrSignalInformation>();
213             if (signal != nullptr) {
214                 signal->ReadFromParcel(data);
215                 result.emplace_back(signal.release());
216             }
217             break;
218         }
219         default:
220             break;
221     }
222 }
223 
OnUpdateCellInfo(MessageParcel & data,MessageParcel & reply)224 int32_t TelephonyStateRegistryStub::OnUpdateCellInfo(MessageParcel &data, MessageParcel &reply)
225 {
226     int32_t ret = TELEPHONY_SUCCESS;
227     int32_t slotId = data.ReadInt32();
228     int32_t size = data.ReadInt32();
229     TELEPHONY_LOGI("TelephonyStateRegistryStub OnUpdateCellInfo:size=%{public}d", size);
230     size = ((size > CellInformation::MAX_CELL_NUM) ? 0 : size);
231     if (size <= 0) {
232         ret = TELEPHONY_ERR_FAIL;
233         TELEPHONY_LOGE("TelephonyStateRegistryStub the size less than or equal to 0!");
234         return ret;
235     }
236     std::vector<sptr<CellInformation>> cells;
237     CellInformation::CellType type;
238     for (int i = 0; i < size; ++i) {
239         type = static_cast<CellInformation::CellType>(data.ReadInt32());
240         switch (type) {
241             case CellInformation::CellType::CELL_TYPE_GSM: {
242                 std::unique_ptr<GsmCellInformation> cell = std::make_unique<GsmCellInformation>();
243                 if (cell != nullptr) {
244                     cell->ReadFromParcel(data);
245                     cells.emplace_back(cell.release());
246                 }
247                 break;
248             }
249             case CellInformation::CellType::CELL_TYPE_LTE: {
250                 std::unique_ptr<LteCellInformation> cell = std::make_unique<LteCellInformation>();
251                 if (cell != nullptr) {
252                     cell->ReadFromParcel(data);
253                     cells.emplace_back(cell.release());
254                 }
255                 break;
256             }
257             case CellInformation::CellType::CELL_TYPE_NR: {
258                 std::unique_ptr<NrCellInformation> cell = std::make_unique<NrCellInformation>();
259                 if (cell != nullptr) {
260                     cell->ReadFromParcel(data);
261                     cells.emplace_back(cell.release());
262                 }
263                 break;
264             }
265             default:
266                 break;
267         }
268     }
269     ret = UpdateCellInfo(slotId, cells);
270     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateCellInfo end##ret=%{public}d", ret);
271     reply.WriteInt32(ret);
272     return ret;
273 }
274 
OnUpdateNetworkState(MessageParcel & data,MessageParcel & reply)275 int32_t TelephonyStateRegistryStub::OnUpdateNetworkState(MessageParcel &data, MessageParcel &reply)
276 {
277     int32_t ret = TELEPHONY_SUCCESS;
278     int32_t slotId = data.ReadInt32();
279     sptr<NetworkState> result = NetworkState::Unmarshalling(data);
280     if (result == nullptr) {
281         TELEPHONY_LOGE("TelephonyStateRegistryStub::OnUpdateNetworkState GetNetworkStatus  is null");
282         ret = TELEPHONY_ERR_FAIL;
283         return ret;
284     }
285     ret = UpdateNetworkState(slotId, result);
286     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateNetworkState end##ret=%{public}d", ret);
287     reply.WriteInt32(ret);
288     return ret;
289 }
290 
OnRegisterStateChange(MessageParcel & data,MessageParcel & reply)291 int32_t TelephonyStateRegistryStub::OnRegisterStateChange(MessageParcel &data, MessageParcel &reply)
292 {
293     int32_t ret = TELEPHONY_SUCCESS;
294     int32_t slotId = data.ReadInt32();
295     int32_t mask = data.ReadInt32();
296     bool notifyNow = data.ReadBool();
297     sptr<TelephonyObserverBroker> callback = nullptr;
298     ret = ReadData(data, reply, callback);
299     if (ret != TELEPHONY_SUCCESS) {
300         reply.WriteInt32(ret);
301         TELEPHONY_LOGE("TelephonyStateRegistryStub::OnRegisterStateChange ReadData failed");
302         return ret;
303     }
304     ret = RegisterStateChange(callback, slotId, mask, notifyNow);
305     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnRegisterStateChange end##ret=%{public}d", ret);
306     reply.WriteInt32(ret);
307     return ret;
308 }
309 
OnUnregisterStateChange(MessageParcel & data,MessageParcel & reply)310 int32_t TelephonyStateRegistryStub::OnUnregisterStateChange(MessageParcel &data, MessageParcel &reply)
311 {
312     int32_t slotId = data.ReadInt32();
313     int32_t mask = data.ReadInt32();
314     int32_t ret = UnregisterStateChange(slotId, mask);
315     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUnregisterStateChange end##ret=%{public}d", ret);
316     reply.WriteInt32(ret);
317     return ret;
318 }
319 
ReadData(MessageParcel & data,MessageParcel & reply,sptr<TelephonyObserverBroker> & callback)320 int32_t TelephonyStateRegistryStub::ReadData(
321     MessageParcel &data, MessageParcel &reply, sptr<TelephonyObserverBroker> &callback)
322 {
323     int32_t result = TELEPHONY_SUCCESS;
324     sptr<IRemoteObject> remote = data.ReadRemoteObject();
325     if (remote == nullptr) {
326         TELEPHONY_LOGE("TelephonyStateRegistryStub::ReadData  remote is nullptr.");
327         result = TELEPHONY_ERR_FAIL;
328         reply.WriteInt32(result);
329         return result;
330     }
331     callback = iface_cast<TelephonyObserverBroker>(remote);
332     if (callback == nullptr) {
333         TELEPHONY_LOGE("TelephonyStateRegistryStub::ReadData callback is nullptr.");
334         result = TELEPHONY_ERR_FAIL;
335         reply.WriteInt32(result);
336         return result;
337     }
338     return result;
339 }
340 
OnUpdateCfuIndicator(MessageParcel & data,MessageParcel & reply)341 int32_t TelephonyStateRegistryStub::OnUpdateCfuIndicator(MessageParcel &data, MessageParcel &reply)
342 {
343     int32_t slotId = data.ReadInt32();
344     bool cfuResult = data.ReadBool();
345     int32_t ret = UpdateCfuIndicator(slotId, cfuResult);
346     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateCfuIndicator end##ret=%{public}d", ret);
347     reply.WriteInt32(ret);
348     return ret;
349 }
350 
OnUpdateVoiceMailMsgIndicator(MessageParcel & data,MessageParcel & reply)351 int32_t TelephonyStateRegistryStub::OnUpdateVoiceMailMsgIndicator(MessageParcel &data, MessageParcel &reply)
352 {
353     int32_t slotId = data.ReadInt32();
354     bool voiceMailMsgResult = data.ReadBool();
355     int32_t ret = UpdateVoiceMailMsgIndicator(slotId, voiceMailMsgResult);
356     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateVoiceMailMsgIndicator end##ret=%{public}d", ret);
357     reply.WriteInt32(ret);
358     return ret;
359 }
360 
OnIccAccountUpdated(MessageParcel & data,MessageParcel & reply)361 int32_t TelephonyStateRegistryStub::OnIccAccountUpdated(MessageParcel &data, MessageParcel &reply)
362 {
363     int32_t ret = UpdateIccAccount();
364     TELEPHONY_LOGI("end##ret=%{public}d", ret);
365     reply.WriteInt32(ret);
366     return ret;
367 }
368 
RegisterStateChange(const sptr<TelephonyObserverBroker> & telephonyObserver,int32_t slotId,uint32_t mask,bool isUpdate)369 int32_t TelephonyStateRegistryStub::RegisterStateChange(const sptr<TelephonyObserverBroker> &telephonyObserver,
370     int32_t slotId, uint32_t mask, bool isUpdate)
371 {
372     int32_t uid = IPCSkeleton::GetCallingUid();
373     std::string bundleName = "";
374     TelephonyPermission::GetBundleNameByUid(uid, bundleName);
375     return RegisterStateChange(telephonyObserver, slotId, mask, bundleName, isUpdate,
376         IPCSkeleton::GetCallingPid(), uid);
377 }
378 
UnregisterStateChange(int32_t slotId,uint32_t mask)379 int32_t TelephonyStateRegistryStub::UnregisterStateChange(int32_t slotId, uint32_t mask)
380 {
381     int32_t uid = IPCSkeleton::GetCallingUid();
382     return UnregisterStateChange(slotId, mask, IPCSkeleton::GetCallingPid(), uid);
383 }
384 } // namespace Telephony
385 } // namespace OHOS