-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpd-simple.c
1327 lines (1187 loc) · 48 KB
/
httpd-simple.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) 2010, Swedish Institute of Computer Science.
* Copyright (c) 2014, Texas Instruments Incorporated - http://www.ti.com/
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the Institute nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/**
* \addtogroup cc26xx-st2
* @{
*
* \file
* A simple web server which displays network and sensor information
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "httpd-simple.h"
#include "net/ipv6/uip-ds6-route.h"
#include "batmon-sensor.h"
#include "lib/sensors.h"
#include "lib/list.h"
#include "cc26xx-st2.h"
#include "mqtt-client.h"
#include "net-uart.h"
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <strformat.h>
#include <ctype.h>
/*---------------------------------------------------------------------------*/
#define SEND_STRING(s, str) PSOCK_SEND(s, (uint8_t *)str, strlen(str))
/*---------------------------------------------------------------------------*/
#define CONNS 2
#define CONTENT_LENGTH_MAX 256
#define STATE_WAITING 0
#define STATE_OUTPUT 1
#define IPADDR_BUF_LEN 64
/*---------------------------------------------------------------------------*/
#define RETURN_CODE_OK 0
#define RETURN_CODE_NF 1 /* Not Found */
#define RETURN_CODE_SU 2 /* Service Unavailable */
#define RETURN_CODE_BR 3 /* Bad Request */
#define RETURN_CODE_LR 4 /* Length Required */
#define RETURN_CODE_TL 5 /* Content Length too Large */
/*---------------------------------------------------------------------------*/
/* POST request machine states */
#define PARSE_POST_STATE_INIT 0
#define PARSE_POST_STATE_MORE 1
#define PARSE_POST_STATE_READING_KEY 2
#define PARSE_POST_STATE_READING_VAL 3
#define PARSE_POST_STATE_ERROR 0xFFFFFFFF
/*---------------------------------------------------------------------------*/
#define PARSE_POST_BUF_SIZES 64
/* Last byte always used to null terminate */
#define PARSE_POST_MAX_POS (PARSE_POST_BUF_SIZES - 2)
static char key[PARSE_POST_BUF_SIZES];
static char val_escaped[PARSE_POST_BUF_SIZES];
static char val[PARSE_POST_BUF_SIZES];
static int key_len;
static int val_len;
static int state;
/*---------------------------------------------------------------------------*/
/* Stringified min/max intervals */
#define STRINGIFY(x) XSTR(x)
#define XSTR(x) #x
#define RSSI_INT_MAX STRINGIFY(MQTT_CLIENT_RSSI_MEASURE_INTERVAL_MAX)
#define RSSI_INT_MIN STRINGIFY(MQTT_CLIENT_RSSI_MEASURE_INTERVAL_MIN)
#define PUB_INT_MAX STRINGIFY(MQTT_CLIENT_PUBLISH_INTERVAL_MAX)
#define PUB_INT_MIN STRINGIFY(MQTT_CLIENT_PUBLISH_INTERVAL_MIN)
/*---------------------------------------------------------------------------*/
/*
* We can only handle a single POST request at a time. Since a second POST
* request cannot interrupt us while obtaining a lock, we don't really need
* this lock to be atomic.
*
* An HTTP connection will first request a lock before it starts processing
* a POST request. We maintain a global lock which is either NULL or points
* to the http conn which currently has the lock
*/
static struct httpd_state *lock;
/*---------------------------------------------------------------------------*/
PROCESS(httpd_simple_process, "CC26XX Web Server");
/*---------------------------------------------------------------------------*/
#define ISO_nl 0x0A
#define ISO_space 0x20
#define ISO_slash 0x2F
#define ISO_amp 0x26
#define ISO_column 0x3A
#define ISO_equal 0x3D
/*---------------------------------------------------------------------------*/
#define HTTP_200_OK "HTTP/1.0 200 OK\r\n"
#define HTTP_302_FO "HTTP/1.0 302 Found\r\n"
#define HTTP_400_BR "HTTP/1.0 400 Bad Request\r\n"
#define HTTP_404_NF "HTTP/1.0 404 Not Found\r\n"
#define HTTP_411_LR "HTTP/1.0 411 Length Required\r\n"
#define HTTP_413_TL "HTTP/1.0 413 Request Entity Too Large\r\n"
#define HTTP_503_SU "HTTP/1.0 503 Service Unavailable\r\n"
#define CONN_CLOSE "Connection: close\r\n"
/*---------------------------------------------------------------------------*/
#define SECTION_TAG "div"
#define SECTION_OPEN "<" SECTION_TAG ">"
#define SECTION_CLOSE "</" SECTION_TAG ">"
#define CONTENT_OPEN "<pre>"
#define CONTENT_CLOSE "</pre>"
/*---------------------------------------------------------------------------*/
#define REQUEST_TYPE_GET 1
#define REQUEST_TYPE_POST 2
/*---------------------------------------------------------------------------*/
static const char *NOT_FOUND = "<html><body bgcolor=\"white\">"
"<center>"
"<h1>404 - file not found</h1>"
"</center>"
"</body>"
"</html>";
/*---------------------------------------------------------------------------*/
/* Page template */
static const char http_doctype[] = "<!DOCTYPE html>";
static const char http_header_200[] = HTTP_200_OK;
static const char http_header_302[] = HTTP_302_FO;
static const char http_header_400[] = HTTP_400_BR;
static const char http_header_404[] = HTTP_404_NF;
static const char http_header_411[] = HTTP_411_LR;
static const char http_header_413[] = HTTP_413_TL;
static const char http_header_503[] = HTTP_503_SU;
static const char http_get[] = "GET ";
static const char http_post[] = "POST ";
static const char http_index_html[] = "/index.html";
static const char http_html_start[] = "<html><head>";
static const char *http_header_srv_str[] = {
"Server: Contiki, ",
BOARD_STRING "\r\n",
NULL
};
static const char *http_header_con_close[] = {
CONN_CLOSE,
NULL
};
static const char *http_config_css[] = {
"<style>",
".left{float:left;text-align:right;}",
".right{margin-left:150px;}",
"input[type=\"radio\"]",
"{display: inline-block;}",
"</style>",
NULL
};
static const char http_head_charset[] = "<meta charset=\"UTF-8\">";
static const char http_title_start[] = "<title>";
static const char http_title_end[] = "</title>";
static const char http_head_end[] = "</head>";
static const char http_body_start[] = "<body>";
static const char http_bottom[] = "</body></html>";
/*---------------------------------------------------------------------------*/
static const char http_content_type_html[] = "text/html";
static const char http_content_type_plain[] = "text/plain";
/*---------------------------------------------------------------------------*/
/* For the config page */
static const char config_div_left[] = "<div class=\"left\">";
static const char config_div_right[] = "<div class=\"right\">";
static const char config_div_close[] = "</div>";
/*---------------------------------------------------------------------------*/
static char generate_index(struct httpd_state *s);
static char generate_config(struct httpd_state *s);
/*---------------------------------------------------------------------------*/
typedef struct page {
struct page *next;
char *filename;
char *title;
char (*script)(struct httpd_state *s);
} page_t;
static page_t http_index_page = {
NULL,
"index.html",
"Index",
generate_index,
};
static page_t http_dev_cfg_page = {
NULL,
"config.html",
"Device Config",
generate_config,
};
#if CC26XX_WEB_DEMO_NET_UART
static char generate_net_uart_config(struct httpd_state *s);
static page_t http_net_cfg_page = {
NULL,
"netu.html",
"Net-UART Config",
generate_net_uart_config,
};
#endif
#if CC26XX_WEB_DEMO_MQTT_CLIENT
static char generate_mqtt_config(struct httpd_state *s);
static page_t http_mqtt_cfg_page = {
NULL,
"mqtt.html",
"MQTT/IBM Cloud Config",
generate_mqtt_config,
};
#endif
/*---------------------------------------------------------------------------*/
#define IBM_QUICKSTART_LINK_LEN 128
static char http_mqtt_a[IBM_QUICKSTART_LINK_LEN];
/*---------------------------------------------------------------------------*/
static uint16_t numtimes;
static const httpd_simple_post_handler_t *handler;
/*---------------------------------------------------------------------------*/
static uint8_t config_ok;
process_event_t httpd_simple_event_new_config;
/*---------------------------------------------------------------------------*/
struct httpd_state;
typedef char (*httpd_simple_script_t)(struct httpd_state *s);
struct httpd_state {
char buf[HTTPD_SIMPLE_MAIN_BUF_SIZE];
char tmp_buf[TMP_BUF_SIZE];
struct timer timer;
struct psock sin, sout;
int blen;
const char **ptr;
const cc26xx_web_demo_sensor_reading_t *reading;
const page_t *page;
uip_ds6_route_t *r;
uip_ds6_nbr_t *nbr;
httpd_simple_script_t script;
int content_length;
int tmp_buf_len;
int tmp_buf_copied;
char filename[HTTPD_PATHLEN];
char inputbuf[HTTPD_INBUF_LEN];
struct pt outputpt;
struct pt generate_pt;
struct pt top_matter_pt;
char state;
char request_type;
char return_code;
};
/*---------------------------------------------------------------------------*/
LIST(post_handlers);
LIST(pages_list);
MEMB(conns, struct httpd_state, CONNS);
/*---------------------------------------------------------------------------*/
#define HEX_TO_INT(x) (isdigit(x) ? x - '0' : x - 'W')
static size_t
url_unescape(const char *src, size_t srclen, char *dst, size_t dstlen)
{
size_t i, j;
int a, b;
for(i = j = 0; i < srclen && j < dstlen - 1; i++, j++) {
if(src[i] == '%' && isxdigit(*(unsigned char *)(src + i + 1))
&& isxdigit(*(unsigned char *)(src + i + 2))) {
a = tolower(*(unsigned char *)(src + i + 1));
b = tolower(*(unsigned char *)(src + i + 2));
dst[j] = ((HEX_TO_INT(a) << 4) | HEX_TO_INT(b)) & 0xff;
i += 2;
} else if(src[i] == '+') {
dst[j] = ' ';
} else {
dst[j] = src[i];
}
}
dst[j] = '\0';
return i == srclen;
}
/*---------------------------------------------------------------------------*/
void
httpd_simple_register_post_handler(httpd_simple_post_handler_t *h)
{
list_add(post_handlers, h);
}
/*---------------------------------------------------------------------------*/
static void
get_neighbour_state_text(char *buf, uint8_t state)
{
switch(state) {
case NBR_INCOMPLETE:
memcpy(buf, "INCOMPLETE", strlen("INCOMPLETE"));
break;
case NBR_REACHABLE:
memcpy(buf, "REACHABLE", strlen("REACHABLE"));
break;
case NBR_STALE:
memcpy(buf, "STALE", strlen("STALE"));
break;
case NBR_DELAY:
memcpy(buf, "DELAY", strlen("DELAY"));
break;
case NBR_PROBE:
memcpy(buf, "NBR_PROBE", strlen("NBR_PROBE"));
break;
}
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(enqueue_chunk(struct httpd_state *s, uint8_t immediate,
const char *format, ...))
{
va_list ap;
PSOCK_BEGIN(&s->sout);
va_start(ap, format);
s->tmp_buf_len = vsnprintf(s->tmp_buf, TMP_BUF_SIZE, format, ap);
va_end(ap);
if(s->blen + s->tmp_buf_len < HTTPD_SIMPLE_MAIN_BUF_SIZE) {
/* Enough space for the entire chunk. Copy over */
memcpy(&s->buf[s->blen], s->tmp_buf, s->tmp_buf_len);
s->blen += s->tmp_buf_len;
} else {
memcpy(&s->buf[s->blen], s->tmp_buf, HTTPD_SIMPLE_MAIN_BUF_SIZE - s->blen);
s->tmp_buf_copied = HTTPD_SIMPLE_MAIN_BUF_SIZE - s->blen;
s->blen = HTTPD_SIMPLE_MAIN_BUF_SIZE;
PSOCK_SEND(&s->sout, (uint8_t *)s->buf, s->blen);
s->blen = 0;
if(s->tmp_buf_copied < s->tmp_buf_len) {
memcpy(s->buf, &s->tmp_buf[s->tmp_buf_copied],
s->tmp_buf_len - s->tmp_buf_copied);
s->blen += s->tmp_buf_len - s->tmp_buf_copied;
}
}
if(immediate != 0 && s->blen > 0) {
PSOCK_SEND(&s->sout, (uint8_t *)s->buf, s->blen);
s->blen = 0;
}
PSOCK_END(&s->sout);
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(generate_top_matter(struct httpd_state *s, const char *title,
const char **css))
{
PT_BEGIN(&s->top_matter_pt);
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, http_doctype));
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, http_html_start));
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, http_title_start));
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, title));
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, http_title_end));
if(css != NULL) {
for(s->ptr = css; *(s->ptr) != NULL; s->ptr++) {
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, *(s->ptr)));
}
}
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, http_head_charset));
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, http_head_end));
PT_WAIT_THREAD(&s->top_matter_pt, enqueue_chunk(s, 0, http_body_start));
/* Links */
PT_WAIT_THREAD(&s->top_matter_pt,
enqueue_chunk(s, 0, SECTION_OPEN "<p>"));
s->page = list_head(pages_list);
PT_WAIT_THREAD(&s->top_matter_pt,
enqueue_chunk(s, 0, "[ <a href=\"%s\">%s</a> ]",
s->page->filename, s->page->title));
for(s->page = s->page->next; s->page != NULL; s->page = s->page->next) {
PT_WAIT_THREAD(&s->top_matter_pt,
enqueue_chunk(s, 0, " | [ <a href=\"%s\">%s</a> ]",
s->page->filename, s->page->title));
}
#if CC26XX_WEB_DEMO_MQTT_CLIENT
PT_WAIT_THREAD(&s->top_matter_pt,
enqueue_chunk(s, 0, " | %s", http_mqtt_a));
#endif
PT_WAIT_THREAD(&s->top_matter_pt,
enqueue_chunk(s, 0, "</p>" SECTION_CLOSE));
PT_END(&s->top_matter_pt);
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(generate_index(struct httpd_state *s))
{
char ipaddr_buf[IPADDR_BUF_LEN]; /* Intentionally on stack */
PT_BEGIN(&s->generate_pt);
/* Generate top matter (doctype, title, nav links etc) */
PT_WAIT_THREAD(&s->generate_pt,
generate_top_matter(s, http_index_page.title, NULL));
/* ND Cache */
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, SECTION_OPEN "Neighbors" CONTENT_OPEN));
for(s->nbr = nbr_table_head(ds6_neighbors); s->nbr != NULL;
s->nbr = nbr_table_next(ds6_neighbors, s->nbr)) {
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "\n"));
memset(ipaddr_buf, 0, IPADDR_BUF_LEN);
cc26xx_web_demo_ipaddr_sprintf(ipaddr_buf, IPADDR_BUF_LEN, &s->nbr->ipaddr);
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "%s", ipaddr_buf));
memset(ipaddr_buf, 0, IPADDR_BUF_LEN);
get_neighbour_state_text(ipaddr_buf, s->nbr->state);
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, " %s", ipaddr_buf));
}
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, CONTENT_CLOSE SECTION_CLOSE));
/* Default Route */
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
SECTION_OPEN "Default Route" CONTENT_OPEN));
memset(ipaddr_buf, 0, IPADDR_BUF_LEN);
cc26xx_web_demo_ipaddr_sprintf(ipaddr_buf, IPADDR_BUF_LEN,
uip_ds6_defrt_choose());
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "%s", ipaddr_buf));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, CONTENT_CLOSE SECTION_CLOSE));
/* Routes */
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, SECTION_OPEN "Routes" CONTENT_OPEN));
for(s->r = uip_ds6_route_head(); s->r != NULL;
s->r = uip_ds6_route_next(s->r)) {
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "\n"));
memset(ipaddr_buf, 0, IPADDR_BUF_LEN);
cc26xx_web_demo_ipaddr_sprintf(ipaddr_buf, IPADDR_BUF_LEN, &s->r->ipaddr);
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "%s", ipaddr_buf));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, " / %u via ", s->r->length));
memset(ipaddr_buf, 0, IPADDR_BUF_LEN);
cc26xx_web_demo_ipaddr_sprintf(ipaddr_buf, IPADDR_BUF_LEN,
uip_ds6_route_nexthop(s->r));
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "%s", ipaddr_buf));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
", lifetime=%lus", s->r->state.lifetime));
}
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0,
CONTENT_CLOSE SECTION_CLOSE));
/* Sensors */
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, SECTION_OPEN "Sensors" CONTENT_OPEN));
for(s->reading = cc26xx_web_demo_sensor_first();
s->reading != NULL; s->reading = s->reading->next) {
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "\n%s = %s %s", s->reading->descr,
s->reading->publish ? s->reading->converted : "N/A",
s->reading->units));
}
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, CONTENT_CLOSE SECTION_CLOSE));
/* Footer */
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, SECTION_OPEN));
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "Page hits: %u<br>",
++numtimes));
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "Uptime: %lu secs<br>",
clock_seconds()));
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, SECTION_CLOSE));
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 1, http_bottom));
PT_END(&s->generate_pt);
}
/*---------------------------------------------------------------------------*/
static
PT_THREAD(generate_config(struct httpd_state *s))
{
PT_BEGIN(&s->generate_pt);
/* Generate top matter (doctype, title, nav links etc) */
PT_WAIT_THREAD(&s->generate_pt,
generate_top_matter(s, http_dev_cfg_page.title,
http_config_css));
/* Sensor Settings */
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<h1>Sensors</h1>"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"<form name=\"input\" action=\"%s\" ",
http_dev_cfg_page.filename));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "method=\"post\" enctype=\""));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "application/x-www-form-urlencoded\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "accept-charset=\"UTF-8\">"));
for(s->reading = cc26xx_web_demo_sensor_first();
s->reading != NULL; s->reading = s->reading->next) {
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s%s:%s%s", config_div_left,
s->reading->descr, config_div_close,
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<input type=\"radio\" value=\"1\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "title=\"On\" name=\"%s\"%s>",
s->reading->form_field,
s->reading->publish ? " Checked" : ""));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<input type=\"radio\" value=\"0\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "title=\"Off\" name=\"%s\"%s>%s",
s->reading->form_field,
s->reading->publish ? "" : " Checked",
config_div_close));
}
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"<input type=\"submit\" value=\"Submit\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "</form>"));
/* Actions */
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "<h1>Actions</h1>"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"<form name=\"input\" action=\"%s\" ",
http_dev_cfg_page.filename));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "method=\"post\" enctype=\""));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "application/x-www-form-urlencoded\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "accept-charset=\"UTF-8\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<input type=\"hidden\" value=\"1\" "
"name=\"defaults\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<button>Restore Defaults</button>"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "</form>"));
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 1, http_bottom));
PT_END(&s->generate_pt);
}
/*---------------------------------------------------------------------------*/
#if CC26XX_WEB_DEMO_MQTT_CLIENT
static
PT_THREAD(generate_mqtt_config(struct httpd_state *s))
{
PT_BEGIN(&s->generate_pt);
/* Generate top matter (doctype, title, nav links etc) */
PT_WAIT_THREAD(&s->generate_pt,
generate_top_matter(s, http_mqtt_cfg_page.title,
http_config_css));
/* MQTT client settings */
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<h1>%s</h1>", http_mqtt_cfg_page.title));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"<form name=\"input\" action=\"%s\" ",
http_mqtt_cfg_page.filename));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "method=\"post\" enctype=\""));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "application/x-www-form-urlencoded\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "accept-charset=\"UTF-8\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sType ID:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"text\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%s\" ",
cc26xx_web_demo_config.mqtt_config.type_id));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "name=\"type_id\">%s", config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sOrg ID:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"text\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%s\" ",
cc26xx_web_demo_config.mqtt_config.org_id));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "name=\"org_id\">%s", config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sAuth Token:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"text\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "name=\"auth_token\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sCommand Type:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"text\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%s\" ",
cc26xx_web_demo_config.mqtt_config.cmd_type));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "name=\"cmd_type\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sEvent Type ID:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"text\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%s\" ",
cc26xx_web_demo_config.mqtt_config.event_type_id));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "name=\"event_type_id\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sInterval (secs):%s",
config_div_left, config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"number\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%lu\" ",
(clock_time_t)
(cc26xx_web_demo_config.mqtt_config.pub_interval
/ CLOCK_SECOND)));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"min=\"" PUB_INT_MIN "\" "
"max=\"" PUB_INT_MAX "\" "
"name=\"interval\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sBroker IP:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"text\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%s\" ",
cc26xx_web_demo_config.mqtt_config.broker_ip));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "name=\"broker_ip\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sBroker Port:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"number\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%d\" ",
cc26xx_web_demo_config.mqtt_config.broker_port));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "min=\"1\" max=\"65535\" "
"name=\"broker_port\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sRSSI Interval (secs):%s",
config_div_left, config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"number\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%lu\" ",
(clock_time_t)
(cc26xx_web_demo_config.mqtt_config.def_rt_ping_interval
/ CLOCK_SECOND)));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"min=\"" RSSI_INT_MIN "\" "
"max=\"" RSSI_INT_MAX "\" "
"name=\"ping_interval\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"<input type=\"submit\" value=\"Submit\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "</form>"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"<form name=\"input\" action=\"%s\" ",
http_mqtt_cfg_page.filename));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "method=\"post\" enctype=\""));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "application/x-www-form-urlencoded\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "accept-charset=\"UTF-8\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<input type=\"hidden\" value=\"1\" "
"name=\"reconnect\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<button>MQTT Reconnect</button>"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "</form>"));
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 1, http_bottom));
PT_END(&s->generate_pt);
}
#endif
/*---------------------------------------------------------------------------*/
#if CC26XX_WEB_DEMO_NET_UART
static
PT_THREAD(generate_net_uart_config(struct httpd_state *s))
{
PT_BEGIN(&s->generate_pt);
/* Generate top matter (doctype, title, nav links etc) */
PT_WAIT_THREAD(&s->generate_pt,
generate_top_matter(s, http_net_cfg_page.title,
http_config_css));
/* Net-UART settings */
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<h1>%s</h1>", http_net_cfg_page.title));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"<form name=\"input\" action=\"%s\" ",
http_net_cfg_page.filename));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "method=\"post\" enctype=\""));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "application/x-www-form-urlencoded\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "accept-charset=\"UTF-8\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sRemote IPv6:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"text\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%s\" ",
cc26xx_web_demo_config.net_uart.remote_address));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "name=\"net_uart_ip\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%sRemote Port:%s", config_div_left,
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s<input type=\"number\" ",
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "value=\"%u\" ",
cc26xx_web_demo_config.net_uart.remote_port));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "min=\"1\" max=\"65535\" "
"name=\"net_uart_port\">%s",
config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "%s%s:%s%s", config_div_left,
"Enable", config_div_close,
config_div_right));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<input type=\"radio\" value=\"1\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "title=\"On\" name=\"net_uart_on\"%s>",
cc26xx_web_demo_config.net_uart.enable ?
" Checked" : ""));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "<input type=\"radio\" value=\"0\" "));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "title=\"Off\" name=\"net_uart_on\""
"%s>%s",
cc26xx_web_demo_config.net_uart.enable ?
"" : " Checked", config_div_close));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0,
"<input type=\"submit\" value=\"Submit\">"));
PT_WAIT_THREAD(&s->generate_pt,
enqueue_chunk(s, 0, "</form>"));
PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 1, http_bottom));
PT_END(&s->generate_pt);
}
#endif
/*---------------------------------------------------------------------------*/
static void
lock_obtain(struct httpd_state *s)
{
if(lock == NULL) {
lock = s;
}
}
/*---------------------------------------------------------------------------*/
static void
lock_release(struct httpd_state *s)
{
if(lock == s) {
lock = NULL;
}
}
/*---------------------------------------------------------------------------*/
static void
parse_post_request_chunk(char *buf, int buf_len, int last_chunk)
{
int i;
int finish;
for(i = 0; i < buf_len; i++) {
switch(state) {
case PARSE_POST_STATE_INIT:
state = PARSE_POST_STATE_MORE;
/* continue */
case PARSE_POST_STATE_MORE:
memset(key, 0, PARSE_POST_BUF_SIZES);
memset(val, 0, PARSE_POST_BUF_SIZES);
memset(val_escaped, 0, PARSE_POST_BUF_SIZES);
key_len = 0;
val_len = 0;
state = PARSE_POST_STATE_READING_KEY;
/* continue */
case PARSE_POST_STATE_READING_KEY:
if(buf[i] == ISO_equal) {
state = PARSE_POST_STATE_READING_VAL;
} else if(buf[i] == ISO_amp) {
/* Don't accept an amp while reading a key */
state = PARSE_POST_STATE_ERROR;
} else {
/* Make sure we don't overshoot key's boundary */
if(key_len <= PARSE_POST_MAX_POS) {
key[key_len] = buf[i];
key_len++;
} else {
/* Not enough space for the key. Abort */
state = PARSE_POST_STATE_ERROR;
}
}
break;
case PARSE_POST_STATE_READING_VAL:
finish = 0;
if(buf[i] == ISO_amp) {
finish = 1;
} else if(buf[i] == ISO_equal) {
/* Don't accept an '=' while reading a val */
state = PARSE_POST_STATE_ERROR;
} else {
/* Make sure we don't overshoot key's boundary */
if(val_len <= PARSE_POST_MAX_POS) {
val[val_len] = buf[i];
val_len++;
/* Last character of the last chunk */
if((i == buf_len - 1) && (last_chunk == 1)) {
finish = 1;
}
} else {
/* Not enough space for the value. Abort */
state = PARSE_POST_STATE_ERROR;
}
}
if(finish == 1) {
/*
* Done reading a key=value pair, either because we encountered an amp
* or because we reached the end of the message body.
*
* First, unescape the value.
*
* Then invoke handlers. We will bail out with PARSE_POST_STATE_ERROR,
* unless the key-val gets correctly processed
*/
url_unescape(val, val_len, val_escaped, PARSE_POST_BUF_SIZES);
val_len = strlen(val_escaped);
for(handler = list_head(post_handlers); handler != NULL;
handler = list_item_next((void *)handler)) {
if(handler->handler != NULL) {
finish = handler->handler(key, key_len, val_escaped, val_len);
}
if(finish == HTTPD_SIMPLE_POST_HANDLER_ERROR) {
state = PARSE_POST_STATE_ERROR;
break;
} else if(finish == HTTPD_SIMPLE_POST_HANDLER_OK) {
/* Restart the state machine to expect the next pair */
state = PARSE_POST_STATE_MORE;
/*
* At least one handler returned OK, therefore we must generate a
* new config event when we're done.
*/
config_ok = 1;
break;
}
/* Else, continue */
}
}
break;
case PARSE_POST_STATE_ERROR:
/* If we entered the error state earlier, do nothing */
return;
default:
break;
}
}
}
/*---------------------------------------------------------------------------*/
static httpd_simple_script_t
get_script(const char *name)
{
page_t *page;
for(page = list_head(pages_list); page != NULL;
page = list_item_next(page)) {
if(strncmp(name, page->filename, strlen(page->filename)) == 0) {
return page->script;
}
}
return NULL;
}
/*---------------------------------------------------------------------------*/