Commander-API  V2.0.0
Commander-API is a C++ API, that parses character based commands.
Commander-API.hpp
Go to the documentation of this file.
1 /*
2  * Created on June 18 2020
3  *
4  * Copyright (c) 2020 - Daniel Hajnal
5  * hajnal.daniel96@gmail.com
6  * This file is part of the Commander-API project.
7  * Modified 2022.02.06
8 */
9 
10 /*
11 MIT License
12 
13 Copyright (c) 2020 Daniel Hajnal
14 
15 Permission is hereby granted, free of charge, to any person obtaining a copy
16 of this software and associated documentation files (the "Software"), to deal
17 in the Software without restriction, including without limitation the rights
18 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19 copies of the Software, and to permit persons to whom the Software is
20 furnished to do so, subject to the following conditions:
21 
22 The above copyright notice and this permission notice shall be included in all
23 copies or substantial portions of the Software.
24 
25 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31 SOFTWARE.
32 */
33 
34 
35 #ifndef COMMANDER_API_SRC_COMMANDER_HPP_
36 #define COMMANDER_API_SRC_COMMANDER_HPP_
37 
38 #define COMMANDER_API_VERSION (const char*)"2.0.0"
39 
40 #include "stdint.h"
41 #include "string.h"
42 
43 #include "Commander-Settings.hpp"
44 #include "Commander-IO.hpp"
45 
47 #ifdef COMMANDER_USE_SERIAL_RESPONSE
48 #include "Serial.hpp"
49 #endif
50 
52 #ifdef ARDUINO
53 #include "Arduino.h"
54 #endif
55 
56 
61 #define apiElement( name, desc, func ) { 0, NULL, NULL, (const char*)name, (const char*)desc, func }
62 
67 #define attachTree( name ) attachTreeFunction( name, sizeof( name ) / sizeof( name[ 0 ] ) )
68 
78 class Commander{
79 
80 public:
81 
83  static const char *version;
84 
90  typedef struct API_t{
91 
92  uint16_t place; // This will store the alphabetical place of the command in the tree
93  struct API_t *left; // Left element on a binary tree branch
94  struct API_t *right; // Right element on a binary tree branch
95  const char *name; // Name of the command
96  const char *desc; // Description of the command
97  void(*func)( char*, commandResponse *response ); // Function pointer to the command function
98 
100 
107  void attachTreeFunction( API_t *API_tree_p, uint32_t API_tree_size_p );
108 
115  void init();
116 
122  API_t* operator [] ( int i );
123 
129  API_t* operator [] ( char* name );
130 
136  API_t* operator [] ( const char* name );
137 
144  void execute( char *cmd );
145 
152  void execute( const char *cmd );
153 
154  #ifdef COMMANDER_USE_SERIAL_RESPONSE
155  void execute( char *cmd, Serial *resp );
163 
171  void execute( const char *cmd, Serial *resp );
172 
178  void attachDebugChannel( Serial *resp );
179  #endif
180 
181  #ifdef COMMANDER_USE_ARDUINO_SERIAL_RESPONSE
182  void execute( char *cmd, HardwareSerial *resp );
190 
198  void execute( const char *cmd, HardwareSerial *resp );
199 
205  void attachDebugChannel( HardwareSerial *resp );
206  #endif
207 
208  #ifdef COMMANDER_USE_WIFI_CLIENT_RESPONSE
209 
217  void execute( char *cmd, WiFiClient *resp );
225  void execute( const char *cmd, WiFiClient *resp );
226 
232  void attachDebugChannel( WiFiClient *resp );
233  #endif
234 
236  void enableDebug();
237 
239  void disableDebug();
240 
241 private:
242 
244  API_t *API_tree = NULL;
245 
247  uint32_t API_tree_size = 0;
248 
250  uint32_t elementCounter;
251 
256  char tempBuff[ COMMANDER_MAX_COMMAND_SIZE ];
257 
259  commandResponse defaultResponse;
260 
261  #ifdef COMMANDER_USE_SERIAL_RESPONSE
262  commandResponseSerial serialResponse;
264  #endif
265 
266  #ifdef COMMANDER_USE_ARDUINO_SERIAL_RESPONSE
267  commandResponseArduinoSerial arduinoSerialResponse;
269  #endif
270 
271  #ifdef COMMANDER_USE_WIFI_CLIENT_RESPONSE
272  commandResponseWiFiClient WiFiClientResponse;
274  #endif
275 
278  commandResponse *response = &defaultResponse;
279 
281  bool debugEnabled = false;
282 
284  commandResponse defaultDebugResponse;
285 
286  #ifdef COMMANDER_USE_SERIAL_RESPONSE
287  commandResponseSerial serialDebugResponse;
289  #endif
290 
291  #ifdef COMMANDER_USE_ARDUINO_SERIAL_RESPONSE
292  commandResponseArduinoSerial arduinoSerialDebugResponse;
294  #endif
295 
296  #ifdef COMMANDER_USE_WIFI_CLIENT_RESPONSE
297  commandResponseWiFiClient WiFiClientDebugResponse;
299  #endif
300 
303  commandResponse *dbgResponse = &defaultDebugResponse;
304 
306  uint16_t find_api_index_by_place( uint16_t place );
307 
309  void swap_api_elements( uint16_t index, uint16_t place );
310 
312  void optimize_api_tree();
313 
320  void recursive_optimizer( int32_t start_index, int32_t stop_index );
321 
327  void executeCommand( char *cmd );
328 
335  void helpFunction( bool description = false );
336 
337 };
338 
339 
340 
341 #endif /* COMMANDER_API_SRC_COMMANDER_HPP_ */
Commander
Commander class.
Definition: Commander-API.hpp:78
Commander::version
static const char * version
Library version string.
Definition: Commander-API.hpp:83
Commander::API_t::name
const char * name
Definition: Commander-API.hpp:95
COMMANDER_MAX_COMMAND_SIZE
#define COMMANDER_MAX_COMMAND_SIZE
Maximum length of the terminal command.
Definition: Commander-Settings.hpp:52
Commander::API_t
Structure for command data.
Definition: Commander-API.hpp:90
Commander::API_t
struct Commander::API_t API_t
Structure for command data.
Commander::API_t::desc
const char * desc
Definition: Commander-API.hpp:96
Commander::operator[]
API_t * operator[](int i)
Array index operator overload for int type.
Definition: Commander-API.cpp:489
Commander::attachTreeFunction
void attachTreeFunction(API_t *API_tree_p, uint32_t API_tree_size_p)
Attach API-tree to the object.
Definition: Commander-API.cpp:39
Commander::disableDebug
void disableDebug()
Disables debug messages.
Definition: Commander-API.cpp:483
Commander::execute
void execute(char *cmd)
Default execution function.
Definition: Commander-API.cpp:331
Commander::API_t::right
struct API_t * right
Definition: Commander-API.hpp:94
Commander::enableDebug
void enableDebug()
Enables debug messages.
Definition: Commander-API.cpp:477
Commander::init
void init()
Initializer.
Definition: Commander-API.cpp:49
commandResponse
Default response class.
Definition: Commander-IO.hpp:64
Commander::API_t::left
struct API_t * left
Definition: Commander-API.hpp:93
Commander-Settings.hpp
Commander-IO.hpp
Commander::API_t::func
void(* func)(char *, commandResponse *response)
Definition: Commander-API.hpp:97
Commander::API_t::place
uint16_t place
Definition: Commander-API.hpp:92