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 "handler.h" 26 27 namespace bluetooth { 28 namespace os { 29 30 class WakelockManager { 31 public: 32 static const std::string kBtWakelockId; 33 Get()34 static WakelockManager& Get() { 35 static WakelockManager instance; 36 return instance; 37 } 38 39 // The set of functions required by GD to grab wake locks. A caller with a custom wakelock 40 // implementation should implement this class and passed into the stack through SetCallouts() 41 class OsCallouts { 42 public: 43 virtual ~OsCallouts() = default; 44 virtual void AcquireCallout(const std::string& lock_name) = 0; 45 virtual void ReleaseCallout(const std::string& lock_name) = 0; 46 }; 47 48 // Set the Bluetooth OS callouts to |callouts|. 49 // 50 // This function should be called when native kernel wakelock are not used directly. 51 // If this function is not called, or |callouts| is nullptr, then native kernel wakelock will be 52 // used. When |callouts| are used, the callbacks are going to be invoked asynchronously to avoid 53 // being blocked by upper layer delays. Therefore, a handler is needed and the callout result will 54 // be ignored. 55 // 56 // This method must be called before calling Acquire() or Release() 57 void SetOsCallouts(OsCallouts* callouts, Handler* handler); 58 59 // Acquire the Bluetooth wakelock. 60 // Return true on success, otherwise false. 61 // The function is thread safe. 62 bool Acquire(); 63 64 // Release the Bluetooth wakelock. 65 // Return true on success, otherwise false. 66 // The function is thread safe. 67 bool Release(); 68 69 // Cleanup the wakelock internal runtime state. 70 // This will NOT clean up the callouts 71 void CleanUp(); 72 73 /// Write debug information relevant for the wakelock manager 74 /// to the dumpsys output file descriptor. 75 void Dump(int fd) const; 76 77 ~WakelockManager(); 78 79 private: 80 WakelockManager(); 81 82 mutable std::recursive_mutex mutex_; 83 bool initialized_ = false; 84 OsCallouts* os_callouts_ = nullptr; 85 Handler* os_callouts_handler_ = nullptr; 86 bool is_native_ = true; 87 88 struct Stats; 89 std::unique_ptr<Stats> pstats_; 90 }; 91 92 } // namespace os 93 } // namespace bluetooth 94