• 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_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 TelephonyStateRegistryClient::TelephonyStateRegistryClient() = default;
28 TelephonyStateRegistryClient::~TelephonyStateRegistryClient() = default;
29 
GetProxy()30 sptr<ITelephonyStateNotify> TelephonyStateRegistryClient::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 TelephonyStateRegistryClient::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 
UpdateCellularDataConnectState(int32_t slotId,int32_t dataState,int32_t networkState)82 int32_t TelephonyStateRegistryClient::UpdateCellularDataConnectState(
83     int32_t slotId, int32_t dataState, int32_t networkState)
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->UpdateCellularDataConnectState(slotId, dataState, networkState);
91 }
92 
UpdateCellularDataFlow(int32_t slotId,int32_t dataFlowType)93 int32_t TelephonyStateRegistryClient::UpdateCellularDataFlow(int32_t slotId, int32_t dataFlowType)
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->UpdateCellularDataFlow(slotId, dataFlowType);
101 }
102 
UpdateCallState(int32_t slotId,int32_t callStatus,const std::u16string & number)103 int32_t TelephonyStateRegistryClient::UpdateCallState(
104     int32_t slotId, int32_t callStatus, const std::u16string &number)
105 {
106     auto proxy = GetProxy();
107     if (proxy == nullptr) {
108         TELEPHONY_LOGE("proxy is null!");
109         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
110     }
111     return proxy->UpdateCallState(slotId, callStatus, number);
112 }
113 
UpdateCallStateForSlotId(int32_t slotId,int32_t callId,int32_t callStatus,const std::u16string & number)114 int32_t TelephonyStateRegistryClient::UpdateCallStateForSlotId(
115     int32_t slotId, int32_t callId, int32_t callStatus, const std::u16string &number)
116 {
117     auto proxy = GetProxy();
118     if (proxy == nullptr) {
119         TELEPHONY_LOGE("proxy is null!");
120         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
121     }
122     return proxy->UpdateCallStateForSlotId(slotId, callId, callStatus, number);
123 }
124 
UpdateSignalInfo(int32_t slotId,const std::vector<sptr<SignalInformation>> & vec)125 int32_t TelephonyStateRegistryClient::UpdateSignalInfo(
126     int32_t slotId, const std::vector<sptr<SignalInformation>> &vec)
127 {
128     auto proxy = GetProxy();
129     if (proxy == nullptr) {
130         TELEPHONY_LOGE("proxy is null!");
131         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
132     }
133     return proxy->UpdateSignalInfo(slotId, vec);
134 }
135 
UpdateCellInfo(int32_t slotId,const std::vector<sptr<CellInformation>> & vec)136 int32_t TelephonyStateRegistryClient::UpdateCellInfo(
137     int32_t slotId, const std::vector<sptr<CellInformation>> &vec)
138 {
139     auto proxy = GetProxy();
140     if (proxy == nullptr) {
141         TELEPHONY_LOGE("proxy is null!");
142         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
143     }
144     return proxy->UpdateCellInfo(slotId, vec);
145 }
146 
UpdateNetworkState(int32_t slotId,const sptr<NetworkState> & networkState)147 int32_t TelephonyStateRegistryClient::UpdateNetworkState(
148     int32_t slotId, const sptr<NetworkState> &networkState)
149 {
150     auto proxy = GetProxy();
151     if (proxy == nullptr) {
152         TELEPHONY_LOGE("proxy is null!");
153         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
154     }
155     return proxy->UpdateNetworkState(slotId, networkState);
156 }
157 
UpdateSimState(int32_t slotId,CardType type,SimState state,LockReason reason)158 int32_t TelephonyStateRegistryClient::UpdateSimState(
159     int32_t slotId, CardType type, SimState state, LockReason reason)
160 {
161     auto proxy = GetProxy();
162     if (proxy == nullptr) {
163         TELEPHONY_LOGE("proxy is null!");
164         return TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL;
165     }
166     return proxy->UpdateSimState(slotId, type, state, reason);
167 }
168 }
169 }
170 
171