From 05861567c962cea3216a2c46227366639fa70525 Mon Sep 17 00:00:00 2001 From: Matt Porter Date: Tue, 8 Aug 2017 09:17:37 -0400 Subject: add support for subscription-based events Convert telephony-binding and phone app from broadcast events to subscribe/unsubscribe events. Bug-AGL: SPEC-815 Change-Id: I6288db22332277e217b3f1a4e79a391cb63ebc16 Signed-off-by: Matt Porter --- app/api/Telephony.qml | 13 +++++-- telephony-binding/telephony-binding.c | 72 +++++++++++++++++++++++++++++++++-- 2 files changed, 77 insertions(+), 8 deletions(-) diff --git a/app/api/Telephony.qml b/app/api/Telephony.qml index c7d9218..2db2d0b 100644 --- a/app/api/Telephony.qml +++ b/app/api/Telephony.qml @@ -58,6 +58,7 @@ WebSocket { var payload = JSON.parse(JSON.stringify(json[2])) var event = payload.event var data = payload.data + console.debug("event: " + event) if (event == "telephony/incomingCall") { callClipColp = data.clip callStatus = "incoming" @@ -76,6 +77,10 @@ WebSocket { onStatusChanged: { switch (status) { case WebSocket.Open: + sendSocketMessage("subscribe", { value: "callStateChanged" }) + sendSocketMessage("subscribe", { value: "dialingCall" }) + sendSocketMessage("subscribe", { value: "incomingCall" }) + sendSocketMessage("subscribe", { value: "terminatedCall" }) break case WebSocket.Error: root.statusString = "WebSocket error: " + root.errorString @@ -83,7 +88,7 @@ WebSocket { } } - function sendSocketMesage(verb, parameter) { + function sendSocketMessage(verb, parameter) { var requestJson = [ msgid.call, payloadLength, apiString + '/' + verb, parameter ] verbs.push(verb) sendTextMessage(JSON.stringify(requestJson)) @@ -91,16 +96,16 @@ WebSocket { function dial(number) { var parameterJson = { value: number } - sendSocketMesage("dial", parameterJson) + sendSocketMessage("dial", parameterJson) } function answer() { var parameterJson = 'None' - sendSocketMesage("answer", parameterJson) + sendSocketMessage("answer", parameterJson) } function hangup() { var parameterJson = 'None' - sendSocketMesage("hangup", parameterJson) + sendSocketMessage("hangup", parameterJson) } } diff --git a/telephony-binding/telephony-binding.c b/telephony-binding/telephony-binding.c index b1c39f1..f0865ed 100644 --- a/telephony-binding/telephony-binding.c +++ b/telephony-binding/telephony-binding.c @@ -18,6 +18,7 @@ #include #include +#include #define AFB_BINDING_VERSION 2 #include @@ -29,6 +30,10 @@ static OrgOfonoVoiceCallManager *vcm; static OrgOfonoVoiceCall *incoming_call, *voice_call; +static struct afb_event call_state_changed_event; +static struct afb_event dialing_call_event; +static struct afb_event incoming_call_event; +static struct afb_event terminated_call_event; static void dial(struct afb_req request) { @@ -84,12 +89,54 @@ static void answer(struct afb_req request) } } +static void subscribe(struct afb_req request) +{ + const char *value = afb_req_value(request, "value"); + if(value) { + if (!strcasecmp(value, "callStateChanged")) { + afb_req_subscribe(request, call_state_changed_event); + } else if (!strcasecmp(value, "dialingCall")) { + afb_req_subscribe(request, dialing_call_event); + } else if (!strcasecmp(value, "incomingCall")) { + afb_req_subscribe(request, incoming_call_event); + } else if (!strcasecmp(value, "terminatedCall")) { + afb_req_subscribe(request, terminated_call_event); + } else { + afb_req_fail(request, "failed", "Invalid event"); + return; + } + } + + afb_req_success(request, NULL, NULL); +} + +static void unsubscribe(struct afb_req request) +{ + const char *value = afb_req_value(request, "value"); + if(value) { + if (!strcasecmp(value, "callStateChanged")) { + afb_req_unsubscribe(request, call_state_changed_event); + } else if (!strcasecmp(value, "dialingCall")) { + afb_req_unsubscribe(request, dialing_call_event); + } else if (!strcasecmp(value, "incomingCall")) { + afb_req_unsubscribe(request, incoming_call_event); + } else if (!strcasecmp(value, "terminatedCall")) { + afb_req_unsubscribe(request, terminated_call_event); + } else { + afb_req_fail(request, "failed", "Invalid event"); + return; + } + } + + afb_req_success(request, NULL, NULL); +} + static void call_state_changed_cb(OrgOfonoVoiceCall *vc, gchar *state) { struct json_object *call_state; call_state = json_object_new_object(); json_object_object_add(call_state, "state", json_object_new_string(state)); - afb_daemon_broadcast_event("callStateChanged", call_state); + afb_event_push(call_state_changed_event, call_state); } static void incoming_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op, gchar *clip) @@ -98,7 +145,7 @@ static void incoming_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op, gchar call_info = json_object_new_object(); json_object_object_add(call_info, "clip", json_object_new_string(clip)); - afb_daemon_broadcast_event("incomingCall", call_info); + afb_event_push(incoming_call_event, call_info); incoming_call = ofono_voicecall_new(op, call_state_changed_cb); } @@ -108,7 +155,7 @@ static void dialing_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op, gchar call_info = json_object_new_object(); json_object_object_add(call_info, "colp", json_object_new_string(colp)); - afb_daemon_broadcast_event("dialingCall", call_info); + afb_event_push(dialing_call_event, call_info); voice_call = ofono_voicecall_new(op, call_state_changed_cb); } @@ -121,7 +168,7 @@ static void terminated_call_cb(OrgOfonoVoiceCallManager *manager, gchar *op) ofono_voicecall_free(voice_call); } voice_call = NULL; - afb_daemon_broadcast_event("terminatedCall", NULL); + afb_event_push(terminated_call_event, NULL); } static void *main_loop_thread(void *unused) @@ -136,6 +183,11 @@ static int ofono_init(void) pthread_t tid; int ret = 0; + call_state_changed_event = afb_daemon_make_event("callStateChanged"); + dialing_call_event = afb_daemon_make_event("dialingCall"); + incoming_call_event = afb_daemon_make_event("incomingCall"); + terminated_call_event = afb_daemon_make_event("terminatedCall"); + /* Start the main loop thread */ pthread_create(&tid, NULL, main_loop_thread, NULL); @@ -183,6 +235,18 @@ static const struct afb_verb_v2 verbs[]= { .auth = NULL, .session = AFB_SESSION_NONE, }, + { + .verb = "subscribe", + .callback = subscribe, + .auth = NULL, + .session = AFB_SESSION_NONE, + }, + { + .verb = "unsubscribe", + .callback = unsubscribe, + .auth = NULL, + .session = AFB_SESSION_NONE, + }, {NULL} }; -- cgit 1.2.3-korg