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