Thanks a lot for your answer.
It is now a bit clearer for me.
The library function is at follows:
/**************************************************************************/
/*!
@brief Queue a binary message for transmission
@param data
The binary message
@param len
The length of the binary message in bytes
@param msg_id
A pointer to a uint64_t which will hold the assigned message ID
@return SWARM_M138_ERROR_SUCCESS if successful
SWARM_M138_ERROR_MEM_ALLOC if the memory allocation fails
SWARM_M138_ERROR_ERR if a command ERR is received - error is returned in commandError
SWARM_M138_ERROR_ERROR if unsuccessful
*/
/**************************************************************************/
Swarm_M138_Error_e SWARM_M138::transmitBinary(const uint8_t *data, size_t len, uint64_t *msg_id)
{
return (transmitBinary(data, len, msg_id, false, 0, false, 0, false, 0));
}
and
// Queue a binary message for transmission
// Return the allocated message ID in msg_id
Swarm_M138_Error_e SWARM_M138::transmitBinary(const uint8_t *data, size_t len, uint64_t *msg_id, bool useAppID, uint16_t appID,
bool useHold, uint32_t hold, bool useEpoch, uint32_t epoch)
{
char *command;
char *response;
char *scratchpad;
Swarm_M138_Error_e err;
// Calculate the possible message length
size_t msgLen = strlen(SWARM_M138_COMMAND_TX_DATA); // $TD
msgLen += 1; // Space
if (useAppID) msgLen += 3 + 5 + 1; // AI=65535,
if (useHold) msgLen += 3 + 8 + 1; // HD=34819200,
if (useEpoch) msgLen += 3 + 10 + 1; // ET=2147483647,
msgLen += 2 * len; // The message length in ASCII Hex
msgLen += 5; // asterix, checksum chars, line feed, null
// Allocate memory for the command, message, asterix, checksum bytes, \n and \0
command = swarm_m138_alloc_char(msgLen);
if (command == NULL)
return (SWARM_M138_ERROR_MEM_ALLOC);
memset(command, 0, msgLen); // Clear it
// Allocate memory for the scratchpad
scratchpad = swarm_m138_alloc_char(16);
if (scratchpad == NULL)
{
swarm_m138_free_char(command);
return (SWARM_M138_ERROR_MEM_ALLOC);
}
memset(scratchpad, 0, 16); // Clear it
sprintf(command, "%s ", SWARM_M138_COMMAND_TX_DATA); // Copy the command. Append the space
if (useAppID)
{
strcat(command, "AI=");
sprintf(scratchpad, "%d", appID);
strcat(command, scratchpad);
strcat(command, ",");
}
if (useHold)
{
strcat(command, "HD=");
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
sprintf(scratchpad, "%d", hold);
#else
sprintf(scratchpad, "%ld", hold);
#endif
strcat(command, scratchpad);
strcat(command, ",");
}
if (useEpoch)
{
strcat(command, "ET=");
#if defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_ESP8266)
sprintf(scratchpad, "%d", epoch);
#else
sprintf(scratchpad, "%ld", epoch);
#endif
strcat(command, scratchpad);
strcat(command, ",");
}
for (size_t i = 0; i < len; i++)
{
char c1 = (data[i] >> 4) + '0'; // Convert the MS nibble to ASCII
if (c1 >= ':') c1 = c1 + 'A' - ':';
char c2 = (data[i] & 0x0F) + '0'; // Convert the LS nibble to ASCII
if (c2 >= ':') c2 = c2 + 'A' - ':';
sprintf(scratchpad, "%c%c", c1, c2);
strcat(command, scratchpad); // Append each data byte as an ASCII Hex char pair
}
strcat(command, "*"); // Append the asterix
addChecksumLF(command); // Add the checksum bytes and line feed
response = swarm_m138_alloc_char(_RxBuffSize); // Allocate memory for the response
if (response == NULL)
{
swarm_m138_free_char(command);
swarm_m138_free_char(scratchpad);
return(SWARM_M138_ERROR_MEM_ALLOC);
}
memset(response, 0, _RxBuffSize); // Clear it
err = sendCommandWithResponse(command, "$TD OK,", "$TD ERR", response, _RxBuffSize, SWARM_M138_MESSAGE_TRANSMIT_TIMEOUT);
if (err == SWARM_M138_ERROR_SUCCESS) // Check if we got $TD OK
{
char *idStart = strstr(response, "$TD OK,");
if (idStart != NULL)
{
char *idEnd = strchr(idStart, '*'); // Look for the asterix
if (idEnd != NULL)
{
uint64_t theID = 0;
idStart += 7; // Point at the first digit of the ID
while (idStart < idEnd)
{
theID *= 10;
theID += (uint64_t)((*idStart) - '0'); // Add each digit to theID
idStart++;
}
*msg_id = theID;
}
}
}
swarm_m138_free_char(command);
swarm_m138_free_char(scratchpad);
swarm_m138_free_char(response);
return (err);
}
It is quite long, sorry for that but I was afraid of missing something important...