summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlfred E. Heggestad <alfred.heggestad@gmail.com>2017-12-16 17:14:12 +0100
committerAlfred E. Heggestad <alfred.heggestad@gmail.com>2017-12-16 17:14:12 +0100
commitc2b8b2e38e2073ebb6fbb177db713c03c158413b (patch)
tree2323deac6e9667460c7d17f46e5fcb06e3c9edf4
parentc36b42303285fb629145f92a7668a97397e69af0 (diff)
event: move encoding of event from mqtt to event.c (ref #331)
-rw-r--r--include/baresip.h8
-rw-r--r--modules/mqtt/publish.c80
-rw-r--r--src/event.c129
3 files changed, 138 insertions, 79 deletions
diff --git a/include/baresip.h b/include/baresip.h
index a04d88f..f2aacad 100644
--- a/include/baresip.h
+++ b/include/baresip.h
@@ -1172,6 +1172,14 @@ double mos_calculate(double *r_factor, double rtt,
/*
+ * Generic event
+ */
+
+int event_encode_dict(struct odict *od, struct ua *ua, enum ua_event ev,
+ struct call *call, const char *prm);
+
+
+/*
* Baresip instance
*/
diff --git a/modules/mqtt/publish.c b/modules/mqtt/publish.c
index dd250c2..524b0a2 100644
--- a/modules/mqtt/publish.c
+++ b/modules/mqtt/publish.c
@@ -16,50 +16,6 @@
*/
-static int add_rtcp_stats(struct odict *od_parent, const struct rtcp_stats *rs)
-{
- struct odict *od = NULL, *tx = NULL, *rx = NULL;
- int err = 0;
-
- if (!od_parent || !rs)
- return EINVAL;
-
- err = odict_alloc(&od, 8);
- err |= odict_alloc(&tx, 8);
- err |= odict_alloc(&rx, 8);
- if (err)
- goto out;
-
- err = odict_entry_add(tx, "sent", ODICT_INT, (int64_t)rs->tx.sent);
- err |= odict_entry_add(tx, "lost", ODICT_INT, (int64_t)rs->tx.lost);
- err |= odict_entry_add(tx, "jit", ODICT_INT, (int64_t)rs->tx.jit);
- if (err)
- goto out;
-
- err = odict_entry_add(rx, "sent", ODICT_INT, (int64_t)rs->rx.sent);
- err |= odict_entry_add(rx, "lost", ODICT_INT, (int64_t)rs->rx.lost);
- err |= odict_entry_add(rx, "jit", ODICT_INT, (int64_t)rs->rx.jit);
- if (err)
- goto out;
-
- err = odict_entry_add(od, "tx", ODICT_OBJECT, tx);
- err |= odict_entry_add(od, "rx", ODICT_OBJECT, rx);
- err |= odict_entry_add(od, "rtt", ODICT_INT, (int64_t)rs->rtt);
- if (err)
- goto out;
-
- /* add object to the parent */
- err = odict_entry_add(od_parent, "rtcp_stats", ODICT_OBJECT, od);
- if (err)
- goto out;
-
- out:
- mem_deref(od);
-
- return err;
-}
-
-
/*
* Relay UA events as publish messages to the Broker
*
@@ -69,7 +25,6 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
struct call *call, const char *prm, void *arg)
{
struct mqtt *mqtt = arg;
- const char *event_str = uag_event_str(ev);
struct odict *od = NULL;
int err;
@@ -77,43 +32,10 @@ static void ua_event_handler(struct ua *ua, enum ua_event ev,
if (err)
return;
- err |= odict_entry_add(od, "type", ODICT_STRING, event_str);
- err |= odict_entry_add(od, "accountaor", ODICT_STRING, ua_aor(ua));
+ err = event_encode_dict(od, ua, ev, call, prm);
if (err)
goto out;
- if (call) {
-
- const char *dir;
-
- dir = call_is_outgoing(call) ? "outgoing" : "incoming";
-
- err |= odict_entry_add(od, "direction", ODICT_STRING, dir);
- err |= odict_entry_add(od, "peeruri",
- ODICT_STRING, call_peeruri(call));
- if (err)
- goto out;
- }
-
- if (str_isset(prm)) {
- err = odict_entry_add(od, "param", ODICT_STRING, prm);
- if (err)
- goto out;
- }
-
- if (ev == UA_EVENT_CALL_RTCP) {
- struct stream *strm = NULL;
-
- if (0 == str_casecmp(prm, "audio"))
- strm = audio_strm(call_audio(call));
- else if (0 == str_casecmp(prm, "video"))
- strm = video_strm(call_video(call));
-
- err = add_rtcp_stats(od, stream_rtcp_stats(strm));
- if (err)
- goto out;
- }
-
err = mqtt_publish_message(mqtt, "/baresip/event", "%H",
json_encode_odict, od);
if (err) {
diff --git a/src/event.c b/src/event.c
index 77b5676..6cf24cc 100644
--- a/src/event.c
+++ b/src/event.c
@@ -9,6 +9,135 @@
#include "core.h"
+static const char *event_class_name(enum ua_event ev)
+{
+ switch (ev) {
+
+ case UA_EVENT_REGISTERING:
+ case UA_EVENT_REGISTER_OK:
+ case UA_EVENT_REGISTER_FAIL:
+ case UA_EVENT_UNREGISTERING:
+ return "register";
+
+ case UA_EVENT_SHUTDOWN:
+ case UA_EVENT_EXIT:
+ return "application";
+
+ case UA_EVENT_CALL_INCOMING:
+ case UA_EVENT_CALL_RINGING:
+ case UA_EVENT_CALL_PROGRESS:
+ case UA_EVENT_CALL_ESTABLISHED:
+ case UA_EVENT_CALL_CLOSED:
+ case UA_EVENT_CALL_TRANSFER_FAILED:
+ case UA_EVENT_CALL_DTMF_START:
+ case UA_EVENT_CALL_DTMF_END:
+ case UA_EVENT_CALL_RTCP:
+ return "call";
+
+ default:
+ return "other";
+ }
+}
+
+
+static int add_rtcp_stats(struct odict *od_parent, const struct rtcp_stats *rs)
+{
+ struct odict *od = NULL, *tx = NULL, *rx = NULL;
+ int err = 0;
+
+ if (!od_parent || !rs)
+ return EINVAL;
+
+ err = odict_alloc(&od, 8);
+ err |= odict_alloc(&tx, 8);
+ err |= odict_alloc(&rx, 8);
+ if (err)
+ goto out;
+
+ err = odict_entry_add(tx, "sent", ODICT_INT, (int64_t)rs->tx.sent);
+ err |= odict_entry_add(tx, "lost", ODICT_INT, (int64_t)rs->tx.lost);
+ err |= odict_entry_add(tx, "jit", ODICT_INT, (int64_t)rs->tx.jit);
+ if (err)
+ goto out;
+
+ err = odict_entry_add(rx, "sent", ODICT_INT, (int64_t)rs->rx.sent);
+ err |= odict_entry_add(rx, "lost", ODICT_INT, (int64_t)rs->rx.lost);
+ err |= odict_entry_add(rx, "jit", ODICT_INT, (int64_t)rs->rx.jit);
+ if (err)
+ goto out;
+
+ err = odict_entry_add(od, "tx", ODICT_OBJECT, tx);
+ err |= odict_entry_add(od, "rx", ODICT_OBJECT, rx);
+ err |= odict_entry_add(od, "rtt", ODICT_INT, (int64_t)rs->rtt);
+ if (err)
+ goto out;
+
+ /* add object to the parent */
+ err = odict_entry_add(od_parent, "rtcp_stats", ODICT_OBJECT, od);
+ if (err)
+ goto out;
+
+ out:
+ mem_deref(od);
+
+ return err;
+}
+
+
+int event_encode_dict(struct odict *od, struct ua *ua, enum ua_event ev,
+ struct call *call, const char *prm)
+{
+ const char *event_str = uag_event_str(ev);
+ int err = 0;
+
+ if (!od)
+ return EINVAL;
+
+ err |= odict_entry_add(od, "type", ODICT_STRING, event_str);
+ err |= odict_entry_add(od, "class",
+ ODICT_STRING, event_class_name(ev));
+ err |= odict_entry_add(od, "accountaor", ODICT_STRING, ua_aor(ua));
+ if (err)
+ goto out;
+
+ if (call) {
+
+ const char *dir;
+
+ dir = call_is_outgoing(call) ? "outgoing" : "incoming";
+
+ err |= odict_entry_add(od, "direction", ODICT_STRING, dir);
+ err |= odict_entry_add(od, "peeruri",
+ ODICT_STRING, call_peeruri(call));
+ if (err)
+ goto out;
+ }
+
+ if (str_isset(prm)) {
+ err = odict_entry_add(od, "param", ODICT_STRING, prm);
+ if (err)
+ goto out;
+ }
+
+ if (ev == UA_EVENT_CALL_RTCP) {
+ struct stream *strm = NULL;
+
+ if (0 == str_casecmp(prm, "audio"))
+ strm = audio_strm(call_audio(call));
+ else if (0 == str_casecmp(prm, "video"))
+ strm = video_strm(call_video(call));
+
+ err = add_rtcp_stats(od, stream_rtcp_stats(strm));
+ if (err)
+ goto out;
+ }
+
+ out:
+
+ return err;
+}
+
+
/**
* Get the name of the User-Agent event
*