1 /*
2  * Copyright 2019 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 #include "os/repeating_alarm.h"
18 
19 #include <bluetooth/log.h>
20 #include <sys/timerfd.h>
21 #include <unistd.h>
22 
23 #include <cstring>
24 
25 #include "common/bind.h"
26 #include "os/linux_generic/linux.h"
27 #include "os/utils.h"
28 
29 #ifdef __ANDROID__
30 #define ALARM_CLOCK CLOCK_BOOTTIME_ALARM
31 #else
32 #define ALARM_CLOCK CLOCK_BOOTTIME
33 #endif
34 
35 namespace bluetooth {
36 namespace os {
37 using common::Closure;
38 
RepeatingAlarm(Handler * handler)39 RepeatingAlarm::RepeatingAlarm(Handler* handler)
40     : handler_(handler), fd_(TIMERFD_CREATE(ALARM_CLOCK, 0)) {
41   log::assert_that(fd_ != -1, "assert failed: fd_ != -1");
42 
43   token_ = handler_->thread_->GetReactor()->Register(
44           fd_, common::Bind(&RepeatingAlarm::on_fire, common::Unretained(this)), common::Closure());
45 }
46 
~RepeatingAlarm()47 RepeatingAlarm::~RepeatingAlarm() {
48   handler_->thread_->GetReactor()->Unregister(token_);
49 
50   int close_status;
51   RUN_NO_INTR(close_status = TIMERFD_CLOSE(fd_));
52   log::assert_that(close_status != -1, "assert failed: close_status != -1");
53 }
54 
Schedule(Closure task,std::chrono::milliseconds period)55 void RepeatingAlarm::Schedule(Closure task, std::chrono::milliseconds period) {
56   std::lock_guard<std::mutex> lock(mutex_);
57   long period_ms = period.count();
58   itimerspec timer_itimerspec{{period_ms / 1000, period_ms % 1000 * 1000000},
59                               {period_ms / 1000, period_ms % 1000 * 1000000}};
60   int result = TIMERFD_SETTIME(fd_, 0, &timer_itimerspec, nullptr);
61   log::assert_that(result == 0, "assert failed: result == 0");
62 
63   task_ = std::move(task);
64 }
65 
Cancel()66 void RepeatingAlarm::Cancel() {
67   std::lock_guard<std::mutex> lock(mutex_);
68   itimerspec disarm_itimerspec{/* disarm timer */};
69   int result = TIMERFD_SETTIME(fd_, 0, &disarm_itimerspec, nullptr);
70   log::assert_that(result == 0, "assert failed: result == 0");
71 }
72 
on_fire()73 void RepeatingAlarm::on_fire() {
74   std::unique_lock<std::mutex> lock(mutex_);
75   auto task = task_;
76   uint64_t times_invoked;
77   auto bytes_read = read(fd_, &times_invoked, sizeof(uint64_t));
78   lock.unlock();
79   task.Run();
80   log::assert_that(bytes_read == static_cast<ssize_t>(sizeof(uint64_t)),
81                    "assert failed: bytes_read == static_cast<ssize_t>(sizeof(uint64_t))");
82 }
83 
84 }  // namespace os
85 }  // namespace bluetooth
86