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