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