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_service.h"
17
18 #include <sstream>
19
20 #include "common_event_manager.h"
21 #include "common_event_support.h"
22 #include "iservice_registry.h"
23 #include "state_registry_errors.h"
24 #include "string_ex.h"
25 #include "system_ability.h"
26 #include "system_ability_definition.h"
27 #include "telephony_permission.h"
28 #include "telephony_state_manager.h"
29 #include "telephony_state_registry_dump_helper.h"
30 #include "telephony_types.h"
31
32 namespace OHOS {
33 namespace Telephony {
34 using namespace OHOS::EventFwk;
35 bool g_registerResult =
36 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<TelephonyStateRegistryService>::GetInstance().get());
37
TelephonyStateRegistryService()38 TelephonyStateRegistryService::TelephonyStateRegistryService()
39 : SystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID, true)
40 {
41 TELEPHONY_LOGI("TelephonyStateRegistryService SystemAbility create");
42 slotSize_ = SIM_SLOT_COUNT;
43 }
44
~TelephonyStateRegistryService()45 TelephonyStateRegistryService::~TelephonyStateRegistryService()
46 {
47 stateRecords_.clear();
48 callState_.clear();
49 callIncomingNumber_.clear();
50 signalInfos_.clear();
51 searchNetworkState_.clear();
52 }
53
OnStart()54 void TelephonyStateRegistryService::OnStart()
55 {
56 bindStartTime_ =
57 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
58 .count();
59 std::lock_guard<std::mutex> guard(lock_);
60 if (state_ == ServiceRunningState::STATE_RUNNING) {
61 TELEPHONY_LOGE("Leave, FAILED, already running");
62 return;
63 }
64 state_ = ServiceRunningState::STATE_RUNNING;
65 bool ret = SystemAbility::Publish(DelayedSingleton<TelephonyStateRegistryService>::GetInstance().get());
66 if (!ret) {
67 TELEPHONY_LOGE("Leave, Failed to publish TelephonyStateRegistryService");
68 }
69 TELEPHONY_LOGI("TelephonyStateRegistryService start success.");
70 bindEndTime_ =
71 std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch())
72 .count();
73 if (IsCommonEventServiceAbilityExist()) {
74 for (int32_t i = 0; i < slotSize_; i++) {
75 TELEPHONY_LOGI("TelephonyStateRegistryService send disconnected call state.");
76 SendCallStateChanged(i, static_cast<int32_t>(TelCallState::CALL_STATUS_DISCONNECTED), u"");
77 }
78 }
79 }
80
OnStop()81 void TelephonyStateRegistryService::OnStop()
82 {
83 TELEPHONY_LOGI("TelephonyStateRegistryService OnStop ");
84 std::lock_guard<std::mutex> guard(lock_);
85 state_ = ServiceRunningState::STATE_STOPPED;
86 }
87
Finalize()88 void TelephonyStateRegistryService::Finalize()
89 {
90 TELEPHONY_LOGI("TelephonyStateRegistryService Finalize");
91 }
92
OnDump()93 void TelephonyStateRegistryService::OnDump() {}
94
UpdateCellularDataConnectState(int32_t slotId,int32_t dataState,int32_t networkType)95 int32_t TelephonyStateRegistryService::UpdateCellularDataConnectState(
96 int32_t slotId, int32_t dataState, int32_t networkType)
97 {
98 if (!VerifySlotId(slotId)) {
99 TELEPHONY_LOGE("UpdateCellularDataConnectState##VerifySlotId failed ##slotId = %{public}d", slotId);
100 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
101 }
102 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
103 TELEPHONY_LOGE("Check permission failed.");
104 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
105 }
106 std::lock_guard<std::mutex> guard(lock_);
107 cellularDataConnectionState_[slotId] = dataState;
108 cellularDataConnectionNetworkType_[slotId] = networkType;
109 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
110 for (size_t i = 0; i < stateRecords_.size(); i++) {
111 TelephonyStateRegistryRecord record = stateRecords_[i];
112 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_DATA_CONNECTION_STATE) &&
113 (record.slotId_ == slotId) && record.telephonyObserver_ != nullptr) {
114 record.telephonyObserver_->OnCellularDataConnectStateUpdated(slotId, dataState, networkType);
115 result = TELEPHONY_SUCCESS;
116 }
117 }
118 SendCellularDataConnectStateChanged(slotId, dataState, networkType);
119 return result;
120 }
121
UpdateCellularDataFlow(int32_t slotId,int32_t flowData)122 int32_t TelephonyStateRegistryService::UpdateCellularDataFlow(int32_t slotId, int32_t flowData)
123 {
124 if (!VerifySlotId(slotId)) {
125 TELEPHONY_LOGE("UpdateCellularDataFlow##VerifySlotId failed ##slotId = %{public}d", slotId);
126 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
127 }
128 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
129 TELEPHONY_LOGE("Check permission failed.");
130 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
131 }
132 cellularDataFlow_[slotId] = flowData;
133 std::lock_guard<std::mutex> guard(lock_);
134 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
135 for (size_t i = 0; i < stateRecords_.size(); i++) {
136 TelephonyStateRegistryRecord record = stateRecords_[i];
137 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_DATA_FLOW) &&
138 (record.slotId_ == slotId) && record.telephonyObserver_ != nullptr) {
139 record.telephonyObserver_->OnCellularDataFlowUpdated(slotId, flowData);
140 result = TELEPHONY_SUCCESS;
141 }
142 }
143 return result;
144 }
145
UpdateCallState(int32_t slotId,int32_t callState,const std::u16string & number)146 int32_t TelephonyStateRegistryService::UpdateCallState(int32_t slotId, int32_t callState, const std::u16string &number)
147 {
148 if (!VerifySlotId(slotId)) {
149 TELEPHONY_LOGE("UpdateCallState##VerifySlotId failed ##slotId = %{public}d", slotId);
150 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
151 }
152 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
153 TELEPHONY_LOGE("Check permission failed.");
154 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
155 }
156 std::lock_guard<std::mutex> guard(lock_);
157 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
158 for (size_t i = 0; i < stateRecords_.size(); i++) {
159 TelephonyStateRegistryRecord record = stateRecords_[i];
160 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_CALL_STATE) &&
161 (record.slotId_ == slotId) && record.telephonyObserver_ != nullptr) {
162 std::u16string phoneNumberStr;
163 if (record.IsCanReadCallHistory()) {
164 phoneNumberStr = number;
165 } else {
166 phoneNumberStr = Str8ToStr16("");
167 }
168 record.telephonyObserver_->OnCallStateUpdated(slotId, callState, phoneNumberStr);
169 result = TELEPHONY_SUCCESS;
170 }
171 }
172 SendCallStateChanged(slotId, callState, number);
173 return result;
174 }
175
UpdateCallStateForSlotId(int32_t slotId,int32_t callId,int32_t callState,const std::u16string & incomingNumber)176 int32_t TelephonyStateRegistryService::UpdateCallStateForSlotId(
177 int32_t slotId, int32_t callId, int32_t callState, const std::u16string &incomingNumber)
178 {
179 std::u16string incomingNumberStr = incomingNumber;
180 if (!VerifySlotId(slotId)) {
181 TELEPHONY_LOGE("UpdateCallStateForSlotId##VerifySlotId failed ##slotId = %{public}d", slotId);
182 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
183 }
184 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
185 TELEPHONY_LOGE("Check permission failed.");
186 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
187 }
188 std::lock_guard<std::mutex> guard(lock_);
189 callState_[slotId] = callState;
190 callIncomingNumber_[slotId] = incomingNumber;
191 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
192 for (size_t i = 0; i < stateRecords_.size(); i++) {
193 TelephonyStateRegistryRecord record = stateRecords_[i];
194 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_CALL_STATE) &&
195 (record.slotId_ == slotId) && record.telephonyObserver_ != nullptr) {
196 incomingNumberStr = GetCallIncomingNumberForSlotId(record, slotId);
197 record.telephonyObserver_->OnCallStateUpdated(slotId, callState, incomingNumberStr);
198 result = TELEPHONY_SUCCESS;
199 }
200 }
201 SendCallStateChanged(slotId, callState, incomingNumberStr);
202 return result;
203 }
204
UpdateSimState(int32_t slotId,CardType type,SimState state,LockReason reason)205 int32_t TelephonyStateRegistryService::UpdateSimState(int32_t slotId, CardType type, SimState state, LockReason reason)
206 {
207 if (!VerifySlotId(slotId)) {
208 TELEPHONY_LOGE("UpdateSimState##VerifySlotId failed ##slotId = %{public}d", slotId);
209 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
210 }
211 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
212 TELEPHONY_LOGE("Check permission failed.");
213 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
214 }
215 std::lock_guard<std::mutex> guard(lock_);
216 simState_[slotId] = state;
217 simReason_[slotId] = reason;
218 cardType_[slotId] = type;
219 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
220 for (size_t i = 0; i < stateRecords_.size(); i++) {
221 TelephonyStateRegistryRecord record = stateRecords_[i];
222 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_SIM_STATE) &&
223 (record.slotId_ == slotId) && record.telephonyObserver_ != nullptr) {
224 record.telephonyObserver_->OnSimStateUpdated(slotId, type, state, reason);
225 result = TELEPHONY_SUCCESS;
226 }
227 }
228 SendSimStateChanged(slotId, type, state, reason);
229 return result;
230 }
231
UpdateSignalInfo(int32_t slotId,const std::vector<sptr<SignalInformation>> & vec)232 int32_t TelephonyStateRegistryService::UpdateSignalInfo(int32_t slotId, const std::vector<sptr<SignalInformation>> &vec)
233 {
234 if (!VerifySlotId(slotId)) {
235 TELEPHONY_LOGE("UpdateSignalInfo##VerifySlotId failed ##slotId = %{public}d", slotId);
236 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
237 }
238 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
239 TELEPHONY_LOGE("Check permission failed.");
240 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
241 }
242 std::lock_guard<std::mutex> guard(lock_);
243 signalInfos_[slotId] = vec;
244 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
245 for (size_t i = 0; i < stateRecords_.size(); i++) {
246 TelephonyStateRegistryRecord record = stateRecords_[i];
247 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_SIGNAL_STRENGTHS) &&
248 (record.slotId_ == slotId) && record.telephonyObserver_ != nullptr) {
249 record.telephonyObserver_->OnSignalInfoUpdated(slotId, vec);
250 result = TELEPHONY_SUCCESS;
251 }
252 }
253 SendSignalInfoChanged(slotId, vec);
254 return result;
255 }
256
UpdateCellInfo(int32_t slotId,const std::vector<sptr<CellInformation>> & vec)257 int32_t TelephonyStateRegistryService::UpdateCellInfo(int32_t slotId, const std::vector<sptr<CellInformation>> &vec)
258 {
259 if (!VerifySlotId(slotId)) {
260 TELEPHONY_LOGE("UpdateCellInfo##VerifySlotId failed ##slotId = %{public}d", slotId);
261 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
262 }
263 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE) ||
264 !TelephonyPermission::CheckPermission(Permission::CELL_LOCATION)) {
265 TELEPHONY_LOGE("Check permission failed.");
266 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
267 }
268 std::lock_guard<std::mutex> guard(lock_);
269 cellInfos_[slotId] = vec;
270 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
271 for (size_t i = 0; i < stateRecords_.size(); i++) {
272 TelephonyStateRegistryRecord record = stateRecords_[i];
273 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO) && record.slotId_ == slotId) {
274 if (record.telephonyObserver_ == nullptr) {
275 TELEPHONY_LOGE("record.telephonyObserver_ is nullptr");
276 return TELEPHONY_ERR_LOCAL_PTR_NULL;
277 }
278 record.telephonyObserver_->OnCellInfoUpdated(slotId, vec);
279 result = TELEPHONY_SUCCESS;
280 }
281 }
282 return result;
283 }
284
UpdateNetworkState(int32_t slotId,const sptr<NetworkState> & networkState)285 int32_t TelephonyStateRegistryService::UpdateNetworkState(int32_t slotId, const sptr<NetworkState> &networkState)
286 {
287 if (!VerifySlotId(slotId)) {
288 TELEPHONY_LOGE("UpdateNetworkState##VerifySlotId failed ##slotId = %{public}d", slotId);
289 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
290 }
291 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
292 TELEPHONY_LOGE("Check permission failed.");
293 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
294 }
295 std::lock_guard<std::mutex> guard(lock_);
296 searchNetworkState_[slotId] = networkState;
297 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
298 for (size_t i = 0; i < stateRecords_.size(); i++) {
299 TelephonyStateRegistryRecord r = stateRecords_[i];
300 if (r.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE) && (r.slotId_ == slotId) &&
301 r.telephonyObserver_ != nullptr) {
302 r.telephonyObserver_->OnNetworkStateUpdated(slotId, networkState);
303 result = TELEPHONY_SUCCESS;
304 }
305 }
306 SendNetworkStateChanged(slotId, networkState);
307 TELEPHONY_LOGI("TelephonyStateRegistryService::UpdateNetworkState end");
308 return result;
309 }
310
UpdateCfuIndicator(int32_t slotId,bool cfuResult)311 int32_t TelephonyStateRegistryService::UpdateCfuIndicator(int32_t slotId, bool cfuResult)
312 {
313 if (!VerifySlotId(slotId)) {
314 TELEPHONY_LOGE("UpdateCfuIndicator##VerifySlotId failed ##slotId = %{public}d", slotId);
315 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
316 }
317 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
318 TELEPHONY_LOGE("Check permission failed.");
319 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
320 }
321 std::lock_guard<std::mutex> guard(lock_);
322 cfuResult_[slotId] = cfuResult;
323 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
324 for (size_t i = 0; i < stateRecords_.size(); i++) {
325 TelephonyStateRegistryRecord record = stateRecords_[i];
326 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_CFU_INDICATOR) &&
327 (record.slotId_ == slotId) && record.telephonyObserver_ != nullptr) {
328 record.telephonyObserver_->OnCfuIndicatorUpdated(slotId, cfuResult);
329 result = TELEPHONY_SUCCESS;
330 }
331 }
332 TELEPHONY_LOGI("TelephonyStateRegistryService::UpdateCfuIndicator end");
333 return result;
334 }
335
UpdateIccAccount()336 int32_t TelephonyStateRegistryService::UpdateIccAccount()
337 {
338 std::lock_guard<std::mutex> guard(lock_);
339 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
340 for (size_t i = 0; i < stateRecords_.size(); i++) {
341 TelephonyStateRegistryRecord record = stateRecords_[i];
342 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_ICC_ACCOUNT) &&
343 (record.telephonyObserver_ != nullptr)) {
344 record.telephonyObserver_->OnIccAccountUpdated();
345 result = TELEPHONY_SUCCESS;
346 }
347 }
348 TELEPHONY_LOGI("TelephonyStateRegistryService::UpdateIccAccount end");
349 return result;
350 }
351
UpdateVoiceMailMsgIndicator(int32_t slotId,bool voiceMailMsgResult)352 int32_t TelephonyStateRegistryService::UpdateVoiceMailMsgIndicator(int32_t slotId, bool voiceMailMsgResult)
353 {
354 if (!VerifySlotId(slotId)) {
355 TELEPHONY_LOGE("UpdateVoiceMailMsgIndicator##VerifySlotId failed ##slotId = %{public}d", slotId);
356 return TELEPHONY_STATE_REGISTRY_SLODID_ERROR;
357 }
358 if (!TelephonyPermission::CheckPermission(Permission::SET_TELEPHONY_STATE)) {
359 TELEPHONY_LOGE("Check permission failed.");
360 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
361 }
362 std::lock_guard<std::mutex> guard(lock_);
363 voiceMailMsgResult_[slotId] = voiceMailMsgResult;
364 int32_t result = TELEPHONY_STATE_REGISTRY_DATA_NOT_EXIST;
365 for (size_t i = 0; i < stateRecords_.size(); i++) {
366 TelephonyStateRegistryRecord record = stateRecords_[i];
367 if (record.IsExistStateListener(TelephonyObserverBroker::OBSERVER_MASK_VOICE_MAIL_MSG_INDICATOR) &&
368 (record.slotId_ == slotId) && record.telephonyObserver_ != nullptr) {
369 record.telephonyObserver_->OnVoiceMailMsgIndicatorUpdated(slotId, voiceMailMsgResult);
370 result = TELEPHONY_SUCCESS;
371 }
372 }
373 TELEPHONY_LOGI("TelephonyStateRegistryService::UpdateVoiceMailMsgIndicator end");
374 return result;
375 }
376
CheckCallerIsSystemApp(uint32_t mask)377 bool TelephonyStateRegistryService::CheckCallerIsSystemApp(uint32_t mask)
378 {
379 if ((mask & TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO) != 0) {
380 if (!TelephonyPermission::CheckCallerIsSystemApp()) {
381 TELEPHONY_LOGE("The listening event is cellInfoChange. Non-system applications use system APIs!");
382 return false;
383 }
384 }
385 if ((mask & TelephonyObserverBroker::OBSERVER_MASK_CFU_INDICATOR) != 0) {
386 if (!TelephonyPermission::CheckCallerIsSystemApp()) {
387 TELEPHONY_LOGE("The listening event is cfuIndicatorChange. Non-system applications use system APIs!");
388 return false;
389 }
390 }
391 if ((mask & TelephonyObserverBroker::OBSERVER_MASK_VOICE_MAIL_MSG_INDICATOR) != 0) {
392 if (!TelephonyPermission::CheckCallerIsSystemApp()) {
393 TELEPHONY_LOGE(
394 "The listening event is voiceMailMsgIndicatorChange. Non-system applications use system APIs!");
395 return false;
396 }
397 }
398 return true;
399 }
400
RegisterStateChange(const sptr<TelephonyObserverBroker> & telephonyObserver,int32_t slotId,uint32_t mask,const std::string & bundleName,bool isUpdate,pid_t pid)401 int32_t TelephonyStateRegistryService::RegisterStateChange(
402 const sptr<TelephonyObserverBroker> &telephonyObserver,
403 int32_t slotId, uint32_t mask, const std::string &bundleName, bool isUpdate, pid_t pid)
404 {
405 if (!CheckCallerIsSystemApp(mask)) {
406 return TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
407 }
408 if (!CheckPermission(mask)) {
409 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
410 }
411 std::lock_guard<std::mutex> guard(lock_);
412 bool isExist = false;
413 TelephonyStateRegistryRecord record;
414 for (size_t i = 0; i < stateRecords_.size(); i++) {
415 record = stateRecords_[i];
416 if (record.slotId_ == slotId && record.mask_ == mask && record.pid_ == pid) {
417 isExist = true;
418 break;
419 }
420 }
421
422 if (!isExist) {
423 record.pid_ = pid;
424 record.slotId_ = slotId;
425 record.mask_ = mask;
426 record.bundleName_ = bundleName;
427 record.telephonyObserver_ = telephonyObserver;
428 stateRecords_.push_back(record);
429 }
430
431 if (isUpdate && VerifySlotId(slotId)) {
432 UpdateData(record);
433 }
434 return TELEPHONY_SUCCESS;
435 }
436
UnregisterStateChange(int32_t slotId,uint32_t mask,pid_t pid)437 int32_t TelephonyStateRegistryService::UnregisterStateChange(int32_t slotId, uint32_t mask, pid_t pid)
438 {
439 if (!CheckCallerIsSystemApp(mask)) {
440 return TELEPHONY_ERR_ILLEGAL_USE_OF_SYSTEM_API;
441 }
442 if (!CheckPermission(mask)) {
443 return TELEPHONY_STATE_REGISTRY_PERMISSION_DENIED;
444 }
445 std::lock_guard<std::mutex> guard(lock_);
446 int32_t result = TELEPHONY_STATE_UNREGISTRY_DATA_NOT_EXIST;
447 std::vector<TelephonyStateRegistryRecord>::iterator it;
448 for (it = stateRecords_.begin(); it != stateRecords_.end(); ++it) {
449 if (it->slotId_ == slotId && it->mask_ == mask && it->pid_ == pid) {
450 stateRecords_.erase(it);
451 result = TELEPHONY_SUCCESS;
452 break;
453 }
454 }
455 return result;
456 }
457
CheckPermission(uint32_t mask)458 bool TelephonyStateRegistryService::CheckPermission(uint32_t mask)
459 {
460 if ((mask & TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE) != 0) {
461 if (!TelephonyPermission::CheckPermission(Permission::GET_NETWORK_INFO)) {
462 TELEPHONY_LOGE("Check permission failed,"
463 "you must declare ohos.permission.GET_NETWORK_INFO permission for network state");
464 return false;
465 }
466 }
467 if ((mask & TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO) != 0) {
468 if (!TelephonyPermission::CheckPermission(Permission::CELL_LOCATION)) {
469 TELEPHONY_LOGE("Check permission failed,"
470 "you must declare ohos.permission.LOCATION permission for cell info");
471 return false;
472 }
473 }
474 return true;
475 }
476
VerifySlotId(int slotId)477 bool TelephonyStateRegistryService::VerifySlotId(int slotId)
478 {
479 return slotId >= 0 && slotId < slotSize_;
480 }
481
GetCallIncomingNumberForSlotId(TelephonyStateRegistryRecord record,int32_t slotId)482 std::u16string TelephonyStateRegistryService::GetCallIncomingNumberForSlotId(
483 TelephonyStateRegistryRecord record, int32_t slotId)
484 {
485 if (record.IsCanReadCallHistory()) {
486 return callIncomingNumber_[slotId];
487 } else {
488 return Str8ToStr16("");
489 }
490 }
491
UpdateData(const TelephonyStateRegistryRecord & record)492 void TelephonyStateRegistryService::UpdateData(const TelephonyStateRegistryRecord &record)
493 {
494 if (record.telephonyObserver_ == nullptr) {
495 TELEPHONY_LOGE("record.telephonyObserver_ is nullptr");
496 return;
497 }
498 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_CALL_STATE) != 0) {
499 std::u16string phoneNumber = GetCallIncomingNumberForSlotId(record, record.slotId_);
500 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_CALL_STATE");
501 record.telephonyObserver_->OnCallStateUpdated(record.slotId_, callState_[record.slotId_], phoneNumber);
502 }
503 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_SIGNAL_STRENGTHS) != 0) {
504 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_SIGNAL_STRENGTHS");
505 record.telephonyObserver_->OnSignalInfoUpdated(record.slotId_, signalInfos_[record.slotId_]);
506 }
507 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_NETWORK_STATE) != 0) {
508 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_NETWORK_STATE");
509 record.telephonyObserver_->OnNetworkStateUpdated(record.slotId_, searchNetworkState_[record.slotId_]);
510 }
511 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_CELL_INFO) != 0) {
512 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_CELL_INFO");
513 record.telephonyObserver_->OnCellInfoUpdated(record.slotId_, cellInfos_[record.slotId_]);
514 }
515 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_SIM_STATE) != 0) {
516 record.telephonyObserver_->OnSimStateUpdated(
517 record.slotId_, cardType_[record.slotId_], simState_[record.slotId_], simReason_[record.slotId_]);
518 }
519 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_DATA_CONNECTION_STATE) != 0) {
520 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_DATA_CONNECTION_STATE");
521 record.telephonyObserver_->OnCellularDataConnectStateUpdated(record.slotId_,
522 cellularDataConnectionState_[record.slotId_], cellularDataConnectionNetworkType_[record.slotId_]);
523 }
524 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_DATA_FLOW) != 0) {
525 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_DATA_FLOW");
526 record.telephonyObserver_->OnCellularDataFlowUpdated(record.slotId_, cellularDataFlow_[record.slotId_]);
527 }
528 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_CFU_INDICATOR) != 0) {
529 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_CFU_INDICATOR");
530 record.telephonyObserver_->OnCfuIndicatorUpdated(record.slotId_, cfuResult_[record.slotId_]);
531 }
532 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_VOICE_MAIL_MSG_INDICATOR) != 0) {
533 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_VOICE_MAIL_MSG_INDICATOR");
534 record.telephonyObserver_->OnVoiceMailMsgIndicatorUpdated(record.slotId_, voiceMailMsgResult_[record.slotId_]);
535 }
536 if ((record.mask_ & TelephonyObserverBroker::OBSERVER_MASK_ICC_ACCOUNT) != 0) {
537 TELEPHONY_LOGI("RegisterStateChange##Notify-OBSERVER_MASK_ICC_ACCOUNT");
538 record.telephonyObserver_->OnIccAccountUpdated();
539 }
540 }
541
PublishCommonEvent(const AAFwk::Want & want,int eventCode,const std::string & eventData)542 bool TelephonyStateRegistryService::PublishCommonEvent(
543 const AAFwk::Want &want, int eventCode, const std::string &eventData)
544 {
545 EventFwk::CommonEventData data;
546 data.SetWant(want);
547 data.SetCode(eventCode);
548 data.SetData(eventData);
549 EventFwk::CommonEventPublishInfo publishInfo;
550 publishInfo.SetOrdered(true);
551 bool publishResult = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
552 TELEPHONY_LOGI("PublishCommonEvent end###publishResult = %{public}d\n", publishResult);
553 return publishResult;
554 }
555
SendCallStateChanged(int32_t slotId,int32_t state,const std::u16string & number)556 void TelephonyStateRegistryService::SendCallStateChanged(int32_t slotId, int32_t state, const std::u16string &number)
557 {
558 AAFwk::Want want;
559 want.SetParam("slotId", slotId);
560 want.SetParam("state", state);
561 want.SetParam("number", Str16ToStr8(number));
562 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CALL_STATE_CHANGED);
563
564 EventFwk::CommonEventData data;
565 data.SetWant(want);
566 EventFwk::CommonEventPublishInfo publishInfo;
567 publishInfo.SetOrdered(true);
568 std::vector<std::string> callPermissions;
569 callPermissions.emplace_back(Permission::GET_TELEPHONY_STATE);
570 publishInfo.SetSubscriberPermissions(callPermissions);
571 bool publishResult = EventFwk::CommonEventManager::PublishCommonEvent(data, publishInfo, nullptr);
572 if (!publishResult) {
573 TELEPHONY_LOGE("SendCallStateChanged PublishBroadcastEvent result fail");
574 }
575 }
576
SendCellularDataConnectStateChanged(int32_t slotId,int32_t dataState,int32_t networkType)577 void TelephonyStateRegistryService::SendCellularDataConnectStateChanged(
578 int32_t slotId, int32_t dataState, int32_t networkType)
579 {
580 AAFwk::Want want;
581 want.SetParam("slotId", slotId);
582 want.SetParam("dataState", dataState);
583 want.SetParam("networkType", networkType);
584 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_CELLULAR_DATA_STATE_CHANGED);
585 int32_t eventCode = 1;
586 std::string eventData("connectStateChanged");
587 PublishCommonEvent(want, eventCode, eventData);
588 }
589
SendSimStateChanged(int32_t slotId,CardType type,SimState state,LockReason reason)590 void TelephonyStateRegistryService::SendSimStateChanged(
591 int32_t slotId, CardType type, SimState state, LockReason reason)
592 {
593 AAFwk::Want want;
594 want.SetParam("slotId", slotId);
595 want.SetParam("cardType", static_cast<int32_t>(type));
596 want.SetParam("reason", static_cast<int32_t>(reason));
597 want.SetParam("state", static_cast<int32_t>(state));
598 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SIM_STATE_CHANGED);
599 int32_t eventCode = 1;
600 std::string eventData("simStateChanged");
601 PublishCommonEvent(want, eventCode, eventData);
602 }
603
SendSignalInfoChanged(int32_t slotId,const std::vector<sptr<SignalInformation>> & vec)604 void TelephonyStateRegistryService::SendSignalInfoChanged(
605 int32_t slotId, const std::vector<sptr<SignalInformation>> &vec)
606 {
607 AAFwk::Want want;
608 want.SetParam("slotId", slotId);
609 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_SIGNAL_INFO_CHANGED);
610 std::vector<std::string> contentStr;
611 for (size_t i = 0; i < vec.size(); i++) {
612 sptr<SignalInformation> signal = vec[i];
613 if (signal != nullptr) {
614 contentStr.push_back(signal->ToString());
615 }
616 }
617 want.SetParam("signalInfos", contentStr);
618 int32_t eventCode = 1;
619 std::string eventData("signalInfoChanged");
620 PublishCommonEvent(want, eventCode, eventData);
621 }
622
SendNetworkStateChanged(int32_t slotId,const sptr<NetworkState> & networkState)623 void TelephonyStateRegistryService::SendNetworkStateChanged(int32_t slotId, const sptr<NetworkState> &networkState)
624 {
625 AAFwk::Want want;
626 want.SetParam("slotId", slotId);
627 want.SetAction(EventFwk::CommonEventSupport::COMMON_EVENT_NETWORK_STATE_CHANGED);
628 int32_t eventCode = 1;
629 if (networkState != nullptr) {
630 want.SetParam("networkState", networkState->ToString());
631 }
632 std::string eventData("networkStateChanged");
633 PublishCommonEvent(want, eventCode, eventData);
634 }
635
Dump(std::int32_t fd,const std::vector<std::u16string> & args)636 int TelephonyStateRegistryService::Dump(std::int32_t fd, const std::vector<std::u16string> &args)
637 {
638 if (fd < 0) {
639 TELEPHONY_LOGE("dump fd invalid");
640 return TELEPHONY_ERR_FAIL;
641 }
642 std::vector<std::string> argsInStr;
643 for (const auto &arg : args) {
644 TELEPHONY_LOGI("Dump args: %{public}s", Str16ToStr8(arg).c_str());
645 argsInStr.emplace_back(Str16ToStr8(arg));
646 }
647 std::string result;
648 TelephonyStateRegistryDumpHelper dumpHelper;
649 if (dumpHelper.Dump(argsInStr, stateRecords_, result)) {
650 std::int32_t ret = dprintf(fd, "%s", result.c_str());
651 if (ret < 0) {
652 TELEPHONY_LOGE("dprintf to dump fd failed");
653 return TELEPHONY_ERR_FAIL;
654 }
655 return TELEPHONY_SUCCESS;
656 }
657 TELEPHONY_LOGW("dumpHelper failed");
658 return TELEPHONY_ERR_FAIL;
659 }
660
GetBindStartTime()661 std::string TelephonyStateRegistryService::GetBindStartTime()
662 {
663 std::ostringstream oss;
664 oss << bindStartTime_;
665 return oss.str();
666 }
667
GetBindEndTime()668 std::string TelephonyStateRegistryService::GetBindEndTime()
669 {
670 std::ostringstream oss;
671 oss << bindEndTime_;
672 return oss.str();
673 }
674
GetBindSpendTime()675 std::string TelephonyStateRegistryService::GetBindSpendTime()
676 {
677 std::ostringstream oss;
678 oss << (bindEndTime_ - bindStartTime_);
679 return oss.str();
680 }
681
GetServiceRunningState()682 int32_t TelephonyStateRegistryService::GetServiceRunningState()
683 {
684 return static_cast<int32_t>(state_);
685 }
686
GetSimState(int32_t slotId)687 int32_t TelephonyStateRegistryService::GetSimState(int32_t slotId)
688 {
689 std::map<int32_t, SimState>::iterator it;
690 int32_t result = TELEPHONY_ERROR;
691 for (it = simState_.begin(); it != simState_.end(); ++it) {
692 if (it->first == slotId) {
693 result = static_cast<int32_t>(it->second);
694 TELEPHONY_LOGI("CallState = %{public}d", result);
695 break;
696 }
697 }
698 return result;
699 }
700
GetCallState(int32_t slotId)701 int32_t TelephonyStateRegistryService::GetCallState(int32_t slotId)
702 {
703 std::map<int32_t, int32_t>::iterator it;
704 int32_t result = TELEPHONY_ERROR;
705 for (it = callState_.begin(); it != callState_.end(); ++it) {
706 if (it->first == slotId) {
707 result = it->second;
708 break;
709 }
710 }
711 return result;
712 }
713
GetCardType(int32_t slotId)714 int32_t TelephonyStateRegistryService::GetCardType(int32_t slotId)
715 {
716 std::map<int32_t, CardType>::iterator it;
717 int32_t result = TELEPHONY_ERROR;
718 for (it = cardType_.begin(); it != cardType_.end(); ++it) {
719 if (it->first == slotId) {
720 result = static_cast<int32_t>(it->second);
721 break;
722 }
723 }
724 return result;
725 }
726
GetCellularDataConnectionState(int32_t slotId)727 int32_t TelephonyStateRegistryService::GetCellularDataConnectionState(int32_t slotId)
728 {
729 std::map<int32_t, int32_t>::iterator it;
730 int32_t result = TELEPHONY_ERROR;
731 for (it = cellularDataConnectionState_.begin(); it != cellularDataConnectionState_.end(); ++it) {
732 if (it->first == slotId) {
733 result = it->second;
734 break;
735 }
736 }
737 return result;
738 }
739
GetCellularDataFlow(int32_t slotId)740 int32_t TelephonyStateRegistryService::GetCellularDataFlow(int32_t slotId)
741 {
742 std::map<int32_t, int32_t>::iterator it;
743 int32_t result = TELEPHONY_ERROR;
744 for (it = cellularDataFlow_.begin(); it != cellularDataFlow_.end(); ++it) {
745 if (it->first == slotId) {
746 result = it->second;
747 break;
748 }
749 }
750 return result;
751 }
752
GetCellularDataConnectionNetworkType(int32_t slotId)753 int32_t TelephonyStateRegistryService::GetCellularDataConnectionNetworkType(int32_t slotId)
754 {
755 std::map<int32_t, int32_t>::iterator it;
756 int32_t result = TELEPHONY_ERROR;
757 for (it = cellularDataConnectionNetworkType_.begin(); it != cellularDataConnectionNetworkType_.end(); ++it) {
758 if (it->first == slotId) {
759 result = it->second;
760 break;
761 }
762 }
763 return result;
764 }
765
GetLockReason(int32_t slotId)766 int32_t TelephonyStateRegistryService::GetLockReason(int32_t slotId)
767 {
768 std::map<int32_t, LockReason>::iterator it;
769 int32_t result = TELEPHONY_ERROR;
770 for (it = simReason_.begin(); it != simReason_.end(); ++it) {
771 if (it->first == slotId) {
772 result = static_cast<int32_t>(it->second);
773 break;
774 }
775 }
776 return result;
777 }
778
IsCommonEventServiceAbilityExist()779 bool TelephonyStateRegistryService::IsCommonEventServiceAbilityExist()
780 {
781 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
782 if (sm == nullptr) {
783 TELEPHONY_LOGE("IsCommonEventServiceAbilityExist Get ISystemAbilityManager failed, no SystemAbilityManager");
784 return false;
785 }
786 sptr<IRemoteObject> remote = sm->CheckSystemAbility(COMMON_EVENT_SERVICE_ID);
787 if (remote == nullptr) {
788 TELEPHONY_LOGE("No CesServiceAbility");
789 return false;
790 }
791 return true;
792 }
793 } // namespace Telephony
794 } // namespace OHOS
795