-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands_v3.c
1643 lines (1302 loc) · 62.4 KB
/
commands_v3.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
/*
commands_v3.c - An embedded CNC Controller with rs274/ngc (g-code) support
WebUI backend for https://github.com/luc-github/ESP3D-webui
Part of grblHAL
Copyright (c) 2019-2024 Terje Io
grblHAL 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.
grblHAL 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 grblHAL. If not, see <http://www.gnu.org/licenses/>.
*/
#include "driver.h"
#if WEBUI_ENABLE == 1 || WEBUI_ENABLE == 3
#include <stdlib.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "../networking/networking.h"
#include "../networking/utils.h"
#include "../networking/strutils.h"
#include "../networking/cJSON.h"
#include "args.h"
#include "webui.h"
#include "fs_handlers.h"
#include "grbl/vfs.h"
#include "grbl/report.h"
#include "grbl/state_machine.h"
#include "grbl/motion_control.h"
#include "sdcard/sdcard.h"
#if WIFI_ENABLE
#include "wifi.h"
#endif
//#include "flashfs.h"
#define WEBUI_EOL "\n"
#define FIRMWARE_ID "80"
#define FIRMWARE_TARGET "grblHAL"
extern void data_is_json (void);
typedef struct {
setting_id_t id;
int8_t bit;
} webui_setting_map_t;
typedef struct {
webui_auth_level_t read;
webui_auth_level_t execute;
} webui_auth_required_t;
typedef struct webui_cmd_binding {
uint_fast16_t id;
status_code_t (*handler)(const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
webui_auth_required_t auth;
const char *help;
webui_setting_map_t setting;
} webui_cmd_binding_t;
//typedef status_code_t (*webui_command_handler_ptr)(const webui_cmd_binding_t *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static cJSON *json_create_response_hdr (uint_fast16_t cmd, bool array, bool ok, cJSON **data, const char *msg);
static bool json_write_response (cJSON *json, vfs_file_t *file);
static status_code_t list_commands (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t esp_setting (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t get_settings (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t set_setting (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t get_set_time (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t get_system_status (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t get_firmware_spec (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t get_current_ip (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t set_cpu_state (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t show_pins (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t handle_job_status (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t get_sd_status (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t global_fs_ls (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t global_fs_action (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t flashfs_ls (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t flashfs_action (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t sdcard_ls (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t sdcard_action (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t sdcard_format (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
#if WIFI_ENABLE
static status_code_t get_ap_list (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
#endif
static status_code_t flash_read_file (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static status_code_t flash_format (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file);
static const webui_cmd_binding_t webui_commands[] = {
{ 0, list_commands, { WebUIAuth_Guest, WebUIAuth_None}, "(<command id>) - display this help" },
// Settings commands
#if WIFI_ENABLE
{ 100, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<ssid>) - display/set STA SSID", { Setting_WiFi_STA_SSID, -1 } },
{ 101, esp_setting, { WebUIAuth_Admin, WebUIAuth_Admin}, "<password> - set STA password", { Setting_WiFi_STA_Password, -1 } },
{ 102, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<DHCP|STATIC>) - display/set STA IP mode", { Setting_IpMode, -1 } },
{ 103, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(IP=<ipv4> MSK=<ipv4> GW=<ipv4>) - display/set STA IP/Mask/GW", { 0, -1 } },
{ 105, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<ssid>) - display/set AP SSID", { Setting_WiFi_AP_SSID, -1 } },
{ 106, esp_setting, { WebUIAuth_Admin, WebUIAuth_Admin}, "<password> - set AP password", { Setting_WiFi_AP_Password, -1 } },
{ 107, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<ipv4>) - display/set AP IP", { Setting_IpAddress2, -1 } },
{ 108, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<channel>) - display/set AP channel", { Setting_Wifi_AP_Channel, -1 } },
// { 110, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "<BT|WIFI-STA|WIFI-AP|WIFI-SETUP|ETH-STA|OFF> - display/set radio state", { Setting_WifiMode, -1 } },
#elif ETHERNET_ENABLE
{ 102, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<DHCP|STATIC>) - display/set Ethernet IP mode", { Setting_IpMode, -1 } },
{ 103, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(IP=<ipv4> MSK=<ipv4> GW=<ipv4>) - display/set Ethernet IP/Mask/GW", { 0, -1 } },
#endif
#if WIFI_ENABLE || ETHERNET_ENABLE
{ 111, get_current_ip, { WebUIAuth_Guest, WebUIAuth_Admin}, "- display current IP" },
{ 112, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<hostname>) - display/set Hostname", { Setting_Hostname, -1 } },
#if HTTP_ENABLE
{ 120, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<ON|OFF>) - display/set HTTP state", { Setting_NetworkServices, 2 } },
{ 121, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<port>) - display/set HTTP port", { Setting_HttpPort, -1 } },
#endif
#if TELNET_ENABLE
{ 130, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<ON|OFF>) - display/set Telnet state", { Setting_NetworkServices, 0 } },
{ 131, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<port>) - display/set Telnet port", { Setting_TelnetPort, -1 } },
#endif
{ 140, get_set_time, { WebUIAuth_Guest, WebUIAuth_Admin}, "(json=yes) - sync/display/set time"},
#if WEBSOCKET_ENABLE
{ 160, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<ON|OFF>) - display/set WebSocket state", { Setting_NetworkServices, 1 } },
{ 161, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<port>) - display/set WebSocket port", { Setting_WebSocketPort, -1 } },
#endif
#if FTP_ENABLE
{ 180, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<ON|OFF>) - display/set FTP state", { Setting_NetworkServices, 3 } },
{ 181, esp_setting, { WebUIAuth_Guest, WebUIAuth_Admin}, "(<port>) - display/set FTP port", { Setting_WebSocketPort, -1 } },
#endif
#endif
// Action commands
{ 200, get_sd_status, { WebUIAuth_User, WebUIAuth_Admin}, "(json=yes) (<RELEASE|REFRESH>) - display/set SD Card Status" },
{ 220, show_pins, { WebUIAuth_User, WebUIAuth_Admin}, "(json=yes) (<SNAP>) - show pins" },
{ 400, get_settings, { WebUIAuth_User, WebUIAuth_Admin}, " - display ESP3D settings in JSON" },
{ 401, set_setting, { WebUIAuth_Admin, WebUIAuth_Admin}, "P=<setting id> T=<type> V=<value> - set specific setting" },
#if WIFI_ENABLE
{ 410, get_ap_list, { WebUIAuth_User, WebUIAuth_Admin}, "(json=yes) - display available AP list (limited to 30) in plain/JSON" },
#endif
{ 420, get_system_status, { WebUIAuth_User, WebUIAuth_Admin}, "(json=yes) - display ESP3D current status in plain/JSON" },
{ 444, set_cpu_state, { WebUIAuth_Admin, WebUIAuth_Admin}, "<RESET|RESTART> - set ESP3D state" },
{ 700, flash_read_file, { WebUIAuth_User, WebUIAuth_Admin}, "<filename> - read local filesystem file" },
{ 701, handle_job_status, { WebUIAuth_Guest, WebUIAuth_Admin}, "(action=<PAUSE|RESUME|ABORT>) - query or control current job" },
{ 710, flash_format, { WebUIAuth_Admin, WebUIAuth_Admin}, "FORMATFS - format flash filesystem" },
{ 715, sdcard_format, { WebUIAuth_Admin, WebUIAuth_Admin}, "FORMATFS - format sd filesystem" },
{ 720, flashfs_ls, { WebUIAuth_Guest, WebUIAuth_Admin}, "<Root> json=<no> pwd=<admin password> - list flash file system" },
{ 730, flashfs_action, { WebUIAuth_Guest, WebUIAuth_Admin}, "<create|exists|remove|mkdir|rmdir>=<path> (json=no) - action on flash file system" },
{ 740, sdcard_ls, { WebUIAuth_Guest, WebUIAuth_Admin}, "<Root> json=<no> pwd=<admin password> - list sd file system" },
{ 750, sdcard_action, { WebUIAuth_Guest, WebUIAuth_Admin}, "<create|exists|remove|mkdir|rmdir>=<path> (json=no) - action on sd file system" },
{ 780, global_fs_ls, { WebUIAuth_Guest, WebUIAuth_Admin}, "<Root> json=<no> pwd=<admin password> - list global file system" },
{ 790, global_fs_action, { WebUIAuth_Guest, WebUIAuth_Admin}, "<create|exists|remove|mkdir|rmdir>=<path> (json=no) - action on global file system" },
{ 800, get_firmware_spec, { WebUIAuth_Guest, WebUIAuth_Guest}, "(json=yes) - display FW Informations in plain/JSON" }
};
status_code_t webui_v3_command_handler (uint32_t command, uint_fast16_t argc, char **argv, webui_auth_level_t auth_level, vfs_file_t *file)
{
bool json;
status_code_t status = Status_Unhandled;
// hal.delay_ms(100, NULL);
webui_trim_arg(&argc, argv, "pwd=");
json = webui_get_arg(argc, argv, "json=") == NULL || webui_get_bool_arg(argc, argv, "json=");
webui_trim_arg(&argc, argv, "json");
webui_trim_arg(&argc, argv, "json=");
uint32_t i = sizeof(webui_commands) / sizeof(webui_cmd_binding_t);
while(i) {
if(webui_commands[--i].id == command) {
#if WEBUI_AUTH_ENABLE
if(auth_level < (argc == 0 ? webui_commands[i].auth.read : webui_commands[i].auth.execute)) {
if(json)
json_write_response(json_create_response_hdr(webui_commands[i].id, false, true, NULL, "Wrong authentication level"), file);
else
vfs_puts("Wrong authentication level" ASCII_EOL, file);
status = auth_level < WebUIAuth_User ? Status_AuthenticationRequired : Status_AccessDenied;
} else
#endif
status = webui_commands[i].handler(&webui_commands[i], argc, argv, json, file);
i = 0;
}
}
return status;
}
// Shared functions
static cJSON *json_create_response_hdr (uint_fast16_t cmd, bool array, bool ok, cJSON **data, const char *msg)
{
bool success;
cJSON *root = cJSON_CreateObject();
if((success = root != NULL)) {
success &= !!cJSON_AddStringToObject(root, "cmd", uitoa((uint32_t)cmd));
success &= !!cJSON_AddStringToObject(root, "status", ok ? "ok" : "error");
if(msg)
success &= !!cJSON_AddStringToObject(root, "data", msg);
else if(data != NULL)
success &= !!(*data = (array ? cJSON_AddArrayToObject(root, "data") : cJSON_AddObjectToObject(root, "data")));
else
success = false;
}
if(!success && root) {
cJSON_Delete(root);
root = NULL;
}
return root;
}
static bool json_write_response (cJSON *json, vfs_file_t *file)
{
bool ok = false;
if(json) {
char *resp = cJSON_PrintUnformatted(json);
if((ok = !!resp)) {
cJSON_Delete(json);
data_is_json();
vfs_puts(resp, file);
cJSON_free(resp);
} else
cJSON_Delete(json);
}
return ok;
}
// Add value to the JSON response array
static bool json_add_value (cJSON *settings, char *id, char *value)
{
bool ok = true;
cJSON *setting;
if((ok = !!(setting = cJSON_CreateObject())))
{
ok = !!cJSON_AddStringToObject(setting, "id", id);
ok = ok & !!cJSON_AddStringToObject(setting, "value", value);
if(ok)
cJSON_AddItemToArray(settings, setting);
}
return ok;
}
// ESP0
static status_code_t list_commands (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file)
{
bool ok;
char buf[200];
int32_t cmd = -1;
const webui_cmd_binding_t *cmdp = NULL;
uint32_t i, n = sizeof(webui_commands) / sizeof(webui_cmd_binding_t);
cJSON *root = NULL;
if(!(ok = argc == 0)) {
cmd = atoi(argv[0]);
for(i = 0; i < n; i++) {
if(webui_commands[i].id == cmd) {
cmdp = &webui_commands[i];
break;
}
}
if(cmdp == NULL)
strappend(buf, 2, "This command is not supported: ", argv[0]);
else
sprintf(buf, "[ESP%d]%s", cmdp->id, cmdp->help);
if(json) {
cJSON *data;
if((ok = !!(root = json_create_response_hdr(command->id, false, cmdp != NULL, &data, cmdp == NULL ? buf : NULL)))) {
if(cmdp) {
ok = ok & !!cJSON_AddStringToObject(data, "id", uitoa(cmdp->id));
ok = ok & !!cJSON_AddStringToObject(data, "help", buf);
}
}
} else {
vfs_puts(buf, file);
vfs_puts(WEBUI_EOL, file);
}
} else if(json) {
cJSON *data, *msg;
if((ok = !!(root = json_create_response_hdr(command->id, true, true, &data, NULL)))) {
for(i = 0; i < n; i++) {
if((ok = !!(msg = cJSON_CreateObject())))
{
sprintf(buf, "[ESP%d]%s", webui_commands[i].id, webui_commands[i].help);
ok = ok & !!cJSON_AddStringToObject(msg, "id", uitoa(webui_commands[i].id));
ok = ok & !!cJSON_AddStringToObject(msg, "help", buf);
if(ok)
cJSON_AddItemToArray(data, msg);
}
}
}
} else {
vfs_puts("[List of ESP3D commands]" WEBUI_EOL, file);
for(i = 0; i < n; i++) {
sprintf(buf, "[ESP%d]%s" WEBUI_EOL, webui_commands[i].id, webui_commands[i].help);
vfs_puts(buf, file);
}
}
if(root)
json_write_response(root, file);
return Status_OK;
}
// ESP100 - ESP181 except ESP111 and ESP140
static status_code_t sys_set_setting (setting_id_t id, char *value)
{
sys_state_t state = state_get();
status_code_t retval = Status_OK;
if(state == STATE_IDLE || (state & (STATE_ALARM|STATE_ESTOP|STATE_CHECK_MODE))) {
retval = settings_store_setting(id, value);
} else
retval = Status_IdleError;
return retval;
}
static char *get_setting_value (char *data, setting_id_t id)
{
char *value = NULL;
const setting_detail_t *setting = setting_get_details(id, NULL);
if(setting)
value = setting_get_value(setting, id - setting->id);
if(value)
strcpy(data, value);
return value ? data : NULL;
}
static status_code_t esp_setting (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file)
{
bool ok;
char response[100];
cJSON *root = NULL;
status_code_t status = Status_Unhandled;
if(command->setting.id != 0) { // straight mapping
const setting_detail_t *setting = setting_get_details(command->setting.id, NULL);
bool lookup = setting->datatype == Format_RadioButtons;
if(argc == 0) {
uint32_t value;
char buf[100], *svalue;
if(setting) {
status = Status_OK;
value = command->setting.bit == -1 ? setting_get_int_value(setting, 0) : ((setting_get_int_value(setting, 0) & (1 << command->setting.bit)) ? 1 : 0);
svalue = lookup ? strgetentry(buf, setting->format, value, ',') : (command->setting.bit == -1 ? strcpy(buf, setting_get_value(setting, 0)) : (value ? "ON" : "OFF"));
}
if(json) {
ok = !!(root = json_create_response_hdr(command->id, false, !!setting, NULL, svalue));
} else {
if(setting) {
status = Status_OK;
vfs_puts(strappend(response, 2, svalue, WEBUI_EOL), file);
}
}
} else {
char *param = argv[0];
if(command->setting.bit != -1) {
int32_t mode = strlookup(param, "OFF,ON", ',');
uint32_t pmask = 1 << command->setting.bit, tmask = setting_get_int_value(setting, 0);
if(mode >= 0) {
if(mode)
tmask |= pmask;
else
tmask ^= pmask;
param = uitoa(tmask);
}
} else if(lookup) {
int32_t value = strlookup(param, setting->format, ',');
if(value != -1)
param = uitoa((uint32_t)value);
}
status = sys_set_setting(command->setting.id, param);
if(json)
root = json_create_response_hdr(command->id, false, status == Status_OK, NULL, status == Status_OK ? "ok" : "Set failed");
}
} else switch(command->id) {
case 103: // GetSetSTA_IP:
status = Status_OK;
if(argc == 0) {
if(json) {
cJSON *data;
if((ok = !!(root = json_create_response_hdr(command->id, false, true, &data, NULL)))) {
const setting_detail_t *setting;
if((setting = setting_get_details(Setting_IpAddress, 0)))
ok = ok & !!cJSON_AddStringToObject(data, "ip", setting_get_value(setting, 0));
if((setting = setting_get_details(Setting_Gateway, 0)))
ok = ok & !!cJSON_AddStringToObject(data, "gw", setting_get_value(setting, 0));
if((setting = setting_get_details(Setting_NetMask, 0)))
ok = ok & !!cJSON_AddStringToObject(data, "msk", setting_get_value(setting, 0));
}
} else {
char ip[16], gw[16], mask[16];
sprintf(response, "IP:%s, GW:%s, MSK:%s\n", get_setting_value(ip, Setting_IpAddress), get_setting_value(gw, Setting_Gateway), get_setting_value(mask, Setting_NetMask));
vfs_puts(response, file);
}
} else {
char *ip;
bool found = false;
if((ip = webui_get_arg(argc, argv, "IP=")) && status == Status_OK) {
found = true;
status = sys_set_setting(Setting_IpAddress, ip);
}
if((ip = webui_get_arg(argc, argv, "GW=")) && status == Status_OK) {
found = true;
status = sys_set_setting(Setting_Gateway, ip);
}
if((ip = webui_get_arg(argc, argv, "MSK=")) && status == Status_OK) {
found = true;
status = sys_set_setting(Setting_NetMask, ip);
}
if(!found)
status = Status_Unhandled;
if(json)
root = json_create_response_hdr(command->id, false, status == Status_OK, NULL, status == Status_OK ? "ok" : "Set failed");
}
break;
default:
break;
}
if(root)
json_write_response(root, file);
if(status != Status_OK && !json)
vfs_puts("error:setting failure", file);
return status;
}
// ESP111
static status_code_t get_current_ip (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file)
{
char response[20];
if(json)
json_write_response(json_create_response_hdr(command->id, false, true, NULL, networking_get_info()->status.ip), file);
else
vfs_puts(strappend(response, 3, argv[0], networking_get_info()->status.ip, WEBUI_EOL), file);
return Status_OK;
}
// ESP140
static status_code_t get_set_time (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file)
{
bool ok = false;
char buf[50];
if(hal.rtc.get_datetime) {
if(argc) {
bool sync = !!webui_get_arg(argc, argv, "SYNC"),
now = !!webui_get_arg(argc, argv, "NOW"),
dst = webui_get_bool_arg(argc, argv, "dst=");
char *srv1 = webui_get_arg(argc, argv, "srv1="),
*srv2 = webui_get_arg(argc, argv, "srv2="),
*srv3 = webui_get_arg(argc, argv, "srv3="),
*time = webui_get_arg(argc, argv, "time="),
*dstp = webui_get_arg(argc, argv, "dst="),
*zone = webui_get_arg(argc, argv, "zone=");
webui_trim_arg(&argc, argv, "SYNC");
webui_trim_arg(&argc, argv, "NOW");
webui_trim_arg(&argc, argv, "dst=");
webui_trim_arg(&argc, argv, "srv1=");
webui_trim_arg(&argc, argv, "srv2=");
webui_trim_arg(&argc, argv, "srv3=");
webui_trim_arg(&argc, argv, "time=");
webui_trim_arg(&argc, argv, "zone=");
UNUSED(sync);
UNUSED(now);
UNUSED(dst);
UNUSED(srv1);
UNUSED(srv2);
UNUSED(srv3);
UNUSED(zone);
ok = argc == 0;
if(srv1 && ok) {
ok = false;
strcpy(buf, "Set server 1 failed");
}
if(srv2 && ok) {
ok = false;
strcpy(buf, "Set server 2 failed");
}
if(srv3 && ok) {
ok = false;
strcpy(buf, "Set server 3 failed");
}
if(zone && ok) {
ok = false;
strcpy(buf, "Set time zone failed");
}
if(dstp && ok) {
ok = false;
strcpy(buf, "Set daylight failed");
}
if(time && ok) {
struct tm *dt;
if(strlen(time) > 16) {
time[10] = 'T';
time[13] = ':';
time[16] = ':';
}
if(!(ok = hal.rtc.set_datetime && (dt = get_datetime(time)) && hal.rtc.set_datetime(dt)))
strcpy(buf, "Set time failed");
}
if(sync && ok) {
ok = false;
strcpy(buf, "Time is manual");
}
if(now && ok) {
struct tm time;
if((hal.rtc.get_datetime(&time)))
snprintf(buf, sizeof(buf), "%4d-%02d-%02dT%02d:%02d:%02d", time.tm_year + 1900, time.tm_mon + 1, time.tm_mday, time.tm_hour, time.tm_min, time.tm_sec);
else
strcpy(buf, "Time not available");
}
if(!ok || *buf == '\0' || argc) {
ok = false;
strcpy(buf, "No parameter");
}
if(json)
json_write_response(json_create_response_hdr(command->id, false, ok, NULL, buf), file);
else {
vfs_puts(buf, file);
vfs_puts(WEBUI_EOL, file);
}
} else {
if(json) {
cJSON *root, *data;
if((ok = !!(root = json_create_response_hdr(command->id, false, true, &data, NULL)))) {
ok = ok & !!cJSON_AddStringToObject(data, "srv1", "");
ok = ok & !!cJSON_AddStringToObject(data, "srv2", "");
ok = ok & !!cJSON_AddStringToObject(data, "srv3", "");
ok = ok & !!cJSON_AddStringToObject(data, "zone", "GMT");
ok = ok & !!cJSON_AddStringToObject(data, "dst", "NO");
json_write_response(root, file);
}
} else {
vfs_puts(strappend(buf, 3, "srv1=", "", WEBUI_EOL), file);
vfs_puts(strappend(buf, 3, "srv2=", "", WEBUI_EOL), file);
vfs_puts(strappend(buf, 3, "srv3=", "", WEBUI_EOL), file);
vfs_puts(strappend(buf, 3, "zone=", "GMT", WEBUI_EOL), file);
vfs_puts(strappend(buf, 3, "dst=", "NO", WEBUI_EOL), file);
}
}
} else {
if(json)
json_write_response(json_create_response_hdr(command->id, false, false, NULL, "N/A"), file);
else
vfs_puts("N/A" ASCII_EOL, file);
}
return Status_OK;
}
// ESP200
static status_code_t get_sd_status (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file)
{
char *msg;
if(argc == 1) {
bool refresh = !!webui_get_arg(argc, argv, "REFRESH"), release = !!webui_get_arg(argc, argv, "RELEASE");
UNUSED(refresh);
webui_trim_arg(&argc, argv, "REFRESH"); // Mount?
webui_trim_arg(&argc, argv, "RELEASE"); // Unmount?
msg = argc ? "Unknown parameter" : (release ? "SD card released" : "SD card ok");
} else
msg = hal.stream.type == StreamType_SDCard ? "Busy" : (sdcard_getfs() ? "SD card detected" : "Not available");
if(json)
json_write_response(json_create_response_hdr(command->id, false, true, NULL, msg), file);
else {
vfs_puts(msg, file);
vfs_puts(WEBUI_EOL, file);
}
return Status_OK;
}
// ESP220
static const char *get_pinname (pin_function_t function)
{
const char *name = NULL;
uint_fast8_t idx = sizeof(pin_names) / sizeof(pin_name_t);
do {
if(pin_names[--idx].function == function)
name = pin_names[idx].name;
} while(idx && !name);
return name ? name : "N/A";
}
static void show_pin_json (xbar_t *pin, void *data)
{
char id[10];
strcat(strcpy(id, (char *)pin->port), uitoa(pin->pin));
json_add_value((cJSON *)data, (char *)get_pinname(pin->function), id);
}
static void show_pin_txt (xbar_t *pin, void *file)
{
char buf[50];
vfs_puts(strappend(buf, 5, (char *)pin->port, uitoa(pin->pin), ": ", get_pinname(pin->function), WEBUI_EOL), (vfs_file_t *)file);
}
static status_code_t show_pins (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file)
{
if(json) {
cJSON *root, *data;
if(hal.enumerate_pins && !!(root = json_create_response_hdr(command->id, true, true, &data, NULL))) {
hal.enumerate_pins(false, show_pin_json, data);
json_write_response(root, file);
}
} else
hal.enumerate_pins(false, show_pin_txt, file);
return Status_OK; // for now
}
// ESP400
// Add setting to the JSON response array
static bool add_setting (cJSON *settings, const setting_detail_t *setting, int32_t bit, uint_fast16_t offset)
{
static const char *tmap[] = { "B", "M", "X", "B", "M", "I", "F", "S", "S", "A", "I", "I" };
bool ok;
cJSON *settingobj = NULL;
if((ok = setting && (setting->is_available == NULL || setting->is_available(setting)) && !!(settingobj = cJSON_CreateObject()))) {
char opt[50], name[50], *q;
uint_fast8_t suboffset = setting->flags.subgroups ? offset / setting->flags.increment : offset;
const setting_group_detail_t *group = setting_get_group_details(setting->group + (setting->flags.subgroups ? suboffset : 0));
if(setting->datatype == Format_Bool)
bit = 0;
if(setting->group == Group_Axis0) {
strcpy(name, setting->name + 1);
*name = CAPS(*name);
} else if((q = strchr(setting->name, '?'))) {
strncpy(name, setting->name, q - setting->name);
name[q - setting->name] = '\0';
strcat(name, uitoa(suboffset + 1));
strcat(name, q + 1);
} else
strcpy(name, setting->name);
strcpy(opt, group->name);
strcat(opt, "/");
strcat(opt, group->name);
ok = !!cJSON_AddStringToObject(settingobj, "F", opt);
strcpy(opt, uitoa(setting->id + offset));
if(bit >= 0) {
strcat(opt, "#");
strcat(opt, uitoa(bit));
}
ok = ok & !!cJSON_AddStringToObject(settingobj, "P", opt);
ok = ok & !!cJSON_AddStringToObject(settingobj, "T", tmap[setting->datatype]);
ok = ok & !!cJSON_AddStringToObject(settingobj, "V", bit == -1 ? setting_get_value(setting, offset) : setting_get_int_value(setting, offset) & (1 << bit) ? "1" : "0");
ok = ok & !!cJSON_AddStringToObject(settingobj, "H", bit == -1 || setting->datatype == Format_Bool ? name : strgetentry(opt, setting->format, bit, ','));
if(setting->unit)
ok = ok & !!cJSON_AddStringToObject(settingobj, "U", setting->unit);
if(setting->flags.reboot_required)
ok = ok & !!cJSON_AddStringToObject(settingobj, "R", "1");
if(ok) switch(setting->datatype) {
case Format_Bool:
case Format_Bitfield:
case Format_XBitfield:
case Format_RadioButtons:
{
cJSON *option, *options;
if((ok = !!(options = cJSON_AddArrayToObject(settingobj, "O")))) {
if(bit == -1) {
uint_fast16_t i, j = strnumentries(setting->format, ',');
for(i = 0; ok && i < j; i++) {
// option = cJSON_CreateObject();
// if(isv3 && i == 0)
// cJSON_AddStringToObject(option, strgetentry(opt, setting->format, i, ','), uitoa(1 << bit));
// else
if(strcmp(strgetentry(opt, setting->format, i, ','), "N/A")) {
if((ok = !!(option = cJSON_CreateObject()))) {
ok = !!cJSON_AddStringToObject(option, opt, uitoa(i));
if(!(ok = ok & !!cJSON_AddItemToArray(options, option)))
cJSON_Delete(option);
}
}
}
} else if((ok = !!(option = cJSON_CreateObject()))) {
ok = !!cJSON_AddStringToObject(option, "Enabled", "1");
if(!(ok = ok & !!cJSON_AddItemToArray(options, option)))
cJSON_Delete(option);
else {
ok = ok & !!(option = cJSON_CreateObject());
ok = ok & !!cJSON_AddStringToObject(option, "Disabled", "0");
if(!(ok = ok & !!cJSON_AddItemToArray(options, option)))
cJSON_Delete(option);
}
}
if(!ok)
cJSON_Delete(options);
}
}
break;
case Format_AxisMask:
{
uint_fast16_t i;
cJSON *option, *options;
if((ok = !!(options = cJSON_AddArrayToObject(settingobj, "O")))) {
for(i = 0; ok && i < N_AXIS; i++) {
if((ok = !!(option = cJSON_CreateObject()))) {
ok = ok & !!cJSON_AddStringToObject(option, axis_letter[i], uitoa(i));
if(!(ok = ok & !!cJSON_AddItemToArray(options, option)))
cJSON_Delete(option);
}
}
if(!ok)
cJSON_Delete(options);
}
}
break;
case Format_IPv4:
break;
default:
{
char whole[30] = "", fraction[10] = "";
const char *min = setting->min_value, *max = setting->max_value;
if(setting->format && (setting->datatype == Format_Decimal || setting_is_integer(setting))) {
char c, *s1 = (char *)setting->format, *s2 = whole;
while((c = *s1++)) {
if(c == '.')
s2 = fraction;
else {
*s2++ = '9';
*s2 = '\0';
}
}
if(*fraction != '\0')
strcat(strcat(strcpy(opt, whole), "."), fraction);
else
strcpy(opt, whole);
if(min == NULL)
min = *setting->format == '-' ? strcat(strcpy(whole, "-"), opt) : "0";
if(max == NULL)
max = opt;
if(setting->datatype == Format_Decimal)
ok = ok & !!cJSON_AddStringToObject(settingobj, "E", uitoa(strlen(fraction)));
}
if(min && !setting_is_list(setting)) {
ok = ok & !!cJSON_AddStringToObject(settingobj, "M", setting->flags.allow_null ? "0" : min);
if(setting->flags.allow_null)
ok = ok & !!cJSON_AddStringToObject(settingobj, "MS", min);
}
if(max)
ok = ok & !!cJSON_AddStringToObject(settingobj, "S", max);
}
break;
}
if(!(ok = ok && !!cJSON_AddItemToArray(settings, settingobj)))
cJSON_Delete(settingobj);
}
return ok;
}
static bool add_setting2 (const setting_detail_t *setting, uint_fast16_t offset, void *settings)
{
return add_setting((cJSON *)settings, setting, -1, offset);
}
static int cmp_settings (const void *a, const void *b)
{
uint32_t av = ((*(setting_detail_t **)(a))->group << 16) | (*(setting_detail_t **)(a))->id,
bv = ((*(setting_detail_t **)(b))->group << 16) | (*(setting_detail_t **)(b))->id;
return av - bv;
}
static status_code_t get_settings (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file)
{
bool ok;
uint_fast16_t idx, n_settings = 0;
cJSON *root = cJSON_CreateObject(), *settings = NULL;
setting_details_t *details = settings_get_details();
const setting_detail_t *setting;
setting_detail_t **all_settings, **psetting;
if((ok = root != NULL)) {
ok = ok & !!cJSON_AddStringToObject(root, "cmd", uitoa(command->id));
ok = ok & !!cJSON_AddStringToObject(root, "status", "ok");
}
if((ok = ok & !!(settings = cJSON_AddArrayToObject(root, "data")))) {
do {
n_settings += details->n_settings;
} while((details = details->next));
details = settings_get_details();
if((all_settings = psetting = calloc(n_settings, sizeof(setting_detail_t *)))) {
n_settings = 0;
do {
for(idx = 0; idx < details->n_settings; idx++) {
setting = &details->settings[idx];
if(setting->is_available == NULL || setting->is_available(setting)) {
*psetting++ = (setting_detail_t *)setting;
n_settings++;
}
}
} while((details = details->next));
qsort(all_settings, n_settings, sizeof(setting_detail_t *), cmp_settings);
for(idx = 0; ok && idx < n_settings; idx++)
ok = settings_iterator(all_settings[idx], add_setting2, settings);
free(all_settings);
} else do {
for(idx = 0; ok && idx < details->n_settings; idx++) {
setting = &details->settings[idx];
if(setting->is_available == NULL || setting->is_available(setting))
ok = settings_iterator(setting, add_setting2, settings);
}
} while((details = details->next));
}
if(root) {
if(ok)
json_write_response(root, file);
else
cJSON_Delete(root);
}
return Status_OK;
}
// ESP401
static status_code_t set_setting (const struct webui_cmd_binding *command, uint_fast16_t argc, char **argv, bool json, vfs_file_t *file)
{
bool ok = false;
char *setting_id = webui_get_arg(argc, argv, "P=");
char *value = webui_get_arg(argc, argv, "V=");
status_code_t status = Status_Unhandled;
if(setting_id && value) {
char *bitp;
const setting_detail_t *setting;
setting_id_t id = (setting_id_t)atoi(setting_id);
ok = !!(setting = setting_get_details(id, NULL));
// "hack" for bitfield settings
if(ok && (bitp = strchr(setting_id, '#'))) {
*bitp++ = '\0';
uint32_t pmask = 1 << atoi(bitp), tmask;
tmask = setting_get_int_value(setting, 0);
if(*value == '0')
tmask ^= pmask;
else
tmask |= pmask;
if(setting->datatype == Format_XBitfield && (tmask & 0x01) == 0)
tmask = 0;
value = uitoa(tmask);
}
if(ok) {
// Block disable of websocket daemon from WebUI
if(setting->id == Setting_NetworkServices) {
network_services_t services;
services.mask = (uint8_t)atoi(value);
if(!services.websocket) {
services.websocket = On;
value = uitoa(services.mask);
}
}