Skip to content

Commit 7adb120

Browse files
committed
Caller interface prototype.
From now on the caller object is used instead of response pointer. It is more failsafe, because the caller object acts like a Stream object.
1 parent e78f881 commit 7adb120

File tree

10 files changed

+344
-130
lines changed

10 files changed

+344
-130
lines changed

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@
1313
"ios": "cpp",
1414
"xmemory": "cpp",
1515
"system_error": "cpp"
16-
}
16+
},
17+
"C_Cpp.errorSquiggles": "disabled"
1718
}

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ set( SOURCES
4949
#src/Commander-Database.cpp
5050
src/Commander-Database.hpp
5151
src/Commander-DefaultSettings.hpp
52+
src/Commander-Caller-Interface.hpp
53+
src/Commander-Caller-Interface.cpp
5254

5355
extras/simulator/Arduino.h
5456
extras/simulator/Print.cpp

extras/examples_desktop/Desktop/Memory/Memory.cpp

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,21 @@ stdioStream stdioChannel;
3939
// We have to create an object from Commander class.
4040
Commander commander;
4141

42+
bool cat_func( char *args, CommandCaller* caller );
43+
bool dog_func( char *args, CommandCaller* caller );
44+
bool sum_func( char *args, CommandCaller* caller );
45+
4246
// Add echo and env commands to the API tree.
4347
Commander::systemCommand_t API_tree[] = {
44-
SYSTEM_COMMAND_MEMDUMP
48+
systemCommand( "cat", "Description for cat command.", cat_func ),
49+
systemCommand( "dog", "Description for dog command.", dog_func ),
50+
systemCommand( "sum", "This function sums two number from the argument list.", sum_func )
4551
};
4652

4753
char commandBuffer[ COMMAND_SIZE ];
4854

55+
char pipeBuffer[ COMMANDER_PIPE_BUFFER_SIZE ];
56+
commanderPipeChannel pipeChannel;
4957

5058

5159

@@ -74,10 +82,12 @@ int main(){
7482
// itself for the fastest runtime possible. It creates a balanced
7583
// binary tree from the API_tree to boost the search speed.
7684
// This part uses some recursion, to make the code space small.
77-
// But recursion is a bit stack hungry, so please initialize
85+
// But recursion is a bit stack hungry, so pleacatse initialize
7886
// Commander at the beginning of your code to prevent stack-overlow.
7987
commander.init();
8088

89+
commander.enablePipeModule( pipeBuffer, &pipeChannel );
90+
8191
stdioChannel.println();
8292
stdioChannel.println( "---- Init Finished ----" );
8393
stdioChannel.println();
@@ -101,3 +111,62 @@ int main(){
101111
return 0;
102112

103113
}
114+
115+
116+
/// This is an example function for the cat command
117+
bool cat_func( char *args, CommandCaller* caller ){
118+
119+
caller -> print("Hello from cat function!\r\n");
120+
return true;
121+
122+
}
123+
124+
/// This is an example function for the dog command
125+
bool dog_func( char *args, CommandCaller* caller ){
126+
127+
caller -> print("Hello from dog function!\r\n");
128+
return true;
129+
130+
}
131+
132+
/// This is an example function for the sum command
133+
bool sum_func( char *args, CommandCaller* caller ){
134+
135+
// These variables will hold the value of the
136+
// two numbers, that has to be summed.
137+
int a = 0;
138+
int b = 0;
139+
140+
// This variable will hold the result of the
141+
// argument parser.
142+
int argResult;
143+
144+
// This variable will hold the sum result.
145+
int sum = 0;
146+
147+
argResult = sscanf( args, "%d %d", &a, &b );
148+
149+
// We have to check that we parsed successfully the two
150+
// numbers from the argument string.
151+
if( argResult != 2 ){
152+
153+
// If we could not parse two numbers, we have an argument problem.
154+
// We print out the problem to the response channel.
155+
caller -> print( "Argument error! Two numbers required, separated with a blank space.\r\n" );
156+
157+
// Sadly we have to stop the command execution and return.
158+
return false;
159+
160+
}
161+
162+
// Calculate the sum.
163+
sum = a + b;
164+
165+
// Print out the result.
166+
caller -> print( a );
167+
caller -> print( " + " );
168+
caller -> print( b );
169+
caller -> print( " = " );
170+
caller -> println( sum );
171+
return true;
172+
}

src/Commander-API-Commands.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ SOFTWARE.
5454

5555
#include "Commander-API.hpp"
5656
#include "Commander-Arguments.hpp"
57+
#include "Commander-Caller-Interface.hpp"
5758

5859
void printCommandNotImplemented( Stream* channel_p );
5960

@@ -462,7 +463,7 @@ bool commander_export_func( char *args, Stream *response, void* parent );
462463
/// Premade function for memDump command.
463464
/// @param args Pointer to the argument string.
464465
/// @param response Response channel for messages.
465-
bool commander_memDump_func( char *args, Stream *response, void* parent );
466+
bool commander_memDump_func( char *args, CommandCaller* caller );
466467

467468
//-------- i2cScan function --------//
468469
#define SYSTEM_COMMAND_I2CSCAN_NAME "i2cScan"

src/Commander-API.cpp

Lines changed: 57 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ bool Commander::enablePipeModuleFunc( char* buffer, int bufferSize, commanderPip
8282
return true;
8383
}
8484

85-
bool Commander::executeCommand( const char *cmd, void* parent ){
85+
bool Commander::executeCommand( const char *cmd ){
8686

8787
// The beginning of the argument list will be stored in this pointer
8888
char *arg;
@@ -120,13 +120,13 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
120120
// Check if piping is requested, but disabled.
121121
if( pipeArgBuffer == NULL ){
122122

123-
if( response != NULL ){
124-
response -> print( __CONST_TXT__( "The command contains piping but the pipe module is disabled!" ) );
125-
}
123+
//if( caller -> channel != NULL ){
124+
caller -> print( __CONST_TXT__( "The command contains piping but the pipe module is disabled!" ) );
125+
//}
126126
return false;
127127
}
128128

129-
// Terminate where pip is found.
129+
// Terminate where pipe is found.
130130
tempBuff[ pipePos ] = '\0';
131131

132132
}
@@ -181,31 +181,31 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
181181

182182
if( memoryType == MEMORY_REGULAR ){
183183

184-
if( response != NULL ){
184+
//if( caller -> channel != NULL ){
185185

186186
// Print the description text to the output channel.
187-
response -> print( commandData_ptr -> name );
188-
response -> print( ':' );
189-
response -> print( ' ' );
190-
response -> println( commandData_ptr -> data.desc );
187+
caller -> print( commandData_ptr -> name );
188+
caller -> print( ':' );
189+
caller -> print( ' ' );
190+
caller -> println( commandData_ptr -> data.desc );
191191

192-
}
192+
//}
193193

194194
}
195195

196196
#ifdef __AVR__
197197

198198
else if( memoryType == MEMORY_PROGMEM ){
199199

200-
if( response != NULL ){
200+
//if( response != NULL ){
201201

202202
// Print the description text to the output channel.
203-
response -> print( commandData_ptr -> name_P );
204-
response -> print( ':' );
205-
response -> print( ' ' );
206-
response -> println( commandData_ptr -> data.desc_P );
203+
caller -> print( commandData_ptr -> name_P );
204+
caller -> print( ':' );
205+
caller -> print( ' ' );
206+
caller -> println( commandData_ptr -> data.desc_P );
207207

208-
}
208+
//}
209209

210210
}
211211

@@ -257,7 +257,10 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
257257
if( ( pipeArgBuffer != NULL ) && ( pipePos > 0 ) ){
258258

259259
// Execute commands function and redirect the output to the pipe channel.
260-
executionStat = ( commandData_ptr -> data.func )( arg, pipeChannel, parent );
260+
Stream* tmp = caller -> getChannel();
261+
caller -> setChannel( pipeChannel );
262+
executionStat = ( commandData_ptr -> data.func )( arg, caller );
263+
caller -> setChannel( tmp );
261264

262265
// If the actual command execution was successful, we have to increment the pipeCounter
263266
if( executionStat ){
@@ -271,7 +274,7 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
271274
else{
272275

273276
// Execute command function.
274-
executionStat = ( commandData_ptr -> data.func )( arg, response, parent );
277+
executionStat = ( commandData_ptr -> data.func )( arg, caller );
275278

276279
}
277280

@@ -369,21 +372,21 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
369372
// We have to check for single or described help function.
370373
if( strcmp( arg, (const char*)"-d" ) == 0 ){
371374

372-
printHelp( response, true, formatting );
375+
printHelp( caller, true, formatting );
373376

374377
}
375378

376379
else{
377380

378-
printHelp( response, false, formatting );
381+
printHelp( caller, false, formatting );
379382

380383
}
381384

382385
}
383386

384387
else{
385388

386-
if( response != NULL ){
389+
//if( caller -> channel != NULL ){
387390
if( pipePos > 0 ){
388391
tempBuff[ pipePos ] = '|';
389392
printBrokenPipe();
@@ -395,45 +398,34 @@ bool Commander::executeCommand( const char *cmd, void* parent ){
395398

396399
// If we went through the whole tree and we did not found the command in it,
397400
// we have to notice the user abut the problem. Maybe a Type-O
398-
response -> print( __CONST_TXT__( "Command \'" ) );
399-
response -> print( tempBuff );
400-
response -> print( __CONST_TXT__( "\' not found!" ) );
401+
caller -> print( __CONST_TXT__( "Command \'" ) );
402+
caller -> print( tempBuff );
403+
caller -> print( __CONST_TXT__( "\' not found!" ) );
401404

402-
}
405+
//}
403406
return false;
404407

405408
}
406409
return true;
407410
}
408411

409-
bool Commander::execute( const char *cmd ){
410-
411-
// Default execute handler, so the default response will be chosen.
412-
response = NULL;
413-
414-
// Reset the pipe position tracker.
415-
pipeCounter = 0;
412+
bool Commander::execute( const char *cmd, Stream* channel_p, CommandCaller* caller_p ){
416413

417-
// Save the address of the original message.
418-
originalCommandData = cmd;
414+
if( channel_p == NULL ){
415+
caller = &defaultCommandCaller;
416+
caller -> clearChannel();
417+
}
419418

420-
// If piping is enabled, we have to flush the pipe channel before command execution.
421-
if( pipeArgBuffer != NULL ){
422-
while( pipeChannel -> available() ){
423-
pipeChannel -> read();
419+
else{
420+
if( caller_p == NULL ){
421+
caller = &defaultCommandCaller;
422+
caller -> setChannel( channel_p );
423+
}
424+
else{
425+
caller = caller_p;
424426
}
425427
}
426428

427-
// Execute the command.
428-
return( executeCommand( cmd ) );
429-
430-
}
431-
432-
bool Commander::execute( const char *cmd, Stream *resp, void* parent ){
433-
434-
// Redirect the response to the specified Stream.
435-
response = resp;
436-
437429
// Reset the pipe position tracker.
438430
pipeCounter = 0;
439431

@@ -448,7 +440,7 @@ bool Commander::execute( const char *cmd, Stream *resp, void* parent ){
448440
}
449441

450442
// Execute the command.
451-
return( executeCommand( cmd, parent ) );
443+
return( executeCommand( cmd ) );
452444

453445
}
454446

@@ -891,51 +883,51 @@ void Commander::printBrokenPipe(){
891883

892884
if( brokenPipePos >= 0 ){
893885

894-
if( response != NULL ){
886+
//if( caller -> channel != NULL ){
895887

896888
// In case of broken pipe, inform the user about the problematic section.
897889
if( formatting ){
898-
response -> print( __CONST_TXT__( "\033[1;35m" ) );
890+
caller -> print( __CONST_TXT__( "\033[1;35m" ) );
899891
}
900892

901-
response -> println( __CONST_TXT__( "\r\nBroken pipe!" ) );
893+
caller -> println( __CONST_TXT__( "\r\nBroken pipe!" ) );
902894

903895
if( formatting ){
904-
response -> print( __CONST_TXT__( "\033[0;37m" ) );
896+
caller -> print( __CONST_TXT__( "\033[0;37m" ) );
905897
}
906898

907-
response -> println( originalCommandData );
899+
caller -> println( originalCommandData );
908900

909901
for( i = 0; i < brokenPipePos; i++ ){
910-
response -> print( ' ' );
902+
caller -> print( ' ' );
911903
}
912904

913905
if( formatting ){
914-
response -> print( __CONST_TXT__( "\033[1;31m" ) );
906+
caller -> print( __CONST_TXT__( "\033[1;31m" ) );
915907
}
916908

917-
response -> println( "\u25B2" );
909+
caller -> println( "\u25B2" );
918910

919911
for( i = 0; i < brokenPipePos; i++ ){
920-
response -> print( ' ' );
912+
caller -> print( ' ' );
921913
}
922914

923-
response -> println( "\u2514 The pipe broke here" );
915+
caller -> println( "\u2514 The pipe broke here" );
924916

925917
if( formatting ){
926-
response -> print( __CONST_TXT__( "\033[0;37m" ) );
918+
caller -> print( __CONST_TXT__( "\033[0;37m" ) );
927919
}
928920

929921
if( pipeChannel -> available() ){
930-
response -> print( __CONST_TXT__( "Pipe data: " ) );
922+
caller -> print( __CONST_TXT__( "Pipe data: " ) );
931923
while( pipeChannel -> available() ){
932-
response -> print( (char)pipeChannel -> read() );
924+
caller -> print( (char)pipeChannel -> read() );
933925
}
934-
response -> println();
926+
caller -> println();
935927

936928
}
937929

938-
}
930+
//}
939931

940932
}
941933

0 commit comments

Comments
 (0)