]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Simplify Mac OS implementation of sp_list_ports().
[libserialport.git] / serialport.c
index 89fbcec6a3b5ef7f638ba0ee4c2f5b8b69960437..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>
@@ -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,
@@ -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