-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGCResponse.m
118 lines (107 loc) · 3.01 KB
/
GCResponse.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
/*
* GCResponse.m
* libGlobalCache
*
* Created on 7/29/09.
* Copyright (c) 2009-2013 by NSDex. All rights reserved.
*/
#import "GCResponse.h"
@implementation GCResponse
@synthesize dataParsed;
@synthesize module;
@synthesize connector;
@synthesize devices;
@synthesize versionString;
@synthesize irid;
@synthesize sensorIsClosed;
- (id)init {
return nil;
}
- (id)initWithType:(GCCommandType)t {
if(self = [super init]) {
type = t;
dataParsed = NO;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
- (BOOL)parseData:(NSData*)data {
#ifdef DEBUG
printf("%s - %s: Parse data called for Command\n", GCDebugHeader, "GCResponse");
#endif
char *bytes = (char *)[data bytes];
int pos = 0;
//Find the response type
if(!strncmp(bytes, "device", 6) && type == GCGETDEVICES) {
if(self.devices == nil) self.devices = [NSMutableArray arrayWithCapacity:0];
pos = 6;
if(bytes[pos] != ',') { return NO; }
pos++;
char buffer[256]; int i; for(i=0; i<sizeof(buffer); i++) { buffer[i] = bytes[pos+i]; if(buffer[i] == GCCommandTerminator) break; } buffer[i] = '\0';
[self.devices addObject:[NSString stringWithCString:buffer encoding:NSASCIIStringEncoding]];
}
else if(!strncmp(bytes, "endlistdevices", 14) && type == GCGETDEVICES) {
if(self.devices == nil) self.devices = [NSMutableArray arrayWithCapacity:0];
pos = 14;
dataParsed = YES;
return YES;
}
else if(!strncmp(bytes, "version", 7) && type == GCGETVERSION) {
pos = 7;
if(bytes[pos] != ',') { return NO; }
pos++;
char buf[2];
buf[0] = bytes[pos]; buf[1] = '\0';
module = atoi(buf);
pos++;
if(bytes[pos] != ',') { return NO; }
pos++;
char buffer[256]; int i; for(i=0; i<sizeof(buffer); i++) { buffer[i] = bytes[pos+i]; if(buffer[i] == GCCommandTerminator) break; } buffer[i] = '\0';
self.versionString = [NSString stringWithCString:buffer encoding:NSASCIIStringEncoding];
dataParsed = YES;
return YES;
}
else if(!strncmp(bytes, "completeir", 10) && type == GCSENDIR) {
pos = 10;
if(bytes[pos] != ',') { return NO; }
pos++;
char buf[2];
buf[0] = bytes[pos]; buf[1] = '\0';
module = atoi(buf);
pos++;
if(bytes[pos] != ':') { return NO; }
pos++;
buf[0] = bytes[pos]; buf[1] = '\0';
connector = atoi(buf);
pos++;
if(bytes[pos] != ',') { return NO; }
pos++;
char buffer[256]; int i; for(i=0; i<sizeof(buffer); i++) { buffer[i] = bytes[pos+i]; if(buffer[i] == GCCommandTerminator) break; } buffer[i] = '\0';
irid = atoi(buffer);
dataParsed = YES;
return YES;
}
else if(!strncmp(bytes, "state", 5) && (type == GCGETSTATE || type == GCSETSTATE)) {
pos = 5;
if(bytes[pos] != ',') { return NO; }
pos++;
char buf[2];
buf[0] = bytes[pos]; buf[1] = '\0';
module = atoi(buf);
pos++;
if(bytes[pos] != ':') { return NO; }
pos++;
buf[0] = bytes[pos]; buf[1] = '\0';
connector = atoi(buf);
pos++;
if(bytes[pos] != ',') { return NO; }
pos++;
if(bytes[pos] == '1') sensorIsClosed = YES; else if(bytes[pos] == '0') sensorIsClosed = NO; else return NO;
dataParsed = YES;
return YES;
}
return YES;
}
@end