forked from pilight/pilight
-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon.c
2715 lines (2459 loc) · 76.7 KB
/
daemon.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
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2013 - 2014 CurlyMo
This file is part of pilight.
pilight 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 3 of the License, or (at your option) any later
version.
pilight 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 pilight. If not, see <http://www.gnu.org/licenses/>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#ifdef _WIN32
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <winsvc.h>
#include <shellapi.h>
#define MSG_NOSIGNAL 0
#else
#include <sys/socket.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <arpa/inet.h>
#ifdef __mips__
#define __USE_UNIX98
#endif
#endif
#include <pthread.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include <ctype.h>
#include <dirent.h>
#include "libs/pilight/core/threads.h"
#include "libs/pilight/core/pilight.h"
#include "libs/pilight/core/datetime.h"
#include "libs/pilight/core/common.h"
#include "libs/pilight/core/network.h"
#include "libs/pilight/core/gc.h"
#include "libs/pilight/core/log.h"
#include "libs/pilight/core/options.h"
#include "libs/pilight/core/socket.h"
#include "libs/pilight/core/json.h"
#include "libs/pilight/core/irq.h"
#include "libs/pilight/core/ssdp.h"
#include "libs/pilight/core/dso.h"
#include "libs/pilight/core/firmware.h"
#include "libs/pilight/core/proc.h"
#include "libs/pilight/core/ntp.h"
#include "libs/pilight/core/config.h"
#ifdef EVENTS
#include "libs/pilight/events/events.h"
#endif
#ifdef WEBSERVER
#include "libs/pilight/core/webserver.h"
#endif
#include "libs/pilight/config/hardware.h"
#include "libs/pilight/config/registry.h"
#include "libs/pilight/config/devices.h"
#include "libs/pilight/config/settings.h"
#include "libs/pilight/config/gui.h"
#include "libs/wiringx/wiringX.h"
#ifdef _WIN32
static char server_name[40];
#endif
typedef struct clients_t {
char uuid[UUID_LENGTH];
int id;
int receiver;
int config;
int core;
int stats;
int forward;
char media[8];
double cpu;
double ram;
struct clients_t *next;
} clients_t;
static struct clients_t *clients = NULL;
typedef struct sendqueue_t {
unsigned int id;
char *protoname;
char *settings;
char *message;
enum origin_t origin;
struct protocol_t *protopt;
int code[MAXPULSESTREAMLENGTH];
int length;
char uuid[UUID_LENGTH];
struct sendqueue_t *next;
} sendqueue_t;
static struct sendqueue_t *sendqueue;
static struct sendqueue_t *sendqueue_head;
typedef struct recvqueue_t {
int raw[MAXPULSESTREAMLENGTH];
int rawlen;
int hwtype;
int plslen;
struct recvqueue_t *next;
} recvqueue_t;
static struct recvqueue_t *recvqueue;
static struct recvqueue_t *recvqueue_head;
static pthread_mutex_t sendqueue_lock;
static pthread_cond_t sendqueue_signal;
static pthread_mutexattr_t sendqueue_attr;
static unsigned short sendqueue_init = 0;
static int sendqueue_number = 0;
static int recvqueue_number = 0;
static pthread_mutex_t recvqueue_lock;
static pthread_cond_t recvqueue_signal;
static pthread_mutexattr_t recvqueue_attr;
static unsigned short recvqueue_init = 0;
typedef struct bcqueue_t {
struct JsonNode *jmessage;
char *protoname;
enum origin_t origin;
struct bcqueue_t *next;
} bcqueue_t;
static struct bcqueue_t *bcqueue;
static struct bcqueue_t *bcqueue_head;
static pthread_mutex_t bcqueue_lock;
static pthread_cond_t bcqueue_signal;
static pthread_mutexattr_t bcqueue_attr;
static unsigned short bcqueue_init = 0;
static int bcqueue_number = 0;
static struct protocol_t *procProtocol;
/* The pid_file and pid of this daemon */
#ifndef _WIN32
static char *pid_file;
static unsigned short pid_file_free = 0;
#endif
static pid_t pid;
/* Daemonize or not */
static int nodaemon = 0;
/* Run tracktracer */
static int stacktracer = 0;
/* Run thread profiler */
static int threadprofiler = 0;
/* Are we already running */
static int running = 1;
/* Are we currently sending code */
static int sending = 0;
/* Socket identifier to the server if we are running as client */
static int sockfd = 0;
/* Thread pointers */
static pthread_t logpth;
/* While loop conditions */
static unsigned short main_loop = 1;
/* Reset repeats after a certain amount of time */
static struct timeval tv;
/* Are we running standalone */
static int standalone = 0;
/* What is the minimum rawlenth to consider a pulse stream valid */
static int minrawlen = 1000;
/* What is the maximum rawlenth to consider a pulse stream valid */
static int maxrawlen = 0;
/* What is the minimum rawlenth to consider a pulse stream valid */
static int maxgaplen = 5100;
/* What is the maximum rawlenth to consider a pulse stream valid */
static int mingaplen = 10000;
/* Do we need to connect to a master server:port? */
static char *master_server = NULL;
static unsigned short master_port = 0;
static char *configtmp = NULL;
static int verbosity = LOG_DEBUG;
struct socket_callback_t socket_callback;
#ifdef _WIN32
static int console = 0;
static int oldverbosity = LOG_NOTICE;
#endif
#ifdef WEBSERVER
/* Do we enable the webserver */
static int webserver_enable = WEBSERVER_ENABLE;
static int webgui_websockets = WEBGUI_WEBSOCKETS;
/* On what port does the webserver run */
static int webserver_http_port = WEBSERVER_HTTP_PORT;
/* The webroot of pilight */
static char *webserver_root = NULL;
static int webserver_root_free = 0;
#endif
static void client_remove(int id) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
struct clients_t *currP, *prevP;
prevP = NULL;
for(currP = clients; currP != NULL; prevP = currP, currP = currP->next) {
if(currP->id == id) {
if(prevP == NULL) {
clients = currP->next;
} else {
prevP->next = currP->next;
}
FREE(currP);
break;
}
}
}
static void broadcast_queue(char *protoname, struct JsonNode *json, enum origin_t origin) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
if(main_loop == 1) {
pthread_mutex_lock(&bcqueue_lock);
if(bcqueue_number <= 1024) {
struct bcqueue_t *bnode = MALLOC(sizeof(struct bcqueue_t));
if(bnode == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
char *jstr = json_stringify(json, NULL);
bnode->jmessage = json_decode(jstr);
if(json_find_member(bnode->jmessage, "uuid") == NULL && strlen(pilight_uuid) > 0) {
json_append_member(bnode->jmessage, "uuid", json_mkstring(pilight_uuid));
}
json_free(jstr);
if((bnode->protoname = MALLOC(strlen(protoname)+1)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(bnode->protoname, protoname);
bnode->origin = origin;
if(bcqueue_number == 0) {
bcqueue = bnode;
bcqueue_head = bnode;
} else {
bcqueue_head->next = bnode;
bcqueue_head = bnode;
}
bcqueue_number++;
} else {
logprintf(LOG_ERR, "broadcast queue full");
}
pthread_mutex_unlock(&bcqueue_lock);
pthread_cond_signal(&bcqueue_signal);
}
}
void *broadcast(void *param) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
int broadcasted = 0;
pthread_mutex_lock(&bcqueue_lock);
while(main_loop) {
if(bcqueue_number > 0) {
pthread_mutex_lock(&bcqueue_lock);
logprintf(LOG_STACK, "%s::unlocked", __FUNCTION__);
broadcasted = 0;
struct JsonNode *jret = NULL;
char *origin = NULL;
if(json_find_string(bcqueue->jmessage, "origin", &origin) == 0) {
if(strcmp(origin, "core") == 0) {
double tmp = 0;
json_find_number(bcqueue->jmessage, "type", &tmp);
char *conf = json_stringify(bcqueue->jmessage, NULL);
struct clients_t *tmp_clients = clients;
while(tmp_clients) {
if(((int)tmp < 0 && tmp_clients->core == 1) ||
((int)tmp >= 0 && tmp_clients->config == 1) ||
((int)tmp == PROCESS && tmp_clients->stats == 1)) {
socket_write(tmp_clients->id, conf);
broadcasted = 1;
}
tmp_clients = tmp_clients->next;
}
if(pilight.runmode == ADHOC && sockfd > 0) {
struct JsonNode *jupdate = json_decode(conf);
json_append_member(jupdate, "action", json_mkstring("update"));
char *ret = json_stringify(jupdate, NULL);
socket_write(sockfd, ret);
broadcasted = 1;
json_delete(jupdate);
json_free(ret);
}
if(broadcasted == 1) {
logprintf(LOG_DEBUG, "broadcasted: %s", conf);
}
json_free(conf);
} else {
/* Update the config */
if(devices_update(bcqueue->protoname, bcqueue->jmessage, bcqueue->origin, &jret) == 0) {
char *tmp = json_stringify(jret, NULL);
struct clients_t *tmp_clients = clients;
unsigned short match1 = 0, match2 = 0;
while(tmp_clients) {
if(tmp_clients->config == 1) {
struct JsonNode *jtmp = json_decode(tmp);
struct JsonNode *jdevices = json_find_member(jtmp, "devices");
if(jdevices != NULL) {
match1 = 0;
struct JsonNode *jchilds = json_first_child(jdevices);
struct gui_values_t *gui_values = NULL;
while(jchilds) {
match2 = 0;
if(jchilds->tag == JSON_STRING) {
if((gui_values = gui_media(jchilds->string_)) != NULL) {
while(gui_values) {
if(gui_values->type == JSON_STRING) {
if(strcmp(gui_values->string_, tmp_clients->media) == 0 ||
strcmp(gui_values->string_, "all") == 0 ||
strcmp(tmp_clients->media, "all") == 0) {
match1 = 1;
match2 = 1;
}
}
gui_values = gui_values->next;
}
} else {
match1 = 1;
match2 = 1;
}
}
if(match2 == 0) {
json_remove_from_parent(jchilds);
}
struct JsonNode *jtmp1 = jchilds;
jchilds = jchilds->next;
if(match2 == 0) {
json_delete(jtmp1);
}
}
}
if(match1 == 1) {
char *conf = json_stringify(jtmp, NULL);
socket_write(tmp_clients->id, conf);
logprintf(LOG_DEBUG, "broadcasted: %s", conf);
json_free(conf);
}
json_delete(jtmp);
}
tmp_clients = tmp_clients->next;
}
json_free(tmp);
json_delete(jret);
}
/* The settings objects inside the broadcast queue is only of interest for the
internal pilight functions. For the outside world we only communicate the
message part of the queue so we remove the settings */
char *internal = json_stringify(bcqueue->jmessage, NULL);
struct JsonNode *jsettings = NULL;
if((jsettings = json_find_member(bcqueue->jmessage, "settings"))) {
json_remove_from_parent(jsettings);
json_delete(jsettings);
}
struct JsonNode *tmp = json_find_member(bcqueue->jmessage, "action");
if(tmp != NULL && tmp->tag == JSON_STRING && strcmp(tmp->string_, "update") == 0) {
json_remove_from_parent(tmp);
json_delete(tmp);
}
char *out = json_stringify(bcqueue->jmessage, NULL);
if(strcmp(bcqueue->protoname, "pilight_firmware") == 0) {
struct JsonNode *code = NULL;
if((code = json_find_member(bcqueue->jmessage, "message")) != NULL) {
json_find_number(code, "version", &firmware.version);
json_find_number(code, "lpf", &firmware.lpf);
json_find_number(code, "hpf", &firmware.hpf);
if(firmware.version > 0 && firmware.lpf > 0 && firmware.hpf > 0) {
registry_set_number("pilight.firmware.version", firmware.version, 0);
registry_set_number("pilight.firmware.lpf", firmware.lpf, 0);
registry_set_number("pilight.firmware.hpf", firmware.hpf, 0);
struct JsonNode *jmessage = json_mkobject();
struct JsonNode *jcode = json_mkobject();
json_append_member(jcode, "version", json_mknumber(firmware.version, 0));
json_append_member(jcode, "lpf", json_mknumber(firmware.lpf, 0));
json_append_member(jcode, "hpf", json_mknumber(firmware.hpf, 0));
json_append_member(jmessage, "values", jcode);
json_append_member(jmessage, "origin", json_mkstring("core"));
json_append_member(jmessage, "type", json_mknumber(FIRMWARE, 0));
char pname[17];
strcpy(pname, "pilight-firmware");
pilight.broadcast(pname, jmessage, FW);
json_delete(jmessage);
jmessage = NULL;
}
}
}
broadcasted = 0;
struct JsonNode *childs = json_first_child(bcqueue->jmessage);
int nrchilds = 0;
while(childs) {
nrchilds++;
childs = childs->next;
}
/* Write the message to all receivers */
struct clients_t *tmp_clients = clients;
while(tmp_clients) {
if(tmp_clients->receiver == 1 && tmp_clients->forward == 0) {
if(strcmp(out, "{}") != 0 && nrchilds > 1) {
socket_write(tmp_clients->id, out);
broadcasted = 1;
}
}
tmp_clients = tmp_clients->next;
}
if(pilight.runmode == ADHOC && sockfd > 0) {
struct JsonNode *jupdate = json_decode(internal);
json_append_member(jupdate, "action", json_mkstring("update"));
char *ret = json_stringify(jupdate, NULL);
socket_write(sockfd, ret);
broadcasted = 1;
json_delete(jupdate);
json_free(ret);
}
if((broadcasted == 1 || nodaemon == 1) && (strcmp(out, "{}") != 0 && nrchilds > 1)) {
logprintf(LOG_DEBUG, "broadcasted: %s", out);
}
json_free(internal);
json_free(out);
}
}
struct bcqueue_t *tmp = bcqueue;
FREE(tmp->protoname);
json_delete(tmp->jmessage);
bcqueue = bcqueue->next;
FREE(tmp);
bcqueue_number--;
pthread_mutex_unlock(&bcqueue_lock);
} else {
pthread_cond_wait(&bcqueue_signal, &bcqueue_lock);
}
}
return (void *)NULL;
}
static void receive_queue(int *raw, int rawlen, int plslen, int hwtype) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
int i = 0;
if(main_loop == 1) {
pthread_mutex_lock(&recvqueue_lock);
if(recvqueue_number <= 1024) {
struct recvqueue_t *rnode = MALLOC(sizeof(struct recvqueue_t));
if(rnode == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
for(i=0;i<rawlen;i++) {
rnode->raw[i] = raw[i];
}
rnode->rawlen = rawlen;
rnode->plslen = plslen;
rnode->hwtype = hwtype;
if(recvqueue_number == 0) {
recvqueue = rnode;
recvqueue_head = rnode;
} else {
recvqueue_head->next = rnode;
recvqueue_head = rnode;
}
recvqueue_number++;
} else {
logprintf(LOG_ERR, "receiver queue full");
}
pthread_mutex_unlock(&recvqueue_lock);
pthread_cond_signal(&recvqueue_signal);
}
}
static void receiver_create_message(protocol_t *protocol) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
if(protocol->message != NULL) {
char *valid = json_stringify(protocol->message, NULL);
json_delete(protocol->message);
if(valid != NULL && json_validate(valid) == true) {
struct JsonNode *jmessage = json_mkobject();
json_append_member(jmessage, "message", json_decode(valid));
json_append_member(jmessage, "origin", json_mkstring("receiver"));
json_append_member(jmessage, "protocol", json_mkstring(protocol->id));
if(strlen(pilight_uuid) > 0) {
json_append_member(jmessage, "uuid", json_mkstring(pilight_uuid));
}
if(protocol->repeats > -1) {
json_append_member(jmessage, "repeats", json_mknumber(protocol->repeats, 0));
}
char *output = json_stringify(jmessage, NULL);
struct JsonNode *json = json_decode(output);
broadcast_queue(protocol->id, json, RECEIVER);
json_free(output);
json_delete(json);
json = NULL;
json_delete(jmessage);
}
json_free(valid);
}
protocol->message = NULL;
}
void *receive_parse_code(void *param) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
pthread_mutex_lock(&recvqueue_lock);
while(main_loop) {
if(recvqueue_number > 0) {
pthread_mutex_lock(&recvqueue_lock);
logprintf(LOG_STACK, "%s::unlocked", __FUNCTION__);
struct protocol_t *protocol = NULL;
struct protocols_t *pnode = protocols;
while(pnode != NULL && main_loop) {
protocol = pnode->listener;
if((protocol->hwtype == recvqueue->hwtype || protocol->hwtype == -1 || recvqueue->hwtype == -1) &&
(protocol->parseCode != NULL && protocol->validate != NULL)) {
if(recvqueue->rawlen < MAXPULSESTREAMLENGTH) {
protocol->raw = recvqueue->raw;
}
protocol->rawlen = recvqueue->rawlen;
if(protocol->validate() == 0) {
logprintf(LOG_DEBUG, "possible %s protocol", protocol->id);
gettimeofday(&tv, NULL);
if(protocol->first > 0) {
protocol->first = protocol->second;
}
protocol->second = 1000000 * (unsigned int)tv.tv_sec + (unsigned int)tv.tv_usec;
if(protocol->first == 0) {
protocol->first = protocol->second;
}
/* Reset # of repeats after a certain delay */
if(((int)protocol->second-(int)protocol->first) > 500000) {
protocol->repeats = 0;
}
protocol->repeats++;
if(protocol->parseCode != NULL) {
logprintf(LOG_DEBUG, "recevied pulse length of %d", recvqueue->plslen);
logprintf(LOG_DEBUG, "caught minimum # of repeats %d of %s", protocol->repeats, protocol->id);
logprintf(LOG_DEBUG, "called %s parseRaw()", protocol->id);
protocol->parseCode();
receiver_create_message(protocol);
}
}
}
pnode = pnode->next;
}
struct recvqueue_t *tmp = recvqueue;
recvqueue = recvqueue->next;
FREE(tmp);
recvqueue_number--;
pthread_mutex_unlock(&recvqueue_lock);
} else {
pthread_cond_wait(&recvqueue_signal, &recvqueue_lock);
}
}
return (void *)NULL;
}
void *send_code(void *param) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
int i = 0;
/* Make sure the pilight sender gets
the highest priority available */
#ifdef _WIN32
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
#else
struct sched_param sched;
memset(&sched, 0, sizeof(sched));
sched.sched_priority = 80;
pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched);
#endif
pthread_mutex_lock(&sendqueue_lock);
while(main_loop) {
if(sendqueue_number > 0) {
pthread_mutex_lock(&sendqueue_lock);
logprintf(LOG_STACK, "%s::unlocked", __FUNCTION__);
sending = 1;
struct protocol_t *protocol = sendqueue->protopt;
struct hardware_t *hw = NULL;
struct JsonNode *message = NULL;
if(sendqueue->message != NULL && strcmp(sendqueue->message, "{}") != 0) {
if(json_validate(sendqueue->message) == true) {
if(message == NULL) {
message = json_mkobject();
}
json_append_member(message, "origin", json_mkstring("sender"));
json_append_member(message, "protocol", json_mkstring(protocol->id));
json_append_member(message, "message", json_decode(sendqueue->message));
if(strlen(sendqueue->uuid) > 0) {
json_append_member(message, "uuid", json_mkstring(sendqueue->uuid));
}
json_append_member(message, "repeat", json_mknumber(1, 0));
}
}
if(sendqueue->settings != NULL && strcmp(sendqueue->settings, "{}") != 0) {
if(json_validate(sendqueue->settings) == true) {
if(message == NULL) {
message = json_mkobject();
}
json_append_member(message, "settings", json_decode(sendqueue->settings));
}
}
struct conf_hardware_t *tmp_confhw = conf_hardware;
while(tmp_confhw) {
if(protocol->hwtype == tmp_confhw->hardware->hwtype) {
hw = tmp_confhw->hardware;
break;
}
tmp_confhw = tmp_confhw->next;
}
if(hw != NULL && hw->send != NULL) {
if(hw->receiveOOK != NULL || hw->receivePulseTrain != NULL) {
hw->wait = 1;
pthread_mutex_unlock(&hw->lock);
pthread_cond_signal(&hw->signal);
}
logprintf(LOG_DEBUG, "**** RAW CODE ****");
if(log_level_get() >= LOG_DEBUG) {
for(i=0;i<sendqueue->length;i++) {
printf("%d ", sendqueue->code[i]);
}
printf("\n");
}
logprintf(LOG_DEBUG, "**** RAW CODE ****");
if(hw->send(sendqueue->code, sendqueue->length, protocol->txrpt) == 0) {
logprintf(LOG_DEBUG, "successfully send %s code", protocol->id);
} else {
logprintf(LOG_ERR, "failed to send code");
}
if(strcmp(protocol->id, "raw") == 0) {
int plslen = sendqueue->code[sendqueue->length-1]/PULSE_DIV;
receive_queue(sendqueue->code, sendqueue->length, plslen, -1);
}
if(hw->receiveOOK != NULL || hw->receivePulseTrain != NULL) {
hw->wait = 0;
pthread_mutex_unlock(&hw->lock);
pthread_cond_signal(&hw->signal);
}
} else {
if(strcmp(protocol->id, "raw") == 0) {
int plslen = sendqueue->code[sendqueue->length-1]/PULSE_DIV;
receive_queue(sendqueue->code, sendqueue->length, plslen, -1);
}
}
if(message != NULL) {
broadcast_queue(sendqueue->protoname, message, sendqueue->origin);
json_delete(message);
message = NULL;
}
struct sendqueue_t *tmp = sendqueue;
if(tmp->message != NULL) {
FREE(tmp->message);
}
if(tmp->settings != NULL) {
FREE(tmp->settings);
}
FREE(tmp->protoname);
sendqueue = sendqueue->next;
FREE(tmp);
sendqueue_number--;
sending = 0;
pthread_mutex_unlock(&sendqueue_lock);
} else {
pthread_cond_wait(&sendqueue_signal, &sendqueue_lock);
}
}
return (void *)NULL;
}
/* Send a specific code */
static int send_queue(struct JsonNode *json, enum origin_t origin) {
pthread_mutex_lock(&sendqueue_lock);
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
int match = 0, raw[MAXPULSESTREAMLENGTH-1];
struct timeval tcurrent;
struct clients_t *tmp_clients = NULL;
char *uuid = NULL, *buffer = NULL;
/* Hold the final protocol struct */
struct protocol_t *protocol = NULL;
#ifdef _WIN32
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST);
#else
/* Make sure the pilight sender gets
the highest priority available */
struct sched_param sched;
memset(&sched, 0, sizeof(sched));
sched.sched_priority = 80;
pthread_setschedparam(pthread_self(), SCHED_FIFO, &sched);
#endif
struct JsonNode *jcode = NULL;
struct JsonNode *jprotocols = NULL;
struct JsonNode *jprotocol = NULL;
buffer = json_stringify(json, NULL);
tmp_clients = clients;
while(tmp_clients) {
if(tmp_clients->forward == 1) {
socket_write(tmp_clients->id, buffer);
}
tmp_clients = tmp_clients->next;
}
json_free(buffer);
if((jcode = json_find_member(json, "code")) == NULL) {
logprintf(LOG_ERR, "sender did not send any codes");
json_delete(jcode);
pthread_mutex_unlock(&sendqueue_lock);
return -1;
} else if((jprotocols = json_find_member(jcode, "protocol")) == NULL) {
logprintf(LOG_ERR, "sender did not provide a protocol name");
json_delete(jcode);
pthread_mutex_unlock(&sendqueue_lock);
return -1;
} else {
json_find_string(jcode, "uuid", &uuid);
/* If we matched a protocol and are not already sending, continue */
if(uuid == NULL || (uuid != NULL && strcmp(uuid, pilight_uuid) == 0)) {
jprotocol = json_first_child(jprotocols);
while(jprotocol && match == 0) {
match = 0;
if(jprotocol->tag == JSON_STRING) {
struct protocols_t *pnode = protocols;
/* Retrieve the used protocol */
while(pnode) {
protocol = pnode->listener;
/* Check if the protocol exists */
if(protocol_device_exists(protocol, jprotocol->string_) == 0 && match == 0) {
match = 1;
break;
}
pnode = pnode->next;
}
}
jprotocol = jprotocol->next;
}
memset(raw, 0, MAXPULSESTREAMLENGTH-1);
protocol->raw = raw;
if(match == 1 && protocol->createCode != NULL) {
/* Let the protocol create his code */
if(protocol->createCode(jcode) == 0 && main_loop == 1) {
if(sendqueue_number <= 1024) {
struct sendqueue_t *mnode = MALLOC(sizeof(struct sendqueue_t));
if(mnode == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
gettimeofday(&tcurrent, NULL);
mnode->origin = origin;
mnode->id = 1000000 * (unsigned int)tcurrent.tv_sec + (unsigned int)tcurrent.tv_usec;
mnode->message = NULL;
if(protocol->message != NULL) {
char *jsonstr = json_stringify(protocol->message, NULL);
json_delete(protocol->message);
if(json_validate(jsonstr) == true) {
if((mnode->message = MALLOC(strlen(jsonstr)+1)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(mnode->message, jsonstr);
}
json_free(jsonstr);
protocol->message = NULL;
}
mnode->length = protocol->rawlen;
memcpy(mnode->code, protocol->raw, sizeof(int)*protocol->rawlen);
if((mnode->protoname = MALLOC(strlen(protocol->id)+1)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(mnode->protoname, protocol->id);
mnode->protopt = protocol;
struct options_t *tmp_options = protocol->options;
char *stmp = NULL;
struct JsonNode *jsettings = json_mkobject();
struct JsonNode *jtmp = NULL;
while(tmp_options) {
if(tmp_options->conftype == DEVICES_SETTING) {
if(tmp_options->vartype == JSON_NUMBER &&
(jtmp = json_find_member(jcode, tmp_options->name)) != NULL &&
jtmp->tag == JSON_NUMBER) {
json_append_member(jsettings, tmp_options->name, json_mknumber(jtmp->number_, jtmp->decimals_));
} else if(tmp_options->vartype == JSON_STRING && json_find_string(jcode, tmp_options->name, &stmp) == 0) {
json_append_member(jsettings, tmp_options->name, json_mkstring(stmp));
}
}
tmp_options = tmp_options->next;
}
char *strsett = json_stringify(jsettings, NULL);
if((mnode->settings = MALLOC(strlen(strsett)+1)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
strcpy(mnode->settings, strsett);
json_free(strsett);
json_delete(jsettings);
if(uuid != NULL) {
strcpy(mnode->uuid, uuid);
} else {
memset(mnode->uuid, '\0', UUID_LENGTH);
}
if(sendqueue_number == 0) {
sendqueue = mnode;
sendqueue_head = mnode;
} else {
sendqueue_head->next = mnode;
sendqueue_head = mnode;
}
sendqueue_number++;
} else {
logprintf(LOG_ERR, "send queue full");
pthread_mutex_unlock(&sendqueue_lock);
return -1;
}
pthread_mutex_unlock(&sendqueue_lock);
pthread_cond_signal(&sendqueue_signal);
return 0;
} else {
pthread_mutex_unlock(&sendqueue_lock);
return -1;
}
}
} else {
pthread_mutex_unlock(&sendqueue_lock);
return 0;
}
}
pthread_mutex_unlock(&sendqueue_lock);
return -1;
}
#ifdef WEBSERVER
static void client_webserver_parse_code(int i, char buffer[BUFFER_SIZE]) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
int sd = socket_get_clients(i);
int x = 0;
FILE *f;
unsigned char *p = NULL;
unsigned char buff[BUFFER_SIZE];
char *cache = NULL;
char *path = NULL, buf[INET_ADDRSTRLEN+1];
char *mimetype = NULL;
struct stat sb;
struct sockaddr_in sockin;
socklen_t len = sizeof(sockin);
if(getsockname(sd, (struct sockaddr *)&sockin, &len) == -1) {
logprintf(LOG_ERR, "could not determine server ip address");
} else {
if(strstr(buffer, " HTTP/") == NULL) {
return;
}
p = buff;
if(strstr(buffer, "/logo.png") != NULL) {
if((path = MALLOC(strlen(webserver_root)+strlen("logo.png")+2)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
sprintf(path, "%s/logo.png", webserver_root);
if((f = fopen(path, "rb"))) {
fstat(fileno(f), &sb);
mimetype = webserver_mimetype("image/png");
webserver_create_header(&p, "200 OK", mimetype, (unsigned int)sb.st_size);
send(sd, (const char *)buff, (size_t)(p-buff), MSG_NOSIGNAL);
x = 0;
if((cache = MALLOC(BUFFER_SIZE)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
memset(cache, '\0', BUFFER_SIZE);
while(!feof(f)) {
x = (int)fread(cache, 1, BUFFER_SIZE, f);
send(sd, (const char *)cache, (size_t)x, MSG_NOSIGNAL);
}
fclose(f);
FREE(cache);
FREE(mimetype);
} else {
logprintf(LOG_WARNING, "pilight logo not found");
}
FREE(path);
} else {
/* Catch all webserver page to inform users on which port the webserver runs */
mimetype = webserver_mimetype("text/html");
webserver_create_header(&p, "200 OK", mimetype, (unsigned int)BUFFER_SIZE);
send(sd, (const char *)buff, (size_t)(p-buff), MSG_NOSIGNAL);
if(webserver_enable == 1) {
if((cache = MALLOC(BUFFER_SIZE)) == NULL) {
fprintf(stderr, "out of memory\n");
exit(EXIT_FAILURE);
}
memset(cache, '\0', BUFFER_SIZE);
memset(&buf, '\0', INET_ADDRSTRLEN+1);
inet_ntop(AF_INET, (void *)&(sockin.sin_addr), buf, INET_ADDRSTRLEN+1);
sprintf(cache, "<html><head><title>pilight</title></head>"
"<body><center><img src=\"logo.png\"><br />"
"<p style=\"color: #0099ff; font-weight: 800px;"
"font-family: Verdana; font-size: 20px;\">"
"The pilight webgui is located at "
"<a style=\"text-decoration: none; color: #0099ff;"
"font-weight: 800px; font-family: Verdana; font-size:"
"20px;\" href=\"http://%s:%d\">http://%s:%d</a></p>"
"</center></body></html>",
buf, webserver_http_port, buf, webserver_http_port);
send(sd, (const char *)cache, strlen(cache), MSG_NOSIGNAL);
FREE(cache);
} else {
send(sd, "<body><center><img src=\"logo.png\"></center></body></html>", 57, MSG_NOSIGNAL);
}
FREE(mimetype);
}
}
}
#endif
static int control_device(struct devices_t *dev, char *state, struct JsonNode *values, enum origin_t origin) {
logprintf(LOG_STACK, "%s(...)", __FUNCTION__);
struct devices_settings_t *sett = NULL;