• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #ifndef NAPI_BLUETOOTH_EVENT_H_
16 #define NAPI_BLUETOOTH_EVENT_H_
17 
18 #include <string>
19 #include <map>
20 #include <napi_bluetooth_utils.h>
21 
22 namespace OHOS {
23 namespace Bluetooth {
24 
25 class AsyncEventData : public BluetoothCallbackInfo {
26 public:
27     std::function<napi_value ()> packResult;
28 
AsyncEventData(const std::shared_ptr<BluetoothCallbackInfo> & cb,std::function<napi_value ()> p)29     explicit AsyncEventData(const std::shared_ptr<BluetoothCallbackInfo> &cb, std::function<napi_value ()> p)
30     {
31         this->env_ = cb->env_;
32         this->callback_ = cb->callback_;
33         this->state_ = cb->state_;
34         this->deviceId_ = cb->deviceId_;
35         this->info_ = cb->info_;
36         packResult = p;
37     }
38 
39     AsyncEventData() = delete;
40 
~AsyncEventData()41     virtual ~AsyncEventData() {
42     }
43 };
44 
45 class NapiEvent {
46 public:
47     static napi_value CreateResult(const std::shared_ptr<BluetoothCallbackInfo> &cb, int value);
48     static napi_value CreateResult(const std::shared_ptr<BluetoothCallbackInfo> &cb,
49         BluetoothOppTransferInformation &information);
50     static napi_value CreateResult(const std::shared_ptr<BluetoothCallbackInfo> &cb,
51         GattCharacteristic &characteristic);
52     static napi_value OnEvent(napi_env env, napi_callback_info cbinfo,
53         std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> &callbackInfos);
54     static napi_value OffEvent(napi_env env, napi_callback_info cbinfo,
55         std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> &callbackInfos);
56     static void EventNotify(AsyncEventData *asyncEvent);
57 
58     template<typename T>
CheckAndNotify(const std::shared_ptr<BluetoothCallbackInfo> & cb,const T & obj)59     static void CheckAndNotify(const std::shared_ptr<BluetoothCallbackInfo> &cb, const T& obj)
60     {
61         if (cb == nullptr) {
62             HILOGI("checkAndNotify event cb is nullptr!");
63             return;
64         }
65         auto func = std::bind(
66             [](const std::shared_ptr<BluetoothCallbackInfo> &cb, T& obj) -> napi_value {
67                 return NapiEvent::CreateResult(cb, obj);
68             },
69             cb, obj);
70         AsyncEventData *asyncEvent = new (std::nothrow)AsyncEventData(cb, func);
71         if (asyncEvent == nullptr) {
72             HILOGI("asyncEvent is nullptr!");
73             return;
74         }
75         EventNotify(asyncEvent);
76     }
77 };
78 
79 }  // namespace Bluetooth
80 }  // namespace OHOS
81 #endif /* NAPI_BLUETOOTH_EVENT_H_ */