- This example show how to initialize µC/DHCPc:
- Initialize DHCP client's tasks and objects
- Start the DHCP management of an interfaces and request DNS address from the DHCP server during the negotiation.
- Monitor the DHCP negotiation.
- Retrieve Host address configured and DNS address, if the negotiation was successfully completed.
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||
#include <Source/os.h> /* OS header. */
#include <IP/IPv4/net_ipv4.h>
#include <Source/net_type.h>
#include <Source/net_ip.h>
#include <Source/dhcp-c.h>
CPU_BOOLEAN AppDHCPc_Init (NET_IF_NBR if_nbr,
NET_IPv4_ADDR *p_addr_cfgd,
NET_IPv4_ADDR *p_addr_dns)
{
DHCPc_STATUS status;
CPU_BOOLEAN done;
DHCPc_OPT_CODE req_param[DHCPc_CFG_PARAM_REQ_TBL_SIZE];
CPU_INT08U req_param_qty;
NET_IPv4_ADDR addr_tbl[NET_IPv4_CFG_IF_MAX_NBR_ADDR];
NET_IP_ADDRS_QTY addr_ip_tbl_qty;
CPU_INT16U size;
OS_ERR err_os;
NET_ERR err_net;
DHCPc_ERR err;
/* --------------- INITIALIZE uC/DHCPc ---------------- */
err = DHCPc_Init(); /* See Note #1. */
if (err != DHCPc_ERR_NONE) {
return (DEF_FAIL);
}
/* ---------- CFG DHCPC PARAMETER REQUESTED ----------- */
req_param[0] = DHCP_OPT_DOMAIN_NAME_SERVER; /* Obtain DNS address. See Note #2 */
req_param_qty = 1u; /* 1 parameter requested. */
/* ----------- START DHCPC ON THE INTERFACE ----------- */
DHCPc_Start(if_nbr, req_param, req_param_qty, &err); /* See Note #3. */
if (err != DHCPc_ERR_NONE) {
return (DEF_FAIL);
}
/* ------- WAIT UNTIL THE PROCESS IS COMPLETED -------- */
done = DEF_NO;
while (done == DEF_NO) {
OSTimeDlyHMSM(0, 0, 0, 200, OS_OPT_TIME_DLY, &err_os); /* TODO change following OS API. See Note #4. */
status = DHCPc_ChkStatus(if_nbr, &err); /* Check DHCP status. See Note #5. */
switch (status) {
case DHCP_STATUS_CFG_IN_PROGRESS: /* See Note 5a. */
break;
case DHCP_STATUS_CFGD: /* See Note 5b. */
case DHCP_STATUS_CFGD_NO_TMR: /* See Note 5c. */
size = sizeof(NET_IPv4_ADDR); /* An address has been configured. */
DHCPc_GetOptVal( if_nbr, /* Get DNS address obtained by the DHCPc. See Note #6. */
DHCP_OPT_DOMAIN_NAME_SERVER,
(CPU_INT08U *)p_addr_dns,
&size,
&err);
/* Break intentionally omitted. */
case DHCP_STATUS_CFGD_LOCAL_LINK: /* See Note 5d. */
done = DEF_YES;
addr_ip_tbl_qty = sizeof(addr_tbl) / sizeof(NET_IPv4_ADDR);
(void)NetIPv4_GetAddrHost(if_nbr, /* See Note #7. Get current address configured. */
&addr_tbl[0],
&addr_ip_tbl_qty,
&err_net);
if (err_net != NET_IPv4_ERR_NONE) {
return (DEF_FAIL);
}
*p_addr_cfgd = addr_tbl[0];
break;
case DHCP_STATUS_FAIL: /* See Note #5e. No address has been configured. */
DHCPc_Stop(if_nbr, &err);
return (DEF_FAIL);
default:
break;
}
}
return (DEF_OK);
} |
Panel | ||
---|---|---|
| ||
|