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(void) = 0; 34 virtual int DisableBluetooth(void) = 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); 55 56 private: 57 int ProcessEnableBluetoothEvent(void); 58 int ProcessDisableBluetoothEvent(void); 59 int ProcessEnableBluetoothToRestrictModeEvent(void); 60 int ProcessBluetoothOnEvent(void); 61 int ProcessBluetoothOffEvent(void); 62 int ProcessBluetoothHalfEvent(void); 63 int ProcessBluetoothSwitchAction(std::function<int(void)> action, BluetoothSwitchEvent cachedEvent); 64 int ProcessBluetoothSwitchCachedEvent(BluetoothSwitchEvent event); 65 int ProcessBluetoothSwitchActionEnd( 66 BluetoothSwitchEvent curSwitchActionEvent, std::vector<BluetoothSwitchEvent> expectedEventVec); 67 void DeduplicateCacheEvent(BluetoothSwitchEvent curEvent); 68 void LogCacheEventIgnored(std::vector<BluetoothSwitchEvent> eventVec); 69 void LogBluetoothSwitchEvent(BluetoothSwitchEvent event); 70 void OnTaskTimeout(void); 71 72 const uint64_t DEFAULT_TASK_TIMEOUT = 10000000; // 10s 73 uint64_t taskTimeout_ = DEFAULT_TASK_TIMEOUT; 74 ffrt::task_handle taskTimeoutHandle_; 75 ffrt::queue ffrtQueue_; 76 77 std::unique_ptr<IBluetoothSwitchAction> switchAction_ { nullptr }; 78 std::atomic_bool isBtSwitchProcessing_ { false }; 79 std::vector<BluetoothSwitchEvent> cachedEventVec_ {}; 80 std::mutex bluetoothSwitchEventMutex_ {}; // Used for ProcessBluetoothSwitchEvent function 81 }; 82 } // namespace Bluetooth 83 } // namespace OHOS 84 #endif // BLUETOOTH_SWITCH_MODULE_H 85