Skip to content

Commit d122f2d

Browse files
committed
update(demo): include dnserver & dhserver config for rndis & ecm
Signed-off-by: sakumisu <[email protected]>
1 parent d012974 commit d122f2d

File tree

2 files changed

+75
-24
lines changed

2 files changed

+75
-24
lines changed

demo/cdc_ecm_template.c

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
*/
66
#include "usbd_core.h"
77
#include "usbd_cdc_ecm.h"
8+
#include "dhserver.h"
9+
#include "dnserver.h"
10+
#include "netif/etharp.h"
11+
#include "lwip/init.h"
12+
#include "lwip/netif.h"
13+
#include "lwip/pbuf.h"
814

915
#ifndef CONFIG_USBDEV_CDC_ECM_USING_LWIP
1016
#error "Please enable CONFIG_USBDEV_CDC_ECM_USING_LWIP for this demo"
@@ -203,7 +209,7 @@ const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
203209
/*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
204210
#define IP_ADDR0 (uint8_t)192
205211
#define IP_ADDR1 (uint8_t)168
206-
#define IP_ADDR2 (uint8_t)123
212+
#define IP_ADDR2 (uint8_t)7
207213
#define IP_ADDR3 (uint8_t)100
208214

209215
/*NETMASK*/
@@ -215,18 +221,40 @@ const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
215221
/*Gateway Address*/
216222
#define GW_ADDR0 (uint8_t)192
217223
#define GW_ADDR1 (uint8_t)168
218-
#define GW_ADDR2 (uint8_t)123
224+
#define GW_ADDR2 (uint8_t)7
219225
#define GW_ADDR3 (uint8_t)1
220226

221-
#include "netif/etharp.h"
222-
#include "lwip/init.h"
223-
#include "lwip/netif.h"
224-
#include "lwip/pbuf.h"
225-
226227
const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
227228
const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
228229
const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
229230

231+
#define NUM_DHCP_ENTRY 3
232+
233+
static dhcp_entry_t entries[NUM_DHCP_ENTRY] = {
234+
/* mac ip address subnet mask lease time */
235+
{ { 0 }, { 192, 168, 7, 2 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
236+
{ { 0 }, { 192, 168, 7, 3 }, { 255, 255, 255, 0 }, 24 * 60 * 60 },
237+
{ { 0 }, { 192, 168, 7, 4 }, { 255, 255, 255, 0 }, 24 * 60 * 60 }
238+
};
239+
240+
static dhcp_config_t dhcp_config = {
241+
{ 192, 168, 7, 1 }, /* server address */
242+
67, /* port */
243+
{ 192, 168, 7, 1 }, /* dns server */
244+
"cherry", /* dns suffix */
245+
NUM_DHCP_ENTRY, /* num entry */
246+
entries /* entries */
247+
};
248+
249+
static bool dns_query_proc(const char *name, ip_addr_t *addr)
250+
{
251+
if (strcmp(name, "rndis.cherry") == 0 || strcmp(name, "www.rndis.cherry") == 0) {
252+
addr->addr = *(uint32_t *)ipaddr;
253+
return true;
254+
}
255+
return false;
256+
}
257+
230258
static struct netif cdc_ecm_netif; //network interface
231259

232260
/* Network interface name */
@@ -292,7 +320,7 @@ void cdc_ecm_lwip_init(void)
292320

293321
while (dhserv_init(&dhcp_config)) {}
294322

295-
while (dnserv_init(&ipaddr, PORT_DNS, dns_query_proc)) {}
323+
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc)) {}
296324
}
297325

298326
void usbd_cdc_ecm_data_recv_done(uint32_t len)

demo/cdc_rndis_template.c

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
#include "usbd_core.h"
77
#include "usbd_rndis.h"
8+
#include "dhserver.h"
9+
#include "dnserver.h"
810

911
#ifndef CONFIG_USBDEV_RNDIS_USING_LWIP
1012
#error "Please enable CONFIG_USBDEV_RNDIS_USING_LWIP for this demo"
@@ -175,10 +177,11 @@ static const uint8_t cdc_rndis_descriptor[] = {
175177
#endif
176178

177179
const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
180+
178181
/*Static IP ADDRESS: IP_ADDR0.IP_ADDR1.IP_ADDR2.IP_ADDR3 */
179182
#define IP_ADDR0 (uint8_t)192
180183
#define IP_ADDR1 (uint8_t)168
181-
#define IP_ADDR2 (uint8_t)123
184+
#define IP_ADDR2 (uint8_t)7
182185
#define IP_ADDR3 (uint8_t)100
183186

184187
/*NETMASK*/
@@ -190,18 +193,45 @@ const uint8_t mac[6] = { 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff };
190193
/*Gateway Address*/
191194
#define GW_ADDR0 (uint8_t)192
192195
#define GW_ADDR1 (uint8_t)168
193-
#define GW_ADDR2 (uint8_t)123
196+
#define GW_ADDR2 (uint8_t)7
194197
#define GW_ADDR3 (uint8_t)1
195198

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 = *(uint32_t *)ipaddr;
225+
return true;
226+
}
227+
return false;
228+
}
229+
196230
#ifdef RT_USING_LWIP
197231
#include <rtthread.h>
198232
#include <rtdevice.h>
199233
#include <netif/ethernetif.h>
200234

201-
const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
202-
const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
203-
const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
204-
205235
struct eth_device rndis_dev;
206236

207237
static rt_err_t rt_usbd_rndis_control(rt_device_t dev, int cmd, void *args)
@@ -243,7 +273,7 @@ void usbd_rndis_data_recv_done(uint32_t len)
243273
eth_device_ready(&rndis_dev);
244274
}
245275

246-
void rt_usbd_rndis_init(void)
276+
void rndis_lwip_init(void)
247277
{
248278
rndis_dev.parent.control = rt_usbd_rndis_control;
249279
rndis_dev.eth_rx = rt_usbd_rndis_eth_rx;
@@ -259,10 +289,6 @@ void rt_usbd_rndis_init(void)
259289
#include "lwip/netif.h"
260290
#include "lwip/pbuf.h"
261291

262-
const ip_addr_t ipaddr = IPADDR4_INIT_BYTES(IP_ADDR0, IP_ADDR1, IP_ADDR2, IP_ADDR3);
263-
const ip_addr_t netmask = IPADDR4_INIT_BYTES(NETMASK_ADDR0, NETMASK_ADDR1, NETMASK_ADDR2, NETMASK_ADDR3);
264-
const ip_addr_t gateway = IPADDR4_INIT_BYTES(GW_ADDR0, GW_ADDR1, GW_ADDR2, GW_ADDR3);
265-
266292
static struct netif rndis_netif; //network interface
267293

268294
/* Network interface name */
@@ -328,7 +354,7 @@ void rndis_lwip_init(void)
328354

329355
while (dhserv_init(&dhcp_config)) {}
330356

331-
while (dnserv_init(&ipaddr, PORT_DNS, dns_query_proc)) {}
357+
while (dnserv_init(IP_ADDR_ANY, 53, dns_query_proc)) {}
332358
}
333359

334360
void usbd_rndis_data_recv_done(uint32_t len)
@@ -374,11 +400,8 @@ struct usbd_interface intf1;
374400

375401
void cdc_rndis_init(uint8_t busid, uintptr_t reg_base)
376402
{
377-
#ifdef RT_USING_LWIP
378-
rt_usbd_rndis_init();
379-
#else
380403
rndis_lwip_init();
381-
#endif
404+
382405
#ifdef CONFIG_USBDEV_ADVANCE_DESC
383406
usbd_desc_register(busid, &cdc_rndis_descriptor);
384407
#else

0 commit comments

Comments
 (0)