35#include <sys/select.h>
39#include <AvailabilityMacros.h>
41#include <CoreFoundation/CoreFoundation.h>
43#include <IOKit/IOKitLib.h>
44#include <IOKit/serial/IOSerialKeys.h>
45#if defined(MAC_OS_X_VERSION_10_3) && \
46 (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_3)
47#include <IOKit/serial/ioss.h>
49#include <IOKit/IOBSD.h>
53#include <ApplicationServices/ApplicationServices.h>
62static kern_return_t FindSerialPorts(io_iterator_t* matchingServices) {
63 kern_return_t kernResult;
64 CFMutableDictionaryRef classesToMatch;
82 classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
83 if (classesToMatch == NULL) {
84 printf(
"IOServiceMatching returned a NULL dictionary.\n");
108 CFDictionarySetValue(classesToMatch, CFSTR(kIOSerialBSDTypeKey),
109 CFSTR(kIOSerialBSDAllTypes));
141 kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault,
142 classesToMatch, matchingServices);
143 if (KERN_SUCCESS != kernResult) {
144 printf(
"IOServiceGetMatchingServices returned %d\n", kernResult);
155static int GetSerialPortPath(io_iterator_t serialPortIterator,
char** pNames,
156 int iMaxNames, CFIndex maxPathSize) {
157 io_object_t modemService;
159 Boolean modemFound =
false;
160 char bsdPath[maxPathSize];
161 int iCurrentNameIndex = 0;
167 while ((modemService = IOIteratorNext(serialPortIterator)) && !modemFound) {
168 CFTypeRef bsdPathAsCFString;
174 bsdPathAsCFString = IORegistryEntryCreateCFProperty(
175 modemService, CFSTR(kIOCalloutDeviceKey), kCFAllocatorDefault, 0);
176 if (bsdPathAsCFString) {
182 result = CFStringGetCString(bsdPathAsCFString, bsdPath, maxPathSize,
183 kCFStringEncodingUTF8);
184 CFRelease(bsdPathAsCFString);
187 pNames[iCurrentNameIndex] = calloc(1, strlen(bsdPath) + 1);
188 strncpy(pNames[iCurrentNameIndex], bsdPath, strlen(bsdPath) + 1);
193 (void)IOObjectRelease(modemService);
195 return iCurrentNameIndex;
198int FindSerialPortNames(
char** pNames,
int iMaxNames) {
199 int iActiveNameCount = 0;
200 kern_return_t kernResult;
202 io_iterator_t serialPortIterator;
204 kernResult = FindSerialPorts(&serialPortIterator);
207 GetSerialPortPath(serialPortIterator, pNames, iMaxNames, MAXPATHLEN);
209 IOObjectRelease(serialPortIterator);
210 return iActiveNameCount;
213bool ValidateSerialPortName(
char* pPortName,
int iMaxNamestoSearch) {
214 char* paPortNames[iMaxNamestoSearch];
217 bool bPortFound =
false;
218 char* pPortSubName = index(pPortName,
':');
220 if (0 == strcasecmp(pPortName,
"NONE"))
return true;
223 if (NULL == pPortSubName)
224 pPortSubName = pPortName;
228 memset(paPortNames, 0x00,
sizeof(paPortNames));
229 iPortNameCount = FindSerialPortNames(&paPortNames[0], iMaxNamestoSearch);
230 for (iPortIndex = 0; iPortIndex < iPortNameCount; iPortIndex++) {
232 int iStrCompresult = strcmp(paPortNames[iPortIndex], pPortSubName);
233 if (
false == bPortFound) bPortFound = (bool)(0 == iStrCompresult);
234 free(paPortNames[iPortIndex]);
242int GetMacMonitorSize() {
243 CGSize displayPhysicalSize = CGDisplayScreenSize(CGMainDisplayID());
244 return displayPhysicalSize.width;