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.

TM4C129ENCPDT: Execution goes to hardfault and then to error handler when DHCP client restarts for several times.

Part Number: TM4C129ENCPDT

Hi Guys,

I have a device that I configured as an HTTP client. I'm using the NDK stack to configure the Ethernet. Also, my project is configured in TIRTOS. I have a main task and a communication task, which handles the network-related tasks. It works fine. But when I remove and plug in the Ethernet several times, the execution goes to the error callback, which I created, and restarts. I currently don't have any idea why this issue is happening. The main task has higher priority than the communication task. and I'm using a semaphore to switch these tasks. When the network-related activities (sending data to the server, etc) are to be done, the main task goes to pending, and thus the communication task is executed. After completion, a semaphore post is called from the communication task. The error or hardfault happens when the Ethernet is disconnected and connected several times. So in the program, it happens just after the semaphore post is called. Then I checked the ROV and found that my communication task goes preempted in this situation. I'm attaching the screenshot below.

So, now I'm doubting if the dhcpStates task, which has a higher priority of 5, is causing the preemption to the Comm task.

or is it something related to any dynamic buffer allocated by the stack every time I plug in and maybe it doesn't free that, which causes a memory-related hardfault?

I'm adding the Comm task here for your reference.

void CommTask(void)
{
    char data[100];
    int ret;
    int len;
    char CONTENT_LENGTH[3];
    struct sockaddr_in addr;
    char *Payload;
    TLS_Params tlsParams;
    len = strlen(data);
    sprintf(CONTENT_LENGTH, "%d", len);
    System_printf("\nData: %s\n", data);
    System_printf("len(int): %d\n", len);
    System_printf("CONTENT_LENGTH: %s\n", CONTENT_LENGTH);
    System_flush();

    HTTPCli_Params params;

    HTTPCli_Field fields[] = {
               { HTTPStd_FIELD_NAME_HOST,HOSTNAME },
               { HTTPStd_FIELD_NAME_CONNECTION, "keep-alive"},
               { HTTPStd_FIELD_NAME_USER_AGENT, USER_AGENT },
               { HTTPStd_FIELD_NAME_ACCEPT, "application/octet-stream"},
               { NULL, NULL },
               { NULL, NULL }
           };

    while(1)
    {
        switch(NetworkState)
        {
        case NO_NETWORK:
            Task_sleep(400);
            if(EMAC_isLinkUp(link_index))               // check connectivity
            {
                if(Network_notifierFlag)
                {
                    UpdateDeviceTime();
                    NetworkState = NETWORK_INIT;
                }
                else
                {
                    if(NetworkStatus.Enable){
                        UartWrite("\n\rNo network \n\r");
                        NetworkStatus.Enable = FALSE;
                        NetworkStatus.Counter = 0;
                    }
                }
            }
            else
            {
                if(NetworkStatus.Enable){
                    UartWrite("\n\rNo Link\n\r");
                    NetworkStatus.Enable = FALSE;
                    NetworkStatus.Counter = 0;
                }
            }
            break;

        case NETWORK_INIT:
            if(NetworkInitializedFlag == false)             // create TLS task and construct HTTP client.
            {
                if(!Fota_Active)
                    UartWrite("\n\rSending a HTTPS POST request\n\r");

                TLS_Params_init(&tlsParams);
                tlsParams.ca = (uint8_t*)ca;
                tlsParams.calen = calen;
                tls = TLS_create(TLS_METHOD_CLIENT_TLSV1_2, &tlsParams, NULL);
                if (!tls)
                {
                    UartWrite("\nhttpsTask: TLS create failed\n\r");
                    Response_status = false;
                }
                HTTPCli_construct(&cli);
                HTTPCli_setRequestFields(&cli, fields);
                HTTPCli_Params_init(&params);
                params.tls = tls;
                params.timeout = 100;
                NetworkInitializedFlag = true;
                NetworkState = NEWTWORK_ONLINE;
            }
            if(!Global_NW_Status)
            {
                if(!EMAC_isLinkUp(link_index))
                {
                    NetworkState = NETWORK_CLOSE;
                }
            }
            Watchdog_clear(watchdogHandle);
            break;

        case NEWTWORK_ONLINE:
            Mailbox_pend(CommMail, data, BIOS_WAIT_FOREVER);        //Check for new message
            if(!EMAC_isLinkUp(link_index))                      //check if there is connectivity
            {
                Response_status = false;
                NetworkState = NETWORK_CLOSE;
                UartWrite("no link detected\n\r");
                break;
            }
            else if(strcmp(data,"N/W_CHK") == false)            // For network checking
            {
                Checking_NW_Active = true;
            }
            else
            {
                Payload = data;
                len = strlen(data);
            }
            sprintf(CONTENT_LENGTH, "%d", len);

            memset(&addr, 0, sizeof(addr));
            ret = HTTPCli_initSockAddr((struct sockaddr *)&addr,HOSTNAME, 0);  // socket creation in HTTP client.
            if (ret < 0)
            {
                UartWrite("\nhttpsTask: sock address resolution failed\n\r");
                Response_status = false;
                NetworkState = NETWORK_CLOSE;
                Watchdog_clear(watchdogHandle);
            }
            ret = HTTPCli_connect(&cli, (struct sockaddr *)&addr, 0,&params);   // connect to the server.
            if (ret < 0)
            {
                UartWrite("\nhttpsTask: connect failed \n\r");
                Response_status = false;
                NetworkState = NETWORK_CLOSE;
            }

            if(Checking_NW_Active == true)                                  // check the network is active or not.
            {
                if(ret<0)
                {
                    Response_status = false;
                }
                else
                {
                    Response_status = true;
                }
                Checking_NW_Active = false;
                NetworkState = NETWORK_CLOSE;
                break;
            }
            else
            {
                ret = HTTPCli_sendRequest(&cli, HTTPStd_POST, REQUEST_URI, true);   // sending request to server
                if (ret < 0)
                {
                    UartWrite("\nhttpsTask: send req failed \n\r");
                    Response_status = false;
                    Watchdog_clear(watchdogHandle);
                    NetworkState = NETWORK_CLOSE;
                }
                else
                {
                    UartWrite("\nsendRequest successful\n\r");
                }
            }

            ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_LENGTH, CONTENT_LENGTH, false);        // sending packet length to server
            if (ret < 0)
            {
                UartWrite("\nhttpsTask: sendfeild1 failed\n\r");
                Response_status = false;
                NetworkState = NETWORK_CLOSE;
            }

            ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE_JSON, true); // sending packets type to the server
            if (ret < 0)
            {
                UartWrite("\nhttpsTask: sendfeild1 failed\n\r");
                Response_status = false;
                NetworkState = NETWORK_CLOSE;
                break;
            }
            ret = HTTPCli_sendRequestBody(&cli, Payload, len);                              // sending packets to server
            if (ret < 0)
            {
               UartWrite("\nhttpTask: data body couldn't be sent\n\r");
               Response_status = false;
               NetworkState = NETWORK_CLOSE;
            }
            else if(!Fota_Active)
            {
                UartWrite("\nData sent to server successfully\n\r");
            }
            Response_status = false;        // resets the status before checking for response.

            ret = HTTPCli_getResponseStatus(&cli);           // wait sometime and get response from server.
            if(ret == HTTPStd_OK)
            {
                memset(RCVData, '\0', sizeof(RCVData));
                ret = HTTPCli_readRawResponseBody(&cli, RCVData, sizeof(RCVData));          // reading the raw body from the server response.
              
                if(RCVData[RESPONSE_PACKET_LOCATION]=='1')        // successfully posted in database
                {
                    Response_status = true;
                    UartWrite("\nSuccessfully post data to server\n\r");
                }
                else if(RCVData[RESPONSE_PACKET_LOCATION]=='3')                          // 338 for new database                        // Not write to database due to duplication
                {
                    Response_status = true;
                    UartWrite("\nError writing to the database\n\r");
                }
                else if( RCVData[RESPONSE_PACKET_LOCATION]=='4')                                                 // Not write to database due to error in format
                {
                    Response_status = true;
                    UartWrite("\nError in the packet format\n\r");
                }
                else if(RCVData[RESPONSE_PACKET_LOCATION]=='2')                                                  // Mac id is wrong
                {
                    Response_status = true;         // for testing
                    UartWrite("\nDoes not recognize the mac id\n\rData clearing\n\r");
                }
                else
                {
                     Response_status = true;        //for testing
                     UartWrite("\nUnknown response from server\n\rData clearing\n\r");
                }
            }
            else
            {
                Response_status = false;
                System_printf("\nData post failed with error code :%d\n\rhttpTask: Variable data couldn't be sent\n\r",ret);
                System_flush();
                UartWrite("\nData post failed\n\rhttpTask: Variable data couldn't be sent\n\r");
            }
            NetworkState = NETWORK_CLOSE;
            break;

        case NETWORK_CLOSE:
            if(NetworkInitializedFlag)          // delete TLS task and de-struct the HTTP client
            {
                HTTPCli_disconnect(&cli);
                HTTPCli_destruct(&cli);
                TLS_delete(&tls);
                NetworkState = NETWORK_INIT;
                NetworkInitializedFlag = false;

                Watchdog_clear(watchdogHandle);
                //Release Sync semaphore
                Semaphore_post(syncObject);
                Task_sleep(10);
            }
            else
            {
                NetworkState = NETWORK_INIT;
            }

            if(!EMAC_isLinkUp(link_index))
            {
                dhcpRunning = false;
                NetworkState = NO_NETWORK;
            }
            break;
        }
        Task_sleep(10);
    }
}