[cj]: support cj api

Signed-off-by: zj94 <zhoujing106@huawei.com>
Change-Id: Ib9fc4016d36637c8665356760850990b8899111f
This commit is contained in:
zj94 2024-09-23 20:12:47 +08:00 committed by zhoujing
parent 85a0211470
commit 975e2500d3
7 changed files with 755 additions and 0 deletions

View File

@ -41,6 +41,7 @@
"base_group": [],
"fwk_group": [
"//base/sensors/sensor/frameworks/js/napi:sensor_js_target",
"//base/sensors/sensor/frameworks/cj:cj_sensor_ffi",
"//base/sensors/sensor/frameworks/native:sensor_target",
"//base/sensors/sensor/frameworks/native:ohsensor",
"//base/sensors/sensor/utils:sensor_utils_target"

55
frameworks/cj/BUILD.gn Normal file
View File

@ -0,0 +1,55 @@
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
import("./../../sensor.gni")
ohos_shared_library("cj_sensor_ffi") {
branch_protector_ret = "pac_ret"
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
cflags = [
"-fdata-sections",
"-ffunction-sections",
"-fvisibility=hidden",
]
include_dirs = [
"$SUBSYSTEM_DIR/frameworks/native/include",
"$SUBSYSTEM_DIR/interfaces/inner_api",
"$SUBSYSTEM_DIR/utils/common/include",
"include",
]
sources = [
"src/cj_sensor_ffi.cpp",
"src/cj_sensor_impl.cpp",
]
deps = [ "$SUBSYSTEM_DIR/frameworks/native:sensor_interface_native" ]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"napi:cj_bind_ffi",
"napi:cj_bind_native",
]
innerapi_tags = [ "platformsdk" ]
part_name = "sensor"
subsystem_name = "sensors"
}

View File

@ -0,0 +1,105 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_CJ_SENSOR_FFI_H
#define OHOS_CJ_SENSOR_FFI_H
#include <cstdint>
#include "cj_sensor_visibility.h"
#include "sensor_agent_type.h"
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
typedef struct {
float latitude;
float longitude;
float altitude;
} CLocationOptions;
typedef struct {
float x;
float y;
float z;
float geomagneticDip;
float deflectionAngle;
float levelIntensity;
float totalIntensity;
} CGeomagneticData;
typedef struct {
float *head;
int64_t size;
} CArrFloat32;
typedef struct {
int32_t x;
int32_t y;
} CCoordinatesOptions;
typedef struct {
CArrFloat32 rotation;
CArrFloat32 inclination;
} CRotationMatrixResponse;
typedef struct CSensor {
char *sensorName;
char *vendorName;
char *firmwareVersion;
char *hardwareVersion;
int32_t sensorTypeId;
float maxRange;
int64_t minSamplePeriod;
int64_t maxSamplePeriod;
float precision;
float power;
} CSensor;
typedef struct {
CSensor *head;
int64_t size;
} CSensorArray;
SENSOR_FFI_EXPORT int32_t FfiSensorSubscribeSensor(int32_t sensorId, int64_t interval,
void (*callback)(SensorEvent *event));
SENSOR_FFI_EXPORT int32_t FfiSensorUnSubscribeSensor(int32_t sensorId);
SENSOR_FFI_EXPORT CGeomagneticData FfiSensorGetGeomagneticInfo(CLocationOptions location, int64_t timeMillis);
SENSOR_FFI_EXPORT int32_t FfiSensorGetDeviceAltitude(float seaPressure, float currentPressure, float *altitude);
SENSOR_FFI_EXPORT int32_t FfiSensorGetInclination(CArrFloat32 inclinationMatrix, float *inclination);
SENSOR_FFI_EXPORT int32_t FfiSensorGetAngleVariation(CArrFloat32 currentRotationMatrix, CArrFloat32 preRotationMatrix,
CArrFloat32 *angleChange);
SENSOR_FFI_EXPORT int32_t FfiSensorGetRotationMatrix(CArrFloat32 rotationVector, CArrFloat32 *rotation);
SENSOR_FFI_EXPORT int32_t FfiSensorTransformRotationMatrix(CArrFloat32 inRotationVector,
CCoordinatesOptions coordinates,
CArrFloat32 *rotationMatrix);
SENSOR_FFI_EXPORT int32_t FfiSensorGetQuaternion(CArrFloat32 rotationVector, CArrFloat32 *quaternion);
SENSOR_FFI_EXPORT int32_t FfiSensorGetOrientation(CArrFloat32 rotationMatrix, CArrFloat32 *rotationAngle);
SENSOR_FFI_EXPORT int32_t FfiSensorGetRotationMatrixByGravityAndGeomagnetic(CArrFloat32 gravity,
CArrFloat32 geomagnetic,
CRotationMatrixResponse *rotationMaxtrix);
SENSOR_FFI_EXPORT int32_t FfiSensorGetAllSensors(CSensorArray *sensors);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif /* OHOS_CJ_SENSOR_FFI_H */
/**< @} */

View File

@ -0,0 +1,79 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_CJ_SENSOR_IMPL_H
#define OHOS_CJ_SENSOR_IMPL_H
#include <functional>
#include <map>
#include <optional>
#include "cj_sensor_ffi.h"
#include "sensor_agent_type.h"
#include "singleton.h"
namespace OHOS {
namespace Sensors {
using SensorCallbackType = std::function<void(SensorEvent *)>;
class CJSensorImpl {
DECLARE_DELAYED_SINGLETON(CJSensorImpl);
public:
DISALLOW_COPY_AND_MOVE(CJSensorImpl);
int32_t OnSensorChange(int32_t sensorId, int64_t interval, void (*callback)(SensorEvent *event));
int32_t OffSensorChange(int32_t sensorId);
void EmitCallBack(SensorEvent *event);
CGeomagneticData GetGeomagneticInfo(CLocationOptions location, int64_t timeMillis);
int32_t GetAltitude(float seaPressure, float currentPressure, float *altitude);
int32_t GetGeomagneticDip(CArrFloat32 inclinationMatrix, float *geomagneticDip);
int32_t GetAngleModify(const CArrFloat32 &curRotationMatrix, const CArrFloat32 &preRotationMatrix,
CArrFloat32 *angleChange);
int32_t GetRotationMatrix(const CArrFloat32 &rotationCArr, CArrFloat32 &rotation);
int32_t TransformRotationMatrix(const CArrFloat32 &rotationCArr, int32_t axisX, int32_t axisY,
CArrFloat32 &outRotationMatrix);
int32_t GetQuaternion(const CArrFloat32 &rotationVector, CArrFloat32 &quaternionOut);
int32_t GetOrientation(const CArrFloat32 &rotationMatrix, CArrFloat32 &rotationAngleOut);
int32_t GetRotationMatrixByGraityAndGeomagnetic(const CArrFloat32 gravity, const CArrFloat32 geomagnetic,
CArrFloat32 &rotationMatrix, CArrFloat32 &inclinationMatrix);
int32_t GetAllSensorList(CSensorArray &sensorList);
private:
std::map<int32_t, SensorCallbackType> eventMap_;
std::mutex mutex_;
int32_t SubscribeSensorImpl(int32_t sensorId, int64_t interval);
int32_t UnsubscribeSensorImpl(int32_t sensorTypeId);
void DelCallback(int32_t type);
void AddCallback2Map(int32_t type, SensorCallbackType callback);
std::optional<SensorCallbackType> FindCallback(int32_t type);
char *MallocCString(const std::string origin);
void Transform2CSensor(const SensorInfo &in, CSensor &out);
std::vector<float> ConvertCArr2Vector(const CArrFloat32 &in);
CArrFloat32 ConvertVector2CArr(const std::vector<float> &in);
static void CJDataCallbackImpl(SensorEvent *event);
const SensorUser cjUser_ = {.callback = CJDataCallbackImpl};
};
#define CJ_SENSOR_IMPL OHOS::DelayedSingleton<CJSensorImpl>::GetInstance()
} // namespace Sensors
} // namespace OHOS
#endif // OHOS_CJ_SENSOR_IMPL_H

View File

@ -0,0 +1,23 @@
/*
* Copyright (C) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_CJ_SENSOR_VISIBILITY_H
#define OHOS_CJ_SENSOR_VISIBILITY_H
#ifndef SENSOR_FFI_EXPORT
#define SENSOR_FFI_EXPORT __attribute__((visibility("default")))
#endif
#endif // OHOS_CJ_SENSOR_VISIBILITY_H

View File

@ -0,0 +1,118 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cj_sensor_ffi.h"
#include "cj_sensor_impl.h"
#include "sensor_errors.h"
using OHOS::Sensors::CJSensorImpl;
using OHOS::Sensors::PARAMETER_ERROR;
extern "C" {
int32_t FfiSensorSubscribeSensor(int32_t sensorId, int64_t interval, void (*callback)(SensorEvent *event))
{
return CJ_SENSOR_IMPL->OnSensorChange(sensorId, interval, callback);
}
int32_t FfiSensorUnSubscribeSensor(int32_t sensorId)
{
return CJ_SENSOR_IMPL->OffSensorChange(sensorId);
}
CGeomagneticData FfiSensorGetGeomagneticInfo(CLocationOptions location, int64_t timeMillis)
{
return CJ_SENSOR_IMPL->GetGeomagneticInfo(location, timeMillis);
}
int32_t FfiSensorGetDeviceAltitude(float seaPressure, float currentPressure, float *altitude)
{
return CJ_SENSOR_IMPL->GetAltitude(seaPressure, currentPressure, altitude);
}
int32_t FfiSensorGetInclination(CArrFloat32 inclinationMatrix, float *geomagneticDip)
{
return CJ_SENSOR_IMPL->GetGeomagneticDip(inclinationMatrix, geomagneticDip);
}
int32_t FfiSensorGetAngleVariation(CArrFloat32 currentRotationMatrix, CArrFloat32 preRotationMatrix,
CArrFloat32 *angleChange)
{
return CJ_SENSOR_IMPL->GetAngleModify(currentRotationMatrix, preRotationMatrix, angleChange);
}
int32_t FfiSensorGetRotationMatrix(CArrFloat32 rotationVector, CArrFloat32 *rotation)
{
if (rotation == nullptr) {
SEN_HILOGE("Invalid parameter, rotation is nullptr!");
return PARAMETER_ERROR;
}
return CJ_SENSOR_IMPL->GetRotationMatrix(rotationVector, *rotation);
}
int32_t FfiSensorTransformRotationMatrix(CArrFloat32 inRotationVector, CCoordinatesOptions coordinates,
CArrFloat32 *rotationMatrix)
{
if (rotationMatrix == nullptr) {
SEN_HILOGE("Invalid parameter, rotationMatrix is nullptr!");
return PARAMETER_ERROR;
}
return CJ_SENSOR_IMPL->TransformRotationMatrix(inRotationVector, coordinates.x, coordinates.y, *rotationMatrix);
}
int32_t FfiSensorGetQuaternion(CArrFloat32 rotationVector, CArrFloat32 *quaternion)
{
if (quaternion == nullptr) {
SEN_HILOGE("Invalid parameter, quaternion is nullptr!");
return PARAMETER_ERROR;
}
return CJ_SENSOR_IMPL->GetQuaternion(rotationVector, *quaternion);
}
int32_t FfiSensorGetOrientation(CArrFloat32 rotationMatrix, CArrFloat32 *rotationAngle)
{
if (rotationAngle == nullptr) {
SEN_HILOGE("Invalid parameter, rotationAngle is nullptr!");
return PARAMETER_ERROR;
}
return CJ_SENSOR_IMPL->GetOrientation(rotationMatrix, *rotationAngle);
}
int32_t FfiSensorGetRotationMatrixByGravityAndGeomagnetic(CArrFloat32 gravity, CArrFloat32 geomagnetic,
CRotationMatrixResponse *rotationMaxtrix)
{
if (rotationMaxtrix == nullptr) {
SEN_HILOGE("Invalid parameter, rotationMaxtrix is nullptr!");
return PARAMETER_ERROR;
}
return CJ_SENSOR_IMPL->GetRotationMatrixByGraityAndGeomagnetic(gravity, geomagnetic, rotationMaxtrix->rotation,
rotationMaxtrix->inclination);
}
int32_t FfiSensorGetAllSensors(CSensorArray *sensors)
{
if (sensors == nullptr) {
SEN_HILOGE("Invalid parameter, sensors is nullptr!");
return PARAMETER_ERROR;
}
return CJ_SENSOR_IMPL->GetAllSensorList(*sensors);
}
}

View File

@ -0,0 +1,374 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "cj_sensor_impl.h"
#include <cstddef>
#include <cstdint>
#include <optional>
#include "cj_lambda.h"
#include "cj_sensor_ffi.h"
#include "errors.h"
#include "geomagnetic_field.h"
#include "sensor_agent.h"
#include "sensor_algorithm.h"
#include "sensor_errors.h"
namespace OHOS {
namespace Sensors {
namespace {
constexpr int32_t ROTATION_VECTOR_LENGTH = 3;
constexpr int32_t QUATERNION_LENGTH = 4;
constexpr int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9;
constexpr int32_t DATA_LENGTH = 16;
} // namespace
CJSensorImpl::CJSensorImpl() {}
CJSensorImpl::~CJSensorImpl() {}
void CJSensorImpl::CJDataCallbackImpl(SensorEvent *event)
{
CHKPV(event);
CJ_SENSOR_IMPL->EmitCallBack(event);
}
int32_t CJSensorImpl::SubscribeSensorImpl(int32_t sensorId, int64_t interval)
{
CALL_LOG_ENTER;
int32_t ret = SubscribeSensor(sensorId, &cjUser_);
if (ret != ERR_OK) {
SEN_HILOGE("SubscribeSensor failed");
return ret;
}
ret = SetBatch(sensorId, &cjUser_, interval, 0);
if (ret != ERR_OK) {
SEN_HILOGE("SetBatch failed");
return ret;
}
return ActivateSensor(sensorId, &cjUser_);
}
int32_t CJSensorImpl::UnsubscribeSensorImpl(int32_t sensorTypeId)
{
CALL_LOG_ENTER;
int32_t ret = DeactivateSensor(sensorTypeId, &cjUser_);
if (ret != ERR_OK) {
SEN_HILOGE("DeactivateSensor failed");
return ret;
}
return UnsubscribeSensor(sensorTypeId, &cjUser_);
}
char *CJSensorImpl::MallocCString(const std::string origin)
{
if (origin.empty()) {
SEN_HILOGD("String is empty.");
return nullptr;
}
auto len = origin.length() + 1;
char *res = static_cast<char *>(malloc(sizeof(char) * len));
if (res == nullptr) {
SEN_HILOGE("Malloc failed.");
return nullptr;
}
return std::char_traits<char>::copy(res, origin.c_str(), len);
}
void CJSensorImpl::Transform2CSensor(const SensorInfo &in, CSensor &out)
{
out.sensorName = MallocCString(in.sensorName);
out.vendorName = MallocCString(in.vendorName);
out.firmwareVersion = MallocCString(in.firmwareVersion);
out.hardwareVersion = MallocCString(in.hardwareVersion);
out.sensorTypeId = in.sensorTypeId;
out.maxRange = in.maxRange;
out.minSamplePeriod = in.minSamplePeriod;
out.maxSamplePeriod = in.maxSamplePeriod;
out.precision = in.precision;
out.power = in.power;
}
std::vector<float> CJSensorImpl::ConvertCArr2Vector(const CArrFloat32 &in)
{
std::vector<float> res;
if (in.head == nullptr || in.size <= 0) {
SEN_HILOGD("head is nullptr or size is zero.");
return res;
}
for (int64_t i = 0; i < in.size; i++) {
res.push_back(in.head[i]);
}
return res;
}
CArrFloat32 CJSensorImpl::ConvertVector2CArr(const std::vector<float> &in)
{
CArrFloat32 res = {NULL, 0};
if (in.empty()) {
SEN_HILOGD("vector is empty.");
return res;
}
res.head = static_cast<float *>(malloc(sizeof(float) * in.size()));
if (res.head == nullptr) {
SEN_HILOGE("Malloc failed.");
return res;
}
size_t i = 0;
for (; i < in.size(); ++i) {
res.head[i] = in[i];
}
res.size = i;
return res;
}
int32_t CJSensorImpl::OnSensorChange(int32_t sensorId, int64_t interval, void (*callback)(SensorEvent *event))
{
CALL_LOG_ENTER;
int32_t ret = SubscribeSensorImpl(sensorId, interval);
if (ret != ERR_OK) {
SEN_HILOGE("subscribe sensor failed, %{public}d.", sensorId);
return ret;
}
AddCallback2Map(sensorId, CJLambda::Create(callback));
return ERR_OK;
}
int32_t CJSensorImpl::OffSensorChange(int32_t sensorId)
{
CALL_LOG_ENTER;
int32_t ret = UnsubscribeSensorImpl(sensorId);
if (ret != ERR_OK) {
SEN_HILOGE("unsubscribe sensor failed, %{public}d.", sensorId);
return ret;
}
DelCallback(sensorId);
return ERR_OK;
}
void CJSensorImpl::EmitCallBack(SensorEvent *event)
{
auto callback = FindCallback(event->sensorTypeId);
if (callback == std::nullopt) {
SEN_HILOGE("EmitCallBack failed, %{public}d not find.", event->sensorTypeId);
return;
}
callback.value()(event);
}
void CJSensorImpl::AddCallback2Map(int32_t type, SensorCallbackType callback)
{
std::lock_guard<std::mutex> mutex(mutex_);
eventMap_[type] = callback;
}
void CJSensorImpl::DelCallback(int32_t type)
{
std::lock_guard<std::mutex> mutex(mutex_);
eventMap_.erase(type);
}
std::optional<SensorCallbackType> CJSensorImpl::FindCallback(int32_t type)
{
std::lock_guard<std::mutex> mutex(mutex_);
auto iter = eventMap_.find(type);
if (iter != eventMap_.end()) {
return iter->second;
}
return std::nullopt;
}
CGeomagneticData CJSensorImpl::GetGeomagneticInfo(CLocationOptions location, int64_t timeMillis)
{
GeomagneticField geomagneticField(location.latitude, location.longitude, location.altitude, timeMillis);
CGeomagneticData res = {
.x = geomagneticField.ObtainX(),
.y = geomagneticField.ObtainY(),
.z = geomagneticField.ObtainZ(),
.geomagneticDip = geomagneticField.ObtainGeomagneticDip(),
.deflectionAngle = geomagneticField.ObtainDeflectionAngle(),
.levelIntensity = geomagneticField.ObtainLevelIntensity(),
.totalIntensity = geomagneticField.ObtainTotalIntensity(),
};
return res;
}
int32_t CJSensorImpl::GetAltitude(float seaPressure, float currentPressure, float *altitude)
{
SensorAlgorithm sensorAlgorithm;
int32_t ret = sensorAlgorithm.GetAltitude(seaPressure, currentPressure, altitude);
if (ret != ERR_OK) {
SEN_HILOGE("Get altitude failed, ret:%{public}d", ret);
}
return ret;
}
int32_t CJSensorImpl::GetGeomagneticDip(CArrFloat32 inclinationMatrix, float *geomagneticDip)
{
SensorAlgorithm sensorAlgorithm;
int32_t ret = sensorAlgorithm.GetGeomagneticDip(ConvertCArr2Vector(inclinationMatrix), geomagneticDip);
if (ret != ERR_OK) {
SEN_HILOGE("Get geomagnetic dip failed, ret:%{public}d", ret);
}
return ret;
}
int32_t CJSensorImpl::GetAngleModify(const CArrFloat32 &curRotationMatrix, const CArrFloat32 &preRotationMatrix,
CArrFloat32 *angleChangeOut)
{
if (angleChangeOut == nullptr) {
SEN_HILOGE("Invalid parameter, angleChangeOut is nullptr!");
return PARAMETER_ERROR;
}
std::vector<float> angleChange(ROTATION_VECTOR_LENGTH);
SensorAlgorithm sensorAlgorithm;
int32_t ret = sensorAlgorithm.GetAngleModify(ConvertCArr2Vector(curRotationMatrix),
ConvertCArr2Vector(preRotationMatrix), angleChange);
if (ret != ERR_OK) {
SEN_HILOGE("Get angle change failed, ret:%{public}d", ret);
return ret;
}
*angleChangeOut = ConvertVector2CArr(angleChange);
return ret;
}
int32_t CJSensorImpl::GetRotationMatrix(const CArrFloat32 &rotationCArr, CArrFloat32 &rotation)
{
std::vector<float> rotationMatrix(THREE_DIMENSIONAL_MATRIX_LENGTH);
SensorAlgorithm sensorAlgorithm;
int32_t ret = sensorAlgorithm.CreateRotationMatrix(ConvertCArr2Vector(rotationCArr), rotationMatrix);
if (ret != ERR_OK) {
SEN_HILOGE("Get rotation matrix failed, ret:%{public}d", ret);
return ret;
}
rotation = ConvertVector2CArr(rotationMatrix);
return ret;
}
int32_t CJSensorImpl::TransformRotationMatrix(const CArrFloat32 &rotationCArr, int32_t axisX, int32_t axisY,
CArrFloat32 &outRotationMatrix)
{
int64_t length = rotationCArr.size;
if ((length != DATA_LENGTH) && (length != THREE_DIMENSIONAL_MATRIX_LENGTH)) {
SEN_HILOGE("Transform rotation mastrix failed, invalid parameter.");
return PARAMETER_ERROR;
}
std::vector<float> outRotationVector(length);
SensorAlgorithm sensorAlgorithm;
int32_t ret =
sensorAlgorithm.TransformCoordinateSystem(ConvertCArr2Vector(rotationCArr), axisX, axisY, outRotationVector);
if (ret != ERR_OK) {
SEN_HILOGE("Transform coordinate system failed, ret:%{public}d", ret);
return ret;
}
outRotationMatrix = ConvertVector2CArr(outRotationVector);
return ret;
}
int32_t CJSensorImpl::GetQuaternion(const CArrFloat32 &rotationVector, CArrFloat32 &quaternionOut)
{
std::vector<float> quaternion(QUATERNION_LENGTH);
SensorAlgorithm sensorAlgorithm;
int32_t ret = sensorAlgorithm.CreateQuaternion(ConvertCArr2Vector(rotationVector), quaternion);
if (ret != ERR_OK) {
SEN_HILOGE("Get quaternion failed, ret:%{public}d", ret);
return ret;
}
quaternionOut = ConvertVector2CArr(quaternion);
return ret;
}
int32_t CJSensorImpl::GetOrientation(const CArrFloat32 &rotationMatrix, CArrFloat32 &rotationAngleOut)
{
std::vector<float> rotationAngle(ROTATION_VECTOR_LENGTH);
SensorAlgorithm sensorAlgorithm;
int32_t ret = sensorAlgorithm.GetDirection(ConvertCArr2Vector(rotationMatrix), rotationAngle);
if (ret != OHOS::ERR_OK) {
SEN_HILOGE("Get direction failed, ret:%{public}d", ret);
return ret;
}
rotationAngleOut = ConvertVector2CArr(rotationAngle);
return ret;
}
int32_t CJSensorImpl::GetRotationMatrixByGraityAndGeomagnetic(const CArrFloat32 gravity, const CArrFloat32 geomagnetic,
CArrFloat32 &rotationMatrix,
CArrFloat32 &inclinationMatrix)
{
std::vector<float> rotation(THREE_DIMENSIONAL_MATRIX_LENGTH);
std::vector<float> inclination(THREE_DIMENSIONAL_MATRIX_LENGTH);
SensorAlgorithm sensorAlgorithm;
int32_t ret = sensorAlgorithm.CreateRotationAndInclination(ConvertCArr2Vector(gravity),
ConvertCArr2Vector(geomagnetic), rotation, inclination);
if (ret != OHOS::ERR_OK) {
SEN_HILOGE("Create rotation and inclination failed, ret:%{public}d", ret);
return ret;
}
rotationMatrix = ConvertVector2CArr(rotation);
inclinationMatrix = ConvertVector2CArr(inclination);
return ret;
}
int32_t CJSensorImpl::GetAllSensorList(CSensorArray &sensorList)
{
SensorInfo *sensorInfos = nullptr;
int32_t count = 0;
int32_t ret = GetAllSensors(&sensorInfos, &count);
if (ret != OHOS::ERR_OK) {
SEN_HILOGE("Get all sensors failed, ret:%{public}d", ret);
return ret;
}
if (count == 0) {
return ERR_OK;
}
sensorList.head = static_cast<CSensor *>(malloc(sizeof(CSensor) * count));
if (sensorList.head == nullptr) {
SEN_HILOGE("Malloc failed.");
return ERR_OK;
}
int32_t i = 0;
for (; i < count; ++i) {
if ((sensorInfos[i].sensorTypeId == SENSOR_TYPE_ID_AMBIENT_LIGHT1) ||
(sensorInfos[i].sensorTypeId == SENSOR_TYPE_ID_PROXIMITY1)) {
SEN_HILOGD("This sensor is secondary ambient light");
continue;
}
Transform2CSensor(sensorInfos[i], sensorList.head[i]);
}
sensorList.size = i;
return ERR_OK;
}
} // namespace Sensors
} // namespace OHOS