55 */
66#include "usbd_core.h"
77#include "usbd_rndis.h"
8- #include "dhserver.h"
9- #include "dnserver.h"
108
119#ifndef CONFIG_USBDEV_RNDIS_USING_LWIP
1210#error "Please enable CONFIG_USBDEV_RNDIS_USING_LWIP for this demo"
@@ -178,55 +176,6 @@ static const uint8_t cdc_rndis_descriptor[] = {
178176
179177const uint8_t mac [6 ] = { 0xaa , 0xbb , 0xcc , 0xdd , 0xee , 0xff };
180178
181- /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
182- #define IP_ADDR0 (uint8_t)192
183- #define IP_ADDR1 (uint8_t)168
184- #define IP_ADDR2 (uint8_t)7
185- #define IP_ADDR3 (uint8_t)1
186-
187- /*NETMASK*/
188- #define NETMASK_ADDR0 (uint8_t)255
189- #define NETMASK_ADDR1 (uint8_t)255
190- #define NETMASK_ADDR2 (uint8_t)255
191- #define NETMASK_ADDR3 (uint8_t)0
192-
193- /*Gateway Address*/
194- #define GW_ADDR0 (uint8_t)0
195- #define GW_ADDR1 (uint8_t)0
196- #define GW_ADDR2 (uint8_t)0
197- #define GW_ADDR3 (uint8_t)0
198-
199- const ip_addr_t ipaddr = IPADDR4_INIT_BYTES (IP_ADDR0 , IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
200- const ip_addr_t netmask = IPADDR4_INIT_BYTES (NETMASK_ADDR0 , NETMASK_ADDR1 , NETMASK_ADDR2 , NETMASK_ADDR3 );
201- const ip_addr_t gateway = IPADDR4_INIT_BYTES (GW_ADDR0 , GW_ADDR1 , GW_ADDR2 , GW_ADDR3 );
202-
203- #define NUM_DHCP_ENTRY 3
204-
205- static dhcp_entry_t entries [NUM_DHCP_ENTRY ] = {
206- /* mac ip address subnet mask lease time */
207- { { 0 }, { 192 , 168 , 7 , 2 }, { 255 , 255 , 255 , 0 }, 24 * 60 * 60 },
208- { { 0 }, { 192 , 168 , 7 , 3 }, { 255 , 255 , 255 , 0 }, 24 * 60 * 60 },
209- { { 0 }, { 192 , 168 , 7 , 4 }, { 255 , 255 , 255 , 0 }, 24 * 60 * 60 }
210- };
211-
212- static dhcp_config_t dhcp_config = {
213- { 192 , 168 , 7 , 1 }, /* server address */
214- 67 , /* port */
215- { 192 , 168 , 7 , 1 }, /* dns server */
216- "cherry" , /* dns suffix */
217- NUM_DHCP_ENTRY , /* num entry */
218- entries /* entries */
219- };
220-
221- static bool dns_query_proc (const char * name , ip_addr_t * addr )
222- {
223- if (strcmp (name , "rndis.cherry" ) == 0 || strcmp (name , "www.rndis.cherry" ) == 0 ) {
224- addr -> addr = ipaddr .addr ;
225- return true;
226- }
227- return false;
228- }
229-
230179volatile bool rndis_tx_done = false;
231180
232181void usbd_rndis_data_send_done (uint32_t len )
@@ -235,9 +184,19 @@ void usbd_rndis_data_send_done(uint32_t len)
235184}
236185
237186#ifdef RT_USING_LWIP
187+
188+ #ifndef RT_LWIP_DHCP
189+ #error rndis must enable RT_LWIP_DHCP
190+ #endif
191+
192+ #ifndef LWIP_USING_DHCPD
193+ #error rndis must enable LWIP_USING_DHCPD
194+ #endif
195+
238196#include <rtthread.h>
239197#include <rtdevice.h>
240198#include <netif/ethernetif.h>
199+ #include <dhcp_server.h>
241200
242201struct eth_device rndis_dev ;
243202
@@ -291,15 +250,6 @@ void rndis_lwip_init(void)
291250 eth_device_init (& rndis_dev , "u0" );
292251
293252 eth_device_linkchange (& rndis_dev , RT_FALSE );
294-
295- while (!netif_is_up (rndis_dev .netif )) {
296- }
297-
298- while (dhserv_init (& dhcp_config )) {
299- }
300-
301- while (dnserv_init (IP_ADDR_ANY , 53 , dns_query_proc )) {
302- }
303253}
304254
305255void usbd_rndis_data_recv_done (uint32_t len )
@@ -313,11 +263,63 @@ void usbd_rndis_data_recv_done(uint32_t len)
313263#include "lwip/netif.h"
314264#include "lwip/pbuf.h"
315265
266+ #include "dhserver.h"
267+ #include "dnserver.h"
268+
269+ /*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
270+ #define IP_ADDR0 (uint8_t)192
271+ #define IP_ADDR1 (uint8_t)168
272+ #define IP_ADDR2 (uint8_t)7
273+ #define IP_ADDR3 (uint8_t)1
274+
275+ /*NETMASK*/
276+ #define NETMASK_ADDR0 (uint8_t)255
277+ #define NETMASK_ADDR1 (uint8_t)255
278+ #define NETMASK_ADDR2 (uint8_t)255
279+ #define NETMASK_ADDR3 (uint8_t)0
280+
281+ /*Gateway Address*/
282+ #define GW_ADDR0 (uint8_t)0
283+ #define GW_ADDR1 (uint8_t)0
284+ #define GW_ADDR2 (uint8_t)0
285+ #define GW_ADDR3 (uint8_t)0
286+
287+ const ip_addr_t ipaddr = IPADDR4_INIT_BYTES (IP_ADDR0 , IP_ADDR1 , IP_ADDR2 , IP_ADDR3 );
288+ const ip_addr_t netmask = IPADDR4_INIT_BYTES (NETMASK_ADDR0 , NETMASK_ADDR1 , NETMASK_ADDR2 , NETMASK_ADDR3 );
289+ const ip_addr_t gateway = IPADDR4_INIT_BYTES (GW_ADDR0 , GW_ADDR1 , GW_ADDR2 , GW_ADDR3 );
290+
291+ #define NUM_DHCP_ENTRY 3
292+
293+ static dhcp_entry_t entries [NUM_DHCP_ENTRY ] = {
294+ /* mac ip address subnet mask lease time */
295+ { { 0 }, { 192 , 168 , 7 , 2 }, { 255 , 255 , 255 , 0 }, 24 * 60 * 60 },
296+ { { 0 }, { 192 , 168 , 7 , 3 }, { 255 , 255 , 255 , 0 }, 24 * 60 * 60 },
297+ { { 0 }, { 192 , 168 , 7 , 4 }, { 255 , 255 , 255 , 0 }, 24 * 60 * 60 }
298+ };
299+
300+ static dhcp_config_t dhcp_config = {
301+ { 192 , 168 , 7 , 1 }, /* server address */
302+ 67 , /* port */
303+ { 192 , 168 , 7 , 1 }, /* dns server */
304+ "cherry" , /* dns suffix */
305+ NUM_DHCP_ENTRY , /* num entry */
306+ entries /* entries */
307+ };
308+
309+ static bool dns_query_proc (const char * name , ip_addr_t * addr )
310+ {
311+ if (strcmp (name , "rndis.cherry" ) == 0 || strcmp (name , "www.rndis.cherry" ) == 0 ) {
312+ addr -> addr = ipaddr .addr ;
313+ return true;
314+ }
315+ return false;
316+ }
317+
316318static struct netif rndis_netif ; //network interface
317319
318320/* Network interface name */
319- #define IFNAME0 'E'
320- #define IFNAME1 'X'
321+ #define IFNAME0 'E'
322+ #define IFNAME1 'X'
321323
322324err_t linkoutput_fn (struct netif * netif , struct pbuf * p )
323325{
@@ -388,7 +390,6 @@ void rndis_lwip_init(void)
388390
389391void usbd_rndis_data_recv_done (uint32_t len )
390392{
391-
392393}
393394
394395void rndis_input_poll (void )
@@ -413,6 +414,7 @@ static void usbd_event_handler(uint8_t busid, uint8_t event)
413414 case USBD_EVENT_CONFIGURED :
414415#ifdef RT_USING_LWIP
415416 eth_device_linkchange (& rndis_dev , RT_TRUE );
417+ dhcpd_start ("u0" );
416418#endif
417419 break ;
418420 case USBD_EVENT_SET_REMOTE_WAKEUP :
0 commit comments