/
ovms_command.cpp
1030 lines (941 loc) · 27.9 KB
/
ovms_command.cpp
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
/*
; Project: Open Vehicle Monitor System
; Date: 14th March 2017
;
; Changes:
; 1.0 Initial release
;
; (C) 2011 Michael Stegen / Stegen Electronics
; (C) 2011-2017 Mark Webb-Johnson
; (C) 2011 Sonny Chen @ EPRO/DX
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in
; all copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
; THE SOFTWARE.
*/
#include "ovms_log.h"
static const char *TAG = "command";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <functional>
#include <esp_log.h>
#include <sys/types.h>
#include <dirent.h>
#include "freertos/FreeRTOS.h"
#include "ovms_command.h"
#include "ovms_config.h"
#include "ovms_events.h"
#include "ovms_peripherals.h"
#include "ovms_utils.h"
#include "ovms_script.h"
#include "buffered_shell.h"
#include "log_buffers.h"
OvmsCommandApp MyCommandApp __attribute__ ((init_priority (1000)));
bool CompareCharPtr::operator()(const char* a, const char* b)
{
return strcmp(a, b) < 0;
}
OvmsWriter::OvmsWriter()
{
std::string p = MyConfig.GetParamValue("password","module");
if (p.empty())
m_issecure = true;
else
m_issecure = false;
m_insert = NULL;
m_userData = NULL;
m_monitoring = false;
}
OvmsWriter::~OvmsWriter()
{
}
void OvmsWriter::Exit()
{
puts("This console cannot exit.");
}
void OvmsWriter::RegisterInsertCallback(InsertCallback cb, void* ctx)
{
m_insert = cb;
m_userData = ctx;
}
void OvmsWriter::DeregisterInsertCallback(InsertCallback cb)
{
if (m_insert == cb)
{
m_insert = NULL;
m_userData = NULL;
finalise();
ProcessChar('\n');
}
}
bool OvmsWriter::IsSecure()
{
return m_issecure;
}
void OvmsWriter::SetSecure(bool secure)
{
m_issecure = secure;
}
OvmsCommand* OvmsCommandMap::FindUniquePrefix(const char* key)
{
int len = strlen(key);
OvmsCommand* found = NULL;
for (iterator it = begin(); it != end(); ++it)
{
if (strncmp(it->first, key, len) == 0)
{
if (len == strlen(it->first))
{
return it->second;
}
if (found)
{
return NULL;
}
else
{
found = it->second;
}
}
}
return found;
}
OvmsCommand::OvmsCommand()
{
m_parent = NULL;
}
OvmsCommand::OvmsCommand(const char* name, const char* title, void (*execute)(int, OvmsWriter*, OvmsCommand*, int, const char* const*),
const char *usage, int min, int max, bool secure)
{
m_name = name;
m_title = title;
m_execute = execute;
m_usage_template= usage;
m_min = min;
m_max = max;
m_parent = NULL;
m_secure = secure;
}
OvmsCommand::~OvmsCommand()
{
}
const char* OvmsCommand::GetName()
{
return m_name;
}
const char* OvmsCommand::GetTitle()
{
return m_title;
}
// Dynamic generation of "Usage:" messages. Syntax of the usage template string:
// - Prefix is "Usage: " followed by names from ancestors (if any) and name of self
// - "$C" expands to children as child1|child2|child3
// - "[$C]" expands to optional children as [child1|child2|child3]
// - $G$ expands to the usage of the first child (typically used after $C)
// - $Gfoo$ expands to the usage of the child named "foo"
// - Parameters after command and subcommand tokens may be explicit like " <metric>"
// - Empty usage template "" defaults to "$C" for non-terminal OvmsCommand
const char* OvmsCommand::GetUsage(OvmsWriter* writer)
{
std::string usage = !m_usage_template ? "" : (!*m_usage_template && !m_execute) ? "$C" : m_usage_template;
m_usage ="Usage: ";
size_t pos = m_usage.size();
for (OvmsCommand* parent = m_parent; parent && parent->m_parent; parent = parent->m_parent)
{
m_usage.insert(pos, " ");
m_usage.insert(pos, parent->m_name);
}
m_usage += m_name;
m_usage += " ";
ExpandUsage(usage, writer);
return m_usage.c_str();
}
void OvmsCommand::ExpandUsage(std::string usage, OvmsWriter* writer)
{
size_t pos;
if ((pos = usage.find_first_of("$C")) != std::string::npos)
{
m_usage += usage.substr(0, pos);
pos += 2;
size_t z = m_usage.size();
bool found = false;
for (OvmsCommandMap::iterator it = m_children.begin(); it != m_children.end(); ++it)
{
if (!it->second->m_secure || writer->m_issecure)
{
if (found)
m_usage += "|";
m_usage += it->first;
found = true;
}
}
if (m_usage.size() == z)
{
m_usage = "All subcommands require 'enable' mode";
pos = usage.size(); // Don't append any more
}
}
else pos = 0;
size_t pos2;
if ((pos2 = usage.find_first_of("$G", pos)) != std::string::npos)
{
m_usage += usage.substr(pos, pos2-pos);
pos2 += 2;
size_t pos3;
OvmsCommandMap::iterator it = m_children.end();
if ((pos3 = usage.find_first_of("$", pos2)) != std::string::npos)
{
if (pos3 == pos2)
it = m_children.begin();
else
it = m_children.find(usage.substr(pos2, pos3-pos2).c_str());
pos = pos3 + 1;
if (it != m_children.end() && (!it->second->m_secure || writer->m_issecure))
{
OvmsCommand* child = it->second;
child->ExpandUsage(child->m_usage_template, writer);
m_usage += child->m_usage;
}
}
else
pos = pos2;
if (it == m_children.end())
m_usage += "ERROR IN USAGE TEMPLATE";
}
m_usage += usage.substr(pos);
}
OvmsCommand* OvmsCommand::RegisterCommand(const char* name, const char* title, void (*execute)(int, OvmsWriter*, OvmsCommand*, int, const char* const*),
const char *usage, int min, int max, bool secure)
{
OvmsCommand* cmd = new OvmsCommand(name, title, execute, usage, min, max, secure);
m_children[name] = cmd;
cmd->m_parent = this;
//printf("Registered '%s' under '%s'\n",name,m_title);
return cmd;
}
char ** OvmsCommand::Complete(OvmsWriter* writer, int argc, const char * const * argv)
{
if (argc <= 1)
{
return writer->GetCompletion(m_children, argc > 0 ? argv[0] : "");
}
OvmsCommand* cmd = m_children.FindUniquePrefix(argv[0]);
if (!cmd)
{
return writer->GetCompletion(m_children, NULL);
}
return cmd->Complete(writer, argc-1, ++argv);
}
void OvmsCommand::Execute(int verbosity, OvmsWriter* writer, int argc, const char * const * argv)
{
// if (argc>0)
// {
// printf("Execute(%s/%d) verbosity=%d argc=%d first=%s\n",m_title, m_children.size(), verbosity,argc,argv[0]);
// }
// else
// {
// printf("Execute(%s/%d) verbosity=%d (no args)\n",m_title, m_children.size(), verbosity);
// }
if (m_execute && (m_children.empty() || argc == 0))
{
//puts("Executing directly...");
if (argc < m_min || argc > m_max || (argc > 0 && strcmp(argv[argc-1],"?")==0))
{
writer->puts(GetUsage(writer));
return;
}
if ((!m_secure)||(m_secure && writer->m_issecure))
m_execute(verbosity,writer,this,argc,argv);
else
writer->puts("Error: Secure command requires 'enable' mode");
return;
}
else
{
//puts("Looking for a matching command");
if (argc <= 0)
{
writer->puts("Subcommand required");
writer->puts(GetUsage(writer));
return;
}
if (strcmp(argv[0],"?")==0)
{
if (m_usage_template && *m_usage_template && m_execute)
writer->puts(GetUsage(writer));
// Show available commands
int avail = 0;
for (OvmsCommandMap::iterator it=m_children.begin(); it!=m_children.end(); ++it)
{
if (it->second->IsSecure() && !writer->m_issecure)
continue;
const char* k = it->first;
const char* v = it->second->GetTitle();
writer->printf("%-20.20s %s\n",k,v);
++avail;
}
if (!avail)
writer->printf("All subcommands require 'enable' mode\n");
return;
}
OvmsCommand* cmd = m_children.FindUniquePrefix(argv[0]);
if (!cmd)
{
writer->puts("Unrecognised command");
if (!m_usage.empty())
writer->puts(GetUsage(writer));
return;
}
if (argc>1)
{
cmd->Execute(verbosity,writer,argc-1,++argv);
}
else
{
cmd->Execute(verbosity,writer,0,NULL);
}
}
}
OvmsCommand* OvmsCommand::GetParent()
{
return m_parent;
}
OvmsCommand* OvmsCommand::FindCommand(const char* name)
{
return m_children.FindUniquePrefix(name);
}
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
static duk_ret_t DukOvmsCommand(duk_context *ctx)
{
const char *cmd = duk_to_string(ctx,0);
if (cmd != NULL)
{
BufferedShell* bs = new BufferedShell(false, COMMAND_RESULT_NORMAL);
bs->SetSecure(true); // this is an authorized channel
bs->ProcessChars(cmd, strlen(cmd));
bs->ProcessChar('\n');
std::string val; bs->Dump(val);
delete bs;
duk_push_string(ctx, val.c_str());
return 1; /* one return value */
}
else
{
return 0;
}
}
#endif //#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
void help(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
writer->puts("Enter a single \"?\" to get the root command list.");
writer->puts("Commands can have multiple levels of subcommands.");
writer->puts("Use \"command [...] ?\" to get the list of subcommands and parameters.");
writer->puts("Commands can be abbreviated, push <TAB> for auto completion at any level");
writer->puts("including at the start of a subcommand to get a list of subcommands.");
writer->puts("Use \"enable\" to enter secure (admin) mode.");
}
void Exit(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
writer->Exit();
}
void log_level(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
const char* tag = "*";
if (argc > 0)
tag = argv[0];
const char* title = cmd->GetTitle();
esp_log_level_t level_num = (esp_log_level_t)(*(title+strlen(title)-2) - '0');
esp_log_level_set(tag, level_num);
writer->printf("Logging level for %s set to %s\n",tag,cmd->GetName());
}
void log_file(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
if (argc == 0)
{
MyCommandApp.SetLogfile("");
writer->puts("Closed log file");
return;
}
if (MyConfig.ProtectedPath(argv[0]))
{
writer->puts("Error: protected path");
return;
}
if (!MyCommandApp.SetLogfile(argv[0]))
{
writer->puts("Error: VFS file cannot be opened for append");
return;
}
writer->printf("Logging to file: %s\n", argv[0]);
}
void log_expire(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
if (MyCommandApp.m_expiretask)
{
writer->puts("Abort: expire task is currently running");
return;
}
int keepdays;
if (argc == 0)
keepdays = MyConfig.GetParamValueInt("log", "file.keepdays", 30);
else
keepdays = atoi(argv[0]);
MyCommandApp.ExpireLogFiles(verbosity, writer, keepdays);
}
static OvmsCommand* monitor;
static OvmsCommand* monitor_yes;
void log_monitor(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
bool state;
if (cmd == monitor)
state = !writer->IsMonitoring();
else
state = (cmd == monitor_yes);
writer->printf("Monitoring log messages %s\n", state ? "enabled" : "disabled");
writer->SetMonitoring(state);
}
typedef struct
{
std::string password;
int tries;
} PasswordContext;
bool enableInsert(OvmsWriter* writer, void* v, char ch)
{
PasswordContext* pc = (PasswordContext*)v;
if (ch == '\n')
{
std::string p = MyConfig.GetParamValue("password","module");
if (p.compare(pc->password) == 0)
{
writer->SetSecure(true);
writer->printf("\nSecure mode");
delete pc;
return false;
}
if (++pc->tries == 3)
{
writer->printf("\nError: %d incorrect password attempts", pc->tries);
vTaskDelay(5000 / portTICK_PERIOD_MS);
delete pc;
return false;
}
writer->printf("\nSorry, try again.\nPassword:");
pc->password.erase();
return true;
}
if (ch == 'C'-0100)
{
delete pc;
return false;
}
if (ch != '\r')
pc->password.append(1, ch);
return true;
}
void enable(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
std::string p = MyConfig.GetParamValue("password","module");
if ((p.empty())||((argc==1)&&(p.compare(argv[0])==0)))
{
writer->SetSecure(true);
writer->puts("Secure mode");
}
else if (argc == 1)
{
vTaskDelay(5000 / portTICK_PERIOD_MS);
writer->puts("Error: Invalid password");
}
else
{
PasswordContext* pc = new PasswordContext;
pc->tries = 0;
writer->printf("Password:");
writer->RegisterInsertCallback(enableInsert, pc);
}
}
void disable(int verbosity, OvmsWriter* writer, OvmsCommand* cmd, int argc, const char* const* argv)
{
writer->SetSecure(false);
}
OvmsCommandApp::OvmsCommandApp()
{
ESP_LOGI(TAG, "Initialising COMMAND (1000)");
m_logfile = NULL;
m_logfile_path = "";
m_logfile_size = 0;
m_logfile_maxsize = 0;
m_expiretask = 0;
m_root.RegisterCommand("help", "Ask for help", help, "", 0, 0);
m_root.RegisterCommand("exit", "End console session", Exit , "", 0, 0);
OvmsCommand* cmd_log = MyCommandApp.RegisterCommand("log","LOG framework",NULL, "", 0, 0, true);
cmd_log->RegisterCommand("file", "Start logging to specified file", log_file , "<vfspath>", 0, 1, true);
cmd_log->RegisterCommand("close", "Stop logging to file", log_file , "", 0, 0, true);
cmd_log->RegisterCommand("expire", "Expire old log files", log_expire, "[<keepdays>]", 0, 1, true);
OvmsCommand* level_cmd = cmd_log->RegisterCommand("level", "Set logging level", NULL, "$C [<tag>]");
level_cmd->RegisterCommand("verbose", "Log at the VERBOSE level (5)", log_level , "[<tag>]", 0, 1, true);
level_cmd->RegisterCommand("debug", "Log at the DEBUG level (4)", log_level , "[<tag>]", 0, 1, true);
level_cmd->RegisterCommand("info", "Log at the INFO level (3)", log_level , "[<tag>]", 0, 1, true);
level_cmd->RegisterCommand("warn", "Log at the WARN level (2)", log_level , "[<tag>]", 0, 1);
level_cmd->RegisterCommand("error", "Log at the ERROR level (1)", log_level , "[<tag>]", 0, 1);
level_cmd->RegisterCommand("none", "No logging (0)", log_level , "[<tag>]", 0, 1);
monitor = cmd_log->RegisterCommand("monitor", "Monitor log on this console", log_monitor , "[$C]", 0, 1, true);
monitor_yes = monitor->RegisterCommand("yes", "Monitor log", log_monitor , "", 0, 0, true);
monitor->RegisterCommand("no", "Don't monitor log", log_monitor , "", 0, 0, true);
m_root.RegisterCommand("enable","Enter secure mode (enable access to all commands)", enable, "[<password>]", 0, 1);
m_root.RegisterCommand("disable","Leave secure mode (disable access to most commands)", disable, "", 0, 0, true);
}
OvmsCommandApp::~OvmsCommandApp()
{
}
void OvmsCommandApp::ConfigureLogging()
{
MyConfig.RegisterParam("log", "Logging configuration", true, true);
using std::placeholders::_1;
using std::placeholders::_2;
MyEvents.RegisterEvent(TAG, "config.changed", std::bind(&OvmsCommandApp::EventHandler, this, _1, _2));
MyEvents.RegisterEvent(TAG, "sd.mounted", std::bind(&OvmsCommandApp::EventHandler, this, _1, _2));
MyEvents.RegisterEvent(TAG, "sd.unmounting", std::bind(&OvmsCommandApp::EventHandler, this, _1, _2));
MyEvents.RegisterEvent(TAG, "ticker.3600", std::bind(&OvmsCommandApp::EventHandler, this, _1, _2));
ReadConfig();
#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
ESP_LOGI(TAG, "Expanding DUKTAPE javascript engine");
duk_context* ctx = MyScripts.Duktape();
if (ctx)
{
duk_push_c_function(ctx, DukOvmsCommand, 1 /*nargs*/);
duk_put_global_string(ctx, "OvmsCommand");
}
#endif //#ifdef CONFIG_OVMS_SC_JAVASCRIPT_DUKTAPE
}
OvmsCommand* OvmsCommandApp::RegisterCommand(const char* name, const char* title, void (*execute)(int, OvmsWriter*, OvmsCommand*, int, const char* const*),
const char *usage, int min, int max, bool secure)
{
return m_root.RegisterCommand(name, title, execute, usage, min, max, secure);
}
OvmsCommand* OvmsCommandApp::FindCommand(const char* name)
{
return m_root.FindCommand(name);
}
void OvmsCommandApp::RegisterConsole(OvmsWriter* writer)
{
m_consoles.insert(writer);
}
void OvmsCommandApp::DeregisterConsole(OvmsWriter* writer)
{
m_consoles.erase(writer);
}
int OvmsCommandApp::Log(const char* fmt, ...)
{
va_list args;
va_start(args, fmt);
size_t ret = Log(fmt, args);
va_end(args);
return ret;
}
int OvmsCommandApp::Log(const char* fmt, va_list args)
{
LogBuffers* lb;
TaskHandle_t task = xTaskGetCurrentTaskHandle();
PartialLogs::iterator it = m_partials.find(task);
if (it == m_partials.end())
lb = new LogBuffers();
else
{
lb = it->second;
m_partials.erase(task);
}
int ret = LogBuffer(lb, fmt, args);
lb->set(m_consoles.size());
for (ConsoleSet::iterator it = m_consoles.begin(); it != m_consoles.end(); ++it)
{
(*it)->Log(lb);
}
return ret;
}
int OvmsCommandApp::LogPartial(const char* fmt, ...)
{
LogBuffers* lb;
TaskHandle_t task = xTaskGetCurrentTaskHandle();
PartialLogs::iterator it = m_partials.find(task);
if (it == m_partials.end())
{
lb = new LogBuffers();
m_partials[task] = lb;
}
else
{
lb = it->second;
}
va_list args;
va_start(args, fmt);
int ret = LogBuffer(lb, fmt, args);
va_end(args);
return ret;
}
int OvmsCommandApp::LogBuffer(LogBuffers* lb, const char* fmt, va_list args)
{
char *buffer;
int ret = vasprintf(&buffer, fmt, args);
if (ret < 0) return ret;
// replace CR/LF except last by "|", but don't leave '|' at the end
char* s;
for (s=buffer; *s; s++)
{
if ((*s=='\r' || *s=='\n') && *(s+1))
*s = '|';
}
for (--s; s > buffer; --s)
{
if (*s=='\r' || *s=='\n')
continue;
if (*s != '|')
break;
*s++ = '\n';
*s-- = '\0';
}
if (m_logfile)
{
// Log to the log file as well...
// We need to protect this with a mutex lock, as fsync appears to NOT be thread safe
// https://github.com/espressif/esp-idf/issues/1837
OvmsMutexLock fsynclock(&m_fsync_mutex);
m_logfile_size += fwrite(buffer,1,strlen(buffer),m_logfile);
if (m_logfile_maxsize && m_logfile_size > (m_logfile_maxsize*1024))
{
CycleLogfile();
}
else
{
fflush(m_logfile);
fsync(fileno(m_logfile));
}
}
lb->append(buffer);
return ret;
}
int OvmsCommandApp::HexDump(const char* tag, const char* prefix, const char* data, size_t length, size_t colsize /*=16*/)
{
char* buffer = NULL;
int rlength = (int)length;
while (rlength>0)
{
rlength = FormatHexDump(&buffer, data, rlength, colsize);
data += colsize;
ESP_LOGV(tag, "%s: %s", prefix, buffer);
}
if (buffer)
free(buffer);
return length;
}
char ** OvmsCommandApp::Complete(OvmsWriter* writer, int argc, const char * const * argv)
{
return m_root.Complete(writer, argc, argv);
}
void OvmsCommandApp::Execute(int verbosity, OvmsWriter* writer, int argc, const char * const * argv)
{
if (argc == 0)
{
writer->puts("Error: Empty command unrecognised");
}
else
{
m_root.Execute(verbosity, writer, argc, argv);
}
}
bool OvmsCommandApp::SetLogfile(std::string path)
{
// close old file:
if (m_logfile)
{
fclose(m_logfile);
m_logfile = NULL;
ESP_LOGI(TAG, "SetLogfile: file logging stopped");
}
// open new file:
if (!path.empty())
{
if (MyConfig.ProtectedPath(path))
{
ESP_LOGE(TAG, "SetLogfile: '%s' is a protected path", path.c_str());
return false;
}
#ifdef CONFIG_OVMS_COMP_SDCARD
if (startsWith(path, "/sd") && (!MyPeripherals || !MyPeripherals->m_sdcard || !MyPeripherals->m_sdcard->isavailable()))
{
ESP_LOGW(TAG, "SetLogfile: cannot open '%s', will retry on SD mount", path.c_str());
return false;
}
#endif // #ifdef CONFIG_OVMS_COMP_SDCARD
struct stat st;
if (stat(path.c_str(), &st) == 0)
m_logfile_size = st.st_size;
else
m_logfile_size = 0;
FILE* file = fopen(path.c_str(), "a+");
if (file == NULL)
{
ESP_LOGE(TAG, "SetLogfile: cannot open '%s'", path.c_str());
return false;
}
else
{
ESP_LOGI(TAG, "SetLogfile: now logging to file '%s'", path.c_str());
}
m_logfile = file;
}
return true;
}
void OvmsCommandApp::SetLoglevel(std::string tag, std::string level)
{
int level_num;
if (level == "verbose")
level_num = 5;
else if (level == "debug")
level_num = 4;
else if (level == "info")
level_num = 3;
else if (level == "warn")
level_num = 2;
else if (level == "error")
level_num = 1;
else if (level == "none")
level_num = 0;
else
level_num = CONFIG_LOG_DEFAULT_LEVEL;
if (tag.empty())
esp_log_level_set("*", (esp_log_level_t)level_num);
else
esp_log_level_set(tag.c_str(), (esp_log_level_t)level_num);
}
void OvmsCommandApp::CycleLogfile()
{
if (!m_logfile)
return;
fclose(m_logfile);
m_logfile = NULL;
char ts[20];
time_t tm = time(NULL);
strftime(ts, sizeof(ts), ".%Y%m%d-%H%M%S", localtime(&tm));
std::string archpath = m_logfile_path;
archpath.append(ts);
if (rename(m_logfile_path.c_str(), archpath.c_str()) == 0)
ESP_LOGI(TAG, "CycleLogfile: log file '%s' archived as '%s'", m_logfile_path.c_str(), archpath.c_str());
else
ESP_LOGE(TAG, "CycleLogfile: rename log file '%s' to '%s' failed", m_logfile_path.c_str(), archpath.c_str());
SetLogfile(m_logfile_path);
}
void OvmsCommandApp::ExpireLogFiles(int verbosity, OvmsWriter* writer, int keepdays)
{
if (keepdays <= 0)
{
if (writer)
writer->printf("Abort: expire disabled (keepdays=%d)\n", keepdays);
else
ESP_LOGD(TAG, "ExpireLogFiles: disabled (keepdays=%d)", keepdays);
return;
}
// get archive directory:
std::string archdir = m_logfile_path;
std::string::size_type p = archdir.find_last_of('/');
if (p == std::string::npos)
{
if (writer)
writer->puts("Error: log path not set");
else
ESP_LOGE(TAG, "ExpireLogFiles: log path not set");
return;
}
archdir.resize(p);
DIR *dir = opendir(archdir.c_str());
if (!dir)
{
if (writer)
writer->printf("Error: cannot open log directory '%s'\n", archdir.c_str());
else
ESP_LOGE(TAG, "ExpireLogFiles: cannot open log directory '%s'", archdir.c_str());
return;
}
else if (writer && verbosity >= COMMAND_RESULT_NORMAL)
{
writer->printf("Scanning directory '%s'...\n", archdir.c_str());
}
time_t tm = time(NULL) - keepdays * 86400;
struct dirent *dp;
char path[PATH_MAX];
struct stat st;
int delcnt = 0;
while ((dp = readdir(dir)) != NULL)
{
snprintf(path, sizeof(path), "%s/%s", archdir.c_str(), dp->d_name);
if (strncmp(path, m_logfile_path.c_str(), m_logfile_path.size()) != 0)
continue;
if (stat(path, &st))
{
if (writer)
writer->printf("Error: cannot stat '%s'\n", path);
else
ESP_LOGE(TAG, "ExpireLogFiles: cannot stat '%s'", path);
continue;
}
if (st.st_mtime < tm)
{
if (writer && verbosity >= COMMAND_RESULT_NORMAL)
writer->printf("Deleting '%s'\n", path);
else
ESP_LOGD(TAG, "ExpireLogFiles: deleting '%s'", path);
if (unlink(path))
{
if (writer)
writer->printf("Error: cannot delete '%s'\n", path);
else
ESP_LOGE(TAG, "ExpireLogFiles: cannot delete '%s'", path);
}
else
delcnt++;
}
}
closedir(dir);
if (writer)
writer->printf("Done, %d file(s) deleted.\n", delcnt);
else
ESP_LOGI(TAG, "ExpireLogFiles: %d file(s) deleted", delcnt);
}
void OvmsCommandApp::ExpireTask(void* data)
{
int keepdays = MyConfig.GetParamValueInt("log", "file.keepdays", 30);
MyCommandApp.ExpireLogFiles(0, NULL, keepdays);
MyCommandApp.m_expiretask = 0;
vTaskDelete(NULL);
}
void OvmsCommandApp::EventHandler(std::string event, void* data)
{
if (event == "config.changed")
{
OvmsConfigParam* param = (OvmsConfigParam*) data;
if (param && param->GetName() == "log")
ReadConfig();
}
else if (event == "sd.mounted")
{
if (startsWith(m_logfile_path, "/sd"))
SetLogfile(m_logfile_path);
}
else if (event == "sd.unmounting")
{
if (startsWith(m_logfile_path, "/sd"))
SetLogfile("");
}
else if (event == "ticker.3600")
{
int keepdays = MyConfig.GetParamValueInt("log", "file.keepdays", 30);
time_t utm = time(NULL);
struct tm* ltm = localtime(&utm);
if (keepdays && ltm->tm_hour == 0 && !m_expiretask)
xTaskCreatePinnedToCore(ExpireTask, "OVMS ExpireLogs", 4096, NULL, 0, &m_expiretask, 1);
}
}
void OvmsCommandApp::ReadConfig()
{
OvmsConfigParam* param = MyConfig.CachedParam("log");
// configure log levels:
std::string level = MyConfig.GetParamValue("log", "level");
if (!level.empty())
SetLoglevel("*", level);
for (auto const& kv : param->m_map)
{
if (startsWith(kv.first, "level.") && !kv.second.empty())
SetLoglevel(kv.first.substr(6), kv.second);
}
// configure log file:
m_logfile_maxsize = MyConfig.GetParamValueInt("log", "file.maxsize", 1024);
if (MyConfig.GetParamValueBool("log", "file.enable", false) == false)
m_logfile_path = "";
else
m_logfile_path = MyConfig.GetParamValue("log", "file.path");
if (!m_logfile_path.empty())
SetLogfile(m_logfile_path);
}
OvmsCommandTask::OvmsCommandTask(int _verbosity, OvmsWriter* _writer, OvmsCommand* _cmd, int _argc, const char* const* _argv)
: TaskBase(_cmd->GetName())
{
m_state = OCS_Init;
// clone command arguments:
verbosity = _verbosity;
writer = _writer;
cmd = _cmd;
argc = _argc;
if (argc == 0)
argv = NULL;
else
{
argv = (char**) ExternalRamMalloc(argc * sizeof(char*));
for (int i=0; i < argc; i++)
argv[i] = strdup(_argv[i]);
}
}
OvmsCommandState_t OvmsCommandTask::Prepare()
{
return (writer->IsInteractive()) ? OCS_RunLoop : OCS_RunOnce;
}
bool OvmsCommandTask::Run()
{
m_state = Prepare();
switch (m_state)
{
case OCS_RunLoop:
// start task:
writer->RegisterInsertCallback(Terminator, (void*) this);
if (!Instantiate())
{
delete this;
return false;
}
return true;
break;
case OCS_RunOnce:
Service();
Cleanup();
delete this;