1/*
2 * Copyright (C) 2024 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 "oh_bluetooth.h"
17
18#include "bluetooth_log.h"
19#include "bluetooth_host.h"
20
21#ifdef __cplusplus
22extern "C" {
23#endif
24
25namespace OHOS {
26namespace Bluetooth {
27
28Bluetooth_ResultCode OH_Bluetooth_GetBluetoothSwitchState(Bluetooth_SwitchState *state)
29{
30    if (!state) {
31        HILOGE("invalid param");
32        return Bluetooth_ResultCode::BLUETOOTH_INVALID_PARAM;
33    }
34    BluetoothHost *host = &BluetoothHost::GetDefaultHost();
35    int32_t btState = BTStateID::STATE_TURN_OFF;
36    host->GetBtState(btState);
37
38    Bluetooth_SwitchState status = Bluetooth_SwitchState::BLUETOOTH_STATE_OFF;
39    switch (btState) {
40        case BTStateID::STATE_TURNING_ON:
41            status = Bluetooth_SwitchState::BLUETOOTH_STATE_TURNING_ON;
42            break;
43        case BTStateID::STATE_TURN_ON:
44            status = Bluetooth_SwitchState::BLUETOOTH_STATE_ON;
45            break;
46        case BTStateID::STATE_TURNING_OFF:
47            status = Bluetooth_SwitchState::BLUETOOTH_STATE_TURNING_OFF;
48            break;
49        case BTStateID::STATE_TURN_OFF:
50            status = Bluetooth_SwitchState::BLUETOOTH_STATE_OFF;
51            break;
52        default:
53            HILOGE("get state failed");
54            break;
55    }
56
57    bool enableBle = host->IsBleEnabled();
58    if (enableBle && (btState == BTStateID::STATE_TURN_OFF)) {
59        status = Bluetooth_SwitchState::BLUETOOTH_STATE_BLE_ON;
60    } else if (!enableBle && (btState == BTStateID::STATE_TURN_OFF)) {
61        status = Bluetooth_SwitchState::BLUETOOTH_STATE_OFF;
62    }
63    *state = status;
64    return Bluetooth_ResultCode::BLUETOOTH_SUCCESS;
65}
66
67} // namespace Bluetooth
68} // namespace OHOS
69
70#ifdef __cplusplus
71}
72#endif
73