1 /*
2 * Copyright (c) 2022 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 "devicestatus_client.h"
17
18 #include <if_system_ability_manager.h>
19 #include <iservice_registry.h>
20 #include <system_ability_definition.h>
21
22 #include "devicestatus_common.h"
23
24 #include "iremote_broker.h"
25 #include "iremote_object.h"
26
27 namespace OHOS {
28 namespace Msdp {
DevicestatusClient()29 DevicestatusClient::DevicestatusClient() {}
~DevicestatusClient()30 DevicestatusClient::~DevicestatusClient()
31 {
32 if (devicestatusProxy_ != nullptr) {
33 auto remoteObject = devicestatusProxy_->AsObject();
34 if (remoteObject != nullptr) {
35 remoteObject->RemoveDeathRecipient(deathRecipient_);
36 }
37 }
38 }
39
Connect()40 ErrCode DevicestatusClient::Connect()
41 {
42 std::lock_guard<std::mutex> lock(mutex_);
43 if (devicestatusProxy_ != nullptr) {
44 DEV_HILOGE(INNERKIT, "devicestatusProxy_ is nut nullptr");
45 return ERR_OK;
46 }
47
48 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
49 if (sam == nullptr) {
50 DEV_HILOGE(INNERKIT, "GetSystemAbilityManager failed");
51 return E_DEVICESTATUS_GET_SYSTEM_ABILITY_MANAGER_FAILED;
52 }
53
54 sptr<IRemoteObject> remoteObject_ = sam->CheckSystemAbility(MSDP_DEVICESTATUS_SERVICE_ID);
55 if (remoteObject_ == nullptr) {
56 DEV_HILOGE(INNERKIT, "CheckSystemAbility failed");
57 return E_DEVICESTATUS_GET_SERVICE_FAILED;
58 }
59
60 deathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new DevicestatusDeathRecipient());
61 if (deathRecipient_ == nullptr) {
62 DEV_HILOGE(INNERKIT, "Failed to create DevicestatusDeathRecipient");
63 return ERR_NO_MEMORY;
64 }
65
66 if ((remoteObject_->IsProxyObject()) && (!remoteObject_->AddDeathRecipient(deathRecipient_))) {
67 DEV_HILOGE(INNERKIT, "Add death recipient to Devicestatus service failed");
68 return E_DEVICESTATUS_ADD_DEATH_RECIPIENT_FAILED;
69 }
70
71 devicestatusProxy_ = iface_cast<Idevicestatus>(remoteObject_);
72 DEV_HILOGD(INNERKIT, "Connecting DevicestatusService success");
73 return ERR_OK;
74 }
75
ResetProxy(const wptr<IRemoteObject> & remote)76 void DevicestatusClient::ResetProxy(const wptr<IRemoteObject>& remote)
77 {
78 std::lock_guard<std::mutex> lock(mutex_);
79 DEVICESTATUS_RETURN_IF(devicestatusProxy_ == nullptr);
80
81 auto serviceRemote = devicestatusProxy_->AsObject();
82 if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
83 serviceRemote->RemoveDeathRecipient(deathRecipient_);
84 devicestatusProxy_ = nullptr;
85 }
86 }
87
OnRemoteDied(const wptr<IRemoteObject> & remote)88 void DevicestatusClient::DevicestatusDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
89 {
90 if (remote == nullptr) {
91 DEV_HILOGE(INNERKIT, "OnRemoteDied failed, remote is nullptr");
92 return;
93 }
94
95 DevicestatusClient::GetInstance().ResetProxy(remote);
96 DEV_HILOGD(INNERKIT, "Recv death notice");
97 }
98
SubscribeCallback(const DevicestatusDataUtils::DevicestatusType & type,const sptr<IdevicestatusCallback> & callback)99 void DevicestatusClient::SubscribeCallback(const DevicestatusDataUtils::DevicestatusType& type, \
100 const sptr<IdevicestatusCallback>& callback)
101 {
102 DEV_HILOGD(INNERKIT, "Enter");
103 DEVICESTATUS_RETURN_IF((callback == nullptr) || (Connect() != ERR_OK));
104 if (devicestatusProxy_ == nullptr) {
105 DEV_HILOGE(SERVICE, "devicestatusProxy_ is nullptr");
106 return;
107 }
108 if (type > DevicestatusDataUtils::DevicestatusType::TYPE_INVALID
109 && type <= DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN) {
110 devicestatusProxy_->Subscribe(type, callback);
111 }
112 return;
113 DEV_HILOGD(INNERKIT, "Exit");
114 }
115
UnSubscribeCallback(const DevicestatusDataUtils::DevicestatusType & type,const sptr<IdevicestatusCallback> & callback)116 void DevicestatusClient::UnSubscribeCallback(const DevicestatusDataUtils::DevicestatusType& type, \
117 const sptr<IdevicestatusCallback>& callback)
118 {
119 DEV_HILOGD(INNERKIT, "Enter");
120 DEVICESTATUS_RETURN_IF((callback == nullptr) || (Connect() != ERR_OK));
121 if (devicestatusProxy_ == nullptr) {
122 DEV_HILOGE(SERVICE, "devicestatusProxy_ is nullptr");
123 return;
124 }
125 if (type > DevicestatusDataUtils::DevicestatusType::TYPE_INVALID
126 && type <= DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN) {
127 devicestatusProxy_->UnSubscribe(type, callback);
128 }
129 return;
130 DEV_HILOGD(INNERKIT, "Exit");
131 }
132
GetDevicestatusData(const DevicestatusDataUtils::DevicestatusType & type)133 DevicestatusDataUtils::DevicestatusData DevicestatusClient::GetDevicestatusData(const \
134 DevicestatusDataUtils::DevicestatusType& type)
135 {
136 DEV_HILOGD(INNERKIT, "Enter");
137 DevicestatusDataUtils::DevicestatusData devicestatusData;
138 devicestatusData.type = DevicestatusDataUtils::DevicestatusType::TYPE_INVALID;
139 devicestatusData.value = DevicestatusDataUtils::DevicestatusValue::VALUE_INVALID;
140
141 DEVICESTATUS_RETURN_IF_WITH_RET((Connect() != ERR_OK), devicestatusData);
142 if (devicestatusProxy_ == nullptr) {
143 DEV_HILOGE(SERVICE, "devicestatusProxy_ is nullptr");
144 return devicestatusData;
145 }
146 if (type > DevicestatusDataUtils::DevicestatusType::TYPE_INVALID
147 && type <= DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN) {
148 devicestatusData = devicestatusProxy_->GetCache(type);
149 }
150 DEV_HILOGD(INNERKIT, "Exit");
151 return devicestatusData;
152 }
153 } // namespace Msdp
154 } // namespace OHOS
155