DHCP Allows selecting the DHCP server's network interface (#426)
This commit is contained in:
		
							parent
							
								
									260dd1bde8
								
							
						
					
					
						commit
						cb4ea206fa
					
				
					 4 changed files with 64 additions and 67 deletions
				
			
		|  | @ -1,11 +1,11 @@ | |||
| /* Very basic example showing usage of access point mode and the DHCP server.
 | ||||
| 
 | ||||
|    The ESP in the example runs a telnet server on 172.16.0.1 (port 23) that | ||||
|    outputs some status information if you connect to it, then closes | ||||
|    the connection. | ||||
| 
 | ||||
|    This example code is in the public domain. | ||||
| */ | ||||
| /**
 | ||||
|  * Very basic example showing usage of access point mode and the DHCP server. | ||||
|  * The ESP in the example runs a telnet server on 172.16.0.1 (port 23) that | ||||
|  * outputs some status information if you connect to it, then closes | ||||
|  * the connection. | ||||
|  * | ||||
|  * This example code is in the public domain. | ||||
|  */ | ||||
| #include <string.h> | ||||
| 
 | ||||
| #include <espressif/esp_common.h> | ||||
|  | @ -36,61 +36,55 @@ void user_init(void) | |||
|     IP4_ADDR(&ap_ip.netmask, 255, 255, 0, 0); | ||||
|     sdk_wifi_set_ip_info(1, &ap_ip); | ||||
| 
 | ||||
|     struct sdk_softap_config ap_config = { | ||||
|         .ssid = AP_SSID, | ||||
|         .ssid_hidden = 0, | ||||
|         .channel = 3, | ||||
|         .ssid_len = strlen(AP_SSID), | ||||
|         .authmode = AUTH_WPA_WPA2_PSK, | ||||
|         .password = AP_PSK, | ||||
|         .max_connection = 3, | ||||
|         .beacon_interval = 100, | ||||
|     }; | ||||
|     struct sdk_softap_config ap_config = { .ssid = AP_SSID, .ssid_hidden = 0, .channel = 3, .ssid_len = strlen(AP_SSID), .authmode = | ||||
|             AUTH_WPA_WPA2_PSK, .password = AP_PSK, .max_connection = 3, .beacon_interval = 100, }; | ||||
|     sdk_wifi_softap_set_config(&ap_config); | ||||
| 
 | ||||
|     ip4_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); | ||||
| } | ||||
| 
 | ||||
| /* Telnet task listens on port 23, returns some status information and then closes
 | ||||
|    the connection if you connect to it. | ||||
| */ | ||||
|  the connection if you connect to it. | ||||
|  */ | ||||
| static void telnetTask(void *pvParameters) | ||||
| { | ||||
|   struct netconn *nc = netconn_new (NETCONN_TCP); | ||||
|   if(!nc) { | ||||
|     printf("Status monitor: Failed to allocate socket.\r\n"); | ||||
|     return; | ||||
|   } | ||||
|   netconn_bind(nc, IP_ANY_TYPE, TELNET_PORT); | ||||
|   netconn_listen(nc); | ||||
|     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); | ||||
| 
 | ||||
|   while(1) { | ||||
|     struct netconn *client = NULL; | ||||
|     err_t err = netconn_accept(nc, &client); | ||||
| 
 | ||||
|     if (err != ERR_OK) { | ||||
|       if(client) | ||||
| 	netconn_delete(client); | ||||
|       continue; | ||||
|     struct netconn *nc = netconn_new(NETCONN_TCP); | ||||
|     if (!nc) | ||||
|     { | ||||
|         printf("Status monitor: Failed to allocate socket.\r\n"); | ||||
|         return; | ||||
|     } | ||||
|     netconn_bind(nc, IP_ANY_TYPE, TELNET_PORT); | ||||
|     netconn_listen(nc); | ||||
| 
 | ||||
|     ip_addr_t client_addr; | ||||
|     uint16_t port_ignore; | ||||
|     netconn_peer(client, &client_addr, &port_ignore); | ||||
|     while (1) | ||||
|     { | ||||
|         struct netconn *client = NULL; | ||||
|         err_t err = netconn_accept(nc, &client); | ||||
| 
 | ||||
|     char buf[80]; | ||||
|     snprintf(buf, sizeof(buf), "Uptime %d seconds\r\n", | ||||
| 	     xTaskGetTickCount()*portTICK_PERIOD_MS/1000); | ||||
|     netconn_write(client, buf, strlen(buf), NETCONN_COPY); | ||||
|     snprintf(buf, sizeof(buf), "Free heap %d bytes\r\n", (int)xPortGetFreeHeapSize()); | ||||
|     netconn_write(client, buf, strlen(buf), NETCONN_COPY); | ||||
|     char abuf[40]; | ||||
|     snprintf(buf, sizeof(buf), "Your address is %s\r\n\r\n", ipaddr_ntoa_r(&client_addr, abuf, sizeof(abuf))); | ||||
|     netconn_write(client, buf, strlen(buf), NETCONN_COPY); | ||||
|     netconn_delete(client); | ||||
|   } | ||||
|         if (err != ERR_OK) | ||||
|         { | ||||
|             if (client) | ||||
|                 netconn_delete(client); | ||||
|             continue; | ||||
|         } | ||||
| 
 | ||||
|         ip_addr_t client_addr; | ||||
|         uint16_t port_ignore; | ||||
|         netconn_peer(client, &client_addr, &port_ignore); | ||||
| 
 | ||||
|         char buf[80]; | ||||
|         snprintf(buf, sizeof(buf), "Uptime %d seconds\r\n", xTaskGetTickCount() * portTICK_PERIOD_MS / 1000); | ||||
|         netconn_write(client, buf, strlen(buf), NETCONN_COPY); | ||||
|         snprintf(buf, sizeof(buf), "Free heap %d bytes\r\n", (int) xPortGetFreeHeapSize()); | ||||
|         netconn_write(client, buf, strlen(buf), NETCONN_COPY); | ||||
|         char abuf[40]; | ||||
|         snprintf(buf, sizeof(buf), "Your address is %s\r\n\r\n", ipaddr_ntoa_r(&client_addr, abuf, sizeof(abuf))); | ||||
|         netconn_write(client, buf, strlen(buf), NETCONN_COPY); | ||||
|         netconn_delete(client); | ||||
|     } | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue