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#undef LOG_TAG
24#define LOG_TAG "DeviceStatusAgent"
25
26namespace OHOS {
27namespace Msdp {
28namespace DeviceStatus {
29
30void 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    CHKPV(agent->agentEvent_);
38    agent->agentEvent_->OnEventResult(devicestatusData);
39}
40
41int32_t DeviceStatusAgent::SubscribeAgentEvent(Type type,
42    ActivityEvent event,
43    ReportLatencyNs latency,
44    std::shared_ptr<DeviceStatusAgent::DeviceStatusAgentEvent> agentEvent)
45{
46    CALL_DEBUG_ENTER;
47    CHKPR(agentEvent, ERR_INVALID_VALUE);
48    if (!(type > Type::TYPE_INVALID && type <= Type::TYPE_LID_OPEN) ||
49        !(event > ActivityEvent::EVENT_INVALID && event <= ActivityEvent::ENTER_EXIT)) {
50        FI_HILOGE("Subscription agent event failed");
51        return ERR_INVALID_VALUE;
52    }
53    RegisterServiceEvent(type, event, latency);
54    agentEvent_ = agentEvent;
55    return RET_OK;
56}
57
58int32_t DeviceStatusAgent::UnsubscribeAgentEvent(Type type, ActivityEvent event)
59{
60    if (type > Type::TYPE_INVALID && type <= Type::TYPE_LID_OPEN && event > ActivityEvent::EVENT_INVALID
61        && event <= ActivityEvent::ENTER_EXIT) {
62        UnRegisterServiceEvent(type, event);
63        return RET_OK;
64    }
65    FI_HILOGE("Unsubscription agent event failed");
66    return ERR_INVALID_VALUE;
67}
68
69void DeviceStatusAgent::RegisterServiceEvent(Type type, ActivityEvent event, ReportLatencyNs latency)
70{
71    CALL_DEBUG_ENTER;
72    callback_ = new (std::nothrow) DeviceStatusAgentCallback(shared_from_this());
73    CHKPV(callback_);
74    StationaryManager::GetInstance()->SubscribeCallback(type, event, latency, callback_);
75}
76
77void DeviceStatusAgent::UnRegisterServiceEvent(Type type, ActivityEvent event)
78{
79    CALL_DEBUG_ENTER;
80    StationaryManager::GetInstance()->UnsubscribeCallback(type, event, callback_);
81}
82} // namespace DeviceStatus
83} // namespace Msdp
84} // namespace OHOS
85