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.

TIVA,TM4C1294NCPDT, NDK run-time configuration of Static and Dynamic IP

Other Parts Discussed in Thread: TM4C1294NCPDT

Dear Sir,

I am working on a project using Tiva, TM4C1294NCPDT MCU ON TIRTOS environment and uses NDK module for TCP client application. My application requires runtime configuration of static and dynamic IP. I mean, the user should be able to configure the static IP with subnet mask, gateway address etc and also change to dynamic IP mode by selecting automatic IP obtain option. I am using XGCONFIG to Configure NDK.

I am able to select the static IP option in XGCONFIG and configure IP, subnet mask and other parameters using ‘Stack thread begin hook’  function. But I could not find any method to change between static IP and the dynamic ip option. One of the threads mention about using “Network IP address hook” for enabling DHCP but this hook is activated after the static IP is bound. Hence this may not solve my issue.

On a closer analysis of the file ‘ ATMcontroller_pem4f.c ‘, generated by XGCONFIG, I could figure out that the function  “Void ti_ndk_config_ip_init(HANDLE hCfg)” is the one which binds the IP, and this is generated by XGCONFIG as per the selected “Obtain IP address Automatically” option in XGCONFIG IP module configuration. I could not a find a way how to bypass this function and select my function to bind the IP depending on my configuration.

With regards

Suresh

  • Hello Suresh,

    Moving it to the TI RTOS Forum

    Regards
    Amit
  • Hi Suresh,

    Sorry for the delayed response.

    If I am understanding you correctly, it seems like you would like to be able to determine whether to configure static or dynamic IP based on an input.  By default, NDK will initialize it in either static or dynamic based on your *.cfg file configuration, this all happens in the NDK stack thread.  

    I think the best approach for what you want to accomplish is to write your own NDK stack thread.  In this thread, you can pend until you get the input to be static or dynamic IP.  To configure you custom NDK stack thread you must add the following to your *.cfg file:

    /* Set user thread for NDK */
    Global.stackThreadUser = "&userNdkStackThread";

    Below I took the NDK stack thread from the TI-RTOS tcpEcho example and added comments to what you can modify:

    Void userNdkStackThread(UArg arg0, UArg arg1)
    {
        int rc;
        HANDLE hCfg;
    
        ti_sysbios_knl_Clock_Params clockParams;
    
        /* Create the NDK heart beat */
        ti_sysbios_knl_Clock_Params_init(&clockParams);
        clockParams.startFlag = TRUE;
        clockParams.period = 100;
        ti_sysbios_knl_Clock_create(&llTimerTick, clockParams.period, &clockParams, NULL);
    
        /* THIS MUST BE THE ABSOLUTE FIRST THING DONE IN AN APPLICATION!! */
        rc = NC_SystemOpen(NC_PRIORITY_LOW, NC_OPMODE_INTERRUPT);
        if (rc) {
            xdc_runtime_System_abort("NC_SystemOpen Failed (%d)\n");
        }
    
        /* Create and build the system configuration from scratch. */
        hCfg = CfgNew();
        if (!hCfg) {
            xdc_runtime_System_printf("Unable to create configuration\n");
            goto main_exit;
        }
    
        /* Pend on a semaphore until you know if you want static or dynamic IP */
        Semaphore_pend(ipBlockingSem, BIOS_WAIT_FOREVER);
    
        /* add the IP module configuration settings. */
        if (staticIP) {
            /* THIS IS COPIED FROM ti_ndk_config_ip_init(hCfg) WHEN CONFIGURED FOR STATIC IP */
            /* Configure IP address manually on interface 1 */
            {
                CI_IPNET NA;
                CI_ROUTE RT;
                /* Setup manual IP address */
                bzero(&NA, sizeof(NA));
                NA.IPAddr  = inet_addr(LocalIPAddr);
                NA.IPMask  = inet_addr(LocalIPMask);
                strcpy(NA.Domain, DomainName);
                NA.NetType = 0;
    
                CfgAddEntry(hCfg, CFGTAG_IPNET, 1, 0,
                        sizeof(CI_IPNET), (UINT8 *)&NA, 0);
    
                /*
                 *  Add the default gateway. Since it is the default, the
                 *  destination address and mask are both zero (we go ahead
                 *  and show the assignment for clarity).
                 */
                bzero(&RT, sizeof(RT));
                RT.IPDestAddr = 0;
                RT.IPDestMask = 0;
                RT.IPGateAddr = inet_addr(GatewayIP);
    
                CfgAddEntry(hCfg, CFGTAG_ROUTE, 0, 0,
                        sizeof(CI_ROUTE), (UINT8 *)&RT, 0);
            }
        }
        else {
            /* THIS IS COPIED FROM ti_ndk_config_ip_init(hCfg) WHEN CONFIGURED FOR DYNAMIC IP */
            /* Use DHCP to obtain IP address on interface 1 */
            {
                CI_SERVICE_DHCPC dhcpc;
                UINT8 DHCP_OPTIONS[] = {
                    DHCPOPT_SUBNET_MASK,
                };
    
                /* Specify DHCP Service on IF specified by "IfIdx" */
                bzero(&dhcpc, sizeof(dhcpc));
                dhcpc.cisargs.Mode   = 1;
                dhcpc.cisargs.IfIdx  = 1;
                dhcpc.cisargs.pCbSrv = &ti_ndk_config_Global_serviceReport;
                dhcpc.param.pOptions = DHCP_OPTIONS;
                dhcpc.param.len = 1;
                CfgAddEntry(hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0,
                        sizeof(dhcpc), (UINT8 *)&dhcpc, 0);
            }
        }
    
        /* add the Tcp module configuration settings. */
        ti_ndk_config_tcp_init(hCfg);
    
        /* add the Udp module configuration settings. */
        ti_ndk_config_udp_init(hCfg);
    
        /* add the configuration settings for NDK low priority tasks stack size. */
        rc = 1024;
        CfgAddEntry(hCfg, CFGTAG_OS, CFGITEM_OS_TASKSTKLOW,
                     CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );
    
        /* add the configuration settings for NDK norm priority tasks stack size. */
        rc = 1024;
        CfgAddEntry(hCfg, CFGTAG_OS, CFGITEM_OS_TASKSTKNORM,
                     CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );
    
        /* add the configuration settings for NDK high priority tasks stack size. */
        rc = 1024;
        CfgAddEntry(hCfg, CFGTAG_OS, CFGITEM_OS_TASKSTKHIGH,
                     CFG_ADDMODE_UNIQUE, sizeof(uint), (UINT8 *)&rc, 0 );
    
        /*
         *  Boot the system using this configuration
         *
         *  We keep booting until the function returns 0. This allows
         *  us to have a "reboot" command.
        */
        do
        {
            rc = NC_NetStart(hCfg, ti_ndk_config_Global_NetworkOpen, 
                             ti_ndk_config_Global_NetworkClose, 
                             ti_ndk_config_Global_NetworkIPAddr);
    
        } while( rc > 0 );
    
        /* Delete Configuration */
        CfgFree(hCfg);
    
        /* Close the OS */
    main_exit:
        NC_SystemClose();
        xdc_runtime_System_flush();
    
    }

    Hope this helps,

    -- Emmanuel

  • Dear Emmanuel,
    Thank you very much for your reply. I will try your method and get back to you with the results.
    Meanwhile I tried another method successfully by configuring the static IP in the .cfg file and implementing following code int he network Open hook.
    void nwopenHook(){
    System_printf("In network Open HOOK \n");
    HANDLE hCfgIpAddr;
    HANDLE hCfg = CfgGetDefault();
    CfgGetEntry(0, CFGTAG_IPNET, 1, 1, &hCfgIpAddr);
    /* remove the current static IP entry */
    CfgRemoveEntry(0, hCfgIpAddr);
    //Wating for initializing the IP parameters configured by the user
    Semaphore_pend(networkConnectSem,BIOS_WAIT_FOREVER);
    if (hCfg) {
    if (Comm.ethernetMode==0x00){ //Dynamic IP configured by the user
    CI_SERVICE_DHCPC dhcpc;
    UINT8 DHCP_OPTIONS[] =
    {
    DHCPOPT_SUBNET_MASK,
    };

    /* Specify DHCP Service on IF specified by "IfIdx" */
    bzero(&dhcpc, sizeof(dhcpc));
    dhcpc.cisargs.Mode = CIS_FLG_IFIDXVALID;;
    dhcpc.cisargs.IfIdx = 1;
    dhcpc.cisargs.pCbSrv = &ti_ndk_config_Global_serviceReport;
    dhcpc.param.pOptions = DHCP_OPTIONS;
    dhcpc.param.len = 1;
    CfgAddEntry(hCfg, CFGTAG_SERVICE, CFGITEM_SERVICE_DHCPCLIENT, 0,
    sizeof(dhcpc), (UINT8 *)&dhcpc, 0);
    }else{ //Dynamic IP configured by the user

    sprintf(IpAddress,"%d.%d.%d.%d",Comm.ipAddress>>24 & 0xFF,Comm.ipAddress>>16 & 0xFF,Comm.ipAddress>>8 & 0xFF,Comm.ipAddress & 0xFF);
    LocalIPAddr=IpAddress;
    sprintf(subnetMask,"%d.%d.%d.%d",Comm.subnetMask>>24 & 0xFF,Comm.subnetMask>>16 & 0xFF,Comm.subnetMask>>8 & 0xFF,Comm.subnetMask & 0xFF);
    LocalIPMask=subnetMask;
    sprintf(gatewayIP,"%d.%d.%d.%d",Comm.gateWay>>24 & 0xFF,Comm.gateWay>>16 & 0xFF,Comm.gateWay>>8 & 0xFF,Comm.gateWay & 0xFF);
    GatewayIP=gatewayIP;
    sprintf(domainName,"%d.%d.%d.%d",Comm.DNS>>24 & 0xFF,Comm.DNS>>16 & 0xFF,Comm.DNS>>8 & 0xFF,Comm.DNS & 0xFF);
    DomainName=domainName;
    ti_ndk_config_ip_init(hCfg);
    }
    }

    }
    The NDK stack first gets the configured static IP and then waits for the user defined NDK parameters to initialize. Then it unbinds the static ip and configures the static/dynamic IP as configured by the user. This is serving my purpose for the time being. We are not sure whether above method is having any other disadvantages and is testing the same.
    But I feel it would have been quite easier if we had a global flag to identify whether the user wants Static or Dynamic IP , just as done in the case of LocalIP, subnet mask etc, which can be set in Stack thread begin hook and the 'ti_ndk_config_ip_init' function does the initialization depending on this flag.

    Thanking you once more

    With best regards

    Suresh