• 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 
16 #include "napi_bluetooth_ble_advertise_callback.h"
17 
18 #include "bluetooth_log.h"
19 #include "napi_async_work.h"
20 #include "napi_native_object.h"
21 #include "napi_bluetooth_utils.h"
22 #include "napi_bluetooth_ble_utils.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
NapiBluetoothBleAdvertiseCallback()26 NapiBluetoothBleAdvertiseCallback::NapiBluetoothBleAdvertiseCallback()
27     : eventSubscribe_(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, BT_MODULE_NAME)
28 {}
29 
GetInstance(void)30 std::shared_ptr<NapiBluetoothBleAdvertiseCallback> NapiBluetoothBleAdvertiseCallback::GetInstance(void)
31 {
32     static std::shared_ptr<NapiBluetoothBleAdvertiseCallback> instance =
33         std::make_shared<NapiBluetoothBleAdvertiseCallback>();
34     return instance;
35 }
36 
OnStartResultEvent(int result,int advHandle)37 void NapiBluetoothBleAdvertiseCallback::OnStartResultEvent(int result, int advHandle)
38 {
39     HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
40     auto nativeObject =
41         std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::STARTED));
42     eventSubscribe_.PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
43 }
44 
OnEnableResultEvent(int result,int advHandle)45 void NapiBluetoothBleAdvertiseCallback::OnEnableResultEvent(int result, int advHandle)
46 {
47     HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle);
48     auto nativeObject =
49         std::make_shared<NapiNativeAdvertisingStateInfo>(advHandle, static_cast<int>(AdvertisingState::ENABLED));
50     eventSubscribe_.PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
51 }
52 
OnDisableResultEvent(int result,int advHandle)53 void NapiBluetoothBleAdvertiseCallback::OnDisableResultEvent(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::DISABLED));
58     eventSubscribe_.PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
59 }
60 
OnStopResultEvent(int result,int advHandle)61 void NapiBluetoothBleAdvertiseCallback::OnStopResultEvent(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::STOPPED));
66     eventSubscribe_.PublishEvent(REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE, nativeObject);
67 }
68 
OnSetAdvDataEvent(int result)69 void NapiBluetoothBleAdvertiseCallback::OnSetAdvDataEvent(int result)
70 {}
71 
OnGetAdvHandleEvent(int result,int advHandle)72 void NapiBluetoothBleAdvertiseCallback::OnGetAdvHandleEvent(int result, int advHandle)
73 {
74     auto napiAdvHandle = std::make_shared<NapiNativeInt>(advHandle);
75     AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GET_ADVERTISING_HANDLE, napiAdvHandle, result);
76 }
77 }  // namespace Bluetooth
78 }  // namespace OHOS