• 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_napi_ble_advertise_callback"
17 #endif
18 
19 #include "napi_bluetooth_ble_advertise_callback.h"
20 
21 #include "bluetooth_log.h"
22 #include "napi_async_work.h"
23 #include "napi_native_object.h"
24 #include "napi_bluetooth_utils.h"
25 #include "napi_bluetooth_ble_utils.h"
26 #include "napi_bluetooth_ble.h"
27 
28 namespace OHOS {
29 namespace Bluetooth {
NapiBluetoothBleAdvertiseCallback()30 NapiBluetoothBleAdvertiseCallback::NapiBluetoothBleAdvertiseCallback()
31 {}
32 
GetInstance(void)33 std::shared_ptr<NapiBluetoothBleAdvertiseCallback> NapiBluetoothBleAdvertiseCallback::GetInstance(void)
34 {
35     static std::shared_ptr<NapiBluetoothBleAdvertiseCallback> instance =
36         std::make_shared<NapiBluetoothBleAdvertiseCallback>();
37     return instance;
38 }
39 
OnStartResultEvent(int result,int advHandle)40 void NapiBluetoothBleAdvertiseCallback::OnStartResultEvent(int result, int advHandle)
41 {
42     HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
43     if (advHandle_ == advHandle) {
44         HILOGI("OnStartResultEvent, advHandle is same, advHandle: %{public}d", advHandle_);
45         auto napiAdvHandle = std::make_shared<NapiNativeInt>(advHandle);
46         AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GET_ADVERTISING_HANDLE, napiAdvHandle, result);
47     }
48     auto nativeObject =
49         std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::STARTED));
50     GetEventSubscribe()->PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
51 }
52 
OnEnableResultEvent(int result,int advHandle)53 void NapiBluetoothBleAdvertiseCallback::OnEnableResultEvent(int result, int advHandle)
54 {
55     HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
56     if (advHandle_ == advHandle) {
57         HILOGI("OnEnableResultEvent, advHandle is same, advHandle: %{public}d", advHandle_);
58         auto napiAdvResult = std::make_shared<NapiNativeInt>(result);
59         AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::BLE_ENABLE_ADVERTISING, napiAdvResult, result);
60     }
61     auto nativeObject =
62         std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::ENABLED));
63     GetEventSubscribe()->PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
64 }
65 
OnDisableResultEvent(int result,int advHandle)66 void NapiBluetoothBleAdvertiseCallback::OnDisableResultEvent(int result, int advHandle)
67 {
68     HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
69     if (advHandle_ == advHandle) {
70         HILOGI("OnDisableResultEvent, advHandle is same, advHandle: %{public}d", advHandle_);
71         auto napiAdvResult = std::make_shared<NapiNativeInt>(result);
72         AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::BLE_DISABLE_ADVERTISING, napiAdvResult, result);
73     }
74     auto nativeObject =
75         std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::DISABLED));
76     GetEventSubscribe()->PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
77 }
78 
OnStopResultEvent(int result,int advHandle)79 void NapiBluetoothBleAdvertiseCallback::OnStopResultEvent(int result, int advHandle)
80 {
81     HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
82     if (advHandle_ == advHandle) {
83         HILOGI("OnStopResultEvent, advHandle is same, advHandle: %{public}d", advHandle_);
84         auto napiAdvResult = std::make_shared<NapiNativeInt>(result);
85         AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::BLE_STOP_ADVERTISING, napiAdvResult, result);
86         advHandle_ = -1;
87     }
88     auto nativeObject =
89         std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::STOPPED));
90     GetEventSubscribe()->PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
91 }
92 
OnSetAdvDataEvent(int result)93 void NapiBluetoothBleAdvertiseCallback::OnSetAdvDataEvent(int result)
94 {}
95 
OnGetAdvHandleEvent(int result,int advHandle)96 void NapiBluetoothBleAdvertiseCallback::OnGetAdvHandleEvent(int result, int advHandle)
97 {
98     advHandle_ = advHandle;
99 }
100 
OnChangeAdvResultEvent(int result,int advHandle)101 void NapiBluetoothBleAdvertiseCallback::OnChangeAdvResultEvent(int result, int advHandle)
102 {}
103 }  // namespace Bluetooth
104 }  // namespace OHOS