-
Notifications
You must be signed in to change notification settings - Fork 15
/
jsonrpc.c
1255 lines (1140 loc) · 37.6 KB
/
jsonrpc.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) 2014, 2015, Louis Opter <[email protected]>
//
// This file is part of lighstd.
//
// lighstd 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.
//
// lighstd 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 lighstd. If not, see <http://www.gnu.org/licenses/>.
#include <sys/queue.h>
#include <sys/tree.h>
#include <arpa/inet.h>
#include <assert.h>
#include <ctype.h>
#include <endian.h>
#include <err.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <event2/bufferevent.h>
#include <event2/util.h>
#include "lifx/wire_proto.h"
#include "jsmn.h"
#include "jsonrpc.h"
#include "client.h"
#include "proto.h"
#include "lightsd.h"
static bool
lgtd_jsonrpc_type_integer(const jsmntok_t *t, const char *json)
{
if (t->type != JSMN_PRIMITIVE) {
return false;
}
const char *endptr = NULL;
errno = 0;
strtol(&json[t->start], (char **)&endptr, 10);
return endptr == json + t->end && errno != ERANGE;
}
static bool
lgtd_jsonrpc_type_float_between_0_and_1(const jsmntok_t *t,
const char *json)
{
if (t->type != JSMN_PRIMITIVE) {
return false;
}
int i = t->start;
bool dot_seen = false;
bool first_digit_is_one = false;
for (; i < t->end; i++) {
if (json[i] == '.') {
if (dot_seen) {
return false;
}
dot_seen = true;
} else if (dot_seen) {
if (json[i] < '0' || json[i] > '9') {
return false;
} if (first_digit_is_one && json[i] != '0') {
return false;
}
} else {
if (first_digit_is_one) {
return false;
} else if (json[i] == '1') {
first_digit_is_one = true;
} else if (json[i] != '0') {
return false;
}
}
}
return true;
}
static bool
lgtd_jsonrpc_type_float_between_0_and_360(const jsmntok_t *t,
const char *json)
{
if (t->type != JSMN_PRIMITIVE) {
return false;
}
char c = json[t->start];
if (c != '-' && (c > '9' || c < '0')) {
return false;
}
const char *endptr = NULL;
errno = 0;
long intpart = strtol(&json[t->start], (char **)&endptr, 10);
if ((endptr != json + t->end && *endptr != '.')
|| errno == ERANGE || intpart < 0 || intpart > 360
|| (intpart == 0 && c == '-')) {
return false;
}
if (endptr == json + t->end) {
return true;
}
long fracpart = strtol(++endptr, (char **)&endptr, 10);
return endptr == json + t->end && errno != ERANGE
&& fracpart >= 0 && (intpart < 360 || fracpart == 0);
}
static bool
lgtd_jsonrpc_type_number(const jsmntok_t *t, const char *json)
{
if (t->type != JSMN_PRIMITIVE) {
return false;
}
char c = json[t->start];
return c == '-' || (c >= '0' && c <= '9');
}
static bool
lgtd_jsonrpc_type_bool(const jsmntok_t *t, const char *json)
{
if (t->type != JSMN_PRIMITIVE) {
return false;
}
char c = json[t->start];
return c == 't' || c == 'f';
}
static bool
lgtd_jsonrpc_type_null(const jsmntok_t *t, const char *json)
{
return memcmp(
&json[t->start], "null", LGTD_MIN(t->size, (int)sizeof("null"))
);
}
static bool
lgtd_jsonrpc_type_string(const jsmntok_t *t, const char *json)
{
(void)json;
return t->type == JSMN_STRING;
}
static bool
lgtd_jsonrpc_type_array(const jsmntok_t *t, const char *json)
{
(void)json;
return t->type == JSMN_ARRAY;
}
static bool
lgtd_jsonrpc_type_object(const jsmntok_t *t, const char *json)
{
(void)json;
return t->type == JSMN_OBJECT;
}
static bool
lgtd_jsonrpc_type_object_or_array(const jsmntok_t *t, const char *json)
{
(void)json;
return t->type == JSMN_OBJECT || t->type == JSMN_ARRAY;
}
static bool
lgtd_jsonrpc_type_string_number_or_null(const jsmntok_t *t,
const char *json)
{
return lgtd_jsonrpc_type_number(t, json)
|| lgtd_jsonrpc_type_null(t, json)
|| lgtd_jsonrpc_type_string(t, json);
}
static bool
lgtd_jsonrpc_type_string_or_number(const jsmntok_t *t,
const char *json)
{
return lgtd_jsonrpc_type_string(t, json)
|| lgtd_jsonrpc_type_number(t, json);
}
static bool __attribute__((unused))
lgtd_jsonrpc_type_string_or_array(const jsmntok_t *t, const char *json)
{
return lgtd_jsonrpc_type_string(t, json)
|| lgtd_jsonrpc_type_array(t, json);
}
static bool
lgtd_jsonrpc_type_string_number_or_array(const jsmntok_t *t, const char *json)
{
return lgtd_jsonrpc_type_string(t, json)
|| lgtd_jsonrpc_type_number(t, json)
|| lgtd_jsonrpc_type_array(t, json);
}
static int
lgtd_jsonrpc_float_range_to_uint16(const char *s, int len, int start, int stop)
{
assert(s);
assert(len > 0);
assert(start < stop);
int range;
range = stop * LGTD_JSONRPC_FLOAT_PREC - start * LGTD_JSONRPC_FLOAT_PREC;
const char *dot = NULL;
long fracpart = 0;
long intpart = strtol(s, (char **)&dot, 10) * LGTD_JSONRPC_FLOAT_PREC;
if (dot - s != len && *dot == '.') {
for (int i = dot - s + 1, multiplier = LGTD_JSONRPC_FLOAT_PREC / 10;
i != len && multiplier != 0;
i++, multiplier /= 10) {
fracpart += (s[i] - '0') * multiplier;
}
}
return ((int64_t)intpart + (int64_t)fracpart) * UINT16_MAX / (int64_t)range;
}
void
lgtd_jsonrpc_uint16_range_to_float_string(uint16_t encoded, int start, int stop,
char *out, int size)
{
assert(out);
assert(size > 1);
assert(start < stop);
if (size < 2) {
if (size) {
*out = '\0';
}
return;
}
int range;
range = stop * LGTD_JSONRPC_FLOAT_PREC - start * LGTD_JSONRPC_FLOAT_PREC;
int value = (uint64_t)encoded * (uint64_t)range / UINT16_MAX;
if (!value) {
out[0] = '0';
out[1] = '\0';
return;
}
int multiplier = 1;
while (value / (multiplier * 10)) {
multiplier *= 10;
}
int i = 0;
if (LGTD_JSONRPC_FLOAT_PREC / 10 > multiplier) {
out[i++] = '0';
if (i != size) {
out[i++] = '.';
}
for (int divider = 10;
LGTD_JSONRPC_FLOAT_PREC / divider > multiplier && i != size;
divider *= 10) {
out[i++] = '0';
}
}
do {
if (multiplier == LGTD_JSONRPC_FLOAT_PREC / 10) {
if (i == 0) {
out[i++] = '0';
}
if (i != size) {
out[i++] = '.';
}
}
if (i != size) {
out[i++] = '0' + value / multiplier;
}
value -= value / multiplier * multiplier;
multiplier /= 10;
} while ((value || multiplier >= LGTD_JSONRPC_FLOAT_PREC)
&& multiplier && i != size);
out[LGTD_MIN(i, size - 1)] = '\0';
assert(i <= size);
}
static int
lgtd_jsonrpc_consume_object_or_array(const jsmntok_t *tokens,
int ti,
int parsed,
const char *json)
{
assert(lgtd_jsonrpc_type_object_or_array(&tokens[ti], json));
assert(ti < parsed);
jsmntype_t container_type = tokens[ti].type;
int objsize = tokens[ti++].size;
while (objsize-- && ti < parsed) {
if (container_type == JSMN_OBJECT) {
ti++; // move to the value
}
if (lgtd_jsonrpc_type_object_or_array(&tokens[ti], json)) {
ti = lgtd_jsonrpc_consume_object_or_array(tokens, ti, parsed, json);
} else {
ti++;
}
}
return ti;
}
static bool
lgtd_jsonrpc_extract_values_from_schema_and_dict(void *output,
const struct lgtd_jsonrpc_node *schema,
int schema_size,
const jsmntok_t *tokens,
int ntokens,
const char *json)
{
if (!ntokens || tokens[0].type != JSMN_OBJECT) {
return false;
}
for (int ti = 1; ti < ntokens;) {
// make sure it's a key, otherwise we reached the end of the object:
if (tokens[ti].type != JSMN_STRING) {
break;
}
int si = 0;
for (;; si++) {
if (si == schema_size) {
ti++; // nothing matched, skip the key
break;
}
int tokenlen = LGTD_JSONRPC_TOKEN_LEN(&tokens[ti]);
if (schema[si].keylen != tokenlen) {
continue;
}
int diff = memcmp(
schema[si].key, &json[tokens[ti].start], tokenlen
);
if (!diff) {
ti++; // keys looks good, move to the value
if (!schema[si].type_cmp(&tokens[ti], json)) {
lgtd_debug(
"jsonrpc client sent an invalid value for %s",
schema[si].key
);
return false;
}
if (schema[si].value_offset != -1) {
const jsmntok_t *seen = LGTD_JSONRPC_GET_JSMNTOK(
output, schema[si].value_offset
);
if (seen) { // duplicate key
lgtd_debug(
"jsonrpc client sent duplicate parameter %s",
schema[si].key
);
return false;
}
LGTD_JSONRPC_SET_JSMNTOK(
output, schema[si].value_offset, &tokens[ti]
);
}
break;
}
}
// skip the value, if it's an object or an array we need to
// skip everything in it:
int value_ntokens = ti;
if (tokens[ti].type == JSMN_OBJECT || tokens[ti].type == JSMN_ARRAY) {
ti = lgtd_jsonrpc_consume_object_or_array(
tokens, ti, ntokens, json
);
} else {
ti++;
}
value_ntokens = ti - value_ntokens;
if (si < schema_size && schema[si].ntokens_offset != -1) {
LGTD_JSONRPC_SET_NTOKENS(
output, schema[si].ntokens_offset, value_ntokens
);
}
}
for (int si = 0; si != schema_size; si++) {
if (!schema[si].optional && schema[si].value_offset != -1) {
const jsmntok_t *seen = LGTD_JSONRPC_GET_JSMNTOK(
output, schema[si].value_offset
);
if (!seen) {
lgtd_debug("missing jsonrpc parameter %s", schema[si].key);
return false;
}
lgtd_debug("got jsonrpc parameter %s", schema[si].key);
}
}
return true;
}
static bool
lgtd_jsonrpc_extract_values_from_schema_and_array(void *output,
const struct lgtd_jsonrpc_node *schema,
int schema_size,
const jsmntok_t *tokens,
int ntokens,
const char *json)
{
if (!ntokens || tokens[0].type != JSMN_ARRAY) {
return false;
}
int si, ti, objsize = tokens[0].size;
for (si = 0, ti = 1; si < schema_size && ti < ntokens && objsize--; si++) {
if (!schema[si].type_cmp(&tokens[ti], json)) {
lgtd_debug(
"jsonrpc client sent an invalid value for %s",
schema[si].key
);
return false;
}
if (schema[si].value_offset != -1) {
LGTD_JSONRPC_SET_JSMNTOK(
output, schema[si].value_offset, &tokens[ti]
);
}
// skip the value, if it's an object or an array we need to
// skip everything in it:
int value_ntokens = ti;
if (tokens[ti].type == JSMN_OBJECT || tokens[ti].type == JSMN_ARRAY) {
ti = lgtd_jsonrpc_consume_object_or_array(
tokens, ti, ntokens, json
);
} else {
ti++;
}
value_ntokens = ti - value_ntokens;
if (schema[si].ntokens_offset != -1) {
LGTD_JSONRPC_SET_NTOKENS(
output, schema[si].ntokens_offset, value_ntokens
);
}
}
return !objsize || (si < schema_size && schema[si].optional);
}
static bool
lgtd_jsonrpc_extract_and_validate_params_against_schema(void *output,
const struct lgtd_jsonrpc_node *schema,
int schema_size,
const jsmntok_t *tokens,
int ntokens,
const char *json)
{
if (!ntokens) {
// "params" were omitted, make sure no args were required or that they
// are all optional:
while (schema_size--) {
if (!schema[schema_size].optional) {
return false;
}
}
return true;
}
switch (tokens[0].type) {
case JSMN_OBJECT:
return lgtd_jsonrpc_extract_values_from_schema_and_dict(
output, schema, schema_size, tokens, ntokens, json
);
case JSMN_ARRAY:
return lgtd_jsonrpc_extract_values_from_schema_and_array(
output, schema, schema_size, tokens, ntokens, json
);
default:
return false;
}
}
static void
lgtd_jsonrpc_write_id(struct lgtd_client *client)
{
if (!client->current_request || !client->current_request->id) {
lgtd_client_write_string(client, "null");
return;
}
int start, stop;
if (client->current_request->id->type == JSMN_STRING) { // get the quotes
start = client->current_request->id->start - 1;
stop = client->current_request->id->end + 1;
} else {
start = client->current_request->id->start;
stop = client->current_request->id->end;
}
lgtd_client_write_buf(client, &client->json[start], stop - start);
}
void
lgtd_jsonrpc_send_error(struct lgtd_client *client,
enum lgtd_jsonrpc_error_code code,
const char *message)
{
assert(client);
assert(message);
lgtd_client_write_string(client, "{\"jsonrpc\": \"2.0\", \"id\": ");
lgtd_jsonrpc_write_id(client);
lgtd_client_write_string(client, ", \"error\": {\"code\": ");
char str_code[8] = { 0 };
snprintf(str_code, sizeof(str_code), "%d", code);
lgtd_client_write_string(client, str_code);
lgtd_client_write_string(client, ", \"message\": \"");
lgtd_client_write_string(client, message);
lgtd_client_write_string(client, "\"}}");
}
void
lgtd_jsonrpc_send_response(struct lgtd_client *client,
const char *result)
{
assert(client);
assert(result);
lgtd_client_write_string(client, "{\"jsonrpc\": \"2.0\", \"id\": ");
lgtd_jsonrpc_write_id(client);
lgtd_client_write_string(client, ", \"result\": ");
lgtd_client_write_string(client, result);
lgtd_client_write_string(client, "}");
}
void
lgtd_jsonrpc_start_send_response(struct lgtd_client *client)
{
assert(client);
lgtd_client_write_string(client, "{\"jsonrpc\": \"2.0\", \"id\": ");
lgtd_jsonrpc_write_id(client);
lgtd_client_write_string(client, ", \"result\": ");
}
void
lgtd_jsonrpc_end_send_response(struct lgtd_client *client)
{
lgtd_client_write_string(client, "}");
}
static bool
lgtd_jsonrpc_check_and_extract_request(struct lgtd_jsonrpc_request *request,
const jsmntok_t *tokens,
int ntokens,
const char *json)
{
static const struct lgtd_jsonrpc_node request_schema[] = {
LGTD_JSONRPC_NODE(
"jsonrpc", -1, -1, lgtd_jsonrpc_type_string, false
),
LGTD_JSONRPC_NODE(
"method",
offsetof(struct lgtd_jsonrpc_request, method),
-1,
lgtd_jsonrpc_type_string,
false
),
LGTD_JSONRPC_NODE(
"params",
offsetof(struct lgtd_jsonrpc_request, params),
offsetof(struct lgtd_jsonrpc_request, params_ntokens),
lgtd_jsonrpc_type_object_or_array,
true
),
LGTD_JSONRPC_NODE(
"id",
offsetof(struct lgtd_jsonrpc_request, id),
-1,
lgtd_jsonrpc_type_string_number_or_null,
true
)
};
bool ok = lgtd_jsonrpc_extract_values_from_schema_and_dict(
request,
request_schema,
LGTD_ARRAY_SIZE(request_schema),
tokens,
ntokens,
json
);
if (!ok) {
return false;
}
request->request_ntokens = 1 + 2 + 2; // dict itself + jsonrpc + method
if (request->params) {
request->request_ntokens += 1 + request->params_ntokens;
}
if (request->id) {
request->request_ntokens += 2;
}
return true;
}
static bool
lgtd_jsonrpc_build_target_list(struct lgtd_proto_target_list *targets,
struct lgtd_client *client,
const jsmntok_t *target,
int target_ntokens)
{
assert(targets);
assert(client);
assert(target);
if (target_ntokens < 1) {
return false;
}
if (lgtd_jsonrpc_type_array(target, client->json)) {
target_ntokens -= 1;
target++;
} else if (target_ntokens != 1) {
return false;
}
for (int ti = target_ntokens; ti--;) {
int token_len = LGTD_JSONRPC_TOKEN_LEN(&target[ti]);
if (lgtd_jsonrpc_type_string_or_number(&target[ti], client->json)) {
struct lgtd_proto_target *t = malloc(sizeof(*t) + token_len + 1);
if (!t) {
lgtd_warn("can't allocate a new target");
lgtd_jsonrpc_send_error(
client, LGTD_JSONRPC_INTERNAL_ERROR, "Can't allocate memory"
);
goto error;
}
memcpy(t->target, client->json + target[ti].start, token_len);
t->target[token_len] = '\0';
SLIST_INSERT_HEAD(targets, t, link);
} else {
lgtd_debug(
"invalid target value %.*s",
token_len,
client->json + target[ti].start
);
lgtd_jsonrpc_send_error(
client, LGTD_JSONRPC_INVALID_PARAMS, "Invalid parameters"
);
goto error;
}
}
return true;
error:
lgtd_proto_target_list_clear(targets);
return false;
}
static void
lgtd_jsonrpc_check_and_call_set_light_from_hsbk(struct lgtd_client *client)
{
struct lgtd_jsonrpc_set_light_from_hsbk_args {
const jsmntok_t *target;
int target_ntokens;
const jsmntok_t *h;
const jsmntok_t *s;
const jsmntok_t *b;
const jsmntok_t *k;
const jsmntok_t *t;
} params = { NULL, 0, NULL, NULL, NULL, NULL, NULL };
static const struct lgtd_jsonrpc_node schema[] = {
LGTD_JSONRPC_NODE(
"target",
offsetof(struct lgtd_jsonrpc_set_light_from_hsbk_args, target),
offsetof(struct lgtd_jsonrpc_set_light_from_hsbk_args, target_ntokens),
lgtd_jsonrpc_type_string_number_or_array,
false
),
LGTD_JSONRPC_NODE(
"hue",
offsetof(struct lgtd_jsonrpc_set_light_from_hsbk_args, h),
-1,
lgtd_jsonrpc_type_float_between_0_and_360,
false
),
LGTD_JSONRPC_NODE(
"saturation",
offsetof(struct lgtd_jsonrpc_set_light_from_hsbk_args, s),
-1,
lgtd_jsonrpc_type_float_between_0_and_1,
false
),
LGTD_JSONRPC_NODE(
"brightness",
offsetof(struct lgtd_jsonrpc_set_light_from_hsbk_args, b),
-1,
lgtd_jsonrpc_type_float_between_0_and_1,
false
),
LGTD_JSONRPC_NODE(
"kelvin",
offsetof(struct lgtd_jsonrpc_set_light_from_hsbk_args, k),
-1,
lgtd_jsonrpc_type_integer,
false
),
LGTD_JSONRPC_NODE(
"transition",
offsetof(struct lgtd_jsonrpc_set_light_from_hsbk_args, t),
-1,
lgtd_jsonrpc_type_integer,
true
),
};
bool ok = lgtd_jsonrpc_extract_and_validate_params_against_schema(
¶ms,
schema,
LGTD_ARRAY_SIZE(schema),
client->current_request->params,
client->current_request->params_ntokens,
client->json
);
if (!ok) {
goto error_invalid_params;
}
int h = lgtd_jsonrpc_float_range_to_uint16(
&client->json[params.h->start], LGTD_JSONRPC_TOKEN_LEN(params.h), 0, 360
);
int s = lgtd_jsonrpc_float_range_to_uint16(
&client->json[params.s->start], LGTD_JSONRPC_TOKEN_LEN(params.s), 0, 1
);
int b = lgtd_jsonrpc_float_range_to_uint16(
&client->json[params.b->start], LGTD_JSONRPC_TOKEN_LEN(params.b), 0, 1
);
errno = 0;
int k = strtol(&client->json[params.k->start], NULL, 10);
if (k < 2500 || k > 9000 || errno == ERANGE) {
goto error_invalid_params;
}
int t = 0;
if (params.t) {
t = strtol(&client->json[params.t->start], NULL, 10);
if (t < 0 || errno == ERANGE) {
goto error_invalid_params;
}
}
struct lgtd_proto_target_list targets = SLIST_HEAD_INITIALIZER(&targets);
ok = lgtd_jsonrpc_build_target_list(
&targets, client, params.target, params.target_ntokens
);
if (!ok) {
return;
}
lgtd_proto_set_light_from_hsbk(client, &targets, h, s, b, k, t);
lgtd_proto_target_list_clear(&targets);
return;
error_invalid_params:
lgtd_jsonrpc_send_error(
client, LGTD_JSONRPC_INVALID_PARAMS, "Invalid parameters"
);
}
static void
lgtd_jsonrpc_check_and_call_set_waveform(struct lgtd_client *client)
{
struct lgtd_jsonrpc_set_waveform_args {
const jsmntok_t *target;
int target_ntokens;
const jsmntok_t *waveform;
const jsmntok_t *h;
const jsmntok_t *s;
const jsmntok_t *b;
const jsmntok_t *k;
const jsmntok_t *period;
const jsmntok_t *cycles;
const jsmntok_t *skew_ratio;
const jsmntok_t *transient;
} params = { NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
static const struct lgtd_jsonrpc_node schema[] = {
LGTD_JSONRPC_NODE(
"target",
offsetof(struct lgtd_jsonrpc_set_waveform_args, target),
offsetof(struct lgtd_jsonrpc_set_waveform_args, target_ntokens),
lgtd_jsonrpc_type_string_number_or_array,
false
),
LGTD_JSONRPC_NODE(
"waveform",
offsetof(struct lgtd_jsonrpc_set_waveform_args, waveform),
-1,
lgtd_jsonrpc_type_string,
false
),
LGTD_JSONRPC_NODE(
"hue",
offsetof(struct lgtd_jsonrpc_set_waveform_args, h),
-1,
lgtd_jsonrpc_type_float_between_0_and_360,
false
),
LGTD_JSONRPC_NODE(
"saturation",
offsetof(struct lgtd_jsonrpc_set_waveform_args, s),
-1,
lgtd_jsonrpc_type_float_between_0_and_1,
false
),
LGTD_JSONRPC_NODE(
"brightness",
offsetof(struct lgtd_jsonrpc_set_waveform_args, b),
-1,
lgtd_jsonrpc_type_float_between_0_and_1,
false
),
LGTD_JSONRPC_NODE(
"kelvin",
offsetof(struct lgtd_jsonrpc_set_waveform_args, k),
-1,
lgtd_jsonrpc_type_integer,
false
),
LGTD_JSONRPC_NODE(
"period",
offsetof(struct lgtd_jsonrpc_set_waveform_args, period),
-1,
lgtd_jsonrpc_type_integer,
false
),
LGTD_JSONRPC_NODE(
"cycles",
offsetof(struct lgtd_jsonrpc_set_waveform_args, cycles),
-1,
lgtd_jsonrpc_type_integer,
false
),
LGTD_JSONRPC_NODE(
"skew_ratio",
offsetof(struct lgtd_jsonrpc_set_waveform_args, skew_ratio),
-1,
lgtd_jsonrpc_type_float_between_0_and_1,
false
),
LGTD_JSONRPC_NODE(
"transient",
offsetof(struct lgtd_jsonrpc_set_waveform_args, transient),
-1,
lgtd_jsonrpc_type_bool,
true
),
};
bool ok = lgtd_jsonrpc_extract_and_validate_params_against_schema(
¶ms,
schema,
LGTD_ARRAY_SIZE(schema),
client->current_request->params,
client->current_request->params_ntokens,
client->json
);
if (!ok) {
goto error_invalid_params;
}
enum lgtd_lifx_waveform_type waveform;
waveform = lgtd_lifx_wire_waveform_string_id_to_type(
&client->json[params.waveform->start], LGTD_JSONRPC_TOKEN_LEN(params.waveform)
);
if (waveform == LGTD_LIFX_WAVEFORM_INVALID) {
goto error_invalid_params;
}
int h = lgtd_jsonrpc_float_range_to_uint16(
&client->json[params.h->start], LGTD_JSONRPC_TOKEN_LEN(params.h), 0, 360
);
int s = lgtd_jsonrpc_float_range_to_uint16(
&client->json[params.s->start], LGTD_JSONRPC_TOKEN_LEN(params.s), 0, 1
);
int b = lgtd_jsonrpc_float_range_to_uint16(
&client->json[params.b->start], LGTD_JSONRPC_TOKEN_LEN(params.b), 0, 1
);
errno = 0;
int k = strtol(&client->json[params.k->start], NULL, 10);
if (k < 2500 || k > 9000 || errno == ERANGE) {
goto error_invalid_params;
}
int period = strtol(&client->json[params.period->start], NULL, 10);
if (period <= 0 || errno == ERANGE) {
goto error_invalid_params;
}
int cycles = strtol(&client->json[params.cycles->start], NULL, 10);
if (cycles <= 0 || errno == ERANGE) {
goto error_invalid_params;
}
int skew_ratio = lgtd_jsonrpc_float_range_to_uint16(
&client->json[params.skew_ratio->start],
LGTD_JSONRPC_TOKEN_LEN(params.skew_ratio),
0, 1
);
skew_ratio -= UINT16_MAX / 2;
bool transient = params.transient ?
client->json[params.transient->start] == 't' : true;
struct lgtd_proto_target_list targets = SLIST_HEAD_INITIALIZER(&targets);
ok = lgtd_jsonrpc_build_target_list(
&targets, client, params.target, params.target_ntokens
);
if (!ok) {
return;
}
lgtd_proto_set_waveform(
client, &targets,
waveform, h, s, b, k,
period, cycles, skew_ratio, transient
);
lgtd_proto_target_list_clear(&targets);
return;
error_invalid_params:
lgtd_jsonrpc_send_error(
client, LGTD_JSONRPC_INVALID_PARAMS, "Invalid parameters"
);
}
static bool
lgtd_jsonrpc_extract_target_list(struct lgtd_proto_target_list *targets,
struct lgtd_client *client)
{
struct lgtd_jsonrpc_target_args {
const jsmntok_t *target;
int target_ntokens;
} params = { NULL, 0 };
static const struct lgtd_jsonrpc_node schema[] = {
LGTD_JSONRPC_NODE(
"target",
offsetof(struct lgtd_jsonrpc_target_args, target),
offsetof(struct lgtd_jsonrpc_target_args, target_ntokens),
lgtd_jsonrpc_type_string_number_or_array,
false
)
};
struct lgtd_jsonrpc_request *req = client->current_request;
bool ok = lgtd_jsonrpc_extract_and_validate_params_against_schema(
¶ms, schema, 1, req->params, req->params_ntokens, client->json
);
if (!ok) {
lgtd_jsonrpc_send_error(
client, LGTD_JSONRPC_INVALID_PARAMS, "Invalid parameters"
);
return false;
}
return lgtd_jsonrpc_build_target_list(
targets, client, params.target, params.target_ntokens
);
}
#define CHECK_AND_CALL_TARGETS_ONLY_METHOD(proto_method) \
static void \
lgtd_jsonrpc_check_and_call_##proto_method(struct lgtd_client *client) \
{ \
struct lgtd_proto_target_list targets = SLIST_HEAD_INITIALIZER(&targets); \
bool ok = lgtd_jsonrpc_extract_target_list(&targets, client); \
if (!ok) { \
return; \
} \
\
lgtd_proto_##proto_method(client, &targets); \
lgtd_proto_target_list_clear(&targets); \
}
CHECK_AND_CALL_TARGETS_ONLY_METHOD(power_on);
CHECK_AND_CALL_TARGETS_ONLY_METHOD(power_off);
CHECK_AND_CALL_TARGETS_ONLY_METHOD(power_toggle);
CHECK_AND_CALL_TARGETS_ONLY_METHOD(get_light_state);