Newer
Older
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2007 - 2008, Digium, Inc.
*
* Russell Bryant <russell@digium.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Internal generic event system
*
*/
#include "asterisk.h"
ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
#include "asterisk/_private.h"
#include "asterisk/event.h"
#include "asterisk/linkedlists.h"
#include "asterisk/dlinkedlists.h"
#include "asterisk/lock.h"
#include "asterisk/utils.h"
#include "asterisk/unaligned.h"
#include "asterisk/utils.h"
Russell Bryant
committed
#include "asterisk/taskprocessor.h"
Russell Bryant
committed
struct ast_taskprocessor *event_dispatcher;
/*!
* \brief An event information element
*
* \note The format of this structure is important. Since these events may
* be sent directly over a network, changing this structure will break
* compatibility with older versions. However, at this point, this code
* has not made it into a release, so it is still fair game for change.
*/
struct ast_event_ie {
enum ast_event_ie_type ie_type:16;
/*! Total length of the IE payload */
uint16_t ie_payload_len;
unsigned char ie_payload[0];
} __attribute__ ((packed));
/*!
* \brief An event
*
* An ast_event consists of an event header (this structure), and zero or
* more information elements defined by ast_event_ie.
* \note The format of this structure is important. Since these events may
* be sent directly over a network, changing this structure will break
* compatibility with older versions. However, at this point, this code
* has not made it into a release, so it is still fair game for change.
*/
struct ast_event {
/*! Event type */
enum ast_event_type type:16;
/*! Total length of the event */
uint16_t event_len:16;
/*! The data payload of the event, made up of information elements */
unsigned char payload[0];
} __attribute__ ((packed));
struct ast_event_ref {
struct ast_event *event;
AST_LIST_ENTRY(ast_event_ref) entry;
};
struct ast_event_ie_val {
AST_LIST_ENTRY(ast_event_ie_val) entry;
enum ast_event_ie_type ie_type;
enum ast_event_ie_pltype ie_pltype;
union {
uint32_t uint;
const char *str;
};
/*! \brief Event subscription */
struct ast_event_sub {
enum ast_event_type type;
ast_event_cb_t cb;
void *userdata;
uint32_t uniqueid;
AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
AST_RWDLLIST_ENTRY(ast_event_sub) entry;
};
static uint32_t sub_uniqueid;
/*! \brief Event subscriptions
* The event subscribers are indexed by which event they are subscribed to */
static AST_RWDLLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
/*! \brief Cached events
* The event cache is indexed on the event type. The purpose of this is
* for events that express some sort of state. So, when someone first
* needs to know this state, it can get the last known state from the cache. */
static AST_RWLIST_HEAD(ast_event_ref_list, ast_event_ref) ast_event_cache[AST_EVENT_TOTAL];
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*!
* The index of each entry _must_ match the event type number!
*/
static struct event_name {
enum ast_event_type type;
const char *name;
} event_names[] = {
{ 0, "" },
{ AST_EVENT_CUSTOM, "Custom" },
{ AST_EVENT_MWI, "MWI" },
{ AST_EVENT_SUB, "Subscription" },
{ AST_EVENT_UNSUB, "Unsubscription" },
{ AST_EVENT_DEVICE_STATE, "DeviceState" },
{ AST_EVENT_DEVICE_STATE_CHANGE, "DeviceStateChange" },
};
/*!
* The index of each entry _must_ match the event ie number!
*/
static struct ie_map {
enum ast_event_ie_type ie_type;
enum ast_event_ie_pltype ie_pltype;
const char *name;
} ie_maps[] = {
{ 0, 0, "" },
{ AST_EVENT_IE_NEWMSGS, AST_EVENT_IE_PLTYPE_UINT, "NewMessages" },
{ AST_EVENT_IE_OLDMSGS, AST_EVENT_IE_PLTYPE_UINT, "OldMessages" },
{ AST_EVENT_IE_MAILBOX, AST_EVENT_IE_PLTYPE_STR, "Mailbox" },
{ AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, "UniqueID" },
{ AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, "EventType" },
{ AST_EVENT_IE_EXISTS, AST_EVENT_IE_PLTYPE_UINT, "Exists" },
{ AST_EVENT_IE_DEVICE, AST_EVENT_IE_PLTYPE_STR, "Device" },
{ AST_EVENT_IE_STATE, AST_EVENT_IE_PLTYPE_UINT, "State" },
{ AST_EVENT_IE_CONTEXT, AST_EVENT_IE_PLTYPE_STR, "Context" },
{ AST_EVENT_IE_EID, AST_EVENT_IE_PLTYPE_RAW, "EntityID" },
};
const char *ast_event_get_type_name(const struct ast_event *event)
{
enum ast_event_type type;
type = ast_event_get_type(event);
if (type >= AST_EVENT_TOTAL || type < 0) {
ast_log(LOG_ERROR, "Invalid event type - '%d'\n", type);
return "";
}
return event_names[type].name;
}
int ast_event_str_to_event_type(const char *str, enum ast_event_type *event_type)
{
int i;
for (i = 0; i < ARRAY_LEN(event_names); i++) {
if (strcasecmp(event_names[i].name, str))
continue;
*event_type = event_names[i].type;
return 0;
}
return -1;
}
const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
{
if (ie_type <= 0 || ie_type > AST_EVENT_IE_MAX) {
ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
return "";
}
if (ie_maps[ie_type].ie_type != ie_type) {
ast_log(LOG_ERROR, "The ie type passed in does not match the ie type defined in the ie table.\n");
return "";
}
return ie_maps[ie_type].name;
}
enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
{
if (ie_type <= 0 || ie_type > AST_EVENT_IE_MAX) {
ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
return AST_EVENT_IE_PLTYPE_UNKNOWN;
}
if (ie_maps[ie_type].ie_type != ie_type) {
ast_log(LOG_ERROR, "The ie type passed in does not match the ie type defined in the ie table.\n");
return AST_EVENT_IE_PLTYPE_UNKNOWN;
}
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
return ie_maps[ie_type].ie_pltype;
}
int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type)
{
int i;
for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
if (strcasecmp(ie_maps[i].name, str))
continue;
*ie_type = ie_maps[i].ie_type;
return 0;
}
return -1;
}
size_t ast_event_get_size(const struct ast_event *event)
{
size_t res;
res = ntohs(event->event_len);
return res;
}
static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
{
switch (ie_val->ie_pltype) {
case AST_EVENT_IE_PLTYPE_STR:
case AST_EVENT_IE_PLTYPE_RAW:
ast_free(ie_val->payload.raw);
break;
case AST_EVENT_IE_PLTYPE_UINT:
case AST_EVENT_IE_PLTYPE_EXISTS:
case AST_EVENT_IE_PLTYPE_UNKNOWN:
break;
}
Tilghman Lesher
committed
ast_free(ie_val);
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
}
enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
{
va_list ap;
enum ast_event_ie_type ie_type;
enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
struct ast_event_ie_val *ie_val, *sub_ie_val;
struct ast_event_sub *sub;
AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
if (type >= AST_EVENT_TOTAL) {
ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
return res;
}
va_start(ap, type);
for (ie_type = va_arg(ap, enum ast_event_type);
ie_type != AST_EVENT_IE_END;
ie_type = va_arg(ap, enum ast_event_type))
{
struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
memset(ie_val, 0, sizeof(*ie_val));
ie_val->ie_type = ie_type;
ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
ie_val->payload.uint = va_arg(ap, uint32_t);
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) {
void *data = va_arg(ap, void *);
size_t datalen = va_arg(ap, size_t);
ie_val->payload.raw = alloca(datalen);
memcpy(ie_val->payload.raw, data, datalen);
ie_val->raw_datalen = datalen;
}
AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
}
va_end(ap);
AST_RWDLLIST_RDLOCK(&ast_event_subs[type]);
AST_RWDLLIST_TRAVERSE(&ast_event_subs[type], sub, entry) {
AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
if (sub_ie_val->ie_type == ie_val->ie_type)
break;
}
if (!sub_ie_val) {
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
break;
continue;
}
/* The subscriber doesn't actually care what the value is */
if (sub_ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS)
continue;
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT &&
ie_val->payload.uint != sub_ie_val->payload.uint)
break;
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR &&
strcmp(ie_val->payload.str, sub_ie_val->payload.str))
break;
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW &&
memcmp(ie_val->payload.raw, sub_ie_val->payload.raw, ie_val->raw_datalen))
break;
}
if (!ie_val)
break;
}
AST_RWDLLIST_UNLOCK(&ast_event_subs[type]);
if (sub) /* All parameters were matched */
return AST_EVENT_SUB_EXISTS;
AST_RWDLLIST_RDLOCK(&ast_event_subs[AST_EVENT_ALL]);
if (!AST_DLLIST_EMPTY(&ast_event_subs[AST_EVENT_ALL]))
AST_RWDLLIST_UNLOCK(&ast_event_subs[AST_EVENT_ALL]);
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
static int match_ie_val(struct ast_event *event, struct ast_event_ie_val *ie_val, struct ast_event *event2)
{
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT) {
uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
if (val == ast_event_get_ie_uint(event, ie_val->ie_type))
return 1;
return 0;
}
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR) {
const char *str = event2 ? ast_event_get_ie_str(event2, ie_val->ie_type) : ie_val->payload.str;
if (str && !strcmp(str, ast_event_get_ie_str(event, ie_val->ie_type)))
return 1;
return 0;
}
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) {
const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
if (buf && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_val->raw_datalen))
return 1;
return 0;
}
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_EXISTS) {
if (ast_event_get_ie_raw(event, ie_val->ie_type))
return 1;
return 0;
}
return 0;
}
/*! \brief Dump the event cache for the subscribed event type */
void ast_event_dump_cache(const struct ast_event_sub *event_sub)
{
struct ast_event_ref *event_ref;
enum ast_event_type type = event_sub->type;
AST_RWLIST_RDLOCK(&ast_event_cache[type]);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[type], event_ref, entry) {
struct ast_event_ie_val *ie_val;
AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
if (!match_ie_val(event_ref->event, ie_val, NULL))
break;
}
if (!ie_val) {
/* All parameters were matched on this cache entry, so dump it */
event_sub->cb(event_ref->event, event_sub->userdata);
}
}
AST_RWLIST_TRAVERSE_SAFE_END
AST_RWLIST_UNLOCK(&ast_event_cache[type]);
}
static struct ast_event *gen_sub_event(struct ast_event_sub *sub)
{
struct ast_event_ie_val *ie_val;
struct ast_event *event;
event = ast_event_new(AST_EVENT_SUB,
AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
AST_EVENT_IE_END);
if (!event)
return NULL;
AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
switch (ie_val->ie_pltype) {
case AST_EVENT_IE_PLTYPE_UNKNOWN:
break;
case AST_EVENT_IE_PLTYPE_EXISTS:
ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
break;
case AST_EVENT_IE_PLTYPE_UINT:
ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
break;
case AST_EVENT_IE_PLTYPE_STR:
ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
break;
case AST_EVENT_IE_PLTYPE_RAW:
ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
break;
}
if (!event)
break;
}
return event;
}
/*! \brief Send AST_EVENT_SUB events to this subscriber of ... subscriber events */
void ast_event_report_subs(const struct ast_event_sub *event_sub)
{
struct ast_event *event;
struct ast_event_sub *sub;
enum ast_event_type event_type = -1;
struct ast_event_ie_val *ie_val;
if (event_sub->type != AST_EVENT_SUB)
return;
AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
event_type = ie_val->payload.uint;
break;
}
}
if (event_type == -1)
return;
AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
if (event_sub == sub)
continue;
event = gen_sub_event(sub);
if (!event)
continue;
event_sub->cb(event, event_sub->userdata);
ast_event_destroy(event);
}
AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type,
ast_event_cb_t cb, void *userdata)
{
struct ast_event_sub *sub;
if (type < 0 || type >= AST_EVENT_TOTAL) {
ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
return NULL;
}
if (!(sub = ast_calloc(1, sizeof(*sub))))
return NULL;
sub->type = type;
sub->cb = cb;
sub->userdata = userdata;
sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
return sub;
}
int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
enum ast_event_ie_type ie_type, uint32_t uint)
{
struct ast_event_ie_val *ie_val;
if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
return -1;
if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
return -1;
ie_val->ie_type = ie_type;
ie_val->payload.uint = uint;
ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
return 0;
}
int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
enum ast_event_ie_type ie_type)
{
struct ast_event_ie_val *ie_val;
if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
return -1;
if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
return -1;
ie_val->ie_type = ie_type;
ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
return 0;
}
int ast_event_sub_append_ie_str(struct ast_event_sub *sub,
enum ast_event_ie_type ie_type, const char *str)
{
struct ast_event_ie_val *ie_val;
if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
return -1;
if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
return -1;
ie_val->ie_type = ie_type;
ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
if (!(ie_val->payload.str = ast_strdup(str))) {
ast_free(ie_val);
return -1;
}
AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
return 0;
}
int ast_event_sub_append_ie_raw(struct ast_event_sub *sub,
enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
{
struct ast_event_ie_val *ie_val;
if (ie_type < 0 || ie_type > AST_EVENT_IE_MAX)
return -1;
if (!(ie_val = ast_calloc(1, sizeof(*ie_val))))
return -1;
ie_val->ie_type = ie_type;
ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
ie_val->raw_datalen = raw_datalen;
if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
ast_free(ie_val);
return -1;
}
memcpy(ie_val->payload.raw, data, raw_datalen);
AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
return 0;
}
int ast_event_sub_activate(struct ast_event_sub *sub)
{
if (ast_event_check_subscriber(AST_EVENT_SUB,
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
struct ast_event *event;
event = gen_sub_event(sub);
if (event)
ast_event_queue(event);
}
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
return 0;
}
struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
void *userdata, ...)
{
va_list ap;
enum ast_event_ie_type ie_type;
struct ast_event_sub *sub;
if (!(sub = ast_event_subscribe_new(type, cb, userdata)))
return NULL;
va_start(ap, userdata);
for (ie_type = va_arg(ap, enum ast_event_type);
ie_type != AST_EVENT_IE_END;
ie_type = va_arg(ap, enum ast_event_type))
{
enum ast_event_ie_pltype ie_pltype;
ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
switch (ie_pltype) {
case AST_EVENT_IE_PLTYPE_UNKNOWN:
break;
case AST_EVENT_IE_PLTYPE_UINT:
{
uint32_t uint = va_arg(ap, uint32_t);
ast_event_sub_append_ie_uint(sub, ie_type, uint);
break;
}
case AST_EVENT_IE_PLTYPE_STR:
{
const char *str = va_arg(ap, const char *);
ast_event_sub_append_ie_str(sub, ie_type, str);
break;
}
case AST_EVENT_IE_PLTYPE_RAW:
{
void *data = va_arg(ap, void *);
size_t data_len = va_arg(ap, size_t);
ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
break;
}
case AST_EVENT_IE_PLTYPE_EXISTS:
ast_event_sub_append_ie_exists(sub, ie_type);
break;
}
}
va_end(ap);
ast_event_sub_activate(sub);
void ast_event_sub_destroy(struct ast_event_sub *sub)
{
struct ast_event_ie_val *ie_val;
while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry)))
ast_event_ie_val_destroy(ie_val);
Tilghman Lesher
committed
ast_free(sub);
struct ast_event_sub *ast_event_unsubscribe(struct ast_event_sub *sub)
{
struct ast_event *event;
AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
if (ast_event_check_subscriber(AST_EVENT_UNSUB,
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
event = ast_event_new(AST_EVENT_UNSUB,
AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
AST_EVENT_IE_END);
if (event)
ast_event_queue(event);
}
ast_event_sub_destroy(sub);
Joshua Colp
committed
void ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
{
iterator->event_len = ntohs(event->event_len);
iterator->event = event;
iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
Joshua Colp
committed
return;
}
int ast_event_iterator_next(struct ast_event_iterator *iterator)
{
iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
Mark Michelson
committed
return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
Joshua Colp
committed
}
enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
{
return ntohs(iterator->ie->ie_type);
Joshua Colp
committed
}
uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
Joshua Colp
committed
{
return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
Joshua Colp
committed
}
const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
{
return (const char*)iterator->ie->ie_payload;
}
void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
{
return iterator->ie->ie_payload;
}
enum ast_event_type ast_event_get_type(const struct ast_event *event)
{
return ntohs(event->type);
}
uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
{
const uint32_t *ie_val;
ie_val = ast_event_get_ie_raw(event, ie_type);
return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
}
const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
{
return ast_event_get_ie_raw(event, ie_type);
}
const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
{
Joshua Colp
committed
struct ast_event_iterator iterator;
int res = 0;
for (ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
if (ast_event_iterator_get_ie_type(&iterator) == ie_type)
return ast_event_iterator_get_ie_raw(&iterator);
Joshua Colp
committed
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
return NULL;
}
int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
const char *str)
{
return ast_event_append_ie_raw(event, ie_type, str, strlen(str) + 1);
}
int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
uint32_t data)
{
data = htonl(data);
return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
}
int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
const void *data, size_t data_len)
{
struct ast_event_ie *ie;
unsigned int extra_len;
uint16_t event_len;
event_len = ntohs((*event)->event_len);
extra_len = sizeof(*ie) + data_len;
if (!(*event = ast_realloc(*event, event_len + extra_len)))
return -1;
ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
ie->ie_type = htons(ie_type);
ie->ie_payload_len = htons(data_len);
memcpy(ie->ie_payload, data, data_len);
(*event)->event_len = htons(event_len + extra_len);
return 0;
}
struct ast_event *ast_event_new(enum ast_event_type type, ...)
{
va_list ap;
struct ast_event *event;
enum ast_event_type ie_type;
struct ast_event_ie_val *ie_val;
AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
/* Invalid type */
if (type >= AST_EVENT_TOTAL) {
ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
"type '%d'!\n", type);
return NULL;
}
va_start(ap, type);
for (ie_type = va_arg(ap, enum ast_event_type);
ie_type != AST_EVENT_IE_END;
ie_type = va_arg(ap, enum ast_event_type))
{
struct ast_event_ie_val *ie_val = alloca(sizeof(*ie_val));
memset(ie_val, 0, sizeof(*ie_val));
ie_val->ie_type = ie_type;
ie_val->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
ie_val->payload.uint = va_arg(ap, uint32_t);
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
ie_val->payload.str = ast_strdupa(va_arg(ap, const char *));
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) {
void *data = va_arg(ap, void *);
size_t datalen = va_arg(ap, size_t);
ie_val->payload.raw = alloca(datalen);
memcpy(ie_val->payload.raw, data, datalen);
ie_val->raw_datalen = datalen;
}
AST_LIST_INSERT_TAIL(&ie_vals, ie_val, entry);
}
va_end(ap);
if (!(event = ast_calloc(1, sizeof(*event))))
return NULL;
event->type = htons(type);
event->event_len = htons(sizeof(*event));
AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
else if (ie_val->ie_pltype == AST_EVENT_IE_PLTYPE_RAW)
ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
if (!event)
break;
}
if (!ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
/* If the event is originating on this server, add the server's
* entity ID to the event. */
ast_event_append_ie_raw(&event, AST_EVENT_IE_EID, &g_eid, sizeof(g_eid));
}
return event;
}
void ast_event_destroy(struct ast_event *event)
{
Tilghman Lesher
committed
ast_free(event);
}
static void ast_event_ref_destroy(struct ast_event_ref *event_ref)
{
ast_event_destroy(event_ref->event);
Tilghman Lesher
committed
ast_free(event_ref);
}
static struct ast_event *ast_event_dup(const struct ast_event *event)
{
struct ast_event *dup_event;
uint16_t event_len;
event_len = ast_event_get_size(event);
if (!(dup_event = ast_calloc(1, event_len)))
return NULL;
memcpy(dup_event, event, event_len);
return dup_event;
}
struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
{
va_list ap;
enum ast_event_ie_type ie_type;
struct ast_event *dup_event = NULL;
struct ast_event_ref *event_ref;
struct ast_event_ie_val *cache_arg;
AST_LIST_HEAD_NOLOCK_STATIC(cache_args, ast_event_ie_val);
if (type >= AST_EVENT_TOTAL) {
ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
return NULL;
}
va_start(ap, type);
for (ie_type = va_arg(ap, enum ast_event_type);
ie_type != AST_EVENT_IE_END;
ie_type = va_arg(ap, enum ast_event_type))
{
cache_arg = alloca(sizeof(*cache_arg));
memset(cache_arg, 0, sizeof(*cache_arg));
cache_arg->ie_type = ie_type;
cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_UINT)
cache_arg->payload.uint = va_arg(ap, uint32_t);
else if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_STR)
cache_arg->payload.str = ast_strdupa(va_arg(ap, const char *));
else if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_RAW) {
void *data = va_arg(ap, void *);
size_t datalen = va_arg(ap, size_t);
cache_arg->payload.raw = alloca(datalen);
memcpy(cache_arg->payload.raw, data, datalen);
cache_arg->raw_datalen = datalen;
}
AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
}
va_end(ap);
if (AST_LIST_EMPTY(&cache_args)) {
ast_log(LOG_ERROR, "Events can not be retrieved from the cache without "
"specifying at least one IE type!\n");
return NULL;
}
AST_RWLIST_RDLOCK(&ast_event_cache[type]);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[type], event_ref, entry) {
AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
if (!match_ie_val(event_ref->event, cache_arg, NULL))
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
break;
}
if (!cache_arg) {
/* All parameters were matched on this cache entry, so return it */
dup_event = ast_event_dup(event_ref->event);
break;
}
}
AST_RWLIST_TRAVERSE_SAFE_END
AST_RWLIST_UNLOCK(&ast_event_cache[type]);
return dup_event;
}
/*! \brief Duplicate an event and add it to the cache
* \note This assumes this index in to the cache is locked */
static int ast_event_dup_and_cache(const struct ast_event *event)
{
struct ast_event *dup_event;
struct ast_event_ref *event_ref;
if (!(dup_event = ast_event_dup(event)))
return -1;
if (!(event_ref = ast_calloc(1, sizeof(*event_ref))))
return -1;
event_ref->event = dup_event;
AST_LIST_INSERT_TAIL(&ast_event_cache[ntohs(event->type)], event_ref, entry);
return 0;
}
int ast_event_queue_and_cache(struct ast_event *event, ...)
{
va_list ap;
enum ast_event_type ie_type;
uint16_t host_event_type;
struct ast_event_ref *event_ref;
int res;
struct ast_event_ie_val *cache_arg;
AST_LIST_HEAD_NOLOCK_STATIC(cache_args, ast_event_ie_val);
host_event_type = ntohs(event->type);
/* Invalid type */
if (host_event_type >= AST_EVENT_TOTAL) {
ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
"type '%d'!\n", host_event_type);
return -1;
}
va_start(ap, event);
for (ie_type = va_arg(ap, enum ast_event_type);
ie_type != AST_EVENT_IE_END;
ie_type = va_arg(ap, enum ast_event_type))
{
cache_arg = alloca(sizeof(*cache_arg));
memset(cache_arg, 0, sizeof(*cache_arg));
cache_arg->ie_type = ie_type;
cache_arg->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
if (cache_arg->ie_pltype == AST_EVENT_IE_PLTYPE_RAW)
cache_arg->raw_datalen = va_arg(ap, size_t);
AST_LIST_INSERT_TAIL(&cache_args, cache_arg, entry);
}
va_end(ap);
if (AST_LIST_EMPTY(&cache_args)) {
ast_log(LOG_ERROR, "Events can not be cached without specifying at "
"least one IE type!\n");
return ast_event_queue(event);
}
AST_RWLIST_WRLOCK(&ast_event_cache[host_event_type]);
AST_RWLIST_TRAVERSE_SAFE_BEGIN(&ast_event_cache[host_event_type], event_ref, entry) {
AST_LIST_TRAVERSE(&cache_args, cache_arg, entry) {
if (!match_ie_val(event_ref->event, cache_arg, event))