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_agent.h"
17
18 #include "devicestatus_common.h"
19 #include "devicestatus_client.h"
20
21 namespace OHOS {
22 namespace Msdp {
OnDevicestatusChanged(const DevicestatusDataUtils::DevicestatusData & devicestatusData)23 void DeviceStatusAgent::DeviceStatusAgentCallback::OnDevicestatusChanged(
24 const DevicestatusDataUtils::DevicestatusData& devicestatusData)
25 {
26 DEV_HILOGI(INNERKIT, "type = %{punlic}d, value=%{public}d",
27 static_cast<DevicestatusDataUtils::DevicestatusType>(devicestatusData.type),
28 static_cast<DevicestatusDataUtils::DevicestatusValue>(devicestatusData.value));
29 std::shared_ptr<DeviceStatusAgent> agent = agent_.lock();
30 agent->agentEvent_->OnEventResult(devicestatusData);
31 }
32
SubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType & type,const std::shared_ptr<DeviceStatusAgent::DeviceStatusAgentEvent> & agentEvent)33 int32_t DeviceStatusAgent::SubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType& type,
34 const std::shared_ptr<DeviceStatusAgent::DeviceStatusAgentEvent>& agentEvent)
35 {
36 DEV_HILOGI(INNERKIT, "Enter");
37 RegisterServiceEvent(type);
38 if (agentEvent == nullptr) {
39 return ERR_INVALID_VALUE;
40 }
41 agentEvent_ = agentEvent;
42 return ERR_OK;
43 }
44
UnSubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType & type)45 int32_t DeviceStatusAgent::UnSubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType& type)
46 {
47 UnRegisterServiceEvent(type);
48 return ERR_OK;
49 }
50
RegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType & type)51 void DeviceStatusAgent::RegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType& type)
52 {
53 DEV_HILOGI(INNERKIT, "Enter");
54 callback_ = new DeviceStatusAgentCallback(shared_from_this());
55 DevicestatusClient::GetInstance().SubscribeCallback(type, callback_);
56 }
57
UnRegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType & type)58 void DeviceStatusAgent::UnRegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType& type)
59 {
60 DEV_HILOGI(INNERKIT, "Enter");
61 DevicestatusClient::GetInstance().UnSubscribeCallback(type, callback_);
62 }
63 } // namespace Msdp
64 } // namespace OHOS