OOKwiz
on/off-keying for ESP32 and a variety of supported radio modules
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
CLI.cpp
Go to the documentation of this file.
1 #include "CLI.h"
2 #include "config.h"
3 #include "serial_output.h"
4 #include "tools.h"
5 #include "Settings.h"
6 #include "Radio.h"
7 #include "OOKwiz.h"
8 
9 
10 namespace CLI {
11 
12  bool cli_start_msg_printed = false;
13  bool semicolon_parsing = true;
15  void parse(String cmd);
16  void RFlinkParse(String cmd);
17 
18  void loop() {
19  if (!cli_start_msg_printed) {
20  INFO("CLI started on Serial. Type 'help' for list of commands.\n");
21  cli_start_msg_printed = true;
22  }
23  while (Serial.available()) {
24  char inp = Serial.read();
25  if (inp == ';' && serial_buffer == "10") {
26  // RFlink format uses semicolons, so when command starts with "10;",keep it together until eol
27  semicolon_parsing = false;
28  }
29  if (inp == char(13) || inp == char(10) || (inp == ';' && semicolon_parsing)) {
30  semicolon_parsing = true;
31  tools::trim(serial_buffer);
32  if (serial_buffer != "") {
33  if (serial_buffer.startsWith("10;")) {
34  RFlinkParse(serial_buffer);
35  } else {
36  parse(serial_buffer);
37  }
38  }
39  serial_buffer = "";
40  } else {
41  serial_buffer += inp;
42  }
43  }
44  }
45 
46  void parse(String cli_string) {
47  INFO("\nCLI: %s\n", cli_string.c_str());
48  String cmd;
49  String args;
50  tools::split(cli_string, " ", cmd, args);
51 
52  if (cmd == "help") {
53 INFO(R"(
54 OOKwiz version %s Command Line Interpreter help.
55 
56 Available commands:
57 
58 help - prints this message
59 set - shows current configuration settings
60 set x - sets configuration flag x
61 set x y - sets configuration value x to y
62 unset x - unsets a flag or variable
63 load [<file>] - loads the default saved settings, or from a named file in flash
64 save - saves to a file named 'default', which is what is used at boot time.
65 save [<file>] - saves the settings to a named file in SPIFFS flash
66 ls - lists stored configuration files in SPIFFS flash
67 rm <file> - deletes a configuration file
68 reboot - reboot using the saved defaults
69 standby - set radio to standby mode
70 receive - set radio to receive mode
71 sim <string> - Takes a RawTimings, Pulsetrain or Meaning string representation and
72  acts like it just came in off the air.
73 transmit <string> - Takes a RawTimings, Pulsetrain or Meaning string representation and
74  transmits it.
75 
76 rm default;reboot - restore factory settings
77 sr - shorthand for "save;reboot"
78 
79 
80 See the OOKwiz README.md on GitHub for a quick-start guide and full documentation
81 
82 )", OOKWIZ_VERSION);
83  return;
84  }
85 
86  if (cmd == "set") {
87  if (args == "") {
88  INFO("%s\n", Settings::list().c_str());
89  return;
90  }
91  SPLIT(args, " ", name, value);
92  // Works both with 'set x y' and 'set x=y'
93  if (value == "") {
94  tools::split(args, "=", name, value);
95  }
96  if (Settings::set(name, value)) {
97  if (value != "") {
98  INFO("'%s' set to '%s'\n", name.c_str(), value.c_str());
99  } else {
100  INFO("'%s' set\n", name.c_str());
101  }
102  }
103  return;
104  }
105 
106  if (cmd == "unset") {
107  if (Settings::unset(args)) {
108  INFO("Setting '%s' removed.\n", args.c_str());
109  }
110  return;
111  }
112 
113  if (cmd == "load") {
114  if (args == "") {
115  args = "default";
116  }
117  Settings::load(args);
118  return;
119  }
120 
121  if (cmd == "save") {
122  if (args == "") {
123  args = "default";
124  }
125  Settings::save(args);
126  return;
127  }
128 
129  if (cmd == "ls") {
131  return;
132  }
133 
134  if (cmd == "rm") {
135  Settings::rm(args);
136  return;
137  }
138 
139  if (cmd == "reboot") {
140  ESP.restart();
141  return;
142  }
143 
144  if (cmd == "receive") {
145  if (OOKwiz::receive()) {
146  INFO("Receiver active, waiting for pulses.\n");
147  }
148  return;
149  }
150 
151  if (cmd == "standby") {
152  if (OOKwiz::standby()) {
153  INFO("Transceiver placed in standby mode.\n");
154  }
155  return;
156  }
157 
158  if (cmd == "transmit") {
159  OOKwiz::transmit(args);
160  return;
161  }
162 
163  if (cmd == "sim") {
164  OOKwiz::simulate(args);
165  return;
166  }
167 
168  if (cmd == "sr") {
169  if (Settings::save("default")) {
170  ESP.restart();
171  }
172  return;
173  }
174 
175  INFO("Unknown command '%s'. Enter 'help' for a list of commands.\n", cmd.c_str());
176  }
177 
178  void RFlinkParse(String cmd) {
179  INFO("\nCLI: %s\n", cmd.c_str());
180  cmd = cmd.substring(3); // get rid of "10;"
181 
182  // This may parse further commands later, now it just assumes the first term in semicolons
183  // after "10;" is a plugin name and the rest is what is to be transmitted.
184  String plugin_name;
185  String txString;
186  tools::split(cmd, ";", plugin_name, txString);
187  Device::transmit(plugin_name, txString);
188  }
189 
190 }