Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added printf method. Fixed unsecured usage of print and println. #35

Merged
merged 3 commits into from
Feb 17, 2023

Conversation

baltazorr
Copy link

No description provided.

@baltazorr baltazorr mentioned this pull request Dec 28, 2022
@LennartHennigs
Copy link
Owner

LennartHennigs commented Dec 30, 2022

Hey @baltazorr,
thank you for the pull request and your efforts!
It all looks fine but I have a general question. Both, the ESP8266 and ESP32 client.printf() function acts weird:

  byte decimal_number = 65;
  Serial.printf("\n%d decimal ", decimal_number);
  telnet.println(decimal_number);
  telnet.println(decimal_number, 10);
  telnet.printf("%d is %x", decimal_number, decimal_number);
  telnet.printf("\n%d decimal ", decimal_number);
  telnet.printf("\n%i decimal ", decimal_number);
  telnet.printf("\n%c char", decimal_number);
  telnet.printf("\n%X hex", decimal_number);

The above code returns this:

A
65
1073741552 is 3ffffee0
1073741552 decimal
1073741552 decimal
� char
3FFFFEF0 char

Any idea why it turns 65 into 1073741552?

@baltazorr
Copy link
Author

Hi @LennartHennigs, you are right. It was wrong implementation. It is not possible to forward an invocation of a variadic function in c.
So I made other solution. I have copied whole implementation of printf method from Print class and now it works. I have tested it.
Hope it is acceptable solution.

@DadelNet
Copy link

Hi, I am using PlatformIO (version lennarthennigs/ESP Telnet @ ^2.0.0).
I would most appreciate it if you could explain what I need to do to implement this printf fix.

@baltazorr
Copy link
Author

@DadelNet, you need to wait until @LennartHennigs merge this PR and release new version of ESPTelnet.

As a workaround you can use dependency on my fork:
lib_deps = https://github.com/baltazorr/ESPTelnet instead of lib_deps = lennarthennigs/ESP Telnet@^2.0.0

@LennartHennigs
Copy link
Owner

Hey, will take a look at it in the next couple of days.
Cheers
l.

@DadelNet
Copy link

@baltazorr
Thank you so much for your fork and the tip to make it work from PlatformIO. It works great. You made my day !

@DadelNet
Copy link

@baltazorr
FYI, I call the following routine from setup() and a macro to make it easy to switch printing between the different destinations (Serial, Telnet, or no print) and I use SPRT, SPLN, SPF1 - SPF7 throughout my code instead of the .print, .println, .printf . There may be a better way, but it works perfect for me. Thanks again.

// #defined SERIAL_PRINT
#defined TELNET_PRINT

void startSerial() { // triggered from setup()
#if defined (SERIAL_PRINT)
Serial.begin(115200); // Start the Serial communication to send messages to the computer
#elif defined (TELNET_PRINT)
Serial.begin(115200); // Start the Serial communication to send messages to the computer
startTelnet();
#endif

#if defined (SERIAL_PRINT)
#define SPRT(x) Serial.print(x)
#define SPRF(x, y) Serial.print(x, y)
#define SPLN(x) Serial.println(x)
#define SPF1(x, y) Serial.printf(x, y)
#define SPF2(x, y, z) Serial.printf(x, y, z)
#define SPF3(x, y, z, t) Serial.printf(x, y, z, t)
#define SPF4(x, y, z, t, u) Serial.printf(x, y, z, t, u)
#define SPF5(x, y, z, t, u, v) Serial.printf(x, y, z, t, u, v)
#define SPF6(x, y, z, t, u, v, w) Serial.printf(x, y, z, t, u, v, w)
#define SPF7(x, y, z, s, t, u, v, w) Serial.printf(x, y, z, s, t, u, v, w)
#define SPF8(x, y, z, s, t, u, v, w, r) Serial.printf(x, y, z, s, t, u, v, w, r)
#elif defined (BT_PRINT)
#define SPRT(x) SerialBT.print(x)
#define SPRF(x, y) SerialBT.print(x, y)
#define SPLN(x) SerialBT.println(x)
#define SPF1(x, y) SerialBT.printf(x, y)
#define SPF2(x, y, z) SerialBT.printf(x, y, z)
#define SPF3(x, y, z, t) SerialBT.printf(x, y, z, t)
#define SPF4(x, y, z, t, u) SerialBT.printf(x, y, z, t, u)
#define SPF5(x, y, z, t, u, v) SerialBT.printf(x, y, z, t, u, v)
#define SPF6(x, y, z, t, u, v, w) SerialBT.printf(x, y, z, t, u, v, w)
#define SPF7(x, y, z, s, t, u, v, w) SerialBT.printf(x, y, z, s, t, u, v, w)
#define SPF8(x, y, z, s, t, u, v, w, r) SerialBT.printf(x, y, z, s, t, u, v, w, r)
#elif defined (TELNET_PRINT)
#define SPRT(x) telnet.print(x)
#define SPRF(x, y) telnet.print(x, y)
#define SPLN(x) telnet.println(x)
#define SPF1(x, y) telnet.printf(x, y)
#define SPF2(x, y, z) telnet.printf(x, y, z)
#define SPF3(x, y, z, t) telnet.printf(x, y, z, t)
#define SPF4(x, y, z, t, u) telnet.printf(x, y, z, t, u)
#define SPF5(x, y, z, t, u, v) telnet.printf(x, y, z, t, u, v)
#define SPF6(x, y, z, t, u, v, w) telnet.printf(x, y, z, t, u, v, w)
#define SPF7(x, y, z, s, t, u, v, w) telnet.printf(x, y, z, s, t, u, v, w)
#define SPF8(x, y, z, s, t, u, v, w, r) telnet.printf(x, y, z, s, t, u, v, w, r)
#else
#define SPRT(x)
#define SPRF(x, y)
#define SPLN(x)
#define SPF1(x, y)
#define SPF2(x, y, z)
#define SPF3(x, y, z, t)
#define SPF4(x, y, z, t, u)
#define SPF5(x, y, z, t, u, v)
#define SPF6(x, y, z, t, u, v, w)
#define SPF7(x, y, z, s, t, u, v, w)
#define SPF8(x, y, z, s, t, u, v, w, r)
#endif
}

example of print statement :
SPF5 ("PlugCtrl() : plug #%d (WIP=%d>%d) DO set to %s (%d)", plug, PlugWIP[plug], SS, OffOnAutoStr[updateDOstate], updateDOstate) ;

@baltazorr
Copy link
Author

@DadelNet look at this project: https://github.com/OpenSmartKit/OSK_Debug/blob/main/src/Debug.h
It is macros implementation that correctly works with printf

@LennartHennigs
Copy link
Owner

Hey @baltazorr,
thanks again for your suggested changes!

@LennartHennigs LennartHennigs merged commit ee2f766 into LennartHennigs:master Feb 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants