• 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_observer_client.h"
17 
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21 
22 #include "telephony_log_wrapper.h"
23 #include "state_registry_errors.h"
24 
25 namespace OHOS {
26 namespace Telephony {
27 TelephonyObserverClient::TelephonyObserverClient() = default;
28 TelephonyObserverClient::~TelephonyObserverClient() = default;
29 
GetProxy()30 sptr<ITelephonyStateNotify> TelephonyObserverClient::GetProxy()
31 {
32     std::lock_guard<std::mutex> lock(mutexProxy_);
33     if (proxy_ != nullptr) {
34         return proxy_;
35     }
36 
37     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
38     if (sam == nullptr) {
39         TELEPHONY_LOGE("Failed to get system ability manager");
40         return nullptr;
41     }
42     sptr<IRemoteObject> obj = sam->CheckSystemAbility(TELEPHONY_STATE_REGISTRY_SYS_ABILITY_ID);
43     if (obj == nullptr) {
44         TELEPHONY_LOGE("Failed to get state registry service");
45         return nullptr;
46     }
47     std::unique_ptr<StateRegistryDeathRecipient> recipient = std::make_unique<StateRegistryDeathRecipient>(*this);
48     if (recipient == nullptr) {
49         TELEPHONY_LOGE("recipient is null");
50         return nullptr;
51     }
52     sptr<IRemoteObject::DeathRecipient> dr(recipient.release());
53     if ((obj->IsProxyObject()) && (!obj->AddDeathRecipient(dr))) {
54         TELEPHONY_LOGE("Failed to add death recipient");
55         return nullptr;
56     }
57     proxy_ = iface_cast<ITelephonyStateNotify>(obj);
58     deathRecipient_ = dr;
59     TELEPHONY_LOGI("Succeed to connect state registry service %{public}d", proxy_ == nullptr);
60     return proxy_;
61 }
62 
OnRemoteDied(const wptr<IRemoteObject> & remote)63 void TelephonyObserverClient::OnRemoteDied(const wptr<IRemoteObject> &remote)
64 {
65     if (remote == nullptr) {
66         TELEPHONY_LOGE("OnRemoteDied failed, remote is nullptr");
67         return;
68     }
69     std::lock_guard<std::mutex> lock(mutexProxy_);
70     if (proxy_ == nullptr) {
71         TELEPHONY_LOGE("OnRemoteDied proxy_ is nullptr");
72         return;
73     }
74     auto serviceRemote = proxy_->AsObject();
75     if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
76         serviceRemote->RemoveDeathRecipient(deathRecipient_);
77         proxy_ = nullptr;
78         TELEPHONY_LOGE("on remote died");
79     }
80 }
81 
AddStateObserver(const sptr<TelephonyObserverBroker> & telephonyObserver,int32_t slotId,uint32_t mask,bool isUpdate)82 int32_t TelephonyObserverClient::AddStateObserver(const sptr<TelephonyObserverBroker> &telephonyObserver,
83     int32_t slotId, uint32_t mask, bool isUpdate)
84 {
85     auto proxy = GetProxy();
86     if (proxy == nullptr) {
87         TELEPHONY_LOGE("proxy is null!");
88         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
89     }
90     return proxy->RegisterStateChange(telephonyObserver, slotId, mask, isUpdate);
91 }
92 
RemoveStateObserver(int32_t slotId,uint32_t mask)93 int32_t TelephonyObserverClient::RemoveStateObserver(int32_t slotId, uint32_t mask)
94 {
95     auto proxy = GetProxy();
96     if (proxy == nullptr) {
97         TELEPHONY_LOGE("proxy is null!");
98         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
99     }
100     return proxy->UnregisterStateChange(slotId, mask);
101 }
102 }
103 }
104 
105