|
| 1 | +// |
| 2 | +// Copyright 2014-2017 Cristian Maglie. All rights reserved. |
| 3 | +// Use of this source code is governed by a BSD-style |
| 4 | +// license that can be found in the LICENSE file. |
| 5 | +// |
| 6 | + |
| 7 | +// +build !go1.10,darwin |
| 8 | + |
| 9 | +// This file is here to keep compatibility with the older versions of go |
| 10 | +// and is no more maintained or bugfixed, please update your go version |
| 11 | +// to at least 1.10 to get the latest updates. |
| 12 | + |
| 13 | +package enumerator // import "go.bug.st/serial.v1/enumerator" |
| 14 | + |
| 15 | +// #cgo LDFLAGS: -framework CoreFoundation -framework IOKit |
| 16 | +// #include <IOKit/IOKitLib.h> |
| 17 | +// #include <CoreFoundation/CoreFoundation.h> |
| 18 | +// #include <stdlib.h> |
| 19 | +import "C" |
| 20 | +import ( |
| 21 | + "errors" |
| 22 | + "fmt" |
| 23 | + "unsafe" |
| 24 | +) |
| 25 | + |
| 26 | +func nativeGetDetailedPortsList() ([]*PortDetails, error) { |
| 27 | + var ports []*PortDetails |
| 28 | + |
| 29 | + services, err := getAllServices("IOSerialBSDClient") |
| 30 | + if err != nil { |
| 31 | + return nil, &PortEnumerationError{causedBy: err} |
| 32 | + } |
| 33 | + for _, service := range services { |
| 34 | + defer service.Release() |
| 35 | + |
| 36 | + port, err := extractPortInfo(C.io_registry_entry_t(service)) |
| 37 | + if err != nil { |
| 38 | + return nil, &PortEnumerationError{causedBy: err} |
| 39 | + } |
| 40 | + ports = append(ports, port) |
| 41 | + } |
| 42 | + return ports, nil |
| 43 | +} |
| 44 | + |
| 45 | +func extractPortInfo(service C.io_registry_entry_t) (*PortDetails, error) { |
| 46 | + name, err := service.GetStringProperty("IOCalloutDevice") |
| 47 | + if err != nil { |
| 48 | + return nil, fmt.Errorf("Error extracting port info from device: %s", err.Error()) |
| 49 | + } |
| 50 | + port := &PortDetails{} |
| 51 | + port.Name = name |
| 52 | + port.IsUSB = false |
| 53 | + |
| 54 | + usbDevice := service |
| 55 | + for usbDevice.GetClass() != "IOUSBDevice" { |
| 56 | + if usbDevice, err = usbDevice.GetParent("IOService"); err != nil { |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + if err == nil { |
| 61 | + // It's an IOUSBDevice |
| 62 | + vid, _ := usbDevice.GetIntProperty("idVendor", C.kCFNumberSInt16Type) |
| 63 | + pid, _ := usbDevice.GetIntProperty("idProduct", C.kCFNumberSInt16Type) |
| 64 | + serialNumber, _ := usbDevice.GetStringProperty("USB Serial Number") |
| 65 | + //product, _ := usbDevice.GetStringProperty("USB Product Name") |
| 66 | + //manufacturer, _ := usbDevice.GetStringProperty("USB Vendor Name") |
| 67 | + //fmt.Println(product + " - " + manufacturer) |
| 68 | + |
| 69 | + port.IsUSB = true |
| 70 | + port.VID = fmt.Sprintf("%04X", vid) |
| 71 | + port.PID = fmt.Sprintf("%04X", pid) |
| 72 | + port.SerialNumber = serialNumber |
| 73 | + } |
| 74 | + return port, nil |
| 75 | +} |
| 76 | + |
| 77 | +func getAllServices(serviceType string) ([]C.io_object_t, error) { |
| 78 | + i, err := getMatchingServices(serviceMatching(serviceType)) |
| 79 | + if err != nil { |
| 80 | + return nil, err |
| 81 | + } |
| 82 | + defer i.Release() |
| 83 | + |
| 84 | + var services []C.io_object_t |
| 85 | + tries := 0 |
| 86 | + for tries < 5 { |
| 87 | + // Extract all elements from iterator |
| 88 | + if service, ok := i.Next(); ok { |
| 89 | + services = append(services, service) |
| 90 | + continue |
| 91 | + } |
| 92 | + // If iterator is still valid return the result |
| 93 | + if i.IsValid() { |
| 94 | + return services, nil |
| 95 | + } |
| 96 | + // Otherwise empty the result and retry |
| 97 | + for _, s := range services { |
| 98 | + s.Release() |
| 99 | + } |
| 100 | + services = []C.io_object_t{} |
| 101 | + i.Reset() |
| 102 | + tries++ |
| 103 | + } |
| 104 | + // Give up if the iteration continues to fail... |
| 105 | + return nil, fmt.Errorf("IOServiceGetMatchingServices failed, data changed while iterating") |
| 106 | +} |
| 107 | + |
| 108 | +// serviceMatching create a matching dictionary that specifies an IOService class match. |
| 109 | +func serviceMatching(serviceType string) C.CFMutableDictionaryRef { |
| 110 | + t := C.CString(serviceType) |
| 111 | + defer C.free(unsafe.Pointer(t)) |
| 112 | + return C.IOServiceMatching(t) |
| 113 | +} |
| 114 | + |
| 115 | +// getMatchingServices look up registered IOService objects that match a matching dictionary. |
| 116 | +func getMatchingServices(matcher C.CFMutableDictionaryRef) (C.io_iterator_t, error) { |
| 117 | + var i C.io_iterator_t |
| 118 | + err := C.IOServiceGetMatchingServices(C.kIOMasterPortDefault, matcher, &i) |
| 119 | + if err != C.KERN_SUCCESS { |
| 120 | + return 0, fmt.Errorf("IOServiceGetMatchingServices failed (code %d)", err) |
| 121 | + } |
| 122 | + return i, nil |
| 123 | +} |
| 124 | + |
| 125 | +// CFStringRef |
| 126 | + |
| 127 | +func cfStringCreateWithString(s string) C.CFStringRef { |
| 128 | + c := C.CString(s) |
| 129 | + defer C.free(unsafe.Pointer(c)) |
| 130 | + return C.CFStringCreateWithCString( |
| 131 | + C.kCFAllocatorDefault, c, C.kCFStringEncodingMacRoman) |
| 132 | +} |
| 133 | + |
| 134 | +// io_registry_entry_t |
| 135 | + |
| 136 | +func (me *C.io_registry_entry_t) GetParent(plane string) (C.io_registry_entry_t, error) { |
| 137 | + cPlane := C.CString(plane) |
| 138 | + defer C.free(unsafe.Pointer(cPlane)) |
| 139 | + var parent C.io_registry_entry_t |
| 140 | + err := C.IORegistryEntryGetParentEntry(*me, cPlane, &parent) |
| 141 | + if err != 0 { |
| 142 | + return 0, errors.New("No parent device available") |
| 143 | + } |
| 144 | + return parent, nil |
| 145 | +} |
| 146 | + |
| 147 | +func (me *C.io_registry_entry_t) GetClass() string { |
| 148 | + obj := (*C.io_object_t)(me) |
| 149 | + return obj.GetClass() |
| 150 | +} |
| 151 | + |
| 152 | +func (me *C.io_registry_entry_t) GetStringProperty(key string) (string, error) { |
| 153 | + k := cfStringCreateWithString(key) |
| 154 | + defer C.CFRelease(C.CFTypeRef(k)) |
| 155 | + property := C.IORegistryEntryCreateCFProperty(*me, k, C.kCFAllocatorDefault, 0) |
| 156 | + if property == nil { |
| 157 | + return "", errors.New("Property not found: " + key) |
| 158 | + } |
| 159 | + defer C.CFRelease(property) |
| 160 | + |
| 161 | + if ptr := C.CFStringGetCStringPtr((C.CFStringRef)(unsafe.Pointer(property)), 0); ptr != nil { |
| 162 | + return C.GoString(ptr), nil |
| 163 | + } |
| 164 | + // in certain circumstances CFStringGetCStringPtr may return NULL |
| 165 | + // and we must retrieve the string by copy |
| 166 | + buff := make([]C.char, 1024) |
| 167 | + if C.CFStringGetCString((C.CFStringRef)(property), &buff[0], 1024, 0) != C.true { |
| 168 | + return "", fmt.Errorf("Property '%s' can't be converted", key) |
| 169 | + } |
| 170 | + return C.GoString(&buff[0]), nil |
| 171 | +} |
| 172 | + |
| 173 | +func (me *C.io_registry_entry_t) GetIntProperty(key string, intType C.CFNumberType) (int, error) { |
| 174 | + k := cfStringCreateWithString(key) |
| 175 | + defer C.CFRelease(C.CFTypeRef(k)) |
| 176 | + property := C.IORegistryEntryCreateCFProperty(*me, k, C.kCFAllocatorDefault, 0) |
| 177 | + if property == nil { |
| 178 | + return 0, errors.New("Property not found: " + key) |
| 179 | + } |
| 180 | + defer C.CFRelease(property) |
| 181 | + var res int |
| 182 | + if C.CFNumberGetValue((C.CFNumberRef)(property), intType, unsafe.Pointer(&res)) != C.true { |
| 183 | + return res, fmt.Errorf("Property '%s' can't be converted or has been truncated", key) |
| 184 | + } |
| 185 | + return res, nil |
| 186 | +} |
| 187 | + |
| 188 | +// io_iterator_t |
| 189 | + |
| 190 | +// IsValid checks if an iterator is still valid. |
| 191 | +// Some iterators will be made invalid if changes are made to the |
| 192 | +// structure they are iterating over. This function checks the iterator |
| 193 | +// is still valid and should be called when Next returns zero. |
| 194 | +// An invalid iterator can be Reset and the iteration restarted. |
| 195 | +func (me *C.io_iterator_t) IsValid() bool { |
| 196 | + return C.IOIteratorIsValid(*me) == C.true |
| 197 | +} |
| 198 | + |
| 199 | +func (me *C.io_iterator_t) Reset() { |
| 200 | + C.IOIteratorReset(*me) |
| 201 | +} |
| 202 | + |
| 203 | +func (me *C.io_iterator_t) Next() (C.io_object_t, bool) { |
| 204 | + res := C.IOIteratorNext(*me) |
| 205 | + return res, res != 0 |
| 206 | +} |
| 207 | + |
| 208 | +func (me *C.io_iterator_t) Release() { |
| 209 | + C.IOObjectRelease(C.io_object_t(*me)) |
| 210 | +} |
| 211 | + |
| 212 | +// io_object_t |
| 213 | + |
| 214 | +func (me *C.io_object_t) Release() { |
| 215 | + C.IOObjectRelease(*me) |
| 216 | +} |
| 217 | + |
| 218 | +func (me *C.io_object_t) GetClass() string { |
| 219 | + class := make([]C.char, 1024) |
| 220 | + C.IOObjectGetClass(*me, &class[0]) |
| 221 | + return C.GoString(&class[0]) |
| 222 | +} |
0 commit comments