]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Simplify Mac OS implementation of sp_list_ports().
[libserialport.git] / serialport.c
index 12cb52946f7096644c3b6c9cecbe18465be5c749..3d15106d585f5ec843a249fc289a9e2c0dcf321a 100644 (file)
@@ -32,7 +32,6 @@
 #ifdef _WIN32
 #include <windows.h>
 #include <tchar.h>
-#include <stdio.h>
 #else
 #include <limits.h>
 #include <termios.h>
@@ -166,10 +165,10 @@ void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
 
 /* Debug output macros. */
 #define DEBUG(fmt, ...) do { if (sp_debug_handler) sp_debug_handler(fmt ".\n", ##__VA_ARGS__); } while (0)
-#define DEBUG_ERROR(err, msg) DEBUG("%s returning " #err ": " msg, __func__)
-#define DEBUG_FAIL(msg) do { \
+#define DEBUG_ERROR(err, fmt, ...) DEBUG("%s returning " #err ": " fmt, __func__, ##__VA_ARGS__)
+#define DEBUG_FAIL(fmt, ...) do {               \
        char *errmsg = sp_last_error_message(); \
-       DEBUG("%s returning SP_ERR_FAIL: " msg ": %s", __func__, errmsg); \
+       DEBUG("%s returning SP_ERR_FAIL: "fmt": %s", __func__,##__VA_ARGS__,errmsg); \
        sp_free_error_message(errmsg); \
 } while (0);
 #define RETURN() do { DEBUG("%s returning", __func__); return; } while(0)
@@ -184,8 +183,8 @@ void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
        } \
 } while (0)
 #define RETURN_OK() RETURN_CODE(SP_OK);
-#define RETURN_ERROR(err, msg) do { DEBUG_ERROR(err, msg); return err; } while (0)
-#define RETURN_FAIL(msg) do { DEBUG_FAIL(msg); return SP_ERR_FAIL; } while (0)
+#define RETURN_ERROR(err, ...) do { DEBUG_ERROR(err, __VA_ARGS__); return err; } while (0)
+#define RETURN_FAIL(...) do { DEBUG_FAIL(__VA_ARGS__); return SP_ERR_FAIL; } while (0)
 #define RETURN_VALUE(fmt, x) do { \
        typeof(x) _x = x; \
        DEBUG("%s returning " fmt, __func__, _x); \
@@ -198,7 +197,6 @@ void (*sp_debug_handler)(const char *format, ...) = sp_default_debug_handler;
 #define TRY(x) do { int ret = x; if (ret != SP_OK) RETURN_CODEVAL(ret); } while (0)
 
 /* Helper functions. */
-static struct sp_port **list_append(struct sp_port **list, const char *portname);
 static enum sp_return get_config(struct sp_port *port, struct port_data *data,
        struct sp_port_config *config);
 static enum sp_return set_config(struct sp_port *port, struct port_data *data,
@@ -389,7 +387,7 @@ enum sp_return sp_list_ports(struct sp_port ***list_ptr)
                data_len = data_size / sizeof(TCHAR);
                data[data_len] = '\0';
 #ifdef UNICODE
-               name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL)
+               name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL);
 #else
                name_len = data_len + 1;
 #endif
@@ -420,49 +418,35 @@ out_close:
 out_done:
 #endif
 #ifdef __APPLE__
-       mach_port_t master;
        CFMutableDictionaryRef classes;
        io_iterator_t iter;
-       char *path;
+       char path[PATH_MAX];
        io_object_t port;
        CFTypeRef cf_path;
        Boolean result;
 
        ret = SP_OK;
 
-       DEBUG("Getting IOKit master port");
-       if (IOMasterPort(MACH_PORT_NULL, &master) != KERN_SUCCESS) {
-               SET_FAIL(ret, "IOMasterPort() failed");
-               goto out_done;
-       }
-
        DEBUG("Creating matching dictionary");
        if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
                SET_FAIL(ret, "IOServiceMatching() failed");
                goto out_done;
        }
 
-       CFDictionarySetValue(classes,
-                       CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
-
        DEBUG("Getting matching services");
-       if (IOServiceGetMatchingServices(master, classes, &iter) != KERN_SUCCESS) {
+       if (IOServiceGetMatchingServices(kIOMasterPortDefault, classes,
+                                        &iter) != KERN_SUCCESS) {
                SET_FAIL(ret, "IOServiceGetMatchingServices() failed");
                goto out_done;
        }
 
-       if (!(path = malloc(PATH_MAX))) {
-               SET_ERROR(ret, SP_ERR_MEM, "device path malloc failed");
-               goto out_release;
-       }
-
        DEBUG("Iterating over results");
        while ((port = IOIteratorNext(iter))) {
                cf_path = IORegistryEntryCreateCFProperty(port,
                                CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
                if (cf_path) {
-                       result = CFStringGetCString(cf_path,
-                                       path, PATH_MAX, kCFStringEncodingASCII);
+                       result = CFStringGetCString(cf_path, path, sizeof(path),
+                                                   kCFStringEncodingASCII);
                        CFRelease(cf_path);
                        if (result) {
                                DEBUG("Found port %s", path);
@@ -476,8 +460,6 @@ out_done:
                IOObjectRelease(port);
        }
 out:
-       free(path);
-out_release:
        IOObjectRelease(iter);
 out_done:
 #endif
@@ -626,7 +608,7 @@ enum sp_return sp_open(struct sp_port *port, enum sp_mode flags)
        COMSTAT status;
 
        /* Prefix port name with '\\.\' to work with ports above COM9. */
-       if (!(escaped_port_name = malloc(strlen(port->name + 5))))
+       if (!(escaped_port_name = malloc(strlen(port->name) + 5)))
                RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
        sprintf(escaped_port_name, "\\\\.\\%s", port->name);
 
@@ -2471,3 +2453,45 @@ void sp_default_debug_handler(const char *format, ...)
        }
        va_end(args);
 }
+
+int sp_get_major_package_version(void)
+{
+       return SP_PACKAGE_VERSION_MAJOR;
+}
+
+int sp_get_minor_package_version(void)
+{
+       return SP_PACKAGE_VERSION_MINOR;
+}
+
+int sp_get_micro_package_version(void)
+{
+       return SP_PACKAGE_VERSION_MICRO;
+}
+
+const char *sp_get_package_version_string(void)
+{
+       return SP_PACKAGE_VERSION_STRING;
+}
+
+int sp_get_current_lib_version(void)
+{
+       return SP_LIB_VERSION_CURRENT;
+}
+
+int sp_get_revision_lib_version(void)
+{
+       return SP_LIB_VERSION_REVISION;
+}
+
+int sp_get_age_lib_version(void)
+{
+       return SP_LIB_VERSION_AGE;
+}
+
+const char *sp_get_lib_version_string(void)
+{
+       return SP_LIB_VERSION_STRING;
+}
+
+/** @} */