]> git.rkrishnan.org Git - pihpsdr.git/commitdiff
Made STEMlab discovery more robust, and eliminated a bunch of duplicate code.
authorc vw <dl1ycf@darc.de>
Fri, 15 Jan 2021 15:14:55 +0000 (16:14 +0100)
committerc vw <dl1ycf@darc.de>
Fri, 15 Jan 2021 15:14:55 +0000 (16:14 +0100)
discovery.c
old_discovery.c

index cdd71f3cd72b775f712efc392ed7cd5917ecfc49..78c0b2f5324a35d25ceebc9073e31ff83da29acf 100644 (file)
@@ -77,6 +77,12 @@ GtkWidget *host_port_spinner;
 gint host_port=45000;
 #endif
 
+//
+// This is a variable for the second phase of STEMlab discovery.
+// Here we know we have a stemlab and will re-discover only P1
+//
+static int discover_only_p1 = 0;
+
 static gboolean delete_event_cb(GtkWidget *widget, GdkEvent *event, gpointer data) {
   _exit(0);
 }
@@ -97,21 +103,16 @@ static gboolean start_cb (GtkWidget *widget, GdkEventButton *event, gpointer dat
        ret=stemlab_start_app(gtk_combo_box_get_active_id(GTK_COMBO_BOX(apps_combobox[device_id])));
     }
     //
-    // We have started the SDR app on the RedPitaya, but may need to fill
-    // in information necessary for starting the radio, including the
-    // MAC address and the interface listening to. Even when using AVAHI,
-    // we miss some information.
-    // To get all required info, we do a "fake" discovery on the RedPitaya IP address.
-    // Here we also try TCP if UDP does not work, such that we can work with STEMlabs
-    // in remote subnets.
+    // To make this bullet-proof, we do another "discover" now
+    // and proceeding this way is the only way to choose between UDP and TCP connection
+    // Since we have just started the app, we temporarily deactivate STEMlab detection
     //
-    if (ret == 0) {
-      ret=stemlab_get_info(device_id);
-    }
-    // At this point, if stemlab_start_app failed, we cannot recover
-    if (ret != 0) exit(-1);
+    stemlab_cleanup();
+    discover_only_p1=1;
+    gtk_widget_destroy(discovery_dialog);
+    g_idle_add(ext_discovery,NULL);
+    return TRUE;
   }
-  stemlab_cleanup();
 #endif
   gtk_widget_destroy(discovery_dialog);
   start_radio();
@@ -335,7 +336,7 @@ void discovery() {
 #endif
 
 #ifdef STEMLAB_DISCOVERY
-  if(enable_stemlab) {
+  if(enable_stemlab && ! discover_only_p1) {
 #ifdef NO_AVAHI
     status_text("Looking for STEMlab WEB apps");
 #else
@@ -350,18 +351,21 @@ void discovery() {
     old_discovery();
   }
 
-  if(enable_protocol_2) {
+  if(enable_protocol_2 && ! discover_only_p1) {
     status_text("Protocol 2 ... Discovering Devices");
     new_discovery();
   }
 
 #ifdef SOAPYSDR
-  if(enable_soapy_protocol) {
+  if(enable_soapy_protocol && ! discover_only_p1) {
     status_text("SoapySDR ... Discovering Devices");
     soapy_discovery();
   }
 #endif
 
+  // subsequent discoveries check all protocols enabled.
+  discover_only_p1=0;
+
   status_text("Discovery");
   
     fprintf(stderr,"discovery: found %d devices\n", devices);
@@ -437,7 +441,7 @@ fprintf(stderr,"%p Protocol=%d name=%s\n",d,d->protocol,d->name);
 #ifdef STEMLAB_DISCOVERY
           case STEMLAB_PROTOCOL:
 #ifdef NO_AVAHI
-            sprintf(text,"Choose RedPitaya App from %s and start radio: ",inet_ntoa(d->info.network.address.sin_addr));
+            sprintf(text,"Choose RedPitaya App from %s and re-discover: ",inet_ntoa(d->info.network.address.sin_addr));
 #else
             sprintf(text, "STEMlab (%02X:%02X:%02X:%02X:%02X:%02X) on %s",
                            d->info.network.mac_address[0],
index 6d0a47568506c66a606a26fa82a5b9b53b6a7c9a..9f1e39ce937aef511a23507c59571ac806bf08e0 100644 (file)
@@ -238,144 +238,6 @@ static void discover(struct ifaddrs* iface) {
 
 }
 
-#ifdef STEMLAB_DISCOVERY
-//
-// We have just started the SDR app on the RedPitaya without using AVAHI.
-// Therefore we must send a discovery packet and analyze its response
-// to have all information to start the radio.
-// Since this essentially what happens in the original discovery process,
-// we put this function HERE and not into stemlab_discovery.c
-//
-int stemlab_get_info(int id) {
-  int ret;
-  unsigned char buffer[1032];
-  int optval,i;
-
-  // Allow RP app to come up
-  sleep(2);
-
-  devices=id;
-
-  discovery_socket = socket(AF_INET, SOCK_DGRAM, 0);
-  if (discovery_socket < 0) {
-    perror("stemlab_get_info socket():");
-    return(1);
-  }
-
-  optval = 1;
-  setsockopt(discovery_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
-  setsockopt(discovery_socket, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
-
-  memset(&discovery_addr, 0, sizeof(discovery_addr));
-  discovery_addr.sin_family=AF_INET;
-  discovery_addr.sin_addr.s_addr=htonl(INADDR_ANY);
-  // use system-assigned port in case 1024 is in use
-  // e.g. by an HPSDR simulator
-  discovery_addr.sin_port=htons(0);
-  ret=bind(discovery_socket, (struct sockaddr *)&discovery_addr, sizeof(discovery_addr));
-  if (ret < 0) {
-    perror("stemlab_get_info bind():");
-    return 1;
-  }
-  setsockopt(discovery_socket, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(optval));
-
-  // start a receive thread to collect discovery response packets
-  discover_thread_id = g_thread_new( "old discover receive", discover_receive_thread, NULL);
-  if( ! discover_thread_id )
-  {
-      fprintf(stderr,"g_thread_new failed on discover_receive_thread\n");
-      return 1;
-  }
-
-  // send discovery packet
-  buffer[0]=0xEF;
-  buffer[1]=0xFE;
-  buffer[2]=0x02;
-  for(i=3;i<63;i++) {
-      buffer[i]=0x00;
-  }
-
-  ret=sendto(discovery_socket,buffer,63,0,
-             (struct sockaddr*)&(discovered[id].info.network.address),
-             sizeof(discovered[id].info.network.address));
-  if (ret < 63) {
-    perror("stemlab_get_info UDP sendto():");
-    return -1;
-  }
-
-  // wait for receive thread to complete
-  g_thread_join(discover_thread_id);
-
-  close(discovery_socket);
-
-  // if all went well, we have now filled in data for exactly
-  // one device.
-  if (devices == id+1) return 0;
-
-  if (devices > id+1) return 1;
-
-  // If we have not received an answer, then possibly the STEMlab is located
-  // on a different subnet and UDP packets are not correctly routed through.
-  // In this case, we try to open a connection using TCP.
-
-  discovery_socket = socket(AF_INET, SOCK_STREAM, 0);
-  if (discovery_socket < 0) {
-    perror("stemlab_get_info socket():");
-    return 1;
-  }
-
-  optval = 1;
-  setsockopt(discovery_socket, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval));
-  setsockopt(discovery_socket, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval));
-  ret=connect(discovery_socket, (struct sockaddr*)&(discovered[id].info.network.address),
-                                  sizeof(discovered[id].info.network.address));
-  if (ret < 0) {
-    perror("stemlab_get_info connect():");
-    return 1;
-  }
-
-  // send discovery packet
-  buffer[0]=0xEF;
-  buffer[1]=0xFE;
-  buffer[2]=0x02;
-  for(i=3;i<1032;i++) {
-      buffer[i]=0x00;
-  }
-
-  // start a receive thread to collect discovery response packets
-  discover_thread_id = g_thread_new( "old discover receive", discover_receive_thread, NULL);
-  if( ! discover_thread_id )
-  {
-      fprintf(stderr,"g_thread_new failed on discover_receive_thread\n");
-      return 1;
-  }
-
-  ret=sendto(discovery_socket,buffer,1032,0,
-             (struct sockaddr*)&(discovered[id].info.network.address),
-             sizeof(discovered[id].info.network.address));
-  if (ret < 1032) {
-    perror("stemlab_get_info TCP  sendto():");
-    return -1;
-  }
-
-  // wait for receive thread to complete
-  g_thread_join(discover_thread_id);
-
-  close(discovery_socket);
-
-  // if all went well, we have now filled in data for exactly
-  // one device. We must set a flag that this one can ONLY do
-  // TCP
-  if (devices == id+1) {
-     fprintf(stderr,"UDP did not work, but TCP does!\n");
-     discovered[id].use_tcp=1;
-     return 0;
-  }
-  return 1;
-}
-#endif
-
-//static void *discover_receive_thread(void* arg) {
 static gpointer discover_receive_thread(gpointer data) {
     struct sockaddr_in addr;
     socklen_t len;