1 // Copyright 2023 The Pigweed Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 4 // use this file except in compliance with the License. You may obtain a copy of 5 // the License at 6 // 7 // https://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, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations under 13 // the License. 14 #include "chre/platform/system_timer.h" 15 16 #include "chre/platform/log.h" 17 #include "chre/util/time.h" 18 19 namespace chre { 20 OnExpired()21void SystemTimerBase::OnExpired() { 22 SystemTimer* timer = static_cast<SystemTimer*>(this); 23 timer->mCallback(timer->mData); 24 } 25 SystemTimer()26SystemTimer::SystemTimer() {} 27 ~SystemTimer()28SystemTimer::~SystemTimer() { 29 if (!initialized_) { 30 return; 31 } 32 cancel(); 33 initialized_ = false; 34 } 35 init()36bool SystemTimer::init() { 37 initialized_ = true; 38 return initialized_; 39 } 40 set(SystemTimerCallback * callback,void * data,Nanoseconds delay)41bool SystemTimer::set(SystemTimerCallback* callback, 42 void* data, 43 Nanoseconds delay) { 44 if (!initialized_) { 45 return false; 46 } 47 mCallback = callback; 48 mData = data; 49 pw::chrono::SystemClock::duration interval = 50 std::chrono::nanoseconds(delay.toRawNanoseconds()); 51 const pw::chrono::SystemClock::time_point now = 52 pw::chrono::SystemClock::now(); 53 timer_.InvokeAt(now + interval); 54 return true; 55 } 56 cancel()57bool SystemTimer::cancel() { 58 if (!initialized_) { 59 return false; 60 } 61 timer_.Cancel(); 62 return true; 63 } 64 isActive()65bool SystemTimer::isActive() { return is_active_; } 66 67 } // namespace chre 68