Then why do you need the second buffer?
Couldn’t you just do this?
void sendFixedData(const char * data)
{
setAddrConfiguration();
size_t size { strlen(data) + 1 };
Serial1.write(data, size);
byte addh = incomingADDH;
byte addl = incomingADDL;
String lengthString { size };
Lora.sendFixedMessage(addh, addl, buffer.c_str(), lengthString);
// Restore the default values of the addresses and the receive/transmit channel after the end of the transfer
setDefaultConfiguration();
}
Or better yet, let the caller specify the number of bytes to be copied:
void sendFixedData(const char * data, size_t size)
{
setAddrConfiguration();
Serial1.write(data, size);
byte addh = incomingADDH;
byte addl = incomingADDL;
String lengthString { size };
Lora.sendFixedMessage(addh, addl, buffer.c_str(), lengthString);
// Restore the default values of the addresses and the receive/transmit channel after the end of the transfer
setDefaultConfiguration();
}
Then you can do sendFixedData(fullMessage, strlen(fullMessage) + 1);
, or something more efficient…
That’s going to be inefficient because strcat
seeks through the entire message to find the null terminator at the end. (See Shlemiel the painter’s algorithm.)
You’d be better off doing:
// Make sure the buffer is actually null terminated before passing it to strcat
char fullMessage[42] = "";
// Use a pointer to make the concatenation more efficient
char * buffer = &fullMessage[0];
buffer = strcat(buffer, nickname);
buffer = strcat(buffer, ": ");
buffer = strcat(buffer, message);
buffer = strcat(buffer, "(addrChan)");
// No need to use strlen to get the size
size_t size { (buffer - &fullMessage[0]) + 1 };
// Formatting and size calculation complete, so send the data...
sendFixedData(fullMessage, size);
Which would cause the buffer
pointer to always point to the null terminator ('\0'
), thus skipping the seeking process of strcat
.
It could be made better still by ensuring that the buffer won’t overflow. Unfortunately the C standard library’s string handling functions are pretty poor at that sort of thing, even with strncpy
and strncat
.
Something like this would be safer:
char * safe_strcat(char * destination_start, char * destination_end, const char * source)
{
char * destination { destination_start };
while(destination != destination_end)
{
char character = *source;
++source;
*destination = character;
++destination;
if(character == '\0')
break;
}
return destination;
}
Then you could do:
// Make sure the buffer is actually null terminated before passing it to strcat
char fullMessage[42] = "";
// Create the end marker
char * end = &fullMessage[42];
// Use a pointer to make the concatenation more efficient
char * buffer = &fullMessage[0];
buffer = safe_strcat(buffer, end, nickname);
buffer = safe_strcat(buffer, end, ": ");
buffer = safe_strcat(buffer, end, message);
buffer = safe_strcat(buffer, end, "(addrChan)");
// No need to use strlen to get the size
size_t size { (buffer - &fullMessage[0]) + 1 };
// Formatting and size calculation complete, so send the data...
sendFixedData(fullMessage, size);