|
@@ -0,0 +1,135 @@
|
|
1
|
+//
|
|
2
|
+// Serial.m
|
|
3
|
+// SerialGamepad
|
|
4
|
+//
|
|
5
|
+// For more informations refer to this document:
|
|
6
|
+// https://developer.apple.com/library/mac/documentation/DeviceDrivers/Conceptual/WorkingWSerial/WWSerial_SerialDevs/SerialDevices.html
|
|
7
|
+//
|
|
8
|
+// Created by Thomas Buck on 14.12.15.
|
|
9
|
+// Copyright © 2015 xythobuz. All rights reserved.
|
|
10
|
+//
|
|
11
|
+
|
|
12
|
+#import <IOKit/serial/IOSerialKeys.h>
|
|
13
|
+
|
|
14
|
+#import "Serial.h"
|
|
15
|
+
|
|
16
|
+@implementation Serial
|
|
17
|
+
|
|
18
|
++ (NSArray *)listSerialPorts {
|
|
19
|
+ // Get Iterator with all serial ports
|
|
20
|
+ io_iterator_t serialPortIterator;
|
|
21
|
+ kern_return_t kernResult = findSerialPorts(&serialPortIterator);
|
|
22
|
+
|
|
23
|
+ // Create 2D array
|
|
24
|
+ char **portList;
|
|
25
|
+ portList = malloc(100 * sizeof(char *));
|
|
26
|
+ for (int i = 0; i < 100; i++) portList[i] = malloc(200 * sizeof(char));
|
|
27
|
+
|
|
28
|
+ // Copy device name into C-String array
|
|
29
|
+ kernResult = getSerialPortPath(serialPortIterator, portList, 100, 200);
|
|
30
|
+ IOObjectRelease(serialPortIterator);
|
|
31
|
+
|
|
32
|
+ // Copy contents into NSString Array
|
|
33
|
+ NSString *stringList[100];
|
|
34
|
+ NSUInteger realCount = 0;
|
|
35
|
+ while (portList[realCount] != NULL) {
|
|
36
|
+ stringList[realCount] = [NSString stringWithCString:portList[realCount] encoding:NSUTF8StringEncoding];
|
|
37
|
+ realCount++;
|
|
38
|
+ }
|
|
39
|
+
|
|
40
|
+ // Destroy 2D array
|
|
41
|
+ for (int i = 0; i < 100; i++) free(portList[i]);
|
|
42
|
+ free(portList);
|
|
43
|
+
|
|
44
|
+ // And return them as NSArray
|
|
45
|
+ return [[NSArray alloc] initWithObjects:stringList count:realCount];
|
|
46
|
+}
|
|
47
|
+
|
|
48
|
+@end
|
|
49
|
+
|
|
50
|
+kern_return_t findSerialPorts(io_iterator_t *matches) {
|
|
51
|
+ kern_return_t kernResult;
|
|
52
|
+ mach_port_t masterPort;
|
|
53
|
+ CFMutableDictionaryRef classesToMatch;
|
|
54
|
+
|
|
55
|
+ kernResult = IOMasterPort(MACH_PORT_NULL, &masterPort);
|
|
56
|
+ if (KERN_SUCCESS != kernResult) {
|
|
57
|
+ NSLog(@"IOMasterPort returned %d\n", kernResult);
|
|
58
|
+ return kernResult;
|
|
59
|
+ }
|
|
60
|
+
|
|
61
|
+ // Serial devices are instances of class IOSerialBSDClient.
|
|
62
|
+ classesToMatch = IOServiceMatching(kIOSerialBSDServiceValue);
|
|
63
|
+ if (classesToMatch == NULL) {
|
|
64
|
+ NSLog(@"IOServiceMatching returned a NULL dictionary.\n");
|
|
65
|
+ } else {
|
|
66
|
+ CFDictionarySetValue(classesToMatch,
|
|
67
|
+ CFSTR(kIOSerialBSDTypeKey),
|
|
68
|
+ CFSTR(kIOSerialBSDRS232Type));
|
|
69
|
+
|
|
70
|
+ // Each serial device object has a property with key
|
|
71
|
+ // kIOSerialBSDTypeKey and a value that is one of
|
|
72
|
+ // kIOSerialBSDAllTypes, kIOSerialBSDModemType,
|
|
73
|
+ // or kIOSerialBSDRS232Type. You can change the
|
|
74
|
+ // matching dictionary to find other types of serial
|
|
75
|
+ // devices by changing the last parameter in the above call
|
|
76
|
+ // to CFDictionarySetValue.
|
|
77
|
+ }
|
|
78
|
+
|
|
79
|
+ kernResult = IOServiceGetMatchingServices(masterPort, classesToMatch, matches);
|
|
80
|
+ if (KERN_SUCCESS != kernResult) {
|
|
81
|
+ NSLog(@"IOServiceGetMatchingServices returned %d\n", kernResult);
|
|
82
|
+ return kernResult;
|
|
83
|
+ }
|
|
84
|
+
|
|
85
|
+ return kernResult;
|
|
86
|
+}
|
|
87
|
+
|
|
88
|
+kern_return_t getSerialPortPath(io_iterator_t serialPortIterator, char **deviceFilePath, CFIndex maxPathCount, CFIndex maxPathSize) {
|
|
89
|
+ io_object_t modemService;
|
|
90
|
+ kern_return_t kernResult = KERN_FAILURE;
|
|
91
|
+ CFIndex i = 0;
|
|
92
|
+
|
|
93
|
+ while ((modemService = IOIteratorNext(serialPortIterator)) && (i < (maxPathCount - 1))) {
|
|
94
|
+ CFTypeRef deviceFilePathAsCFString;
|
|
95
|
+
|
|
96
|
+ // Get the callout device's path (/dev/cu.xxxxx).
|
|
97
|
+ // The callout device should almost always be
|
|
98
|
+ // used. You would use the dialin device (/dev/tty.xxxxx) when
|
|
99
|
+ // monitoring a serial port for
|
|
100
|
+ // incoming calls, for example, a fax listener.
|
|
101
|
+
|
|
102
|
+ deviceFilePathAsCFString = IORegistryEntryCreateCFProperty(modemService,
|
|
103
|
+ CFSTR(kIODialinDeviceKey),
|
|
104
|
+ kCFAllocatorDefault,
|
|
105
|
+ 0);
|
|
106
|
+ if (deviceFilePathAsCFString) {
|
|
107
|
+ Boolean result;
|
|
108
|
+
|
|
109
|
+ deviceFilePath[i][0] = '\0';
|
|
110
|
+
|
|
111
|
+ // Convert the path from a CFString to a NULL-terminated C string
|
|
112
|
+ // for use with the POSIX open() call.
|
|
113
|
+
|
|
114
|
+ result = CFStringGetCString(deviceFilePathAsCFString,
|
|
115
|
+ deviceFilePath[i],
|
|
116
|
+ maxPathSize,
|
|
117
|
+ kCFStringEncodingASCII);
|
|
118
|
+ CFRelease(deviceFilePathAsCFString);
|
|
119
|
+
|
|
120
|
+ if (result) {
|
|
121
|
+ NSLog(@"BSD path: %s\n", deviceFilePath[i]);
|
|
122
|
+ i++;
|
|
123
|
+ kernResult = KERN_SUCCESS;
|
|
124
|
+ }
|
|
125
|
+ }
|
|
126
|
+
|
|
127
|
+ // Release the io_service_t now that we are done with it.
|
|
128
|
+
|
|
129
|
+ (void) IOObjectRelease(modemService);
|
|
130
|
+ }
|
|
131
|
+
|
|
132
|
+ deviceFilePath[i] = NULL;
|
|
133
|
+
|
|
134
|
+ return kernResult;
|
|
135
|
+}
|