1 /* 2 * Copyright (C) 2024 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #pragma once 18 19 #include "chre/util/time.h" 20 21 namespace chre::platform_linux { 22 23 /** 24 * Override the value returned by SystemTime::getMonotonicTime(). Useful for 25 * testing. 26 * 27 * @param ns The value that should be returned by SystemTime::getMonotonicTime() 28 */ 29 void overrideMonotonicTime(Nanoseconds ns); 30 31 /** 32 * Reset SystemTime::getMonotonicTime() to its default behavior of returning a 33 * real time reference. 34 */ 35 void clearMonotonicTimeOverride(void); 36 37 //! RAII way to override the time and then reset 38 class SystemTimeOverride { 39 public: SystemTimeOverride(Nanoseconds ns)40 explicit SystemTimeOverride(Nanoseconds ns) { 41 update(ns); 42 } SystemTimeOverride(uint64_t ns)43 explicit SystemTimeOverride(uint64_t ns) { 44 update(ns); 45 } ~SystemTimeOverride()46 ~SystemTimeOverride() { 47 clearMonotonicTimeOverride(); 48 } 49 update(Nanoseconds ns)50 void update(Nanoseconds ns) { 51 overrideMonotonicTime(ns); 52 } update(uint64_t ns)53 void update(uint64_t ns) { 54 update(Nanoseconds(ns)); 55 } 56 }; 57 58 } // namespace chre::platform_linux 59