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
27 namespace OHOS {
28 namespace Bluetooth {
NapiBluetoothBleAdvertiseCallback()29 NapiBluetoothBleAdvertiseCallback::NapiBluetoothBleAdvertiseCallback()
30 : eventSubscribe_(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, BT_MODULE_NAME)
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 eventSubscribe_.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 auto nativeObject =
57 std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::ENABLED));
58 eventSubscribe_.PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
59 }
60
OnDisableResultEvent(int result,int advHandle)61 void NapiBluetoothBleAdvertiseCallback::OnDisableResultEvent(int result, int advHandle)
62 {
63 HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
64 auto nativeObject =
65 std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::DISABLED));
66 eventSubscribe_.PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
67 }
68
OnStopResultEvent(int result,int advHandle)69 void NapiBluetoothBleAdvertiseCallback::OnStopResultEvent(int result, int advHandle)
70 {
71 HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
72 if (advHandle_ == advHandle) {
73 HILOGI("OnStopResultEvent, advHandle is same, advHandle: %{public}d", advHandle_);
74 auto napiAdvResult = std::make_shared<NapiNativeInt>(result);
75 AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::BLE_STOP_ADVERTISING, napiAdvResult, result);
76 advHandle_ = -1;
77 }
78 auto nativeObject =
79 std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::STOPPED));
80 eventSubscribe_.PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
81 }
82
OnSetAdvDataEvent(int result)83 void NapiBluetoothBleAdvertiseCallback::OnSetAdvDataEvent(int result)
84 {}
85
OnGetAdvHandleEvent(int result,int advHandle)86 void NapiBluetoothBleAdvertiseCallback::OnGetAdvHandleEvent(int result, int advHandle)
87 {
88 advHandle_ = advHandle;
89 }
90 } // namespace Bluetooth
91 } // namespace OHOS