Using argtable in member functions

toma45
Posts: 1
Joined: Wed Dec 11, 2019 1:19 pm

Using argtable in member functions

Postby toma45 » Wed Dec 11, 2019 1:58 pm

Hi,

I'd like to use esp_console + argtable3 in a C++ project.
I'm trying to use argtable in my members functions.
To do so, I'm creating callback functions to my members functions with a global pointer.
I'm sure my class is going to be instanced only once so I assume it's ok to create callback functions.

The problem is that argtable isn't giving me back the parameters entered by the user.
It checks for them successfully (number of args and their type) but the data it gives me back is random.
I've tested my code outside of members functions and it works well. But I want to use it inside members functions to access other parts of my object.
Here is my code :

Code: Select all

// Pointer for my callback functions
MyClass * _callback;

struct arg_int *argInt;
struct arg_end *endPage;

// My callback function (GLOBAL)
int _setInt(int argc, char *argv[])
{
	return _console->setInt(argc, argv);
}

// Tab of struct for argtable lib (GLOBAL)
void *setInt_argtable[] =
{
	argInt = arg_int1(NULL, NULL, "<0-12>", "Integer argument"),
	endInt = arg_end(10)
};

// Function I'm calling back
int MyClass::setInt(int argc, char *argv[])
{
	int nerrors = arg_parse(argc,argv,setInt_argtable);
	if (nerrors > 0)
	{
		arg_print_errors(stdout, endPage, "myprog");
		return 0;
	}
	printf("argc = %d\n", argc); 				// argc gives the correct number of args
	printf("argv[0] = %s\n", argv[0]);			// argv[0] gives the correct command name
	printf("argv[1] = %s\n", argv[1]);			// argv[1] gives the correct value
	printf("argInt->ival[0] = %d\n", argInt->ival[0]);	// argInt->ival[0] gives random value
	return 0;
}

void MyClass::main(void)
{
	// Callback pointer initialisation
	_callback = this;

//
// I'm using the esp_console lib on ESP32. I want to use argtable with it
//

	/* Initialize the console */
	esp_console_config_t console_config
	{
		256,
		8,
		atoi(LOG_COLOR_CYAN),
		0
	};
	ESP_ERROR_CHECK( esp_console_init(&console_config) );

	/* Configure linenoise line completion library */
	/* Enable multiline editing. If not set, long commands will scroll within
	* single line.
	*/
	linenoiseSetMultiLine(1);

	/* Tell linenoise where to get command completions and hints */
	linenoiseSetCompletionCallback(&esp_console_get_completion);
	linenoiseSetHintsCallback((linenoiseHintsCallback*) &esp_console_get_hint);

	/* Set command history size */
	linenoiseHistorySetMaxLen(100);


	esp_console_register_help_command();

	//
	// Feeding my console with argtable parameters
	//

	esp_console_cmd_t consoleCmd;
	consoleCmd.command  = "setInt";
	consoleCmd.func     = &_setInt;
	consoleCmd.help     = "Trying to set a integer argument";
	consoleCmd.argtable = setInt_argtable;
	esp_console_cmd_register(&consoleCmd);

	/* Main loop */
	while(true)
	{
		// Getting command from user
	}
}
The problem seems to be here :
In argatable3.c :
at the end of function static int arg_int_scanfn(struct arg_int *parent, const char *argval)

Code: Select all

if (errorcode == 0)
{
	parent->ival[parent->count++] = val;
	printf("val = %ld\n", val);
	printf("parent->ival[parent->count] = %d\n", parent->ival[parent->count]);
}
The first printf gives me back the correct value but the second one gives me back random value.

Is my approach of using callback member function good ?
Any idea of what is my problem and how I could solve it ?


Thanks in advance for your answers.

Who is online

Users browsing this forum: ok-home and 119 guests