forked from openshwprojects/OpenBK7231T_App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_fns.c
More file actions
3355 lines (3053 loc) · 114 KB
/
http_fns.c
File metadata and controls
3355 lines (3053 loc) · 114 KB
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
#include "../new_common.h"
#include "http_fns.h"
#include "../new_pins.h"
#include "../new_cfg.h"
#include "../hal/hal_ota.h"
// Commands register, execution API and cmd tokenizer
#include "../cmnds/cmd_public.h"
#include "../driver/drv_tuyaMCU.h"
#include "../driver/drv_public.h"
#include "../driver/drv_bl_shared.h"
#include "../hal/hal_wifi.h"
#include "../hal/hal_pins.h"
#include "../hal/hal_flashConfig.h"
#include "../logging/logging.h"
#include "../devicegroups/deviceGroups_public.h"
#include "../mqtt/new_mqtt.h"
#include "hass.h"
#include "../cJSON/cJSON.h"
#include <time.h>
#include "../driver/drv_ntp.h"
#include "../driver/drv_local.h"
#ifdef PLATFORM_BEKEN
#include "start_type_pub.h"
#endif
#ifdef WINDOWS
// nothing
#elif PLATFORM_BL602
#include <bl_sys.h>
#include <bl_adc.h> // For BL602 ADC HAL
#include <bl602_adc.h> // For BL602 ADC Standard Driver
#include <bl602_glb.h> // For BL602 Global Register Standard Driver
#include <wifi_mgmr_ext.h> //For BL602 WiFi AP Scan
#elif PLATFORM_W600 || PLATFORM_W800
#elif PLATFORM_XRADIO
#include <image/flash.h>
#include <ota/ota.h>
#elif defined(PLATFORM_BK7231N)
// tuya-iotos-embeded-sdk-wifi-ble-bk7231n/sdk/include/tuya_hal_storage.h
#include "tuya_hal_storage.h"
#include "BkDriverFlash.h"
#include "temp_detect_pub.h"
#elif defined(PLATFORM_LN882H)
#elif defined(PLATFORM_TR6260)
#elif defined(PLATFORM_REALTEK) && !PLATFORM_REALTEK_NEW
#include "wifi_structures.h"
#include "wifi_constants.h"
#include "wifi_conf.h"
extern uint32_t current_fw_idx;
#ifdef PLATFORM_RTL87X0C
#include "hal_sys_ctrl.h"
extern hal_reset_reason_t reset_reason;
#endif
SemaphoreHandle_t scan_hdl;
#elif PLATFORM_REALTEK_NEW
#include "lwip_netconf.h"
#include "ameba_soc.h"
#include "ameba_ota.h"
extern uint32_t current_fw_idx;
#elif defined(PLATFORM_ESPIDF) || PLATFORM_ESP8266
#include "esp_wifi.h"
#include "esp_system.h"
#elif defined(PLATFORM_BK7231T)
// REALLY? A typo in Tuya SDK? Storge?
// tuya-iotos-embeded-sdk-wifi-ble-bk7231t/platforms/bk7231t/tuya_os_adapter/include/driver/tuya_hal_storge.h
#include "tuya_hal_storge.h"
#include "BkDriverFlash.h"
#include "temp_detect_pub.h"
#elif defined(PLATFORM_ECR6600)
#include "hal_system.h"
#endif
#if (defined(PLATFORM_BK7231T) || defined(PLATFORM_BK7231N)) && !defined(PLATFORM_BEKEN_NEW)
int tuya_os_adapt_wifi_all_ap_scan(AP_IF_S** ap_ary, unsigned int* num);
int tuya_os_adapt_wifi_release_ap(AP_IF_S* ap);
#endif
static const char SUBMIT_AND_END_FORM[] = "<br><input type=\"submit\" value=\"Submit\"></form>";
const char* g_typesOffLowMidHigh[] = { "Off","Low","Mid","High" };
const char* g_typesOffLowMidHighHighest[] = { "Off", "Low","Mid","High","Highest" };
const char* g_typesOffLowestLowMidHighHighest[] = { "Off", "Lowest", "Low", "Mid", "High", "Highest" };
const char* g_typesLowMidHighHighest[] = { "Low","Mid","High","Highest" };
const char* g_typesOffOnRemember[] = { "Off", "On", "Remember" };
const char* g_typeLowMidHigh[] = { "Low","Mid","High" };
const char* g_typesLowestLowMidHighHighest[] = { "Lowest", "Low", "Mid", "High", "Highest" };;
const char* g_typeOpenStopClose[] = { "Open","Stop","Close" };
const char* g_typeStopUpDown[] = { "Stop","Up","Down" };
#define ADD_OPTION(t,a) if(type == t) { *numTypes = sizeof(a)/sizeof(a[0]); return a; }
const char **Channel_GetOptionsForChannelType(int type, int *numTypes) {
ADD_OPTION(ChType_OffLowMidHigh, g_typesOffLowMidHigh);
ADD_OPTION(ChType_OffLowestLowMidHighHighest, g_typesOffLowestLowMidHighHighest);
ADD_OPTION(ChType_LowestLowMidHighHighest, g_typesLowestLowMidHighHighest);
ADD_OPTION(ChType_OffLowMidHighHighest, g_typesOffLowMidHighHighest);
ADD_OPTION(ChType_LowMidHighHighest, g_typesLowMidHighHighest);
ADD_OPTION(ChType_OffOnRemember, g_typesOffOnRemember);
ADD_OPTION(ChType_LowMidHigh, g_typeLowMidHigh);
ADD_OPTION(ChType_OpenStopClose, g_typeOpenStopClose);
ADD_OPTION(ChType_StopUpDown, g_typeStopUpDown);
*numTypes = 0;
return 0;
}
unsigned char hexdigit(char hex) {
return (hex <= '9') ? hex - '0' :
toupper((unsigned char)hex) - 'A' + 10;
}
unsigned char hexbyte(const char* hex) {
return (hexdigit(*hex) << 4) | hexdigit(*(hex + 1));
}
int http_fn_empty_url(http_request_t* request) {
poststr(request, "HTTP/1.1 302 OK\nLocation: /index\nConnection: close\n\n");
poststr(request, NULL);
return 0;
}
void postFormAction(http_request_t* request, char* action, char* value) {
//"<form action=\"cfg_pins\"><input type=\"submit\" value=\"Configure Module\"/></form>"
hprintf255(request, "<form action=\"%s\"><input type=\"submit\" value=\"%s\"/></form>", action, value);
}
void poststr_h2(http_request_t* request, const char* content) {
hprintf255(request, "<h2>%s</h2>", content);
}
void poststr_h4(http_request_t* request, const char* content) {
hprintf255(request, "<h4>%s</h4>", content);
}
/// @brief Generate a pair of label and field elements for Name type entry. The field is limited to entry of a-zA-Z0-9_- characters.
/// @param request
/// @param label
/// @param fieldId This also gets used as the field name
/// @param value
/// @param preContent
void add_label_name_field(http_request_t* request, char* label, char* fieldId, const char* value, char* preContent) {
if (strlen(preContent) > 0) {
poststr(request, preContent);
}
hprintf255(request, "<label for=\"%s\">%s:</label><br>", fieldId, label);
hprintf255(request, "<input type=\"text\" id=\"%s\" name=\"%s\" value=\"%s\" ", fieldId, fieldId, value);
poststr(request, "pattern=\"^[a-zA-Z0-9_-]+$\" title=\"Only alphanumerics, underscore and hyphen characters allowed.\">");
}
/// @brief Generate a pair of label and field elements.
/// @param request
/// @param label
/// @param fieldId This also gets used as the field name
/// @param value
/// @param preContent
void add_label_input(http_request_t* request, char* inputType, char* label, char* fieldId, const char* value, char* preContent) {
if (strlen(preContent) > 0) {
poststr(request, preContent);
}
hprintf255(request, "<label for=\"%s\">%s:</label><br>", fieldId, label);
hprintf255(request, "<input type=\"%s\" id=\"%s\" name=\"%s\" value=\"", inputType, fieldId, fieldId);
poststr(request, value);
hprintf255(request, "\">");
}
/// @brief Generates a pair of label and text field elements.
/// @param request
/// @param label Label for the field
/// @param fieldId Field id, this also gets used as the name
/// @param value String value
/// @param preContent Content before the label
void add_label_text_field(http_request_t* request, char* label, char* fieldId, const char* value, char* preContent) {
add_label_input(request, "text", label, fieldId, value, preContent);
}
/// @brief Generates a pair of label and text field elements.
/// @param request
/// @param label Label for the field
/// @param fieldId Field id, this also gets used as the name
/// @param value String value
/// @param preContent Content before the label
void add_label_password_field(http_request_t* request, char* label, char* fieldId, const char* value, char* preContent) {
add_label_input(request, "password", label, fieldId, value, preContent);
}
/// @brief Generate a pair of label and numeric field elements.
/// @param request
/// @param label Label for the field
/// @param fieldId Field id, this also gets used as the name
/// @param value Integer value
/// @param preContent Content before the label
void add_label_numeric_field(http_request_t* request, char* label, char* fieldId, int value, char* preContent) {
char strValue[32];
sprintf(strValue, "%i", value);
add_label_input(request, "number", label, fieldId, strValue, preContent);
}
int http_fn_testmsg(http_request_t* request) {
poststr(request, "This is just a test msg\n\n");
poststr(request, NULL);
return 0;
}
// bit mask telling which channels are hidden from HTTP
// If given bit is set, then given channel is hidden
extern int g_hiddenChannels;
int http_fn_index(http_request_t* request) {
int j, i, ch1, ch2;
char tmpA[128];
int bRawPWMs;
bool bForceShowRGBCW;
float fValue;
int iValue;
bool bForceShowRGB;
const char* inputName;
int channelType;
bRawPWMs = CFG_HasFlag(OBK_FLAG_LED_RAWCHANNELSMODE);
bForceShowRGBCW = CFG_HasFlag(OBK_FLAG_LED_FORCESHOWRGBCWCONTROLLER);
bForceShowRGB = CFG_HasFlag(OBK_FLAG_LED_FORCE_MODE_RGB);
// user override is always stronger, so if no override set
if (bForceShowRGB == false && bForceShowRGBCW == false) {
#ifndef OBK_DISABLE_ALL_DRIVERS
if (DRV_IsRunning("SM16703P")) {
bForceShowRGB = true;
}
if (DRV_IsRunning("DMX")) {
bForceShowRGB = true;
}
#endif
}
http_setup(request, httpMimeTypeHTML); //Add mimetype regardless of the request
// use ?state URL parameter to only request current state
if (!http_getArg(request->url, "state", tmpA, sizeof(tmpA))) {
// full update - include header
http_html_start(request, NULL);
poststr(request, "<div id=\"changed\">");
#if defined(PLATFORM_BEKEN) || defined(WINDOWS)
if (DRV_IsRunning("PWMToggler")) {
DRV_Toggler_ProcessChanges(request);
}
#endif
#if defined(PLATFORM_BEKEN) || defined(WINDOWS)
if (DRV_IsRunning("httpButtons")) {
DRV_HTTPButtons_ProcessChanges(request);
}
#endif
if (http_getArg(request->url, "tgl", tmpA, sizeof(tmpA))) {
j = atoi(tmpA);
if (j == SPECIAL_CHANNEL_LEDPOWER) {
hprintf255(request, "<h3>Toggled LED power!</h3>", j);
}
else {
hprintf255(request, "<h3>Toggled %s!</h3>", CHANNEL_GetLabel(j));
}
CHANNEL_Toggle(j);
}
if (http_getArg(request->url, "on", tmpA, sizeof(tmpA))) {
j = atoi(tmpA);
hprintf255(request, "<h3>Enabled %s!</h3>", CHANNEL_GetLabel(j));
CHANNEL_Set(j, 255, 1);
}
#if ENABLE_LED_BASIC
if (http_getArg(request->url, "rgb", tmpA, sizeof(tmpA))) {
hprintf255(request, "<h3>Set RGB to %s!</h3>", tmpA);
LED_SetBaseColor(0, "led_basecolor", tmpA, 0);
// auto enable - but only for changes made from WWW panel
// This happens when users changes COLOR
if (CFG_HasFlag(OBK_FLAG_LED_AUTOENABLE_ON_WWW_ACTION)) {
LED_SetEnableAll(true);
}
}
#endif
if (http_getArg(request->url, "off", tmpA, sizeof(tmpA))) {
j = atoi(tmpA);
hprintf255(request, "<h3>Disabled %s!</h3>", CHANNEL_GetLabel(j));
CHANNEL_Set(j, 0, 1);
}
if (http_getArg(request->url, "pwm", tmpA, sizeof(tmpA))) {
int newPWMValue = atoi(tmpA);
http_getArg(request->url, "pwmIndex", tmpA, sizeof(tmpA));
j = atoi(tmpA);
if (j == SPECIAL_CHANNEL_TEMPERATURE) {
hprintf255(request, "<h3>Changed Temperature to %i!</h3>", newPWMValue);
}
else {
hprintf255(request, "<h3>Changed pwm %i to %i!</h3>", j, newPWMValue);
}
CHANNEL_Set(j, newPWMValue, 1);
#if ENABLE_LED_BASIC
if (j == SPECIAL_CHANNEL_TEMPERATURE) {
// auto enable - but only for changes made from WWW panel
// This happens when users changes TEMPERATURE
if (CFG_HasFlag(OBK_FLAG_LED_AUTOENABLE_ON_WWW_ACTION)) {
LED_SetEnableAll(true);
}
}
#endif
}
if (http_getArg(request->url, "dim", tmpA, sizeof(tmpA))) {
int newDimmerValue = atoi(tmpA);
http_getArg(request->url, "dimIndex", tmpA, sizeof(tmpA));
j = atoi(tmpA);
if (j == SPECIAL_CHANNEL_BRIGHTNESS) {
hprintf255(request, "<h3>Changed LED brightness to %i!</h3>", newDimmerValue);
}
else {
hprintf255(request, "<h3>Changed dimmer %i to %i!</h3>", j, newDimmerValue);
}
CHANNEL_Set(j, newDimmerValue, 1);
#if ENABLE_LED_BASIC
if (j == SPECIAL_CHANNEL_BRIGHTNESS) {
// auto enable - but only for changes made from WWW panel
// This happens when users changes DIMMER
if (CFG_HasFlag(OBK_FLAG_LED_AUTOENABLE_ON_WWW_ACTION)) {
LED_SetEnableAll(true);
}
}
#endif
}
if (http_getArg(request->url, "set", tmpA, sizeof(tmpA))) {
int newSetValue = atoi(tmpA);
http_getArg(request->url, "setIndex", tmpA, sizeof(tmpA));
j = atoi(tmpA);
hprintf255(request, "<h3>Changed channel %s to %i!</h3>", CHANNEL_GetLabel(j), newSetValue);
CHANNEL_Set(j, newSetValue, 1);
}
if (http_getArg(request->url, "restart", tmpA, sizeof(tmpA))) {
poststr(request, "<h5> Module will restart soon</h5>");
RESET_ScheduleModuleReset(3);
}
if (http_getArg(request->url, "unsafe", tmpA, sizeof(tmpA))) {
poststr(request, "<h5> Will try to do unsafe init in few seconds</h5>");
MAIN_ScheduleUnsafeInit(3);
}
poststr(request, "</div>"); // end div#change
#if ENABLE_OBK_BERRY
void Berry_SaveRequest(http_request_t *r);
Berry_SaveRequest(request);
CMD_Berry_RunEventHandlers_StrPtr(CMD_EVENT_ON_HTTP, "prestate", request);
#endif
#ifndef OBK_DISABLE_ALL_DRIVERS
DRV_AppendInformationToHTTPIndexPage(request, true);
#endif
poststr(request, "<div id=\"state\">"); // replaceable content follows
}
#if ENABLE_OBK_BERRY
void Berry_SaveRequest(http_request_t *r);
Berry_SaveRequest(request);
CMD_Berry_RunEventHandlers_StrPtr(CMD_EVENT_ON_HTTP, "state", request);
#endif
if (!CFG_HasFlag(OBK_FLAG_HTTP_NO_ONOFF_WORDS)){
poststr(request, "<table>"); //Table default to 100% width in stylesheet
for (i = 0; i < CHANNEL_MAX; i++) {
channelType = CHANNEL_GetType(i);
// check ability to hide given channel from gui
if (BIT_CHECK(g_hiddenChannels, i)) {
continue; // hidden
}
bool bToggleInv = channelType == ChType_Toggle_Inv;
if (h_isChannelRelay(i) || channelType == ChType_Toggle) {
if (i <= 1) {
hprintf255(request, "<tr>");
}
if (CHANNEL_Check(i) != bToggleInv) {
poststr(request, "<td class='on'>ON</td>");
}
else {
poststr(request, "<td class='off'>OFF</td>");
}
if (i == CHANNEL_MAX - 1) {
poststr(request, "</tr>");
}
}
}
poststr(request, "</table>");
}
poststr(request, "<table>"); //Table default to 100% width in stylesheet
for (i = 0; i < CHANNEL_MAX; i++) {
// check ability to hide given channel from gui
if (BIT_CHECK(g_hiddenChannels, i)) {
continue; // hidden
}
channelType = CHANNEL_GetType(i);
bool bToggleInv = channelType == ChType_Toggle_Inv;
if (h_isChannelRelay(i) || channelType == ChType_Toggle || bToggleInv) {
const char* c;
const char* prefix;
if (i <= 1) {
hprintf255(request, "<tr>");
}
if (CHANNEL_Check(i) != bToggleInv) {
c = "bgrn";
}
else {
c = "bred";
}
poststr(request, "<td><form action=\"index\">");
hprintf255(request, "<input type=\"hidden\" name=\"tgl\" value=\"%i\">", i);
if (CHANNEL_ShouldAddTogglePrefixToUI(i)) {
prefix = "Toggle ";
}
else {
prefix = "";
}
hprintf255(request, "<input class=\"%s\" type=\"submit\" value=\"%s%s\"/></form></td>", c, prefix, CHANNEL_GetLabel(i));
if (i == CHANNEL_MAX - 1) {
poststr(request, "</tr>");
}
}
}
poststr(request, "</table>");
poststr(request, "<table>"); //Table default to 100% width in stylesheet
for (i = 0; i < PLATFORM_GPIO_MAX; i++) {
int role;
role = PIN_GetPinRoleForPinIndex(i);
if (IS_PIN_DHT_ROLE(role)) {
// DHT pin has two channels - temperature and humidity
poststr(request, "<tr><td>");
ch1 = PIN_GetPinChannelForPinIndex(i);
ch2 = PIN_GetPinChannel2ForPinIndex(i);
iValue = CHANNEL_Get(ch1);
hprintf255(request, "Sensor %s on pin %i temperature %.2fC", PIN_RoleToString(role), i, (float)(iValue * 0.1f));
iValue = CHANNEL_Get(ch2);
hprintf255(request, ", humidity %.1f%%<br>", (float)iValue);
if (ch1 == ch2) {
hprintf255(request, "WARNING: you have the same channel set twice for DHT, please fix in pins config, set two different channels");
}
poststr(request, "</td></tr>");
}
}
for (i = 0; i < CHANNEL_MAX; i++) {
const char **types;
int numTypes;
// check ability to hide given channel from gui
if (BIT_CHECK(g_hiddenChannels, i)) {
continue; // hidden
}
channelType = CHANNEL_GetType(i);
if (channelType == ChType_TimerSeconds) {
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
hprintf255(request, "Timer Channel %s value ", CHANNEL_GetLabel(i));
if (iValue < 60) {
hprintf255(request, "%i seconds<br>", iValue);
}
else if (iValue < 3600) {
int minutes = iValue / 60;
int seconds = iValue % 60;
hprintf255(request, "%i minutes %i seconds<br>", minutes, seconds);
}
else {
int hours = iValue / 3600;
int remainingSeconds = iValue % 3600;
int minutes = remainingSeconds / 60;
int seconds = remainingSeconds % 60;
hprintf255(request, "%i hours %i minutes %i seconds<br>", hours, minutes, seconds);
}
poststr(request, "</td></tr>");
} else if (channelType == ChType_ReadOnlyLowMidHigh) {
const char* types[] = { "Low","Mid","High" };
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
if (iValue >= 0 && iValue <= 2) {
hprintf255(request, "Channel %s = %s", CHANNEL_GetLabel(i), types[iValue]);
}
else {
hprintf255(request, "Channel %s = %i", CHANNEL_GetLabel(i), iValue);
}
poststr(request, "</td></tr>");
}
else if ((types = Channel_GetOptionsForChannelType(channelType, &numTypes)) != 0) {
const char *what;
if (channelType == ChType_OffOnRemember) {
what = "memory";
}
else if (channelType == ChType_OpenStopClose || channelType == ChType_StopUpDown) {
what = "mode";
}
else {
what = "speed";
}
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
hprintf255(request, "<p>Select %s:</p><form action=\"index\">", what);
hprintf255(request, "<input type=\"hidden\" name=\"setIndex\" value=\"%i\">", i);
for (j = 0; j < numTypes; j++) {
const char* check;
if (j == iValue)
check = "checked";
else
check = "";
hprintf255(request, "<input type=\"radio\" name=\"set\" value=\"%i\" onchange=\"this.form.submit()\" %s>%s", j, check, types[j]);
}
hprintf255(request, "</form>");
poststr(request, "</td></tr>");
}
else if (channelType == ChType_TextField) {
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
hprintf255(request, "<p>Change channel %s value:</p><form action=\"index\">", CHANNEL_GetLabel(i));
hprintf255(request, "<input type=\"hidden\" name=\"setIndex\" value=\"%i\">", i);
hprintf255(request, "<input type=\"number\" name=\"set\" value=\"%i\" onblur=\"this.form.submit()\">", iValue);
hprintf255(request, "<input type=\"submit\" value=\"Set!\"/></form>");
hprintf255(request, "</form>");
poststr(request, "</td></tr>");
}
else if (channelType == ChType_ReadOnly) {
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
hprintf255(request, "Channel %s = %i", CHANNEL_GetLabel(i), iValue);
poststr(request, "</td></tr>");
}
else if (channelType == ChType_Motion || channelType == ChType_Motion_n) {
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
if (iValue == (channelType != ChType_Motion)) {
hprintf255(request, "No motion (ch %i)", i);
}
else {
hprintf255(request, "Motion! (ch %i)", i);
}
poststr(request, "</td></tr>");
}
else if (channelType == ChType_OpenClosed) {
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
if (iValue) {
hprintf255(request, "CLOSED (ch %i)", i);
}
else {
hprintf255(request, "OPEN (ch %i)", i);
}
poststr(request, "</td></tr>");
}
else if (channelType == ChType_OpenClosed_Inv) {
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
if (!iValue) {
hprintf255(request, "CLOSED (ch %i)", i);
}
else {
hprintf255(request, "OPEN (ch %i)", i);
}
poststr(request, "</td></tr>");
}
else if (h_isChannelRelay(i) || channelType == ChType_Toggle || channelType == ChType_Toggle_Inv) {
// HANDLED ABOVE in previous loop
}
else if ((bRawPWMs && h_isChannelPWM(i)) ||
(channelType == ChType_Dimmer) || (channelType == ChType_Dimmer256) || (channelType == ChType_Dimmer1000)
|| channelType == ChType_Percent) {
int maxValue;
// PWM and dimmer both use a slider control
inputName = h_isChannelPWM(i) ? "pwm" : "dim";
int pwmValue;
if (channelType == ChType_Dimmer256) {
maxValue = 255;
}
else if (channelType == ChType_Dimmer1000) {
maxValue = 1000;
}
else {
maxValue = 100;
}
pwmValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
hprintf255(request, "Channel %s:<br><form action=\"index\" id=\"form%i\">", CHANNEL_GetLabel(i), i);
hprintf255(request, "<input type=\"range\" min=\"0\" max=\"%i\" name=\"%s\" id=\"slider%i\" value=\"%i\" onchange=\"this.form.submit()\">", maxValue, inputName, i, pwmValue);
hprintf255(request, "<input type=\"hidden\" name=\"%sIndex\" value=\"%i\">", inputName, i);
hprintf255(request, "<input type=\"submit\" class='disp-none' value=\"Toggle %s\"/></form>", CHANNEL_GetLabel(i));
poststr(request, "</td></tr>");
}
else if (channelType == ChType_OffDimBright) {
const char* types[] = { "Off","Dim","Bright" };
iValue = CHANNEL_Get(i);
poststr(request, "<tr><td>");
hprintf255(request, "<p>Select level:</p><form action=\"index\">");
hprintf255(request, "<input type=\"hidden\" name=\"setIndex\" value=\"%i\">", i);
for (j = 0; j < 3; j++) {
const char* check;
if (j == iValue)
check = "checked";
else
check = "";
hprintf255(request, "<input type=\"radio\" name=\"set\" value=\"%i\" onchange=\"this.form.submit()\" %s>%s", j, check, types[j]);
}
hprintf255(request, "</form>");
poststr(request, "</td></tr>");
}
else {
const char *channelTitle;
channelTitle = ChannelType_GetTitle(channelType);
if (*channelTitle) {
int div;
const char *channelUnit;
char formatStr[16];
strcpy(formatStr, " %.4f");
div = ChannelType_GetDivider(channelType);
channelUnit = ChannelType_GetUnit(channelType);
iValue = CHANNEL_Get(i);
fValue = (float)iValue / (float)div;
poststr(request, "<tr><td>");
poststr(request, channelTitle);
// how many decimal places?
formatStr[3] = '0'+ChannelType_GetDecimalPlaces(channelType);
hprintf255(request, formatStr, fValue);
poststr(request, channelUnit);
hprintf255(request, " (%s)", CHANNEL_GetLabel(i));
poststr(request, "</td></tr>");
}
}
}
bool bForceShowSingleDimmer = 0;
#if ENABLE_DRIVER_GOSUNDSW2
if (DRV_IsRunning("GosundSW2")) {
bForceShowSingleDimmer = 1;
}
#endif
#if ENABLE_LED_BASIC
if (bRawPWMs == 0 || bForceShowRGBCW || bForceShowRGB
|| bForceShowSingleDimmer || LED_IsLedDriverChipRunning()) {
int c_pwms;
int lm;
int c_realPwms = 0;
lm = LED_GetMode();
//c_pwms = PIN_CountPinsWithRoleOrRole(IOR_PWM, IOR_PWM_n);
// This will treat multiple PWMs on a single channel as one.
// Thanks to this users can turn for example RGB LED controller
// into high power 3-outputs single colors LED controller
PIN_get_Relay_PWM_Count(0, &c_pwms, 0);
c_realPwms = c_pwms;
if (LED_IsLedDriverChipRunning()) {
c_pwms = CFG_CountLEDRemapChannels();
}
if (bForceShowSingleDimmer) {
c_pwms = 1;
}
else if (bForceShowRGBCW) {
c_pwms = 5;
}
else if (bForceShowRGB) {
c_pwms = 3;
}
if (c_pwms > 0) {
const char* c;
if (CHANNEL_Check(SPECIAL_CHANNEL_LEDPOWER)) {
c = "bgrn";
}
else {
c = "bred";
}
poststr(request, "<tr><td>");
poststr(request, "<form action=\"index\">");
hprintf255(request, "<input type=\"hidden\" name=\"tgl\" value=\"%i\">", SPECIAL_CHANNEL_LEDPOWER);
hprintf255(request, "<input class=\"%s\" type=\"submit\" value=\"Toggle Light\"/></form>", c);
poststr(request, "</td></tr>");
}
if (c_pwms > 0) {
int pwmValue;
inputName = "dim";
pwmValue = LED_GetDimmer();
poststr(request, "<tr><td>");
hprintf255(request, "<h5>LED Dimmer/Brightness</h5>");
hprintf255(request, "<form action=\"index\" id=\"form%i\">", SPECIAL_CHANNEL_BRIGHTNESS);
hprintf255(request, "<input type=\"range\" min=\"0\" max=\"100\" name=\"%s\" id=\"slider%i\" value=\"%i\" onchange=\"this.form.submit()\">", inputName, SPECIAL_CHANNEL_BRIGHTNESS, pwmValue);
hprintf255(request, "<input type=\"hidden\" name=\"%sIndex\" value=\"%i\">", inputName, SPECIAL_CHANNEL_BRIGHTNESS);
hprintf255(request, "<input type=\"submit\" class='disp-none' value=\"Toggle %i\"/></form>", SPECIAL_CHANNEL_BRIGHTNESS);
poststr(request, "</td></tr>");
}
if (c_pwms >= 3) {
char colorValue[16];
inputName = "rgb";
const char* activeStr = "";
if (lm == Light_RGB) {
activeStr = "[ACTIVE]";
}
LED_GetBaseColorString(colorValue);
poststr(request, "<tr><td>");
hprintf255(request, "<h5>LED RGB Color %s</h5>", activeStr);
hprintf255(request, "<form action=\"index\" id=\"form%i\">", SPECIAL_CHANNEL_BASECOLOR);
// onchange would fire only if colour was changed
// onblur will fire every time
hprintf255(request, "<input type=\"color\" name=\"%s\" id=\"color%i\" value=\"#%s\" oninput=\"this.form.submit()\" >", inputName, SPECIAL_CHANNEL_BASECOLOR, colorValue);
hprintf255(request, "<input type=\"hidden\" name=\"%sIndex\" value=\"%i\">", inputName, SPECIAL_CHANNEL_BASECOLOR);
hprintf255(request, "<input type=\"submit\" class='disp-none' value=\"Toggle Light\"/></form>");
poststr(request, "</td></tr>");
}
bool bShowCWForPixelAnim = false;
#if ENABLE_DRIVER_PIXELANIM
if (DRV_IsRunning("PixelAnim")) {
if (c_realPwms == 2)
bShowCWForPixelAnim = true;
PixelAnim_CreatePanel(request);
}
#endif
if (c_pwms == 2 || c_pwms >= 4 || bShowCWForPixelAnim) {
// TODO: temperature slider
int pwmValue;
const char* activeStr = "";
if (lm == Light_Temperature) {
activeStr = "[ACTIVE]";
}
inputName = "pwm";
pwmValue = LED_GetTemperature();
long pwmKelvin = HASS_TO_KELVIN(pwmValue);
long pwmKelvinMax = HASS_TO_KELVIN(led_temperature_min);
long pwmKelvinMin = HASS_TO_KELVIN(led_temperature_max);
poststr(request, "<tr><td>");
hprintf255(request, "<h5>LED Temperature Slider %s (%ld K) (Warm <--- ---> Cool)</h5>", activeStr, pwmKelvin);
hprintf255(request, "<form class='r' action=\"index\" id=\"form%i\">", SPECIAL_CHANNEL_TEMPERATURE);
//(KELVIN_TEMPERATURE_MAX - KELVIN_TEMPERATURE_MIN) / (HASS_TEMPERATURE_MAX - HASS_TEMPERATURE_MIN) = 13
hprintf255(request, "<input type=\"range\" step='13' min=\"%ld\" max=\"%ld\" ", pwmKelvinMin, pwmKelvinMax);
hprintf255(request, "value=\"%ld\" onchange=\"submitTemperature(this);\"/>", pwmKelvin);
hprintf255(request, "<input type=\"hidden\" name=\"%sIndex\" value=\"%i\"/>", inputName, SPECIAL_CHANNEL_TEMPERATURE);
hprintf255(request, "<input id=\"kelvin%i\" type=\"hidden\" name=\"%s\" />", SPECIAL_CHANNEL_TEMPERATURE, inputName);
poststr(request, "</form></td></tr>");
}
}
#endif
#if defined(PLATFORM_BEKEN) || defined(WINDOWS)
if (DRV_IsRunning("PWMToggler")) {
DRV_Toggler_AddToHtmlPage(request);
}
#endif
#if defined(PLATFORM_BEKEN) || defined(WINDOWS)
if (DRV_IsRunning("httpButtons")) {
DRV_HTTPButtons_AddToHtmlPage(request);
}
#endif
poststr(request, "</table>");
#ifndef OBK_DISABLE_ALL_DRIVERS
DRV_AppendInformationToHTTPIndexPage(request, false);
#endif
if (1) {
int bFirst = true;
hprintf255(request, "<h5>");
for (i = 0; i < CHANNEL_MAX; i++) {
if (CHANNEL_IsInUse(i)) {
float value = CHANNEL_GetFloat(i);
if (bFirst == false) {
hprintf255(request, ", ");
}
hprintf255(request, "Channel %i = %.2f", i, value);
bFirst = false;
}
}
if (1) {
i = RepeatingEvents_GetActiveCount();
if (i) {
if (bFirst == false) {
hprintf255(request, ", ");
}
hprintf255(request, "%i repeating events", i);
bFirst = false;
}
i = EventHandlers_GetActiveCount();
if (i) {
if (bFirst == false) {
hprintf255(request, ", ");
}
hprintf255(request, "%i event handlers", i);
bFirst = false;
}
#if defined(WINDOWS) || defined(PLATFORM_BEKEN)
i = CMD_GetCountActiveScriptThreads();
if (i) {
if (bFirst == false) {
hprintf255(request, ", ");
}
hprintf255(request, "%i script threads", i);
bFirst = false;
}
#endif
}
hprintf255(request, "</h5>");
}
hprintf255(request, "<h5>Cfg size: %i, change counter: %i, ota counter: %i, incomplete boots: %i</h5>",
sizeof(g_cfg), g_cfg.changeCounter, g_cfg.otaCounter, g_bootFailures);
// display temperature - thanks to giedriuslt
// only in Normal mode, and if boot is not failing
#ifndef NO_CHIP_TEMPERATURE
hprintf255(request, "<h5>Chip temperature: %.1f°C</h5>", g_wifi_temperature);
#endif
#if ENABLE_PING_WATCHDOG
inputName = CFG_GetPingHost();
if (inputName && *inputName && CFG_GetPingDisconnectedSecondsToRestart()) {
hprintf255(request, "<h5>Ping watchdog (%s) - ", inputName);
if (g_startPingWatchDogAfter > 0) {
hprintf255(request, "will start in %i!</h5>", g_startPingWatchDogAfter);
}
else {
hprintf255(request, "%i lost, %i ok, last reply was %is ago!</h5>",
PingWatchDog_GetTotalLost(), PingWatchDog_GetTotalReceived(), g_timeSinceLastPingReply);
}
}
#endif
if (Main_HasWiFiConnected())
{
int rssi = HAL_GetWifiStrength();
hprintf255(request, "<h5>Wifi RSSI: %s (%idBm)</h5>", str_rssi[wifi_rssi_scale(rssi)], rssi);
}
#if PLATFORM_BEKEN
/*
typedef enum {
RESET_SOURCE_POWERON = 0,
RESET_SOURCE_REBOOT = 1,
RESET_SOURCE_WATCHDOG = 2,
RESET_SOURCE_DEEPPS_GPIO = 3,
RESET_SOURCE_DEEPPS_RTC = 4,
RESET_SOURCE_CRASH_XAT0 = 5,
RESET_SOURCE_CRASH_UNDEFINED = 6,
RESET_SOURCE_CRASH_PREFETCH_ABORT = 7,
RESET_SOURCE_CRASH_DATA_ABORT = 8,
RESET_SOURCE_CRASH_UNUSED = 9,
} RESET_SOURCE_STATUS;
*/
{
const char* s = "Unk";
if (g_rebootReason == 0)
s = "Pwr";
else if (g_rebootReason == 1)
s = "Rbt";
else if (g_rebootReason == 2)
s = "Wdt";
else if (g_rebootReason == 3)
s = "Pin Interrupt";
else if (g_rebootReason == 4)
s = "Sleep Timer";
hprintf255(request, "<h5>Reboot reason: %i - %s</h5>", g_rebootReason, s);
}
#elif PLATFORM_BL602
char reason[26];
bl_sys_rstinfo_getsting(reason);
hprintf255(request, "<h5>Reboot reason: %s</h5>", reason);
#elif PLATFORM_LN882H
// type is chip_reboot_cause_t
g_rebootReason = ln_chip_get_reboot_cause();
{
const char* s = "Unk";
if (g_rebootReason == 0)
s = "Pwr";
else if (g_rebootReason == 1)
s = "Soft";
else if (g_rebootReason == 2)
s = "Wdt";
hprintf255(request, "<h5>Reboot reason: %i - %s</h5>", g_rebootReason, s);
}
#elif PLATFORM_ESPIDF
esp_reset_reason_t reason = esp_reset_reason();
const char* s = "Unknown";
switch(reason)
{
case ESP_RST_UNKNOWN: s = "ESP_RST_UNKNOWN"; break;
case ESP_RST_POWERON: s = "ESP_RST_POWERON"; break;
case ESP_RST_EXT: s = "ESP_RST_EXT"; break;
case ESP_RST_SW: s = "ESP_RST_SW"; break;
case ESP_RST_PANIC: s = "ESP_RST_PANIC"; break;
case ESP_RST_INT_WDT: s = "ESP_RST_INT_WDT"; break;
case ESP_RST_TASK_WDT: s = "ESP_RST_TASK_WDT"; break;
case ESP_RST_WDT: s = "ESP_RST_WDT"; break;
case ESP_RST_DEEPSLEEP: s = "ESP_RST_DEEPSLEEP"; break;
case ESP_RST_BROWNOUT: s = "ESP_RST_BROWNOUT"; break;
case ESP_RST_SDIO: s = "ESP_RST_SDIO"; break;
case ESP_RST_USB: s = "ESP_RST_USB"; break;
case ESP_RST_JTAG: s = "ESP_RST_JTAG"; break;
case ESP_RST_EFUSE: s = "ESP_RST_EFUSE"; break;
case ESP_RST_PWR_GLITCH: s = "ESP_RST_PWR_GLITCH"; break;
case ESP_RST_CPU_LOCKUP: s = "ESP_RST_CPU_LOCKUP"; break;
default: break;
}
hprintf255(request, "<h5>Reboot reason: %i - %s</h5>", reason, s);
#elif PLATFORM_RTL87X0C
const char* s = "Unk";
switch(reset_reason)
{
case HAL_RESET_REASON_POWER_ON: s = "Pwr"; break;
case HAL_RESET_REASON_SOFTWARE: s = "Soft"; break;
case HAL_RESET_REASON_WATCHDOG: s = "Wdt"; break;
default: break;
}
hprintf255(request, "<h5>Reboot reason: %i - %s</h5>", reset_reason, s);
hprintf255(request, "<h5>Current fw: FW%i</h5>", current_fw_idx);
#elif PLATFORM_RTL8710B || PLATFORM_RTL8720D || PLATFORM_REALTEK_NEW
hprintf255(request, "<h5>Current fw: FW%i</h5>", current_fw_idx + 1);
#elif PLATFORM_ECR6600
RST_TYPE reset_type = hal_get_reset_type();
const char* s;
switch(reset_type)
{
case RST_TYPE_POWER_ON: s = "POWER_ON"; break;
case RST_TYPE_FATAL_EXCEPTION: s = "FATAL_EXCEPTION"; break;
case RST_TYPE_SOFTWARE_REBOOT: s = "SOFTWARE_REBOOT"; break;
case RST_TYPE_HARDWARE_REBOOT: s = "HARDWARE_REBOOT"; break;
case RST_TYPE_OTA: s = "OTA"; break;
case RST_TYPE_WAKEUP: s = "WAKEUP"; break;
case RST_TYPE_HARDWARE_WDT_TIMEOUT: s = "HARDWARE_WDT_TIMEOUT"; break;
case RST_TYPE_SOFTWARE_WDT_TIMEOUT: s = "SOFTWARE_WDT_TIMEOUT"; break;
case RST_TYPE_UNKOWN: s = "UNKNOWN"; break;
default: s = "ERROR"; break;
}
hprintf255(request, "<h5>Reboot reason: %i - %s</h5>", reset_type, s);
#endif
#if ENABLE_MQTT
if (CFG_GetMQTTHost()[0] == 0) {
hprintf255(request, "<h5>MQTT State: not configured<br>");
}
else {
const char* stateStr;
const char* colorStr;
if (mqtt_reconnect > 0) {
stateStr = "awaiting reconnect";
colorStr = "orange";
}
else if (Main_HasMQTTConnected() == 1) {
stateStr = "connected";
colorStr = "green";
}
else {
stateStr = "disconnected";
colorStr = "yellow";
}
hprintf255(request, "<h5>MQTT State: <span style=\"color:%s\">%s</span> RES: %d(%s)<br>", colorStr,
stateStr, MQTT_GetConnectResult(), get_error_name(MQTT_GetConnectResult()));
hprintf255(request, "MQTT ErrMsg: %s <br>", (MQTT_GetStatusMessage() != NULL) ? MQTT_GetStatusMessage() : "");
hprintf255(request, "MQTT Stats: CONN: %d PUB: %d RECV: %d ERR: %d </h5>", MQTT_GetConnectEvents(),
MQTT_GetPublishEventCounter(), MQTT_GetReceivedEventCounter(), MQTT_GetPublishErrorCounter());
}
#endif
/* Format current PINS input state for all unused pins */