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 result = GetSDKAdaptedStatusCode(result); // Adaptation for old sdk
46 auto napiAdvHandle = std::make_shared<NapiNativeInt>(advHandle);
47 AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GET_ADVERTISING_HANDLE, napiAdvHandle, result);
48 }
49 auto nativeObject =
50 std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::STARTED));
51 GetEventSubscribe()->PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
52 }
53
OnEnableResultEvent(int result,int advHandle)54 void NapiBluetoothBleAdvertiseCallback::OnEnableResultEvent(int result, int advHandle)
55 {
56 HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
57 if (advHandle_ == advHandle) {
58 HILOGI("OnEnableResultEvent, advHandle is same, advHandle: %{public}d", advHandle_);
59 result = GetSDKAdaptedStatusCode(result); // Adaptation for old sdk
60 auto napiAdvResult = std::make_shared<NapiNativeInt>(result);
61 AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::BLE_ENABLE_ADVERTISING, napiAdvResult, result);
62 }
63 auto nativeObject =
64 std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::ENABLED));
65 GetEventSubscribe()->PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
66 }
67
OnDisableResultEvent(int result,int advHandle)68 void NapiBluetoothBleAdvertiseCallback::OnDisableResultEvent(int result, int advHandle)
69 {
70 HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
71 if (advHandle_ == advHandle) {
72 HILOGI("OnDisableResultEvent, advHandle is same, advHandle: %{public}d", advHandle_);
73 result = GetSDKAdaptedStatusCode(result); // Adaptation for old sdk
74 auto napiAdvResult = std::make_shared<NapiNativeInt>(result);
75 AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::BLE_DISABLE_ADVERTISING, napiAdvResult, result);
76 }
77 auto nativeObject =
78 std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::DISABLED));
79 GetEventSubscribe()->PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
80 }
81
OnStopResultEvent(int result,int advHandle)82 void NapiBluetoothBleAdvertiseCallback::OnStopResultEvent(int result, int advHandle)
83 {
84 HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
85 if (advHandle_ == advHandle) {
86 HILOGI("OnStopResultEvent, advHandle is same, advHandle: %{public}d", advHandle_);
87 result = GetSDKAdaptedStatusCode(result); // Adaptation for old sdk
88 auto napiAdvResult = std::make_shared<NapiNativeInt>(result);
89 AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::BLE_STOP_ADVERTISING, napiAdvResult, result);
90 advHandle_ = -1;
91 }
92 auto nativeObject =
93 std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::STOPPED));
94 GetEventSubscribe()->PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
95 }
96
OnSetAdvDataEvent(int result)97 void NapiBluetoothBleAdvertiseCallback::OnSetAdvDataEvent(int result)
98 {}
99
OnGetAdvHandleEvent(int result,int advHandle)100 void NapiBluetoothBleAdvertiseCallback::OnGetAdvHandleEvent(int result, int advHandle)
101 {
102 advHandle_ = advHandle;
103 }
104
OnChangeAdvResultEvent(int result,int advHandle)105 void NapiBluetoothBleAdvertiseCallback::OnChangeAdvResultEvent(int result, int advHandle)
106 {}
107 } // namespace Bluetooth
108 } // namespace OHOS