1 /* 2 * Copyright (c) 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 class SensorDataCallback : public Singleton<SensorDataCallback> { 35 public: 36 SensorDataCallback() = default; 37 ~SensorDataCallback(); 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 SensorUser user_; 52 std::list<AccelData> accelDataList_; 53 std::unique_ptr<std::thread> algorithmThread_ { nullptr }; 54 sem_t sem_; 55 std::mutex mutex_; 56 std::atomic<bool> alive_ { true }; 57 std::map<int32_t, SensorCallback> algoMap_; 58 }; 59 } // namespace Msdp 60 } // namespace OHOS 61 #endif // SENSOR_DATA_CALLBACK_H 62