1 /*
2  * Copyright (C) 2022 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 #ifndef CHRE_PLATFORM_TINYSYS_SYSTEM_TIMER_BASE_H_
18 #define CHRE_PLATFORM_TINYSYS_SYSTEM_TIMER_BASE_H_
19 
20 #include <cinttypes>
21 #include <csignal>
22 #include <ctime>
23 
24 #ifdef __cplusplus
25 extern "C" {
26 #endif
27 
28 #include "sensorhub/rt_timer.h"
29 #include "task.h"
30 
31 #ifdef __cplusplus
32 }  // extern "C"
33 #endif
34 
35 namespace chre {
36 
37 class SystemTimerBase {
38  protected:
39   /** Stack size (in words) of the timer callback runner task */
40   static constexpr uint32_t kStackDepthWords = 0x200;  // 2K stack size
41 
42   /** Priority of the callback runner task */
43   static constexpr UBaseType_t kTaskPriority = tskIDLE_PRIORITY + 4;
44 
45   /** Name of the callback runner task */
46   static constexpr char kTaskName[] = "ChreTimerCbRunner";
47 
48   /**
49    * Callback function woken up by the timer, which in turn wakes up the
50    * callback runner task
51    */
52   static void rtTimerCallback(struct rt_timer *timer);
53 
54   /**
55    * The callback runner task that blocks until it gets woken up by the timer
56    * callback.
57    *
58    * This task runs the actual callback set by the user as the timer callback is
59    * running from the ISR.
60    *
61    * @param context a raw pointer that will be casted to a pointer to
62    * SystemTimer.
63    */
64 
65   static void callbackRunner(void *context);
66 
67   /** A FreeRTOS task handle holding the callback runner task */
68   TaskHandle_t mCallbackRunnerHandle = nullptr;
69 
70   /** Tracks whether the timer has been initialized correctly. */
71   bool mInitialized = false;
72 
73   /** The properties of the timer including callback, data, etc. */
74   struct rt_timer rtSystemTimer;
75 };
76 }  // namespace chre
77 #endif  // CHRE_PLATFORM_TINYSYS_SYSTEM_TIMER_BASE_H_