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