-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUSBNotifier.m
More file actions
216 lines (176 loc) · 6.34 KB
/
Copy pathUSBNotifier.m
File metadata and controls
216 lines (176 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
//
// USBNotifier.m
// Gawker
//
// Created by phil piwonka on 2/6/06.
// Copyright 2006 Phil Piwonka. All rights reserved.
//
#import "USBNotifier.h"
//
// Callback function declarations.
static void usbDeviceAdded(void *refCon, io_iterator_t iter);
static void usbDeviceRemoved(void *refCon, io_iterator_t iter);
@interface USBNotifier (Private)
//
// This function does the legwork of setting up
// receiving notifications on USB events
- (void)registerForUSBNotifications;
//
// This function should actually be implemented by the delegate
- (void)usbDeviceAdded:(io_iterator_t)iterator;
//
// This function should actually be implemented by the delegate
- (void)usbDeviceRemoved:(io_iterator_t)iterator;
//
// Returns a string for the USB object in question.
- (NSString *)nameForUSBObject:(io_object_t)thisObject;
@end
@implementation USBNotifier
//
// Init function, defaults to nil delegate
- (id)init
{
NSLog(@"USBNotifier -init should never be called");
return [self initWithDelegate:nil];
}
//
// Designated initializer. Assigns delegate and
// what device to watch for notifications on.
- (id)initWithDelegate:(id)newDelegate
{
if (self = [super init]) {
isInit = YES;
delegate = newDelegate;
watchForDisconnect = [[NSMutableArray array] retain];
[self registerForUSBNotifications];
}
return self;
}
- (void)dealloc
{
delegate = nil;
[watchForDisconnect release];
[super dealloc];
}
- (void)watchDeviceForDisconnect:(NSString *)device
{
NSLog(@"\"%@\" registered for disconnect notification", device);
[watchForDisconnect addObject:device];
}
@end
@implementation USBNotifier (Private)
- (void)registerForUSBNotifications
{
IONotificationPortRef ioKitNotificationPort =
IONotificationPortCreate(kIOMasterPortDefault);
CFRunLoopSourceRef notificationRunLoopSource =
IONotificationPortGetRunLoopSource(ioKitNotificationPort);
CFRunLoopAddSource(CFRunLoopGetCurrent(),
notificationRunLoopSource,
kCFRunLoopDefaultMode);
CFDictionaryRef myMatchDictionary =
IOServiceMatching("IOUSBDevice");
kern_return_t matchingResult;
io_iterator_t addedIter;
matchingResult = IOServiceAddMatchingNotification(ioKitNotificationPort,
kIOMatchedNotification,
myMatchDictionary,
usbDeviceAdded,
(void *)self,
&addedIter);
if (matchingResult != kIOReturnSuccess) {
NSLog(@"Matching notification registration failed: %d",
matchingResult);
}
else {
[self usbDeviceAdded:addedIter];
}
// Removal notification
myMatchDictionary = IOServiceMatching("IOUSBDevice");
kern_return_t removedResult;
io_iterator_t removedIter;
removedResult = IOServiceAddMatchingNotification(ioKitNotificationPort,
kIOTerminatedNotification,
myMatchDictionary,
usbDeviceRemoved,
self,
&removedIter);
if (removedResult != kIOReturnSuccess) {
NSLog(@"Couldn't add FW Device Removal Notification");
}
else {
[self usbDeviceRemoved:removedIter];
}
// This is done since we don't want to send add notifications
// the first time around.
isInit = NO;
}
- (void)usbDeviceAdded:(io_iterator_t)iterator
{
io_object_t thisObject = nil;
while ((thisObject = IOIteratorNext(iterator))) {
NSString *deviceName;
deviceName = [self nameForUSBObject:thisObject];
NSLog(@"USB Device Attached: %@", deviceName);
IOObjectRelease(thisObject);
}
if (!isInit &&
[delegate respondsToSelector:@selector(usbDeviceAdded:)]) {
[delegate usbDeviceAdded:self];
}
}
- (void)usbDeviceRemoved:(io_iterator_t)iterator
{
io_object_t thisObject = nil;
while ((thisObject = IOIteratorNext( iterator ))) {
NSString *deviceName;
deviceName = [self nameForUSBObject: thisObject];
NSLog(@"USB Device Removed: %@" , deviceName);
unsigned index = [watchForDisconnect indexOfObject:deviceName];
if (index != NSNotFound &&
[delegate respondsToSelector:@selector(usbDeviceRemoved:name:)]) {
[delegate usbDeviceRemoved:self name:deviceName];
}
IOObjectRelease(thisObject);
}
}
- (NSString *)nameForUSBObject:(io_object_t)thisObject
{
kern_return_t nameResult;
io_name_t deviceNameChars;
nameResult = IORegistryEntryGetName(thisObject,
deviceNameChars);
NSString *tempDeviceName = [NSString stringWithCString: deviceNameChars];
if (tempDeviceName &&
![tempDeviceName isEqualToString:@"IOUSBDevice"]) {
return tempDeviceName;
}
tempDeviceName =
(NSString *)IORegistryEntrySearchCFProperty(thisObject,
kIOUSBPlane,
(CFStringRef) @"USB Product Name",
nil,
kIORegistryIterateRecursively);
if (tempDeviceName) {
return tempDeviceName;
}
tempDeviceName =
(NSString *)IORegistryEntrySearchCFProperty(thisObject,
kIOUSBPlane,
(CFStringRef) @"USB Vendor Name",
nil,
kIORegistryIterateRecursively);
if (tempDeviceName) {
return tempDeviceName;
}
return @"Unnamed USB Device";
}
@end
static void usbDeviceAdded(void *refCon, io_iterator_t iter)
{
[(USBNotifier*)refCon usbDeviceAdded:iter];
}
static void usbDeviceRemoved(void *refCon, io_iterator_t iter)
{
[(USBNotifier*)refCon usbDeviceRemoved:iter];
}