• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "idevicestatus_callback.h"
22 
23 namespace OHOS {
24 namespace Msdp {
OnDevicestatusChanged(const DevicestatusDataUtils::DevicestatusData & devicestatusData)25 void DeviceStatusAgent::DeviceStatusAgentCallback::OnDevicestatusChanged(
26     const DevicestatusDataUtils::DevicestatusData& devicestatusData)
27 {
28     DEV_HILOGI(INNERKIT, "type=%{punlic}d, value=%{public}d",
29         static_cast<DevicestatusDataUtils::DevicestatusType>(devicestatusData.type),
30         static_cast<DevicestatusDataUtils::DevicestatusValue>(devicestatusData.value));
31     std::shared_ptr<DeviceStatusAgent> agent = agent_.lock();
32     if (agent == nullptr) {
33         DEV_HILOGE(SERVICE, "agent is nullptr");
34         return;
35     }
36     agent->agentEvent_->OnEventResult(devicestatusData);
37 }
38 
SubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType & type,const std::shared_ptr<DeviceStatusAgent::DeviceStatusAgentEvent> & agentEvent)39 int32_t DeviceStatusAgent::SubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType& type,
40     const std::shared_ptr<DeviceStatusAgent::DeviceStatusAgentEvent>& agentEvent)
41 {
42     DEV_HILOGI(INNERKIT, "Enter");
43 
44     if (agentEvent == nullptr) {
45         return ERR_INVALID_VALUE;
46     }
47     if (type > DevicestatusDataUtils::DevicestatusType::TYPE_INVALID
48         && type <= DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN) {
49         RegisterServiceEvent(type);
50         agentEvent_ = agentEvent;
51     } else {
52         return ERR_INVALID_VALUE;
53     }
54     return ERR_OK;
55 }
56 
UnSubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType & type)57 int32_t DeviceStatusAgent::UnSubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType& type)
58 {
59     if (type > DevicestatusDataUtils::DevicestatusType::TYPE_INVALID
60         && type <= DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN) {
61         UnRegisterServiceEvent(type);
62         return ERR_OK;
63     }
64     return ERR_INVALID_VALUE;
65 }
66 
RegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType & type)67 void DeviceStatusAgent::RegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType& type)
68 {
69     DEV_HILOGI(INNERKIT, "Enter");
70     callback_ = new DeviceStatusAgentCallback(shared_from_this());
71     DevicestatusClient::GetInstance().SubscribeCallback(type, callback_);
72 }
73 
UnRegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType & type)74 void DeviceStatusAgent::UnRegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType& type)
75 {
76     DEV_HILOGI(INNERKIT, "Enter");
77     DevicestatusClient::GetInstance().UnSubscribeCallback(type, callback_);
78 }
79 } // namespace Msdp
80 } // namespace OHOS