Open svanscho opened 6 years ago
I have the same error - I need to check the topicName as I'm subscribing to multiple topics. Did you find a solution?
Found a solution - in your callbackhander, add the following:
int topicNameLen = strlen(topicName);
topicName[(topicNameLen - payloadLen)] = 0;
EDIT: Correction - this doesn't work, because the length of the topicName doesn't get truncated, so it remains the size of the first submission.
The original handler https://github.com/ExploreEmbedded/Hornbill-Examples/blob/18980683018d6c00e1761ced703d47615000d60d/arduino-esp32/AWS_IOT/src/AWS_IOT.cpp#L80 has topicNameLen
parameter which is very important. However, the custom handler does not have this one.
You should edit the the library and add this field for custome handler.
One other note, the library supports only one subscription. If you make another subscription, it will override the old one.
@HumamHelfawi : Thx for the hint!
I changed AWS_IOT.cpp as follows, so that the correct topicname is passed to the custom-function:
void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen,
IoT_Publish_Message_Params *params,
void *pData)
{
char shortTopic[512];
strncpy(shortTopic, topicName, topicNameLen);
if (subApplCallBackHandler != 0) //User call back if configured
subApplCallBackHandler(shortTopic, params->payloadLen,
(char *) params->payload);
}
@OllisGit Thank you for your answer but the shortTopic Contains some Junk Characters at the end which can be avoided by adding following line after strncpy(shortTopic, topicName, topicNameLen);
shortTopic[topicNameLen] = 0;
@OllisGit Good spotting! I have also implemented your fix
@muneebmalik78 good work on the null terminator. Always have to be careful of strncpy... it will not null terminate if it hits the n'th character.
You have to do it manually, or use:
snprintf(shortTopic, topicNameLen + 1,"%s", topicName);
When checking the topic name in the callback, it also includes the full payload. Is this expected?