• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #ifndef SENSOR_ANI_H
16 #define SENSOR_ANI_H
17 
18 #include <ani.h>
19 #include <string>
20 #include <iostream>
21 #include <unordered_map>
22 #include <cctype>
23 #include <map>
24 #include <unistd.h>
25 #include <vector>
26 
27 #include "refbase.h"
28 #include "sensor_agent_type.h"
29 #include "sensor_errors.h"
30 #include "sensor_log.h"
31 
32 #undef LOG_TAG
33 #define LOG_TAG "SensorAniAPI"
34 
35 namespace OHOS {
36 namespace Sensors {
37 using std::vector;
38 using std::string;
39 using namespace OHOS::HiviewDFX;
40 constexpr int32_t THREE_DIMENSIONAL_MATRIX_LENGTH = 9;
41 constexpr static int32_t DATA_LENGTH = 16;
42 constexpr int32_t CALLBACK_NUM = 3;
43 enum CallbackDataType {
44     SUBSCRIBE_FAIL = -2,
45     FAIL = -1,
46     OFF_CALLBACK = 0,
47     ON_CALLBACK = 1,
48     ONCE_CALLBACK = 2,
49     GET_GEOMAGNETIC_FIELD = 3,
50     GET_ALTITUDE = 4,
51     GET_GEOMAGNETIC_DIP = 5,
52     GET_ANGLE_MODIFY = 6,
53     CREATE_ROTATION_MATRIX = 7,
54     TRANSFORM_COORDINATE_SYSTEM = 8,
55     CREATE_QUATERNION = 9,
56     GET_DIRECTION = 10,
57     ROTATION_INCLINATION_MATRIX = 11,
58     GET_SENSOR_LIST = 12,
59     GET_SINGLE_SENSOR = 13,
60     SUBSCRIBE_CALLBACK = 14,
61     SUBSCRIBE_COMPASS = 15,
62     GET_BODY_STATE = 16,
63 };
64 
65 struct GeomagneticData {
66     float x;
67     float y;
68     float z;
69     float geomagneticDip;
70     float deflectionAngle;
71     float levelIntensity;
72     float totalIntensity;
73 };
74 
75 struct RationMatrixData {
76     float rotationMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH];
77     float inclinationMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH];
78 };
79 
80 struct CallbackSensorData {
81     int32_t sensorTypeId;
82     uint32_t dataLength;
83     float data[DATA_LENGTH];
84     int64_t timestamp;
85     int32_t sensorAccuracy;
86 };
87 
88 struct ReserveData {
89     float reserve[DATA_LENGTH];
90     int32_t length;
91 };
92 
93 union CallbackData {
94     CallbackSensorData sensorData;
95     GeomagneticData geomagneticData;
96     RationMatrixData rationMatrixData;
97     ReserveData reserveData;
98 };
99 
100 struct BusinessError {
101     int32_t code { 0 };
102     string message;
103     string name;
104     string stack;
105 };
106 
107 class AsyncCallbackInfo : public RefBase {
108 public:
109     ani_vm *vm = nullptr;
110     ani_env *env = nullptr;
111     ani_ref callback[CALLBACK_NUM] = { 0 };
112     CallbackData data;
113     BusinessError error;
114     CallbackDataType type;
115     vector<SensorInfo> sensorInfos;
AsyncCallbackInfo(ani_vm * vm,ani_env * env,CallbackDataType type)116     AsyncCallbackInfo(ani_vm *vm, ani_env *env, CallbackDataType type) : vm(vm), env(env), type(type) {}
~AsyncCallbackInfo()117     ~AsyncCallbackInfo()
118     {
119         CALL_LOG_ENTER;
120         for (int32_t i = 0; i < CALLBACK_NUM; ++i) {
121             if (callback[i] != nullptr) {
122                 SEN_HILOGD("Delete reference, i:%{public}d", i);
123                 env->GlobalReference_Delete(callback[i]);
124                 callback[i] = nullptr;
125             }
126         }
127     }
128 
129 private:
130 };
131 } // namespace Sensors
132 } // namespace OHOS
133 #endif // SENSOR_ANI_H