Programming Guide
The following sections provide example code describing how to use µC/DHCPc.
Include Files
Wherever you want to use µC/DHCPc, you should include the following header:
Include file | Description |
---|---|
Source/dhcp-c.h | Contains DHCPc API definitions |
API reference
Module initialization
Function name | Description |
---|---|
DHCPc_Init() | Initialize µC/DHCPc module's objects and tasks. This function must be called prior to any other API |
Start and Stop DHCP negotiation and management
Function name | Description |
---|---|
DHCPc_Start() | Starts the DHCP address configuration and management on the specified interface. |
DHCPc_Stop() | Stops the DHCP address configuration and management on the specified interface. |
DHCP Status and Option values
Function name | Description |
---|---|
DHCPc_ChkStatus() | Checks an interface’s DHCP status and last error. |
DHCPc_GetOptVal() | Gets the value of a specific DHCP option for a given interface. |
Getting DHCP option values
It is possible to ask the server to send some DHCP parameter option during a DHCP's negociation such as the DNS address. This can be done by adding an option before starting the DHCP management on an interface and using the API DHCPc_GetOptVal() to retrieve the value once the negotiation is completed. Note that it's possible that the server doesn't return a value even if it has been explicitly requested.
Configuration
The requested table size must be properly configured to be able to receive as many parameter as requested. See DHCP Option Configuration for more information.
Example code
Refer to Example Application, where the DNS address parameter is requested.
Link Local Address
If no server is found on the network, µC/DHCPc can configure automatically a link local address as defined in RFC 3927. Link-local addresses for IPv4 are defined in the address block 169.254.0.0/16.
Configuration
It is possible to enable or disable the automatic local address Link configuration feature from the configuration, see Link Local Option Configuration.
How to know if a link local has been configured
When the Link local feature is enabled, the API DHCPc_ChkStatus() would return the status DHCP_STATUS_CFGD_LOCAL_LINK
when no server has been found and an Link local address has been configured.
Managing µC/DHCPc and the interface's link state (Plug/Unplug)
DHPC negotiation and management should be started only when the Interface's link state is up (a cable is plugged). Once the link come down (cable unplugged) DHCP management should be stopped, to allow to get a new IP address when the link come back up again. There are 2 mechanism available to apply such functionality. You can poll µC/TCP-IP to get peridodicaly the link state using the API NetIF_LinkStateGet or be notified by the stack by registering a function with the API function NetIF_LinkStateSubscribe.