]> git.rkrishnan.org Git - pihpsdr.git/commitdiff
call rust ffi function for discovery from C
authorRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Sat, 7 Dec 2024 16:58:09 +0000 (22:28 +0530)
committerRamakrishnan Muthukrishnan <ram@rkrishnan.org>
Sat, 7 Dec 2024 16:58:09 +0000 (22:28 +0530)
Fixes a bunch of types. Also, we don't use the generated header
anymore. Instead use the one used by the rest of the
code (DISCOVERED) which matches the type we defined earlier. Still
helpful to generate the header and verify that it is indeed the one we
are expecting.

discovered.h
discovery.c
rust/discovery/src/discovery.rs
rust/discovery/src/ffi.rs
rust/discovery/src/types.rs

index fe2308e383ee9e6a89088987317b2840d7aec857..a57ccf7f8a45845db9fe38d09e494ea82956261a 100644 (file)
 struct _DISCOVERED {
     int protocol;
     int device;
-    int use_tcp;    // use TCP rather than UDP to connect to radio
     char name[64];
     int software_version;
     int status;
     int supported_receivers;
-    int supported_transmitters;
-    int adcs;
-    int dacs;
     double frequency_min;
     double frequency_max;
     struct network {
@@ -76,6 +72,7 @@ struct _DISCOVERED {
         struct sockaddr_in interface_netmask;
         char interface_name[64];
     } network;
+    int use_tcp;
 };
 
 typedef struct _DISCOVERED DISCOVERED;
index c3875deb9807cebb9c375792ac1916c7724e8704..e327fba22b78b045eb23186948431c36d2f6e121 100644 (file)
@@ -37,7 +37,6 @@
 #endif
 
 #include "discovered.h"
-#include "p1_discovery.h"
 #include "main.h"
 #include "radio.h"
 #ifdef USBOZY
@@ -58,6 +57,7 @@
 static GtkWidget *discovery_dialog;
 static DISCOVERED *d;
 
+extern int discover_devices_ffi(DISCOVERED *devices, int);
 
 GtkWidget *tcpaddr;
 #define IPADDR_LEN 20
@@ -301,8 +301,9 @@ void discovery(void) {
 #endif
 
     if(enable_protocol_1) {
-       status_text("Protocol 1 ... Discovering Devices");
-       p1_discovery();
+       log_info("Protocol 1 ... Discovering Devices");
+       // p1_discovery();
+       devices = discover_devices_ffi(discovered, MAX_DEVICES);
     }
 
     /* if(enable_protocol_2) { */
@@ -310,7 +311,7 @@ void discovery(void) {
     /*   new_discovery(); */
     /* } */
 
-    status_text("Discovery");
+    log_info("Discovery");
   
     log_info("discovery: found %d devices", devices);
     gdk_window_set_cursor(gtk_widget_get_window(top_window),gdk_cursor_new(GDK_ARROW));
index dd319ca3a9cda14a71d14b106c42058bcad2a50b..afc3b45c26c694646549e46faccf11492de1cf8a 100644 (file)
@@ -54,7 +54,7 @@ fn parse_discovery_response(data: &[u8], addr: SocketAddr) -> Option<DiscoveredD
         return None;
     }
 
-    let device_type = data[10];
+    let device_type: i32 = data[10] as i32;
     let (name, freq_min, freq_max) = match device_type {
         0x00 => ("Metis", 0.0, 61440000.0),
         0x01 => ("Hermes", 0.0, 61440000.0),
@@ -67,11 +67,11 @@ fn parse_discovery_response(data: &[u8], addr: SocketAddr) -> Option<DiscoveredD
     mac_addr.copy_from_slice(&data[3..9]);
 
     Some(DiscoveredDevice {
-        protocol: 1,
+        protocol: 0,
         device: device_type,
         name: name.to_string(),
-        software_version: data[9],
-        status: data[2] as u32,
+        software_version: data[9] as i32,
+        status: data[2] as i32,
         supported_receivers: 2,
         frequency_min: freq_min,
         frequency_max: freq_max,
index 41ba7356d4c6ecb53ac741a47ccf9542adf4af79..c93d16b20d74d7a70d7acfd40912e580457b530b 100644 (file)
@@ -1,5 +1,5 @@
-use std::ffi::CString;
-use libc::{c_char, c_int, sockaddr_in};
+use std::ffi::{c_char, c_int, c_double, CString};
+use libc::{sockaddr_in};
 use crate::{discover_devices, discovery::list_interfaces, DiscoveredDevice, NetworkInfo};
 use std::net::SocketAddr;
 
@@ -14,14 +14,14 @@ pub struct CFfiNetworkInfo {
 
 #[repr(C)]
 pub struct CFfiDiscoveredDevice {
-    protocol: u32,
-    device: u8,
-    name: [c_char; 32],
-    software_version: u8,
-    status: u32,
-    supported_receivers: u32,
-    frequency_min: f64,
-    frequency_max: f64,
+    protocol: c_int,
+    device: c_int,
+    name: [c_char; 64],
+    software_version: c_int,
+    status: c_int,
+    supported_receivers: c_int,
+    frequency_min: c_double,
+    frequency_max: c_double,
     network: CFfiNetworkInfo,
     use_tcp: c_int,
 }
@@ -49,7 +49,7 @@ pub extern "C" fn discover_devices_ffi(devices: *mut CFfiDiscoveredDevice, max_d
 
 fn convert_to_c_device(device: &DiscoveredDevice) -> CFfiDiscoveredDevice {
     let name_c = CString::new(device.name.clone()).unwrap();
-    let mut name_arr = [0i8; 32];
+    let mut name_arr = [0i8; 64];
     for (i, &b) in name_c.as_bytes().iter().enumerate().take(31) {
         name_arr[i] = b as i8;
     }
index ed089140f76917f0ed3ea3a9c22f79b48aea1ab5..b157e55b42a6c3cd55ed7088726cf3c2ebbe7494 100644 (file)
@@ -11,12 +11,12 @@ pub struct NetworkInfo {
 
 #[derive(Debug, Clone)]
 pub struct DiscoveredDevice {
-    pub protocol: u32,
-    pub device: u8,
+    pub protocol: i32,
+    pub device: i32,
     pub name: String,
-    pub software_version: u8,
-    pub status: u32,
-    pub supported_receivers: u32,
+    pub software_version: i32,
+    pub status: i32,
+    pub supported_receivers: i32,
     pub frequency_min: f64,
     pub frequency_max: f64,
     pub network: NetworkInfo,