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

Topic name includes payload #10

Open
svanscho opened this issue May 18, 2018 · 6 comments
Open

Topic name includes payload #10

svanscho opened this issue May 18, 2018 · 6 comments

Comments

@svanscho
Copy link

When checking the topic name in the callback, it also includes the full payload. Is this expected?

@gadget-man
Copy link

I have the same error - I need to check the topicName as I'm subscribing to multiple topics. Did you find a solution?

@gadget-man
Copy link

gadget-man commented Jul 29, 2018

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.

@HumamHelfawi
Copy link

The original handler

void iot_subscribe_callback_handler(AWS_IoT_Client *pClient, char *topicName, uint16_t topicNameLen,
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.

@OllisGit
Copy link

OllisGit commented Oct 6, 2018

@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);
}

@muneebmalik78
Copy link

@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;

@intelligentandgoodlooking

@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);

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

No branches or pull requests

6 participants