1 /* 2 * Copyright (c) 2021-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 #ifndef SENSOR_PROXY_H 17 #define SENSOR_PROXY_H 18 19 #include <set> 20 #include <thread> 21 #include "sensor.h" 22 #include "singleton.h" 23 24 #include "sensor_data_channel.h" 25 26 namespace OHOS { 27 namespace Sensors { 28 struct SensorNativeData; 29 struct SensorIdList; 30 typedef int32_t (*SensorDataCallback)(struct SensorNativeData *events, uint32_t num); 31 32 class SensorAgentProxy { 33 DECLARE_DELAYED_SINGLETON(SensorAgentProxy); 34 public: 35 int32_t ActivateSensor(const SensorDescription &sensorDesc, const SensorUser *user); 36 int32_t DeactivateSensor(const SensorDescription &sensorDesc, const SensorUser *user); 37 int32_t SetBatch(const SensorDescription &sensorDesc, const SensorUser *user, int64_t samplingInterval, 38 int64_t reportInterval); 39 int32_t SubscribeSensor(const SensorDescription &sensorDesc, const SensorUser *user); 40 int32_t UnsubscribeSensor(const SensorDescription &sensorDesc, const SensorUser *user); 41 int32_t SetMode(const SensorDescription &sensorDesc, const SensorUser *user, int32_t mode); 42 int32_t SetOption(const SensorDescription &sensorDesc, const SensorUser *user, int32_t option); 43 void SetIsChannelCreated(bool isChannelCreated); 44 int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count) const; 45 int32_t GetDeviceSensors(int32_t deviceId, SensorInfo **singleDevSensorInfo, int32_t *count); 46 int32_t GetLocalDeviceId(int32_t &deviceId) const; 47 int32_t SuspendSensors(int32_t pid); 48 int32_t ResumeSensors(int32_t pid); 49 int32_t GetSensorActiveInfos(int32_t pid, SensorActiveInfo **sensorActiveInfos, int32_t *count) const; 50 int32_t Register(SensorActiveInfoCB callback); 51 int32_t Unregister(SensorActiveInfoCB callback); 52 void HandleSensorData(SensorEvent *events, int32_t num, void *data); 53 int32_t ResetSensors() const; 54 void SetDeviceStatus(uint32_t deviceStatus) const; 55 int32_t SubscribeSensorPlug(const SensorUser *user); 56 int32_t UnsubscribeSensorPlug(const SensorUser *user); 57 bool HandlePlugSensorData(const SensorPlugData &info); 58 59 private: 60 int32_t CreateSensorDataChannel(); 61 int32_t DestroySensorDataChannel(); 62 int32_t ConvertSensorInfos() const; 63 void ClearSensorInfos() const; 64 std::set<RecordSensorCallback> GetSubscribeUserCallback(const SensorDescription &sensorDesc); 65 bool IsSubscribeMapEmpty() const; 66 int32_t UpdateSensorInfo(SensorInfo* sensorInfo, const Sensor& sensor); 67 int32_t UpdateSensorInfosCache(const std::vector<Sensor>& deviceSensorList); 68 bool FindSensorInfo(int32_t deviceId, int32_t sensorIndex, int32_t sensorTypeId); 69 void UpdateSensorStatusEvent(SensorStatusEvent &event, const SensorPlugData &info); 70 bool UpdateSensorInfo(const SensorPlugData &info); 71 void EraseCacheSensorInfos(const SensorPlugData &info); 72 static std::recursive_mutex subscribeMutex_; 73 static std::recursive_mutex subscribePlugMutex_; 74 static std::mutex chanelMutex_; 75 OHOS::sptr<OHOS::Sensors::SensorDataChannel> dataChannel_ = nullptr; 76 std::atomic_bool isChannelCreated_ = false; 77 int64_t samplingInterval_ = -1; 78 int64_t reportInterval_ = -1; 79 std::map<SensorDescription, std::set<const SensorUser *>> subscribeMap_; 80 std::map<SensorDescription, std::set<const SensorUser *>> unsubscribeMap_; 81 std::set<const SensorUser *> subscribeSet_; 82 static std::mutex createChannelMutex_; 83 }; 84 85 const int32_t CHECK_CODE = 0x00ABCDEF; 86 87 struct SensorInfoCheck { 88 int32_t checkCode = CHECK_CODE; 89 SensorInfo *sensorInfos = nullptr; 90 }; 91 92 #define SENSOR_AGENT_IMPL OHOS::DelayedSingleton<SensorAgentProxy>::GetInstance() 93 } // namespace Sensors 94 } // namespace OHOS 95 #endif // endif SENSOR_PROXY_H