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 LOG_TAG
16 #define LOG_TAG "bt_napi_event"
17 #endif
18 
19 #include <uv.h>
20 #include "bluetooth_utils.h"
21 #include "napi_bluetooth_ble_utils.h"
22 #include "napi_bluetooth_event.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
EventNotify(AsyncEventData * asyncEvent)26 void NapiEvent::EventNotify(AsyncEventData *asyncEvent)
27 {
28     uv_loop_s *loop = nullptr;
29     napi_get_uv_event_loop(asyncEvent->env_, &loop);
30 
31     uv_work_t *work = new uv_work_t;
32     if (work == nullptr) {
33         HILOGI("uv_work_t work is null.");
34         delete asyncEvent;
35         asyncEvent = nullptr;
36         return;
37     }
38     work->data = asyncEvent;
39 
40     uv_queue_work(
41         loop,
42         work,
43         [](uv_work_t *work) {},
44         [](uv_work_t *work, int status) {
45             AsyncEventData *callbackInfo = static_cast<AsyncEventData*>(work->data);
46             napi_value callback = nullptr;
47             napi_status ret = napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
48             if (ret == napi_ok && callback != nullptr) {
49                 napi_value result = nullptr;
50                 napi_value undefined = nullptr;
51                 napi_value callResult = nullptr;
52                 if (napi_get_undefined(callbackInfo->env_, &undefined) == napi_ok) {
53                     result = callbackInfo->packResult();
54                 }
55                 if (result != nullptr) {
56                     napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE,
57                         &result, &callResult);
58                 }
59             }
60             delete callbackInfo;
61             delete work;
62             work = nullptr;
63         }
64     );
65 }
66 
CreateResult(const std::shared_ptr<BluetoothCallbackInfo> & cb,int value)67 napi_value NapiEvent::CreateResult(const std::shared_ptr<BluetoothCallbackInfo> &cb, int value)
68 {
69     napi_value result = nullptr;
70     if (cb == nullptr) {
71         HILOGE("CreateResult cb is null!");
72         return result;
73     }
74     napi_create_object(cb->env_, &result);
75     ConvertStateChangeParamToJS(cb->env_, result, cb->deviceId_, value,
76         static_cast<int>(ConnChangeCause::CONNECT_CHANGE_COMMON_CAUSE));
77     return result;
78 }
79 
CreateResult(const std::shared_ptr<BluetoothCallbackInfo> & cb,BluetoothOppTransferInformation & information)80 napi_value NapiEvent::CreateResult(const std::shared_ptr<BluetoothCallbackInfo> &cb,
81     BluetoothOppTransferInformation &information)
82 {
83     napi_value result = nullptr;
84     napi_create_object(cb->env_, &result);
85     ConvertOppTransferInformationToJS(cb->env_, result, information);
86     return result;
87 }
88 
89 // if callbackInfos contains specific type, new callbackInfo will cover the old.
90 // If exist, covered event happen, this function will clear rest reference of old callbackInfo in napi framework.
UpdateCallbackInfo(std::map<std::string,std::shared_ptr<BluetoothCallbackInfo>> callbackInfos,const std::string & type)91 void UpdateCallbackInfo(std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> callbackInfos,
92     const std::string &type)
93 {
94     auto it = callbackInfos.find(type);
95     if (it != callbackInfos.end() && it->second != nullptr) {
96         HILOGD("repetition type %{public}s is register, old callbackInfo will be deleted.", type.c_str());
97         // as long as the type is same, callbackInfo will be covered.
98         uint32_t refCount = INVALID_REF_COUNT;
99         napi_value handlerTemp = nullptr;
100         napi_status status = napi_get_reference_value(it->second->env_, it->second->callback_, &handlerTemp);
101         if (status != napi_ok) {
102             HILOGE("napi_get_reference_value failed. napi status is %{public}d", status);
103             return;
104         }
105         // if handlerTemp exist clear it. no exist, memory leak safe
106         if (handlerTemp != nullptr) {
107             HILOGD("napi_get_reference_value succeed");
108             napi_reference_unref(it->second->env_, it->second->callback_, &refCount);
109             HILOGD("decrements the refernce count, refCount: %{public}d", refCount);
110             // other place like EventNotify before use will add refCount, happen refCount != 0,
111             // ensure other place copy the prepare covered callbackInfo、add refCount and unref and delete refCount
112             if (refCount == 0) {
113                 HILOGD("delete the reference");
114                 napi_delete_reference(it->second->env_, it->second->callback_);
115             }
116             HILOGD("old %{public}s is deleted", type.c_str());
117         } else {
118             HILOGI("napi_get_reference_value is nullptr");
119         }
120     }
121 }
122 
OnEvent(napi_env env,napi_callback_info info,std::map<std::string,std::shared_ptr<BluetoothCallbackInfo>> & callbackInfos)123 napi_value NapiEvent::OnEvent(napi_env env, napi_callback_info info,
124     std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> &callbackInfos)
125 {
126     size_t expectedArgsCount = ARGS_SIZE_TWO;
127     size_t argc = expectedArgsCount;
128     napi_value argv[ARGS_SIZE_TWO] = {0};
129     napi_value thisVar = nullptr;
130 
131     napi_value ret = nullptr;
132     napi_get_undefined(env, &ret);
133 
134     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
135     if (argc != expectedArgsCount) {
136         HILOGE("Requires 2 argument.");
137         return ret;
138     }
139     std::string type;
140     if (!ParseString(env, type, argv[PARAM0])) {
141         HILOGE("string expected.");
142         return ret;
143     }
144     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = std::make_shared<BluetoothCallbackInfo>();
145     callbackInfo->env_ = env;
146 
147     napi_valuetype valueType = napi_undefined;
148     napi_typeof(env, argv[PARAM1], &valueType);
149     if (valueType != napi_function) {
150         HILOGE("Wrong argument type. Function expected.");
151         return ret;
152     }
153 
154     UpdateCallbackInfo(callbackInfos, type);
155     napi_create_reference(env, argv[PARAM1], 1, &callbackInfo->callback_);
156     callbackInfos[type] = callbackInfo;
157     HILOGD("%{public}s is registered", type.c_str());
158     return ret;
159 }
160 
OffEvent(napi_env env,napi_callback_info info,std::map<std::string,std::shared_ptr<BluetoothCallbackInfo>> & callbackInfos)161 napi_value NapiEvent::OffEvent(napi_env env, napi_callback_info info,
162     std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> &callbackInfos)
163 {
164     size_t argc = ARGS_SIZE_ONE;
165     napi_value argv[ARGS_SIZE_TWO] = {0};
166     napi_value thisVar = nullptr;
167 
168     napi_value ret = nullptr;
169     napi_get_undefined(env, &ret);
170 
171     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
172     if (argc != ARGS_SIZE_ONE && argc != ARGS_SIZE_TWO) {
173         HILOGE("Requires 1 or 2 argument.");
174         return ret;
175     }
176     std::string type;
177     if (!ParseString(env, type, argv[PARAM0])) {
178         HILOGE("string expected.");
179         return ret;
180     }
181     auto it = callbackInfos.find(type);
182     if (it == callbackInfos.end() || it->second == nullptr) {
183         HILOGE("type %{public}s callbackInfos isn't exist.", type.c_str());
184         return ret;
185     }
186     if (env != it->second->env_) {
187         HILOGE("env doesn't match, please check.");
188         return ret;
189     }
190     napi_delete_reference(env, it->second->callback_);
191     it->second = nullptr;
192     HILOGI("%{public}s is unregistered", type.c_str());
193     return ret;
194 }
195 }  // namespace Bluetooth
196 }  // namespace OHOS