This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3200: AWS IoT publish_subscribe_sample problem: "_aws_iot_mqtt_internal_decode_packet_remaining_len L#338 Return Code : -12 "

Part Number: CC3200
Other Parts Discussed in Thread: UNIFLASH

Hello,

I have been working with AWS IoT sample code for the CC3200, and have been having problems connecting to the mqtt broker in the amazon side. I keep getting this error msg: "_aws_iot_mqtt_internal_decode_packet_remaining_len L#338 Return Code : -12 " until the mqtt client gives up and calls a timeout at the end.

Thanks in advance,
Andres Blanco

  • Andres,


    Can you try the following code changes?

    Can you update the function "iot_tls_read()" (in platform/tirtos/network_sl.c) as follows (updates in light blue and green)?

    #include <xdc/runtime/System.h>
    ...
    IoT_Error_t iot_tls_read(Network *pNetwork, unsigned char *pMsg, size_t len,
            Timer *timer, size_t *numbytes)
    {
        int bytes = 0;
        int skt;
        struct timeval tv;
        Ssock_Handle ssock = NULL;
        uint32_t timeout;
        int16_t retVal = 0;

    ...

        if ((retVal = setsockopt(skt, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
                sizeof(struct timeval))) == 0) {
            bytes = Ssock_recvall(ssock, pMsg, len, 0);
            if (bytes > 0) {
                *numbytes = (size_t)bytes;
                return (SUCCESS);
            }
            else if (bytes == Ssock_TIMEOUT) {
                /* nothing to read in the socket buffer */
                return (NETWORK_SSL_NOTHING_TO_READ);
            }
            else {
                System_printf("iot_tls_read: Ssock_recvall() failed (%d)\n", bytes);
            }
        }
        else {
            System_printf("iot_tls_read: setsockopt faild, ret code = %d\n", retVal);
        }
        return (NETWORK_SSL_READ_ERROR);
    }

    What's the output of the green print?

    Steve

  • If Ssock_recvall() is returning 0 bytes, it looks like that's due to SimpleLink's sl_Recv() returning 0 bytes.  Unfortunately, the documentation for sl_Recv() is not clear on why it would return 0.

    I have an additional thing for you to check in the code.  Can you update the print (in orange below) to display the value of timeout?

    #include <xdc/runtime/System.h>
    ...
    IoT_Error_t iot_tls_read(Network *pNetwork, unsigned char *pMsg, size_t len,
            Timer *timer, size_t *numbytes)
    {
        int bytes = 0;
        int skt;
        struct timeval tv;
        Ssock_Handle ssock = NULL;
        uint32_t timeout;
        int16_t retVal = 0;

    ...

        timeout = left_ms(timer);
        if (timeout == 0) {
            /* sock timeout of 0 == block forever; just read + return if expired */
            timeout = 1;
        }

    ...

        if ((retVal = setsockopt(skt, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,
                sizeof(struct timeval))) == 0) {
            bytes = Ssock_recvall(ssock, pMsg, len, 0);
            if (bytes > 0) {
                *numbytes = (size_t)bytes;
                return (SUCCESS);
            }
            else if (bytes == Ssock_TIMEOUT) {
                /* nothing to read in the socket buffer */
                return (NETWORK_SSL_NOTHING_TO_READ);
            }
            else {
                System_printf("iot_tls_read: Ssock_recvall() failed (%d). timeout = %d\n", bytes, timeout);
            }
        }
        else {
            System_printf("iot_tls_read: setsockopt faild, ret code = %d\n", retVal);
        }
        return (NETWORK_SSL_READ_ERROR);
    }

    If timeout is 1, can you change the line in yellow to be:

        timeout = 10;

    Steve

  • Hello Steve,

    I updated the code, here is the output I am getting:

    "FUNC_ENTRY: aws_iot_mqtt_internal_wait_for_read L#608
    iot_tls_read: Ssock_recvall() failed (-452). timeout = 19999
    FUNC_ENTRY: _aws_iot_mqtt_internal_decode_packet_remaining_len L#324
    iot_tls_read: Ssock_recvall() failed (0). timeout = 19591
    FUNC_EXIT: _aws_iot_mqtt_internal_decode_packet_remaining_len L#338 Return Code : -12
    iot_tls_read: Ssock_recvall() failed (0). timeout = 19590
    FUNC_ENTRY: _aws_iot_mqtt_internal_decode_packet_remaining_len L#324
    iot_tls_read: Ssock_recvall() failed (0). timeout = 19589
    FUNC_EXIT: _aws_iot_mqtt_internal_decode_packet_remaining_len L#338 Return Code : -12
    iot_tls_read: Ssock_recvall() failed (0). timeout = 19588
    FUNC_ENTRY: _aws_iot_mqtt_internal_decode_packet_remaining_len L#324
    iot_tls_read: Ssock_recvall() failed (0). timeout = 19587"

    and it keeps going down like this.

    Thanks,
    Andres
  • Hi Andres,

    This example is not supported by our team, so I will move it to get you better support.

    Best regards,
    Sarah
  • Andres,

    Andres_Blanco said:
    iot_tls_read: Ssock_recvall() failed (-452). timeout = 19999

    I see that this error means that the TLS connection (or maybe just handshake) was closed or ended by the AWS side.  It seems that AWS seems to think some setting or maybe cert isn't right:

      from tirtos_cc32xx_2_16_00_08/products/tidrivers_cc32xx_2_16_00_08/packages/ti/mw/wifi/cc3x00/simplelink/include/socket.h:

    #define SL_ESECCLOSED   (-452)  /* secure layrer is closed by other size , tcp is still connected  */

    Perhaps there is an issue with your certificate files?  Or your AWS credentials?

    Please double check the macros settings you made in the aws_iot_config.h file.  In particular, double check the values set for these macros:

    #define AWS_IOT_MQTT_HOST              ///< Customer specific MQTT HOST. The same will be used for Thing Shadow

    #define AWS_IOT_MQTT_PORT              8883 ///< default port for MQTT/S

    #define AWS_IOT_MQTT_CLIENT_ID         ///< MQTT client ID should be unique for every device

    #define AWS_IOT_MY_THING_NAME          ///< Thing Name of the Shadow this device is associated with

    Steve

  • Hello,

    I have verified my macros and they are all correct.

    I decided to delete all my certificates and keys and create new ones. Here is the process I followed:

    1. Deleted all certificates and policies on AWS IoT

    2. Created new policy and certificates

    3. Activated the certificate and downloaded them

    4.Copy and pasted the CA-root provided by amazon into a text file with .pem extension

    5. Used openssl to convert the key and certificates from .pem to .der format

    6. Used UniFlash to flash the files into the device, here is a screenshot

    7. Tried again, and no luck. Here is the outputs

    FUNC_ENTRY: aws_iot_mqtt_internal_wait_for_read L#608
    iot_tls_read: Ssock_recvall() failed (-452). timeout = 19998
    FUNC_ENTRY: _aws_iot_mqtt_internal_decode_packet_remaining_len L#324
    iot_tls_read: Ssock_recvall() failed (0). timeout = 19997
    FUNC_EXIT: _aws_iot_mqtt_internal_decode_packet_remaining_len L#338 Return Code : -12

     

     

     

    Thank you for your time,

    Andres Blanco

  • Andres,

    Can you try using the cert flasher application instead of uniflash? In this app, you'll copy the contents of your cert files into C arrays. Running the app will flash the certs.

    The cert flasher app is included in the SDK. Please see the README_cc3200.md file for directions on how to build/run that app.

    It shouldn't be too time consuming to try this app, and it's good to try because this is the way I flashed my certs and tested the apps.

    Steve
  • Steve,

    As you suggested I formatted the device and ran the certflasher app, following the readme instructions, and I went back to the error I was having earlier during the week, here is the output:

    FUNC_EXIT: aws_iot_mqtt_set_connect_params L#152 Return Code : 0
    FUNC_EXIT: _aws_iot_mqtt_internal_connect L#397 Return Code : -24
    FUNC_ENTRY: aws_iot_mqtt_set_client_state L#101
    FUNC_ENTRY: aws_iot_mqtt_get_client_state L#54
    FUNC_EXIT: aws_iot_mqtt_get_client_state L#59 Return Code : 2
    FUNC_EXIT: aws_iot_mqtt_set_client_state L#126 Return Code : 0
    FUNC_EXIT: aws_iot_mqtt_connect L#481 Return Code : -24
    ERROR: runAWSClient L#121 Error(-24) connecting to a1d0asj9dz0ihb.iot.us-west-2.amazonaws.com:8883
    FUNC_ENTRY: aws_iot_mqtt_autoreconnect_set_status L#278
    FUNC_EXIT: aws_iot_mqtt_autoreconnect_set_status L#283 Return Code : 0
    Subscribing...
    FUNC_ENTRY: aws_iot_mqtt_subscribe L#292
    FUNC_ENTRY: aws_iot_mqtt_is_client_connected L#233
    FUNC_EXIT: aws_iot_mqtt_is_client_connected L#264 Return Code : 0
    FUNC_EXIT: aws_iot_mqtt_subscribe L#299 Return Code : -13
    ERROR: runAWSClient L#139 Error subscribing (-13)
    ERROR: runAWSClient L#194 An error occurred in the loop. Error code = -13
  • Andres,

    I've been able to reproduce this issue locally. I'm debugging it now and will get back to you once I know more.

    Steve
  • Andres -

    I was able to get the example to work.  The problem was the AWS policy.

    I changed my policy for the thing that I reproduced the issue on to be more generic:

    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": "iot:*",
          "Resource": "*"
        }
      ]
    }

    What does your policy look like?  Assuming it's different than the above, can you try changing it to have the above parts in bold?

    I changed the policy via the AWS console web page.  Within the policy, I was able to edit it and create a new version (with the above code), then set the new version to be the default for the policy (see screen shot).

    Steve

  • Thank you! This helped, I am able to establish a connection and send messages up to the AWS cloud.