• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "android-base/logging.h"
2 #include "gki.h"
3 
4 // Timers are handled explicitly by the fuzzer
GKI_start_timer(unsigned char,int,bool)5 void GKI_start_timer(unsigned char, int, bool) {}
GKI_stop_timer(uint8_t)6 void GKI_stop_timer(uint8_t) {}
7 
GKI_get_remaining_ticks(TIMER_LIST_Q *,TIMER_LIST_ENT *)8 uint32_t GKI_get_remaining_ticks(TIMER_LIST_Q*, TIMER_LIST_ENT*) {
9   CHECK(false);
10   return 0;
11 }
12 
GKI_init_timer_list(TIMER_LIST_Q * p_timer_listq)13 void GKI_init_timer_list(TIMER_LIST_Q* p_timer_listq) {
14   new (p_timer_listq) TIMER_LIST_Q;
15 }
16 
GKI_add_to_timer_list(TIMER_LIST_Q * p_timer_listq,TIMER_LIST_ENT * p_tle)17 void GKI_add_to_timer_list(TIMER_LIST_Q* p_timer_listq, TIMER_LIST_ENT* p_tle) {
18   if (!p_tle || !p_timer_listq || p_tle->ticks < 0) {
19     return;
20   }
21 
22   p_tle->in_use = true;
23 
24   auto it = p_timer_listq->begin();
25   while (it != p_timer_listq->end()) {
26     TIMER_LIST_ENT* p_successor = *it;
27 
28     // Reached successor
29     if (p_tle->ticks <= p_successor->ticks) {
30       p_successor->ticks -= p_tle->ticks;
31       break;
32     }
33 
34     // Account for predecessor ticks
35     if (p_successor->ticks > 0) {
36       p_tle->ticks -= p_successor->ticks;
37     }
38 
39     it++;
40   }
41 
42   p_timer_listq->insert(it, p_tle);
43 }
44 
GKI_remove_from_timer_list(TIMER_LIST_Q * p_timer_listq,TIMER_LIST_ENT * p_tle)45 void GKI_remove_from_timer_list(TIMER_LIST_Q* p_timer_listq,
46                                 TIMER_LIST_ENT* p_tle) {
47   if (!p_tle || !p_tle->in_use) {
48     return;
49   }
50 
51   auto it = std::find(p_timer_listq->begin(), p_timer_listq->end(), p_tle);
52   if (it == p_timer_listq->end()) {
53     return;
54   }
55 
56   auto successor_it = p_timer_listq->erase(it);
57 
58   // Add remaining ticks to subsequent timer
59   if (successor_it != p_timer_listq->end()) {
60     (*successor_it)->ticks += p_tle->ticks;
61   }
62 
63   p_tle->ticks = 0;
64   p_tle->in_use = false;
65 }
66 
GKI_update_timer_list(TIMER_LIST_Q * p_timer_listq,int32_t num_units_since_last_update)67 uint16_t GKI_update_timer_list(TIMER_LIST_Q* p_timer_listq,
68                                int32_t num_units_since_last_update) {
69   uint16_t num_time_out = 0;
70 
71   for (auto it : *p_timer_listq) {
72     if (it->ticks <= 0) {
73       num_time_out++;
74       continue;
75     }
76 
77     if (num_units_since_last_update <= 0) {
78       break;
79     }
80 
81     int32_t temp_ticks = it->ticks;
82     it->ticks -= num_units_since_last_update;
83 
84     // Check for timeout
85     if (it->ticks <= 0) {
86       it->ticks = 0;
87       num_time_out++;
88     }
89 
90     // Decrement ticks to process
91     num_units_since_last_update -= temp_ticks;
92   }
93 
94   return num_time_out;
95 }
96 
GKI_timer_list_empty(TIMER_LIST_Q * p_timer_listq)97 bool GKI_timer_list_empty(TIMER_LIST_Q* p_timer_listq) {
98   return p_timer_listq->empty();
99 }
100 
GKI_timer_list_first(TIMER_LIST_Q * p_timer_listq)101 TIMER_LIST_ENT* GKI_timer_list_first(TIMER_LIST_Q* p_timer_listq) {
102   return *p_timer_listq->begin();
103 }
104 
105 uint32_t g_tick_count = 0;
GKI_get_tick_count()106 uint32_t GKI_get_tick_count() { return g_tick_count++; }
107