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