diff --git a/examples/access_point/access_point.c b/examples/access_point/access_point.c index f8fcd50..31996fa 100644 --- a/examples/access_point/access_point.c +++ b/examples/access_point/access_point.c @@ -48,10 +48,6 @@ void user_init(void) }; sdk_wifi_softap_set_config(&ap_config); - ip_addr_t first_client_ip; - IP4_ADDR(&first_client_ip, 172, 16, 0, 2); - dhcpserver_start(&first_client_ip, 4); - xTaskCreate(telnetTask, "telnetTask", 512, NULL, 2, NULL); } @@ -60,6 +56,10 @@ void user_init(void) */ static void telnetTask(void *pvParameters) { + ip_addr_t first_client_ip; + IP4_ADDR(&first_client_ip, 172, 16, 0, 2); + dhcpserver_start(sdk_system_get_netif(SOFTAP_IF), &first_client_ip, 4); + struct netconn *nc = netconn_new (NETCONN_TCP); if(!nc) { printf("Status monitor: Failed to allocate socket.\r\n"); diff --git a/extras/dhcpserver/dhcpserver.c b/extras/dhcpserver/dhcpserver.c index d87daab..49557b8 100644 --- a/extras/dhcpserver/dhcpserver.c +++ b/extras/dhcpserver/dhcpserver.c @@ -77,8 +77,12 @@ inline static void sprintf_ipaddr(const ip_addr_t *addr, char *dest) ip4_addr2(addr), ip4_addr3(addr), ip4_addr4(addr)); } -void dhcpserver_start(const ip_addr_t *first_client_addr, uint8_t max_leases) +void dhcpserver_start(struct netif *server_if, const ip_addr_t *first_client_addr, uint8_t max_leases) { + if(!server_if){ + printf("DHCP Server Error: server_if is NULL.\r\n"); + return; + } /* Stop any existing running dhcpserver */ if(dhcpserver_task_handle) dhcpserver_stop(); @@ -89,7 +93,7 @@ void dhcpserver_start(const ip_addr_t *first_client_addr, uint8_t max_leases) // state->server_if is assigned once the task is running - see comment in dhcpserver_task() ip_addr_copy(state->first_client_addr, *first_client_addr); - xTaskCreate(dhcpserver_task, "DHCPServer", 768, NULL, 8, &dhcpserver_task_handle); + xTaskCreate(dhcpserver_task, "DHCPServer", 768, server_if, 8, &dhcpserver_task_handle); } void dhcpserver_stop(void) @@ -104,7 +108,7 @@ void dhcpserver_stop(void) static void dhcpserver_task(void *pxParameter) { /* netif_list isn't assigned until after user_init completes, which is why we do it inside the task */ - state->server_if = netif_list; /* TODO: Make this configurable */ + state->server_if = pxParameter; state->nc = netconn_new (NETCONN_UDP); if(!state->nc) { diff --git a/extras/dhcpserver/include/dhcpserver.h b/extras/dhcpserver/include/dhcpserver.h index 95a59b8..fa46e83 100644 --- a/extras/dhcpserver/include/dhcpserver.h +++ b/extras/dhcpserver/include/dhcpserver.h @@ -25,8 +25,10 @@ extern "C" { first_client_addr is the IP address of the first lease to be handed to a client. Subsequent lease addresses are calculated by incrementing the final octet of the IPv4 address, up to max_leases. + + The server will wait for requests on server_if interface. */ -void dhcpserver_start(const ip_addr_t *first_client_addr, uint8_t max_leases); +void dhcpserver_start(struct netif *server_if, const ip_addr_t *first_client_addr, uint8_t max_leases); void dhcpserver_get_lease(const ip_addr_t *first_client_addr, uint8_t max_leases); diff --git a/tests/cases/04_wifi_basic.c b/tests/cases/04_wifi_basic.c index 129ddd0..9fa8be2 100644 --- a/tests/cases/04_wifi_basic.c +++ b/tests/cases/04_wifi_basic.c @@ -40,6 +40,11 @@ DEFINE_TESTCASE(04_wifi_basic, DUAL) static void server_task(void *pvParameters) { + + ip_addr_t first_client_ip; + IP4_ADDR(&first_client_ip, 172, 16, 0, 2); + dhcpserver_start(sdk_system_get_netif(SOFTAP_IF), &first_client_ip, 4); + char buf[BUF_SIZE]; struct netconn *nc = netconn_new(NETCONN_TCP); TEST_ASSERT_TRUE_MESSAGE(nc != 0, "Failed to allocate socket"); @@ -103,10 +108,6 @@ static void a_04_wifi_basic(void) }; sdk_wifi_softap_set_config(&ap_config); - ip_addr_t first_client_ip; - IP4_ADDR(&first_client_ip, 172, 16, 0, 2); - dhcpserver_start(&first_client_ip, 4); - xTaskCreate(server_task, "setver_task", 1024, NULL, 2, NULL); }