-
Notifications
You must be signed in to change notification settings - Fork 84
/
PHKAccessory.cpp
executable file
·796 lines (655 loc) · 25.7 KB
/
PHKAccessory.cpp
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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
//
// PHKAccessory.c
// Workbench
//
// Created by Wai Man Chan on 9/27/14.
//
//
#include "PHKAccessory.h"
#include "PHKNetworkIP.h"
#include "Configuration.h"
const char hapJsonType[] = "application/hap+json";
const char pairingTlv8Type[] = "application/pairing+tlv8";
void *announce(void *info) {
broadcastInfo *_info = (broadcastInfo *)info;
void *sender = _info->sender;
char *desc = _info->desc;
char *reply = new char[1024];
int len = snprintf(reply, 1024, "EVENT/1.0 200 OK\r\nContent-Type: application/hap+json\r\nContent-Length: %lu\r\n\r\n%s", strlen(desc), desc);
#if HomeKitLog == 1 && HomeKitReplyHeaderLog==1
printf("%s\n", reply);
#endif
broadcastMessage(sender, reply, len);
delete [] reply;
delete [] desc;
delete [] info;
}
//Wrap to JSON
inline string wrap(const char *str) { return (string)"\""+str+"\""; }
//Value String
inline string attribute(unsigned int type, unsigned short acclaim, int p, string value) {
string result;
if (p & premission_read) {
result += wrap("value")+":";
result += value;
result += ",";
}
result += wrap("perms")+":";
result += "[";
if (p & premission_read) result += wrap("pr")+",";
if (p & premission_write) result += wrap("pw")+",";
if (p & premission_notify) result += wrap("ev")+",";
result = result.substr(0, result.size()-1);
result += "]";
result += ",";
char tempStr[4];
snprintf(tempStr, 4, "%X", type);
result += wrap("type")+":"+wrap(tempStr);
result += ",";
snprintf(tempStr, 4, "%hd", acclaim);
result += wrap("iid")+":"+tempStr;
result += ",";
result += "\"format\":\"bool\"";
return "{"+result+"}";
}
inline string attribute(unsigned int type, unsigned short acclaim, int p, string value, int minVal, int maxVal, int step, unit valueUnit) {
string result;
char tempStr[16];
if (p & premission_read) {
result += wrap("value")+":"+value;
result += ",";
}
snprintf(tempStr, 16, "%d", minVal);
if (minVal != INT32_MIN)
result += wrap("minValue")+":"+tempStr+",";
snprintf(tempStr, 16, "%d", maxVal);
if (maxVal != INT32_MAX)
result += wrap("maxValue")+":"+tempStr+",";
snprintf(tempStr, 16, "%d", step);
if (step > 0)
result += wrap("minStep")+":"+tempStr+",";
result += wrap("perms")+":";
result += "[";
if (p & premission_read) result += wrap("pr")+",";
if (p & premission_write) result += wrap("pw")+",";
if (p & premission_notify) result += wrap("ev")+",";
result = result.substr(0, result.size()-1);
result += "]";
result += ",";
snprintf(tempStr, 16, "%X", type);
result += wrap("type")+":"+wrap(tempStr);
result += ",";
snprintf(tempStr, 16, "%hd", acclaim);
result += wrap("iid")+":"+tempStr;
result += ",";
switch (valueUnit) {
case unit_arcDegree:
result += wrap("unit")+":"+wrap("arcdegrees")+",";
break;
case unit_celsius:
result += wrap("unit")+":"+wrap("celsius")+",";
break;
case unit_percentage:
result += wrap("unit")+":"+wrap("percentage")+",";
break;
}
result += "\"format\":\"int\"";
return "{"+result+"}";
}
inline string attribute(unsigned int type, unsigned short acclaim, int p, string value, float minVal, float maxVal, float step, unit valueUnit) {
string result;
char tempStr[16];
if (p & premission_read) {
result += wrap("value")+":"+value;
result += ",";
}
snprintf(tempStr, 16, "%f", minVal);
if (minVal != INT32_MIN)
result += wrap("minValue")+":"+tempStr+",";
snprintf(tempStr, 16, "%f", maxVal);
if (maxVal != INT32_MAX)
result += wrap("maxValue")+":"+tempStr+",";
snprintf(tempStr, 16, "%f", step);
if (step > 0)
result += wrap("minStep")+":"+tempStr+",";
result += wrap("perms")+":";
result += "[";
if (p & premission_read) result += wrap("pr")+",";
if (p & premission_write) result += wrap("pw")+",";
if (p & premission_notify) result += wrap("ev")+",";
result = result.substr(0, result.size()-1);
result += "]";
result += ",";
snprintf(tempStr, 16, "%X", type);
result += wrap("type")+":"+wrap(tempStr);
result += ",";
snprintf(tempStr, 16, "%hd", acclaim);
result += wrap("iid")+":"+tempStr;
result += ",";
switch (valueUnit) {
case unit_arcDegree:
result += wrap("unit")+":"+wrap("arcdegrees")+",";
break;
case unit_celsius:
result += wrap("unit")+":"+wrap("celsius")+",";
break;
case unit_percentage:
result += wrap("unit")+":"+wrap("percentage")+",";
break;
}
result += "\"format\":\"float\"";
return "{"+result+"}";
}
//Raw value
inline string attribute(unsigned int type, unsigned short acclaim, int p, bool value) {
string result;
if (p & premission_read) {
result += wrap("value")+":";
if (value) result += "true";
else result += "false";
result += ",";
}
result += wrap("perms")+":";
result += "[";
if (p & premission_read) result += wrap("pr")+",";
if (p & premission_write) result += wrap("pw")+",";
if (p & premission_notify) result += wrap("ev")+",";
result = result.substr(0, result.size()-1);
result += "]";
result += ",";
char tempStr[4];
snprintf(tempStr, 4, "%X", type);
result += wrap("type")+":"+wrap(tempStr);
result += ",";
snprintf(tempStr, 4, "%hd", acclaim);
result += wrap("iid")+":"+tempStr;
result += ",";
result += "\"format\":\"bool\"";
return "{"+result+"}";
}
inline string attribute(unsigned int type, unsigned short acclaim, int p, int value, int minVal, int maxVal, int step, unit valueUnit) {
string result;
char tempStr[16];
snprintf(tempStr, 16, "%d", value);
if (p & premission_read) {
result += wrap("value")+":"+tempStr;
result += ",";
}
snprintf(tempStr, 16, "%d", minVal);
if (minVal != INT32_MIN)
result += wrap("minValue")+":"+tempStr+",";
snprintf(tempStr, 16, "%d", maxVal);
if (maxVal != INT32_MAX)
result += wrap("maxValue")+":"+tempStr+",";
snprintf(tempStr, 16, "%d", step);
if (step > 0)
result += wrap("minStep")+":"+tempStr+",";
result += wrap("perms")+":";
result += "[";
if (p & premission_read) result += wrap("pr")+",";
if (p & premission_write) result += wrap("pw")+",";
if (p & premission_notify) result += wrap("ev")+",";
result = result.substr(0, result.size()-1);
result += "]";
result += ",";
snprintf(tempStr, 16, "%X", type);
result += wrap("type")+":"+wrap(tempStr);
result += ",";
snprintf(tempStr, 16, "%hd", acclaim);
result += wrap("iid")+":"+tempStr;
result += ",";
switch (valueUnit) {
case unit_arcDegree:
result += wrap("unit")+":"+wrap("arcdegrees")+",";
break;
case unit_celsius:
result += wrap("unit")+":"+wrap("celsius")+",";
break;
case unit_percentage:
result += wrap("unit")+":"+wrap("percentage")+",";
break;
}
result += "\"format\":\"int\"";
return "{"+result+"}";
}
inline string attribute(unsigned int type, unsigned short acclaim, int p, float value, float minVal, float maxVal, float step, unit valueUnit) {
string result;
char tempStr[16];
snprintf(tempStr, 16, "%f", value);
if (p & premission_read) {
result += wrap("value")+":"+tempStr;
result += ",";
}
snprintf(tempStr, 16, "%f", minVal);
if (minVal != INT32_MIN)
result += wrap("minValue")+":"+tempStr+",";
snprintf(tempStr, 16, "%f", maxVal);
if (maxVal != INT32_MAX)
result += wrap("maxValue")+":"+tempStr+",";
snprintf(tempStr, 16, "%f", step);
if (step > 0)
result += wrap("minStep")+":"+tempStr+",";
result += wrap("perms")+":";
result += "[";
if (p & premission_read) result += wrap("pr")+",";
if (p & premission_write) result += wrap("pw")+",";
if (p & premission_notify) result += wrap("ev")+",";
result = result.substr(0, result.size()-1);
result += "]";
result += ",";
snprintf(tempStr, 16, "%X", type);
result += wrap("type")+":"+wrap(tempStr);
result += ",";
snprintf(tempStr, 16, "%hd", acclaim);
result += wrap("iid")+":"+tempStr;
result += ",";
switch (valueUnit) {
case unit_arcDegree:
result += wrap("unit")+":"+wrap("arcdegrees")+",";
break;
case unit_celsius:
result += wrap("unit")+":"+wrap("celsius")+",";
break;
case unit_percentage:
result += wrap("unit")+":"+wrap("percentage")+",";
break;
}
result += "\"format\":\"float\"";
return "{"+result+"}";
}
inline string attribute(unsigned int type, unsigned short acclaim, int p, string value, unsigned short len) {
string result;
char tempStr[4];
if (p & premission_read) {
result += wrap("value")+":"+value.c_str();
result += ",";
}
result += wrap("perms")+":";
result += "[";
if (p & premission_read) result += wrap("pr")+",";
if (p & premission_write) result += wrap("pw")+",";
if (p & premission_notify) result += wrap("ev")+",";
result = result.substr(0, result.size()-1);
result += "]";
result += ",";
snprintf(tempStr, 4, "%X", type);
result += wrap("type")+":"+wrap(tempStr);
result += ",";
snprintf(tempStr, 4, "%hd", acclaim);
result += wrap("iid")+":"+tempStr;
result += ",";
if (len > 0) {
snprintf(tempStr, 4, "%hd", len);
result += wrap("maxLen")+":"+tempStr;
result += ",";
}
result += "\"format\":\"string\"";
return "{"+result+"}";
}
inline string arrayWrap(string *s, unsigned short len) {
string result;
result += "[";
for (int i = 0; i < len; i++) {
result += s[i]+",";
}
result = result.substr(0, result.size()-1);
result += "]";
return result;
}
inline string dictionaryWrap(string *key, string *value, unsigned short len) {
string result;
result += "{";
for (int i = 0; i < len; i++) {
result += wrap(key[i].c_str())+":"+value[i]+",";
}
result = result.substr(0, result.size()-1);
result += "}";
return result;
}
void characteristics::notify() {
char *broadcastTemp = new char[1024];
snprintf(broadcastTemp, 1024, "{\"characteristics\":[{\"aid\": %d, \"iid\": %d, \"value\": %s}]}", accessory->aid, iid, value(NULL).c_str());
broadcastInfo * info = new broadcastInfo;
info->sender = this;
info->desc = broadcastTemp;
pthread_t thread;
pthread_create(&thread, NULL, announce, info);
}
string boolCharacteristics::describe(connectionInfo *sender) {
return attribute(type, iid, premission, value(sender));
}
string floatCharacteristics::describe(connectionInfo *sender) {
return attribute(type, iid, premission, value(sender), _minVal, _maxVal, _step, _unit);
}
string intCharacteristics::describe(connectionInfo *sender) {
return attribute(type, iid, premission, value(sender), _minVal, _maxVal, _step, _unit);
}
string stringCharacteristics::describe(connectionInfo *sender) {
return attribute(type, iid, premission, value(sender), maxLen);
}
string Service::describe(connectionInfo *sender) {
string keys[3] = {"iid", "type", "characteristics"};
string values[3];
{
char temp[8];
snprintf(temp, 8, "%d", serviceID);
values[0] = temp;
}
{
char temp[8];
snprintf(temp, 8, "\"%X\"", uuid);
values[1] = temp;
}
{
int no = numberOfCharacteristics();
string *chars = new string[no];
for (int i = 0; i < no; i++) {
chars[i] = _characteristics[i]->describe(sender);
}
values[2] = arrayWrap(chars, no);
delete [] chars;
}
return dictionaryWrap(keys, values, 3);
}
string Accessory::describe(connectionInfo *sender) {
string keys[2];
string values[2];
{
keys[0] = "aid";
char temp[8];
sprintf(temp, "%d", aid);
values[0] = temp;
}
{
//Form services list
int noOfService = numberOfService();
string *services = new string[noOfService];
for (int i = 0; i < noOfService; i++) {
services[i] = _services[i]->describe(sender);
}
keys[1] = "services";
values[1] = arrayWrap(services, noOfService);
delete [] services;
}
string result = dictionaryWrap(keys, values, 2);
return result;
}
string AccessorySet::describe(connectionInfo *sender) {
int numberOfAcc = numberOfAccessory();
string *desc = new string[numberOfAcc];
for (int i = 0; i < numberOfAcc; i++) {
desc[i] = _accessories[i]->describe(sender);
}
string result = arrayWrap(desc, numberOfAcc);
delete [] desc;
string key = "accessories";
result = dictionaryWrap(&key, &result, 1);
return result;
}
void updateValueFromDeviceEnd(characteristics *c, int aid, int iid, string value) {
c->setValue(value);
char *broadcastTemp = new char[1024];
snprintf(broadcastTemp, 1024, "{\"characteristics\":[{\"aid\":%d,\"iid\":%d,\"value\":%s}]}", aid, iid, value.c_str());
broadcastInfo * info = new broadcastInfo;
info->sender = c;
info->desc = broadcastTemp;
pthread_t thread;
pthread_create(&thread, NULL, announce, info);
}
void handleAccessory(const char *request, unsigned int requestLen, char **reply, unsigned int *replyLen, connectionInfo *sender) {
#if HomeKitLog == 1
printf("Receive request: %s\n", request);
#endif
int index = 5;
char method[5];
{
//Read method
method[4] = 0;
bcopy(request, method, 4);
if (method[3] == ' ') {
method[3] = 0;
index = 4;
}
}
char path[1024];
int i;
for (i = 0; i < 1024 && request[index] != ' '; i++, index++) {
path[i] = request[index];
}
path[i] = 0;
#if HomeKitLog == 1
printf("Path: %s\n", path);
#endif
const char *dataPtr = request;
while (true) {
dataPtr = &dataPtr[1];
if (dataPtr[0] == '\r' && dataPtr[1] == '\n' && dataPtr[2] == '\r' && dataPtr[3] == '\n') break;
}
dataPtr += 4;
char *replyData = NULL; unsigned short replyDataLen = 0;
int statusCode = 0;
const char *protocol = "HTTP/1.1";
const char *returnType = hapJsonType;
if (strcmp(path, "/accessories") == 0) {
//Publish the characterists of the accessories
#if HomeKitLog == 1
printf("Ask for accessories info\n");
#endif
statusCode = 200;
string desc = AccessorySet::getInstance().describe(sender);
replyDataLen = desc.length();
replyData = new char[replyDataLen+1];
bcopy(desc.c_str(), replyData, replyDataLen);
replyData[replyDataLen] = 0;
} else if (strcmp(path, "/pairings") == 0) {
PHKNetworkMessage msg(request);
statusCode = 200;
#if HomeKitLog == 1
printf("%d\n", *msg.data.dataPtrForIndex(0));
#endif
if (*msg.data.dataPtrForIndex(0) == 3) {
//Pairing with new user
printf("Add new user\n");
PHKKeyRecord controllerRec;
bcopy(msg.data.dataPtrForIndex(3), controllerRec.publicKey, 32);
bcopy(msg.data.dataPtrForIndex(1), controllerRec.controllerID, 36);
addControllerKey(controllerRec);
PHKNetworkMessageDataRecord drec;
drec.activate = true; drec.data = new char[1]; *drec.data = 2;
drec.index = 6; drec.length = 1;
PHKNetworkMessageData data;
data.addRecord(drec);
data.rawData((const char **)&replyData, &replyDataLen);
returnType = pairingTlv8Type;
statusCode = 200;
} else {
printf("Delete user");
PHKKeyRecord controllerRec;
bcopy(msg.data.dataPtrForIndex(1), controllerRec.controllerID, 36);
removeControllerKey(controllerRec);
PHKNetworkMessageDataRecord drec;
drec.activate = true; drec.data = new char[1]; *drec.data = 2;
drec.index = 6; drec.length = 1;
PHKNetworkMessageData data;
data.addRecord(drec);
data.rawData((const char **)&replyData, &replyDataLen);
returnType = pairingTlv8Type;
statusCode = 200;
}
//Pairing status change, so update
updatePairable();
} else if (strncmp(path, "/characteristics", 16) == 0){
pthread_mutex_lock(&AccessorySet::getInstance().accessoryMutex);
printf("Characteristics\n");
if (strncmp(method, "GET", 3) == 0) {
//Read characteristics
int aid = 0; int iid = 0;
char indexBuffer[1000];
sscanf(path, "/characteristics?id=%[^\n]", indexBuffer);
printf("Get characteristics %s with len %d\n", indexBuffer, strlen(indexBuffer));
statusCode = 404;
string result = "[";
while (strlen(indexBuffer) > 0) {
printf("Get characteristics %s with len %d\n", indexBuffer, strlen(indexBuffer));
char temp[1000];
//Initial the temp
temp[0] = 0;
sscanf(indexBuffer, "%d.%d%[^\n]", &aid, &iid, temp);
printf("Get temp %s with len %d\n", temp, strlen(temp));
strncpy(indexBuffer, temp, 1000);
printf("Get characteristics %s with len %d\n", indexBuffer, strlen(indexBuffer));
//Trim comma
if (indexBuffer[0] == ',') {
indexBuffer[0] = '0';
}
Accessory *a = AccessorySet::getInstance().accessoryAtIndex(aid);
if (a != NULL) {
characteristics *c = a->characteristicsAtIndex(iid);
if (c != NULL) {
#if HomeKitLog == 1
printf("Ask for one characteristics: %d . %d\n", aid, iid);
#endif
char c1[3], c2[3];
sprintf(c1, "%d", aid);
sprintf(c2, "%d", iid);
string s[3] = {string(c1), string(c2), c->value(sender)};
string k[3] = {"aid", "iid", "value"};
if (result.length() != 1) {
result += ",";
}
string _result = dictionaryWrap(k, s, 3);
result += _result;
}
}
}
result += "]";
string d = "characteristics";
result = dictionaryWrap(&d, &result, 1);
replyDataLen = result.length();
replyData = new char[replyDataLen+1];
replyData[replyDataLen] = 0;
bcopy(result.c_str(), replyData, replyDataLen);
statusCode = 200;
} else if (strncmp(method, "PUT", 3) == 0) {
//Change characteristics
printf("PUT characteristics: \n");
char characteristicsBuffer[1000];
sscanf(dataPtr, "{\"characteristics\":[{%[^]]s}", characteristicsBuffer);
char *buffer2 = characteristicsBuffer;
while (strlen(buffer2) && statusCode != 400) {
bool reachLast = false; bool updateNotify = false;
char *buffer1;
buffer1 = strtok_r(buffer2, "}", &buffer2);
if (*buffer2 != 0) buffer2+=2;
int aid = 0; int iid = 0; char value[16];
int result = sscanf(buffer1, "\"aid\":%d,\"iid\":%d,\"value\":%s", &aid, &iid, value);
if (result == 2) {
sscanf(buffer1, "\"aid\":%d,\"iid\":%d,\"ev\":%s", &aid, &iid, value);
updateNotify = true;
} else if (result == 0) {
sscanf(buffer1, "\"remote\":true,\"value\":%[^,],\"aid\":%d,\"iid\":%d", value, &aid, &iid);
if (result == 2) {
sscanf(buffer1, "\"remote\":true,\"aid\":%d,\"iid\":%d,\"ev\":%s", &aid, &iid, value);
updateNotify = true;
}
sender->relay = true;
}
printf("%d . %d\n",aid, iid);
Accessory *a = AccessorySet::getInstance().accessoryAtIndex(aid);
if (a==NULL) {
statusCode = 400;
} else {
characteristics *c = a->characteristicsAtIndex(iid);
if (updateNotify) {
#if HomeKitLog == 1
printf("Ask to notify one characteristics: %d . %d -> %s\n", aid, iid, value);
#endif
if (c==NULL) {
statusCode = 400;
} else {
if (c->notifiable()) {
if (strncmp(value, "1", 1)==0 || strncmp(value, "true", 4) == 0)
sender->addNotify(c, aid, iid);
else
sender->removeNotify(c);
statusCode = 204;
} else {
statusCode = 400;
}
}
} else {
#if HomeKitLog == 1
printf("Ask to change one characteristics: %d . %d -> %s\n", aid, iid, value);
#endif
if (c==NULL) {
statusCode = 400;
} else {
if (c->writable()) {
c->setValue(value, sender);
char *broadcastTemp = new char[1024];
snprintf(broadcastTemp, 1024, "{\"characteristics\":[{%s}]}", buffer1);
broadcastInfo * info = new broadcastInfo;
info->sender = c;
info->desc = broadcastTemp;
pthread_t thread;
pthread_create(&thread, NULL, announce, info);
statusCode = 204;
} else {
statusCode = 400;
}
}
}
}
}
} else {
return;
}
pthread_mutex_unlock(&AccessorySet::getInstance().accessoryMutex);
} else {
//Error
#if HomeKitLog == 1
printf("Ask for something I don't know\n");
#endif
printf("%s\n", request);
printf("%s", path);
statusCode = 404;
}
//Calculate the length of header
char * tmp = new char[256];
bzero(tmp, 256);
int len = snprintf(tmp, 256, "%s %d OK\r\nContent-Type: %s\r\nContent-Length: %u\r\n\r\n", protocol, statusCode, returnType, replyDataLen);
delete [] tmp;
//replyLen should omit the '\0'.
(*replyLen) = len+replyDataLen;
//reply should add '\0', or the printf is incorrect
*reply = new char[*replyLen + 1];
bzero(*reply, *replyLen + 1);
snprintf(*reply, len + 1, "%s %d OK\r\nContent-Type: %s\r\nContent-Length: %u\r\n\r\n", protocol, statusCode, returnType, replyDataLen);
if (replyData) {
bcopy(replyData, &(*reply)[len], replyDataLen);
delete [] replyData;
}
#if HomeKitLog == 1 && HomeKitReplyHeaderLog==1
printf("Reply: %s\n", *reply);
#endif
}
void addInfoServiceToAccessory(Accessory *acc, string accName, string manufactuerName, string modelName, string serialNumber, identifyFunction identifyCallback) {
Service *infoService = new Service(serviceType_accessoryInfo);
acc->addService(infoService);
stringCharacteristics *accNameCha = new stringCharacteristics(charType_serviceName, premission_read, 0);
accNameCha->characteristics::setValue(accName);
acc->addCharacteristics(infoService, accNameCha);
stringCharacteristics *manNameCha = new stringCharacteristics(charType_manufactuer, premission_read, 0);
manNameCha->characteristics::setValue(manufactuerName);
acc->addCharacteristics(infoService, manNameCha);
stringCharacteristics *modelNameCha = new stringCharacteristics(charType_modelName, premission_read, 0);
modelNameCha->characteristics::setValue(modelName);
acc->addCharacteristics(infoService, modelNameCha);
stringCharacteristics *serialNameCha = new stringCharacteristics(charType_serialNumber, premission_read, 0);
serialNameCha->characteristics::setValue(serialNumber);
acc->addCharacteristics(infoService, serialNameCha);
boolCharacteristics *identify = new boolCharacteristics(charType_identify, premission_write);
identify->characteristics::setValue("false");
identify->valueChangeFunctionCall = identifyCallback;
acc->addCharacteristics(infoService, identify);
}