1 /*
2 * Copyright (c) 2022-2023 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_define.h"
19 #include "fi_log.h"
20 #include "stationary_callback.h"
21 #include "stationary_manager.h"
22
23 namespace OHOS {
24 namespace Msdp {
25 namespace DeviceStatus {
26 namespace {
27 constexpr ::OHOS::HiviewDFX::HiLogLabel LABEL { LOG_CORE, MSDP_DOMAIN_ID, "DeviceStatusAgent" };
28 } // namespace
29
OnDeviceStatusChanged(const Data & devicestatusData)30 void DeviceStatusAgent::DeviceStatusAgentCallback::OnDeviceStatusChanged(
31 const Data& devicestatusData)
32 {
33 FI_HILOGI("type:%{public}d, value:%{public}d", static_cast<Type>(devicestatusData.type),
34 static_cast<OnChangedValue>(devicestatusData.value));
35 std::shared_ptr<DeviceStatusAgent> agent = agent_.lock();
36 CHKPV(agent);
37 agent->agentEvent_->OnEventResult(devicestatusData);
38 }
39
SubscribeAgentEvent(Type type,ActivityEvent event,ReportLatencyNs latency,std::shared_ptr<DeviceStatusAgent::DeviceStatusAgentEvent> agentEvent)40 int32_t DeviceStatusAgent::SubscribeAgentEvent(Type type,
41 ActivityEvent event,
42 ReportLatencyNs latency,
43 std::shared_ptr<DeviceStatusAgent::DeviceStatusAgentEvent> agentEvent)
44 {
45 CALL_DEBUG_ENTER;
46 CHKPR(agentEvent, ERR_INVALID_VALUE);
47 if (!(type > Type::TYPE_INVALID && type <= Type::TYPE_LID_OPEN) ||
48 !(event > ActivityEvent::EVENT_INVALID && event <= ActivityEvent::ENTER_EXIT)) {
49 FI_HILOGE("Subscription agent event failed");
50 return ERR_INVALID_VALUE;
51 }
52 RegisterServiceEvent(type, event, latency);
53 agentEvent_ = agentEvent;
54 return RET_OK;
55 }
56
UnsubscribeAgentEvent(Type type,ActivityEvent event)57 int32_t DeviceStatusAgent::UnsubscribeAgentEvent(Type type, ActivityEvent event)
58 {
59 if (type > Type::TYPE_INVALID && type <= Type::TYPE_LID_OPEN && event > ActivityEvent::EVENT_INVALID
60 && event <= ActivityEvent::ENTER_EXIT) {
61 UnRegisterServiceEvent(type, event);
62 return RET_OK;
63 }
64 FI_HILOGE("Unsubscription agent event failed");
65 return ERR_INVALID_VALUE;
66 }
67
RegisterServiceEvent(Type type,ActivityEvent event,ReportLatencyNs latency)68 void DeviceStatusAgent::RegisterServiceEvent(Type type, ActivityEvent event, ReportLatencyNs latency)
69 {
70 CALL_DEBUG_ENTER;
71 callback_ = new (std::nothrow) DeviceStatusAgentCallback(shared_from_this());
72 CHKPV(callback_);
73 StationaryManager::GetInstance()->SubscribeCallback(type, event, latency, callback_);
74 }
75
UnRegisterServiceEvent(Type type,ActivityEvent event)76 void DeviceStatusAgent::UnRegisterServiceEvent(Type type, ActivityEvent event)
77 {
78 CALL_DEBUG_ENTER;
79 StationaryManager::GetInstance()->UnsubscribeCallback(type, event, callback_);
80 }
81 } // namespace DeviceStatus
82 } // namespace Msdp
83 } // namespace OHOS
84