22 #include "wx/wxprec.h"
29 #include <sys/ioctl.h>
34 #include <sys/param.h>
35 #include <sys/select.h>
37 #include <sys/sysctl.h>
40 #include <AvailabilityMacros.h>
42 #include <CoreFoundation/CoreFoundation.h>
44 #include <IOKit/IOKitLib.h>
45 #include <IOKit/serial/IOSerialKeys.h>
46 #if defined(MAC_OS_X_VERSION_10_3) && \
47 (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3)
48 #include <IOKit/serial/ioss.h>
50 #include <IOKit/IOBSD.h>
54 #include <ApplicationServices/ApplicationServices.h>
63 static kern_return_t FindSerialPorts(io_iterator_t* matchingServices) {
64 kern_return_t kernResult;
65 CFMutableDictionaryRef classesToMatch;
83 classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
84 if (classesToMatch == NULL) {
85 printf(
"IOServiceMatching returned a NULL dictionary.\n");
109 CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey),
110 CFSTR(kIOSerialBSDAllTypes));
142 kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault,
143 classesToMatch, matchingServices);
144 if (KERN_SUCCESS != kernResult) {
145 printf(
"IOServiceGetMatchingServices returned %d\n", kernResult);
156 static int GetSerialPortPath(io_iterator_t serialPortIterator,
char** pNames,
157 int iMaxNames, CFIndex maxPathSize) {
158 io_object_t modemService;
160 Boolean modemFound =
false;
161 char bsdPath[maxPathSize];
162 int iCurrentNameIndex = 0;
168 while ((modemService = IOIteratorNext(serialPortIterator)) && !modemFound) {
169 CFTypeRef bsdPathAsCFString;
175 bsdPathAsCFString = IORegistryEntryCreateCFProperty(
176 modemService, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
177 if (bsdPathAsCFString) {
183 result = CFStringGetCString(bsdPathAsCFString, bsdPath, maxPathSize,
184 kCFStringEncodingUTF8);
185 CFRelease(bsdPathAsCFString);
188 pNames[iCurrentNameIndex] = calloc(1, strlen(bsdPath) + 1);
189 strncpy(pNames[iCurrentNameIndex], bsdPath, strlen(bsdPath) + 1);
194 (void)IOObjectRelease(modemService);
196 return iCurrentNameIndex;
199 int FindSerialPortNames(
char** pNames,
int iMaxNames) {
200 int iActiveNameCount = 0;
201 kern_return_t kernResult;
203 io_iterator_t serialPortIterator;
205 kernResult = FindSerialPorts(&serialPortIterator);
208 GetSerialPortPath(serialPortIterator, pNames, iMaxNames, MAXPATHLEN);
210 IOObjectRelease(serialPortIterator);
211 return iActiveNameCount;
214 bool ValidateSerialPortName(
char* pPortName,
int iMaxNamestoSearch) {
215 char* paPortNames[iMaxNamestoSearch];
218 bool bPortFound =
false;
219 char* pPortSubName = index(pPortName,
':');
221 if (0 == strcasecmp(pPortName,
"NONE"))
return true;
224 if (NULL == pPortSubName)
225 pPortSubName = pPortName;
229 memset(paPortNames, 0x00,
sizeof(paPortNames));
230 iPortNameCount = FindSerialPortNames(&paPortNames[0], iMaxNamestoSearch);
231 for (iPortIndex = 0; iPortIndex < iPortNameCount; iPortIndex++) {
233 int iStrCompresult = strcmp(paPortNames[iPortIndex], pPortSubName);
234 if (
false == bPortFound) bPortFound = (bool)(0 == iStrCompresult);
235 free(paPortNames[iPortIndex]);
243 int GetMacMonitorSize() {
244 CGSize displayPhysicalSize = CGDisplayScreenSize(CGMainDisplayID());
245 return displayPhysicalSize.width;
252 int ProcessIsTranslated() {
254 size_t size =
sizeof(ret);
255 if (sysctlbyname(
"sysctl.proc_translated", &ret, &size, NULL, 0) == -1)
264 int IsAppleSilicon() {
266 size_t size =
sizeof(ret);
268 if (sysctlbyname(
"hw.optional.arm64", &ret, &size, NULL, 0) == -1) {