From a5fcd857efef76961bb39909ffd907f4d79875a4 Mon Sep 17 00:00:00 2001 From: Christian Spielberger Date: Tue, 6 Aug 2024 11:36:39 +0200 Subject: [PATCH] bevent: add backwards wrapper --- src/bevent.c | 64 +++++++++++++++++++++++++++++++++++++++++---------- src/call.c | 2 +- test/bevent.c | 48 +++++++++++++++++++++++++++++++++++--- 3 files changed, 98 insertions(+), 16 deletions(-) diff --git a/src/bevent.c b/src/bevent.c index 22c45e1b3..46515b276 100644 --- a/src/bevent.c +++ b/src/bevent.c @@ -404,6 +404,8 @@ static int add_rtcp_stats(struct odict *od_parent, const struct rtcp_stats *rs) * @param call Call object (optional) * @param prm Event parameters * + * @deprecated Use odict_encode_event() instead + * * @return 0 if success, otherwise errorcode */ int event_encode_dict(struct odict *od, struct ua *ua, enum ua_event ev, @@ -570,15 +572,21 @@ int event_add_au_jb_stat(struct odict *od_parent, const struct call *call) * @param h Event handler * @param arg Handler argument * + * @deprecated Use bevent_register() + * * @return 0 if success, otherwise errorcode */ int uag_event_register(ua_event_h *h, void *arg) { struct ua_eh *eh; + static int w = 3; if (!h) return EINVAL; + if (w && w--) + warning("Used deprecated uag_event_register(). " + "Use bevent_register() instead!\n"); uag_event_unregister(h); eh = mem_zalloc(sizeof(*eh), eh_destructor); @@ -598,6 +606,8 @@ int uag_event_register(ua_event_h *h, void *arg) * Unregister a User-Agent event handler * * @param h Event handler + * + * @deprecated Use bevent_unregister() */ void uag_event_unregister(ua_event_h *h) { @@ -666,6 +676,25 @@ void bevent_unregister(bevent_h *eh) } +static void ua_event_private(struct ua *ua, enum ua_event ev, + struct call *call, const char *txt) +{ + /* send event to all clients */ + struct le *le = ehl.head; + while (le) { + struct ua_eh *eh = le->data; + le = le->next; + + if (call_is_evstop(call)) { + call_set_evstop(call, false); + break; + } + + eh->h(ua, ev, call, txt, eh->arg); + } +} + + /** * Send a User-Agent event to all UA event handlers * @@ -674,31 +703,34 @@ void bevent_unregister(bevent_h *eh) * @param call Call object (optional) * @param fmt Formatted arguments * @param ... Variable arguments + * + * @deprecated Use one of the event_xxx_emit() functions */ void ua_event(struct ua *ua, enum ua_event ev, struct call *call, const char *fmt, ...) { - struct le *le; char buf[256]; va_list ap; + static int w = 3; va_start(ap, fmt); (void)re_vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); - /* send event to all clients */ - le = ehl.head; - while (le) { - struct ua_eh *eh = le->data; - le = le->next; + ua_event_private(ua, ev, call, buf); - if (call_is_evstop(call)) { - call_set_evstop(call, false); - break; - } + if (w && w--) + warning("Used deprecated ua_event() for %s. " + "Use one of event_xxx_emit() instead!\n", + uag_event_str(ev)); + struct bevent event = {ev, buf, 0, false, { 0 } }; - eh->h(ua, ev, call, buf, eh->arg); - } + if (bevent_class(ev) == BEVENT_CLASS_CALL) + event.u.call = call; + else if (bevent_class(ev) == BEVENT_CLASS_UA) + event.u.ua = ua; + + (void)bevent_emit_base(&event); } @@ -747,6 +779,9 @@ void module_event(const char *module, const char *event, struct ua *ua, eh->h(ua, UA_EVENT_MODULE, call, buf, eh->arg); } + struct bevent bevent = {UA_EVENT_MODULE, buf, 0, false, { 0 } }; + bevent_emit_base(&bevent); + out: mem_deref(buf); } @@ -771,6 +806,8 @@ static void bevent_emit_base(struct bevent *event) static int bevent_emit(struct bevent *event, const char *fmt, va_list ap) { char *buf; + struct call *call = bevent_get_call(event); + struct ua *ua = bevent_get_ua(event); int err; if (!fmt) @@ -788,9 +825,12 @@ static int bevent_emit(struct bevent *event, const char *fmt, va_list ap) goto out; } + /* backwards compatibility */ if (event->stop) goto out; + ua_event_private(ua, event->ev, call, event->txt); + out: mem_deref(buf); return err; diff --git a/src/call.c b/src/call.c index ccf455973..e042b223a 100644 --- a/src/call.c +++ b/src/call.c @@ -82,7 +82,7 @@ struct call { bool use_video; bool use_rtp; char *user_data; /**< User data related to the call */ - bool evstop; /**< UA events stopped flag */ + bool evstop; /**< UA events stopped flag, @deprecated */ }; diff --git a/test/bevent.c b/test/bevent.c index b35d64863..89f6f453b 100644 --- a/test/bevent.c +++ b/test/bevent.c @@ -11,6 +11,7 @@ struct fixture { int cnt; + int cntold; enum ua_event expected_event; }; @@ -69,7 +70,6 @@ static void event_handler(enum ua_event ev, struct bevent *event, void *arg) struct ua *ua = bevent_get_ua(event); struct call *call = bevent_get_call(event); const struct sip_msg *msg = bevent_get_msg(event); - (void)ev; if (apparg && apparg != (void *) 0xdeadbeef) { bevent_set_error(event, EINVAL); @@ -77,9 +77,12 @@ static void event_handler(enum ua_event ev, struct bevent *event, void *arg) else if (ua && ua != (void *) 0xdeadbeef) { bevent_set_error(event, EINVAL); } - else if (call && call != (void *) 0xdeadbeef) { + else if (call) { bevent_set_error(event, EINVAL); } + else if (ev == CALL_EVENT_INCOMING) { + ++f->cnt; + } else if (msg && msg != (void *) 0xdeadbeef) { bevent_set_error(event, EINVAL); } @@ -91,6 +94,24 @@ static void event_handler(enum ua_event ev, struct bevent *event, void *arg) } +static void ua_event_handler(struct ua *ua, enum ua_event ev, + struct call *call, const char *prm, void *arg) +{ + struct fixture *f = arg; + + if (ua == (void *) 0xdeadbeef && !call) + ++f->cntold; + else if (ev == UA_EVENT_CALL_INCOMING) + ++f->cntold; + else if (ev == UA_EVENT_SIPSESS_CONN) + ++f->cntold; + else if (ev == UA_EVENT_EXIT && !str_cmp(prm, "details")) + ++f->cntold; + else if (ev == UA_EVENT_SHUTDOWN && !str_cmp(prm, "details")) + ++f->cntold; +} + + int test_event_register(void) { int err = 0; @@ -101,6 +122,9 @@ int test_event_register(void) err = bevent_register(event_handler, &f); TEST_ERR(err); + err = uag_event_register(ua_event_handler, &f); + TEST_ERR(err); + f.expected_event = UA_EVENT_EXIT; err = bevent_app_emit(UA_EVENT_EXIT, (void *) 0xdeadbeef, "%s", "details"); @@ -124,9 +148,27 @@ int test_event_register(void) (struct sip_msg *) 0xdeadbeef, NULL); TEST_ERR(err); - ASSERT_EQ(4, f.cnt); + ASSERT_EQ(4, f.cntold); + + /* test deprecated ua_event() with old and new handlers */ + f.expected_event = UA_EVENT_EXIT; + ua_event(NULL, UA_EVENT_EXIT, NULL, "%s", "details"); + ua_event(NULL, UA_EVENT_SHUTDOWN, NULL, "%s", "details"); + f.expected_event = UA_EVENT_REGISTER_OK; + ua_event((struct ua *) 0xdeadbeef, UA_EVENT_REGISTER_OK, NULL, "%s", + "details"); + f.expected_event = UA_EVENT_CALL_INCOMING; + ua_event(NULL, UA_EVENT_CALL_INCOMING, NULL, NULL); + f.expected_event = UA_EVENT_SIPSESS_CONN; + ua_event(NULL, UA_EVENT_SIPSESS_CONN, NULL, NULL); + + ASSERT_EQ(8, f.cnt); + + /* event error not supported with deprecated ua_event() */ + ASSERT_EQ(9, f.cntold); out: bevent_unregister(event_handler); + uag_event_unregister(ua_event_handler); return err; }