1 /*
2 * Copyright (c) 2022 Chipsea Technologies (Shenzhen) Corp., 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
16 #include "medical_napi_utils.h"
17
18 #include <iostream>
19 #include <map>
20 #include <string>
21 #include <vector>
22
23 #include "hilog/log.h"
24
25 using namespace OHOS::HiviewDFX;
26 static constexpr HiLogLabel LABEL = {LOG_CORE, 0xD002786, "AfeJsAPI"};
IsMatchType(napi_value value,napi_valuetype type,napi_env env)27 bool IsMatchType(napi_value value, napi_valuetype type, napi_env env)
28 {
29 napi_valuetype paramType;
30 napi_typeof(env, value, ¶mType);
31 if (paramType != type) {
32 HiLog::Error(LABEL, "%{public}s failed!", __func__);
33 return false;
34 }
35 return true;
36 }
37
GetNapiInt32(int32_t number,napi_env env)38 napi_value GetNapiInt32(int32_t number, napi_env env)
39 {
40 napi_value value;
41 napi_create_int32(env, number, &value);
42 return value;
43 }
44
NapiGetNamedProperty(napi_value jsonObject,std::string name,napi_env env)45 napi_value NapiGetNamedProperty(napi_value jsonObject, std::string name, napi_env env)
46 {
47 napi_value value;
48 napi_get_named_property(env, jsonObject, name.c_str(), &value);
49 return value;
50 }
51
GetCppInt32(napi_value value,napi_env env)52 int32_t GetCppInt32(napi_value value, napi_env env)
53 {
54 int32_t number;
55 napi_get_value_int32(env, value, &number);
56 return number;
57 }
58
GetCppInt64(napi_value value,napi_env env)59 int64_t GetCppInt64(napi_value value, napi_env env)
60 {
61 int64_t number;
62 napi_get_value_int64(env, value, &number);
63 return number;
64 }
65
GetCppBool(napi_value value,napi_env env)66 bool GetCppBool(napi_value value, napi_env env)
67 {
68 bool number;
69 napi_get_value_bool(env, value, &number);
70 return number;
71 }
72
GetUndefined(napi_env env)73 napi_value GetUndefined(napi_env env)
74 {
75 napi_value value;
76 napi_get_undefined(env, &value);
77 return value;
78 }
79
80 std::map<int32_t, std::vector<std::string>> g_sensorAttributeList = {
81 { TYPE_ID_NONE, { "dataArray" } },
82 { TYPE_ID_PHOTOPLETHYSMOGRAPH, { "dataArray" } },
83 };
84
EmitAsyncCallbackWork(AsyncCallbackInfo * asyncCallbackInfo)85 void EmitAsyncCallbackWork(AsyncCallbackInfo *asyncCallbackInfo)
86 {
87 HiLog::Debug(LABEL, "%{public}s begin", __func__);
88 if (asyncCallbackInfo == nullptr) {
89 HiLog::Error(LABEL, "%{public}s asyncCallbackInfo is null!", __func__);
90 return;
91 }
92 napi_value resourceName;
93 if (napi_create_string_utf8(asyncCallbackInfo->env, "AsyncCallback", NAPI_AUTO_LENGTH, &resourceName) != napi_ok) {
94 HiLog::Error(LABEL, "%{public}s create string utf8 failed", __func__);
95 return;
96 }
97 napi_create_async_work(
98 asyncCallbackInfo->env, nullptr, resourceName,
99 [](napi_env env, void* data) {},
100 [](napi_env env, napi_status status, void* data) {
101 HiLog::Debug(LABEL, "%{public}s napi_create_async_work in", __func__);
102 AsyncCallbackInfo *asyncCallbackInfo = reinterpret_cast<AsyncCallbackInfo *>(data);
103 napi_value callback;
104 napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback);
105 napi_value callResult = nullptr;
106 napi_value result;
107 napi_get_undefined(env, &result);
108 napi_call_function(env, nullptr, callback, 1, &result, &callResult);
109 napi_delete_reference(env, asyncCallbackInfo->callback[0]);
110 napi_delete_async_work(env, asyncCallbackInfo->asyncWork);
111 delete asyncCallbackInfo;
112 asyncCallbackInfo = nullptr;
113 HiLog::Debug(LABEL, "%{public}s napi_create_async_work left", __func__);
114 },
115 asyncCallbackInfo, &asyncCallbackInfo->asyncWork);
116 napi_queue_async_work(asyncCallbackInfo->env, asyncCallbackInfo->asyncWork);
117 HiLog::Debug(LABEL, "%{public}s end", __func__);
118 }
119
EmitUvEventLoop(AsyncCallbackInfo * asyncCallbackInfo)120 void EmitUvEventLoop(AsyncCallbackInfo *asyncCallbackInfo)
121 {
122 uv_loop_s *loop(nullptr);
123 napi_get_uv_event_loop(asyncCallbackInfo->env, &loop);
124 if (loop == nullptr) {
125 HiLog::Error(LABEL, "%{public}s loop is null", __func__);
126 return;
127 }
128
129 uv_work_t *work = new(std::nothrow) uv_work_t;
130 if (work == nullptr) {
131 HiLog::Error(LABEL, "%{public}s work is null", __func__);
132 return;
133 }
134
135 work->data = reinterpret_cast<void *>(asyncCallbackInfo);
136 uv_queue_work(loop, work, [] (uv_work_t *work) {}, [] (uv_work_t *work, int status) {
137 AsyncCallbackInfo *asyncCallbackInfo = reinterpret_cast<AsyncCallbackInfo *>(work->data);
138 if (asyncCallbackInfo == nullptr) {
139 HiLog::Error(LABEL, "%{public}s asyncCallbackInfo is null", __func__);
140 return;
141 }
142 napi_env env = asyncCallbackInfo->env;
143 napi_value undefined;
144 napi_get_undefined(env, &undefined);
145 if (asyncCallbackInfo->callback[0] == nullptr) {
146 HiLog::Error(LABEL, "%{public}s callback is null", __func__);
147 return;
148 }
149 napi_value callback;
150 napi_get_reference_value(env, asyncCallbackInfo->callback[0], &callback);
151 napi_value callResult = nullptr;
152 napi_value result;
153
154 int32_t sensorTypeId = asyncCallbackInfo->sensorTypeId;
155 if (g_sensorAttributeList.count(sensorTypeId) == 0) {
156 HiLog::Error(LABEL, "%{public}s count of sensorTypeId is zero", __func__);
157 return;
158 }
159
160 std::vector<std::string> sensorAttribute = g_sensorAttributeList[sensorTypeId];
161 napi_create_object(env, &result);
162 for (size_t i = 0; i < sensorAttribute.size(); i++) {
163 napi_value message = nullptr;
164 napi_create_array(env, &message);
165 for (size_t j = 0; j < asyncCallbackInfo->sensorDataLength; j++) {
166 napi_value num;
167 napi_create_uint32(env, asyncCallbackInfo->sensorData[j], &num);
168 napi_set_element(env, message, j, num);
169 }
170 napi_set_named_property(env, result, sensorAttribute[i].c_str(), message);
171 }
172
173 napi_call_function(env, undefined, callback, 1, &result, &callResult);
174
175 delete work;
176 work = nullptr;
177 });
178 }
179