1 /*
2 * Copyright (C) 2024 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 #include "nmea_message_callback_napi.h"
16
17 #include "common_utils.h"
18 #include "ipc_skeleton.h"
19 #include "location_log.h"
20 #include "napi_util.h"
21 #include "constant_definition.h"
22 #include "napi/native_api.h"
23 #include "napi/native_common.h"
24
25 namespace OHOS {
26 namespace Location {
NmeaMessageCallbackNapi()27 NmeaMessageCallbackNapi::NmeaMessageCallbackNapi()
28 {
29 env_ = nullptr;
30 handlerCb_ = nullptr;
31 remoteDied_ = false;
32 }
33
~NmeaMessageCallbackNapi()34 NmeaMessageCallbackNapi::~NmeaMessageCallbackNapi()
35 {
36 }
37
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)38 int NmeaMessageCallbackNapi::OnRemoteRequest(
39 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
40 {
41 LBSLOGD(NMEA_MESSAGE_CALLBACK, "NmeaMessageCallbackNapi::OnRemoteRequest!");
42 if (data.ReadInterfaceToken() != GetDescriptor()) {
43 LBSLOGE(NMEA_MESSAGE_CALLBACK, "invalid token.");
44 return -1;
45 }
46 if (remoteDied_) {
47 LBSLOGD(NMEA_MESSAGE_CALLBACK, "Failed to `%{public}s`,Remote service is died!", __func__);
48 return -1;
49 }
50
51 switch (code) {
52 case RECEIVE_NMEA_MESSAGE_EVENT: {
53 int64_t timestamp = data.ReadInt64();
54 std::string msg = Str16ToStr8(data.ReadString16());
55 OnMessageChange(timestamp, msg);
56 break;
57 }
58 default: {
59 IPCObjectStub::OnRemoteRequest(code, data, reply, option);
60 break;
61 }
62 }
63 return 0;
64 }
65
IsRemoteDied()66 bool NmeaMessageCallbackNapi::IsRemoteDied()
67 {
68 return remoteDied_;
69 }
70
PackResult(const std::string msg)71 napi_value NmeaMessageCallbackNapi::PackResult(const std::string msg)
72 {
73 napi_value result;
74 NAPI_CALL(env_, napi_create_string_utf8(env_, msg.c_str(), NAPI_AUTO_LENGTH, &result));
75 return result;
76 }
77
Send(const std::string msg)78 bool NmeaMessageCallbackNapi::Send(const std::string msg)
79 {
80 std::unique_lock<std::mutex> guard(mutex_);
81 uv_loop_s *loop = nullptr;
82 NAPI_CALL_BASE(env_, napi_get_uv_event_loop(env_, &loop), false);
83 if (loop == nullptr) {
84 LBSLOGE(NMEA_MESSAGE_CALLBACK, "loop == nullptr.");
85 return false;
86 }
87 if (handlerCb_ == nullptr) {
88 LBSLOGE(NMEA_MESSAGE_CALLBACK, "handler is nullptr.");
89 return false;
90 }
91 uv_work_t *work = new (std::nothrow) uv_work_t;
92 if (work == nullptr) {
93 LBSLOGE(NMEA_MESSAGE_CALLBACK, "work == nullptr.");
94 return false;
95 }
96 NmeaAsyncContext *context = new (std::nothrow) NmeaAsyncContext(env_);
97 if (context == nullptr) {
98 LBSLOGE(NMEA_MESSAGE_CALLBACK, "context == nullptr.");
99 delete work;
100 return false;
101 }
102 if (!InitContext(context)) {
103 LBSLOGE(NMEA_MESSAGE_CALLBACK, "InitContext fail");
104 return false;
105 }
106 context->msg = msg;
107 work->data = context;
108 UvQueueWork(loop, work);
109 return true;
110 }
111
112
UvQueueWork(uv_loop_s * loop,uv_work_t * work)113 void NmeaMessageCallbackNapi::UvQueueWork(uv_loop_s* loop, uv_work_t* work)
114 {
115 uv_queue_work(
116 loop,
117 work,
118 [](uv_work_t *work) {},
119 [](uv_work_t *work, int status) {
120 NmeaAsyncContext *context = nullptr;
121 napi_handle_scope scope = nullptr;
122 if (work == nullptr) {
123 LBSLOGE(LOCATOR_CALLBACK, "work is nullptr!");
124 return;
125 }
126 context = static_cast<NmeaAsyncContext *>(work->data);
127 if (context == nullptr || context->env == nullptr) {
128 LBSLOGE(LOCATOR_CALLBACK, "context is nullptr!");
129 delete work;
130 return;
131 }
132 NAPI_CALL_RETURN_VOID(context->env, napi_open_handle_scope(context->env, &scope));
133 if (scope == nullptr) {
134 LBSLOGE(NMEA_MESSAGE_CALLBACK, "scope is nullptr");
135 delete context;
136 delete work;
137 return;
138 }
139 napi_value jsEvent;
140 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
141 napi_create_string_utf8(context->env, context->msg.c_str(), NAPI_AUTO_LENGTH, &jsEvent),
142 scope, context, work);
143 if (context->callback[0] != nullptr) {
144 napi_value undefine;
145 napi_value handler = nullptr;
146 CHK_NAPI_ERR_CLOSE_SCOPE(context->env, napi_get_undefined(context->env, &undefine),
147 scope, context, work);
148 CHK_NAPI_ERR_CLOSE_SCOPE(context->env,
149 napi_get_reference_value(context->env, context->callback[0], &handler),
150 scope, context, work);
151 if (napi_call_function(context->env, nullptr, handler, 1, &jsEvent, &undefine) != napi_ok) {
152 LBSLOGE(NMEA_MESSAGE_CALLBACK, "Report event failed");
153 }
154 }
155 NAPI_CALL_RETURN_VOID(context->env, napi_close_handle_scope(context->env, scope));
156 delete context;
157 delete work;
158 });
159 }
160
OnMessageChange(int64_t timestamp,const std::string msg)161 void NmeaMessageCallbackNapi::OnMessageChange(int64_t timestamp, const std::string msg)
162 {
163 LBSLOGD(NMEA_MESSAGE_CALLBACK, "NmeaMessageCallbackNapi::OnMessageChange");
164 Send(msg);
165 }
166
DeleteHandler()167 void NmeaMessageCallbackNapi::DeleteHandler()
168 {
169 std::unique_lock<std::mutex> guard(mutex_);
170 if (handlerCb_ == nullptr || env_ == nullptr) {
171 LBSLOGE(NMEA_MESSAGE_CALLBACK, "handler or env is nullptr.");
172 return;
173 }
174 NAPI_CALL_RETURN_VOID(env_, napi_delete_reference(env_, handlerCb_));
175 handlerCb_ = nullptr;
176 }
177 } // namespace Location
178 } // namespace OHOS
179