195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
495489c19Sopenharmony_ci * you may not use this file except in compliance with the License.
595489c19Sopenharmony_ci * You may obtain a copy of the License at
695489c19Sopenharmony_ci *
795489c19Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
895489c19Sopenharmony_ci *
995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and
1395489c19Sopenharmony_ci * limitations under the License.
1495489c19Sopenharmony_ci */
1595489c19Sopenharmony_ci
1695489c19Sopenharmony_ci#ifndef BLUETOOTH_SWITCH_MODULE_H
1795489c19Sopenharmony_ci#define BLUETOOTH_SWITCH_MODULE_H
1895489c19Sopenharmony_ci
1995489c19Sopenharmony_ci#include <atomic>
2095489c19Sopenharmony_ci#include <functional>
2195489c19Sopenharmony_ci#include <memory>
2295489c19Sopenharmony_ci#include <mutex>
2395489c19Sopenharmony_ci#include <vector>
2495489c19Sopenharmony_ci#include "ffrt_inner.h"
2595489c19Sopenharmony_ci
2695489c19Sopenharmony_cinamespace OHOS {
2795489c19Sopenharmony_cinamespace Bluetooth {
2895489c19Sopenharmony_ciclass IBluetoothSwitchAction {
2995489c19Sopenharmony_cipublic:
3095489c19Sopenharmony_ci    IBluetoothSwitchAction() = default;
3195489c19Sopenharmony_ci    virtual ~IBluetoothSwitchAction() = default;
3295489c19Sopenharmony_ci
3395489c19Sopenharmony_ci    virtual int EnableBluetooth(void) = 0;
3495489c19Sopenharmony_ci    virtual int DisableBluetooth(void) = 0;
3595489c19Sopenharmony_ci    virtual int EnableBluetoothToRestrictMode(void) = 0;
3695489c19Sopenharmony_ci};
3795489c19Sopenharmony_ci
3895489c19Sopenharmony_cienum class BluetoothSwitchEvent : int {
3995489c19Sopenharmony_ci    NONE = -1,
4095489c19Sopenharmony_ci    ENABLE_BLUETOOTH = 0,
4195489c19Sopenharmony_ci    DISABLE_BLUETOOTH,
4295489c19Sopenharmony_ci    ENABLE_BLUETOOTH_TO_RESTRICE_MODE,
4395489c19Sopenharmony_ci    BLUETOOTH_ON,
4495489c19Sopenharmony_ci    BLUETOOTH_OFF,
4595489c19Sopenharmony_ci    BLUETOOTH_HALF,
4695489c19Sopenharmony_ci};
4795489c19Sopenharmony_ci
4895489c19Sopenharmony_ciclass BluetoothSwitchModule : public std::enable_shared_from_this<BluetoothSwitchModule> {
4995489c19Sopenharmony_cipublic:
5095489c19Sopenharmony_ci    explicit BluetoothSwitchModule(std::unique_ptr<IBluetoothSwitchAction> switchAction)
5195489c19Sopenharmony_ci        : ffrtQueue_("bt_switch"), switchAction_(std::move(switchAction)) {}
5295489c19Sopenharmony_ci    ~BluetoothSwitchModule() = default;
5395489c19Sopenharmony_ci
5495489c19Sopenharmony_ci    int ProcessBluetoothSwitchEvent(BluetoothSwitchEvent event);
5595489c19Sopenharmony_ci
5695489c19Sopenharmony_ciprivate:
5795489c19Sopenharmony_ci    int ProcessEnableBluetoothEvent(void);
5895489c19Sopenharmony_ci    int ProcessDisableBluetoothEvent(void);
5995489c19Sopenharmony_ci    int ProcessEnableBluetoothToRestrictModeEvent(void);
6095489c19Sopenharmony_ci    int ProcessBluetoothOnEvent(void);
6195489c19Sopenharmony_ci    int ProcessBluetoothOffEvent(void);
6295489c19Sopenharmony_ci    int ProcessBluetoothHalfEvent(void);
6395489c19Sopenharmony_ci    int ProcessBluetoothSwitchAction(std::function<int(void)> action, BluetoothSwitchEvent cachedEvent);
6495489c19Sopenharmony_ci    int ProcessBluetoothSwitchCachedEvent(BluetoothSwitchEvent event);
6595489c19Sopenharmony_ci    int ProcessBluetoothSwitchActionEnd(
6695489c19Sopenharmony_ci        BluetoothSwitchEvent curSwitchActionEvent, std::vector<BluetoothSwitchEvent> expectedEventVec);
6795489c19Sopenharmony_ci    void DeduplicateCacheEvent(BluetoothSwitchEvent curEvent);
6895489c19Sopenharmony_ci    void LogCacheEventIgnored(std::vector<BluetoothSwitchEvent> eventVec);
6995489c19Sopenharmony_ci    void LogBluetoothSwitchEvent(BluetoothSwitchEvent event);
7095489c19Sopenharmony_ci    void OnTaskTimeout(void);
7195489c19Sopenharmony_ci
7295489c19Sopenharmony_ci    const uint64_t DEFAULT_TASK_TIMEOUT = 10000000;  // 10s
7395489c19Sopenharmony_ci    uint64_t taskTimeout_ = DEFAULT_TASK_TIMEOUT;
7495489c19Sopenharmony_ci    ffrt::task_handle taskTimeoutHandle_;
7595489c19Sopenharmony_ci    ffrt::queue ffrtQueue_;
7695489c19Sopenharmony_ci
7795489c19Sopenharmony_ci    std::unique_ptr<IBluetoothSwitchAction> switchAction_ { nullptr };
7895489c19Sopenharmony_ci    std::atomic_bool isBtSwitchProcessing_ { false };
7995489c19Sopenharmony_ci    std::vector<BluetoothSwitchEvent> cachedEventVec_ {};
8095489c19Sopenharmony_ci    std::mutex bluetoothSwitchEventMutex_ {};  // Used for ProcessBluetoothSwitchEvent function
8195489c19Sopenharmony_ci};
8295489c19Sopenharmony_ci}  // namespace Bluetooth
8395489c19Sopenharmony_ci}  // namespace OHOS
8495489c19Sopenharmony_ci#endif  // BLUETOOTH_SWITCH_MODULE_H
85