• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef BLUETOOTH_SWITCH_MODULE_H
17 #define BLUETOOTH_SWITCH_MODULE_H
18 
19 #include <atomic>
20 #include <functional>
21 #include <memory>
22 #include <mutex>
23 #include <vector>
24 #include "ffrt_inner.h"
25 
26 namespace OHOS {
27 namespace Bluetooth {
28 class IBluetoothSwitchAction {
29 public:
30     IBluetoothSwitchAction() = default;
31     virtual ~IBluetoothSwitchAction() = default;
32 
33     virtual int EnableBluetooth(bool, bool) = 0;
34     virtual int DisableBluetooth(bool) = 0;
35     virtual int EnableBluetoothToRestrictMode(void) = 0;
36 };
37 
38 enum class BluetoothSwitchEvent : int {
39     NONE = -1,
40     ENABLE_BLUETOOTH = 0,
41     DISABLE_BLUETOOTH,
42     ENABLE_BLUETOOTH_TO_RESTRICE_MODE,
43     BLUETOOTH_ON,
44     BLUETOOTH_OFF,
45     BLUETOOTH_HALF,
46 };
47 
48 class BluetoothSwitchModule : public std::enable_shared_from_this<BluetoothSwitchModule> {
49 public:
BluetoothSwitchModule(std::unique_ptr<IBluetoothSwitchAction> switchAction)50     explicit BluetoothSwitchModule(std::unique_ptr<IBluetoothSwitchAction> switchAction)
51         : ffrtQueue_("bt_switch"), switchAction_(std::move(switchAction)) {}
52     ~BluetoothSwitchModule() = default;
53 
54     int ProcessBluetoothSwitchEvent(BluetoothSwitchEvent event, bool isAsync = false);
55     void SetNoAutoConnect(bool noAutoConnect);
56 
57 private:
58     int ProcessEnableBluetoothEvent(bool isAsync = false);
59     int ProcessDisableBluetoothEvent(bool isAsync = false);
60     int ProcessEnableBluetoothToRestrictModeEvent(void);
61     int ProcessBluetoothOnEvent(void);
62     int ProcessBluetoothOffEvent(void);
63     int ProcessBluetoothHalfEvent(void);
64     int ProcessBluetoothSwitchAction(std::function<int(void)> action, BluetoothSwitchEvent cachedEvent);
65     int ProcessBluetoothSwitchCachedEvent(BluetoothSwitchEvent event);
66     int ProcessBluetoothSwitchActionEnd(
67         BluetoothSwitchEvent curSwitchActionEvent, std::vector<BluetoothSwitchEvent> expectedEventVec);
68     void DeduplicateCacheEvent(BluetoothSwitchEvent curEvent);
69     void LogCacheEventIgnored(std::vector<BluetoothSwitchEvent> eventVec);
70     void LogBluetoothSwitchEvent(BluetoothSwitchEvent event);
71     void OnTaskTimeout(void);
72 
73     const uint64_t DEFAULT_TASK_TIMEOUT = 10000000;  // 10s
74     uint64_t taskTimeout_ = DEFAULT_TASK_TIMEOUT;
75     ffrt::task_handle taskTimeoutHandle_;
76     ffrt::queue ffrtQueue_;
77 
78     std::unique_ptr<IBluetoothSwitchAction> switchAction_ { nullptr };
79     std::atomic_bool isBtSwitchProcessing_ { false };
80     std::vector<BluetoothSwitchEvent> cachedEventVec_ {};
81     std::mutex bluetoothSwitchEventMutex_ {};  // Used for ProcessBluetoothSwitchEvent function
82     std::atomic_bool noAutoConnect_ { false };
83 };
84 }  // namespace Bluetooth
85 }  // namespace OHOS
86 #endif  // BLUETOOTH_SWITCH_MODULE_H
87