1 /* 2 * Copyright (c) 2024 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 OHOS_CJ_SENSOR_IMPL_H 17 #define OHOS_CJ_SENSOR_IMPL_H 18 19 #include <functional> 20 #include <map> 21 #include <optional> 22 23 #include "cj_sensor_ffi.h" 24 #include "sensor_agent_type.h" 25 #include "singleton.h" 26 27 namespace OHOS { 28 namespace Sensors { 29 using SensorCallbackType = std::function<void(SensorEvent *)>; 30 31 class CJSensorImpl { 32 DECLARE_DELAYED_SINGLETON(CJSensorImpl); 33 34 public: 35 DISALLOW_COPY_AND_MOVE(CJSensorImpl); 36 int32_t OnSensorChange(int32_t sensorId, int64_t interval, void (*callback)(SensorEvent *event)); 37 int32_t OffSensorChange(int32_t sensorId); 38 void EmitCallBack(SensorEvent *event); 39 40 CGeomagneticData GetGeomagneticInfo(CLocationOptions location, int64_t timeMillis); 41 int32_t GetAltitude(float seaPressure, float currentPressure, float *altitude); 42 int32_t GetGeomagneticDip(CArrFloat32 inclinationMatrix, float *geomagneticDip); 43 int32_t GetAngleModify(const CArrFloat32 &curRotationMatrix, const CArrFloat32 &preRotationMatrix, 44 CArrFloat32 *angleChange); 45 int32_t GetRotationMatrix(const CArrFloat32 &rotationCArr, CArrFloat32 &rotation); 46 int32_t TransformRotationMatrix(const CArrFloat32 &rotationCArr, int32_t axisX, int32_t axisY, 47 CArrFloat32 &outRotationMatrix); 48 int32_t GetQuaternion(const CArrFloat32 &rotationVector, CArrFloat32 &quaternionOut); 49 int32_t GetOrientation(const CArrFloat32 &rotationMatrix, CArrFloat32 &rotationAngleOut); 50 int32_t GetRotationMatrixByGraityAndGeomagnetic(const CArrFloat32 gravity, const CArrFloat32 geomagnetic, 51 CArrFloat32 &rotationMatrix, CArrFloat32 &inclinationMatrix); 52 int32_t GetAllSensorList(CSensorArray &sensorList); 53 54 private: 55 std::map<int32_t, SensorCallbackType> eventMap_; 56 std::mutex mutex_; 57 58 int32_t SubscribeSensorImpl(int32_t sensorId, int64_t interval); 59 int32_t UnsubscribeSensorImpl(int32_t sensorTypeId); 60 61 void DelCallback(int32_t type); 62 void AddCallback2Map(int32_t type, SensorCallbackType callback); 63 std::optional<SensorCallbackType> FindCallback(int32_t type); 64 65 char *MallocCString(const std::string origin); 66 void Transform2CSensor(const SensorInfo &in, CSensor &out); 67 std::vector<float> ConvertCArr2Vector(const CArrFloat32 &in); 68 CArrFloat32 ConvertVector2CArr(const std::vector<float> &in); 69 70 static void CJDataCallbackImpl(SensorEvent *event); 71 const SensorUser cjUser_ = {.callback = CJDataCallbackImpl}; 72 }; 73 74 #define CJ_SENSOR_IMPL OHOS::DelayedSingleton<CJSensorImpl>::GetInstance() 75 76 } // namespace Sensors 77 } // namespace OHOS 78 79 #endif // OHOS_CJ_SENSOR_IMPL_H