-
Notifications
You must be signed in to change notification settings - Fork 60
/
accessories.c
704 lines (581 loc) · 20.6 KB
/
accessories.c
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
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
#include <stdlib.h>
#include <string.h>
#include "types.h"
bool homekit_value_equal(homekit_value_t *a, homekit_value_t *b) {
if (a->is_null != b->is_null)
return false;
if (a->format != b->format)
return false;
switch (a->format) {
case homekit_format_bool:
return a->bool_value == b->bool_value;
case homekit_format_uint8:
case homekit_format_uint16:
case homekit_format_uint32:
case homekit_format_uint64:
case homekit_format_int:
return a->int_value == b->int_value;
case homekit_format_float:
return a->float_value == b->float_value;
case homekit_format_string:
return !strcmp(a->string_value, b->string_value);
case homekit_format_tlv: {
if (!a->tlv_values && !b->tlv_values)
return true;
if (!a->tlv_values || !b->tlv_values)
return false;
// TODO: implement order independent comparison?
tlv_t *ta = a->tlv_values->head, *tb = b->tlv_values->head;
for (; ta && tb; ta=ta->next, tb=tb->next) {
if (ta->type != tb->type || ta->size != tb->size)
return false;
if (strncmp((char *)ta->value, (char *)tb->value, ta->size))
return false;
}
return (!ta && !tb);
}
case homekit_format_data:
// TODO: implement comparison
return false;
}
return false;
}
void homekit_value_copy(homekit_value_t *dst, homekit_value_t *src) {
memset(dst, 0, sizeof(*dst));
dst->format = src->format;
dst->is_null = src->is_null;
if (!src->is_null) {
switch (src->format) {
case homekit_format_bool:
dst->bool_value = src->bool_value;
break;
case homekit_format_uint8:
case homekit_format_uint16:
case homekit_format_uint32:
case homekit_format_uint64:
case homekit_format_int:
dst->int_value = src->int_value;
break;
case homekit_format_float:
dst->float_value = src->float_value;
break;
case homekit_format_string:
if (src->is_static) {
dst->string_value = src->string_value;
dst->is_static = true;
} else {
dst->string_value = strdup(src->string_value);
}
break;
case homekit_format_tlv: {
if (src->is_static) {
dst->tlv_values = src->tlv_values;
dst->is_static = true;
} else {
dst->tlv_values = tlv_new();
for (tlv_t *v=src->tlv_values->head; v; v=v->next) {
tlv_add_value(dst->tlv_values, v->type, v->value, v->size);
}
}
break;
}
case homekit_format_data:
// TODO:
break;
default:
// unknown format
break;
}
}
}
homekit_value_t *homekit_value_clone(homekit_value_t *value) {
homekit_value_t *copy = malloc(sizeof(homekit_value_t));
homekit_value_copy(copy, value);
return copy;
}
void homekit_value_destruct(homekit_value_t *value) {
if (!value->is_null) {
switch (value->format) {
case homekit_format_string:
if (!value->is_static && value->string_value)
free(value->string_value);
break;
case homekit_format_tlv:
if (!value->is_static && value->tlv_values)
tlv_free(value->tlv_values);
break;
case homekit_format_data:
// TODO:
break;
default:
// unknown format
break;
}
}
}
void homekit_value_free(homekit_value_t *value) {
homekit_value_destruct(value);
free(value);
}
size_t align_size(size_t size) {
if (size % sizeof(void*)) {
size += sizeof(void*) - size % sizeof(void*);
}
return size;
}
void *align_pointer(void *ptr) {
uintptr_t p = (uintptr_t) ptr;
if (p % sizeof(void*)) {
p += sizeof(void*) - p % sizeof(void*);
}
return (void*) p;
}
// to be checked
homekit_characteristic_t* homekit_add_characteristic_to_service(homekit_service_t* service, homekit_characteristic_t* ch) {
if (service->characteristics) {
int i = 0;
size_t size;
while (service->characteristics[i])
i++;
size += sizeof(homekit_characteristic_t*) * (i + 2);
homekit_characteristic_t** old = service->characteristics;
service->characteristics =calloc(1, size);
int n = 0;
while (old[n]) {
service->characteristics[n] = old[n];
n++;
}
//free(old);
service->characteristics[i] = ch;
service->characteristics[i+1] = NULL;
}
return ch;
}
homekit_characteristic_t* homekit_characteristic_clone(homekit_characteristic_t* ch) {
size_t type_len = strlen(ch->type) + 1;
size_t description_len = ch->description ? strlen(ch->description) + 1 : 0;
size_t size = align_size(sizeof(homekit_characteristic_t) + type_len + description_len);
if (ch->min_value)
size += sizeof(float);
if (ch->max_value)
size += sizeof(float);
if (ch->min_step)
size += sizeof(float);
if (ch->max_len)
size += sizeof(int);
if (ch->max_data_len)
size += sizeof(int);
if (ch->valid_values.count)
size += align_size(sizeof(uint8_t) * ch->valid_values.count);
if (ch->valid_values_ranges.count)
size += align_size(sizeof(homekit_valid_values_range_t) * ch->valid_values_ranges.count);
if (ch->callback) {
homekit_characteristic_change_callback_t *c = ch->callback;
while (c) {
size += align_size(sizeof(homekit_characteristic_change_callback_t));
c = c->next;
}
}
uint8_t* p = calloc(1, size);
homekit_characteristic_t* clone = (homekit_characteristic_t*) p;
p += sizeof(homekit_characteristic_t);
clone->service = ch->service;
clone->id = ch->id;
clone->type = (char*) p;
strncpy((char*) p, ch->type, type_len);
p[type_len - 1] = 0;
p += type_len;
clone->description = (char*) p;
strncpy((char*) p, ch->description, description_len);
p[description_len - 1] = 0;
p += description_len;
p = align_pointer(p);
clone->format = ch->format;
clone->unit = ch->unit;
clone->permissions = ch->permissions;
homekit_value_copy(&clone->value, &ch->value);
if (ch->min_value) {
clone->min_value = (float*) p;
*clone->min_value = *ch->min_value;
p += sizeof(float);
}
if (ch->max_value) {
clone->max_value = (float*) p;
*clone->max_value = *ch->max_value;
p += sizeof(float);
}
if (ch->min_step) {
clone->min_step = (float*) p;
*clone->min_step = *ch->min_step;
p += sizeof(float);
}
if (ch->max_len) {
clone->max_len = (int*) p;
*clone->max_len = *ch->max_len;
p += sizeof(int);
}
if (ch->max_data_len) {
clone->max_data_len = (int*) p;
*clone->max_data_len = *ch->max_data_len;
p += sizeof(int);
}
if (ch->valid_values.count) {
clone->valid_values.count = ch->valid_values.count;
clone->valid_values.values = (uint8_t*) p;
memcpy(clone->valid_values.values, ch->valid_values.values,
sizeof(uint8_t) * ch->valid_values.count);
p += align_size(sizeof(uint8_t) * ch->valid_values.count);
}
if (ch->valid_values_ranges.count) {
int c = ch->valid_values_ranges.count;
clone->valid_values_ranges.count = c;
clone->valid_values_ranges.ranges = (homekit_valid_values_range_t*) p;
memcpy(clone->valid_values_ranges.ranges,
ch->valid_values_ranges.ranges,
sizeof(homekit_valid_values_range_t*) * c);
p += align_size(sizeof(homekit_valid_values_range_t*) * c);
}
if (ch->callback) {
int c = 1;
homekit_characteristic_change_callback_t *callback_in = ch->callback;
clone->callback = (homekit_characteristic_change_callback_t*)p;
homekit_characteristic_change_callback_t *callback_out = clone->callback;
memcpy(callback_out, callback_in, sizeof(*callback_out));
while (callback_in->next) {
callback_in = callback_in->next;
callback_out->next = callback_out + 1;
callback_out = callback_out->next;
memcpy(callback_out, callback_in, sizeof(*callback_out));
c++;
}
callback_out->next = NULL;
p += align_size(sizeof(homekit_characteristic_change_callback_t)) * c;
}
clone->getter = ch->getter;
clone->setter = ch->setter;
clone->getter_ex = ch->getter_ex;
clone->setter_ex = ch->setter_ex;
clone->context = ch->context;
return clone;
}
homekit_service_t* homekit_service_clone_preserve_characteristic(homekit_service_t* service) {
size_t type_len = strlen(service->type) + 1;
size_t size = align_size(sizeof(homekit_service_t) + type_len);
if (service->linked) {
int i = 0;
while (service->linked[i])
i++;
size += sizeof(homekit_service_t*) * (i + 1);
}
if (service->characteristics) {
int i = 0;
while (service->characteristics[i])
i++;
size += sizeof(homekit_characteristic_t*) * (i + 1);
}
uint8_t *p = calloc(1, size);
homekit_service_t *clone = (homekit_service_t*)p;
p += sizeof(homekit_service_t);
clone->accessory = service->accessory;
clone->id = service->id;
clone->type = strncpy((char*)p, service->type, type_len);
p[type_len - 1] = 0;
p += align_size(type_len);
clone->hidden = service->hidden;
clone->primary = service->primary;
if (service->linked) {
clone->linked = (homekit_service_t**)p;
int i = 0;
while (service->linked[i]) {
clone->linked[i] = service->linked[i];
i++;
}
clone->linked[i] = NULL;
p += (i + 1) * sizeof(homekit_service_t*);
}
if (service->characteristics) {
clone->characteristics = (homekit_characteristic_t**)p;
int i = 0;
while (service->characteristics[i]) {
clone->characteristics[i] = service->characteristics[i];
i++;
}
clone->characteristics[i] = NULL;
p += (i + 1) * sizeof(homekit_characteristic_t*);
}
return clone;
}
homekit_service_t* homekit_service_clone(homekit_service_t* service) {
size_t type_len = strlen(service->type) + 1;
size_t size = align_size(sizeof(homekit_service_t) + type_len);
if (service->linked) {
int i = 0;
while (service->linked[i])
i++;
size += sizeof(homekit_service_t*) * (i + 1);
}
if (service->characteristics) {
int i = 0;
while (service->characteristics[i])
i++;
size += sizeof(homekit_characteristic_t*) * (i + 1);
}
uint8_t *p = calloc(1, size);
homekit_service_t *clone = (homekit_service_t*) p;
p += sizeof(homekit_service_t);
clone->accessory = service->accessory;
clone->id = service->id;
clone->type = strncpy((char*) p, service->type, type_len);
p[type_len - 1] = 0;
p += align_size(type_len);
clone->hidden = service->hidden;
clone->primary = service->primary;
if (service->linked) {
clone->linked = (homekit_service_t**) p;
int i = 0;
while (service->linked[i]) {
clone->linked[i] = service->linked[i];
i++;
}
clone->linked[i] = NULL;
p += (i + 1) * sizeof(homekit_service_t*);
}
if (service->characteristics) {
clone->characteristics = (homekit_characteristic_t**) p;
int i = 0;
while (service->characteristics[i]) {
clone->characteristics[i] = service->characteristics[i];
i++;
}
clone->characteristics[i] = NULL;
p += (i + 1) * sizeof(homekit_characteristic_t*);
}
return clone;
}
void homekit_accessory_free(homekit_accessory_t* ac,bool freeservices) {
if (freeservices && ac->services) {
for (int i = 0; ac->services[i]; i++) {
free(ac->services[i]);
ac->services[i] = 0;
}
}
free(ac);
}
homekit_accessory_t* homekit_accessory_clone(homekit_accessory_t* ac) {
size_t size = sizeof(homekit_accessory_t);
if (ac->services) {
for (int i=0; ac->services[i]; i++) {
size += sizeof(homekit_service_t*);
}
size += sizeof(homekit_service_t*);
}
uint8_t* p = calloc(1, size);
homekit_accessory_t* clone = (homekit_accessory_t*) p;
p += align_size(sizeof(homekit_accessory_t));
clone->id = ac->id;
clone->category = ac->category;
clone->config_number = ac->config_number;
if (ac->services) {
clone->services = (homekit_service_t**) p;
int i = 0;
while (ac->services[i]) {
clone->services[i] = ac->services[i];
i++;
}
clone->services[i] = NULL;
p += sizeof(homekit_service_t*) * (i + 1);
}
return clone;
}
homekit_value_t homekit_characteristic_ex_old_getter(const homekit_characteristic_t *ch) {
return ch->getter();
}
void homekit_characteristic_ex_old_setter(homekit_characteristic_t *ch, homekit_value_t value) {
ch->setter(value);
}
void homekit_accessories_init(homekit_accessory_t **accessories) {
int aid = 1;
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
homekit_accessory_t *accessory = *accessory_it;
if (accessory->id) {
if (accessory->id >= aid)
aid = accessory->id+1;
} else {
accessory->id = aid++;
}
int iid = 1;
for (homekit_service_t **service_it = accessory->services; *service_it; service_it++) {
homekit_service_t *service = *service_it;
service->accessory = accessory;
if (service->id) {
if (service->id >= iid)
iid = service->id+1;
} else {
service->id = iid++;
}
for (homekit_characteristic_t **ch_it = service->characteristics; *ch_it; ch_it++) {
homekit_characteristic_t *ch = *ch_it;
ch->service = service;
if (ch->id) {
if (ch->id >= iid)
iid = ch->id+1;
} else {
ch->id = iid++;
}
if (!ch->getter_ex && ch->getter) {
ch->getter_ex = homekit_characteristic_ex_old_getter;
}
if (!ch->setter_ex && ch->setter) {
ch->setter_ex = homekit_characteristic_ex_old_setter;
}
ch->value.format = ch->format;
}
}
}
}
homekit_accessory_t *homekit_accessory_by_id(homekit_accessory_t **accessories, int aid) {
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
homekit_accessory_t *accessory = *accessory_it;
if (accessory->id == aid)
return accessory;
}
return NULL;
}
homekit_service_t *homekit_service_by_type(homekit_accessory_t *accessory, const char *type) {
for (homekit_service_t **service_it = accessory->services; *service_it; service_it++) {
homekit_service_t *service = *service_it;
if (!strcmp(service->type, type))
return service;
}
return NULL;
}
homekit_characteristic_t *homekit_service_characteristic_by_type(homekit_service_t *service, const char *type) {
for (homekit_characteristic_t **ch_it = service->characteristics; *ch_it; ch_it++) {
homekit_characteristic_t *ch = *ch_it;
if (!strcmp(ch->type, type))
return ch;
}
return NULL;
}
homekit_characteristic_t *homekit_characteristic_by_aid_and_iid(homekit_accessory_t **accessories, int aid, int iid) {
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
homekit_accessory_t *accessory = *accessory_it;
if (accessory->id != aid)
continue;
for (homekit_service_t **service_it = accessory->services; *service_it; service_it++) {
homekit_service_t *service = *service_it;
for (homekit_characteristic_t **ch_it = service->characteristics; *ch_it; ch_it++) {
homekit_characteristic_t *ch = *ch_it;
if (ch->id == iid)
return ch;
}
}
}
return NULL;
}
homekit_characteristic_t *homekit_characteristic_find_by_type(homekit_accessory_t **accessories, int aid, const char *type) {
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
homekit_accessory_t *accessory = *accessory_it;
if (accessory->id != aid)
continue;
for (homekit_service_t **service_it = accessory->services; *service_it; service_it++) {
homekit_service_t *service = *service_it;
for (homekit_characteristic_t **ch_it = service->characteristics; *ch_it; ch_it++) {
homekit_characteristic_t *ch = *ch_it;
if (!strcmp(ch->type, type))
return ch;
}
}
}
return NULL;
}
void homekit_characteristic_notify(homekit_characteristic_t *ch, homekit_value_t value) {
homekit_characteristic_change_callback_t *callback = ch->callback;
while (callback) {
callback->function(ch, value, callback->context);
callback = callback->next;
}
}
void homekit_characteristic_add_notify_callback(
homekit_characteristic_t *ch,
homekit_characteristic_change_callback_fn function,
void *context
) {
homekit_characteristic_change_callback_t *new_callback = malloc(sizeof(homekit_characteristic_change_callback_t));
new_callback->function = function;
new_callback->context = context;
new_callback->next = NULL;
if (!ch->callback) {
ch->callback = new_callback;
} else {
homekit_characteristic_change_callback_t *callback = ch->callback;
if (callback->function == function && callback->context == context) {
free(new_callback);
return;
}
while (callback->next) {
if (callback->next->function == function && callback->next->context == context) {
free(new_callback);
return;
}
callback = callback->next;
}
callback->next = new_callback;
}
}
void homekit_characteristic_remove_notify_callback(
homekit_characteristic_t *ch,
homekit_characteristic_change_callback_fn function,
void *context
) {
while (ch->callback) {
if (ch->callback->function != function || ch->callback->context != context) {
break;
}
homekit_characteristic_change_callback_t *c = ch->callback;
ch->callback = ch->callback->next;
free(c);
}
if (!ch->callback)
return;
homekit_characteristic_change_callback_t *callback = ch->callback;
while (callback->next) {
if (callback->next->function == function && callback->next->context == context) {
homekit_characteristic_change_callback_t *c = callback->next;
callback->next = callback->next->next;
free(c);
} else {
callback = callback->next;
}
}
}
// Removes particular callback from all characteristics
void homekit_accessories_clear_notify_callbacks(
homekit_accessory_t **accessories,
homekit_characteristic_change_callback_fn function,
void *context
) {
for (homekit_accessory_t **accessory_it = accessories; *accessory_it; accessory_it++) {
homekit_accessory_t *accessory = *accessory_it;
for (homekit_service_t **service_it = accessory->services; *service_it; service_it++) {
homekit_service_t *service = *service_it;
for (homekit_characteristic_t **ch_it = service->characteristics; *ch_it; ch_it++) {
homekit_characteristic_t *ch = *ch_it;
homekit_characteristic_remove_notify_callback(ch, function, context);
}
}
}
}
bool homekit_characteristic_has_notify_callback(
const homekit_characteristic_t *ch,
homekit_characteristic_change_callback_fn function,
void *context
) {
homekit_characteristic_change_callback_t *callback = ch->callback;
while (callback) {
if (callback->function == function && callback->context == context)
return true;
callback = callback->next;
}
return false;
}