1 /*
2  * Copyright (C) 2021 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_BLUETOOTH_GATT_SERVER_H_
17 #define NAPI_BLUETOOTH_GATT_SERVER_H_
18 
19 #include <vector>
20 #include "bluetooth_gatt_server.h"
21 #include "bluetooth_log.h"
22 #include "napi_bluetooth_gatt_server_callback.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 class NapiGattServer {
27 
28 public:
29     static napi_value CreateGattServer(napi_env env, napi_callback_info info);
30     static void DefineGattServerJSClass(napi_env env);
31     static napi_value GattServerConstructor(napi_env env, napi_callback_info info);
32 
33     static napi_value On(napi_env env, napi_callback_info info);
34     static napi_value Off(napi_env env, napi_callback_info info);
35 
36     static napi_value AddService(napi_env env, napi_callback_info info);
37     static napi_value Close(napi_env env, napi_callback_info info);
38     static napi_value RemoveGattService(napi_env env, napi_callback_info info);
39     static napi_value SendResponse(napi_env env, napi_callback_info info);
40 
41 #ifdef BLUETOOTH_API_SINCE_10
42     static napi_value NotifyCharacteristicChangedEx(napi_env env, napi_callback_info info);
43 #else
44     static napi_value NotifyCharacteristicChanged(napi_env env, napi_callback_info info);
45 #endif
46 
GetServer()47     std::shared_ptr<GattServer> &GetServer()
48     {
49         return server_;
50     }
GetCallback()51     std::shared_ptr<NapiGattServerCallback> GetCallback()
52     {
53         return callback_;
54     }
55     static std::vector<std::string> deviceList_;
56     static std::mutex deviceListMutex_;
57 
NapiGattServer()58     NapiGattServer()
59     {
60         HILOGI("enter");
61         callback_ = std::make_shared<NapiGattServerCallback>();
62         std::shared_ptr<GattServerCallback> tmp = std::static_pointer_cast<GattServerCallback>(callback_);
63         server_ = GattServer::CreateInstance(tmp);
64     }
65     ~NapiGattServer() = default;
66 
67     static thread_local napi_ref consRef_;
68 
69 private:
70     std::shared_ptr<GattServer> server_ = nullptr;
71     std::shared_ptr<NapiGattServerCallback> callback_;
72 };
73 }  // namespace Bluetooth
74 }  // namespace OHOS
75 #endif /* NAPI_BLUETOOTH_GATT_SERVER_H_ */
76