Return ast_event_get_ie_raw to using an iterator and fix logic in ast_event_iterator_...
[asterisk/asterisk.git] / main / event.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Digium, Inc.
5  *
6  * Russell Bryant <russell@digium.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  */
18
19 /*! \file
20  *
21  * \brief Internal generic event system
22  *
23  * \author Russell Bryant <russell@digium.com>
24  */
25
26 #include "asterisk.h"
27
28 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
29
30 #include <stdlib.h>
31 #include <stdio.h>
32
33 #include "asterisk/event.h"
34 #include "asterisk/linkedlists.h"
35 #include "asterisk/lock.h"
36 #include "asterisk/utils.h"
37
38 /* Only use one thread for now to ensure ordered delivery */
39 #define NUM_EVENT_THREADS 1
40
41 struct ast_event_ie {
42         enum ast_event_ie_type ie_type:16;
43         /*! Total length of the IE payload */
44         uint16_t ie_payload_len;
45         unsigned char ie_payload[0];
46 } __attribute__ ((packed));
47
48 /*!
49  * \brief An event
50  *
51  * \note The format of this structure is important, and can not change, since
52  *       they are sent directly over the network (via IAX2).
53  *
54  */
55 struct ast_event {
56         /*! Event type */
57         enum ast_event_type type:16;
58         /*! Total length of the event */
59         uint16_t event_len:16;
60         /*! The data payload of the event, made up of information elements */
61         unsigned char payload[0];
62 } __attribute__ ((packed));
63
64 struct ast_event_ref {
65         struct ast_event *event;
66         AST_LIST_ENTRY(ast_event_ref) entry;
67 };
68
69 struct ast_event_iterator {
70         uint16_t event_len;
71         const struct ast_event *event;
72         struct ast_event_ie *ie;
73 };
74
75 /*! \brief data shared between event dispatching threads */
76 static struct {
77         ast_cond_t cond;
78         ast_mutex_t lock;
79         AST_LIST_HEAD_NOLOCK(, ast_event_ref) event_q;
80 } event_thread = {
81         .lock = AST_MUTEX_INIT_VALUE,
82 };
83
84 struct ast_event_ie_val {
85         AST_LIST_ENTRY(ast_event_ie_val) entry;
86         enum ast_event_ie_type ie_type;
87         enum ast_event_ie_pltype ie_pltype;
88         union {
89                 uint32_t uint;
90                 const char *str;
91         } payload;
92 };
93
94 /*! \brief Event subscription */
95 struct ast_event_sub {
96         enum ast_event_type type;
97         ast_event_cb_t cb;
98         void *userdata;
99         uint32_t uniqueid;
100         AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
101         AST_RWLIST_ENTRY(ast_event_sub) entry;
102 };
103
104 static uint32_t sub_uniqueid;
105
106 /*! \brief Event subscriptions
107  * The event subscribers are indexed by which event they are subscribed to */
108 static AST_RWLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
109
110 /*! \brief Cached events
111  * The event cache is indexed on the event type.  The purpose of this is 
112  * for events that express some sort of state.  So, when someone first
113  * needs to know this state, it can get the last known state from the cache. */
114 static AST_RWLIST_HEAD(ast_event_ref_list, ast_event_ref) ast_event_cache[AST_EVENT_TOTAL];
115
116 static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
117 {
118         if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
119                 ast_free((void *) ie_val->payload.str);
120
121         ast_free(ie_val);
122 }
123
124 enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
125 {
126         va_list ap;
127         enum ast_event_ie_type ie_type;
128         enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
129         struct ast_event_ie_val *ie_val, *sub_ie_val;
130         struct ast_event_sub *sub;
131         AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
132
133         if (type >= AST_EVENT_TOTAL) {
134                 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
135                 return res;
136         }
137
138         va_start(ap, type);
139         for (ie_type = va_arg(ap, enum ast_event_type);
140                 ie_type != AST_EVENT_IE_END;
141                 ie_type = va_arg(ap, enum ast_event_type))
142         {
143                 struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
144                 memset(ie_val, 0, sizeof(*ie_val));
145                 ie_val->ie_type = ie_type;
146                 ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
147                 if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
148                         ie_val->payload.uint = va_arg(ap, uint32_t);
149                 else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
150                         ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
151                 AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
152         }
153         va_end(ap);
154
155         AST_RWLIST_RDLOCK(&ast_event_subs[type]);
156         AST_RWLIST_TRAVERSE(&ast_event_subs[type], sub, entry) {
157                 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
158                         AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
159                                 if (sub_ie_val->ie_type == ie_val->ie_type)
160                                         break;
161                         }
162                         if (!sub_ie_val) {
163                                 if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
164                                         break;
165                                 continue;
166                         }
167                         /* The subscriber doesn't actually care what the value is */
168                         if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
169                                 continue;
170                         if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
171                                 ie_val->payload.uint != sub_ie_val->payload.uint)
172                                 break;
173                         if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
174                                 strcmp(ie_val->payload.str, sub_ie_val->payload.str))
175                                 break;
176                 }
177                 if (!ie_val)
178                         break;
179         }
180         AST_RWLIST_UNLOCK(&ast_event_subs[type]);
181
182         if (sub) /* All parameters were matched */
183                 return AST_EVENT_SUB_EXISTS;
184
185         AST_RWLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
186         if (!AST_LIST_EMPTY(&ast_event_subs[AST_EVENT_ALL]))
187                 res = AST_EVENT_SUB_EXISTS;
188         AST_RWLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
189
190         return res;
191 }
192
193 /*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
194 void ast_event_report_subs(const struct ast_event_sub *event_sub)
195 {
196         struct ast_event *event;
197         struct ast_event_sub *sub;
198         enum ast_event_type event_type = -1;
199         struct ast_event_ie_val *ie_val;
200
201         if (event_sub->type != AST_EVENT_SUB)
202                 return;
203
204         AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
205                 if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
206                         event_type = ie_val->payload.uint;
207                         break;
208                 }
209         }
210
211         if (event_type == -1)
212                 return;
213
214         AST_RWLIST_RDLOCK(&ast_event_subs[event_type]);
215         AST_RWLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
216                 if (event_sub == sub)
217                         continue;
218
219                 event = ast_event_new(AST_EVENT_SUB,
220                         AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
221                         AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
222                         AST_EVENT_IE_END);
223
224                 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
225                         switch (ie_val->ie_pltype) {
226                         case AST_EVENT_IE_PLTYPE_EXISTS:
227                                 ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
228                                 break;
229                         case AST_EVENT_IE_PLTYPE_UINT:
230                                 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
231                                 break;
232                         case AST_EVENT_IE_PLTYPE_STR:
233                                 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
234                                 break;
235                         }
236                         if (!event)
237                                 break;
238                 }
239
240                 if (!event)
241                         continue;
242
243                 event_sub->cb(event, event_sub->userdata);
244
245                 ast_event_destroy(event);
246         }
247         AST_RWLIST_UNLOCK(&ast_event_subs[event_type]);
248 }
249
250 struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb, 
251         void *userdata, ...)
252 {
253         va_list ap;
254         enum ast_event_ie_type ie_type;
255         struct ast_event_sub *sub;
256         struct ast_event *event;
257
258         if (type >= AST_EVENT_TOTAL) {
259                 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
260                 return NULL;
261         }
262
263         if (!(sub = ast_calloc(1, sizeof(*sub))))
264                 return NULL;
265
266         va_start(ap, userdata);
267         for (ie_type = va_arg(ap, enum ast_event_type);
268                 ie_type != AST_EVENT_IE_END;
269                 ie_type = va_arg(ap, enum ast_event_type))
270         {
271                 struct ast_event_ie_val *ie_val;
272                 if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
273                         continue;
274                 ie_val->ie_type = ie_type;
275                 ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
276                 if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
277                         ie_val->payload.uint = va_arg(ap, uint32_t);
278                 else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) {
279                         if (!(ie_val->payload.str = ast_strdup(va_arg(ap, const char *)))) {
280                                 ast_free(ie_val);
281                                 continue;
282                         }
283                 }
284                 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
285         }
286         va_end(ap);
287
288         sub->type = type;
289         sub->cb = cb;
290         sub->userdata = userdata;
291         sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
292
293         if (ast_event_check_subscriber(AST_EVENT_SUB,
294                 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, type,
295                 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
296                 struct ast_event_ie_val *ie_val;
297
298                 event = ast_event_new(AST_EVENT_SUB,
299                         AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
300                         AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
301                         AST_EVENT_IE_END);
302
303                 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
304                         switch (ie_val->ie_pltype) {
305                         case AST_EVENT_IE_PLTYPE_EXISTS:
306                                 ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
307                                 break;
308                         case AST_EVENT_IE_PLTYPE_UINT:
309                                 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
310                                 break;
311                         case AST_EVENT_IE_PLTYPE_STR:
312                                 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
313                                 break;
314                         }
315                         if (!event)
316                                 break;
317                 }
318
319                 if (event)
320                         ast_event_queue(event);
321         }
322
323         AST_RWLIST_WRLOCK(&ast_event_subs[type]);
324         AST_RWLIST_INSERT_TAIL(&ast_event_subs[type], sub, entry);
325         AST_RWLIST_UNLOCK(&ast_event_subs[type]);
326
327         return sub;
328 }
329
330 static void ast_event_sub_destroy(struct ast_event_sub *sub)
331 {
332         struct ast_event_ie_val *ie_val;
333
334         while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry)))
335                 ast_event_ie_val_destroy(ie_val);
336
337         ast_free(sub);
338 }
339
340 void ast_event_unsubscribe(struct ast_event_sub *sub)
341 {
342         struct ast_event *event;
343
344         AST_RWLIST_WRLOCK(&ast_event_subs[sub->type]);
345         AST_LIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
346         AST_RWLIST_UNLOCK(&ast_event_subs[sub->type]);
347
348         if (ast_event_check_subscriber(AST_EVENT_UNSUB,
349                 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
350                 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
351                 
352                 event = ast_event_new(AST_EVENT_UNSUB,
353                         AST_EVENT_IE_UNIQUEID,  AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
354                         AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
355                         AST_EVENT_IE_END);
356
357                 if (event)
358                         ast_event_queue(event);
359         }
360
361         ast_event_sub_destroy(sub);
362 }
363
364 void ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
365 {
366         iterator->event_len = ntohs(event->event_len);
367         iterator->event = event;
368         iterator->ie = ((void *) event) + sizeof(*event);
369         return;
370 }
371
372 int ast_event_iterator_next(struct ast_event_iterator *iterator)
373 {
374         iterator->ie = ((void *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len);
375         return ((iterator->event_len < (((void *) iterator->ie) - ((void *) iterator->event))) ? -1 : 0);
376 }
377
378 enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
379 {
380         return iterator->ie->ie_type;
381 }
382
383 uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
384 {
385         return ntohl(*iterator->ie->ie_payload);
386 }
387
388 const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
389 {
390         return (const char*)iterator->ie->ie_payload;
391 }
392
393 void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
394 {
395         return iterator->ie->ie_payload;
396 }
397
398 enum ast_event_type ast_event_get_type(const struct ast_event *event)
399 {
400         return ntohs(event->type);
401 }
402
403 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
404 {
405         const uint32_t *ie_val;
406
407         ie_val = ast_event_get_ie_raw(event, ie_type);
408
409         return ie_val ? ntohl(*ie_val) : 0;
410 }
411
412 const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
413 {
414         return ast_event_get_ie_raw(event, ie_type);
415 }
416
417 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
418 {
419         struct ast_event_iterator iterator;
420         int res = 0;
421
422         ie_type = ntohs(ie_type);
423
424         for (ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
425                 if (ast_event_iterator_get_ie_type(&iterator) == ie_type)
426                         return ast_event_iterator_get_ie_raw(&iterator);
427         }
428
429         return NULL;
430 }
431
432 int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
433         const char *str)
434 {
435         return ast_event_append_ie_raw(event, ie_type, str, strlen(str) + 1);
436 }
437
438 int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
439         uint32_t data)
440 {
441         data = htonl(data);
442         return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
443 }
444
445 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
446         const void *data, size_t data_len)
447 {
448         struct ast_event_ie *ie;
449         unsigned int extra_len;
450         uint16_t event_len;
451
452         event_len = ntohs((*event)->event_len);
453         extra_len = sizeof(*ie) + data_len;
454
455         if (!(*event = ast_realloc(*event, event_len + extra_len)))
456                 return -1;
457
458         ie = ((void *) *event) + event_len;
459         ie->ie_type = htons(ie_type);
460         ie->ie_payload_len = htons(data_len);
461         memcpy(ie->ie_payload, data, data_len);
462
463         (*event)->event_len = htons(event_len + extra_len);
464
465         return 0;
466 }
467
468 struct ast_event *ast_event_new(enum ast_event_type type, ...)
469 {
470         va_list ap;
471         struct ast_event *event;
472         enum ast_event_type ie_type;
473         struct ast_event_ie_val *ie_val;
474         AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
475
476         /* Invalid type */
477         if (type >= AST_EVENT_TOTAL) {
478                 ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
479                         "type '%d'!\n", type);
480                 return NULL;
481         }
482
483         va_start(ap, type);
484         for (ie_type = va_arg(ap, enum ast_event_type);
485                 ie_type != AST_EVENT_IE_END;
486                 ie_type = va_arg(ap, enum ast_event_type))
487         {
488                 struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
489                 memset(ie_val, 0, sizeof(*ie_val));
490                 ie_val->ie_type = ie_type;
491                 ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
492                 if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
493                         ie_val->payload.uint = va_arg(ap, uint32_t);
494                 else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
495                         ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
496                 AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
497         }
498         va_end(ap);
499
500         if (!(event = ast_calloc(1, sizeof(*event))))
501                 return NULL;
502
503         event->type = htons(type);
504         event->event_len = htons(sizeof(*event));
505
506         AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
507                 if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
508                         ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
509                 else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
510                         ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
511
512                 if (!event)
513                         break;
514         }
515
516         return event;
517 }
518
519 void ast_event_destroy(struct ast_event *event)
520 {
521         ast_free(event);
522 }
523
524 static void ast_event_ref_destroy(struct ast_event_ref *event_ref)
525 {
526         ast_event_destroy(event_ref->event);
527         ast_free(event_ref);
528 }
529
530 static struct ast_event *ast_event_dup(const struct ast_event *event)
531 {
532         struct ast_event *dup_event;
533         uint16_t event_len;
534
535         event_len = ntohs(event->event_len);
536
537         if (!(dup_event = ast_calloc(1, event_len)))
538                 return NULL;
539         
540         memcpy(dup_event, event, event_len);
541
542         return dup_event;
543 }
544
545 struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
546 {
547         va_list ap;
548         enum ast_event_ie_type ie_type;
549         struct ast_event *dup_event = NULL;
550         struct ast_event_ref *event_ref;
551         struct cache_arg {
552                 AST_LIST_ENTRY(cache_arg) entry;
553                 enum ast_event_ie_type ie_type;
554                 enum ast_event_ie_pltype ie_pltype;
555                 union {
556                         uint32_t uint;
557                         const char *str;
558                 } payload;
559         } *cache_arg;
560         AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
561
562         if (type >= AST_EVENT_TOTAL) {
563                 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
564                 return NULL;
565         }
566
567         va_start(ap, type);
568         for (ie_type = va_arg(ap, enum ast_event_type);
569                 ie_type != AST_EVENT_IE_END;
570                 ie_type = va_arg(ap, enum ast_event_type))
571         {
572                 cache_arg = alloca(sizeof(*cache_arg));
573                 memset(cache_arg, 0, sizeof(*cache_arg));
574                 cache_arg->ie_type = ie_type;
575                 cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
576                 if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
577                         cache_arg->payload.uint = va_arg(ap, uint32_t);
578                 else if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
579                         cache_arg->payload.str = ast_strdupa(va_arg(ap, const char *));
580                 AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
581         }
582         va_end(ap);
583
584         if (AST_LIST_EMPTY(&cache_args)) {
585                 ast_log(LOG_ERROR, "Events can not be retrieved from the cache without "
586                         "specifying at least one IE type!\n");
587                 return NULL;
588         }
589
590         AST_RWLIST_RDLOCK(&ast_event_cache[type]);
591         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[type], event_ref, entry) {
592                 AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
593                         if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
594                            (cache_arg->payload.uint ==
595                             ast_event_get_ie_uint(event_ref->event, cache_arg->ie_type))) ||
596
597                            (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
598                            (!strcmp(cache_arg->payload.str,
599                              ast_event_get_ie_str(event_ref->event, cache_arg->ie_type)))) ||
600
601                            (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
602                             ast_event_get_ie_raw(event_ref->event, cache_arg->ie_type)) ) ) 
603                         {
604                                 break;  
605                         }
606                 }
607                 if (!cache_arg) {
608                         /* All parameters were matched on this cache entry, so return it */
609                         dup_event = ast_event_dup(event_ref->event);
610                         break;
611                 }
612         }
613         AST_RWLIST_TRAVERSE_SAFE_END
614         AST_RWLIST_UNLOCK(&ast_event_cache[type]);
615
616         return dup_event;
617 }
618
619 /*! \brief Duplicate an event and add it to the cache
620  * \note This assumes this index in to the cache is locked */
621 static int ast_event_dup_and_cache(const struct ast_event *event)
622 {
623         struct ast_event *dup_event;
624         struct ast_event_ref *event_ref;
625
626         if (!(dup_event = ast_event_dup(event)))
627                 return -1;
628         if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
629                 return -1;
630         
631         event_ref->event = dup_event;
632
633         AST_LIST_INSERT_TAIL(&ast_event_cache[ntohs(event->type)], event_ref, entry);
634
635         return 0;
636 }
637
638 int ast_event_queue_and_cache(struct ast_event *event, ...)
639 {
640         va_list ap;
641         enum ast_event_type ie_type;
642         uint16_t host_event_type;
643         struct ast_event_ref *event_ref;
644         int res;
645         struct cache_arg {
646                 AST_LIST_ENTRY(cache_arg) entry;
647                 enum ast_event_ie_type ie_type;
648                 enum ast_event_ie_pltype ie_pltype;
649         } *cache_arg;
650         AST_LIST_HEAD_NOLOCK_STATIC(cache_args, cache_arg);
651
652         host_event_type = ntohs(event->type);
653
654         /* Invalid type */
655         if (host_event_type >= AST_EVENT_TOTAL) {
656                 ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
657                         "type '%d'!\n", host_event_type);
658                 return -1;
659         }
660
661         va_start(ap, event);
662         for (ie_type = va_arg(ap, enum ast_event_type);
663                 ie_type != AST_EVENT_IE_END;
664                 ie_type = va_arg(ap, enum ast_event_type))
665         {
666                 cache_arg = alloca(sizeof(*cache_arg));
667                 memset(cache_arg, 0, sizeof(*cache_arg));
668                 cache_arg->ie_type = ie_type;
669                 cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
670                 AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
671         }
672         va_end(ap);
673
674         if (AST_LIST_EMPTY(&cache_args)) {
675                 ast_log(LOG_ERROR, "Events can not be cached without specifying at "
676                         "least one IE type!\n");
677                 return ast_event_queue(event);
678         }
679  
680         AST_RWLIST_WRLOCK(&ast_event_cache[host_event_type]);
681         AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[host_event_type], event_ref, entry) {
682                 AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
683                         if ( ! ( (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
684                            (ast_event_get_ie_uint(event, cache_arg->ie_type) ==
685                             ast_event_get_ie_uint(event_ref->event, cache_arg->ie_type))) ||
686
687                            (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
688                            (!strcmp(ast_event_get_ie_str(event, cache_arg->ie_type),
689                              ast_event_get_ie_str(event_ref->event, cache_arg->ie_type)))) ||
690
691                            (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
692                             ast_event_get_ie_raw(event_ref->event, cache_arg->ie_type)) ) )
693                         {
694                                 break;  
695                         }
696                 }
697                 if (!cache_arg) {
698                         /* All parameters were matched on this cache entry, so remove it */
699                         AST_LIST_REMOVE_CURRENT(&ast_event_cache[host_event_type], entry);
700                         ast_event_ref_destroy(event_ref);
701                 }
702         }
703         AST_RWLIST_TRAVERSE_SAFE_END
704         res = ast_event_dup_and_cache(event);
705         AST_RWLIST_UNLOCK(&ast_event_cache[host_event_type]);
706
707         return (ast_event_queue(event) || res) ? -1 : 0;
708 }
709
710 int ast_event_queue(struct ast_event *event)
711 {
712         struct ast_event_ref *event_ref;
713         uint16_t host_event_type;
714
715         host_event_type = ntohs(event->type);
716
717         /* Invalid type */
718         if (host_event_type >= AST_EVENT_TOTAL) {
719                 ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
720                         "type '%d'!\n", host_event_type);
721                 return -1;
722         }
723
724         /* If nobody has subscribed to this event type, throw it away now */
725         if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END) 
726                 == AST_EVENT_SUB_NONE) {
727                 ast_event_destroy(event);
728                 return 0;
729         }
730
731         if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
732                 return -1;
733
734         event_ref->event = event;
735
736         ast_mutex_lock(&event_thread.lock);
737         AST_LIST_INSERT_TAIL(&event_thread.event_q, event_ref, entry);
738         ast_cond_signal(&event_thread.cond);
739         ast_mutex_unlock(&event_thread.lock);
740
741         return 0;
742 }
743
744 static void *ast_event_dispatcher(void *unused)
745 {
746         for (;;) {
747                 struct ast_event_ref *event_ref;
748                 struct ast_event_sub *sub;
749                 uint16_t host_event_type;
750
751                 ast_mutex_lock(&event_thread.lock);
752                 while (!(event_ref = AST_LIST_REMOVE_HEAD(&event_thread.event_q, entry)))
753                         ast_cond_wait(&event_thread.cond, &event_thread.lock);
754                 ast_mutex_unlock(&event_thread.lock);
755
756                 host_event_type = ntohs(event_ref->event->type);
757
758                 /* Subscribers to this specific event first */
759                 AST_RWLIST_RDLOCK(&ast_event_subs[host_event_type]);
760                 AST_RWLIST_TRAVERSE(&ast_event_subs[host_event_type], sub, entry) {
761                         struct ast_event_ie_val *ie_val;
762                         AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
763                                 if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS &&
764                                         ast_event_get_ie_raw(event_ref->event, ie_val->ie_type)) {
765                                         continue;
766                                 } else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
767                                         ast_event_get_ie_uint(event_ref->event, ie_val->ie_type) 
768                                         == ie_val->payload.uint) {
769                                         continue;
770                                 } else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
771                                         !strcmp(ast_event_get_ie_str(event_ref->event, ie_val->ie_type),
772                                                 ie_val->payload.str)) {
773                                         continue;
774                                 }
775                                 break;
776                         }
777                         if (ie_val)
778                                 continue;
779                         sub->cb(event_ref->event, sub->userdata);
780                 }
781                 AST_RWLIST_UNLOCK(&ast_event_subs[host_event_type]);
782
783                 /* Now to subscribers to all event types */
784                 AST_RWLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
785                 AST_RWLIST_TRAVERSE(&ast_event_subs[AST_EVENT_ALL], sub, entry)
786                         sub->cb(event_ref->event, sub->userdata);
787                 AST_RWLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
788
789                 ast_event_ref_destroy(event_ref);
790         }
791
792         return NULL;
793 }
794
795 void ast_event_init(void)
796 {
797         int i;
798
799         for (i = 0; i < AST_EVENT_TOTAL; i++)
800                 AST_RWLIST_HEAD_INIT(&ast_event_subs[i]);
801
802         for (i = 0; i < AST_EVENT_TOTAL; i++)
803                 AST_RWLIST_HEAD_INIT(&ast_event_cache[i]);
804
805         ast_cond_init(&event_thread.cond, NULL);
806
807         for (i = 0; i < NUM_EVENT_THREADS; i++) {
808                 pthread_t dont_care;
809                 ast_pthread_create_background(&dont_care, NULL, ast_event_dispatcher, NULL);
810         }
811 }