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.

TM4C1294NCPDT: E_stackOverflow Daemon Task

Part Number: TM4C1294NCPDT
Other Parts Discussed in Thread: SYSBIOS

Hi,

We have created three individual tasks as TCP Client, TCP Server, Heart Beat Tasks and HTTP Server.

While connecting through browser by pressing refresh "ti.sysbios.knl.Task: line 373: E_stackOverflow: Task 0x20001c68 stack overflow.
xdc.runtime.Error.raise: terminating execution" and getting aborting the operation flow.

ROV Status at the time of system got aborted

Address 0x20001c68

Lable:

Priority: 5

Module: Blocked

Function: daemon

Arg0: 0x0

Arg1: 0x0

Stack Peak: 1024

Stack Size: 1024

Stack Base: 0x20001cb8

Current Code ID: n/a

Affinity: n/a

Blocked On: Semaphore: 0x200020c8

Please go through attached source code for your reference and suggest us any enhancement required for best performance.

Can you please guide us how to fix this issue.

Int getTime(SOCKET s, int length)
{
    Char buf[200];
    static UInt scalar = 0;

    if (scalar == 0) {
        scalar = 1000000u / Clock_tickPeriod;
    }

    httpSendStatusLine(s, HTTP_OK, CONTENT_TYPE);
    httpSendClientStr(s, CRLF);

    System_sprintf(buf, "<p style=\"font-size: 22px;\">Up for %d seconds</p>\n",
        ((unsigned long)Clock_getTicks() / scalar));

    httpSendClientStr(s, buf);
    httpSendClientStr(s, "</table></body></html>");
    return (1);
}

Void tcpWorker(UArg arg0, UArg arg1)
{
    int  clientfd = (int)arg0;
    int  bytesRcvd;
    int  bytesSent;
    unsigned char buffer[TCPPACKETSIZE];
    unsigned char wCnt;
    unsigned int dCnt = 0;

    System_printf("tcpWorker: start clientfd = 0x%x\n", clientfd);

	while ((bytesRcvd = recv(clientfd, buffer, TCPPACKETSIZE, 0)) > 11) {
		if(bytesRcvd > 11){
			System_printf("Received Frame Length = 0x%d\n", bytesRcvd);
			memmove(MB_Recv_Buffer,buffer,bytesRcvd);
			memset(buffer,TCPPACKETSIZE, 0x00);
			bytesRcvd = 0;
			serialflag = 1;
			if((MB_Recv_Buffer[6] == Slave_ID) && ((MB_Recv_Buffer[7] == 0x01)||(MB_Recv_Buffer[7] == 0x03) \
			 ||(MB_Recv_Buffer[7] == 0x04)))
				HandleModbusRequest(1);
			memset(MB_Recv_Buffer,0x00,128);
			if(tx_start == 1){
				bytesSent = send(clientfd, MB_Tran_Buffer, no_tx_char, 0);
				System_printf("Transmit Frame Length = 0x%d\n", bytesSent);
				tx_start = 0;
			}
		}
	}

	System_printf("tcpWorker stop clientfd = 0x%x\n", clientfd);
	Task_sleep(1000);
	close(clientfd);
}

/*
 *  ======== tcpHandler ========
 *  Creates new Task to handle new TCP connections.
 */
Void tcpHandler(UArg arg0, UArg arg1)
{
    int                status;
    int                clientfd;
    int                server;
    struct sockaddr_in localAddr;
    struct sockaddr_in clientAddr;
    int                optval;
    int                optlen = sizeof(optval);
    socklen_t          addrlen = sizeof(clientAddr);
    Task_Handle        taskHandle;
    Task_Params        taskParams;
    Error_Block        eb;

    server = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
    if (server == -1) {
        System_printf("Error: socket not created.\n");
        goto shutdown;
    }


    memset(&localAddr, 0, sizeof(localAddr));
    localAddr.sin_family = AF_INET;
    localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    localAddr.sin_port = htons(arg0);

    status = bind(server, (struct sockaddr *)&localAddr, sizeof(localAddr));
    if (status == -1) {
        System_printf("Error: bind failed.\n");
        goto shutdown;
    }

    status = listen(server, NUMTCPWORKERS);
    if (status == -1) {
        System_printf("Error: listen failed.\n");
        goto shutdown;
    }

    optval = 1;
    if (setsockopt(server, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen) < 0) {
        System_printf("Error: setsockopt failed\n");
        goto shutdown;
    }

	while ((clientfd =
			accept(server, (struct sockaddr *)&clientAddr, &addrlen)) != -1) {

		System_printf("tcpHandler: Creating thread clientfd = %d\n", clientfd);

		/* Init the Error_Block */
		Error_init(&eb);

		/* Initialize the defaults and set the parameters. */
		Task_Params_init(&taskParams);
		taskParams.arg0 = (UArg)clientfd;
		taskParams.stackSize = 1280;
		taskHandle = Task_create((Task_FuncPtr)tcpWorker, &taskParams, &eb);
		if (taskHandle == NULL) {
			System_printf("Error: Failed to create new Task\n");
			close(clientfd);
		}

		/* addrlen is a value-result param, must reset for next accept call */
		addrlen = sizeof(clientAddr);
	}

	System_printf("Error: accept failed.\n");

shutdown:
	if (server > 0) {
		close(server);
	}
}

Void tcpHandler2(UArg arg0, UArg arg1)
{
    int                status;
    int                clientfd;
    int                optval;
    int                optlen = sizeof(optval);
    int                txbytes;

    SOCKET lSocket;
    struct sockaddr_in sLocalAddr;

    lSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

    if (lSocket < 0) {
        System_printf("tcpHandler2: socket failed\n");
        Task_exit();
        return;
    }

    memset((char *)&sLocalAddr, 0, sizeof(sLocalAddr));
    sLocalAddr.sin_family = AF_INET;//
    sLocalAddr.sin_addr.s_addr = Server_Ip_Address_Static;
    sLocalAddr.sin_port = htons(arg0);

    while(clientfd = (connect(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr)) < 0)){
        Task_sleep(100);
        System_printf("Connection failed.\n%d", errno);
    }

    System_flush();

    status = bind(lSocket, (struct sockaddr *)&sLocalAddr, sizeof(sLocalAddr));
    if (status == -1) {
        System_printf("Error: bind failed.\n");
    }

    optval = 2;
    if (setsockopt(lSocket, SOL_SOCKET, SO_RCVTIMEO, &optval, optlen) < 0) {
        System_printf("Error: setsockopt failed\n");
    }

    optval = 1;
    setsockopt(lSocket, SOL_SOCKET, SO_KEEPALIVE, &optval, optlen);
    while (1) {

        txbytes = send(lSocket, "OK",2,0);
        System_printf("No. of Bytes Transmitted on 5000 Port %d\n", txbytes);
        System_printf("Bytes Transmitted on 5000 Port %s\n", MB_Tran_Buffer);
        tx_comp = 0;
        if (connect(lSocket, (struct sockaddr *) &sLocalAddr, sizeof(sLocalAddr)) < 0){
          System_printf("DID NOT CONNECT \n");
          System_printf("%d\n", fdError());
        } else {
          System_printf("CONNECTED \n");
        }
        Task_sleep(10000);
	}
}

int main(void)
{
    /* Call board init functions */
    Board_initGeneral();
    Board_initGPIO();
    Board_initEMAC();

    System_printf("Starting the Styrax MODBUS TCP \n Web UI \n System provider is set to "
                  "SysMin. Halt the target to view any SysMin contents in"
                  " ROV.\n");
    /* SysMin will only print to the console when you call flush or exit */
    System_flush();

    Internal_EEPROM_Init_Read_Write();

    /* Start BIOS */
    BIOS_start();

    return (0);
}

Void functionNetworkOpenHook(Void)
{
    Task_Handle taskHandle;
    Task_Params taskParams;
    Error_Block eb;
    Error_init(&eb);


    char *HostName = "_csl_";

    CI_IPNET NA;

    CI_ROUTE RT;
    HANDLE hCfg;

    HANDLE hCfgIpAddr;

    /* Setup manual IP address */
    bzero(&NA, sizeof(NA));
    NA.IPAddr = Ip_Address_Static;//inet_addr("192.168.0.143");//
    NA.IPMask = GateWayAddress;//inet_addr("255.255.255.0");
    strcpy(NA.Domain, "demo.net");
    NA.NetType = 0;

    /* get the current static IP entry */
    CfgGetEntry(0, CFGTAG_IPNET, 1, 1, &hCfgIpAddr);

    /* remove the current static IP entry */
    CfgRemoveEntry(0, hCfgIpAddr);

    /* add a new static IP entry */
    CfgAddEntry(0, CFGTAG_IPNET, 1, 0,
    sizeof(CI_IPNET), (UINT8 *)&NA, 0);


    bzero( &RT, sizeof(RT) );
    RT.IPDestAddr = 0;
    RT.IPDestMask = 0;
    RT.IPGateAddr = GateWayAddress;//inet_addr("10.7.55.254");

    hCfg = CfgNew();
    CfgAddEntry( hCfg, CFGTAG_SYSINFO, CFGITEM_DHCP_HOSTNAME, 0,
    strlen(HostName), (UINT8 *)HostName, 0 );

    // Add the route
    CfgAddEntry( hCfg, CFGTAG_ROUTE, 0, 0, sizeof(CI_ROUTE), (UINT8 *)&RT, 0 );

    Task_Params_init(&taskParams);
    taskParams.stackSize = PORT502_STACK;
    taskParams.priority = 1;
    taskParams.arg0 = TCPPORT;
    taskHandle = Task_create((Task_FuncPtr)tcpHandler, &taskParams, &eb);
    if (taskHandle == NULL) {
        System_printf("netOpenHook: Failed to create MODBUS TCP Server Task\n");
    }
    System_flush();

    Task_Params_init(&taskParams);
    taskParams.stackSize = PORT5000_STACK;
    taskParams.priority = 2;
    taskParams.arg0 = TCPPORT1;
    taskHandle = Task_create((Task_FuncPtr)tcpHandler2, &taskParams, &eb);
    if (taskHandle == NULL) {
        System_printf("netOpenHook: Failed to create TCP Client Port 5000 Task\n");
    }
    System_flush();

    Task_Params_init(&taskParams);
    taskParams.stackSize = HEARTBEAT_TASK;
    taskParams.priority = 3;
    taskParams.arg0 = 1000;
    taskHandle = Task_create((Task_FuncPtr)heartBeatFxn, &taskParams, &eb);
    if (taskHandle == NULL) {
        System_printf("netOpenHook: Failed to create Heart Beat Task\n");
    }
    System_flush();

}

With Regards,