• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <uv.h>
16 #include "bluetooth_utils.h"
17 #include "napi_bluetooth_event.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
EventNotify(AsyncEventData * asyncEvent)21 void NapiEvent::EventNotify(AsyncEventData *asyncEvent)
22 {
23     HILOGI("Enter bluetooth event notify");
24 
25     if (asyncEvent == nullptr || asyncEvent->callback_ == nullptr) {
26         HILOGI("asyncEvent callback_ is null.");
27         return;
28     }
29     uv_loop_s *loop = nullptr;
30     napi_get_uv_event_loop(asyncEvent->env_, &loop);
31 
32     uv_work_t *work = new uv_work_t;
33     if (work == nullptr) {
34         HILOGI("uv_work_t work is null.");
35         delete asyncEvent;
36         asyncEvent = nullptr;
37         return;
38     }
39     uint32_t refCount = INVALID_REF_COUNT;
40     napi_reference_ref(asyncEvent->env_, asyncEvent->callback_, &refCount);
41     HILOGI("increments the reference count, refCount: %{public}d", refCount);
42     work->data = asyncEvent;
43 
44     uv_queue_work(
45         loop,
46         work,
47         [](uv_work_t *work) {},
48         [](uv_work_t *work, int status) {
49             AsyncEventData *callbackInfo = static_cast<AsyncEventData*>(work->data);
50             napi_value result = nullptr;
51             napi_value callback = nullptr;
52             napi_value undefined = nullptr;
53             napi_value callResult = nullptr;
54             napi_get_undefined(callbackInfo->env_, &undefined);
55             result = callbackInfo->packResult();
56             napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
57             if (callback != nullptr) {
58                 HILOGI("napi_call_function called");
59                 napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
60             }
61             uint32_t refCount = INVALID_REF_COUNT;
62             napi_reference_unref(callbackInfo->env_, callbackInfo->callback_, &refCount);
63             HILOGI("uv_queue_work unref, refCount: %{public}d", refCount);
64             if (refCount == 0) {
65                 napi_delete_reference(callbackInfo->env_, callbackInfo->callback_);
66             }
67             delete callbackInfo;
68             delete work;
69             work = nullptr;
70         }
71     );
72 }
73 
CreateResult(const std::shared_ptr<BluetoothCallbackInfo> & cb,int value)74 napi_value NapiEvent::CreateResult(const std::shared_ptr<BluetoothCallbackInfo> &cb, int value)
75 {
76     napi_value result = nullptr;
77     napi_create_object(cb->env_, &result);
78     ConvertStateChangeParamToJS(cb->env_, result, cb->deviceId_, value);
79     return result;
80 }
81 
CreateResult(const std::shared_ptr<BluetoothCallbackInfo> & cb,BluetoothOppTransferInformation & information)82 napi_value NapiEvent::CreateResult(const std::shared_ptr<BluetoothCallbackInfo> &cb,
83     BluetoothOppTransferInformation &information)
84 {
85     napi_value result = nullptr;
86     napi_create_object(cb->env_, &result);
87     ConvertOppTransferInformationToJS(cb->env_, result, information);
88     return result;
89 }
90 
CreateResult(const std::shared_ptr<BluetoothCallbackInfo> & cb,GattCharacteristic & characteristic)91 napi_value NapiEvent::CreateResult(const std::shared_ptr<BluetoothCallbackInfo> &cb,
92     GattCharacteristic &characteristic)
93 {
94     napi_value result = nullptr;
95     napi_create_object(cb->env_, &result);
96     ConvertBLECharacteristicToJS(cb->env_, result, characteristic);
97     return result;
98 }
99 
OnEvent(napi_env env,napi_callback_info info,std::map<std::string,std::shared_ptr<BluetoothCallbackInfo>> & callbackInfos)100 napi_value NapiEvent::OnEvent(napi_env env, napi_callback_info info,
101     std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> &callbackInfos)
102 {
103     size_t expectedArgsCount = ARGS_SIZE_TWO;
104     size_t argc = expectedArgsCount;
105     napi_value argv[ARGS_SIZE_TWO] = {0};
106     napi_value thisVar = nullptr;
107 
108     napi_value ret = nullptr;
109     napi_get_undefined(env, &ret);
110 
111     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
112     if (argc != expectedArgsCount) {
113         HILOGE("Requires 2 argument.");
114         return ret;
115     }
116     std::string type;
117     if (!ParseString(env, type, argv[PARAM0])) {
118         HILOGE("string expected.");
119         return ret;
120     }
121     std::shared_ptr<BluetoothCallbackInfo> callbackInfo = std::make_shared<BluetoothCallbackInfo>();
122     callbackInfo->env_ = env;
123 
124     napi_valuetype valueType = napi_undefined;
125     napi_typeof(env, argv[PARAM1], &valueType);
126     if (valueType != napi_function) {
127         HILOGE("Wrong argument type. Function expected.");
128         return ret;
129     }
130     napi_create_reference(env, argv[PARAM1], 1, &callbackInfo->callback_);
131     callbackInfos[type] = callbackInfo;
132     HILOGI("%{public}s is registered", type.c_str());
133     return ret;
134 }
135 
OffEvent(napi_env env,napi_callback_info info,std::map<std::string,std::shared_ptr<BluetoothCallbackInfo>> & callbackInfos)136 napi_value NapiEvent::OffEvent(napi_env env, napi_callback_info info,
137     std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> &callbackInfos)
138 {
139     size_t argc = ARGS_SIZE_ONE;
140     napi_value argv[ARGS_SIZE_TWO] = {0};
141     napi_value thisVar = nullptr;
142 
143     napi_value ret = nullptr;
144     napi_get_undefined(env, &ret);
145 
146     napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
147     if (argc != ARGS_SIZE_ONE && argc != ARGS_SIZE_TWO) {
148         HILOGE("Requires 1 or 2 argument.");
149         return ret;
150     }
151     std::string type;
152     if (!ParseString(env, type, argv[PARAM0])) {
153         HILOGE("string expected.");
154         return ret;
155     }
156     auto it = callbackInfos.find(type);
157     if (it == callbackInfos.end() || it->second == nullptr) {
158         HILOGE("type %{public}s callbackInfos isn't exist.", type.c_str());
159         return ret;
160     }
161 
162     uint32_t refCount = INVALID_REF_COUNT;
163     napi_reference_unref(env, it->second->callback_, &refCount);
164     HILOGI("decrements the refernce count, refCount: %{public}d", refCount);
165     if (refCount == 0) {
166         napi_delete_reference(env, it->second->callback_);
167     }
168     it->second = nullptr;
169     HILOGI("%{public}s is unregistered", type.c_str());
170     return ret;
171 }
172 }  // namespace Bluetooth
173 }  // namespace OHOS