00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include "asterisk.h"
00027
00028 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 269417 $")
00029
00030 #include "asterisk/_private.h"
00031
00032 #include "asterisk/event.h"
00033 #include "asterisk/linkedlists.h"
00034 #include "asterisk/dlinkedlists.h"
00035 #include "asterisk/lock.h"
00036 #include "asterisk/utils.h"
00037 #include "asterisk/unaligned.h"
00038 #include "asterisk/utils.h"
00039 #include "asterisk/taskprocessor.h"
00040 #include "asterisk/astobj2.h"
00041
00042 static struct ast_taskprocessor *event_dispatcher;
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 struct ast_event_ie {
00053 enum ast_event_ie_type ie_type:16;
00054
00055 uint16_t ie_payload_len;
00056 unsigned char ie_payload[0];
00057 } __attribute__((packed));
00058
00059
00060
00061
00062 struct ast_event_ie_str_payload {
00063
00064 uint32_t hash;
00065
00066 char str[1];
00067 } __attribute__((packed));
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080 struct ast_event {
00081
00082 enum ast_event_type type:16;
00083
00084 uint16_t event_len:16;
00085
00086 unsigned char payload[0];
00087 } __attribute__((packed));
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099 struct ast_event_ref {
00100 struct ast_event *event;
00101 };
00102
00103 struct ast_event_ie_val {
00104 AST_LIST_ENTRY(ast_event_ie_val) entry;
00105 enum ast_event_ie_type ie_type;
00106 enum ast_event_ie_pltype ie_pltype;
00107 union {
00108 uint32_t uint;
00109 struct {
00110 uint32_t hash;
00111 const char *str;
00112 };
00113 void *raw;
00114 } payload;
00115 size_t raw_datalen;
00116 };
00117
00118
00119 struct ast_event_sub {
00120 enum ast_event_type type;
00121 ast_event_cb_t cb;
00122 char description[64];
00123 void *userdata;
00124 uint32_t uniqueid;
00125 AST_LIST_HEAD_NOLOCK(, ast_event_ie_val) ie_vals;
00126 AST_RWDLLIST_ENTRY(ast_event_sub) entry;
00127 };
00128
00129 static uint32_t sub_uniqueid;
00130
00131
00132
00133 static AST_RWDLLIST_HEAD(ast_event_sub_list, ast_event_sub) ast_event_subs[AST_EVENT_TOTAL];
00134
00135 static int ast_event_cmp(void *obj, void *arg, int flags);
00136 static int ast_event_hash_mwi(const void *obj, const int flags);
00137 static int ast_event_hash_devstate(const void *obj, const int flags);
00138 static int ast_event_hash_devstate_change(const void *obj, const int flags);
00139
00140 #ifdef LOW_MEMORY
00141 #define NUM_CACHE_BUCKETS 17
00142 #else
00143 #define NUM_CACHE_BUCKETS 563
00144 #endif
00145
00146 #define MAX_CACHE_ARGS 8
00147
00148
00149
00150
00151 static struct {
00152
00153
00154
00155
00156
00157
00158 struct ao2_container *container;
00159
00160 ao2_hash_fn *hash_fn;
00161
00162
00163
00164
00165
00166
00167
00168
00169 enum ast_event_ie_type cache_args[MAX_CACHE_ARGS];
00170 } ast_event_cache[AST_EVENT_TOTAL] = {
00171 [AST_EVENT_MWI] = {
00172 .hash_fn = ast_event_hash_mwi,
00173 .cache_args = { AST_EVENT_IE_MAILBOX, AST_EVENT_IE_CONTEXT },
00174 },
00175 [AST_EVENT_DEVICE_STATE] = {
00176 .hash_fn = ast_event_hash_devstate,
00177 .cache_args = { AST_EVENT_IE_DEVICE, },
00178 },
00179 [AST_EVENT_DEVICE_STATE_CHANGE] = {
00180 .hash_fn = ast_event_hash_devstate_change,
00181 .cache_args = { AST_EVENT_IE_DEVICE, AST_EVENT_IE_EID, },
00182 },
00183 };
00184
00185
00186
00187
00188 static const char * const event_names[AST_EVENT_TOTAL] = {
00189 [AST_EVENT_CUSTOM] = "Custom",
00190 [AST_EVENT_MWI] = "MWI",
00191 [AST_EVENT_SUB] = "Subscription",
00192 [AST_EVENT_UNSUB] = "Unsubscription",
00193 [AST_EVENT_DEVICE_STATE] = "DeviceState",
00194 [AST_EVENT_DEVICE_STATE_CHANGE] = "DeviceStateChange",
00195 [AST_EVENT_CEL] = "CEL",
00196 [AST_EVENT_SECURITY] = "Security",
00197 };
00198
00199
00200
00201
00202 static const struct ie_map {
00203 enum ast_event_ie_pltype ie_pltype;
00204 const char *name;
00205 } ie_maps[AST_EVENT_IE_TOTAL] = {
00206 [AST_EVENT_IE_NEWMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "NewMessages" },
00207 [AST_EVENT_IE_OLDMSGS] = { AST_EVENT_IE_PLTYPE_UINT, "OldMessages" },
00208 [AST_EVENT_IE_MAILBOX] = { AST_EVENT_IE_PLTYPE_STR, "Mailbox" },
00209 [AST_EVENT_IE_UNIQUEID] = { AST_EVENT_IE_PLTYPE_UINT, "UniqueID" },
00210 [AST_EVENT_IE_EVENTTYPE] = { AST_EVENT_IE_PLTYPE_UINT, "EventType" },
00211 [AST_EVENT_IE_EXISTS] = { AST_EVENT_IE_PLTYPE_UINT, "Exists" },
00212 [AST_EVENT_IE_DEVICE] = { AST_EVENT_IE_PLTYPE_STR, "Device" },
00213 [AST_EVENT_IE_STATE] = { AST_EVENT_IE_PLTYPE_UINT, "State" },
00214 [AST_EVENT_IE_CONTEXT] = { AST_EVENT_IE_PLTYPE_STR, "Context" },
00215 [AST_EVENT_IE_EID] = { AST_EVENT_IE_PLTYPE_RAW, "EntityID" },
00216 [AST_EVENT_IE_CEL_EVENT_TYPE] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventType" },
00217 [AST_EVENT_IE_CEL_EVENT_TIME] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTime" },
00218 [AST_EVENT_IE_CEL_EVENT_TIME_USEC] = { AST_EVENT_IE_PLTYPE_UINT, "CELEventTimeUSec" },
00219 [AST_EVENT_IE_CEL_USEREVENT_NAME] = { AST_EVENT_IE_PLTYPE_UINT, "CELUserEventName" },
00220 [AST_EVENT_IE_CEL_CIDNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDName" },
00221 [AST_EVENT_IE_CEL_CIDNUM] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDNum" },
00222 [AST_EVENT_IE_CEL_EXTEN] = { AST_EVENT_IE_PLTYPE_STR, "CELExten" },
00223 [AST_EVENT_IE_CEL_CONTEXT] = { AST_EVENT_IE_PLTYPE_STR, "CELContext" },
00224 [AST_EVENT_IE_CEL_CHANNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELChanName" },
00225 [AST_EVENT_IE_CEL_APPNAME] = { AST_EVENT_IE_PLTYPE_STR, "CELAppName" },
00226 [AST_EVENT_IE_CEL_APPDATA] = { AST_EVENT_IE_PLTYPE_STR, "CELAppData" },
00227 [AST_EVENT_IE_CEL_AMAFLAGS] = { AST_EVENT_IE_PLTYPE_STR, "CELAMAFlags" },
00228 [AST_EVENT_IE_CEL_ACCTCODE] = { AST_EVENT_IE_PLTYPE_UINT, "CELAcctCode" },
00229 [AST_EVENT_IE_CEL_UNIQUEID] = { AST_EVENT_IE_PLTYPE_STR, "CELUniqueID" },
00230 [AST_EVENT_IE_CEL_USERFIELD] = { AST_EVENT_IE_PLTYPE_STR, "CELUserField" },
00231 [AST_EVENT_IE_CEL_CIDANI] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDani" },
00232 [AST_EVENT_IE_CEL_CIDRDNIS] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDrdnis" },
00233 [AST_EVENT_IE_CEL_CIDDNID] = { AST_EVENT_IE_PLTYPE_STR, "CELCIDdnid" },
00234 [AST_EVENT_IE_CEL_PEER] = { AST_EVENT_IE_PLTYPE_STR, "CELPeer" },
00235 [AST_EVENT_IE_CEL_LINKEDID] = { AST_EVENT_IE_PLTYPE_STR, "CELLinkedID" },
00236 [AST_EVENT_IE_CEL_PEERACCT] = { AST_EVENT_IE_PLTYPE_STR, "CELPeerAcct" },
00237 [AST_EVENT_IE_CEL_EXTRA] = { AST_EVENT_IE_PLTYPE_STR, "CELExtra" },
00238 [AST_EVENT_IE_SECURITY_EVENT] = { AST_EVENT_IE_PLTYPE_STR, "SecurityEvent" },
00239 [AST_EVENT_IE_EVENT_VERSION] = { AST_EVENT_IE_PLTYPE_UINT, "EventVersion" },
00240 [AST_EVENT_IE_SERVICE] = { AST_EVENT_IE_PLTYPE_STR, "Service" },
00241 [AST_EVENT_IE_MODULE] = { AST_EVENT_IE_PLTYPE_STR, "Module" },
00242 [AST_EVENT_IE_ACCOUNT_ID] = { AST_EVENT_IE_PLTYPE_STR, "AccountID" },
00243 [AST_EVENT_IE_SESSION_ID] = { AST_EVENT_IE_PLTYPE_STR, "SessionID" },
00244 [AST_EVENT_IE_SESSION_TV] = { AST_EVENT_IE_PLTYPE_STR, "SessionTV" },
00245 [AST_EVENT_IE_ACL_NAME] = { AST_EVENT_IE_PLTYPE_STR, "ACLName" },
00246 [AST_EVENT_IE_LOCAL_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "LocalAddress" },
00247 [AST_EVENT_IE_REMOTE_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "RemoteAddress" },
00248 [AST_EVENT_IE_EVENT_TV] = { AST_EVENT_IE_PLTYPE_STR, "EventTV" },
00249 [AST_EVENT_IE_REQUEST_TYPE] = { AST_EVENT_IE_PLTYPE_STR, "RequestType" },
00250 [AST_EVENT_IE_REQUEST_PARAMS] = { AST_EVENT_IE_PLTYPE_STR, "RequestParams" },
00251 [AST_EVENT_IE_AUTH_METHOD] = { AST_EVENT_IE_PLTYPE_STR, "AuthMethod" },
00252 [AST_EVENT_IE_SEVERITY] = { AST_EVENT_IE_PLTYPE_STR, "Severity" },
00253 [AST_EVENT_IE_EXPECTED_ADDR] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedAddress" },
00254 [AST_EVENT_IE_CHALLENGE] = { AST_EVENT_IE_PLTYPE_STR, "Challenge" },
00255 [AST_EVENT_IE_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "Response" },
00256 [AST_EVENT_IE_EXPECTED_RESPONSE] = { AST_EVENT_IE_PLTYPE_STR, "ExpectedResponse" },
00257 };
00258
00259 const char *ast_event_get_type_name(const struct ast_event *event)
00260 {
00261 enum ast_event_type type;
00262
00263 type = ast_event_get_type(event);
00264
00265 if (type < 0 || type >= ARRAY_LEN(event_names)) {
00266 ast_log(LOG_ERROR, "Invalid event type - '%d'\n", type);
00267 return "";
00268 }
00269
00270 return event_names[type];
00271 }
00272
00273 int ast_event_str_to_event_type(const char *str, enum ast_event_type *event_type)
00274 {
00275 int i;
00276
00277 for (i = 0; i < ARRAY_LEN(event_names); i++) {
00278 if (strcasecmp(event_names[i], str)) {
00279 continue;
00280 }
00281
00282 *event_type = i;
00283 return 0;
00284 }
00285
00286 return -1;
00287 }
00288
00289 const char *ast_event_get_ie_type_name(enum ast_event_ie_type ie_type)
00290 {
00291 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
00292 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
00293 return "";
00294 }
00295
00296 return ie_maps[ie_type].name;
00297 }
00298
00299 enum ast_event_ie_pltype ast_event_get_ie_pltype(enum ast_event_ie_type ie_type)
00300 {
00301 if (ie_type <= 0 || ie_type >= ARRAY_LEN(ie_maps)) {
00302 ast_log(LOG_ERROR, "Invalid IE type - '%d'\n", ie_type);
00303 return AST_EVENT_IE_PLTYPE_UNKNOWN;
00304 }
00305
00306 return ie_maps[ie_type].ie_pltype;
00307 }
00308
00309 int ast_event_str_to_ie_type(const char *str, enum ast_event_ie_type *ie_type)
00310 {
00311 int i;
00312
00313 for (i = 0; i < ARRAY_LEN(ie_maps); i++) {
00314 if (strcasecmp(ie_maps[i].name, str)) {
00315 continue;
00316 }
00317
00318 *ie_type = i;
00319 return 0;
00320 }
00321
00322 return -1;
00323 }
00324
00325 size_t ast_event_get_size(const struct ast_event *event)
00326 {
00327 size_t res;
00328
00329 res = ntohs(event->event_len);
00330
00331 return res;
00332 }
00333
00334 static void ast_event_ie_val_destroy(struct ast_event_ie_val *ie_val)
00335 {
00336 switch (ie_val->ie_pltype) {
00337 case AST_EVENT_IE_PLTYPE_STR:
00338 ast_free((char *) ie_val->payload.str);
00339 break;
00340 case AST_EVENT_IE_PLTYPE_RAW:
00341 ast_free(ie_val->payload.raw);
00342 break;
00343 case AST_EVENT_IE_PLTYPE_UINT:
00344 case AST_EVENT_IE_PLTYPE_BITFLAGS:
00345 case AST_EVENT_IE_PLTYPE_EXISTS:
00346 case AST_EVENT_IE_PLTYPE_UNKNOWN:
00347 break;
00348 }
00349
00350 ast_free(ie_val);
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 static int match_ie_val_to_sub(const struct ast_event_sub *sub, const struct ast_event_ie_val *ie_val)
00364 {
00365 const struct ast_event_ie_val *sub_ie_val;
00366 int res = 1;
00367
00368 AST_LIST_TRAVERSE(&sub->ie_vals, sub_ie_val, entry) {
00369 if (sub_ie_val->ie_type == ie_val->ie_type) {
00370 break;
00371 }
00372 }
00373
00374 if (!sub_ie_val) {
00375
00376
00377 return 1;
00378 }
00379
00380 switch (ie_val->ie_pltype) {
00381 case AST_EVENT_IE_PLTYPE_UINT:
00382 res = (ie_val->payload.uint != sub_ie_val->payload.uint);
00383 break;
00384 case AST_EVENT_IE_PLTYPE_BITFLAGS:
00385
00386
00387
00388
00389 res = !(ie_val->payload.uint & sub_ie_val->payload.uint);
00390 break;
00391 case AST_EVENT_IE_PLTYPE_STR:
00392 res = strcmp(ie_val->payload.str, sub_ie_val->payload.str);
00393 break;
00394 case AST_EVENT_IE_PLTYPE_RAW:
00395 res = memcmp(ie_val->payload.raw,
00396 sub_ie_val->payload.raw, ie_val->raw_datalen);
00397 break;
00398 case AST_EVENT_IE_PLTYPE_EXISTS:
00399 case AST_EVENT_IE_PLTYPE_UNKNOWN:
00400 break;
00401 }
00402
00403 return res;
00404 }
00405
00406 enum ast_event_subscriber_res ast_event_check_subscriber(enum ast_event_type type, ...)
00407 {
00408 va_list ap;
00409 enum ast_event_ie_type ie_type;
00410 enum ast_event_subscriber_res res = AST_EVENT_SUB_NONE;
00411 struct ast_event_ie_val *ie_val;
00412 struct ast_event_sub *sub;
00413 AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
00414 const enum ast_event_type event_types[] = { type, AST_EVENT_ALL };
00415 int i;
00416
00417 if (type >= AST_EVENT_TOTAL) {
00418 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
00419 return res;
00420 }
00421
00422 va_start(ap, type);
00423 for (ie_type = va_arg(ap, enum ast_event_ie_type);
00424 ie_type != AST_EVENT_IE_END;
00425 ie_type = va_arg(ap, enum ast_event_ie_type))
00426 {
00427 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
00428 int insert = 1;
00429 memset(ie_value, 0, sizeof(*ie_value));
00430 ie_value->ie_type = ie_type;
00431 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
00432 switch (ie_value->ie_pltype) {
00433 case AST_EVENT_IE_PLTYPE_UINT:
00434 ie_value->payload.uint = va_arg(ap, uint32_t);
00435 break;
00436 case AST_EVENT_IE_PLTYPE_BITFLAGS:
00437 ie_value->payload.uint = va_arg(ap, uint32_t);
00438 break;
00439 case AST_EVENT_IE_PLTYPE_STR:
00440 ie_value->payload.str = va_arg(ap, const char *);
00441 break;
00442 case AST_EVENT_IE_PLTYPE_RAW:
00443 {
00444 void *data = va_arg(ap, void *);
00445 size_t datalen = va_arg(ap, size_t);
00446 ie_value->payload.raw = alloca(datalen);
00447 memcpy(ie_value->payload.raw, data, datalen);
00448 ie_value->raw_datalen = datalen;
00449 break;
00450 }
00451 case AST_EVENT_IE_PLTYPE_UNKNOWN:
00452 insert = 0;
00453 case AST_EVENT_IE_PLTYPE_EXISTS:
00454 break;
00455 }
00456
00457 if (insert) {
00458 AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
00459 }
00460 }
00461 va_end(ap);
00462
00463 for (i = 0; i < ARRAY_LEN(event_types); i++) {
00464 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
00465 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
00466 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
00467 if (match_ie_val_to_sub(sub, ie_val)) {
00468 break;
00469 }
00470 }
00471
00472 if (!ie_val) {
00473
00474 break;
00475 }
00476 }
00477 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
00478 if (sub) {
00479 break;
00480 }
00481 }
00482
00483 return sub ? AST_EVENT_SUB_EXISTS : AST_EVENT_SUB_NONE;
00484 }
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499 static int match_ie_val(const struct ast_event *event,
00500 const struct ast_event_ie_val *ie_val, const struct ast_event *event2)
00501 {
00502 switch (ie_val->ie_pltype) {
00503 case AST_EVENT_IE_PLTYPE_UINT:
00504 {
00505 uint32_t val = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
00506
00507 return (val == ast_event_get_ie_uint(event, ie_val->ie_type)) ? 1 : 0;
00508 }
00509
00510 case AST_EVENT_IE_PLTYPE_BITFLAGS:
00511 {
00512 uint32_t flags = event2 ? ast_event_get_ie_uint(event2, ie_val->ie_type) : ie_val->payload.uint;
00513
00514
00515
00516
00517
00518 return (flags & ast_event_get_ie_bitflags(event, ie_val->ie_type)) ? 1 : 0;
00519 }
00520
00521 case AST_EVENT_IE_PLTYPE_STR:
00522 {
00523 const char *str;
00524 uint32_t hash;
00525
00526 hash = event2 ? ast_event_get_ie_str_hash(event2, ie_val->ie_type) : ie_val->payload.hash;
00527 if (hash != ast_event_get_ie_str_hash(event, ie_val->ie_type)) {
00528 return 0;
00529 }
00530
00531 str = event2 ? ast_event_get_ie_str(event2, ie_val->ie_type) : ie_val->payload.str;
00532 if (str && !strcmp(str, ast_event_get_ie_str(event, ie_val->ie_type))) {
00533 return 1;
00534 }
00535
00536 return 0;
00537 }
00538
00539 case AST_EVENT_IE_PLTYPE_RAW:
00540 {
00541 const void *buf = event2 ? ast_event_get_ie_raw(event2, ie_val->ie_type) : ie_val->payload.raw;
00542
00543 return (buf && !memcmp(buf, ast_event_get_ie_raw(event, ie_val->ie_type), ie_val->raw_datalen)) ? 1 : 0;
00544 }
00545
00546 case AST_EVENT_IE_PLTYPE_EXISTS:
00547 {
00548 return ast_event_get_ie_raw(event, ie_val->ie_type) ? 1 : 0;
00549 }
00550
00551 case AST_EVENT_IE_PLTYPE_UNKNOWN:
00552 return 0;
00553 }
00554
00555 return 0;
00556 }
00557
00558 static int dump_cache_cb(void *obj, void *arg, int flags)
00559 {
00560 const struct ast_event_ref *event_ref = obj;
00561 const struct ast_event *event = event_ref->event;
00562 const struct ast_event_sub *event_sub = arg;
00563 struct ast_event_ie_val *ie_val = NULL;
00564
00565 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
00566 if (!match_ie_val(event, ie_val, NULL)) {
00567 break;
00568 }
00569 }
00570
00571 if (!ie_val) {
00572
00573 event_sub->cb(event, event_sub->userdata);
00574 }
00575
00576 return 0;
00577 }
00578
00579
00580 void ast_event_dump_cache(const struct ast_event_sub *event_sub)
00581 {
00582 ao2_callback(ast_event_cache[event_sub->type].container, OBJ_NODATA,
00583 dump_cache_cb, (void *) event_sub);
00584 }
00585
00586 static struct ast_event *gen_sub_event(struct ast_event_sub *sub)
00587 {
00588 struct ast_event_ie_val *ie_val;
00589 struct ast_event *event;
00590
00591 event = ast_event_new(AST_EVENT_SUB,
00592 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
00593 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00594 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
00595 AST_EVENT_IE_END);
00596
00597 if (!event)
00598 return NULL;
00599
00600 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
00601 switch (ie_val->ie_pltype) {
00602 case AST_EVENT_IE_PLTYPE_UNKNOWN:
00603 break;
00604 case AST_EVENT_IE_PLTYPE_EXISTS:
00605 ast_event_append_ie_uint(&event, AST_EVENT_IE_EXISTS, ie_val->ie_type);
00606 break;
00607 case AST_EVENT_IE_PLTYPE_UINT:
00608 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
00609 break;
00610 case AST_EVENT_IE_PLTYPE_BITFLAGS:
00611 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
00612 break;
00613 case AST_EVENT_IE_PLTYPE_STR:
00614 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
00615 break;
00616 case AST_EVENT_IE_PLTYPE_RAW:
00617 ast_event_append_ie_raw(&event, ie_val->ie_type, ie_val->payload.raw, ie_val->raw_datalen);
00618 break;
00619 }
00620 if (!event)
00621 break;
00622 }
00623
00624 return event;
00625 }
00626
00627
00628 void ast_event_report_subs(const struct ast_event_sub *event_sub)
00629 {
00630 struct ast_event *event;
00631 struct ast_event_sub *sub;
00632 enum ast_event_type event_type = -1;
00633 struct ast_event_ie_val *ie_val;
00634
00635 if (event_sub->type != AST_EVENT_SUB)
00636 return;
00637
00638 AST_LIST_TRAVERSE(&event_sub->ie_vals, ie_val, entry) {
00639 if (ie_val->ie_type == AST_EVENT_IE_EVENTTYPE) {
00640 event_type = ie_val->payload.uint;
00641 break;
00642 }
00643 }
00644
00645 if (event_type == -1)
00646 return;
00647
00648 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_type]);
00649 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_type], sub, entry) {
00650 if (event_sub == sub) {
00651 continue;
00652 }
00653
00654 event = gen_sub_event(sub);
00655
00656 if (!event) {
00657 continue;
00658 }
00659
00660 event_sub->cb(event, event_sub->userdata);
00661
00662 ast_event_destroy(event);
00663 }
00664 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_type]);
00665 }
00666
00667 struct ast_event_sub *ast_event_subscribe_new(enum ast_event_type type,
00668 ast_event_cb_t cb, void *userdata)
00669 {
00670 struct ast_event_sub *sub;
00671
00672 if (type < 0 || type >= AST_EVENT_TOTAL) {
00673 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
00674 return NULL;
00675 }
00676
00677 if (!(sub = ast_calloc(1, sizeof(*sub)))) {
00678 return NULL;
00679 }
00680
00681 sub->type = type;
00682 sub->cb = cb;
00683 sub->userdata = userdata;
00684 sub->uniqueid = ast_atomic_fetchadd_int((int *) &sub_uniqueid, 1);
00685
00686 return sub;
00687 }
00688
00689 int ast_event_sub_append_ie_uint(struct ast_event_sub *sub,
00690 enum ast_event_ie_type ie_type, uint32_t unsigned_int)
00691 {
00692 struct ast_event_ie_val *ie_val;
00693
00694 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00695 return -1;
00696 }
00697
00698 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00699 return -1;
00700 }
00701
00702 ie_val->ie_type = ie_type;
00703 ie_val->payload.uint = unsigned_int;
00704 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_UINT;
00705
00706 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00707
00708 return 0;
00709 }
00710
00711 int ast_event_sub_append_ie_bitflags(struct ast_event_sub *sub,
00712 enum ast_event_ie_type ie_type, uint32_t flags)
00713 {
00714 struct ast_event_ie_val *ie_val;
00715
00716 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00717 return -1;
00718 }
00719
00720 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00721 return -1;
00722 }
00723
00724 ie_val->ie_type = ie_type;
00725 ie_val->payload.uint = flags;
00726 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_BITFLAGS;
00727
00728 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00729
00730 return 0;
00731 }
00732
00733 int ast_event_sub_append_ie_exists(struct ast_event_sub *sub,
00734 enum ast_event_ie_type ie_type)
00735 {
00736 struct ast_event_ie_val *ie_val;
00737
00738 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00739 return -1;
00740 }
00741
00742 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00743 return -1;
00744 }
00745
00746 ie_val->ie_type = ie_type;
00747 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_EXISTS;
00748
00749 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00750
00751 return 0;
00752 }
00753
00754 int ast_event_sub_append_ie_str(struct ast_event_sub *sub,
00755 enum ast_event_ie_type ie_type, const char *str)
00756 {
00757 struct ast_event_ie_val *ie_val;
00758
00759 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00760 return -1;
00761 }
00762
00763 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00764 return -1;
00765 }
00766
00767 ie_val->ie_type = ie_type;
00768 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_STR;
00769
00770 if (!(ie_val->payload.str = ast_strdup(str))) {
00771 ast_free(ie_val);
00772 return -1;
00773 }
00774
00775 ie_val->payload.hash = ast_str_hash(str);
00776
00777 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00778
00779 return 0;
00780 }
00781
00782 int ast_event_sub_append_ie_raw(struct ast_event_sub *sub,
00783 enum ast_event_ie_type ie_type, void *data, size_t raw_datalen)
00784 {
00785 struct ast_event_ie_val *ie_val;
00786
00787 if (ie_type <= 0 || ie_type >= AST_EVENT_IE_TOTAL) {
00788 return -1;
00789 }
00790
00791 if (!(ie_val = ast_calloc(1, sizeof(*ie_val)))) {
00792 return -1;
00793 }
00794
00795 ie_val->ie_type = ie_type;
00796 ie_val->ie_pltype = AST_EVENT_IE_PLTYPE_RAW;
00797 ie_val->raw_datalen = raw_datalen;
00798
00799 if (!(ie_val->payload.raw = ast_malloc(raw_datalen))) {
00800 ast_free(ie_val);
00801 return -1;
00802 }
00803
00804 memcpy(ie_val->payload.raw, data, raw_datalen);
00805
00806 AST_LIST_INSERT_TAIL(&sub->ie_vals, ie_val, entry);
00807
00808 return 0;
00809 }
00810
00811 int ast_event_sub_activate(struct ast_event_sub *sub)
00812 {
00813 if (ast_event_check_subscriber(AST_EVENT_SUB,
00814 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00815 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
00816 struct ast_event *event;
00817
00818 event = gen_sub_event(sub);
00819
00820 if (event) {
00821 ast_event_queue(event);
00822 }
00823 }
00824
00825 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
00826 AST_RWDLLIST_INSERT_TAIL(&ast_event_subs[sub->type], sub, entry);
00827 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
00828
00829 return 0;
00830 }
00831
00832 struct ast_event_sub *ast_event_subscribe(enum ast_event_type type, ast_event_cb_t cb,
00833 char *description, void *userdata, ...)
00834 {
00835 va_list ap;
00836 enum ast_event_ie_type ie_type;
00837 struct ast_event_sub *sub;
00838
00839 if (!(sub = ast_event_subscribe_new(type, cb, userdata))) {
00840 return NULL;
00841 }
00842
00843 ast_copy_string(sub->description, description, sizeof(sub->description));
00844
00845 va_start(ap, userdata);
00846 for (ie_type = va_arg(ap, enum ast_event_ie_type);
00847 ie_type != AST_EVENT_IE_END;
00848 ie_type = va_arg(ap, enum ast_event_ie_type))
00849 {
00850 enum ast_event_ie_pltype ie_pltype;
00851
00852 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
00853
00854 switch (ie_pltype) {
00855 case AST_EVENT_IE_PLTYPE_UNKNOWN:
00856 break;
00857 case AST_EVENT_IE_PLTYPE_UINT:
00858 {
00859 uint32_t unsigned_int = va_arg(ap, uint32_t);
00860 ast_event_sub_append_ie_uint(sub, ie_type, unsigned_int);
00861 break;
00862 }
00863 case AST_EVENT_IE_PLTYPE_BITFLAGS:
00864 {
00865 uint32_t unsigned_int = va_arg(ap, uint32_t);
00866 ast_event_sub_append_ie_bitflags(sub, ie_type, unsigned_int);
00867 break;
00868 }
00869 case AST_EVENT_IE_PLTYPE_STR:
00870 {
00871 const char *str = va_arg(ap, const char *);
00872 ast_event_sub_append_ie_str(sub, ie_type, str);
00873 break;
00874 }
00875 case AST_EVENT_IE_PLTYPE_RAW:
00876 {
00877 void *data = va_arg(ap, void *);
00878 size_t data_len = va_arg(ap, size_t);
00879 ast_event_sub_append_ie_raw(sub, ie_type, data, data_len);
00880 break;
00881 }
00882 case AST_EVENT_IE_PLTYPE_EXISTS:
00883 ast_event_sub_append_ie_exists(sub, ie_type);
00884 break;
00885 }
00886 }
00887 va_end(ap);
00888
00889 ast_event_sub_activate(sub);
00890
00891 return sub;
00892 }
00893
00894 void ast_event_sub_destroy(struct ast_event_sub *sub)
00895 {
00896 struct ast_event_ie_val *ie_val;
00897
00898 while ((ie_val = AST_LIST_REMOVE_HEAD(&sub->ie_vals, entry))) {
00899 ast_event_ie_val_destroy(ie_val);
00900 }
00901
00902 ast_free(sub);
00903 }
00904
00905 const char *ast_event_subscriber_get_description(struct ast_event_sub *sub)
00906 {
00907 return sub ? sub->description : NULL;
00908 }
00909
00910 struct ast_event_sub *ast_event_unsubscribe(struct ast_event_sub *sub)
00911 {
00912 struct ast_event *event;
00913
00914 AST_RWDLLIST_WRLOCK(&ast_event_subs[sub->type]);
00915 AST_DLLIST_REMOVE(&ast_event_subs[sub->type], sub, entry);
00916 AST_RWDLLIST_UNLOCK(&ast_event_subs[sub->type]);
00917
00918 if (ast_event_check_subscriber(AST_EVENT_UNSUB,
00919 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00920 AST_EVENT_IE_END) != AST_EVENT_SUB_NONE) {
00921
00922 event = ast_event_new(AST_EVENT_UNSUB,
00923 AST_EVENT_IE_UNIQUEID, AST_EVENT_IE_PLTYPE_UINT, sub->uniqueid,
00924 AST_EVENT_IE_EVENTTYPE, AST_EVENT_IE_PLTYPE_UINT, sub->type,
00925 AST_EVENT_IE_DESCRIPTION, AST_EVENT_IE_PLTYPE_STR, sub->description,
00926 AST_EVENT_IE_END);
00927
00928 if (event) {
00929 ast_event_queue(event);
00930 }
00931 }
00932
00933 ast_event_sub_destroy(sub);
00934
00935 return NULL;
00936 }
00937
00938 int ast_event_iterator_init(struct ast_event_iterator *iterator, const struct ast_event *event)
00939 {
00940 int res = 0;
00941
00942 iterator->event_len = ast_event_get_size(event);
00943 iterator->event = event;
00944 if (iterator->event_len >= sizeof(*event) + sizeof(struct ast_event_ie)) {
00945 iterator->ie = (struct ast_event_ie *) ( ((char *) event) + sizeof(*event) );
00946 } else {
00947 iterator->ie = NULL;
00948 res = -1;
00949 }
00950
00951 return res;
00952 }
00953
00954 int ast_event_iterator_next(struct ast_event_iterator *iterator)
00955 {
00956 iterator->ie = (struct ast_event_ie *) ( ((char *) iterator->ie) + sizeof(*iterator->ie) + ntohs(iterator->ie->ie_payload_len));
00957 return ((iterator->event_len <= (((char *) iterator->ie) - ((char *) iterator->event))) ? -1 : 0);
00958 }
00959
00960 enum ast_event_ie_type ast_event_iterator_get_ie_type(struct ast_event_iterator *iterator)
00961 {
00962 return ntohs(iterator->ie->ie_type);
00963 }
00964
00965 uint32_t ast_event_iterator_get_ie_uint(struct ast_event_iterator *iterator)
00966 {
00967 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
00968 }
00969
00970 uint32_t ast_event_iterator_get_ie_bitflags(struct ast_event_iterator *iterator)
00971 {
00972 return ntohl(get_unaligned_uint32(iterator->ie->ie_payload));
00973 }
00974
00975 const char *ast_event_iterator_get_ie_str(struct ast_event_iterator *iterator)
00976 {
00977 const struct ast_event_ie_str_payload *str_payload;
00978
00979 str_payload = (struct ast_event_ie_str_payload *) iterator->ie->ie_payload;
00980
00981 return str_payload ? str_payload->str : NULL;
00982 }
00983
00984 void *ast_event_iterator_get_ie_raw(struct ast_event_iterator *iterator)
00985 {
00986 return iterator->ie->ie_payload;
00987 }
00988
00989 enum ast_event_type ast_event_get_type(const struct ast_event *event)
00990 {
00991 return ntohs(event->type);
00992 }
00993
00994 uint32_t ast_event_get_ie_uint(const struct ast_event *event, enum ast_event_ie_type ie_type)
00995 {
00996 const uint32_t *ie_val;
00997
00998 ie_val = ast_event_get_ie_raw(event, ie_type);
00999
01000 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
01001 }
01002
01003 uint32_t ast_event_get_ie_bitflags(const struct ast_event *event, enum ast_event_ie_type ie_type)
01004 {
01005 const uint32_t *ie_val;
01006
01007 ie_val = ast_event_get_ie_raw(event, ie_type);
01008
01009 return ie_val ? ntohl(get_unaligned_uint32(ie_val)) : 0;
01010 }
01011
01012 uint32_t ast_event_get_ie_str_hash(const struct ast_event *event, enum ast_event_ie_type ie_type)
01013 {
01014 const struct ast_event_ie_str_payload *str_payload;
01015
01016 str_payload = ast_event_get_ie_raw(event, ie_type);
01017
01018 return str_payload ? str_payload->hash : 0;
01019 }
01020
01021 const char *ast_event_get_ie_str(const struct ast_event *event, enum ast_event_ie_type ie_type)
01022 {
01023 const struct ast_event_ie_str_payload *str_payload;
01024
01025 str_payload = ast_event_get_ie_raw(event, ie_type);
01026
01027 return str_payload ? str_payload->str : NULL;
01028 }
01029
01030 const void *ast_event_get_ie_raw(const struct ast_event *event, enum ast_event_ie_type ie_type)
01031 {
01032 struct ast_event_iterator iterator;
01033 int res;
01034
01035 for (res = ast_event_iterator_init(&iterator, event); !res; res = ast_event_iterator_next(&iterator)) {
01036 if (ast_event_iterator_get_ie_type(&iterator) == ie_type) {
01037 return ast_event_iterator_get_ie_raw(&iterator);
01038 }
01039 }
01040
01041 return NULL;
01042 }
01043
01044 int ast_event_append_ie_str(struct ast_event **event, enum ast_event_ie_type ie_type,
01045 const char *str)
01046 {
01047 struct ast_event_ie_str_payload *str_payload;
01048 size_t payload_len;
01049
01050 payload_len = sizeof(*str_payload) + strlen(str);
01051 str_payload = alloca(payload_len);
01052
01053 strcpy(str_payload->str, str);
01054 str_payload->hash = ast_str_hash(str);
01055
01056 return ast_event_append_ie_raw(event, ie_type, str_payload, payload_len);
01057 }
01058
01059 int ast_event_append_ie_uint(struct ast_event **event, enum ast_event_ie_type ie_type,
01060 uint32_t data)
01061 {
01062 data = htonl(data);
01063 return ast_event_append_ie_raw(event, ie_type, &data, sizeof(data));
01064 }
01065
01066 int ast_event_append_ie_bitflags(struct ast_event **event, enum ast_event_ie_type ie_type,
01067 uint32_t flags)
01068 {
01069 flags = htonl(flags);
01070 return ast_event_append_ie_raw(event, ie_type, &flags, sizeof(flags));
01071 }
01072
01073 int ast_event_append_ie_raw(struct ast_event **event, enum ast_event_ie_type ie_type,
01074 const void *data, size_t data_len)
01075 {
01076 struct ast_event_ie *ie;
01077 unsigned int extra_len;
01078 uint16_t event_len;
01079
01080 event_len = ntohs((*event)->event_len);
01081 extra_len = sizeof(*ie) + data_len;
01082
01083 if (!(*event = ast_realloc(*event, event_len + extra_len))) {
01084 return -1;
01085 }
01086
01087 ie = (struct ast_event_ie *) ( ((char *) *event) + event_len );
01088 ie->ie_type = htons(ie_type);
01089 ie->ie_payload_len = htons(data_len);
01090 memcpy(ie->ie_payload, data, data_len);
01091
01092 (*event)->event_len = htons(event_len + extra_len);
01093
01094 return 0;
01095 }
01096
01097 struct ast_event *ast_event_new(enum ast_event_type type, ...)
01098 {
01099 va_list ap;
01100 struct ast_event *event;
01101 enum ast_event_ie_type ie_type;
01102 struct ast_event_ie_val *ie_val;
01103 int has_ie = 0;
01104 AST_LIST_HEAD_NOLOCK_STATIC(ie_vals, ast_event_ie_val);
01105
01106
01107 if (type >= AST_EVENT_TOTAL) {
01108 ast_log(LOG_WARNING, "Someone tried to create an event of invalid "
01109 "type '%d'!\n", type);
01110 return NULL;
01111 }
01112
01113 va_start(ap, type);
01114 for (ie_type = va_arg(ap, enum ast_event_ie_type);
01115 ie_type != AST_EVENT_IE_END;
01116 ie_type = va_arg(ap, enum ast_event_ie_type))
01117 {
01118 struct ast_event_ie_val *ie_value = alloca(sizeof(*ie_value));
01119 int insert = 1;
01120 memset(ie_value, 0, sizeof(*ie_value));
01121 ie_value->ie_type = ie_type;
01122 ie_value->ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
01123 switch (ie_value->ie_pltype) {
01124 case AST_EVENT_IE_PLTYPE_UINT:
01125 ie_value->payload.uint = va_arg(ap, uint32_t);
01126 break;
01127 case AST_EVENT_IE_PLTYPE_BITFLAGS:
01128 ie_value->payload.uint = va_arg(ap, uint32_t);
01129 break;
01130 case AST_EVENT_IE_PLTYPE_STR:
01131 ie_value->payload.str = va_arg(ap, const char *);
01132 break;
01133 case AST_EVENT_IE_PLTYPE_RAW:
01134 {
01135 void *data = va_arg(ap, void *);
01136 size_t datalen = va_arg(ap, size_t);
01137 ie_value->payload.raw = alloca(datalen);
01138 memcpy(ie_value->payload.raw, data, datalen);
01139 ie_value->raw_datalen = datalen;
01140 break;
01141 }
01142 case AST_EVENT_IE_PLTYPE_UNKNOWN:
01143 insert = 0;
01144 break;
01145 case AST_EVENT_IE_PLTYPE_EXISTS:
01146 break;
01147 }
01148
01149 if (insert) {
01150 AST_LIST_INSERT_TAIL(&ie_vals, ie_value, entry);
01151 has_ie = 1;
01152 }
01153 }
01154 va_end(ap);
01155
01156 if (!(event = ast_calloc(1, sizeof(*event)))) {
01157 return NULL;
01158 }
01159
01160 event->type = htons(type);
01161 event->event_len = htons(sizeof(*event));
01162
01163 AST_LIST_TRAVERSE(&ie_vals, ie_val, entry) {
01164 switch (ie_val->ie_pltype) {
01165 case AST_EVENT_IE_PLTYPE_STR:
01166 ast_event_append_ie_str(&event, ie_val->ie_type, ie_val->payload.str);
01167 break;
01168 case AST_EVENT_IE_PLTYPE_UINT:
01169 ast_event_append_ie_uint(&event, ie_val->ie_type, ie_val->payload.uint);
01170 break;
01171 case AST_EVENT_IE_PLTYPE_BITFLAGS:
01172 ast_event_append_ie_bitflags(&event, ie_val->ie_type, ie_val->payload.uint);
01173 break;
01174 case AST_EVENT_IE_PLTYPE_RAW:
01175 ast_event_append_ie_raw(&event, ie_val->ie_type,
01176 ie_val->payload.raw, ie_val->raw_datalen);
01177 break;
01178 case AST_EVENT_IE_PLTYPE_EXISTS:
01179 ast_log(LOG_WARNING, "PLTYPE_EXISTS unsupported in event_new\n");
01180 break;
01181 case AST_EVENT_IE_PLTYPE_UNKNOWN:
01182 ast_log(LOG_WARNING, "PLTYPE_UNKNOWN passed as an IE type "
01183 "for a new event\n");
01184 break;
01185 }
01186
01187 if (!event) {
01188 break;
01189 }
01190 }
01191
01192 if (has_ie && !ast_event_get_ie_raw(event, AST_EVENT_IE_EID)) {
01193
01194
01195 ast_event_append_eid(&event);
01196 }
01197
01198 return event;
01199 }
01200
01201 int ast_event_append_eid(struct ast_event **event)
01202 {
01203 return ast_event_append_ie_raw(event, AST_EVENT_IE_EID,
01204 &ast_eid_default, sizeof(ast_eid_default));
01205 }
01206
01207 void ast_event_destroy(struct ast_event *event)
01208 {
01209 ast_free(event);
01210 }
01211
01212 static void ast_event_ref_destroy(void *obj)
01213 {
01214 struct ast_event_ref *event_ref = obj;
01215
01216 ast_event_destroy(event_ref->event);
01217 }
01218
01219 static struct ast_event *ast_event_dup(const struct ast_event *event)
01220 {
01221 struct ast_event *dup_event;
01222 uint16_t event_len;
01223
01224 event_len = ast_event_get_size(event);
01225
01226 if (!(dup_event = ast_calloc(1, event_len))) {
01227 return NULL;
01228 }
01229
01230 memcpy(dup_event, event, event_len);
01231
01232 return dup_event;
01233 }
01234
01235 struct ast_event *ast_event_get_cached(enum ast_event_type type, ...)
01236 {
01237 va_list ap;
01238 enum ast_event_ie_type ie_type;
01239 struct ast_event *dup_event = NULL;
01240 struct ast_event_ref *cached_event_ref;
01241 struct ast_event *cache_arg_event;
01242 struct ast_event_ref tmp_event_ref = {
01243 .event = NULL,
01244 };
01245 struct ao2_container *container = NULL;
01246
01247 if (type >= AST_EVENT_TOTAL) {
01248 ast_log(LOG_ERROR, "%u is an invalid type!\n", type);
01249 return NULL;
01250 }
01251
01252 if (!(container = ast_event_cache[type].container)) {
01253 ast_log(LOG_ERROR, "%u is not a cached event type\n", type);
01254 return NULL;
01255 }
01256
01257 if (!(cache_arg_event = ast_event_new(type, AST_EVENT_IE_END))) {
01258 return NULL;
01259 }
01260
01261 va_start(ap, type);
01262 for (ie_type = va_arg(ap, enum ast_event_ie_type);
01263 ie_type != AST_EVENT_IE_END;
01264 ie_type = va_arg(ap, enum ast_event_ie_type))
01265 {
01266 enum ast_event_ie_pltype ie_pltype;
01267
01268 ie_pltype = va_arg(ap, enum ast_event_ie_pltype);
01269
01270 switch (ie_pltype) {
01271 case AST_EVENT_IE_PLTYPE_UINT:
01272 ast_event_append_ie_uint(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
01273 break;
01274 case AST_EVENT_IE_PLTYPE_BITFLAGS:
01275 ast_event_append_ie_bitflags(&cache_arg_event, ie_type, va_arg(ap, uint32_t));
01276 break;
01277 case AST_EVENT_IE_PLTYPE_STR:
01278 ast_event_append_ie_str(&cache_arg_event, ie_type, va_arg(ap, const char *));
01279 break;
01280 case AST_EVENT_IE_PLTYPE_RAW:
01281 {
01282 void *data = va_arg(ap, void *);
01283 size_t datalen = va_arg(ap, size_t);
01284 ast_event_append_ie_raw(&cache_arg_event, ie_type, data, datalen);
01285 }
01286 case AST_EVENT_IE_PLTYPE_EXISTS:
01287 ast_log(LOG_WARNING, "PLTYPE_EXISTS not supported by this function\n");
01288 break;
01289 case AST_EVENT_IE_PLTYPE_UNKNOWN:
01290 break;
01291 }
01292 }
01293 va_end(ap);
01294
01295 tmp_event_ref.event = cache_arg_event;
01296
01297 cached_event_ref = ao2_find(container, &tmp_event_ref, OBJ_POINTER);
01298
01299 ast_event_destroy(cache_arg_event);
01300 cache_arg_event = NULL;
01301
01302 if (cached_event_ref) {
01303 dup_event = ast_event_dup(cached_event_ref->event);
01304 ao2_ref(cached_event_ref, -1);
01305 cached_event_ref = NULL;
01306 }
01307
01308 return dup_event;
01309 }
01310
01311 static struct ast_event_ref *alloc_event_ref(void)
01312 {
01313 return ao2_alloc(sizeof(struct ast_event_ref), ast_event_ref_destroy);
01314 }
01315
01316
01317
01318 static int ast_event_dup_and_cache(const struct ast_event *event)
01319 {
01320 struct ast_event *dup_event;
01321 struct ast_event_ref *event_ref;
01322
01323 if (!(dup_event = ast_event_dup(event))) {
01324 return -1;
01325 }
01326
01327 if (!(event_ref = alloc_event_ref())) {
01328 ast_event_destroy(dup_event);
01329 return -1;
01330 }
01331
01332 event_ref->event = dup_event;
01333
01334 ao2_link(ast_event_cache[ast_event_get_type(event)].container, event_ref);
01335
01336 ao2_ref(event_ref, -1);
01337
01338 return 0;
01339 }
01340
01341 int ast_event_queue_and_cache(struct ast_event *event)
01342 {
01343 struct ao2_container *container;
01344 struct ast_event_ref tmp_event_ref = {
01345 .event = event,
01346 };
01347 int res = -1;
01348
01349 if (!(container = ast_event_cache[ast_event_get_type(event)].container)) {
01350 ast_log(LOG_WARNING, "cache requested for non-cached event type\n");
01351 goto queue_event;
01352 }
01353
01354
01355 ao2_callback(container, OBJ_POINTER | OBJ_UNLINK | OBJ_MULTIPLE | OBJ_NODATA,
01356 ast_event_cmp, &tmp_event_ref);
01357
01358 res = ast_event_dup_and_cache(event);
01359
01360 queue_event:
01361 return ast_event_queue(event) ? -1 : res;
01362 }
01363
01364 static int handle_event(void *data)
01365 {
01366 struct ast_event_ref *event_ref = data;
01367 struct ast_event_sub *sub;
01368 const enum ast_event_type event_types[] = {
01369 ntohs(event_ref->event->type),
01370 AST_EVENT_ALL
01371 };
01372 int i;
01373
01374 for (i = 0; i < ARRAY_LEN(event_types); i++) {
01375 AST_RWDLLIST_RDLOCK(&ast_event_subs[event_types[i]]);
01376 AST_RWDLLIST_TRAVERSE(&ast_event_subs[event_types[i]], sub, entry) {
01377 struct ast_event_ie_val *ie_val;
01378 AST_LIST_TRAVERSE(&sub->ie_vals, ie_val, entry) {
01379 if (!match_ie_val(event_ref->event, ie_val, NULL)) {
01380 break;
01381 }
01382 }
01383 if (ie_val) {
01384 continue;
01385 }
01386 sub->cb(event_ref->event, sub->userdata);
01387 }
01388 AST_RWDLLIST_UNLOCK(&ast_event_subs[event_types[i]]);
01389 }
01390
01391 ao2_ref(event_ref, -1);
01392
01393 return 0;
01394 }
01395
01396 int ast_event_queue(struct ast_event *event)
01397 {
01398 struct ast_event_ref *event_ref;
01399 uint16_t host_event_type;
01400
01401 host_event_type = ntohs(event->type);
01402
01403
01404 if (host_event_type >= AST_EVENT_TOTAL) {
01405 ast_log(LOG_WARNING, "Someone tried to queue an event of invalid "
01406 "type '%d'!\n", host_event_type);
01407 return -1;
01408 }
01409
01410
01411 if (ast_event_check_subscriber(host_event_type, AST_EVENT_IE_END)
01412 == AST_EVENT_SUB_NONE) {
01413 ast_event_destroy(event);
01414 return 0;
01415 }
01416
01417 if (!(event_ref = alloc_event_ref())) {
01418 return -1;
01419 }
01420
01421 event_ref->event = event;
01422
01423 return ast_taskprocessor_push(event_dispatcher, handle_event, event_ref);
01424 }
01425
01426 static int ast_event_hash_mwi(const void *obj, const int flags)
01427 {
01428 const struct ast_event *event = obj;
01429 const char *mailbox = ast_event_get_ie_str(event, AST_EVENT_IE_MAILBOX);
01430 const char *context = ast_event_get_ie_str(event, AST_EVENT_IE_CONTEXT);
01431
01432 return ast_str_hash_add(context, ast_str_hash(mailbox));
01433 }
01434
01435
01436
01437
01438
01439
01440
01441
01442
01443
01444 static int ast_event_hash_devstate(const void *obj, const int flags)
01445 {
01446 const struct ast_event *event = obj;
01447
01448 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
01449 }
01450
01451
01452
01453
01454
01455
01456
01457
01458
01459
01460 static int ast_event_hash_devstate_change(const void *obj, const int flags)
01461 {
01462 const struct ast_event *event = obj;
01463
01464 return ast_str_hash(ast_event_get_ie_str(event, AST_EVENT_IE_DEVICE));
01465 }
01466
01467 static int ast_event_hash(const void *obj, const int flags)
01468 {
01469 const struct ast_event_ref *event_ref;
01470 const struct ast_event *event;
01471 ao2_hash_fn *hash_fn;
01472
01473 event_ref = obj;
01474 event = event_ref->event;
01475
01476 if (!(hash_fn = ast_event_cache[ast_event_get_type(event)].hash_fn)) {
01477 return 0;
01478 }
01479
01480 return hash_fn(event, flags);
01481 }
01482
01483
01484
01485
01486
01487
01488
01489
01490
01491
01492
01493
01494
01495
01496
01497
01498
01499
01500
01501 static int ast_event_cmp(void *obj, void *arg, int flags)
01502 {
01503 struct ast_event_ref *event_ref, *event_ref2;
01504 struct ast_event *event, *event2;
01505 int res = CMP_MATCH;
01506 int i;
01507 enum ast_event_ie_type *cache_args;
01508
01509 event_ref = obj;
01510 event = event_ref->event;
01511
01512 event_ref2 = arg;
01513 event2 = event_ref2->event;
01514
01515 cache_args = ast_event_cache[ast_event_get_type(event)].cache_args;
01516
01517 for (i = 0; i < ARRAY_LEN(ast_event_cache[0].cache_args) && cache_args[i]; i++) {
01518 struct ast_event_ie_val ie_val = {
01519 .ie_pltype = ast_event_get_ie_pltype(cache_args[i]),
01520 .ie_type = cache_args[i],
01521 };
01522
01523 if (!match_ie_val(event, &ie_val, event2)) {
01524 res = 0;
01525 break;
01526 }
01527 }
01528
01529 return res;
01530 }
01531
01532 int ast_event_init(void)
01533 {
01534 int i;
01535
01536 for (i = 0; i < AST_EVENT_TOTAL; i++) {
01537 AST_RWDLLIST_HEAD_INIT(&ast_event_subs[i]);
01538 }
01539
01540 for (i = 0; i < AST_EVENT_TOTAL; i++) {
01541 if (!ast_event_cache[i].hash_fn) {
01542
01543 continue;
01544 }
01545
01546 if (!(ast_event_cache[i].container = ao2_container_alloc(NUM_CACHE_BUCKETS,
01547 ast_event_hash, ast_event_cmp))) {
01548 return -1;
01549 }
01550 }
01551
01552 if (!(event_dispatcher = ast_taskprocessor_get("core_event_dispatcher", 0))) {
01553 return -1;
01554 }
01555
01556 return 0;
01557 }