#define tprintf(...) telnetClient.printf(__VA_ARGS__); help...

mikemoy
Posts: 599
Joined: Fri Jan 12, 2018 9:10 pm

#define tprintf(...) telnetClient.printf(__VA_ARGS__); help...

Postby mikemoy » Tue Apr 10, 2018 5:49 am

I am to far outside my realm to even think of a proper Subject title for my question, so sorry for that.
I am slowly moving from Arduino to ESP-IDF.
In Arduino land I was using this and like it allot for my telnet server.
tprintf("Hello World\n");

To be able tyo use that, I was using this from an example I found.
#define tprintf(...) telnetClient.printf(__VA_ARGS__);

Which of course came from this:
WiFiClient telnetClient;

In ESP-IDF land, I am of course using send(clientSock, msg, len, 0); to sent my data. Can someone shed some light on how I might acheve the same thing in ESP-IDF ?

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: #define tprintf(...) telnetClient.printf(__VA_ARGS__); help...

Postby kolban » Tue Apr 10, 2018 3:18 pm

Howdy,
I'm afraid it isn't clear in your post what you mean by "the same thing". Are you looking to port the telnet client package? Are you looking to map a #define to a function call?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

mikemoy
Posts: 599
Joined: Fri Jan 12, 2018 9:10 pm

Re: #define tprintf(...) telnetClient.printf(__VA_ARGS__); help...

Postby mikemoy » Tue Apr 10, 2018 6:52 pm

Sorry I was not clear. yes, I am trying to map a #define to a function call
When using Arduino. I add this at the top of main.

#define tprintf(...) telnetClient.printf(__VA_ARGS__);
WiFiClient telnetClient;


I have of 4 other threads running. What was nice about this is I was able to call tprintf(...) from any thread which would send that out the Telnet socket connection. Whats not clear to me is what to replace the "telnetClient.printf" with.

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: #define tprintf(...) telnetClient.printf(__VA_ARGS__); help...

Postby kolban » Tue Apr 10, 2018 8:20 pm

Unfortunately its not going to be a great answer ... the story can go in two direction.

The first, #defines. When you code a #define you are asking the environment to perform a smart text replacement before presenting the code to the compilation.

When you code:

#define X(Y) someFunction(1, Y, 2)

and then code:

X(17)

what gets given to the compiler is

someFunction(1, 17, 2)

what that means is that it is as though you had explicitly coded someFunction() instead of X(). This means that macros in this story are merely being used to "cleanup" and "polish" the source.

The second story is more of semantics. If today you were to code:

telnetClient.printf("%d %d", 1, 2);

what would you code instead to achieve your goal. This second story has nothing to do with macros or #defines.

If I were to bet/guess, I'm thinking the story of macros/#defines is a red herring and the real meat of the question is how to replace one library call with some logical equivalent.
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

mikemoy
Posts: 599
Joined: Fri Jan 12, 2018 9:10 pm

Re: #define tprintf(...) telnetClient.printf(__VA_ARGS__); help...

Postby mikemoy » Tue Apr 10, 2018 8:31 pm

ok, I think I may know from your post how to better explain what I am after.

Code: Select all

Currently if I want to send something out via a TCP socket i would do something like this. 
 char the_buf[64];
int len;
...
...
...
sprintf(the_buf,"The Value is: %u\n", value);
len = strlen(the_buf);
bytes_sent = send(clientSock, the_buf, len, 0);

What I am looking to do is streamline that to simply using the #define I shown before which would allow me to simply do this:
tprintf("The Value is: %u\n", value);

I want all the features of printf, and have it send it via the "send" for the socket connection.
From what I gather, they are somehow tying printf and the socket connection together all in one by "telnetClient.printf(__VA_ARGS__); "

mikemoy
Posts: 599
Joined: Fri Jan 12, 2018 9:10 pm

Re: #define tprintf(...) telnetClient.printf(__VA_ARGS__); help...

Postby mikemoy » Tue Apr 10, 2018 10:35 pm

I got it working, and have 2 ways.

#define tprintf(con, ...) ({int ret; ret = send(con,__VA_ARGS__, strlen(__VA_ARGS__),0); ret;})

Who is online

Users browsing this forum: Majestic-12 [Bot], StuartsProjects and 109 guests