| 1 | /*---------------------------------------------------------------------------- |
| 2 | ChucK Strongly-timed Audio Programming Language |
| 3 | Compiler, Virtual Machine, and Synthesis Engine |
| 4 | |
| 5 | Copyright (c) 2003 Ge Wang and Perry R. Cook. All rights reserved. |
| 6 | http://chuck.stanford.edu/ |
| 7 | http://chuck.cs.princeton.edu/ |
| 8 | |
| 9 | This program is free software; you can redistribute it and/or modify |
| 10 | it under the dual-license terms of EITHER the MIT License OR the GNU |
| 11 | General Public License (the latter as published by the Free Software |
| 12 | Foundation; either version 2 of the License or, at your option, any |
| 13 | later version). |
| 14 | |
| 15 | This program is distributed in the hope that it will be useful and/or |
| 16 | interesting, but WITHOUT ANY WARRANTY; without even the implied warranty |
| 17 | of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | MIT Licence and/or the GNU General Public License for details. |
| 19 | |
| 20 | You should have received a copy of the MIT License and the GNU General |
| 21 | Public License (GPL) along with this program; a copy of the GPL can also |
| 22 | be obtained by writing to the Free Software Foundation, Inc., 59 Temple |
| 23 | Place, Suite 330, Boston, MA 02111-1307 U.S.A. |
| 24 | -----------------------------------------------------------------------------*/ |
| 25 | |
| 26 | //----------------------------------------------------------------------------- |
| 27 | // file: util_serial.cpp |
| 28 | // desc: utility for serial I/O |
| 29 | // |
| 30 | // author: Spencer Salazar ([email protected]) |
| 31 | // date: Summer 2012 |
| 32 | //----------------------------------------------------------------------------- |
| 33 | #include "util_serial.h" |
| 34 | #include "chuck_errmsg.h" |
| 35 | |
| 36 | #if defined(__PLATFORM_APPLE__) && !defined(__CHIP_MODE__) |
| 37 | |
| 38 | #include <CoreFoundation/CoreFoundation.h> |
| 39 | #include <IOKit/IOKitLib.h> |
| 40 | #include <IOKit/serial/IOSerialKeys.h> |
| 41 | #include <IOKit/IOBSD.h> |
| 42 | |
| 43 | #include <Availability.h> // 1.5.0.7 (ge) added; __MAC_OS_X_VERSION_MIN_REQUIRED |
| 44 | |
| 45 | using namespace std; |
| 46 | |
| 47 | vector<string> SerialIOManager::availableSerialDevices() |
| 48 | { |
| 49 | vector<string> devices; |
| 50 | |
| 51 | io_iterator_t serialIterator = 0; // NULL; |
| 52 | kern_return_t kernResult; |
| 53 | mach_port_t mainPort; |
| 54 | CFMutableDictionaryRef classesToMatch = NULL; |
| 55 | |
| 56 | io_object_t serialObject; |
| 57 | static char resultStr[256]; |
| 58 | CFStringRef calloutCFKeyName = CFStringCreateWithCString(kCFAllocatorDefault, kIOCalloutDeviceKey, kCFStringEncodingUTF8); |
| 59 | CFStringRef dialinCFKeyName = CFStringCreateWithCString(kCFAllocatorDefault, kIODialinDeviceKey, kCFStringEncodingUTF8); |
| 60 | |
| 61 | // ge: changed NULL to 0 below to get rid of warning 1.3.2.0 |
| 62 | // ge: updated kIOMasterPort (deprecated as of macOS 12) to kIOMainPort 1.5.0.7 |
| 63 | #if __MAC_OS_X_VERSION_MIN_REQUIRED <= 120000 // before macOS 12 |
| 64 | if( (kernResult = IOMasterPort(0, &mainPort)) != KERN_SUCCESS ) |
| 65 | #else |
| 66 | if( (kernResult = IOMainPort(0, &mainPort)) != KERN_SUCCESS ) |
| 67 | #endif |
| 68 | { |
| 69 | EM_log(CK_LOG_WARNING, "[SerialIOManager] IOMasterPort returned %d\n", kernResult); |
| 70 | goto error; |
| 71 | } |
| 72 | |
| 73 | // curious about how the version macro above works? |
| 74 | // in src/core/ try `make mac` (uses OS target, e.g., 1090) vs. `make vanilla` (uses current version 130000) |
| 75 | // uncomment the next three lines to print the preprocessor value at compile time |
| 76 | //#define STRING2(x) #x |
| 77 | //#define STRING(x) STRING2(x) |
| 78 | //#pragma message ("__MAC_OS_X_VERSION_MIN_REQUIRED = " STRING(__MAC_OS_X_VERSION_MIN_REQUIRED)) |
| 79 | |
| 80 | if((classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue)) == NULL) |
| 81 | { |
| 82 | EM_log(CK_LOG_WARNING, "[SerialIOManager] IOServiceMatching returned NULL\n" ); |
| 83 | goto error; |
| 84 | } |
| 85 | CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey), CFSTR(kIOSerialBSDAllTypes)); |
| 86 | kernResult = IOServiceGetMatchingServices(mainPort, classesToMatch, &serialIterator); |
| 87 | classesToMatch = NULL; |
| 88 | if(kernResult != KERN_SUCCESS) |
| 89 | { |
| 90 | EM_log(CK_LOG_WARNING, "[SerialIOManager] IOServiceGetMatchingServices returned %d\n", kernResult); |
| 91 | goto error; |
| 92 | } |
| 93 | |
| 94 | while((serialObject = IOIteratorNext(serialIterator))) |
| 95 | { |
| 96 | CFTypeRef nameCFstring; |
| 97 | nameCFstring = IORegistryEntryCreateCFProperty(serialObject, |
| 98 | calloutCFKeyName, |
| 99 | kCFAllocatorDefault, 0); |
| 100 | if(nameCFstring) |
| 101 | { |
| 102 | CFStringGetCString((CFStringRef) nameCFstring, resultStr, sizeof(resultStr), kCFStringEncodingUTF8); |
| 103 | CFRelease(nameCFstring); |
| 104 | devices.push_back(string(resultStr)); |
| 105 | } |
| 106 | |
| 107 | // TODO: use tty name also? |
| 108 | // see: http://stuffthingsandjunk.blogspot.com/2009/03/devcu-vs-devtty-osx-serial-ports.html |
| 109 | // nameCFstring = IORegistryEntryCreateCFProperty(serialObject, |
| 110 | // dialinCFKeyName, |
| 111 | // kCFAllocatorDefault, 0); |
| 112 | // if(nameCFstring) |
| 113 | // { |
| 114 | // CFStringGetCString((CFStringRef) nameCFstring, resultStr, sizeof(resultStr), kCFStringEncodingUTF8); |
| 115 | // CFRelease(nameCFstring); |
| 116 | // devices.push_back(string(resultStr)); |
| 117 | // } |
| 118 | } |
| 119 | |
| 120 | goto cleanup; |
| 121 | |
| 122 | error: |
| 123 | devices.clear(); |
| 124 | |
| 125 | cleanup: |
| 126 | if(classesToMatch) { CFRelease(classesToMatch); classesToMatch = NULL; } |
| 127 | if(calloutCFKeyName) { CFRelease(calloutCFKeyName); calloutCFKeyName = NULL; } |
| 128 | if(dialinCFKeyName) { CFRelease(dialinCFKeyName); dialinCFKeyName = NULL; } |
| 129 | |
| 130 | return devices; |
| 131 | } |
| 132 | |
| 133 | #elif defined(__PLATFORM_LINUX__) |
| 134 | |
| 135 | |
| 136 | #include <sys/types.h> |
| 137 | #include <dirent.h> |
| 138 | #include <unistd.h> |
| 139 | |
| 140 | using namespace std; |
| 141 | |
| 142 | vector<string> SerialIOManager::availableSerialDevices() |
| 143 | { |
| 144 | vector<string> devices; |
| 145 | |
| 146 | // two sizes | 1.5.0.1 (ge) updated, as pointed out by linux compilation warning |
| 147 | const int link_buf_size = 4096+256; |
| 148 | const int path_buf_size = 4096; |
| 149 | // was PATH_MAX, which apparently isn't |
| 150 | // https://stackoverflow.com/questions/9449241/where-is-path-max-defined-in-linux |
| 151 | // https://insanecoding.blogspot.com/2007/11/pathmax-simply-isnt.html |
| 152 | // https://news.ycombinator.com/item?id=14194681 |
| 153 | const char * serial_dir = "/dev/serial/by-id"; |
| 154 | char link_buf[link_buf_size]; |
| 155 | char path_buf[path_buf_size]; |
| 156 | |
| 157 | DIR * dir = opendir(serial_dir); |
| 158 | |
| 159 | if(dir == NULL) |
| 160 | { |
| 161 | // either we cannot open the directory, or there were no devices |
| 162 | EM_log(CK_LOG_INFO, "(SerialIOManager): unable to enumerate available devices"); |
| 163 | goto error; |
| 164 | } |
| 165 | |
| 166 | dirent * dir_info; |
| 167 | while((dir_info = readdir(dir))) |
| 168 | { |
| 169 | if(strcmp(dir_info->d_name, ".") == 0 || |
| 170 | strcmp(dir_info->d_name, "..") == 0) |
| 171 | continue; |
| 172 | snprintf(link_buf, link_buf_size, "%s/%s", serial_dir, dir_info->d_name); |
| 173 | int link_size = readlink(link_buf, path_buf, path_buf_size-1); |
| 174 | if(link_size <= 0) |
| 175 | { |
| 176 | EM_log(CK_LOG_INFO, "(SerialIOManager): readlink failed on '%s'", link_buf); |
| 177 | goto error; |
| 178 | } |
| 179 | |
| 180 | // readlink does not NULL-terminate (...) |
| 181 | path_buf[link_size] = '\0'; |
| 182 | |
| 183 | if(path_buf[0] != '/') |
| 184 | { |
| 185 | // normalize path |
| 186 | snprintf(link_buf, link_buf_size, "%s/%s", serial_dir, path_buf); |
| 187 | |
| 188 | if(!realpath(link_buf, path_buf)) |
| 189 | goto error; |
| 190 | } |
| 191 | |
| 192 | devices.push_back(string(path_buf)); |
| 193 | } |
| 194 | |
| 195 | goto cleanup; |
| 196 | |
| 197 | error: |
| 198 | devices.clear(); |
| 199 | |
| 200 | cleanup: |
| 201 | return devices; |
| 202 | } |
| 203 | |
| 204 | #elif defined(__PLATFORM_WINDOWS__) |
| 205 | |
| 206 | #include <windows.h> |
| 207 | |
| 208 | using namespace std; |
| 209 | |
| 210 | vector<string> SerialIOManager::availableSerialDevices() |
| 211 | { |
| 212 | vector<string> devices; |
| 213 | |
| 214 | HKEY hSERIALCOMM; |
| 215 | DWORD dwMaxValueNameLen, dwMaxValueLen; |
| 216 | char *name, *value; |
| 217 | |
| 218 | DWORD dwIndex = 0; |
| 219 | DWORD dwType; |
| 220 | DWORD dwValueNameSize; |
| 221 | DWORD dwDataSize; |
| 222 | |
| 223 | if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hSERIALCOMM) != ERROR_SUCCESS) |
| 224 | { |
| 225 | EM_error3("SerialIOManager: unable to open registry key"); |
| 226 | goto error; |
| 227 | } |
| 228 | |
| 229 | //Get the max value name and max value lengths |
| 230 | if(RegQueryInfoKey(hSERIALCOMM, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &dwMaxValueNameLen, &dwMaxValueLen, NULL, NULL) != ERROR_SUCCESS) |
| 231 | { |
| 232 | EM_error3("SerialIOManager: unable to query registry key info"); |
| 233 | goto error; |
| 234 | } |
| 235 | |
| 236 | name = new char[dwMaxValueNameLen+1]; |
| 237 | value = new char[dwMaxValueLen+1]; |
| 238 | |
| 239 | dwIndex = 0; |
| 240 | dwValueNameSize = dwMaxValueNameLen+1; |
| 241 | dwDataSize = dwMaxValueLen+1; |
| 242 | while(RegEnumValue(hSERIALCOMM, dwIndex, name, &dwValueNameSize, NULL, &dwType, (unsigned char *) value, &dwDataSize) == ERROR_SUCCESS) |
| 243 | { |
| 244 | if(dwType == REG_SZ) |
| 245 | { |
| 246 | devices.push_back(std::string(value)); |
| 247 | } |
| 248 | |
| 249 | dwIndex++; |
| 250 | dwValueNameSize = dwMaxValueNameLen+1; |
| 251 | dwDataSize = dwMaxValueLen+1; |
| 252 | } |
| 253 | |
| 254 | return devices; |
| 255 | |
| 256 | error: |
| 257 | |
| 258 | return devices; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | #else // ? e.g., iPhone |
| 263 | |
| 264 | |
| 265 | // for compatibility | 1.5.2.5 (ge & eito) added std:: |
| 266 | std::vector<std::string> SerialIOManager::availableSerialDevices() |
| 267 | { |
| 268 | // vector of devices |
| 269 | std::vector<std::string> devices; |
| 270 | // return empty list |
| 271 | return devices; |
| 272 | } |
| 273 | |
| 274 | |
| 275 | #endif // __PLATFORM_APPLE__ |