From a4df93a83cae154611377beb5254015dc95640b9 Mon Sep 17 00:00:00 2001
From: Nikias Bassen
Date: Sun, 26 Jun 2016 18:30:40 +0200
Subject: fdr: Add fdr_disconnect() function to stop the FDR thread

---
 src/fdr.c | 21 ++++++++++++++++-----
 src/fdr.h |  1 +
 2 files changed, 17 insertions(+), 5 deletions(-)

(limited to 'src')

diff --git a/src/fdr.c b/src/fdr.c
index 1468dc0..d7dc8d1 100644
--- a/src/fdr.c
+++ b/src/fdr.c
@@ -108,15 +108,25 @@ int fdr_connect(idevice_t device, fdr_type_t type, fdr_client_t* fdr)
 	return 0;
 }
 
-void fdr_free(fdr_client_t fdr)
+void fdr_disconnect(fdr_client_t fdr)
 {
 	if (!fdr)
 		return;
 
 	if (fdr->connection) {
-		idevice_disconnect(fdr->connection);
+		idevice_connection_t conn = fdr->connection;
 		fdr->connection = NULL;
+		idevice_disconnect(conn);
 	}
+}
+
+void fdr_free(fdr_client_t fdr)
+{
+	if (!fdr)
+		return;
+
+	fdr_disconnect(fdr);
+
 	free(fdr);
 	fdr = NULL;
 }
@@ -134,8 +144,9 @@ int fdr_poll_and_handle_message(fdr_client_t fdr)
 
 	device_error = idevice_connection_receive_timeout(fdr->connection, (char *)&cmd, sizeof(cmd), &bytes, 20000);
 	if (device_error != IDEVICE_E_SUCCESS) {
-		error("ERROR: Unable to receive message from FDR %p (%d). %u/%d bytes\n",
-		      fdr, device_error, bytes, sizeof(cmd));
+		if (fdr->connection) {
+			error("ERROR: Unable to receive message from FDR %p (%d). %u/%d bytes\n", fdr, device_error, bytes, sizeof(cmd));
+		}
 		return -1;
 	} else if (bytes != sizeof(cmd)) {
 		debug("FDR %p timeout waiting for command\n", fdr);
@@ -166,7 +177,7 @@ void *fdr_listener_thread(void *cdata)
 	fdr_client_t fdr = cdata;
 	int res;
 
-	while (1) {
+	while (fdr && fdr->connection) {
 		debug("FDR %p waiting for message...\n", fdr);
 		res = fdr_poll_and_handle_message(fdr);
 		if (fdr->type == FDR_CTRL && res >= 0)
diff --git a/src/fdr.h b/src/fdr.h
index 6139240..9822efc 100644
--- a/src/fdr.h
+++ b/src/fdr.h
@@ -42,6 +42,7 @@ struct fdr_client {
 typedef struct fdr_client *fdr_client_t;
 
 int fdr_connect(idevice_t device, fdr_type_t type, fdr_client_t *fdr);
+void fdr_disconnect(fdr_client_t fdr);
 void fdr_free(fdr_client_t fdr);
 int fdr_poll_and_handle_message(fdr_client_t fdr);
 void *fdr_listener_thread(void *cdata);
-- 
cgit v1.1-32-gdbae