ETHNET DHCP的两种方式
DHCP API:
nx_dhcp_create
nx_dhcp_start
nx_dhcp_stop
nx_dhcp_delete
nx_ip_address_get //客户端IP获取
nx_dhcp_server_address_get //DHCP SERVER
nx_icmp_ping //ping命令
nx_dhcp_reinitialize
nx_dns_host_by_name_get //通过域名获取IP
1、注意复位PIN:

2、SSP设置如下:

3、测试:
#include "dhcp_thread.h" /* Define the function to call for running a DHCP session. */
UINT run_dhcp_client_session(NX_DHCP *client_ptr, NX_IP *ip_ptr); /* If define, debug output sent to Renesas Virtual Console */
#define SEMI_HOSTING /* Define the wait limit for getting assigned an IP address.
* NX_IP_PERIODIC_RATE is defined internally in NetX for converting
* seconds to timer ticks. This timeout is defined for 2 seconds.
*/
#define WAIT_TO_BE_BOUND (10*NX_IP_PERIODIC_RATE) /* Define error status returns */
#define DHCP_RUN_ERRORS 0x1001
#define DHCP_START_ERROR 0x1002 /* If semi hosting is defined, define the debug output
* method using printf.
*/
#ifdef SEMI_HOSTING
#include "stdio.h"
#ifdef __GNUC__
extern void initialise_monitor_handles(void);
#endif
#endif /* Define some global variables. */
static UINT error_counter = ;
static UINT is_bound = NX_FALSE; /* Declare the DHCP Client state change callback. */
static VOID my_notify_callback(NX_DHCP *dhcp_ptr, UCHAR new_state); char * xp_str_ip(char * x, uint32_t ip)
{
sprintf(x, "%u.%u.%u.%u",
(uint8_t)(ip>> & 0xFF),
(uint8_t)(ip>> & 0xFF),
(uint8_t)(ip>> & 0xFF),
(uint8_t)(ip>> & 0xFF)); return x;
} /**
* This function runs a DHCP Client session.
* client_ptr; pointer to an NX_DHCP instance, an already created DHCP Client instance
* ip_ptr; pointer to an NX_IP instance, an already created IP instance
* If successful return NX_SUCCESS (0x0); else return DHCP_RUN_ERRORS error status.
*/
UINT run_dhcp_client_session(NX_DHCP *client_ptr, NX_IP *ip_ptr)
{ UINT status;
NX_PACKET *my_packet;
ULONG server_address;
ULONG client_address;
ULONG network_mask;
UCHAR buffer[];
UINT buffer_size = ;
ULONG *dns_server_ptr;
UINT wait = ;
UINT wait_limit;
char ip_string[] = {}; /* Initialize the debug output utility. */
#ifdef SEMI_HOSTING
#ifdef __GNUC__
initialise_monitor_handles();
#endif
#endif /* Register a DHCP state change callback function. */
status = nx_dhcp_state_change_notify(client_ptr, my_notify_callback); //方式一:阻塞的方式等待DHCP if (status)
error_counter++; /* Now we're ready to start the DHCP Client. */
status = nx_dhcp_start(client_ptr); /* Check the status. */
if (status)
{
error_counter++; #ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("Aborting DHCP Client. Failed DHCP start 0x%x\n", status);
}
#endif
/* Internal DHCP error or NetX internal error. We cannot continue this demo test. */
nx_dhcp_delete(client_ptr); return DHCP_START_ERROR;
} #ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("DHCP Client is running\n");
}
#endif
/* Wait for the flag that the Client is Bound is set. */
wait = ;
wait_limit = WAIT_TO_BE_BOUND; while(wait < wait_limit)
{ /* If the is_bound flag is set, we have a valid IP address */
if (is_bound == NX_TRUE)
break; /* Not bound yet. Let other application threads run. */
tx_thread_sleep(); /* Update how long we've waited */
wait += ;
} /* Check if Client is bound to an IP address. */
if (is_bound)
{
#ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("DHCP Client is assigned an IP address lease.\n");
}
#endif /* It is. Get the client IP address from this NetX service. */
status = nx_ip_address_get(ip_ptr, &client_address, &network_mask); /* Check for errors. */
if (status)
error_counter++;
else
{
#ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("DHCP Client address is 0x%x. \n",(unsigned int)client_address);
xp_str_ip(ip_string, client_address);
printf("DHCP Client address is:%s\n",ip_string);
}
#endif
} /* Get the DHCP Server IP address. */
status = nx_dhcp_server_address_get(client_ptr, &server_address); /* Check for errors. */
if (status)
{
error_counter++;
}
else
{
/* Check that the device is able to send and receive packets with this IP address. */
status = nx_icmp_ping(ip_ptr, server_address, "Hello World", sizeof("Hello World"), &my_packet, * NX_IP_PERIODIC_RATE); #ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("DHCP Server IP address is 0%x.\n", (unsigned int)server_address);
xp_str_ip(ip_string, server_address);
printf("DHCP Server address is:%s\n",ip_string);
}
#endif /* Check status. */
if (status)
/* No valid ICMP packet received (no packet to release). Update the error counter. */
error_counter++;
else
{
#ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("Successfully pinged Server.\n");
}
#endif
/* Release the echo response packet when we are done with it. */
nx_packet_release(my_packet);
}
}
} /* Stop the DHCP Client. The application can still send and receive network packets. */
status = nx_dhcp_stop(client_ptr); if (status)
error_counter++; /* Prepare the DHCP Client to restart. We can still send and receive
* packets except broadcast packets, but with a source IP address
* of zero, is not very useful except for DHCP. */
status = nx_dhcp_reinitialize(client_ptr); if (status)
error_counter++; #ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("DHCP Client is reinitializing...\n");
}
#endif /* Some time later.... */
tx_thread_sleep(); /* Clear our previous DHCP session flag. */
is_bound = NX_FALSE; /* Restart the DHCP Client thread task. */
status = nx_dhcp_start(client_ptr); #ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("DHCP Client is restarted...\n");
}
#endif /* Check status. */
if (status)
{
/* Update the error count. */
error_counter++; #ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("Aborting DHCP Client. Failed 2nd DHCP start 0x%x\n", status);
}
#endif
/* Internal DHCP error or NetX internal error. We cannot continue this demo test. */
nx_dhcp_delete(client_ptr);
}
else
{
/* This time we'll poll the IP instance directly for a valid IP address. */
wait = ;
do
{
UINT actual_status; /* Check for address resolution. */
status = nx_ip_status_check(ip_ptr, NX_IP_ADDRESS_RESOLVED, (ULONG *) &actual_status, NX_IP_PERIODIC_RATE); //方式二:查询方式等待DHCP /* Check status. */
if (status)
{
/* wait a bit. */
tx_thread_sleep(NX_IP_PERIODIC_RATE); wait += NX_IP_PERIODIC_RATE;
if (wait >= wait_limit)
{
break;
}
} } while (status != NX_SUCCESS); /* Check if we have a valid address. */
if (status == NX_SUCCESS)
{ #ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("DHCP Client is assigned IP address lease for a second time.\n");
}
#endif
/* We do. This time, query the DHCP Client for the DNS Server address. */
status = nx_dhcp_user_option_retrieve(client_ptr, NX_DHCP_OPTION_DNS_SVR, buffer, &buffer_size); /* Check status. */
if (status)
error_counter++;
else
{
dns_server_ptr = (ULONG *)(buffer); /* Send a ping request to the DNS server. */
status = nx_icmp_ping(ip_ptr, *dns_server_ptr, "Hello DNS Server", sizeof("Hello DNS Server"), &my_packet, * NX_IP_PERIODIC_RATE); /* No valid ICMP packet received (no packet to release). Update the error counter. */
if (status)
error_counter++;
else
{
#ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("Successfully pinged Server.\n");
}
#endif
/* Release the echo response packet when we are done with it. */
nx_packet_release(my_packet);
}
} /* We're done with the DHCP Client. */ /* Release the IP address back to the Server. This application should not
send or receive packets with this IP address now. */
status = nx_dhcp_release(client_ptr);
if (status)
error_counter++;
else
{
#ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("Released IP address back to Server.\n");
}
#endif
}
}
#ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("Stopping the DHCP Client.\n");
}
#endif /* Stop the DHCP Client and unbind the DHCP UDP socket.*/
status = nx_dhcp_stop(client_ptr); if (status)
error_counter++; } /* All done. Delete the Client and release resources to NetX and ThreadX. */
status = nx_dhcp_delete(client_ptr); if (status)
error_counter++; #ifdef SEMI_HOSTING
if (CoreDebug->DHCSR & CoreDebug_DHCSR_C_DEBUGEN_Msk)
{
/* Debugger is connected */
printf("DHCP Client demo has completed with %d errors.\n", error_counter);
}
#endif /* Return a status value based on any errors encountered. */
if (error_counter == )
{
return NX_SUCCESS;
}
else
{ return DHCP_RUN_ERRORS;
}
} /**
* This function defines a user callback for DHCP Client to notify
* when there is a DHCP state change.
* NX_DHCP *client_ptr; previously created DHCP Client instance
* UCHAR state; 2 byte numeric representation of DHCP state
* void
*
* In this callback, we only check if the DHCP
* Client has changed to the bound state (has a valid IP
* address) and we set a flag for the application to check.
*/
VOID my_notify_callback(NX_DHCP *dhcp_ptr, UCHAR state)
{ UINT new_state = (UINT)state; NX_PARAMETER_NOT_USED(dhcp_ptr); /* Check if we have transitioned to the bound state
(have a valid IP address). */
if (new_state == NX_DHCP_STATE_BOUND)
{
/* We have. Set the flag for the application. */
is_bound = NX_TRUE;
}
} /* Define the application thread. */
void dhcp_thread_entry(void)
{ UINT error_counter = ;
UINT status; /* Wait for the IP stack and network hardware
* to get initialized.
*/
tx_thread_sleep( *NX_IP_PERIODIC_RATE); /* Start and run a brief DHCP Client session. */
status = run_dhcp_client_session(&g_dhcp_client0, &g_ip0); /* Check for successful result. */
if (status)
error_counter++;
}
4、调试信息如下:
DHCP Client is running
DHCP Client is assigned an IP address lease.
DHCP Client address is 0xc0a81f7d.
DHCP Client address is:192.168.31.125
DHCP Server IP address is 0c0a81f01.
DHCP Server address is:192.168.31.1
Successfully pinged Server.
DHCP Client is reinitializing...
DHCP Client is restarted...
DHCP Client is assigned IP address lease for a second time.
Successfully pinged Server.
Released IP address back to Server.
Stopping the DHCP Client.
DHCP Client demo has completed with errors.
ETHNET DHCP的两种方式的更多相关文章
- 自学Linux Shell9.2-基于Red Hat系统工具包存在两种方式之一:RPM包
点击返回 自学Linux命令行与Shell脚本之路 9.2-基于Red Hat系统工具包存在两种方式之一:RPM包 本节主要介绍基于Red Had的系统(测试系统centos) 1. 工具包存在两种方 ...
- 两种方式测试 GNS3 环境
GNS3已经部署好了,怎么测试环境呢?两种方式,一是使用自带的VPC连接交换机互联互通,二是配合VMware连接GNS3中的交换机互联互通. 自带 VPC 测试 使用两台VPC与一台二层交换机相连,测 ...
- Struts2实现ajax的两种方式
基于Struts2框架下实现Ajax有两种方式,第一种是原声的方式,另外一种是struts2自带的一个插件. js部分调用方式是一样的: JS代码: function testAjax() { var ...
- CSharpGL(18)分别处理glDrawArrays()和glDrawElements()两种方式下的拾取(ColorCodedPicking)
CSharpGL(18)分别处理glDrawArrays()和glDrawElements()两种方式下的拾取(ColorCodedPicking) 我在(Modern OpenGL用Shader拾取 ...
- 两种方式实现java生成Excel
Web应用中难免会遇到需要将数据导出并生成excel文件的需求.同样,对于本博客中的总结,也是建立在为了完成这样的一个需求,才开始去了解其实现形式,并且顺利完成需求的开发,先将实现过程总结于此.本博文 ...
- Android ScrollView监听滑动到顶部和底部的两种方式(你可能不知道的细节)
Android ScrollView监听滑动到顶部和底部,虽然网上很多资料都有说,但是不全,而且有些细节没说清楚 使用场景: 1. 做一些复杂动画的时候,需要动态判断当前的ScrollView是否滚动 ...
- 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入
在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...
- (转)SqlServer 数据库同步的两种方式 (发布、订阅),主从数据库之间的同步
最近在琢磨主从数据库之间的同步,公司正好也需要,在园子里找了一下,看到这篇博文比较详细,比较简单,本人亲自按步骤来过,现在分享给大家. 在这里要提醒大家的是(为了更好的理解,以下是本人自己理解,如有错 ...
- 《连载 | 物联网框架ServerSuperIO教程》- 10.持续传输大块数据流的两种方式(如:文件)
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
随机推荐
- 前段时间说了AssetBundle打包,先设置AssetLabels,再执行打包,但是这样有个弊端就是所有设置了AssetLabels的资源都会打包,这次说说不设置AssetLabels,该如何打包AssetBundle
BuildPipeline.BuildAssetBundles() 这个函数,有多个重载,一个不用AssetBundleBuild数组,一个需要,如果设置了AssetLabels,那么这时候是不需要的 ...
- python模块cgihttpserver启动
cgi是web服务器运行web应用的一种机制,web服务器通过执行cgi脚本,然后将该程序的标准输出作为http响应的一部分 CGIHTTPServer是python标准模块的web服务器,它可以运行 ...
- 修改jpivot源码实现分页
使用jpivot过程中,如果查询到的结果行数超过一个阈值,后面的显示就会丢失,这时需要分页显示. 假设应用中组装的MDX语句已经含有NON EMPTY,把空行直接过滤掉了. 这时需要修改的jpivot ...
- 基于“基于dockerhub的jetty镜像的ossfs镜像”部署war包,遇到的文件夹读写权限被限制的问题解决方案
前提: “基于dockerhub的jetty镜像的ossfs镜像” 已经搭建好了. 部署准备: 1.本地打包:war包-->idea工具 mvn 打包. 2.本地sh脚本:compile_vps ...
- linux jdk安装。
我使用的centos 7: 安转sun公司的jdk要先检查系统中是否安装jdk,一般来说Centos系统会默认会安装OpenJDK,但是openJDK部分内容 与SUN JDK不兼容,因此下面进行重新 ...
- python爬虫-url
特此声明: 以下内容来源于博主:http://blog.csdn.net/pleasecallmewhy http://cuiq ...
- Linux软件安装常用方法
1.软件安装卸载,分几种情况: A:RPM包,这种软件包就像windows的EXE安装文件一样,各种文件已经编译好,并打了包,哪个文件该放到哪个文件夹,都指定好了,安装非常方便,在图形界面里你只需要双 ...
- Codeforces 893E Counting Arrays:dp + 线性筛 + 分解质因数 + 组合数结论
题目链接:http://codeforces.com/problemset/problem/893/E 题意: 共q组数据(q <= 10^5),每组数据给定x,y(x,y <= 10^6 ...
- v2.0 组件通信的总结
在vue.js现在比较流行,层出不穷的js框架越来越强调数据绑定,组件化开发. 正在给公司做一个管理后台,基本思路是编写几个通用组件,采用单页面应用的形式完成: 结构大致如下: mainVue lef ...
- The tag handler class for "home.jsp" (org.apache.taglibs.standard.tag.rt.core.ForEachTag) was not found on the Java Build Path
web.xml中 listener,filter,servlet需按顺序. <listener> <listener-class>listener.VisitCountList ...