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