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 <optional> 20 21 #include "nocopyable.h" 22 23 #include "devicestatus_manager.h" 24 #ifdef MOTION_ENABLE 25 #include "motion_agent.h" 26 #include "motion_callback_stub.h" 27 #endif 28 #include "i_plugin.h" 29 30 #ifdef DEVICE_STATUS_SENSOR_ENABLE 31 #include "sensor_agent.h" 32 #include "sensor_agent_type.h" 33 #endif 34 35 namespace OHOS { 36 namespace Msdp { 37 namespace DeviceStatus { 38 using DeviceStatusMotionEvent = std::function<void(const Data &)>; 39 using StationaryServerEvent = std::function<void(const wptr<IRemoteObject> &)>; 40 #ifdef MOTION_ENABLE 41 class MotionCallback : public MotionCallbackStub { 42 public: MotionCallback(DeviceStatusMotionEvent event)43 MotionCallback(DeviceStatusMotionEvent event) : event_(event) {} 44 virtual ~MotionCallback() = default; 45 void OnMotionChanged(const MotionEvent &motionEvent) override; 46 private: 47 DeviceStatusMotionEvent event_; 48 }; 49 class RemoteDevStaCallbackDeathRecipient : public IRemoteObject::DeathRecipient { 50 public: RemoteDevStaCallbackDeathRecipient(StationaryServerEvent event)51 explicit RemoteDevStaCallbackDeathRecipient(StationaryServerEvent event) : event_(event) {} 52 ~RemoteDevStaCallbackDeathRecipient() = default; 53 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 54 private: 55 StationaryServerEvent event_; 56 }; 57 struct DevStaCallbackCmp { operatorDevStaCallbackCmp58 bool operator() (const sptr<IRemoteDevStaCallback> lhs, const sptr<IRemoteDevStaCallback> rhs) 59 { 60 return lhs->AsObject() < rhs->AsObject(); 61 } 62 }; 63 #endif 64 class StationaryServer { 65 public: 66 StationaryServer(); 67 ~StationaryServer() = default; 68 DISALLOW_COPY_AND_MOVE(StationaryServer); 69 70 int32_t SubscribeStationaryCallback(CallingContext &context, int32_t type, int32_t event, 71 int32_t latency, const sptr<IRemoteDevStaCallback> &subCallback); 72 int32_t UnsubscribeStationaryCallback(CallingContext &context, int32_t type, int32_t event, 73 const sptr<IRemoteDevStaCallback> &unsubCallback); 74 int32_t GetDeviceStatusData(CallingContext &context, int32_t type, int32_t &replyType, int32_t &replyValue); 75 int32_t GetDevicePostureDataSync(CallingContext &context, DevicePostureData &data); 76 77 void DumpDeviceStatusSubscriber(int32_t fd) const; 78 void DumpDeviceStatusChanges(int32_t fd) const; 79 void DumpCurrentDeviceStatus(int32_t fd); 80 81 private: 82 void Subscribe(CallingContext &context, Type type, ActivityEvent event, 83 ReportLatencyNs latency, sptr<IRemoteDevStaCallback> callback); 84 void Unsubscribe(CallingContext &context, Type type, ActivityEvent event, 85 sptr<IRemoteDevStaCallback> callback); 86 bool IsSystemCalling(CallingContext &context); 87 bool IsSystemServiceCalling(CallingContext &context); 88 #ifdef MOTION_ENABLE 89 int32_t SubscribeMotion(Type type, sptr<IRemoteDevStaCallback> callback); 90 int32_t UnsubscribeMotion(Type type, sptr<IRemoteDevStaCallback> callback); 91 void NotifyMotionCallback(Type type, const Data &data); 92 void StationaryServerDeathRecipient(const wptr<IRemoteObject> &remote); 93 #endif 94 Data GetCache(CallingContext &context, const Type &type); 95 void ReportSensorSysEvent(CallingContext &context, int32_t type, bool enable); 96 #ifdef DEVICE_STATUS_SENSOR_ENABLE 97 void TransQuaternionsToZXYRot(RotationVectorData quaternions, DevicePostureData &data); 98 #endif 99 100 #ifdef MOTION_ENABLE 101 std::map<Type, std::set<sptr<IRemoteDevStaCallback>, DevStaCallbackCmp>> deviceStatusMotionCallbacks_; 102 std::map<Type, Data> cacheData_; 103 sptr<IMotionCallback> motionCallback_ { nullptr }; 104 std::mutex mtx_; 105 sptr<IRemoteObject::DeathRecipient> devStaCBDeathRecipient_ { nullptr }; 106 #endif 107 DeviceStatusManager manager_; 108 }; 109 } // namespace DeviceStatus 110 } // namespace Msdp 111 } // namespace OHOS 112 #endif // STATIONARY_SERVER_H