1 /* 2 * Copyright (c) 2024 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 STATIONARY_SERVER_H 17 #define STATIONARY_SERVER_H 18 19 #include "nocopyable.h" 20 21 #include "devicestatus_manager.h" 22 #ifdef MOTION_ENABLE 23 #include "motion_agent.h" 24 #include "motion_callback_stub.h" 25 #endif 26 #include "i_plugin.h" 27 28 namespace OHOS { 29 namespace Msdp { 30 namespace DeviceStatus { 31 using DeviceStatusMotionEvent = std::function<void(const Data &)>; 32 using StationaryServerEvent = std::function<void(const wptr<IRemoteObject> &)>; 33 #ifdef MOTION_ENABLE 34 class MotionCallback : public MotionCallbackStub { 35 public: MotionCallback(DeviceStatusMotionEvent event)36 MotionCallback(DeviceStatusMotionEvent event) : event_(event) {} 37 virtual ~MotionCallback() = default; 38 void OnMotionChanged(const MotionEvent &motionEvent) override; 39 private: 40 DeviceStatusMotionEvent event_; 41 }; 42 class RemoteDevStaCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 43 public: RemoteDevStaCallbackDeathRecipient(StationaryServerEvent event)44 explicit RemoteDevStaCallbackDeathRecipient(StationaryServerEvent event) : event_(event) {} 45 ~RemoteDevStaCallbackDeathRecipient() = default; 46 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 47 private: 48 StationaryServerEvent event_; 49 }; 50 struct DevStaCallbackCmp { operatorDevStaCallbackCmp51 bool operator() (const sptr<IRemoteDevStaCallback> lhs, const sptr<IRemoteDevStaCallback> rhs) 52 { 53 return lhs->AsObject() < rhs->AsObject(); 54 } 55 }; 56 #endif 57 class StationaryServer final : public IPlugin { 58 public: 59 StationaryServer(); 60 ~StationaryServer() = default; 61 DISALLOW_COPY_AND_MOVE(StationaryServer); 62 63 int32_t Enable(CallingContext &context, MessageParcel &data, MessageParcel &reply) override; 64 int32_t Disable(CallingContext &context, MessageParcel &data, MessageParcel &reply) override; 65 int32_t Start(CallingContext &context, MessageParcel &data, MessageParcel &reply) override; 66 int32_t Stop(CallingContext &context, MessageParcel &data, MessageParcel &reply) override; 67 int32_t AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply) override; 68 int32_t RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply) override; 69 int32_t SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply) override; 70 int32_t GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply) override; 71 int32_t Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply) override; 72 73 void DumpDeviceStatusSubscriber(int32_t fd) const; 74 void DumpDeviceStatusChanges(int32_t fd) const; 75 void DumpCurrentDeviceStatus(int32_t fd); 76 77 private: 78 void Subscribe(CallingContext &context, Type type, ActivityEvent event, 79 ReportLatencyNs latency, sptr<IRemoteDevStaCallback> callback); 80 void Unsubscribe(CallingContext &context, Type type, ActivityEvent event, 81 sptr<IRemoteDevStaCallback> callback); 82 #ifdef MOTION_ENABLE 83 int32_t SubscribeMotion(Type type, sptr<IRemoteDevStaCallback> callback); 84 int32_t UnsubscribeMotion(Type type, sptr<IRemoteDevStaCallback> callback); 85 void NotifyMotionCallback(Type type, const Data &data); 86 void StationaryServerDeathRecipient(const wptr<IRemoteObject> &remote); 87 #endif 88 Data GetCache(CallingContext &context, const Type &type); 89 void ReportSensorSysEvent(CallingContext &context, int32_t type, bool enable); 90 91 #ifdef MOTION_ENABLE 92 std::map<Type, std::set<sptr<IRemoteDevStaCallback>, DevStaCallbackCmp>> deviceStatusMotionCallbacks_; 93 std::map<Type, Data> cacheData_; 94 sptr<IMotionCallback> motionCallback_ { nullptr }; 95 std::mutex mtx_; 96 sptr<IRemoteObject::DeathRecipient> devStaCBDeathRecipient_ { nullptr }; 97 #endif 98 DeviceStatusManager manager_; 99 }; 100 } // namespace DeviceStatus 101 } // namespace Msdp 102 } // namespace OHOS 103 #endif // STATIONARY_SERVER_H