1 /****************************************************************************** 2 * 3 * Copyright 2021 Google, Inc. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at: 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 ******************************************************************************/ 18 19 #pragma once 20 21 #include <memory> 22 #include <mutex> 23 #include <string> 24 25 #include <flatbuffers/flatbuffers.h> 26 27 #include "handler.h" 28 #include "wakelock_manager_generated.h" 29 30 namespace bluetooth { 31 namespace os { 32 33 class WakelockManager { 34 public: 35 static const std::string kBtWakelockId; 36 Get()37 static WakelockManager& Get() { 38 static WakelockManager instance; 39 return instance; 40 } 41 42 // The set of functions required by GD to grab wake locks. A caller with a custom wakelock implementation should 43 // implement this class and passed into the stack through SetCallouts() 44 class OsCallouts { 45 public: 46 virtual ~OsCallouts() = default; 47 virtual void AcquireCallout(const std::string& lock_name) = 0; 48 virtual void ReleaseCallout(const std::string& lock_name) = 0; 49 }; 50 51 // Set the Bluetooth OS callouts to |callouts|. 52 // 53 // This function should be called when native kernel wakelock are not used directly. 54 // If this function is not called, or |callouts| is nullptr, then native kernel wakelock will be used. 55 // When |callouts| are used, the callbacks are going to be invoked asynchronously to avoid being blocked by upper 56 // layer delays. Therefore, a handler is needed and the callout result will be ignored. 57 // 58 // This method must be called before calling Acquire() or Release() 59 void SetOsCallouts(OsCallouts* callouts, Handler* handler); 60 61 // Acquire the Bluetooth wakelock. 62 // Return true on success, otherwise false. 63 // The function is thread safe. 64 bool Acquire(); 65 66 // Release the Bluetooth wakelock. 67 // Return true on success, otherwise false. 68 // The function is thread safe. 69 bool Release(); 70 71 // Cleanup the wakelock internal runtime state. 72 // This will NOT clean up the callouts 73 void CleanUp(); 74 75 // Dump wakelock-related debug info to a flat buffer defined in wakelock_manager.fbs 76 flatbuffers::Offset<WakelockManagerData> GetDumpsysData(flatbuffers::FlatBufferBuilder* fb_builder); 77 78 ~WakelockManager(); 79 80 private: 81 WakelockManager(); 82 83 std::recursive_mutex mutex_; 84 bool initialized_ = false; 85 OsCallouts* os_callouts_ = nullptr; 86 Handler* os_callouts_handler_ = nullptr; 87 bool is_native_ = true; 88 89 struct Stats; 90 std::unique_ptr<Stats> pstats_; 91 }; 92 93 } // namespace os 94 } // namespace bluetooth 95