shithub: m8c

Download patch

ref: f8db42a59d96cb9560d762c4d59a85c369808b16
parent: fe4d396d063fa60052da9a28b704c83c9068c7da
author: Maido Käära <maido@producement.com>
date: Wed Dec 14 05:45:40 EST 2022

Add callbacks instead of exposing the handle

--- a/main.c
+++ b/main.c
@@ -56,6 +56,7 @@
 
   signal(SIGINT, intHandler);
   signal(SIGTERM, intHandler);
+  signal(SIGQUIT, intHandler);
 
   slip_init(&slip, &slip_descriptor);
 
--- a/serial.c
+++ b/serial.c
@@ -23,12 +23,23 @@
 #define ACM_CTRL_DTR   0x01
 #define ACM_CTRL_RTS   0x02
 
+usb_callback_t init_callback = NULL;
+usb_callback_t destroy_callback = NULL;
 libusb_device_handle *devh = NULL;
 int file_descriptor = -1;
 
+void set_usb_init_callback(usb_callback_t callback) {
+    init_callback = callback;
+}
+
+void set_usb_destroy_callback(usb_callback_t callback) {
+    destroy_callback = callback;
+}
+
 void set_file_descriptor(int fd) {
     file_descriptor = fd;
     if (fd == -1) {
+        libusb_exit(NULL);
         devh = NULL;
     }
 }
@@ -116,6 +127,15 @@
         return rc;
     }
 
+    if (init_callback != NULL) {
+        rc = init_callback(devh);
+
+        if (rc < 0) {
+            SDL_Log("Init callback failed: %d", rc);
+            return rc;
+        }
+    }
+
     for (int if_num = 0; if_num < 2; if_num++) {
         if (libusb_kernel_driver_active(devh, if_num)) {
             libusb_detach_kernel_driver(devh, if_num);
@@ -196,8 +216,18 @@
 }
 
 int disconnect() {
+
     char buf[1] = {'D'};
     int result;
+
+    if (destroy_callback != NULL) {
+        result = destroy_callback(devh);
+
+        if (result < 0) {
+            SDL_Log("Destroy callback failed: %d", result);
+            return result;
+        }
+    }
 
     SDL_Log("Disconnecting M8\n");
 
--- a/serial.h
+++ b/serial.h
@@ -8,8 +8,10 @@
 // Not sure about this value but it seems to work
 #define serial_read_size 512
 #include <libusb.h>
+typedef int (*usb_callback_t)(libusb_device_handle *devh);
+void set_usb_init_callback(usb_callback_t callback);
+void set_usb_destroy_callback(usb_callback_t callback);
 void set_file_descriptor(int fd);
-libusb_device_handle *get_handle();
 #else
 // maximum amount of bytes to read from the serial in one read()
 #define serial_read_size 324
--