/
WMR100NDeviceController.m
248 lines (196 loc) · 8.52 KB
/
WMR100NDeviceController.m
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//
// WMR100NDeviceController.m
// USBWeatherStationReader
//
// Created by Per Ejeklint on 2009-05-13.
// Copyright 2009 Heimore Group AB. All rights reserved.
//
// Code for proper USB report handling by George Warner, Apple
//
#import "WMR100NDeviceController.h"
#import "AppDelegate.h"
#import "DataKeys.h"
static Boolean IOHIDDevice_GetLongProperty(IOHIDDeviceRef inIOHIDDeviceRef, CFStringRef inKey, long *outValue) {
Boolean result = FALSE;
if ( inIOHIDDeviceRef ) {
assert( IOHIDDeviceGetTypeID() == CFGetTypeID(inIOHIDDeviceRef) );
CFTypeRef tCFTypeRef = IOHIDDeviceGetProperty(inIOHIDDeviceRef, inKey);
if ( tCFTypeRef ) {
// if this is a number
if ( CFNumberGetTypeID() == CFGetTypeID(tCFTypeRef) ) {
// get it's value
result = CFNumberGetValue( (CFNumberRef) tCFTypeRef, kCFNumberSInt32Type, outValue );
}
}
}
return (result);
}
//
// Static callbacks from HID Manager
//
static void Handle_DeviceRemovalCallback(void * inContext, IOReturn inResult, void * inSender, IOHIDDeviceRef inIOHIDDeviceRef)
{
if (inResult != kIOReturnSuccess) {
fprintf(stderr, "%s( context: %p, result: %p, sender: %p ).\n",
__PRETTY_FUNCTION__, inContext, ( void * ) inResult, inSender);
return;
}
WMR100NDeviceController *self = (WMR100NDeviceController *) inContext;
[self closeAndReleaseDevice: inIOHIDDeviceRef];
}
static void Handle_DeviceMatchingCallback(void * inContext, // context from IOHIDManagerRegisterDeviceMatchingCallback
IOReturn inResult, // the result of the matching operation
void * inSender, // the IOHIDManagerRef for the new device
IOHIDDeviceRef inIOHIDDeviceRef) { // the new HID device
WMR100NDeviceController *controller = (WMR100NDeviceController *) inContext;
[controller openAndInitDevice:inResult sender:inSender device:inIOHIDDeviceRef];
}
static void Handle_IOHIDDeviceInputReportCallback(void * inContext, // context from IOHIDDeviceRegisterInputReportCallback
IOReturn inResult, // completion result for the input report operation
void * inSender, // IOHIDDeviceRef of the device this report is from
IOHIDReportType inType, // the report type
uint32_t inReportID, // the report ID
uint8_t * inReport, // pointer to the report data
CFIndex inReportLength) // the actual size of the input report
{
WMR100NDeviceController *self = (WMR100NDeviceController *) inContext;
[self inputReport:inResult sender:inSender type:inType reportID:inReportID report:inReport length:inReportLength];
}
@implementation WMR100NDeviceController
- (BOOL) validateChecksum: (NSData*) reading {
unsigned sum = 0;
int i = 0;
UInt8 *data = (UInt8*) [reading bytes];
unsigned last = (unsigned) [reading length] - 2;
for (i = 0; i < last; i++)
sum += data[i];
if (sum != (data[[reading length]-1] * 256 + data[[reading length]-2])) {
return NO;
} else {
return YES;
}
}
- (void) postReadingAndPrepareForNew: (NSData*) reading {
if ([reading length] < 2) {
return;
}
int expectedReadingLength = 0;
uint8_t *bytes = (uint8_t*) [reading bytes];
switch (bytes[1]) {
case 0x41: // Rain has varying expected lengths
expectedReadingLength = 17;
break;
case 0x42: // Temp & humidity
expectedReadingLength = 12;
break;
case 0x46: // Pressure
expectedReadingLength = 8;
break;
case 0x47: // UV reader
expectedReadingLength = 6;
break;
case 0x48: // Wind
expectedReadingLength = 11;
break;
case 0x60: // Date and time
expectedReadingLength = 12;
break;
}
if ([reading length] != expectedReadingLength || [self validateChecksum:reading] == NO) {
if (DEBUGALOT)
NSLog(@"Discarding reading with wrong length or wrong checksum: %@", reading);
return;
}
// Toss the completed reading up in the nutritional chain
NSData *theData = [NSData dataWithData:reading];
NSDictionary *userInfo = [NSDictionary dictionaryWithObject:theData forKey:@"data"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"DataEvent" object:self userInfo:userInfo];
}
-(void) inputReport:(IOReturn) inResult sender:(void *) inSender type:(IOHIDReportType) inType reportID:(uint32_t) inReportID
report:(uint8_t *) inReport length:(CFIndex) inReportLength {
unsigned int noOfValidBytes = inReport[0]; // Get number of bytes that matters in this report
[buffer appendBytes:&inReport[1] length:noOfValidBytes];
// Find 0xffff - all before that is a reading
int len = (int) [buffer length];
if (len > 1) {
for (int i = 0; i < len - 1; i++) {
uint8_t *bytes = (uint8_t*) [buffer bytes];
if (bytes[i] == 0xff && bytes[i+1] == 0xff) {
NSData *new = [NSData dataWithBytes:[buffer bytes] length:i];
[self postReadingAndPrepareForNew: new];
// Patch from kglueck. Thanks!
if ((i + 2) < len) { // if data exists after the 0xFF 0xFF marker
char *bufferData=[buffer mutableBytes];
memmove(bufferData,(bufferData + i + 2),(len - i - 2));
}
[buffer setLength:(len - i - 2)];
break;
}
}
}
}
- (void) closeAndReleaseDevice: (IOHIDDeviceRef) hidDeviceRef {
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeviceRemoved" object:self userInfo:nil];
}
-(void) openAndInitDevice:(IOReturn) inResult sender:(void *) inSender device:(IOHIDDeviceRef) inIOHIDDeviceRef {
gHidDeviceRef= inIOHIDDeviceRef;
long reportSize = 0;
(void) IOHIDDevice_GetLongProperty(inIOHIDDeviceRef, CFSTR(kIOHIDMaxInputReportSizeKey), &reportSize);
if (reportSize) {
report = calloc(1, reportSize);
if (report) {
IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, // IOHIDDeviceRef for the HID device
report, // pointer to the report data (uint8_t's)
reportSize, // number of bytes in the report (CFIndex)
Handle_IOHIDDeviceInputReportCallback, // the callback routine
self); // context passed to callback
uint8_t triggerReport[] = {0x20, 0x00, 0x08, 0x01, 0x00, 0x00, 0x00, 0x00};
CFIndex reportLength = sizeof(triggerReport);
// synchronous
IOReturn ioReturn = IOHIDDeviceSetReport(inIOHIDDeviceRef, // IOHIDDeviceRef for the HID device
kIOHIDReportTypeOutput, // IOHIDReportType for the report
0, // CFIndex for the report ID
triggerReport, // address of report buffer
reportLength); // length of the report
if (kIOReturnSuccess != ioReturn)
NSLog(@"%s, IOHIDDeviceSetReport error: %ld (0x%08lX)", __PRETTY_FUNCTION__, ioReturn, ioReturn);
}
}
[[NSNotificationCenter defaultCenter] postNotificationName:@"DeviceAdded" object:self userInfo:nil];
}
- (void) setupHidManagerAndCallbacks {
gHIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
if (gHIDManager) {
NSDictionary * matchDict = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithInt:0x0FDE], @kIOHIDVendorIDKey,
[NSNumber numberWithInt:0xCA01], @kIOHIDProductIDKey,
nil];
IOHIDManagerSetDeviceMatching(gHIDManager, (CFDictionaryRef) matchDict);
// Callbacks for device plugin/removal
IOHIDManagerRegisterDeviceMatchingCallback(gHIDManager, Handle_DeviceMatchingCallback, self);
IOHIDManagerRegisterDeviceRemovalCallback(gHIDManager, Handle_DeviceRemovalCallback, self);
// Schedule with the run loop
IOHIDManagerScheduleWithRunLoop(gHIDManager, CFRunLoopGetCurrent( ), kCFRunLoopDefaultMode);
IOReturn ioRet = IOHIDManagerOpen(gHIDManager, kIOHIDOptionsTypeNone);
if (ioRet != kIOReturnSuccess) {
CFRelease(gHIDManager);
gHIDManager = NULL;
NSLog(@"Failed to open the HID Manager");
}
}
}
- (id)init {
if (!(self = [super init]))
return nil;
buffer = [[NSMutableData alloc] initWithCapacity:20];
[self setupHidManagerAndCallbacks];
return self;
}
- (void)dealloc {
if (gHIDManager) {
CFRelease(gHIDManager); // Should release our manager
gHIDManager = NULL;
}
[super dealloc];
}
@end