Added printf_P method (#31)

* Prevent buffer overflow on received data

* pass to 7 char to avoid save to flash by SDK

* return _contentLength, avoid array reparse to know len

* Added FlashStringHelper for text and binary

* Added FlashStringHelper also to AsyncWebSocketClient

* Added PROGMEM doc

* Corrected binary was sending PSTR as text, addded len

* Server calls client method and code as asked @me-no-dev

* server calls client method and code as asked by @me-no-dev

* Changed Code presentation

* Added printf_P methods
This commit is contained in:
Charles
2016-05-15 02:00:33 +02:00
committed by Me No Dev
parent 937d442fce
commit 29b3f72e80
3 changed files with 83 additions and 1 deletions

View File

@@ -489,6 +489,36 @@ size_t AsyncWebSocketClient::printf(const char *format, ...) {
return len;
}
size_t AsyncWebSocketClient::printf_P(PGM_P formatP, ...) {
char* format;
va_list arg;
va_start(arg, formatP);
#ifdef ESP8266
//ToDo: figure out a way around this
size_t len = 1440;
#else
size_t len = vsnprintf(NULL, 0, format, arg)+1;
#endif
size_t fmtLen = strlen_P(formatP);
format = (char*)calloc(fmtLen+1, sizeof(char));
if ( format ) {
strcpy_P(format, formatP);
char * msg = (char*)malloc(len+1);
if(msg == NULL){
va_end(arg);
return 0;
}
len = vsnprintf(msg, len, format, arg);
msg[len] = 0;
text(msg);
va_end(arg);
free(msg);
free(format);
}
return len;
}
void AsyncWebSocketClient::text(const char * message, size_t len){
_queueMessage(new AsyncWebSocketBasicMessage(message, len));
}
@@ -740,6 +770,49 @@ size_t AsyncWebSocket::printfAll(const char *format, ...) {
return len;
}
size_t AsyncWebSocket::printf_P(uint32_t id, PGM_P formatP, ...){
AsyncWebSocketClient * c = client(id);
if(c != NULL){
va_list arg;
va_start(arg, formatP);
size_t len = c->printf_P(formatP, arg);
va_end(arg);
return len;
}
return 0;
}
size_t AsyncWebSocket::printfAll_P(PGM_P formatP, ...) {
char* format;
va_list arg;
va_start(arg, formatP);
#ifdef ESP8266
//ToDo: figure out a way around this
size_t len = 1440;
#else
size_t len = vsnprintf(NULL, 0, format, arg)+1;
#endif
size_t fmtLen = strlen_P(formatP);
format = (char*)calloc(fmtLen+1, sizeof(char));
if ( format ) {
strcpy_P(format, formatP);
char * msg = (char*)malloc(len+1);
if(msg == NULL){
va_end(arg);
free(format);
return 0;
}
len = vsnprintf(msg, len, format, arg);
msg[len] = 0;
textAll(msg);
va_end(arg);
free(msg);
free(format);
}
return len;
}
void AsyncWebSocket::text(uint32_t id, const char * message){
text(id, message, strlen(message));
}