Commander-API  V2.1.0
Simple Command Parser
Loading...
Searching...
No Matches
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/*
11MIT License
12
13Copyright (c) 2020 Daniel Hajnal
14
15Permission is hereby granted, free of charge, to any person obtaining a copy
16of this software and associated documentation files (the "Software"), to deal
17in the Software without restriction, including without limitation the rights
18to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19copies of the Software, and to permit persons to whom the Software is
20furnished to do so, subject to the following conditions:
21
22The above copyright notice and this permission notice shall be included in all
23copies or substantial portions of the Software.
24
25THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31SOFTWARE.
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.1.0"
39
40#include "stdint.h"
41#include "string.h"
42
44#include "Commander-IO.hpp"
45
47#ifdef ARDUINO
48#include "Arduino.h"
49#endif
50
51#include "Stream.h"
52
53#ifdef COMMANDER_USE_WIFI_CLIENT_RESPONSE
54 #ifdef ESP8266
55 #include <ESP8266WiFi.h>
56 #endif
57
58 #ifdef ESP32
59 #include <WiFi.h>
60 #endif
61#endif
62
63#ifdef __AVR__
64#include <avr/pgmspace.h>
65#endif
66
70#define apiElement( name, desc, func ) { 0, NULL, NULL, (const char*)name, (const char*)desc, func }
71
72#ifdef __AVR__
73
78#define apiElement_P( element, name, desc, func_arg ) { element.name_P = F( name ); element.desc_P = F( desc ); element.func = func_arg; }
79
80#endif
81
86#define attachTree( name ) attachTreeFunction( name, sizeof( name ) / sizeof( name[ 0 ] ) )
87
98
99public:
100
102 static const char *version;
103
109 typedef struct API_t{
110
111 uint16_t place; // This will store the alphabetical place of the command in the tree
112 struct API_t *left; // Left element on a binary tree branch
113 struct API_t *right; // Right element on a binary tree branch
114 const char *name; // Name of the command
115 const char *desc; // Description of the command
116
117 void(*func)( char*, Stream *response ); // Function pointer to the command function
118
119 #ifdef __AVR__
120 __FlashStringHelper *name_P; // Name of the command( stored in PROGMEM )
121 __FlashStringHelper *desc_P; // Description of the command( stored in PROGMEM )
122 #endif
123
124 }API_t;
125
129 };
130
133
140 void attachTreeFunction( API_t *API_tree_p, uint32_t API_tree_size_p );
141
148 void init();
149
155 API_t* operator [] ( int i );
156
162 API_t* operator [] ( char* name );
163
169 API_t* operator [] ( const char* name );
170
177 void execute( char *cmd );
178
185 void execute( const char *cmd );
186
194 void execute( char *cmd, Stream *resp );
195
203 void execute( const char *cmd, Stream *resp );
204
210 void attachDebugChannel( Stream *resp );
211
213 void enableDebug();
214
216 void disableDebug();
217
220 void printHelp( Stream* out );
221
222private:
223
226
228 uint32_t API_tree_size = 0;
229
232
238
239 #ifdef __AVR__
240
244
253 int commander_strcmp_progmem( API_t* element1, API_t* element2 );
254
261 int commander_strcmp_tree_ram_progmem( API_t* element1, char* element2 );
262
263 #endif
264
272 int commander_strcmp_regular( API_t* element1, API_t* element2 );
273
280 int commander_strcmp_tree_ram_regular( API_t* element1, char* element2 );
281
285
289
292
296
298 bool debugEnabled = false;
299
302
306
308 uint16_t find_api_index_by_place( uint16_t place );
309
311 void swap_api_elements( uint16_t index, uint16_t place );
312
314 void optimize_api_tree();
315
322 void recursive_optimizer( int32_t start_index, int32_t stop_index );
323
329 void executeCommand( char *cmd );
330
338 void helpFunction( bool description = false );
339
347 void helpFunction( bool description, Stream* out, bool style = false );
348
353 int32_t hasChar( char* str, char c );
354
355 #ifdef COMMANDER_ENABLE_PIPE_MODULE
356
359
363
364 #endif
365
366};
367
368
369
370#endif /* COMMANDER_API_SRC_COMMANDER_HPP_ */
#define COMMANDER_MAX_COMMAND_SIZE
Commander class.
API_t * operator[](int i)
Array index operator overload for int type.
void swap_api_elements(uint16_t index, uint16_t place)
Swap two API elements in the tree.
memoryType_t memoryType
Flag for memory type.
void disableDebug()
Disables debug messages.
API_t * API_tree
Starting address of the API-tree.
int commander_strcmp_tree_ram_progmem(API_t *element1, char *element2)
Compare an API-tree element's name with a regular string.
uint32_t API_tree_size
Number of elements in the API-tree.
int(Commander::* commander_strcmp)(API_t *element1, API_t *element2)
Function pointer to an internal strcmp like function.
char tempBuff[COMMANDER_MAX_COMMAND_SIZE]
Internal command buffer.
void executeCommand(char *cmd)
Command execution.
commandResponse defaultResponse
Default response handler class.
uint32_t elementCounter
Internal variable for counting purpose.
commandResponse defaultDebugResponse
Default response handler for debug messages.
int(Commander::* commander_strcmp_tree_ram)(API_t *element1, char *element2)
Function pointer to an internal strcmp like function.
int commander_strcmp_progmem(API_t *element1, API_t *element2)
Compare two API-tree element's name.
Stream * response
Pointer to response class.
char progmemBuffer[COMMANDER_MAX_COMMAND_SIZE]
With the PROGMEM implementation we need to copy the data from the PROGMEM area to a buffer for comper...
static const char * version
Library version string.
void helpFunction(bool description=false)
Help function.
void printHelp(Stream *out)
Prints out the help string to the specified Stream.
void init()
Initializer.
int32_t hasChar(char *str, char c)
Search for a character in a string.
void recursive_optimizer(int32_t start_index, int32_t stop_index)
Recursive function optimize a section in the tree.
Stream * dbgResponse
Pointer to response class.
int commander_strcmp_tree_ram_regular(API_t *element1, char *element2)
Compare an API-tree element's name with a regular string.
char pipeArgBuffer[COMMANDER_MAX_COMMAND_SIZE]
If piping happenes the output of the first command will be copied to this buffer.
bool debugEnabled
Flag to enable or disable debug messages.
void attachDebugChannel(Stream *resp)
Debug channel for Serial.
void execute(char *cmd)
Default execution function.
int commander_strcmp_regular(API_t *element1, API_t *element2)
Compare two API-tree element's name.
void optimize_api_tree()
Optimizes the tree to make it balanced.
void enableDebug()
Enables debug messages.
uint16_t find_api_index_by_place(uint16_t place)
Find an API element in the tree by alphabetical place.
commanderPipeChannel pipeChannel
Channel for the internal piping.
void attachTreeFunction(API_t *API_tree_p, uint32_t API_tree_size_p)
Attach API-tree to the object.
@ MEMORY_PROGMEM
Progmem memory implementation.
@ MEMORY_REGULAR
Regular memory implementation.
Default response class.
Structure for command data.
struct API_t * left
const char * name
__FlashStringHelper * name_P
const char * desc
__FlashStringHelper * desc_P
void(* func)(char *, Stream *response)
struct API_t * right