list_osx.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. Copyright (c) 2013-2024 Bruce A Henderson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <IOKit/usb/IOUSBLib.h>
  20. #include <IOKit/serial/IOSerialKeys.h>
  21. #include <CoreFoundation/CFNumber.h>
  22. #include <IOKit/IOBSD.h>
  23. #include <sys/param.h>
  24. extern "C" {
  25. #include "blitz.h"
  26. BBObject * io_serial_TSerialPortInfo__create(BBString * portName, BBString * physicalName, BBString * productName,
  27. BBString * enumeratorName, int vendorId, int productId);
  28. void io_serial_TSerialPortInfo__addInfo(BBObject * list, BBObject * info);
  29. void bmx_serial_listports(BBObject * list);
  30. void walkIterator(io_iterator_t serialPortIterator, BBObject * list);
  31. }
  32. void bmx_serial_listports(BBObject * list) {
  33. io_iterator_t serialPortIterator = 0;
  34. CFMutableDictionaryRef matchingDictionary;
  35. if (!(matchingDictionary = IOServiceMatching(kIOSerialBSDServiceValue))) {
  36. return;
  37. }
  38. CFDictionaryAddValue(matchingDictionary, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes));
  39. if (IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &serialPortIterator) != KERN_SUCCESS) {
  40. return;
  41. }
  42. walkIterator(serialPortIterator, list);
  43. IOObjectRelease(serialPortIterator);
  44. serialPortIterator = 0;
  45. if (!(matchingDictionary = IOServiceNameMatching("AppleUSBCDC"))) {
  46. return;
  47. }
  48. if (IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDictionary, &serialPortIterator) != KERN_SUCCESS) {
  49. return;
  50. }
  51. walkIterator(serialPortIterator, list);
  52. IOObjectRelease(serialPortIterator);
  53. }
  54. void walkIterator(io_iterator_t serialPortIterator, BBObject * list) {
  55. io_object_t usbDevice;
  56. while ((usbDevice = IOIteratorNext(serialPortIterator))) {
  57. BBString * bbPortName = &bbEmptyString;
  58. BBString * bbPhysicalName = &bbEmptyString;
  59. BBString * bbProductName = &bbEmptyString;
  60. BBString * bbEnumeratorName = &bbEmptyString;
  61. int bbVendorId = 0;
  62. int bbProductId = 0;
  63. CFTypeRef port = IORegistryEntryCreateCFProperty(usbDevice, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
  64. CFTypeRef productName = 0;
  65. CFTypeRef vendorId = 0;
  66. CFTypeRef productId = 0;
  67. io_registry_entry_t parent;
  68. kern_return_t result = IORegistryEntryGetParentEntry(usbDevice, kIOServicePlane, &parent);
  69. while (result == KERN_SUCCESS && !vendorId && !productId) {
  70. if (!productName) {
  71. productName = IORegistryEntrySearchCFProperty(parent, kIOServicePlane, CFSTR("Product Name"), kCFAllocatorDefault, 0);
  72. }
  73. vendorId = IORegistryEntrySearchCFProperty(parent, kIOServicePlane, CFSTR(kUSBVendorID), kCFAllocatorDefault, 0);
  74. productId = IORegistryEntrySearchCFProperty(parent, kIOServicePlane, CFSTR(kUSBProductID), kCFAllocatorDefault, 0);
  75. io_registry_entry_t previousParent = parent;
  76. result = IORegistryEntryGetParentEntry(parent, kIOServicePlane, &parent);
  77. IOObjectRelease(previousParent);
  78. }
  79. io_string_t physicalName;
  80. IORegistryEntryGetPath(usbDevice, kIOServicePlane, physicalName);
  81. bbPhysicalName = bbStringFromUTF8String((unsigned char*)physicalName);
  82. if (port) {
  83. char buffer[MAXPATHLEN];
  84. if (CFStringGetCString((CFStringRef)port, buffer, PATH_MAX, kCFStringEncodingUTF8)) {
  85. bbPortName = bbStringFromUTF8String((unsigned char*)buffer);
  86. }
  87. CFRelease(port);
  88. }
  89. if (productName) {
  90. char buffer[MAXPATHLEN];
  91. if (CFStringGetCString((CFStringRef)productName, buffer, PATH_MAX, kCFStringEncodingUTF8)) {
  92. bbProductName = bbStringFromUTF8String((unsigned char*)buffer);
  93. }
  94. CFRelease(productName);
  95. }
  96. if (vendorId) {
  97. SInt32 id;
  98. if (CFNumberGetValue((CFNumberRef)vendorId, kCFNumberSInt32Type, &id)) {
  99. bbVendorId = id;
  100. }
  101. CFRelease(vendorId);
  102. }
  103. if (productId) {
  104. SInt32 id;
  105. if (CFNumberGetValue((CFNumberRef)productId, kCFNumberSInt32Type, &id)) {
  106. bbProductId = id;
  107. }
  108. CFRelease(productId);
  109. }
  110. IOObjectRelease(usbDevice);
  111. BBObject * info = io_serial_TSerialPortInfo__create(bbPortName, bbPhysicalName, bbProductName, bbEnumeratorName, bbVendorId, bbProductId);
  112. io_serial_TSerialPortInfo__addInfo(list, info);
  113. }
  114. }