-
Notifications
You must be signed in to change notification settings - Fork 15
/
SuplaApp.m
456 lines (352 loc) · 12.3 KB
/
SuplaApp.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
/*
Copyright (C) AC SOFTWARE SP. Z O.O.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#import "SuplaApp.h"
#import "Database.h"
#import "NSData+AES.h"
#import "SAKeychain.h"
#import "SASuperuserAuthorizationDialog.h"
#import "NSNumber+SUPLA.h"
#import "SUPLA-Swift.h"
#import "AuthProfileItem+CoreDataClass.h"
static SAApp* _Globals = nil;
NSString *kSADataChangedNotification = @"kSA-N01";
NSString *kSAConnectingNotification = @"kSA-N02";
NSString *kSARegisteredNotification = @"kSA-N03";
NSString *kSARegisteringNotification = @"kSA-N04";
NSString *kSARegisterErrorNotification = @"kSA-N05";
NSString *kSADisconnectedNotification = @"kSA-N06";
NSString *kSAConnectedNotification = @"kSA-N07";
NSString *kSAVersionErrorNotification = @"kSA-N08";
NSString *kSAEventNotification = @"kSA-N09";
NSString *kSAConnErrorNotification = @"kSA-N10";
NSString *kSAChannelValueChangedNotification = @"KSA-N11";
NSString *kSARegistrationEnabledNotification = @"KSA-N12";
NSString *kSASuperuserAuthorizationNotification = @"KSA-N14";
NSString *kSAOAuthTokenRequestResult = @"KSA-N13";
NSString *kSASuperuserAuthorizationResult = @"KSA-N14";
NSString *kSACalCfgResult = @"KSA-N15";
NSString *kSAMenubarBackButtonPressed = @"KSA-N16";
NSString *kSAOnChannelState = @"KSA-N17";
NSString *kSAOnSetRegistrationEnableResult = @"KSA-N18";
NSString *kSAOnChannelBasicCfg = @"KSA-N19";
NSString *kSAOnChannelCaptionSetResult = @"KSA-N20";
NSString *kSAOnChannelFunctionSetResult = @"KSA-N21";
NSString *kSAOnZWaveAssignedNodeIdResult = @"KSA-N22";
NSString *kSAOnZWaveNodeListResult = @"KSA-N23";
NSString *kSAOnCalCfgProgressReport = @"KSA-N24";
NSString *kSAOnZWaveResetAndClearResult = @"KSA-N25";
NSString *kSAOnZWaveAddNodeResult = @"KSA-N26";
NSString *kSAOnZWaveRemoveNodeResult = @"KSA-N27";
NSString *kSAOnZWaveAssignNodeIdResult = @"KSA-N28";
NSString *kSAOnZWaveWakeupSettingsReport = @"KSA-N29";
NSString *kSAOnZWaveSetWakeUpTimeResult = @"KSA-N30";
NSString *kSAOnChannelGroupCaptionSetResult = @"OnChannelGroupCaptionSetResult";
@implementation SAApp {
int _cfg_ver;
int _current_access_id;
NSString *_current_server;
NSString *_current_email;
SASuplaClient* _SuplaClient;
SADatabase *_DB;
NSMutableArray *_RestApiClientTasks;
SAOAuthToken *_OAuthToken;
}
-(id)init {
self = [super init];
if ( self ) {
_cfg_ver = 0;
_current_access_id = 0;
_current_server = nil;
signal(SIGPIPE, SIG_IGN);
_RestApiClientTasks = [[NSMutableArray alloc] init];
[[NSNotificationCenter defaultCenter]
addObserver:self selector:@selector(onOAuthTokenRequestResult:)
name:kSAOAuthTokenRequestResult object:nil];
}
return self;
}
+(SAApp*)instance {
@synchronized(self) {
if ( _Globals == nil ) {
_Globals = [[SAApp alloc] init];
}
}
return _Globals;
}
-(BOOL) getRandom:(char*)key size:(int)size forPrefKey:(NSString*)pref_key {
@synchronized(self) {
NSData *data = nil;
BOOL keychainStored = NO;
data = [SAKeychain getObjectWithKey:pref_key];
if (data == nil
|| ![data isKindOfClass:[NSData class]]
|| data.length != size) {
if ([[NSUserDefaults standardUserDefaults]
boolForKey:[NSString stringWithFormat:@"%@_keychain", pref_key]]) {
// Something goes wrong
// Maybe beacuse of kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly
return false;
}
data = [[NSUserDefaults standardUserDefaults] dataForKey:pref_key];
if (data != nil
&& data.length > 0
&& [[NSUserDefaults standardUserDefaults] boolForKey:[NSString stringWithFormat:@"%@_encrypted", pref_key]]) {
data = [data aes128DecryptWithDeviceUniqueId];
// After the device has been restarted but before
// the user has unlocked the device, dectyption result
// can be null beacuse of identifierForVendor.
if (data == nil || data.length == 0) {
return false;
}
}
} else {
keychainStored = YES;
}
if ( data == nil || data.length != size ) {
NSMutableData* newRandomData = [NSMutableData dataWithCapacity:size];
for( int i = 0 ; i < size; ++i ) {
Byte random = arc4random();
[newRandomData appendBytes:(void*)&random length:1];
}
keychainStored = NO;
data = newRandomData;
}
if ( data && [data length] == size ) {
[data getBytes:key length:size];
if (!keychainStored) {
[SAKeychain deleteObjectWithKey:pref_key];
if ([SAKeychain addObject:data withKey:pref_key]) {
[[NSUserDefaults standardUserDefaults] setBool:YES forKey:[NSString stringWithFormat:@"%@_keychain", pref_key]];
}
}
return true;
} else {
memset(key, 0, size);
}
}
return false;
}
+(BOOL) getClientGUID:(char[SUPLA_GUID_SIZE])guid {
return [[self instance] getRandom:guid size:SUPLA_GUID_SIZE forPrefKey:@"client_guid"];
}
+(BOOL) getAuthKey:(char [SUPLA_AUTHKEY_SIZE])auth_key {
return [[self instance] getRandom:auth_key size:SUPLA_AUTHKEY_SIZE forPrefKey:@"auth_key"];
}
+ (id<ProfileManager>)profileManager {
return [[MultiAccountProfileManager alloc] init];
}
-(void) setBrightnessPickerTypeToSlider:(BOOL)slider {
@synchronized(self) {
[[NSUserDefaults standardUserDefaults] setBool:slider forKey:@"pref_brightness_picker_type_slider"];
}
}
-(BOOL) isBrightnessPickerTypeSet {
BOOL result = 0;
@synchronized(self) {
result = [[NSUserDefaults standardUserDefaults] objectForKey:@"pref_brightness_picker_type_slider"] != nil;
}
return result;
}
-(BOOL) isBrightnessPickerTypeSlider {
BOOL result = 0;
@synchronized(self) {
result = [[NSUserDefaults standardUserDefaults] boolForKey:@"pref_brightness_picker_type_slider"];
}
return result;
}
+ (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
+(void) setBrightnessPickerTypeToSlider:(BOOL)slider {
[[self instance] setBrightnessPickerTypeToSlider:slider];
}
+(BOOL) isBrightnessPickerTypeSet {
return [[self instance] isBrightnessPickerTypeSet];
}
+(BOOL) isBrightnessPickerTypeSlider {
return [[self instance] isBrightnessPickerTypeSlider];
}
-(SASuplaClient*) SuplaClientWithOneTimePassword:(NSString *)password {
SASuplaClient *result = nil;
@synchronized(self) {
if ( _SuplaClient == nil) {
_SuplaClient = [[SASuplaClient alloc] initWithOneTimePassword:password];
_SuplaClient.delegate = self;
[_SuplaClient start];
}
result = _SuplaClient;
}
return result;
}
-(BOOL) isClientRegistered {
BOOL result = NO;
@synchronized(self) {
if ( _SuplaClient != nil) {
result = [_SuplaClient isRegistered];
}
}
return result;
}
-(BOOL) isClientWorking {
BOOL result = NO;
@synchronized(self) {
if ( _SuplaClient != nil) {
result = ![_SuplaClient isCancelled];
}
}
return result;
}
-(SASuplaClient *) optionalSuplaClient {
@synchronized(self) {
if ( _SuplaClient != nil) {
return _SuplaClient;
}
}
return nil;
}
-(BOOL) isClientAuthorized {
BOOL result = NO;
@synchronized(self) {
if ( _SuplaClient != nil) {
result = [_SuplaClient isRegistered] && [_SuplaClient isSuperuserAuthorized];
}
}
return result;
}
+(SASuplaClient*) SuplaClient {
return [[self instance] SuplaClientWithOneTimePassword:nil];
}
+(SASuplaClient *) SuplaClientWithOneTimePassword:(NSString*)password {
return [[self instance] SuplaClientWithOneTimePassword:password];
}
+(BOOL) isClientRegistered {
return [[self instance] isClientRegistered];
}
-(BOOL) SuplaClientTerminate {
BOOL result = NO;
@synchronized(self) {
if ( _SuplaClient != nil ) {
[_SuplaClient cancel];
result = YES;
}
}
return result;
}
-(void)onSuplaClientTerminated:(SASuplaClient*)client {
@synchronized(self) {
if ( _SuplaClient == client ) {
_SuplaClient = nil;
}
}
}
+(void) SuplaClientTerminate {
[[self instance] SuplaClientTerminate];
}
+(void) SuplaClientWaitForTerminate {
NSDate *deadline = [NSDate dateWithTimeIntervalSinceNow: 3];
BOOL working = [[self instance] SuplaClientTerminate];
while(working && [[self instance] isClientWorking]) {
@autoreleasepool {
NSDate *cDate = [NSDate date];
if([cDate earlierDate: deadline] == deadline)
break;
else
[[NSRunLoop currentRunLoop] runUntilDate: cDate];
}
}
}
-(BOOL) SuplaClientConnected {
return _SuplaClient != nil && [_SuplaClient isConnected];
}
+(BOOL) SuplaClientConnected {
return [[self instance] SuplaClientConnected];
}
-(SADatabase*)DB {
if ( _DB == nil ) {
_DB = [[SADatabase alloc] init];
[_DB initSaveObserver];
}
return _DB;
}
+(SADatabase*)DB {
return [[self instance] DB];
}
-(NSString*)getMsgHostName {
AuthProfileItem *profile = [SAApp.profileManager getCurrentProfile];
if (profile == nil) {
return @"";
}
SAProfileServer *server = profile.server;
if (server == nil) {
return @"";
}
NSString *hostname = server.address;
if (hostname == nil) {
return @"";
}
if ( [[hostname lowercaseString] containsString:@"supla.org"] ) {
return @"cloud.supla.org";
} else {
return hostname;
}
}
-(void)onOAuthTokenRequestResult:(NSNotification *)notification {
SAOAuthToken *token = [SAOAuthToken notificationToToken:notification];
@synchronized (self) {
NSEnumerator *e = [_RestApiClientTasks objectEnumerator];
SARestApiClientTask *cli;
_OAuthToken = token;
while (cli = [e nextObject]) {
cli.token = token;
}
}
}
-(SAOAuthToken*) registerRestApiClientTask:(SARestApiClientTask *)task {
SAOAuthToken *result = nil;
@synchronized (self) {
if ( _OAuthToken != nil && [_OAuthToken isAlive]) {
result = _OAuthToken;
}
[_RestApiClientTasks addObject:task];
}
return result;
}
- (void) revokeOAuthToken {
@synchronized (self) {
_OAuthToken = nil;
}
}
+ (void) revokeOAuthToken {
[[self instance] revokeOAuthToken];
}
- (void) unregisterRestApiClientTask:(SARestApiClientTask *)task {
@synchronized (self) {
[_RestApiClientTasks removeObject:task];
}
}
-(void) cancelAllRestApiClientTasks {
@synchronized (self) {
NSEnumerator *e = [_RestApiClientTasks objectEnumerator];
SARestApiClientTask *cli;
while (cli = [e nextObject]) {
[cli cancel];
}
}
}
+(void) abstractMethodException:(NSString *)methodName {
[NSException raise:NSInternalInconsistencyException
format:@"You must override %@ in a subclass", methodName];
}
@end