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.

LAUNCHXL-CC3235SF: http_post connection is crashing, while creating two other pthread , other than http_post pthread (MainThread).

Part Number: LAUNCHXL-CC3235SF

Hi TI,

I'm not able to create multiple threads in my application project. i'm using mqtt_client example to connect with mqtt server. project firstly connects with http server and than receives mqtt connection parameter, and by using them it's connects with the mqtt sever. both task are performing from the same task (MainThread). which is working well. apart from this i have two more task. which is :

1) bssOperatorThread

2) rect_charg_control_Thread

while creating these thread, my http_client_executor function is getting disturbed mean's not able to connect with the sever and throwing errors. but if not creating the thread 1 and 2 as mentioned above, than it is working well.

it's seems there could be the memory issue between them, otherwise there no relation between of them task 1 and 2 are independent. while playing with the stack size and task priority some time it's working with the one either task as well , but never worked with both the task. 

here is my http_client_executor , please check what's wrong.

//TODO http_client_executor
extern uint8_t http_client_executor(void *pvParameters)
{
    uint8_t IsConnectedWithHttp=0;
    uint8_t tempReturn=0;

    // =============================== Sending a HTTP POST request starts===============================//
    bool moreDataFlag = false;


    int16_t ret = 0;
    int16_t len = 0;

    LOG_INFO("\r\nSending a HTTP POST request to '%s'\r\n", HOSTNAME);

    HTTPClient_Handle httpClientHandle;
    int16_t statusCode;
    httpClientHandle = HTTPClient_create(&statusCode, 0);
    if (statusCode < 0)
    {
        LOG_ERROR("httpTask: creation of http client handle failed", statusCode);
    }

    ret = HTTPClient_setHeader(httpClientHandle,
    HTTPClient_HFIELD_REQ_AUTHORIZATION,
                               USER_AGENT, strlen(USER_AGENT) + 1,
                               HTTPClient_HFIELD_PERSISTENT);
    if (ret < 0)
    {
        LOG_ERROR("httpTask: setting request header failed", ret);
    }

    /*     ===================  suggested header settings===================    */

    // Content Type-> application/json
    ret = HTTPClient_setHeader(httpClientHandle,
    HTTPClient_HFIELD_REQ_CONTENT_TYPE,
                               "application/json", strlen("application/json"),
                               HTTPClient_HFIELD_PERSISTENT);

    if (ret < 0)
    {
        LOG_ERROR("httpTask: setting request header failed", ret);
    }

    // setting charset=utf-8
    ret = HTTPClient_setHeader(httpClientHandle,
    HTTPClient_HFIELD_REQ_ACCEPT_CHARSET,
                               "charset=utf-8", strlen("HTTPClient_setHeader"),
                               HTTPClient_HFIELD_PERSISTENT);

    if (ret < 0)
    {
        LOG_ERROR("httpTask: setting request header failed", ret);
    }

    // x-api-key
    ret = HTTPClient_setHeaderByName(httpClientHandle,
    HTTPClient_HFIELD_REQ_CONTENT_TYPE,
                                     "x-api-key", API_KEY, strlen(API_KEY),
                                     HTTPClient_HFIELD_PERSISTENT);

    if (ret < 0)
    {
        LOG_ERROR("httpTask: setting request header failed", ret);
    }

    //x-app-name
    ret = HTTPClient_setHeaderByName(httpClientHandle,
    HTTPClient_HFIELD_REQ_CONTENT_TYPE,
                                     "x-app-name", APP_NAME, strlen(APP_NAME),
                                     HTTPClient_HFIELD_PERSISTENT);

    if (ret < 0)
    {
        LOG_ERROR("httpTask: setting request header failed", ret);
    }

    // x-svc
    ret = HTTPClient_setHeaderByName(httpClientHandle,
    HTTPClient_HFIELD_REQ_CONTENT_TYPE,
                                     "x-svc", SVC, strlen(SVC),
                                     HTTPClient_HFIELD_PERSISTENT);

    if (ret < 0)
    {
        LOG_ERROR("httpTask: setting request header failed", ret);
    }

    //---------------------------------------------------------//

    ret = HTTPClient_connect(httpClientHandle, HOSTNAME, 0, 0);
    if (ret < 0)
    {
        LOG_ERROR("httpTask: connect failed : %d", ret);
    }

    //===============   First encryption  ================== //

    uint8_t out[SHA256_HASH_SIZE];
    char out_str_app_key[SHA256_HASH_SIZE * 2 + 1];
    unsigned i;

    // Call hmac-sha256 function
    hmac_sha256(BASE_KEY, strlen(BASE_KEY), APP_NAME, strlen(APP_NAME), &out,
                sizeof(out));

    // Convert `out` to string
    memset(&out_str_app_key, 0, sizeof(out_str_app_key));

    for (i = 0; i < sizeof(out); i++)
    {
        snprintf(&out_str_app_key[i * 2], 3, "%02x", out[i]);
    }

    //LOG_INFO("App_Key: %s\r\n", out_str_app_key);

    //===============   Second encryption  ================== //

    // Call hmac-sha256 function
    memset(&out, 0, sizeof(out));
    hmac_sha256(out_str_app_key, strlen(out_str_app_key), DEVICE_ID,
                strlen(DEVICE_ID), &out, sizeof(out));

    char out_str_device_token[SHA256_HASH_SIZE * 2 + 1];

    // Convert `out` to string
    memset(&out_str_device_token, 0, sizeof(out_str_device_token));

    for (i = 0; i < sizeof(out); i++)
    {
        snprintf(&out_str_device_token[i * 2], 3, "%02x", out[i]);
    }

//    LOG_INFO("Device_Token: %s\r\n", out_str_device_token);

//---------------------------------------------------------//
    char REQ_BODY[150];

    memset(REQ_BODY, '\0', sizeof(REQ_BODY));

    sprintf(REQ_BODY,
            "{\"deviceToken\":\"%s\",\"deviceId\":\"%s\",\"mqttTls\":\"false\"}",
            out_str_device_token, DEVICE_ID);

//    LOG_INFO("REQ_BODY: %s\r\n", REQ_BODY);

    ret = HTTPClient_sendRequest(httpClientHandle, HTTP_METHOD_POST,
    REQUEST_URI,
                                 REQ_BODY, strlen(REQ_BODY), 0);
    if (ret < 0)
    {
        LOG_ERROR("httpTask: send failed", ret);
    }

    if (ret != HTTP_SC_OK)
    {
        LOG_ERROR("httpTask: cannot get status", ret);
        tempReturn=0;
        return tempReturn;
    }
    else
    {
        LOG_INFO("HTTP Response Status Code: %d\r\n", ret);
    }


    memset(data, '\0', sizeof(data));
    memset(Playload_Container, '\0', sizeof(Playload_Container));

    LOG_INFO("strlen of data : %d\r\n", strlen(data));

//    char *Playload_Container;
//    Playload_Container = (char*) malloc(sizeof(data));



    len = 0;
    do
    {
        ret = HTTPClient_readResponseBody(httpClientHandle, data, sizeof(data),
                                          &moreDataFlag);
        if (ret < 0)
        {
            LOG_ERROR("httpTask: response body processing failed", ret);
        }
//        UART_PRINT("%.*s \r\n", ret, data);
        sprintf(&Playload_Container[strlen(Playload_Container)], "%.*s\r\n",
                ret, data);
        len += ret;
    }
    while (moreDataFlag);


    ret = HTTPClient_disconnect(httpClientHandle);
    if (ret < 0)
    {
        LOG_ERROR("httpTask: disconnect failed", ret);
    }

    HTTPClient_destroy(httpClientHandle);


    LOG_INFO("PlayLoad : %s\r\n", Playload_Container);
    LOG_INFO("Received %d bytes of payload\r\n", len);

    //  =============  JSON parsing starts ===============//
    json_init(Http_Resp_PL_Topic);

    //--> Token Value
    char val_buff[100];
    memset(val_buff, '\0', sizeof(val_buff));
    IsConnectedWithHttp+=json_parser(Playload_Container, "\"token\"",
                &val_buff[0]);
    memcpy((http_para_for_mqtt_conn.token), val_buff,
           sizeof(http_para_for_mqtt_conn.token)); // copies token value from http, which going to be used as pw for mqtt connection.
    LOG_INFO("token : %.*s**********!\r\n", 2,(http_para_for_mqtt_conn.token));

    //--> Topics->fwd
    memset(val_buff, '\0', sizeof(val_buff));
    IsConnectedWithHttp+=json_parser(Playload_Container, "\"topic\".\"fwd\"",
                &val_buff[0]);
    memcpy((http_para_for_mqtt_conn.events_topic), val_buff,
           sizeof(http_para_for_mqtt_conn.events_topic)); // copies token value from http, which going to be used as pw for mqtt connection.
    LOG_INFO("fwd topic : %s\r\n", (http_para_for_mqtt_conn.events_topic));

    //--> Topics->rev
    memset(val_buff, '\0', sizeof(val_buff));
    IsConnectedWithHttp+=json_parser(Playload_Container, "\"topic\".\"rev\"",
                &val_buff[0]);
    memcpy((http_para_for_mqtt_conn.cmd_topic), val_buff,
           sizeof(http_para_for_mqtt_conn.cmd_topic)); // copies token value from http, which going to be used as pw for mqtt connection.
    LOG_INFO("rev topic : %s\r\n", (http_para_for_mqtt_conn.cmd_topic));

    //--> Topics->alert
    memset(val_buff, '\0', sizeof(val_buff));
    IsConnectedWithHttp+=json_parser(Playload_Container, "\"topic\".\"alert\"",
                &val_buff[0]);
    memcpy((http_para_for_mqtt_conn.alerts_topic), val_buff,
           sizeof(http_para_for_mqtt_conn.alerts_topic)); // copies token value from http, which going to be used as pw for mqtt connection.
    LOG_INFO("alert topic : %s\r\n", (http_para_for_mqtt_conn.alerts_topic));

    //--> mqttUrl
    memset(val_buff, '\0', sizeof(val_buff));
    IsConnectedWithHttp+=json_parser(Playload_Container, "\"mqttUrl\"",
                &val_buff[0]);
    memcpy((http_para_for_mqtt_conn.mqttUrl), val_buff,
           sizeof(http_para_for_mqtt_conn.mqttUrl)); // copies token value from http, which going to be used as pw for mqtt connection.
    LOG_INFO("mqttUrl : %.*s.**.**.**\r\n",2, (http_para_for_mqtt_conn.mqttUrl));
//    LOG_INFO("mqttUrl : %s\r\n",(http_para_for_mqtt_conn.mqttUrl));

    //--> mqttPort
    memset(val_buff, '\0', sizeof(val_buff));
    IsConnectedWithHttp+=json_parser(Playload_Container, "\"mqttPort\"",
                &val_buff[0]);
    (http_para_for_mqtt_conn.mqttPort)=atoi(val_buff); // copies token value from http, which going to be used as pw for mqtt connection.
    LOG_INFO("mqttPort : %d\r\n", (http_para_for_mqtt_conn.mqttPort));

    //--> tlsCaCrt
    memset(val_buff, '\0', sizeof(val_buff));
    IsConnectedWithHttp+=json_parser(Playload_Container, "\"tlsCaCrt\"",
                &val_buff[0]);
    LOG_INFO("tlsCaCrt : %s\r\n", val_buff);

    //--> mqttTls
    memset(val_buff, '\0', sizeof(val_buff));
    IsConnectedWithHttp+=json_parser(Playload_Container, "\"mqttTls\"",
                &val_buff[0]);

    if (strcmp(val_buff, "false") == 0)
    {
        (http_para_for_mqtt_conn.mqttTls) = false;
    }
    else
    {
        (http_para_for_mqtt_conn.mqttTls) = true;
    }
    LOG_INFO("mqttTls : %d\r\n", (http_para_for_mqtt_conn.mqttTls));

    json_dinit();

    //  =============  JSON parsing ends ===============//

//    free(Playload_Container);


    if(IsConnectedWithHttp==8)
    {
        tempReturn=1;
    }
    else
    {
        tempReturn=0;
    }

    return tempReturn;
    // =============================== Sending a HTTP POST request ends===============================//
}

and both the thread i'm creating is in Main function only. have a look:

int main(void)
{
    /* Call board init functions */
    Board_init();

    /*UART for Serial Monitor Printing*/
    uartHandle = InitTerm();
    UART_control(uartHandle, UART_CMD_RXENABLE, NULL);

    pthread_t thread;
    pthread_attr_t pAttrs;
    struct sched_param priParam;
    int retc;
    int detachState;

    //======================= Main thread starts =================//

    /* Set priority and stack size attributes */
    pthread_attr_init(&pAttrs);
    priParam.sched_priority = 3;

    detachState = PTHREAD_CREATE_DETACHED;
    retc = pthread_attr_setdetachstate(&pAttrs, detachState);
    if (retc != 0)
    {
        /* pthread_attr_setdetachstate() failed */
        while (1)
        {
            ;
        }
    }

    pthread_attr_setschedparam(&pAttrs, &priParam);

    retc |= pthread_attr_setstacksize(&pAttrs, THREADSTACKSIZE);
    if (retc != 0)
    {
        /* pthread_attr_setstacksize() failed */
        while (1)
        {
            ;
        }
    }

    retc = pthread_create(&thread, &pAttrs, mainThread, NULL);
    if (retc != 0)
    {
        /* pthread_create() failed */
        while (1)
        {
            ;
        }
    }

    //======================= Main thread ends =================//

    //======================= Creating BSS Operations thread starts =================//
    pthread_attr_t pAttrs_1;
    struct sched_param priParam_1;
    pthread_t thread_1;
    retc = 0;

    /* Set priority and stack size attributes */
    pthread_attr_init(&pAttrs_1);
    priParam_1.sched_priority = 1;

    retc = pthread_attr_setdetachstate(&pAttrs_1, PTHREAD_CREATE_DETACHED);
    if (retc != 0)
    {
        /* pthread_attr_setdetachstate() failed */
        while (1)
        {
            ;
        }
    }

    retc = pthread_attr_setschedparam(&pAttrs_1, &priParam_1);

    //        retc |= pthread_attr_setstacksize(&pAttrs_1, 2048);
    retc |= pthread_attr_setstacksize(&pAttrs_1, (1024 * 5));
    if (retc != 0)
    {
        /* pthread_attr_setstacksize() failed */
        while (1)
        {
            ;
        }
    }

    retc = pthread_create(&thread_1, &pAttrs_1, bssOperatorThread, NULL);
    if (retc != 0)
    {
        /* pthread_create() failed */
        while (1)
        {
            ;
        }
    }
    //======================= Creating BSS Operations thread ends =================//

    //   *************** creating a new rect_charg_control_Thread **************//
    pthread_attr_t pAttrs_2;
    struct sched_param priParam_2;
    pthread_t thread_2;
    int retc = 0;

    /* Set priority and stack size attributes */
    pthread_attr_init(&pAttrs_2);
    priParam_2.sched_priority = 1;

    retc = pthread_attr_setdetachstate(&pAttrs_2, PTHREAD_CREATE_DETACHED);
    if (retc != 0)
    {
        /* pthread_attr_setdetachstate() failed */
        while (1)
        {
            ;
        }
    }

    pthread_attr_setschedparam(&pAttrs_2, &priParam_2);

    retc |= pthread_attr_setstacksize(&pAttrs_2, (1024 * 5));
    if (retc != 0)
    {
        /* pthread_attr_setstacksize() failed */
        while (1)
        {
            ;
        }
    }

    retc = pthread_create(&thread_2, &pAttrs_2, rect_charg_control_Thread,
    NULL);
    if (retc != 0)
    {
        /* pthread_create() failed */
        while (1)
        {
            ;
        }
    }
    //=======================  rect_charg_control_Thread ends =================//

    /* Start the FreeRTOS scheduler */
    vTaskStartScheduler();

    return (0);
}

please help to get rid from this issue.

Thanks in advance,

sarju