1 /* //device/libs/telephony/ril_event.cpp
3 ** Copyright 2008, The Android Open Source Project
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
9 ** http://www.apache.org/licenses/LICENSE-2.0
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
18 #define LOG_TAG "RILC"
24 #include <utils/Log.h>
25 #include <ril_event.h>
31 static pthread_mutex_t listMutex;
32 #define MUTEX_ACQUIRE() pthread_mutex_lock(&listMutex)
33 #define MUTEX_RELEASE() pthread_mutex_unlock(&listMutex)
34 #define MUTEX_INIT() pthread_mutex_init(&listMutex, NULL)
35 #define MUTEX_DESTROY() pthread_mutex_destroy(&listMutex)
38 #define timeradd(tvp, uvp, vvp) \
40 (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \
41 (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \
42 if ((vvp)->tv_usec >= 1000000) { \
44 (vvp)->tv_usec -= 1000000; \
50 #define timercmp(a, b, op) \
51 ((a)->tv_sec == (b)->tv_sec \
52 ? (a)->tv_usec op (b)->tv_usec \
53 : (a)->tv_sec op (b)->tv_sec)
57 #define timersub(a, b, res) \
59 (res)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
60 (res)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
61 if ((res)->tv_usec < 0) { \
62 (res)->tv_usec += 1000000; \
68 static fd_set readFds;
71 static struct ril_event * watch_table[MAX_FD_EVENTS];
72 static struct ril_event timer_list;
73 static struct ril_event pending_list;
78 #define dlog(x...) RLOGD( x )
79 static void dump_event(struct ril_event * ev)
81 dlog("~~~~ Event %x ~~~~", (unsigned int)ev);
82 dlog(" next = %x", (unsigned int)ev->next);
83 dlog(" prev = %x", (unsigned int)ev->prev);
84 dlog(" fd = %d", ev->fd);
85 dlog(" pers = %d", ev->persist);
86 dlog(" timeout = %ds + %dus", (int)ev->timeout.tv_sec, (int)ev->timeout.tv_usec);
87 dlog(" func = %x", (unsigned int)ev->func);
88 dlog(" param = %x", (unsigned int)ev->param);
89 dlog("~~~~~~~~~~~~~~~~~~");
92 #define dlog(x...) do {} while(0)
93 #define dump_event(x) do {} while(0)
96 static void getNow(struct timeval * tv)
99 clock_gettime(CLOCK_MONOTONIC, &ts);
100 tv->tv_sec = ts.tv_sec;
101 tv->tv_usec = ts.tv_nsec/1000;
104 static void init_list(struct ril_event * list)
106 memset(list, 0, sizeof(struct ril_event));
112 static void addToList(struct ril_event * ev, struct ril_event * list)
115 ev->prev = list->prev;
121 static void removeFromList(struct ril_event * ev)
123 dlog("~~~~ +removeFromList ~~~~");
126 ev->next->prev = ev->prev;
127 ev->prev->next = ev->next;
130 dlog("~~~~ -removeFromList ~~~~");
134 static void removeWatch(struct ril_event * ev, int index)
136 dlog("~~~~ +removeWatch ~~~~");
137 watch_table[index] = NULL;
140 FD_CLR(ev->fd, &readFds);
142 if (ev->fd+1 == nfds) {
145 for (int i = 0; i < MAX_FD_EVENTS; i++) {
146 struct ril_event * rev = watch_table[i];
148 if ((rev != NULL) && (rev->fd > n)) {
153 dlog("~~~~ nfds = %d ~~~~", nfds);
155 dlog("~~~~ -removeWatch ~~~~");
158 static void processTimeouts()
160 dlog("~~~~ +processTimeouts ~~~~");
163 struct ril_event * tev = timer_list.next;
164 struct ril_event * next;
167 // walk list, see if now >= ev->timeout for any events
169 dlog("~~~~ Looking for timers <= %ds + %dus ~~~~", (int)now.tv_sec, (int)now.tv_usec);
170 while ((tev != &timer_list) && (timercmp(&now, &tev->timeout, >))) {
172 dlog("~~~~ firing timer ~~~~");
175 addToList(tev, &pending_list);
179 dlog("~~~~ -processTimeouts ~~~~");
182 static void processReadReadies(fd_set * rfds, int n)
184 dlog("~~~~ +processReadReadies (%d) ~~~~", n);
187 for (int i = 0; (i < MAX_FD_EVENTS) && (n > 0); i++) {
188 struct ril_event * rev = watch_table[i];
189 if (rev != NULL && FD_ISSET(rev->fd, rfds)) {
190 addToList(rev, &pending_list);
191 if (rev->persist == false) {
199 dlog("~~~~ -processReadReadies (%d) ~~~~", n);
202 static void firePending()
204 dlog("~~~~ +firePending ~~~~");
205 struct ril_event * ev = pending_list.next;
206 while (ev != &pending_list) {
207 struct ril_event * next = ev->next;
209 ev->func(ev->fd, 0, ev->param);
212 dlog("~~~~ -firePending ~~~~");
215 static int calcNextTimeout(struct timeval * tv)
217 struct ril_event * tev = timer_list.next;
222 // Sorted list, so calc based on first node
223 if (tev == &timer_list) {
228 dlog("~~~~ now = %ds + %dus ~~~~", (int)now.tv_sec, (int)now.tv_usec);
229 dlog("~~~~ next = %ds + %dus ~~~~",
230 (int)tev->timeout.tv_sec, (int)tev->timeout.tv_usec);
231 if (timercmp(&tev->timeout, &now, >)) {
232 timersub(&tev->timeout, &now, tv);
234 // timer already expired.
235 tv->tv_sec = tv->tv_usec = 0;
240 // Initialize internal data structs
241 void ril_event_init()
246 init_list(&timer_list);
247 init_list(&pending_list);
248 memset(watch_table, 0, sizeof(watch_table));
251 // Initialize an event
252 void ril_event_set(struct ril_event * ev, int fd, bool persist, ril_event_cb func, void * param)
254 dlog("~~~~ ril_event_set %x ~~~~", (unsigned int)ev);
255 memset(ev, 0, sizeof(struct ril_event));
258 ev->persist = persist;
261 fcntl(fd, F_SETFL, O_NONBLOCK);
264 // Add event to watch list
265 void ril_event_add(struct ril_event * ev)
267 dlog("~~~~ +ril_event_add ~~~~");
269 for (int i = 0; i < MAX_FD_EVENTS; i++) {
270 if (watch_table[i] == NULL) {
273 dlog("~~~~ added at %d ~~~~", i);
275 FD_SET(ev->fd, &readFds);
276 if (ev->fd >= nfds) nfds = ev->fd+1;
277 dlog("~~~~ nfds = %d ~~~~", nfds);
282 dlog("~~~~ -ril_event_add ~~~~");
286 void ril_timer_add(struct ril_event * ev, struct timeval * tv)
288 dlog("~~~~ +ril_timer_add ~~~~");
291 struct ril_event * list;
294 list = timer_list.next;
295 ev->fd = -1; // make sure fd is invalid
299 timeradd(&now, tv, &ev->timeout);
302 while (timercmp(&list->timeout, &ev->timeout, < )
303 && (list != &timer_list)) {
306 // list now points to the first event older than ev
311 dlog("~~~~ -ril_timer_add ~~~~");
314 // Remove event from watch or timer list
315 void ril_event_del(struct ril_event * ev)
317 dlog("~~~~ +ril_event_del ~~~~");
320 if (ev->index < 0 || ev->index >= MAX_FD_EVENTS) {
325 removeWatch(ev, ev->index);
328 dlog("~~~~ -ril_event_del ~~~~");
332 static void printReadies(fd_set * rfds)
334 for (int i = 0; (i < MAX_FD_EVENTS); i++) {
335 struct ril_event * rev = watch_table[i];
336 if (rev != NULL && FD_ISSET(rev->fd, rfds)) {
337 dlog("DON: fd=%d is ready", rev->fd);
342 #define printReadies(rfds) do {} while(0)
345 void ril_event_loop()
350 struct timeval * ptv;
355 // make local copy of read fd_set
356 memcpy(&rfds, &readFds, sizeof(fd_set));
357 if (-1 == calcNextTimeout(&tv)) {
358 // no pending timers; block indefinitely
359 dlog("~~~~ no timers; blocking indefinitely ~~~~");
362 dlog("~~~~ blocking for %ds + %dus ~~~~", (int)tv.tv_sec, (int)tv.tv_usec);
366 n = select(nfds, &rfds, NULL, NULL, ptv);
368 dlog("~~~~ %d events fired ~~~~", n);
370 if (errno == EINTR) continue;
372 RLOGE("ril_event: select error (%d)", errno);
377 // Check for timeouts
379 // Check for read-ready
380 processReadReadies(&rfds, n);