• 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_LOGI("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                 TELEPHONY_LOGI("TelephonyStateRegistryStub::parseSignalInfos NetworkType::LTE");
178                 std::unique_ptr<LteSignalInformation> signal = std::make_unique<LteSignalInformation>();
179                 if (signal != nullptr) {
180                     signal->ReadFromParcel(data);
181                     result.emplace_back(signal.release());
182                 }
183                 break;
184             }
185             case SignalInformation::NetworkType::WCDMA: {
186                 TELEPHONY_LOGI("TelephonyStateRegistryStub::UpdateSignalInfoInner NetworkType::Wcdma");
187                 std::unique_ptr<WcdmaSignalInformation> signal = std::make_unique<WcdmaSignalInformation>();
188                 if (signal != nullptr) {
189                     signal->ReadFromParcel(data);
190                     result.emplace_back(signal.release());
191                 }
192                 break;
193             }
194             default:
195                 break;
196         }
197     }
198 }
199 
OnUpdateCellInfo(MessageParcel & data,MessageParcel & reply)200 int32_t TelephonyStateRegistryStub::OnUpdateCellInfo(MessageParcel &data, MessageParcel &reply)
201 {
202     int32_t ret = TELEPHONY_SUCCESS;
203     int32_t slotId = data.ReadInt32();
204     int32_t size = data.ReadInt32();
205     TELEPHONY_LOGI("TelephonyStateRegistryStub OnUpdateCellInfo:size=%{public}d", size);
206     size = ((size > CellInformation::MAX_CELL_NUM) ? 0 : size);
207     if (size <= 0) {
208         ret = TELEPHONY_ERR_FAIL;
209         TELEPHONY_LOGE("TelephonyStateRegistryStub the size less than or equal to 0!");
210         return ret;
211     }
212     std::vector<sptr<CellInformation>> cells;
213     CellInformation::CellType type;
214     for (int i = 0; i < size; ++i) {
215         type = static_cast<CellInformation::CellType>(data.ReadInt32());
216         switch (type) {
217             case CellInformation::CellType::CELL_TYPE_GSM: {
218                 std::unique_ptr<GsmCellInformation> cell = std::make_unique<GsmCellInformation>();
219                 if (cell != nullptr) {
220                     cell->ReadFromParcel(data);
221                     cells.emplace_back(cell.release());
222                 }
223                 break;
224             }
225             case CellInformation::CellType::CELL_TYPE_LTE: {
226                 std::unique_ptr<LteCellInformation> cell = std::make_unique<LteCellInformation>();
227                 if (cell != nullptr) {
228                     cell->ReadFromParcel(data);
229                     cells.emplace_back(cell.release());
230                 }
231                 break;
232             }
233             default:
234                 break;
235         }
236     }
237     ret = UpdateCellInfo(slotId, cells);
238     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateCellInfo end##ret=%{public}d", ret);
239     reply.WriteInt32(ret);
240     return ret;
241 }
242 
OnUpdateNetworkState(MessageParcel & data,MessageParcel & reply)243 int32_t TelephonyStateRegistryStub::OnUpdateNetworkState(MessageParcel &data, MessageParcel &reply)
244 {
245     int32_t ret = TELEPHONY_SUCCESS;
246     int32_t slotId = data.ReadInt32();
247     sptr<NetworkState> result = NetworkState::Unmarshalling(data);
248     if (result == nullptr) {
249         TELEPHONY_LOGE("TelephonyStateRegistryStub::OnUpdateNetworkState GetNetworkStatus  is null");
250         ret = TELEPHONY_ERR_FAIL;
251         return ret;
252     }
253     ret = UpdateNetworkState(slotId, result);
254     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateNetworkState end##ret=%{public}d", ret);
255     reply.WriteInt32(ret);
256     return ret;
257 }
258 
OnRegisterStateChange(MessageParcel & data,MessageParcel & reply)259 int32_t TelephonyStateRegistryStub::OnRegisterStateChange(MessageParcel &data, MessageParcel &reply)
260 {
261     int32_t ret = TELEPHONY_SUCCESS;
262     int32_t slotId = data.ReadInt32();
263     int32_t mask = data.ReadInt32();
264     bool notifyNow = data.ReadBool();
265     sptr<TelephonyObserverBroker> callback = nullptr;
266     ret = ReadData(data, reply, callback);
267     if (ret != TELEPHONY_SUCCESS) {
268         reply.WriteInt32(ret);
269         TELEPHONY_LOGE("TelephonyStateRegistryStub::OnRegisterStateChange ReadData failed");
270         return ret;
271     }
272     ret = RegisterStateChange(callback, slotId, mask, notifyNow);
273     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnRegisterStateChange end##ret=%{public}d", ret);
274     reply.WriteInt32(ret);
275     return ret;
276 }
277 
OnUnregisterStateChange(MessageParcel & data,MessageParcel & reply)278 int32_t TelephonyStateRegistryStub::OnUnregisterStateChange(MessageParcel &data, MessageParcel &reply)
279 {
280     int32_t slotId = data.ReadInt32();
281     int32_t mask = data.ReadInt32();
282     int32_t ret = UnregisterStateChange(slotId, mask);
283     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUnregisterStateChange end##ret=%{public}d", ret);
284     reply.WriteInt32(ret);
285     return ret;
286 }
287 
ReadData(MessageParcel & data,MessageParcel & reply,sptr<TelephonyObserverBroker> & callback)288 int32_t TelephonyStateRegistryStub::ReadData(
289     MessageParcel &data, MessageParcel &reply, sptr<TelephonyObserverBroker> &callback)
290 {
291     int32_t result = TELEPHONY_SUCCESS;
292     sptr<IRemoteObject> remote = data.ReadRemoteObject();
293     if (remote == nullptr) {
294         TELEPHONY_LOGE("TelephonyStateRegistryStub::ReadData  remote is nullptr.");
295         result = TELEPHONY_ERR_FAIL;
296         reply.WriteInt32(result);
297         return result;
298     }
299     callback = iface_cast<TelephonyObserverBroker>(remote);
300     if (callback == nullptr) {
301         TELEPHONY_LOGE("TelephonyStateRegistryStub::ReadData callback is nullptr.");
302         result = TELEPHONY_ERR_FAIL;
303         reply.WriteInt32(result);
304         return result;
305     }
306     return result;
307 }
308 
OnUpdateCfuIndicator(MessageParcel & data,MessageParcel & reply)309 int32_t TelephonyStateRegistryStub::OnUpdateCfuIndicator(MessageParcel &data, MessageParcel &reply)
310 {
311     int32_t slotId = data.ReadInt32();
312     bool cfuResult = data.ReadBool();
313     int32_t ret = UpdateCfuIndicator(slotId, cfuResult);
314     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateCfuIndicator end##ret=%{public}d", ret);
315     reply.WriteInt32(ret);
316     return ret;
317 }
318 
OnUpdateVoiceMailMsgIndicator(MessageParcel & data,MessageParcel & reply)319 int32_t TelephonyStateRegistryStub::OnUpdateVoiceMailMsgIndicator(MessageParcel &data, MessageParcel &reply)
320 {
321     int32_t slotId = data.ReadInt32();
322     bool voiceMailMsgResult = data.ReadBool();
323     int32_t ret = UpdateVoiceMailMsgIndicator(slotId, voiceMailMsgResult);
324     TELEPHONY_LOGI("TelephonyStateRegistryStub::OnUpdateVoiceMailMsgIndicator end##ret=%{public}d", ret);
325     reply.WriteInt32(ret);
326     return ret;
327 }
328 
OnIccAccountUpdated(MessageParcel & data,MessageParcel & reply)329 int32_t TelephonyStateRegistryStub::OnIccAccountUpdated(MessageParcel &data, MessageParcel &reply)
330 {
331     int32_t ret = UpdateIccAccount();
332     TELEPHONY_LOGI("end##ret=%{public}d", ret);
333     reply.WriteInt32(ret);
334     return ret;
335 }
336 
RegisterStateChange(const sptr<TelephonyObserverBroker> & telephonyObserver,int32_t slotId,uint32_t mask,bool isUpdate)337 int32_t TelephonyStateRegistryStub::RegisterStateChange(const sptr<TelephonyObserverBroker> &telephonyObserver,
338     int32_t slotId, uint32_t mask, bool isUpdate)
339 {
340     int32_t uid = IPCSkeleton::GetCallingUid();
341     std::string bundleName = "";
342     TelephonyPermission::GetBundleNameByUid(uid, bundleName);
343     return RegisterStateChange(telephonyObserver, slotId, mask, bundleName, isUpdate, IPCSkeleton::GetCallingPid());
344 }
345 
UnregisterStateChange(int32_t slotId,uint32_t mask)346 int32_t TelephonyStateRegistryStub::UnregisterStateChange(int32_t slotId, uint32_t mask)
347 {
348     return UnregisterStateChange(slotId, mask, IPCSkeleton::GetCallingPid());
349 }
350 } // namespace Telephony
351 } // namespace OHOS