From 597ed5434f369ab614fa2ce543aab3f49101b5f1 Mon Sep 17 00:00:00 2001
From: Matt Porter <mporter@konsulko.com>
Date: Wed, 24 May 2017 11:47:22 -0400
Subject: Add additional error checking/reporting to the telephony binding

Adds additional error checking and reporting to the binding so
that errors resulting from lack of a telephony modem can be reported
at init. This fixes the issue where the phone app crashes and the
afb-daemon can only report a segfault on start.

AGL-Bug: SPEC-621
Change-Id: Ie3e12cb69a2a04cf7308e6bd244add581b8578db
Signed-off-by: Matt Porter <mporter@konsulko.com>
---
 telephony-binding/gdbus/ofono_manager.c | 39 ++++++++++++++++++++-------------
 telephony-binding/gdbus/ofono_manager.h |  2 +-
 telephony-binding/telephony-binding.c   | 30 ++++++++++++++++---------
 3 files changed, 45 insertions(+), 26 deletions(-)

(limited to 'telephony-binding')

diff --git a/telephony-binding/gdbus/ofono_manager.c b/telephony-binding/gdbus/ofono_manager.c
index 2f8c4df..bb99a43 100644
--- a/telephony-binding/gdbus/ofono_manager.c
+++ b/telephony-binding/gdbus/ofono_manager.c
@@ -36,18 +36,19 @@ static OrgOfonoManager *manager;
 static struct ofono_manager_modem default_modem;
 static const struct afb_binding_interface *interface;
 
-void ofono_manager_init(const struct afb_binding_interface *iface)
+int ofono_manager_init(const struct afb_binding_interface *iface)
 {
 	GVariant *out_arg = NULL, *next, *value;
 	GError *error = NULL;
 	gchar *path, *key;
-	GVariantIter *iter, *iter2;
+	GVariantIter *iter, *iter2 = NULL;
+	int ret = 0;
 
 	interface = iface;
 
 	if (manager) {
 		ERROR(interface, "Ofono Manager already initialized\n");
-		return;
+		return -1;
 	}
 
 	manager = org_ofono_manager_proxy_new_for_bus_sync(
@@ -56,26 +57,34 @@ void ofono_manager_init(const struct afb_binding_interface *iface)
 
 	if (!manager) {
 		ERROR(interface, "Ofono Manager not initialized\n");
-		return;
+		return -1;
 	}
 
 	org_ofono_manager_call_get_modems_sync(manager, &out_arg, NULL, &error);
 	if (error == NULL) {
 		g_variant_get(out_arg, "a(oa{sv})", &iter);
 		next = g_variant_iter_next_value(iter);
-		g_variant_get(next, "(oa{sv})", &path, &iter2);
-		default_modem.path = path;
-		while (g_variant_iter_loop(iter2, "{sv}", &key, &value)) {
-			if (!strcmp(key, "Name"))
-				default_modem.name = g_variant_get_string(value, NULL);
-			else if (!strcmp(key, "Type"))
-				default_modem.type = g_variant_get_string(value, NULL);
-			else if (!strcmp(key, "Powered"))
-				default_modem.powered = g_variant_get_boolean(value);
-			else if (!strcmp(key, "Online"))
-				default_modem.online = g_variant_get_boolean(value);
+		if (next) {
+			g_variant_get(next, "(oa{sv})", &path, &iter2);
+			default_modem.path = path;
+			while (g_variant_iter_loop(iter2, "{sv}", &key, &value)) {
+				if (!strcmp(key, "Name"))
+					default_modem.name = g_variant_get_string(value, NULL);
+				else if (!strcmp(key, "Type"))
+					default_modem.type = g_variant_get_string(value, NULL);
+				else if (!strcmp(key, "Powered"))
+					default_modem.powered = g_variant_get_boolean(value);
+				else if (!strcmp(key, "Online"))
+					default_modem.online = g_variant_get_boolean(value);
+			}
+		} else {
+			ret = -1;
 		}
+	} else {
+		ret = -1;
 	}
+
+	return ret;
 }
 
 
diff --git a/telephony-binding/gdbus/ofono_manager.h b/telephony-binding/gdbus/ofono_manager.h
index 3b55547..6454246 100644
--- a/telephony-binding/gdbus/ofono_manager.h
+++ b/telephony-binding/gdbus/ofono_manager.h
@@ -20,7 +20,7 @@
 #include <afb/afb-binding.h>
 #include <afb/afb-service-itf.h>
 
-void ofono_manager_init(const struct afb_binding_interface *iface);
+int ofono_manager_init(const struct afb_binding_interface *iface);
 const gchar *ofono_manager_get_default_modem_path(void);
 const gchar *ofono_manager_get_default_modem_name(void);
 const gchar *ofono_manager_get_default_modem_type(void);
diff --git a/telephony-binding/telephony-binding.c b/telephony-binding/telephony-binding.c
index e3c6d2f..d58aeab 100644
--- a/telephony-binding/telephony-binding.c
+++ b/telephony-binding/telephony-binding.c
@@ -141,16 +141,26 @@ static int ofono_init(void)
 	/* Start the main loop thread */
 	pthread_create(&tid, NULL, main_loop_thread, NULL);
 
-	ofono_manager_init(interface);
-	const gchar *modem_path = ofono_manager_get_default_modem_path();
-	DEBUG(interface, "modem_path: %s\n", modem_path);
-	vcm = ofono_voicecallmanager_init(interface, modem_path,
-					  incoming_call_cb,
-					  dialing_call_cb,
-					  terminated_call_cb);
-	if (!vcm) {
-		ERROR(interface, "Failed to initialize voice call manager\n");
-		ret = -1;
+	ret = ofono_manager_init(interface);
+	if (ret == 0) {
+		const gchar *modem_path = ofono_manager_get_default_modem_path();
+		if (modem_path) {
+			DEBUG(interface, "modem_path: %s\n", modem_path);
+			vcm = ofono_voicecallmanager_init(interface, modem_path,
+					incoming_call_cb,
+					dialing_call_cb,
+					terminated_call_cb);
+			if (!vcm) {
+				ERROR(interface, "[telephony] failed to initialize voice call manager\n");
+				ret = -1;
+			}
+		} else {
+			ERROR(interface, "[telephony] default modem not set\n");
+			ret = -1;
+		}
+	} else {
+		ERROR(interface, "[telephony] failed to initialize ofono manager: " \
+				 "HFP device not connected or Bluetooth disabled\n");
 	}
 
 	return ret;
-- 
cgit