Fix HDHR device discovery (part 2)

Change-Id: I623c4d2589931bdc85666dbeb9bcddb4fbec63ae
diff --git a/hdhomerun_dev.c b/hdhomerun_dev.c
index 79356c5..fa4366a 100644
--- a/hdhomerun_dev.c
+++ b/hdhomerun_dev.c
@@ -105,6 +105,8 @@
     }
 
     // process line
+    printf("hdhr:parse:[%s]\n", line);
+
     p = strstr(line, "Model:");
     if (p) {
       if (get_string_by_name(p, "Model", ht->model, sizeof(ht->model), NULL)) {
diff --git a/hdhomerun_http.c b/hdhomerun_http.c
index a22c57e..88ba972 100644
--- a/hdhomerun_http.c
+++ b/hdhomerun_http.c
@@ -65,16 +65,18 @@
   ret = inet_pton(AF_INET, ip, (void *)(&(remote->sin_addr.s_addr)));
   if (ret < 0) {
     perror("Can't set remote->sin_addr.s_addr");
-    return -1;
+    goto exit_free_rem;
   } else if (ret == 0) {
     fprintf(stderr, "%s is not a valid IP address\n", ip);
-    return -1;
+    ret = -1;
+    goto exit_free_rem;
   }
   remote->sin_port = htons(PORT);
 
   if (connect(sock, (struct sockaddr *)remote, sizeof(struct sockaddr)) < 0) {
     perror("Could not connect");
-    return -1;
+    ret = -1;
+    goto exit_free_rem;
   }
   get = build_get_query(host, page);
 
@@ -84,7 +86,7 @@
     ret = send(sock, get + sent, strlen(get) - sent, 0);
     if (ret == -1) {
       perror("Can't send query");
-      break;
+      goto exit_free_get;
     }
     sent += ret;
   }
@@ -103,10 +105,13 @@
     if (used_bytes < 0)
       break;
   }
+
+exit_free_get:
   free(get);
+exit_free_rem:
   free(remote);
   close(sock);
-  return 1;
+  return (ret < 0) ? -1 : 1;
 }
 
 #ifdef HTTP_GET_APP
diff --git a/hdhomerun_tuner.c b/hdhomerun_tuner.c
index e04052b..d9ad830 100644
--- a/hdhomerun_tuner.c
+++ b/hdhomerun_tuner.c
@@ -355,6 +355,7 @@
   struct hdhomerun_discover_device_t device_info[max_ht_num];
   struct hdhr_tuner_t hdhr;
 
+  printf("hdhr:discovering devices...\n");
   while (1) {
     count = hdhomerun_discover_find_devices_custom(0,
                 HDHOMERUN_DEVICE_TYPE_TUNER, HDHOMERUN_DEVICE_ID_WILDCARD,
@@ -366,9 +367,15 @@
   }
 
   for (i = 0; i < count; i++) {
-    memset(&hdhr, 0, sizeof(hdhr));
-    ip4_address(device_info[i].ip_addr, hdhr.ip, sizeof(hdhr.ip));
-    get_dev_model_and_fw(&hdhr);
+    printf("hdhr:get device %d model and fw\n", i);
+    while (1) {
+      memset(&hdhr, 0, sizeof(hdhr));
+      ip4_address(device_info[i].ip_addr, hdhr.ip, sizeof(hdhr.ip));
+      if (get_dev_model_and_fw(&hdhr) > 0) {
+        break;
+      }
+      sleep(3);
+    }
 
     for (k = 0; k < device_info[i].tuner_count && num < max_ht_num; k++) {
       ht[num].tuner_id = num;