• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 
16 #ifndef NAPI_ASYNC_WORK_H
17 #define NAPI_ASYNC_WORK_H
18 
19 #include <memory>
20 #include <mutex>
21 #include "napi_bluetooth_utils.h"
22 #include "napi_native_object.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 struct NapiAsyncCallback;
27 
28 enum NapiAsyncType : int {
29     GATT_CLIENT_READ_CHARACTER,
30     GATT_CLIENT_READ_REMOTE_RSSI_VALUE,
31     GATT_CLIENT_READ_DESCRIPTOR,
32     GATT_CLIENT_WRITE_CHARACTER,
33     GATT_CLIENT_WRITE_DESCRIPTOR,
34     GATT_CLIENT_ENABLE_CHARACTER_CHANGED,
35     GATT_SERVER_NOTIFY_CHARACTERISTIC,
36     GET_ADVERTISING_HANDLE,
37     BLE_STOP_ADVERTISING,
38     BLE_START_SCAN,
39     BLE_STOP_SCAN,
40     BLE_ENABLE_ADVERTISING,
41     BLE_DISABLE_ADVERTISING
42 };
43 
44 static constexpr bool ASYNC_WORK_NEED_CALLBACK = true;
45 static constexpr bool ASYNC_WORK_NO_NEED_CALLBACK = false;
46 
47 struct NapiAsyncWorkRet {
NapiAsyncWorkRetNapiAsyncWorkRet48     NapiAsyncWorkRet(int errCode) : errCode(errCode) {}
NapiAsyncWorkRetNapiAsyncWorkRet49     NapiAsyncWorkRet(int errCode, std::shared_ptr<NapiNativeObject> object)
50         : errCode(errCode), object(std::move(object)) {}
51 
52     int errCode = -1;
53     std::shared_ptr<NapiNativeObject> object = nullptr;
54 };
55 
56 class NapiAsyncWork : public std::enable_shared_from_this<NapiAsyncWork> {
57 public:
58     NapiAsyncWork(napi_env env, std::function<NapiAsyncWorkRet(void)> func,
59         std::shared_ptr<NapiAsyncCallback> asyncCallback, bool needCallback = false)
env_(env)60         : env_(env), func_(func), napiAsyncCallback_(asyncCallback), needCallback_(needCallback) {}
61     ~NapiAsyncWork() = default;
62 
63     void Run(void);
64     void CallFunction(int errorCode, std::shared_ptr<NapiNativeObject> object);
65     napi_value GetRet(void);
66 
67     struct Info {
68         void Execute(void);
69         void Complete(void);
70 
71         int errCode = -1;
72         bool needCallback = false;
73         napi_async_work asyncWork;
74         std::shared_ptr<NapiNativeObject> object;
75         std::shared_ptr<NapiAsyncWork> napiAsyncWork = nullptr;
76     };
77 
78 private:
79     friend class NapiAsyncWorkMap;
80 
81     void TimeoutCallback(void);
82 
83     napi_env env_;
84     uint32_t timerId_ = 0;  // Is used to reference a timer.
85     std::function<NapiAsyncWorkRet(void)> func_;
86     std::shared_ptr<NapiAsyncCallback> napiAsyncCallback_ = nullptr;
87     std::atomic_bool needCallback_ = false; // Indicates whether an asynchronous work needs to wait for callback.
88     std::atomic_bool triggered_ = false; // Indicates whether the asynchronous callback is called.
89 };
90 
91 class NapiAsyncWorkFactory {
92 public:
93     static std::shared_ptr<NapiAsyncWork> CreateAsyncWork(napi_env env, napi_callback_info info,
94         std::function<NapiAsyncWorkRet(void)> asyncWork, bool needCallback = ASYNC_WORK_NO_NEED_CALLBACK);
95 };
96 
97 class NapiAsyncWorkMap {
98 public:
99     bool TryPush(NapiAsyncType type, std::shared_ptr<NapiAsyncWork> asyncWork);
100     void Erase(NapiAsyncType type);
101     std::shared_ptr<NapiAsyncWork> Get(NapiAsyncType type);
102 
103 private:
104     mutable std::mutex mutex_ {};
105     std::map<int, std::shared_ptr<NapiAsyncWork>> map_ {};
106 };
107 
108 void AsyncWorkCallFunction(NapiAsyncWorkMap &map, NapiAsyncType type, std::shared_ptr<NapiNativeObject> nativeObject,
109     int status);
110 
111 }  // namespace Bluetooth
112 }  // namespace OHOS
113 #endif // NAPI_ASYNC_WORK_H