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