Fix the crash issue in the a2dp select role function 13/164413/1
authorDoHyun Pyun <dh79.pyun@samsung.com>
Tue, 19 Dec 2017 00:38:37 +0000 (09:38 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Tue, 19 Dec 2017 05:00:02 +0000 (14:00 +0900)
Change-Id: I328a9e72358672ec1ade61e19ebd459568132785
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
profiles/audio/media.c

index c714798..fdd5dcc 100755 (executable)
@@ -1081,17 +1081,51 @@ static DBusMessage *a2dp_select_role(DBusConnection *conn, DBusMessage *msg,
                return btd_error_invalid_args(msg);
 
        if (!g_strcmp0(a2dp_role, A2DP_SINK_ROLE)) {
-               btd_adapter_set_a2dp_role(adapter->btd_adapter, BLUETOOTH_A2DP_SINK_ROLE);
-               a2dp_remove_sep(source_endpoint->sep);
+               if (btd_adapter_get_a2dp_role(adapter->btd_adapter) == BLUETOOTH_A2DP_SINK_ROLE) {
+                       DBG("Already selected A2DP sink role");
+                       return btd_error_already_exists(msg);
+               }
+
+               if (sink_endpoint == NULL) {
+                       DBG("sink_endpoint is NULL, so can't init the sink_endpoint");
+                       return btd_error_not_available(msg);
+               }
+
                ret = endpoint_init_a2dp_sink(sink_endpoint, current_delay_reporting, NULL);
-               if (!ret)
+               if (!ret) {
                        DBG("could not init a2dp sink");
+                       return btd_error_failed(msg, "Can't init a2dp sink");
+               }
+
+               btd_adapter_set_a2dp_role(adapter->btd_adapter, BLUETOOTH_A2DP_SINK_ROLE);
+
+               if (source_endpoint && source_endpoint->sep)
+                       a2dp_remove_sep(source_endpoint->sep);
+
+               source_endpoint->sep = NULL;
        } else if (!g_strcmp0(a2dp_role, A2DP_SOURCE_ROLE)) {
-               btd_adapter_set_a2dp_role(adapter->btd_adapter, BLUETOOTH_A2DP_SOURCE_ROLE);
-               a2dp_remove_sep(sink_endpoint->sep);
+               if (btd_adapter_get_a2dp_role(adapter->btd_adapter) == BLUETOOTH_A2DP_SOURCE_ROLE) {
+                       DBG("Already selected A2DP sink role");
+                       return btd_error_already_exists(msg);
+               }
+
+               if (source_endpoint == NULL) {
+                       DBG("source_endpoint is NULL, so can't init the source_endpoint");
+                       return btd_error_not_available(msg);
+               }
+
                ret = endpoint_init_a2dp_source(source_endpoint, current_delay_reporting, NULL);
-               if (!ret)
+               if (!ret) {
                        DBG("could not init a2dp source");
+                       return btd_error_failed(msg, "Can't init a2dp source");
+               }
+
+               btd_adapter_set_a2dp_role(adapter->btd_adapter, BLUETOOTH_A2DP_SOURCE_ROLE);
+
+               if (sink_endpoint && sink_endpoint->sep)
+                       a2dp_remove_sep(sink_endpoint->sep);
+
+               sink_endpoint->sep = NULL;
        } else {
                DBG("invalid a2dp role");
                return btd_error_invalid_args(msg);