Nios II Classic Software Developer’s Handbook

ID 683282
Date 5/14/2015
Public
Document Table of Contents

11.4.4. Calling the Sockets Interface

After you initialize your Ethernet device, use the sockets API in the remainder of your program to access the IP stack.

To create a new task that talks to the IP stack using the sockets API, you must use the function TK_NEWTASK(). The TK_NEWTASK() function is part of the NicheStack TCP/IP Stack operating system (OS) porting layer. TK_NEWTASK() calls the MicroC/OS-II OSTaskCreate() function to create a thread, and performs some other actions specific to the NicheStack TCP/IP Stack.

The prototype for TK_NEWTASK() is:

int TK_NEWTASK(struct inet_task_info* nettask);

An Implementation of get_ip_addr()

#include <alt_iniche_dev.h>
#include "includes.h"
#include "ipport.h"
#include "tcpport.h"
int get_ip_addr(alt_iniche_dev* p_dev,
       ip_addr* ipaddr,
       ip_addr* netmask,
       ip_addr* gw,
       int* use_dhcp)
{
       int ret_code = -1;
       /*
       * The name here is the device name defined in system.h
       */
       if (!strcmp(p_dev->name, "/dev/" INICHE_DEFAULT_IF))
       {
         /* The following is the default IP address if DHCP
         fails, or the static IP address if DHCP_CLIENT is
         undefined. */
       IP4_ADDR(&ipaddr, 10, 1, 1 ,3);
       /* Assign the Default Gateway Address */
       IP4_ADDR(&gw, 10, 1, 1, 254);
       /* Assign the Netmask */
       IP4_ADDR(&netmask, 255, 255, 255, 0);
#ifdef DHCP_CLIENT
       *use_dhcp = 1;
#else
       *use_dhcp = 0;
#endif /* DHCP_CLIENT */
       ret_code = ERR_OK;
     }
     return ret_code;
}
Note: There is no error checking in this example. In a real application, you might need to return -1 on error.

The prototype is defined in <iniche path>/src/nios2/osport.h. You can include this header file as follows:

#include "osport.h"

You can find other details of the OS porting layer in the osport.c file in the NicheStack TCP/IP Stack component directory, <iniche path>/src/nios2/.