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 };
39 
40 static constexpr bool ASYNC_WORK_NEED_CALLBACK = true;
41 static constexpr bool ASYNC_WORK_NO_NEED_CALLBACK = false;
42 
43 struct NapiAsyncWorkRet {
NapiAsyncWorkRetNapiAsyncWorkRet44     NapiAsyncWorkRet(int errCode) : errCode(errCode) {}
NapiAsyncWorkRetNapiAsyncWorkRet45     NapiAsyncWorkRet(int errCode, std::shared_ptr<NapiNativeObject> object)
46         : errCode(errCode), object(std::move(object)) {}
47 
48     int errCode = -1;
49     std::shared_ptr<NapiNativeObject> object = nullptr;
50 };
51 
52 class NapiAsyncWork : public std::enable_shared_from_this<NapiAsyncWork> {
53 public:
54     NapiAsyncWork(napi_env env, std::function<NapiAsyncWorkRet(void)> func,
55         std::shared_ptr<NapiAsyncCallback> asyncCallback, bool needCallback = false)
56         : env_(env), func_(func), napiAsyncCallback_(asyncCallback), needCallback_(needCallback) {}
57     ~NapiAsyncWork() = default;
58 
59     void Run(void);
60     void CallFunction(int errorCode, std::shared_ptr<NapiNativeObject> object);
61     napi_value GetRet(void);
62 
63     struct Info {
64         void Execute(void);
65         void Complete(void);
66 
67         int errCode = -1;
68         bool needCallback = false;
69         napi_async_work asyncWork;
70         std::shared_ptr<NapiNativeObject> object;
71         std::shared_ptr<NapiAsyncWork> napiAsyncWork = nullptr;
72     };
73 
74 private:
75     friend class NapiAsyncWorkMap;
76 
77     void TimeoutCallback(void);
78 
79     napi_env env_;
80     uint32_t timerId_ = 0;  // Is used to reference a timer.
81     std::function<NapiAsyncWorkRet(void)> func_;
82     std::shared_ptr<NapiAsyncCallback> napiAsyncCallback_ = nullptr;
83     std::atomic_bool needCallback_ = false; // Indicates whether an asynchronous work needs to wait for callback.
84     std::atomic_bool triggered_ = false; // Indicates whether the asynchronous callback is called.
85 };
86 
87 class NapiAsyncWorkFactory {
88 public:
89     static std::shared_ptr<NapiAsyncWork> CreateAsyncWork(napi_env env, napi_callback_info info,
90         std::function<NapiAsyncWorkRet(void)> asyncWork, bool needCallback = ASYNC_WORK_NO_NEED_CALLBACK);
91 };
92 
93 class NapiAsyncWorkMap {
94 public:
95     bool TryPush(NapiAsyncType type, std::shared_ptr<NapiAsyncWork> asyncWork);
96     void Erase(NapiAsyncType type);
97     std::shared_ptr<NapiAsyncWork> Get(NapiAsyncType type);
98 
99 private:
100     mutable std::mutex mutex_ {};
101     std::map<int, std::shared_ptr<NapiAsyncWork>> map_ {};
102 };
103 
104 void AsyncWorkCallFunction(NapiAsyncWorkMap &map, NapiAsyncType type, std::shared_ptr<NapiNativeObject> nativeObject,
105     int status);
106 
107 }  // namespace Bluetooth
108 }  // namespace OHOS
109 #endif // NAPI_ASYNC_WORK_H