• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "location/lbs/contexthub/nanoapps/nearby/timer.h"
18 
19 #include <chre.h>
20 
21 #include "third_party/contexthub/chre/util/include/chre/util/nanoapp/log.h"
22 #include "third_party/contexthub/chre/util/include/chre/util/time.h"
23 
24 #define LOG_TAG "[NEARBY][TIMER]"
25 
26 namespace nearby {
27 
StartTimer()28 bool Timer::StartTimer() {
29   if (duration_ms_ == 0) {
30     LOGD("Timer is not started. Timer duration is 0.");
31     return false;
32   }
33   if (!is_one_shot_ && timer_id_ != CHRE_TIMER_INVALID) {
34     chreTimerCancel(timer_id_);
35     timer_id_ = CHRE_TIMER_INVALID;
36   }
37   timer_id_ = chreTimerSet(duration_ms_ * chre::kOneMillisecondInNanoseconds,
38                            &timer_id_, /*oneShot=*/is_one_shot_);
39   if (timer_id_ == CHRE_TIMER_INVALID) {
40     LOGE("Error in configuring timer.");
41     return false;
42   }
43   return true;
44 }
45 
StopTimer()46 bool Timer::StopTimer() {
47   if (timer_id_ == CHRE_TIMER_INVALID) {
48     LOGD("Timer is already stopped.");
49     return false;
50   }
51   if (!chreTimerCancel(timer_id_)) {
52     LOGW(
53         "Error in stopping timer. For a one-shot timer, it may have just "
54         "fired or expired.");
55     return false;
56   }
57   timer_id_ = CHRE_TIMER_INVALID;
58   return true;
59 }
60 
61 }  // namespace nearby
62