1 /* 2 * Copyright (c) 2022-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_DATA_CALLBACK_H 17 #define SENSOR_DATA_CALLBACK_H 18 19 #include <atomic> 20 #include <list> 21 #include <map> 22 #include <mutex> 23 #include <thread> 24 25 #include "semaphore.h" 26 #include "singleton.h" 27 #include "sensor_agent.h" 28 #include "sensor_agent_type.h" 29 30 #include "devicestatus_data_define.h" 31 32 namespace OHOS { 33 namespace Msdp { 34 namespace DeviceStatus { 35 class SensorDataCallback final { 36 DECLARE_SINGLETON(SensorDataCallback); 37 public: 38 bool RegisterCallbackSensor(int32_t sensorTypeId); 39 bool UnregisterCallbackSensor(int32_t sensorTypeId); 40 void Init(); 41 bool Unregister(); 42 bool SubscribeSensorEvent(int32_t sensorTypeId, SensorCallback callback); 43 bool UnsubscribeSensorEvent(int32_t sensorTypeId, SensorCallback callback); 44 bool PushData(int32_t sensorTypeId, uint8_t* data); 45 46 private: 47 bool PopData(int32_t sensorTypeId, AccelData& data); 48 void AlgorithmLoop(); 49 void HandleSensorEvent(); 50 bool NotifyCallback(int32_t sensorTypeId, AccelData* data); 51 52 struct SensorUser user_ = {.name = {0}, .callback = nullptr, .userData = nullptr}; 53 std::list<AccelData> accelDataList_; 54 std::unique_ptr<std::thread> algorithmThread_ { nullptr }; 55 sem_t sem_ = {}; 56 std::mutex callbackMutex_; 57 std::mutex dataMutex_; 58 std::mutex initMutex_; 59 std::mutex sensorMutex_; 60 std::atomic<bool> alive_ { true }; 61 }; 62 #define SENSOR_DATA_CB OHOS::Singleton<SensorDataCallback>::GetInstance() 63 } // namespace DeviceStatus 64 } // namespace Msdp 65 } // namespace OHOS 66 #endif // SENSOR_DATA_CALLBACK_H 67