OS  v1.7.5
Documentation
Loading...
Searching...
No Matches
cli.hpp
1#ifndef QOS_CPP_CLI
2#define QOS_CPP_CLI
3
4#include "include/types.hpp"
5#include "include/list.hpp"
6#include "include/util.hpp"
7
8namespace qOS {
9
10 class commandLineInterface;
11
20 namespace cli {
21
30 enum response : int16_t {
31 ERROR = -32767,
32 NOT_ALLOWED = -32766,
34 OK = 1,
35 DEVID = 32765,
36 NOT_FOUND = 32766,
38 };
39
49 constexpr response ERROR_CODE( int16_t code )
50 {
51 return static_cast<response>( -code );
52 }
53
57 enum commandType : uint16_t {
58 UNDEF = 0x0000,
59 PARA = 0x0100,
60 TEST = 0x0200,
61 READ = 0x0400,
62 ACT = 0x0800,
63 };
64
66 class input {
67 public:
68 virtual ~input() {}
69 protected:
70 char *storage{ nullptr };
71 volatile index_t index{ 0U };
72 index_t maxIndex{ 0U };
73 size_t size{ 0U };
74 volatile bool ready{ false };
75 void flush( void );
76 void operator=( input const& ) = delete;
77 input() {}
78 };
82 #ifdef DOXYGEN
93 class handler_t final {
94 private:
95 commandLineInterface *instance{ nullptr };
96 void *Command{ nullptr };
97 char *StrData{ nullptr };
98 void *Data{ nullptr };
99 size_t StrLen{ 0U };
100 size_t NumArgs{ 0U };
101 commandType Type{ UNDEF };
102 _Handler( _Handler const& ) = delete;
103 void operator=( _Handler const& ) = delete;
104 _Handler() = default;
105 public:
110 inline commandType getType( void ) const
111 {
112 return Type;
113 }
118 inline char* getStringData( void )
119 {
120 return StrData;
121 }
126 inline void* getData( void )
127 {
128 return Data;
129 }
133 inline size_t getStringLength( void ) const
134 {
135 return StrLen;
136 }
142 inline size_t getNumArgs( void ) const
143 {
144 return NumArgs;
145 }
153 char* getArgPtr( index_t n ) const;
162 int getArgInt( index_t n ) const;
180 uint32_t getArgHex( index_t n ) const;
189 char* getArgString( index_t n, char *pOut );
195 void writeOut( const char c ) const;
201 void writeOut( const char *s ) const;
205 char *output{ nullptr };
210 inline command& thisCommand( void ) noexcept;
212 };
213 #endif
214
215 class command;
217 class commandHandler final {
218 private:
219 commandLineInterface *instance{ nullptr };
220 command *Command{ nullptr };
221 char *StrData{ nullptr };
222 void *Data{ nullptr };
223 size_t StrLen{ 0U };
224 size_t NumArgs{ 0U };
225 commandType Type{ UNDEF };
226 commandHandler( commandHandler const& ) = delete;
227 void operator=( commandHandler const& ) = delete;
228 commandHandler() = default;
229 public:
230 inline commandType getType( void ) const
231 {
232 return Type;
233 }
234 inline char* getStringData( void )
235 {
236 return StrData;
237 }
238 inline void* getData( void )
239 {
240 return Data;
241 }
242 inline size_t getStringLength( void ) const
243 {
244 return StrLen;
245 }
246 inline size_t getNumArgs( void ) const
247 {
248 return NumArgs;
249 }
250 inline command& self( void ) noexcept
251 {
252 return *Command;
253 }
254 inline command& thisCommand( void ) noexcept
255 {
256 return *Command;
257 }
258 char* getArgPtr( index_t n ) const;
259 int getArgInt( index_t n ) const;
260 float32_t getArgFloat( index_t n ) const;
261 uint32_t getArgHex( index_t n ) const;
262 char* getArgString( index_t n, char *pOut );
263 void writeOut( const char c ) const;
264 void writeOut( const char *s ) const;
265 char *output{ nullptr };
266 friend class qOS::commandLineInterface;
267 };
268 using handler_t = commandHandler&;
293
297 using options_t = uint16_t;
298
302 class command : protected node {
303 private:
304 commandCallback_t cmdCallback{ nullptr };
305 options_t cmdOpt{ 0U };
306 size_t cmdLen{ 0U };
307 void *param{ nullptr };
308 char *Text{ nullptr };
309 command( command const& ) = delete;
310 void operator=( command const& ) = delete;
311 public:
312 command() = default;
314 virtual ~command() {}
316 inline void* getParam( void ) noexcept
317 {
318 return param;
319 }
321 };
323 }
324
333 class commandLineInterface : protected cli::input {
334 private:
335 list subscribed;
336 cli::commandHandler handler;
337 const char *ok_rsp{ "OK" };
338 const char *er_rsp{ "ERROR" };
339 const char *nf_rsp{ "UNKNOWN" };
340 const char *id_rsp{ "" };
341 const char *eol{ "\r\n" };
342 char delim{ ',' };
343 util::putChar_t outputFcn{ nullptr };
344 size_t sizeOutput{ 0U };
345 void *owner{ nullptr };
346 bool notify( void );
347 bool preProcessing( cli::command *cmd, char *inputBuffer );
348 size_t numOfArgs( const char *str ) const;
349 void handleResponse( cli::response retval );
350 void (*xNotifyFcn)( commandLineInterface *arg) = { nullptr };
352 void operator=( commandLineInterface const& ) = delete;
353 public:
356 virtual ~commandLineInterface() {}
370 bool setup( util::putChar_t outFcn, char *pInput, const size_t sizeIn, char *pOutput, const size_t sizeOut );
371 template <size_t inBufferSize, size_t outBufferSize>
372 bool setup( util::putChar_t outFcn, char (&pInput)[ inBufferSize ], char (&pOutput)[ outBufferSize ] ) // skipcq : CXX-W2066
373 {
374 return setup( outFcn, pInput, inBufferSize, pOutput, outBufferSize );
375 }
408 bool add( cli::command &cmd, char *textCommand, const cli::commandCallback_t &cFcn, cli::options_t cmdOpt, void *param = nullptr );
416 bool isrHandler( const char c );
425 bool isrHandler( char *pData, const size_t n );
431 bool raise( const char *cmd );
436 bool inputFlush( void );
442 cli::response exec( const char *cmd );
448 bool run( void );
453 inline void* getOwner( void )
454 {
455 return owner;
456 }
461 inline void setData( void *pData )
462 {
463 handler.Data = pData;
464 }
465 friend class cli::commandHandler;
466 friend class core;
467 };
469}
470
471
472#endif /*QOS_CPP_CLI*/
An AT-Command object.
Definition cli.hpp:302
void * getParam(void) noexcept
Definition cli.hpp:316
The command argument with all the regarding information of the incoming AT command.
Definition cli.hpp:93
int getArgInt(index_t n) const
Helper method to get the n argument parsed as integer from the incoming AT command.
float32_t getArgFloat(index_t n) const
Helper method to get the n argument parsed as float from the incoming AT command.
char * getArgString(index_t n, char *pOut)
Helper method to get the n argument parsed as string from the incoming AT command.
char * getArgPtr(index_t n) const
Helper method to get the pointer where the desired argument starts.
size_t getStringLength(void) const
The length of the stringStrData.
Definition cli.hpp:133
commandType getType(void) const
Retrieve the he incoming command type.*.
Definition cli.hpp:110
void writeOut(const char c) const
Helper method for printing a character to the CLI output. It displays only one character at a time.
void writeOut(const char *s) const
Writes a string to CLI output without the EOF string appended at the end.
char * getStringData(void)
The string data received after the detected command.
Definition cli.hpp:118
command & thisCommand(void) noexcept
return the instance of command being evaluated
uint32_t getArgHex(index_t n) const
Helper method to get the n HEX argument parsed uint32_t from the incoming AT command.
void * getData(void)
Retrieve a pointer to the user-defined data - Storage Pointer.
Definition cli.hpp:126
char * output
The CLI output buffer. Can be written by the user.
Definition cli.hpp:205
size_t getNumArgs(void) const
Retrieve the number of arguments of the incoming AT command. only available if Type = commandType::PA...
Definition cli.hpp:142
An AT Command Line Interface (CLI) object.
Definition cli.hpp:333
bool raise(const char *cmd)
Sends a command to the Command Line Interface instance.
cli::response exec(const char *cmd)
Try to execute the requested command.
bool run(void)
Run the AT Command Line Interface when the input is ready.
void * getOwner(void)
Retrieves a pointer of the owner of this object.
Definition cli.hpp:453
void setData(void *pData)
Set the cli data or storage-pointer.
Definition cli.hpp:461
bool isrHandler(const char c)
Feed the CLI input with a single character. This call is mandatory from an interrupt context....
bool inputFlush(void)
Flush the CLI input buffer.
bool setup(util::putChar_t outFcn, char(&pInput)[inBufferSize], char(&pOutput)[outBufferSize])
Definition cli.hpp:372
bool add(cli::command &cmd, char *textCommand, const cli::commandCallback_t &cFcn, cli::options_t cmdOpt, void *param=nullptr)
This function subscribes the CLI instance to a specific command with an associated Callback function,...
bool setup(util::putChar_t outFcn, char *pInput, const size_t sizeIn, char *pOutput, const size_t sizeOut)
Setup an instance of the AT Command Line Interface.
bool isrHandler(char *pData, const size_t n)
Feed the CLI input with a string. This call is mandatory from an interrupt context....
The class to interface the OS.
Definition kernel.hpp:83
A list object (Generic double-linked)
Definition list.hpp:119
A list-node object (Used internally)
Definition list.hpp:62
response(*)(handler_t) commandCallback_t
Pointer to function : An AT-Command callback.
Definition cli.hpp:292
uint16_t options_t
A typedef that holds the options for an AT-Command object.
Definition cli.hpp:297
response
an enumeration to define the possible values that can be returned from the callback of a command.
Definition cli.hpp:30
commandType
An enum to describe the available AT command types.
Definition cli.hpp:57
constexpr response ERROR_CODE(int16_t code)
Used to indicate an error code as return value inside a command-callback. This code is defined by the...
Definition cli.hpp:49
@ NOT_ALLOWED
Definition cli.hpp:32
@ DEVID
Definition cli.hpp:35
@ ERROR
Definition cli.hpp:31
@ OUTPUT_RESPONSE
Definition cli.hpp:37
@ NO_RESPONSE
Definition cli.hpp:33
@ OK
Definition cli.hpp:34
@ NOT_FOUND
Definition cli.hpp:36
@ TEST
Definition cli.hpp:60
@ READ
Definition cli.hpp:61
@ PARA
Definition cli.hpp:59
@ UNDEF
Definition cli.hpp:58
@ ACT
Definition cli.hpp:62
void(*)(void *, const char) putChar_t
Pointer to function that write-out a single character.
Definition util.hpp:33
STD_TYPE_SIZE_T index_t
A type to instantiate an OS index variable. Can store the maximum size of a theoretically possible ob...
Definition types.hpp:94
float float32_t
A type to instantiate a single-precision variable of 32-bits IEEE 754.
Definition types.hpp:82
OS/Kernel interfaces.
Definition bytebuffer.hpp:7