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 #ifndef OHOS_MSDP_DEVICESTATUS_AGENT_H 17 #define OHOS_MSDP_DEVICESTATUS_AGENT_H 18 19 #include <memory> 20 21 #include "devicestatus_callback_stub.h" 22 #include "devicestatus_data_utils.h" 23 24 namespace OHOS { 25 namespace Msdp { 26 class DeviceStatusAgent : public std::enable_shared_from_this<DeviceStatusAgent> { 27 public: DeviceStatusAgent()28 DeviceStatusAgent() {}; ~DeviceStatusAgent()29 ~DeviceStatusAgent() {}; 30 class DeviceStatusAgentEvent { 31 public: 32 virtual ~DeviceStatusAgentEvent() = default; 33 virtual bool OnEventResult(const DevicestatusDataUtils::DevicestatusData& devicestatusData) = 0; 34 }; 35 36 class DeviceStatusAgentCallback : public DevicestatusCallbackStub { 37 public: DeviceStatusAgentCallback(std::shared_ptr<DeviceStatusAgent> agent)38 explicit DeviceStatusAgentCallback(std::shared_ptr<DeviceStatusAgent> agent) : agent_(agent) {}; ~DeviceStatusAgentCallback()39 virtual ~DeviceStatusAgentCallback() {}; 40 void OnDevicestatusChanged(const DevicestatusDataUtils::DevicestatusData& devicestatusData) override; 41 private: 42 std::weak_ptr<DeviceStatusAgent> agent_; 43 }; 44 45 int32_t SubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType& type, 46 const std::shared_ptr<DeviceStatusAgent::DeviceStatusAgentEvent>& agentEvent); 47 int32_t UnSubscribeAgentEvent(const DevicestatusDataUtils::DevicestatusType& type); 48 friend class DeviceStatusAgentCallback; 49 private: 50 void RegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType& type); 51 void UnRegisterServiceEvent(const DevicestatusDataUtils::DevicestatusType& type); 52 sptr<IdevicestatusCallback> callback_; 53 std::shared_ptr<DeviceStatusAgentEvent> agentEvent_; 54 }; 55 } // namespace Msdp 56 } // namespace OHOS 57 #endif // OHOS_MSDP_DEVICESTATUS_AGENT_H