35#include <sys/select.h>
36#include <sys/sysctl.h>
42#include <AvailabilityMacros.h>
43#include <CoreFoundation/CoreFoundation.h>
44#include <IOKit/IOKitLib.h>
45#include <IOKit/serial/IOSerialKeys.h>
46#include <IOKit/IOBSD.h>
48#if defined(MAC_OS_X_VERSION_10_3) && \
49 (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3)
50#include <IOKit/serial/ioss.h>
55#include <ApplicationServices/ApplicationServices.h>
64static kern_return_t FindSerialPorts(io_iterator_t* matchingServices) {
65 kern_return_t kernResult;
66 CFMutableDictionaryRef classesToMatch;
84 classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
85 if (classesToMatch == NULL) {
86 printf(
"IOServiceMatching returned a NULL dictionary.\n");
110 CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey),
111 CFSTR(kIOSerialBSDAllTypes));
143 kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault,
144 classesToMatch, matchingServices);
145 if (KERN_SUCCESS != kernResult) {
146 printf(
"IOServiceGetMatchingServices returned %d\n", kernResult);
157static int GetSerialPortPath(io_iterator_t serialPortIterator,
char** pNames,
158 int iMaxNames, CFIndex maxPathSize) {
159 io_object_t modemService;
161 Boolean modemFound =
false;
162 char bsdPath[maxPathSize];
163 int iCurrentNameIndex = 0;
169 while ((modemService = IOIteratorNext(serialPortIterator)) && !modemFound) {
170 CFTypeRef bsdPathAsCFString;
176 bsdPathAsCFString = IORegistryEntryCreateCFProperty(
177 modemService, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
178 if (bsdPathAsCFString) {
184 result = CFStringGetCString(bsdPathAsCFString, bsdPath, maxPathSize,
185 kCFStringEncodingUTF8);
186 CFRelease(bsdPathAsCFString);
189 pNames[iCurrentNameIndex] = calloc(1, strlen(bsdPath) + 1);
190 strncpy(pNames[iCurrentNameIndex], bsdPath, strlen(bsdPath) + 1);
195 (void)IOObjectRelease(modemService);
197 return iCurrentNameIndex;
200int FindSerialPortNames(
char** pNames,
int iMaxNames) {
201 int iActiveNameCount = 0;
202 kern_return_t kernResult;
204 io_iterator_t serialPortIterator;
206 kernResult = FindSerialPorts(&serialPortIterator);
209 GetSerialPortPath(serialPortIterator, pNames, iMaxNames, MAXPATHLEN);
211 IOObjectRelease(serialPortIterator);
212 return iActiveNameCount;
215bool ValidateSerialPortName(
char* pPortName,
int iMaxNamestoSearch) {
216 char* paPortNames[iMaxNamestoSearch];
219 bool bPortFound =
false;
220 char* pPortSubName = index(pPortName,
':');
222 if (0 == strcasecmp(pPortName,
"NONE"))
return true;
225 if (NULL == pPortSubName)
226 pPortSubName = pPortName;
230 memset(paPortNames, 0x00,
sizeof(paPortNames));
231 iPortNameCount = FindSerialPortNames(&paPortNames[0], iMaxNamestoSearch);
232 for (iPortIndex = 0; iPortIndex < iPortNameCount; iPortIndex++) {
234 int iStrCompresult = strcmp(paPortNames[iPortIndex], pPortSubName);
235 if (
false == bPortFound) bPortFound = (bool)(0 == iStrCompresult);
236 free(paPortNames[iPortIndex]);
244int GetMacMonitorSize() {
245 CGSize displayPhysicalSize = CGDisplayScreenSize(CGMainDisplayID());
246 return displayPhysicalSize.width;
254int ProcessIsTranslated() {
256 size_t size =
sizeof(ret);
257 if (sysctlbyname(
"sysctl.proc_translated", &ret, &size, NULL, 0) == -1) {
258 if (errno == ENOENT)
return 0;
264int IsAppleSilicon() {
266 size_t size =
sizeof(ret);
268 if (sysctlbyname(
"hw.optional.arm64", &ret, &size, NULL, 0) == -1) {