forked from teamon/SumoDebugger
-
Notifications
You must be signed in to change notification settings - Fork 1
/
AMSerialPortAdditions.m
executable file
·612 lines (541 loc) · 18.9 KB
/
AMSerialPortAdditions.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
//
// AMSerialPortAdditions.m
//
// Created by Andreas on Thu May 02 2002.
// Copyright (c) 2001 Andreas Mayer. All rights reserved.
//
// 2002-07-02 Andreas Mayer
// - initialize buffer in readString
// 2002-10-04 Andreas Mayer
// - readDataInBackgroundWithTarget:selector: and writeDataInBackground: added
// 2002-10-10 Andreas Mayer
// - stopWriteInBackground added
// - send notifications about sent data through distributed notification center
// 2002-10-17 Andreas Mayer
// - numberOfWriteInBackgroundThreads added
// - if total write time will exceed 3 seconds, send
// CommXWriteInBackgroundProgressNotification without delay
// 2002-10-25 Andreas Mayer
// - readDataInBackground and stopReadInBackground added
// 2004-08-18 Andreas Mayer
// - readStringOfLength: added (suggested by Michael Beck)
// 2005-04-11 Andreas Mayer
// - attempt at a fix for readDataInBackgroundThread - fileDescriptor could already be closed
// (thanks to David Bainbridge for the bug report) does not work as of yet
// 2007-10-26 Sean McBride
// - made code 64 bit and garbage collection clean
#import "AMSDKCompatibility.h"
#import <sys/ioctl.h>
#import <sys/filio.h>
#import "AMSerialPortAdditions.h"
#import "AMSerialErrors.h"
@interface AMSerialPort (AMSerialPortAdditionsPrivate)
- (void)readDataInBackgroundThread;
- (void)writeDataInBackgroundThread:(NSData *)data;
- (id)am_readTarget;
- (void)am_setReadTarget:(id)newReadTarget;
- (NSData *)readAndStopAfterBytes:(BOOL)stopAfterBytes bytes:(NSUInteger)bytes stopAtChar:(BOOL)stopAtChar stopChar:(char)stopChar error:(NSError **)error;
- (void)reportProgress:(NSUInteger)progress dataLen:(NSUInteger)dataLen;
@end
@implementation AMSerialPort (AMSerialPortAdditions)
// ============================================================
#pragma mark -
#pragma mark blocking IO
// ============================================================
- (void)doRead:(NSTimer *)timer
{
(void)timer;
#ifdef AMSerialDebug
NSLog(@"doRead");
#endif
int res;
struct timeval timeout;
if (fileDescriptor >= 0) {
FD_ZERO(readfds);
FD_SET(fileDescriptor, readfds);
[self readTimeoutAsTimeval:&timeout];
res = select(fileDescriptor+1, readfds, nil, nil, &timeout);
if (res >= 1) {
NSString *readStr = [self readStringUsingEncoding:NSUTF8StringEncoding error:NULL];
[[self am_readTarget] performSelector:am_readSelector withObject:readStr];
[self am_setReadTarget:nil];
} else {
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(doRead:) userInfo:self repeats:NO];
}
} else {
// file already closed
[self am_setReadTarget:nil];
}
}
// all blocking reads returns after [self readTimout] seconds elapse, at the latest
- (NSData *)readAndReturnError:(NSError **)error
{
NSData *result = [self readAndStopAfterBytes:NO bytes:0 stopAtChar:NO stopChar:0 error:error];
return result;
}
// returns after 'bytes' bytes are read
- (NSData *)readBytes:(NSUInteger)bytes error:(NSError **)error
{
NSData *result = [self readAndStopAfterBytes:YES bytes:bytes stopAtChar:NO stopChar:0 error:error];
return result;
}
// returns when 'stopChar' is encountered
- (NSData *)readUpToChar:(char)stopChar error:(NSError **)error
{
NSData *result = [self readAndStopAfterBytes:NO bytes:0 stopAtChar:YES stopChar:stopChar error:error];
return result;
}
// returns after 'bytes' bytes are read or if 'stopChar' is encountered, whatever comes first
- (NSData *)readBytes:(NSUInteger)bytes upToChar:(char)stopChar error:(NSError **)error
{
NSData *result = [self readAndStopAfterBytes:YES bytes:bytes stopAtChar:YES stopChar:stopChar error:error];
return result;
}
// data read will be converted into an NSString, using the given encoding
// NOTE: encodings that take up more than one byte per character may fail if only a part of the final string was received
- (NSString *)readStringUsingEncoding:(NSStringEncoding)encoding error:(NSError **)error
{
NSString *result = nil;
NSData *data = [self readAndStopAfterBytes:NO bytes:0 stopAtChar:NO stopChar:0 error:error];
if (data) {
result = [[[NSString alloc] initWithData:data encoding:encoding] autorelease];
}
return result;
}
- (NSString *)readBytes:(NSUInteger)bytes usingEncoding:(NSStringEncoding)encoding error:(NSError **)error
{
NSString *result = nil;
NSData *data = [self readAndStopAfterBytes:YES bytes:bytes stopAtChar:NO stopChar:0 error:error];
if (data) {
result = [[[NSString alloc] initWithData:data encoding:encoding] autorelease];
}
return result;
}
// NOTE: 'stopChar' has to be a byte value, using the given encoding; you can not wait for an arbitrary character from a multi-byte encoding
- (NSString *)readUpToChar:(char)stopChar usingEncoding:(NSStringEncoding)encoding error:(NSError **)error
{
NSString *result = nil;
NSData *data = [self readAndStopAfterBytes:NO bytes:0 stopAtChar:YES stopChar:stopChar error:error];
if (data) {
result = [[[NSString alloc] initWithData:data encoding:encoding] autorelease];
}
return result;
}
- (NSString *)readBytes:(NSUInteger)bytes upToChar:(char)stopChar usingEncoding:(NSStringEncoding)encoding error:(NSError **)error
{
NSString *result = nil;
NSData *data = [self readAndStopAfterBytes:YES bytes:bytes stopAtChar:YES stopChar:stopChar error:error];
if (data) {
result = [[[NSString alloc] initWithData:data encoding:encoding] autorelease];
}
return result;
}
// write to the serial port; NO if an error occured
- (BOOL)writeData:(NSData *)data error:(NSError **)error
{
BOOL result = NO;
const char *dataBytes = (const char*)[data bytes];
NSUInteger dataLen = [data length];
ssize_t bytesWritten = 0;
int errorCode = kAMSerialErrorNone;
if (dataBytes && (dataLen > 0)) {
bytesWritten = write(fileDescriptor, dataBytes, dataLen);
if (bytesWritten < 0) {
errorCode = kAMSerialErrorFatal;
} else if ((NSUInteger)bytesWritten == dataLen) {
result = YES;
} else {
errorCode = kAMSerialErrorOnlySomeDataWritten;
}
} else {
errorCode = kAMSerialErrorNoDataToWrite;
}
if (error) {
NSDictionary *userInfo = nil;
if (bytesWritten > 0) {
NSNumber* bytesWrittenNum = [NSNumber numberWithUnsignedLongLong:bytesWritten];
userInfo = [NSDictionary dictionaryWithObject:bytesWrittenNum forKey:@"bytesWritten"];
}
*error = [NSError errorWithDomain:AMSerialErrorDomain code:errorCode userInfo:userInfo];
}
return result;
}
- (BOOL)writeString:(NSString *)string usingEncoding:(NSStringEncoding)encoding error:(NSError **)error
{
NSData *data = [string dataUsingEncoding:encoding];
return [self writeData:data error:error];
}
- (int)bytesAvailable
{
#ifdef AMSerialDebug
NSLog(@"bytesAvailable");
#endif
// yes, that cast is correct. ioctl() is declared to take a char* but should be void* as really it
// depends on the 2nd parameter. Ahhh, I love crappy old UNIX APIs :)
int result = 0;
int err = ioctl(fileDescriptor, FIONREAD, (char *)&result);
if (err != 0) {
result = -1;
}
return result;
}
- (void)waitForInput:(id)target selector:(SEL)selector
{
#ifdef AMSerialDebug
NSLog(@"waitForInput");
#endif
[self am_setReadTarget:target];
am_readSelector = selector;
[NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(doRead:) userInfo:self repeats:NO];
}
// ============================================================
#pragma mark -
#pragma mark threaded IO
// ============================================================
- (void)readDataInBackground
{
#ifdef AMSerialDebug
NSLog(@"readDataInBackground");
#endif
if (delegateHandlesReadInBackground) {
countReadInBackgroundThreads++;
[NSThread detachNewThreadSelector:@selector(readDataInBackgroundThread) toTarget:self withObject:nil];
} else {
// ... throw exception?
}
}
- (void)stopReadInBackground
{
#ifdef AMSerialDebug
NSLog(@"stopReadInBackground");
#endif
stopReadInBackground = YES;
}
- (void)writeDataInBackground:(NSData *)data
{
#ifdef AMSerialDebug
NSLog(@"writeDataInBackground");
#endif
if (delegateHandlesWriteInBackground) {
countWriteInBackgroundThreads++;
[NSThread detachNewThreadSelector:@selector(writeDataInBackgroundThread:) toTarget:self withObject:data];
} else {
// ... throw exception?
}
}
- (void)stopWriteInBackground
{
#ifdef AMSerialDebug
NSLog(@"stopWriteInBackground");
#endif
stopWriteInBackground = YES;
}
- (int)numberOfWriteInBackgroundThreads
{
return countWriteInBackgroundThreads;
}
@end
#pragma mark -
@implementation AMSerialPort (AMSerialPortAdditionsPrivate)
// ============================================================
#pragma mark -
#pragma mark threaded methods
// ============================================================
- (void)readDataInBackgroundThread
{
NSData *data = nil;
void *localBuffer;
ssize_t bytesRead = 0;
fd_set *localReadFDs = NULL;
[readLock lock]; // read in sequence
//NSLog(@"readDataInBackgroundThread - [readLock lock]");
localBuffer = malloc(AMSER_MAXBUFSIZE);
stopReadInBackground = NO;
NSAutoreleasePool *localAutoreleasePool = [[NSAutoreleasePool alloc] init];
[closeLock lock];
if ((fileDescriptor >= 0) && (!stopReadInBackground)) {
//NSLog(@"readDataInBackgroundThread - [closeLock lock]");
localReadFDs = (fd_set*)malloc(sizeof(fd_set));
FD_ZERO(localReadFDs);
FD_SET(fileDescriptor, localReadFDs);
[closeLock unlock];
//NSLog(@"readDataInBackgroundThread - [closeLock unlock]");
int res = select(fileDescriptor+1, localReadFDs, nil, nil, nil); // timeout);
if ((res >= 1) && (fileDescriptor >= 0)) {
//bytesRead = read(fileDescriptor, localBuffer, AMSER_MAXBUFSIZE);
}
//FIXME: This was modified on 3-20-09 by Pat O'Keefe for a particular application...swap comments for normal operation
//data = [NSData dataWithBytes:localBuffer length:bytesRead];
data = [self readUpToChar:'\n' error:NULL];
[delegate performSelectorOnMainThread:@selector(serialPortReadData:) withObject:[NSDictionary dictionaryWithObjectsAndKeys: self, @"serialPort", data, @"data", nil] waitUntilDone:YES];//was NO
} else {
[closeLock unlock];
}
[localAutoreleasePool release];
if (localReadFDs)
free(localReadFDs);
if (localBuffer)
free(localBuffer);
countReadInBackgroundThreads--;
[readLock unlock];
//NSLog(@"readDataInBackgroundThread - [readLock unlock]");
}
/* new version - does not work yet
- (void)readDataInBackgroundThread
{
NSData *data = nil;
void *localBuffer;
int bytesRead = 0;
fd_set *localReadFDs;
#ifdef AMSerialDebug
NSLog(@"readDataInBackgroundThread: %@", [NSThread currentThread]);
#endif
localBuffer = malloc(AMSER_MAXBUFSIZE);
[stopReadInBackgroundLock lock];
stopReadInBackground = NO;
//NSLog(@"stopReadInBackground set to NO: %@", [NSThread currentThread]);
[stopReadInBackgroundLock unlock];
//NSLog(@"attempt readLock: %@", [NSThread currentThread]);
[readLock lock]; // write in sequence
//NSLog(@"readLock locked: %@", [NSThread currentThread]);
//NSLog(@"attempt closeLock: %@", [NSThread currentThread]);
[closeLock lock];
//NSLog(@"closeLock locked: %@", [NSThread currentThread]);
if (!stopReadInBackground && (fileDescriptor >= 0)) {
NSAutoreleasePool *localAutoreleasePool = [[NSAutoreleasePool alloc] init];
localReadFDs = malloc(sizeof(*localReadFDs));
FD_ZERO(localReadFDs);
FD_SET(fileDescriptor, localReadFDs);
int res = select(fileDescriptor+1, localReadFDs, nil, nil, nil); // timeout);
if (res >= 1) {
#ifdef AMSerialDebug
NSLog(@"attempt read: %@", [NSThread currentThread]);
#endif
bytesRead = read(fileDescriptor, localBuffer, AMSER_MAXBUFSIZE);
}
#ifdef AMSerialDebug
NSLog(@"data read: %@", [NSThread currentThread]);
#endif
data = [NSData dataWithBytes:localBuffer length:bytesRead];
#ifdef AMSerialDebug
NSLog(@"send AMSerialReadInBackgroundDataMessage");
#endif
[delegate performSelectorOnMainThread:@selector(serialPortReadData:) withObject:[NSDictionary dictionaryWithObjectsAndKeys: self, @"serialPort", data, @"data", nil] waitUntilDone:NO];
free(localReadFDs);
[localAutoreleasePool release];
} else {
#ifdef AMSerialDebug
NSLog(@"read stopped: %@", [NSThread currentThread]);
#endif
}
[closeLock unlock];
//NSLog(@"closeLock unlocked: %@", [NSThread currentThread]);
[readLock unlock];
//NSLog(@"readLock unlocked: %@", [NSThread currentThread]);
[countReadInBackgroundThreadsLock lock];
countReadInBackgroundThreads--;
[countReadInBackgroundThreadsLock unlock];
free(localBuffer);
}
*/
- (void)writeDataInBackgroundThread:(NSData *)data
{
#ifdef AMSerialDebug
NSLog(@"writeDataInBackgroundThread");
#endif
void *localBuffer;
NSUInteger pos;
NSUInteger bufferLen;
NSUInteger dataLen;
ssize_t written;
NSDate *nextNotificationDate;
BOOL notificationSent = NO;
long speed;
long estimatedTime;
BOOL error = NO;
NSAutoreleasePool *localAutoreleasePool = [[NSAutoreleasePool alloc] init];
[data retain];
localBuffer = malloc(AMSER_MAXBUFSIZE);
stopWriteInBackground = NO;
[writeLock lock]; // write in sequence
pos = 0;
dataLen = [data length];
speed = [self speed];
estimatedTime = (dataLen*8)/speed;
if (estimatedTime > 3) { // will take more than 3 seconds
notificationSent = YES;
[self reportProgress:pos dataLen:dataLen];
nextNotificationDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
} else {
nextNotificationDate = [NSDate dateWithTimeIntervalSinceNow:2.0];
}
while (!stopWriteInBackground && (pos < dataLen) && !error) {
bufferLen = MIN(AMSER_MAXBUFSIZE, dataLen-pos);
[data getBytes:localBuffer range:NSMakeRange(pos, bufferLen)];
written = write(fileDescriptor, localBuffer, bufferLen);
error = (written == 0); // error condition
if (error)
break;
pos += written;
if ([(NSDate *)[NSDate date] compare:nextNotificationDate] == NSOrderedDescending) {
if (notificationSent || (pos < dataLen)) { // not for last block only
notificationSent = YES;
[self reportProgress:pos dataLen:dataLen];
nextNotificationDate = [NSDate dateWithTimeIntervalSinceNow:1.0];
}
}
}
if (notificationSent) {
[self reportProgress:pos dataLen:dataLen];
}
stopWriteInBackground = NO;
[writeLock unlock];
countWriteInBackgroundThreads--;
free(localBuffer);
[data release];
[localAutoreleasePool release];
}
- (id)am_readTarget
{
return am_readTarget;
}
- (void)am_setReadTarget:(id)newReadTarget
{
if (am_readTarget != newReadTarget) {
[newReadTarget retain];
[am_readTarget release];
am_readTarget = newReadTarget;
}
}
// Low-level blocking read method.
// This method reads from the serial port and blocks as necessary, it returns when:
// - [self readTimeout] seconds has elapsed
// - if stopAfterBytes is YES, when 'bytesToRead' bytes have been read
// - if stopAtChar is YES, when 'stopChar' is found at the end of the read buffer
// - a fatal error occurs
//
// Upon return: as long as some data was actually read, and no serious error occured, an autoreleased NSData
// object with that data is created and returned, otherwise nil is.
- (NSData *)readAndStopAfterBytes:(BOOL)stopAfterBytes bytes:(NSUInteger)bytesToRead stopAtChar:(BOOL)stopAtChar stopChar:(char)stopChar error:(NSError **)error
{
NSData *result = nil;
struct timeval timeout;
NSUInteger bytesRead = 0;
BOOL stop = NO;
int errorCode = kAMSerialErrorNone;
int endCode = kAMSerialEndOfStream;
NSError *underlyingError = nil;
// Note the time that we start
NSDate *startTime = [NSDate date];
// How long, in total, do we block before timing out?
NSTimeInterval totalTimeout = [self readTimeout];
// This value will be decreased each time through the loop
NSTimeInterval remainingTimeout = totalTimeout;
while (!stop) {
if (remainingTimeout <= 0.0) {
stop = YES;
errorCode = kAMSerialErrorTimeout;
break;
} else {
// Convert from NSTimeInterval to struct timeval
double numSecs = trunc(remainingTimeout);
double numUSecs = (remainingTimeout-numSecs)*1000000.0;
timeout.tv_sec = (time_t)lrint(numSecs);
timeout.tv_usec = (suseconds_t)lrint(numUSecs);
#ifdef AMSerialDebug
NSLog(@"timeout: %fs = %ds and %dus", remainingTimeout, timeout.tv_sec, timeout.tv_usec);
#endif
// If the remaining time is so small that it has rounded to zero, bump it up to 1 microsec.
// Why? Because passing a zeroed timeval to select() indicates that we want to poll, but we don't.
if ((timeout.tv_sec == 0) && (timeout.tv_usec == 0)) {
timeout.tv_usec = 1;
}
FD_ZERO(readfds);
FD_SET(fileDescriptor, readfds);
[self readTimeoutAsTimeval:&timeout];
int selectResult = select(fileDescriptor+1, readfds, NULL, NULL, &timeout);
if (selectResult == -1) {
stop = YES;
errorCode = kAMSerialErrorFatal;
break;
} else if (selectResult == 0) {
stop = YES;
errorCode = kAMSerialErrorTimeout;
break;
} else {
size_t sizeToRead;
if (stopAfterBytes) {
sizeToRead = (MIN(bytesToRead, AMSER_MAXBUFSIZE))-bytesRead;
} else {
sizeToRead = AMSER_MAXBUFSIZE-bytesRead;
}
ssize_t readResult = read(fileDescriptor, buffer+bytesRead, sizeToRead);
if (readResult > 0) {
bytesRead += readResult;
if (stopAfterBytes) {
if (bytesRead == bytesToRead) {
stop = YES;
endCode = kAMSerialStopLengthReached;
break;
} else if (bytesRead > bytesToRead) {
stop = YES;
endCode = kAMSerialStopLengthExceeded;
break;
}
}
if (stopAtChar && (buffer[bytesRead-1] == stopChar)) {
stop = YES;
endCode = kAMSerialStopCharReached;
break;
}
if (bytesRead >= AMSER_MAXBUFSIZE) {
stop = YES;
errorCode = kAMSerialErrorInternalBufferFull;
break;
}
} else if (readResult == 0) {
// Should not be possible since select() has indicated data is available
stop = YES;
errorCode = kAMSerialErrorFatal;
break;
} else {
stop = YES;
// Make underlying error
underlyingError = [NSError errorWithDomain:NSPOSIXErrorDomain code:readResult userInfo:nil];
errorCode = kAMSerialErrorFatal;
break;
}
}
// Reduce the timeout value by the amount of time actually spent so far
remainingTimeout = totalTimeout - [[NSDate date] timeIntervalSinceDate:startTime];
}
}
if (error) {
NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
[userInfo setObject:[NSNumber numberWithUnsignedLongLong:bytesRead] forKey:@"bytesRead"];
if (underlyingError) {
[userInfo setObject:underlyingError forKey:NSUnderlyingErrorKey];
}
if (errorCode == kAMSerialErrorNone) {
[userInfo setObject:[NSNumber numberWithInt:endCode] forKey:@"endCode"];
}
*error = [NSError errorWithDomain:AMSerialErrorDomain code:errorCode userInfo:userInfo];
}
if ((bytesRead > 0) && (errorCode != kAMSerialErrorFatal)) {
result = [NSData dataWithBytes:buffer length:bytesRead];
}
return result;
}
- (void)reportProgress:(NSUInteger)progress dataLen:(NSUInteger)dataLen
{
#ifdef AMSerialDebug
NSLog(@"send AMSerialWriteInBackgroundProgressMessage");
#endif
[delegate performSelectorOnMainThread:@selector(serialPortWriteProgress:) withObject:
[NSDictionary dictionaryWithObjectsAndKeys:
self, @"serialPort",
[NSNumber numberWithUnsignedLongLong:progress], @"value",
[NSNumber numberWithUnsignedLongLong:dataLen], @"total", nil]
waitUntilDone:NO];
}
@end