]> sigrok.org Git - libserialport.git/blobdiff - macosx.c
linux: Fix warning by marking vid, pid variables unsigned.
[libserialport.git] / macosx.c
index c1c5d9106387c9a595bbc149c292248dc8941637..236bf4c012b658bf617f69f5671cb265e0e84558 100644 (file)
--- a/macosx.c
+++ b/macosx.c
 #include "libserialport.h"
 #include "libserialport_internal.h"
 
-enum sp_return get_port_details(struct sp_port *port)
+SP_PRIV enum sp_return get_port_details(struct sp_port *port)
 {
        /* Description limited to 127 char,
           anything longer would not be user friendly anyway */
        char description[128];
        int bus, address, vid, pid = -1;
        char manufacturer[128], product[128], serial[128];
-       char baddr[32];
        CFMutableDictionaryRef classes;
        io_iterator_t iter;
-       io_object_t ioport;
+       io_object_t ioport, ioparent;
        CFTypeRef cf_property, cf_bus, cf_address, cf_vendor, cf_product;
        Boolean result;
-       char path[PATH_MAX];
+       char path[PATH_MAX], class[16];
 
        DEBUG("Getting serial port list");
        if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue)))
@@ -175,3 +174,52 @@ enum sp_return get_port_details(struct sp_port *port)
 
        RETURN_OK();
 }
+
+SP_PRIV enum sp_return list_ports(struct sp_port ***list)
+{
+       CFMutableDictionaryRef classes;
+       io_iterator_t iter;
+       char path[PATH_MAX];
+       io_object_t port;
+       CFTypeRef cf_path;
+       Boolean result;
+       int ret = SP_OK;
+
+       DEBUG("Creating matching dictionary");
+       if (!(classes = IOServiceMatching(kIOSerialBSDServiceValue))) {
+               SET_FAIL(ret, "IOServiceMatching() failed");
+               goto out_done;
+       }
+
+       DEBUG("Getting matching services");
+       if (IOServiceGetMatchingServices(kIOMasterPortDefault, classes,
+                                        &iter) != KERN_SUCCESS) {
+               SET_FAIL(ret, "IOServiceGetMatchingServices() failed");
+               goto out_done;
+       }
+
+       DEBUG("Iterating over results");
+       while ((port = IOIteratorNext(iter))) {
+               cf_path = IORegistryEntryCreateCFProperty(port,
+                               CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
+               if (cf_path) {
+                       result = CFStringGetCString(cf_path, path, sizeof(path),
+                                                   kCFStringEncodingASCII);
+                       CFRelease(cf_path);
+                       if (result) {
+                               DEBUG("Found port %s", path);
+                               if (!(*list = list_append(*list, path))) {
+                                       SET_ERROR(ret, SP_ERR_MEM, "list append failed");
+                                       IOObjectRelease(port);
+                                       goto out;
+                               }
+                       }
+               }
+               IOObjectRelease(port);
+       }
+out:
+       IOObjectRelease(iter);
+out_done:
+
+       return ret;
+}