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 <atomic> 20 #include <map> 21 #include <set> 22 #include <thread> 23 #include "refbase.h" 24 #include "singleton.h" 25 26 #include "sensor_agent_type.h" 27 #include "sensor_data_channel.h" 28 29 namespace OHOS { 30 namespace Sensors { 31 struct SensorNativeData; 32 struct SensorIdList; 33 typedef int32_t (*SensorDataCallback)(struct SensorNativeData *events, uint32_t num); 34 35 class SensorAgentProxy { 36 DECLARE_DELAYED_SINGLETON(SensorAgentProxy); 37 public: 38 int32_t ActivateSensor(int32_t sensorId, const SensorUser *user); 39 int32_t DeactivateSensor(int32_t sensorId, const SensorUser *user); 40 int32_t SetBatch(int32_t sensorId, const SensorUser *user, int64_t samplingInterval, int64_t reportInterval); 41 int32_t SubscribeSensor(int32_t sensorId, const SensorUser *user); 42 int32_t UnsubscribeSensor(int32_t sensorId, const SensorUser *user); 43 int32_t SetMode(int32_t sensorId, const SensorUser *user, int32_t mode); 44 int32_t SetOption(int32_t sensorId, const SensorUser *user, int32_t option); 45 void SetIsChannelCreated(bool isChannelCreated); 46 int32_t GetAllSensors(SensorInfo **sensorInfo, int32_t *count) 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 55 private: 56 int32_t CreateSensorDataChannel(); 57 int32_t DestroySensorDataChannel(); 58 int32_t ConvertSensorInfos() const; 59 void ClearSensorInfos() const; 60 std::set<RecordSensorCallback> GetSubscribeUserCallback(int32_t sensorId); 61 bool IsSubscribeMapEmpty() const; 62 static std::recursive_mutex subscribeMutex_; 63 static std::mutex chanelMutex_; 64 OHOS::sptr<OHOS::Sensors::SensorDataChannel> dataChannel_ = nullptr; 65 std::atomic_bool isChannelCreated_ = false; 66 int64_t samplingInterval_ = -1; 67 int64_t reportInterval_ = -1; 68 std::map<int32_t, std::set<const SensorUser *>> subscribeMap_; 69 std::map<int32_t, std::set<const SensorUser *>> unsubscribeMap_; 70 static std::mutex createChannelMutex_; 71 }; 72 73 #define SENSOR_AGENT_IMPL OHOS::DelayedSingleton<SensorAgentProxy>::GetInstance() 74 } // namespace Sensors 75 } // namespace OHOS 76 #endif // endif SENSOR_PROXY_H 77