Skip to content

Topic name includes payload #10

Description

@svanscho

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

Activity

  1. gadget-man commented on Jul 29, 2018

    @gadget-man

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

  2. gadget-man commented on Jul 29, 2018

    @gadget-man

    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.

  3. HumamHelfawi commented on Aug 2, 2018

    @HumamHelfawi

    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.

  4. OllisGit commented on Oct 6, 2018

    @OllisGit

    @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);
    }
    
  5. muneebmalik78 commented on Feb 22, 2019

    @muneebmalik78

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

  6. intelligentandgoodlooking commented on Nov 27, 2020

    @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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions