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/alarm.h"
18 
19 #include <bluetooth/log.h>
20 #include <com_android_bluetooth_flags.h>
21 #include <sys/timerfd.h>
22 #include <unistd.h>
23 
24 #include <cstring>
25 
26 #include "common/bind.h"
27 #include "os/linux_generic/linux.h"
28 #include "os/utils.h"
29 
30 #ifdef __ANDROID__
31 #define ALARM_CLOCK CLOCK_BOOTTIME_ALARM
32 #else
33 #define ALARM_CLOCK CLOCK_BOOTTIME
34 #endif
35 
36 namespace bluetooth {
37 namespace os {
38 using common::Closure;
39 using common::OnceClosure;
40 
Alarm(Handler * handler)41 Alarm::Alarm(Handler* handler) : Alarm(handler, true) {}
42 
Alarm(Handler * handler,bool isWakeAlarm)43 Alarm::Alarm(Handler* handler, bool isWakeAlarm) : handler_(handler) {
44   int timerfd_flag =
45           com::android::bluetooth::flags::non_wake_alarm_for_rpa_rotation() ? TFD_NONBLOCK : 0;
46 
47   fd_ = TIMERFD_CREATE(isWakeAlarm ? ALARM_CLOCK : CLOCK_BOOTTIME, timerfd_flag);
48 
49   log::assert_that(fd_ != -1, "cannot create timerfd: {}", strerror(errno));
50 
51   token_ = handler_->thread_->GetReactor()->Register(
52           fd_, common::Bind(&Alarm::on_fire, common::Unretained(this)), Closure());
53 }
54 
~Alarm()55 Alarm::~Alarm() {
56   handler_->thread_->GetReactor()->Unregister(token_);
57 
58   int close_status;
59   RUN_NO_INTR(close_status = TIMERFD_CLOSE(fd_));
60   log::assert_that(close_status != -1, "assert failed: close_status != -1");
61 }
62 
Schedule(OnceClosure task,std::chrono::milliseconds delay)63 void Alarm::Schedule(OnceClosure task, std::chrono::milliseconds delay) {
64   std::lock_guard<std::mutex> lock(mutex_);
65   long delay_ms = delay.count();
66   itimerspec timer_itimerspec{{/* interval for periodic timer */},
67                               {delay_ms / 1000, delay_ms % 1000 * 1000000}};
68   int result = TIMERFD_SETTIME(fd_, 0, &timer_itimerspec, nullptr);
69   log::assert_that(result == 0, "assert failed: result == 0");
70 
71   task_ = std::move(task);
72 }
73 
Cancel()74 void Alarm::Cancel() {
75   std::lock_guard<std::mutex> lock(mutex_);
76   itimerspec disarm_itimerspec{/* disarm timer */};
77   int result = TIMERFD_SETTIME(fd_, 0, &disarm_itimerspec, nullptr);
78   log::assert_that(result == 0, "assert failed: result == 0");
79 }
80 
on_fire()81 void Alarm::on_fire() {
82   std::unique_lock<std::mutex> lock(mutex_);
83   auto task = std::move(task_);
84   uint64_t times_invoked;
85   auto bytes_read = read(fd_, &times_invoked, sizeof(uint64_t));
86   lock.unlock();
87 
88   if (com::android::bluetooth::flags::non_wake_alarm_for_rpa_rotation() && bytes_read == -1) {
89     log::debug("No data to read.");
90     if (errno == EAGAIN || errno == EWOULDBLOCK) {
91       log::debug("Alarm is already canceled or rescheduled.");
92       return;
93     }
94   }
95 
96   log::assert_that(bytes_read == static_cast<ssize_t>(sizeof(uint64_t)),
97                    "assert failed: bytes_read == static_cast<ssize_t>(sizeof(uint64_t))");
98   log::assert_that(times_invoked == 1u, "Invoked number of times:{} fd:{}", times_invoked, fd_);
99   std::move(task).Run();
100 }
101 
102 }  // namespace os
103 }  // namespace bluetooth
104