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 }; 59 60 struct GeomagneticData { 61 float x; 62 float y; 63 float z; 64 float geomagneticDip; 65 float deflectionAngle; 66 float levelIntensity; 67 float totalIntensity; 68 }; 69 70 struct RationMatrixData { 71 float rotationMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH]; 72 float inclinationMatrix[THREE_DIMENSIONAL_MATRIX_LENGTH]; 73 }; 74 75 struct CallbackSensorData { 76 int32_t sensorTypeId; 77 uint32_t dataLength; 78 float data[DATA_LENGTH]; 79 int64_t timestamp; 80 int32_t sensorAccuracy; 81 }; 82 83 struct ReserveData { 84 float reserve[DATA_LENGTH]; 85 int32_t length; 86 }; 87 88 union CallbackData { 89 CallbackSensorData sensorData; 90 GeomagneticData geomagneticData; 91 RationMatrixData rationMatrixData; 92 ReserveData reserveData; 93 }; 94 95 struct BusinessError { 96 int32_t code { 0 }; 97 string message; 98 string name; 99 string stack; 100 }; 101 102 class AsyncCallbackInfo : public RefBase { 103 public: 104 napi_env env = nullptr; 105 napi_async_work asyncWork = nullptr; 106 uv_work_t *work = nullptr; 107 napi_deferred deferred = nullptr; 108 napi_ref callback[CALLBACK_NUM] = { 0 }; 109 CallbackData data; 110 BusinessError error; 111 CallbackDataType type; 112 vector<SensorInfo> sensorInfos; AsyncCallbackInfo(napi_env env,CallbackDataType type)113 AsyncCallbackInfo(napi_env env, CallbackDataType type) : env(env), type(type) {} ~AsyncCallbackInfo()114 ~AsyncCallbackInfo() 115 { 116 CALL_LOG_ENTER; 117 if (type != ONCE_CALLBACK) { 118 if (asyncWork != nullptr) { 119 SEN_HILOGD("Delete async work"); 120 napi_delete_async_work(env, asyncWork); 121 asyncWork = nullptr; 122 } 123 for (int32_t i = 0; i < CALLBACK_NUM; ++i) { 124 if (callback[i] != nullptr) { 125 SEN_HILOGD("Delete reference, i:%{public}d", i); 126 napi_delete_reference(env, callback[i]); 127 callback[i] = nullptr; 128 } 129 } 130 } 131 } 132 133 private: 134 }; 135 } // namespace Sensors 136 } // namespace OHOS 137 #endif // ASYNC_CALLBACK_INFO_H