mirror of
https://github.com/espressif/esp-idf.git
synced 2024-10-05 20:47:46 -04:00
tcpip_adapter: code clean up
This commit is contained in:
parent
62c891735b
commit
7d0c139fdc
@ -76,18 +76,18 @@ typedef enum {
|
||||
|
||||
/*op*/
|
||||
typedef enum{
|
||||
TCPIP_ADAPTER_OP_START = 0,
|
||||
TCPIP_ADAPTER_OP_SET,
|
||||
TCPIP_ADAPTER_OP_GET,
|
||||
TCPIP_ADAPTER_OP_MAX
|
||||
}tcpip_adapter_option_mode;
|
||||
TCPIP_ADAPTER_OP_START = 0,
|
||||
TCPIP_ADAPTER_OP_SET,
|
||||
TCPIP_ADAPTER_OP_GET,
|
||||
TCPIP_ADAPTER_OP_MAX
|
||||
} tcpip_adapter_option_mode;
|
||||
|
||||
typedef enum{
|
||||
TCPIP_ADAPTER_ROUTER_SOLICITATION_ADDRESS = 32,
|
||||
TCPIP_ADAPTER_REQUESTED_IP_ADDRESS = 50,
|
||||
TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME = 51,
|
||||
TCPIP_ADAPTER_IP_REQUEST_RETRY_TIME = 52,
|
||||
}tcpip_adapter_option_id;
|
||||
TCPIP_ADAPTER_ROUTER_SOLICITATION_ADDRESS = 32,
|
||||
TCPIP_ADAPTER_REQUESTED_IP_ADDRESS = 50,
|
||||
TCPIP_ADAPTER_IP_ADDRESS_LEASE_TIME = 51,
|
||||
TCPIP_ADAPTER_IP_REQUEST_RETRY_TIME = 52,
|
||||
} tcpip_adapter_option_id;
|
||||
|
||||
void tcpip_adapter_init(void);
|
||||
|
||||
@ -110,12 +110,12 @@ esp_err_t tcpip_adapter_set_mac(tcpip_adapter_if_t tcpip_if, uint8_t *mac);
|
||||
#endif
|
||||
|
||||
esp_err_t tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status);
|
||||
esp_err_t tcpip_adapter_dhcps_option(uint8_t opt_op, uint8_t opt_id, void* opt_val, uint32_t opt_len);
|
||||
esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_option_mode opt_op, tcpip_adapter_option_id opt_id, void *opt_val, uint32_t opt_len);
|
||||
esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if);
|
||||
esp_err_t tcpip_adapter_dhcps_stop(tcpip_adapter_if_t tcpip_if);
|
||||
|
||||
esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adapter_dhcp_status_t *status);
|
||||
esp_err_t tcpip_adapter_dhcpc_option(uint8_t opt_op, uint8_t opt_id, void* opt_val, uint32_t opt_len);
|
||||
esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode opt_op, tcpip_adapter_option_id opt_id, void *opt_val, uint32_t opt_len);
|
||||
esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if);
|
||||
esp_err_t tcpip_adapter_dhcpc_stop(tcpip_adapter_if_t tcpip_if);
|
||||
|
||||
|
@ -297,76 +297,97 @@ esp_err_t tcpip_adapter_dhcps_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adap
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t tcpip_adapter_dhcps_option(uint8_t opt_op, uint8_t opt_id, void* opt_val, uint32_t opt_len)
|
||||
esp_err_t tcpip_adapter_dhcps_option(tcpip_adapter_option_mode opt_op, tcpip_adapter_option_id opt_id, void *opt_val, uint32_t opt_len)
|
||||
{
|
||||
void* opt_info = dhcps_option_info(opt_id, opt_len);
|
||||
if (opt_info == NULL || opt_val == NULL)
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
void *opt_info = dhcps_option_info(opt_id, opt_len);
|
||||
|
||||
if (opt_op == TCPIP_ADAPTER_OP_GET){
|
||||
if (dhcps_status == TCPIP_ADAPTER_DHCP_STOPED)
|
||||
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPED;
|
||||
if (opt_info == NULL || opt_val == NULL) {
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
switch (opt_id){
|
||||
case IP_ADDRESS_LEASE_TIME:
|
||||
*(uint32_t*)opt_val = *(uint32_t*)opt_info;
|
||||
break;
|
||||
case REQUESTED_IP_ADDRESS:
|
||||
memcpy(opt_val, opt_info, opt_len);
|
||||
break;
|
||||
case ROUTER_SOLICITATION_ADDRESS:
|
||||
*(uint8_t *)opt_val = (*(uint8_t *)opt_info) & OFFER_ROUTER;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else{
|
||||
if (dhcps_status == TCPIP_ADAPTER_DHCP_STARTED)
|
||||
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED;
|
||||
if (opt_op == TCPIP_ADAPTER_OP_GET) {
|
||||
if (dhcps_status == TCPIP_ADAPTER_DHCP_STOPED) {
|
||||
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STOPED;
|
||||
}
|
||||
|
||||
switch (opt_id){
|
||||
case IP_ADDRESS_LEASE_TIME:
|
||||
if (*(uint32_t*)opt_val != 0)
|
||||
*(uint32_t*)opt_info = *(uint32_t*)opt_val;
|
||||
else
|
||||
*(uint32_t*)opt_info = DHCPS_LEASE_TIME_DEF;
|
||||
break;
|
||||
case REQUESTED_IP_ADDRESS:{
|
||||
struct ip_info info;
|
||||
uint32_t softap_ip = 0;uint32_t start_ip = 0;uint32_t end_ip = 0;
|
||||
struct dhcps_lease *poll = opt_val;
|
||||
switch (opt_id) {
|
||||
case IP_ADDRESS_LEASE_TIME:
|
||||
{
|
||||
*(uint32_t*)opt_val = *(uint32_t*)opt_info;
|
||||
break;
|
||||
}
|
||||
case REQUESTED_IP_ADDRESS:
|
||||
{
|
||||
memcpy(opt_val, opt_info, opt_len);
|
||||
break;
|
||||
}
|
||||
case ROUTER_SOLICITATION_ADDRESS:
|
||||
{
|
||||
*(uint8_t *)opt_val = (*(uint8_t *)opt_info) & OFFER_ROUTER;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else if (opt_op == TCPIP_ADAPTER_OP_SET) {
|
||||
if (dhcps_status == TCPIP_ADAPTER_DHCP_STARTED) {
|
||||
return ESP_ERR_TCPIP_ADAPTER_DHCP_ALREADY_STARTED;
|
||||
}
|
||||
|
||||
memset(&info, 0x00, sizeof(struct ip_info));
|
||||
tcpip_adapter_get_ip_info(WIFI_IF_AP, &info);
|
||||
softap_ip = htonl(info.ip.addr);
|
||||
start_ip = htonl(poll->start_ip.addr);
|
||||
end_ip = htonl(poll->end_ip.addr);
|
||||
switch (opt_id) {
|
||||
case IP_ADDRESS_LEASE_TIME:
|
||||
{
|
||||
if (*(uint32_t*)opt_val != 0)
|
||||
*(uint32_t*)opt_info = *(uint32_t*)opt_val;
|
||||
else
|
||||
*(uint32_t*)opt_info = DHCPS_LEASE_TIME_DEF;
|
||||
break;
|
||||
}
|
||||
case REQUESTED_IP_ADDRESS:
|
||||
{
|
||||
struct ip_info info;
|
||||
uint32_t softap_ip = 0;
|
||||
uint32_t start_ip = 0;
|
||||
uint32_t end_ip = 0;
|
||||
struct dhcps_lease *poll = opt_val;
|
||||
|
||||
/*config ip information can't contain local ip*/
|
||||
if ((start_ip <= softap_ip) && (softap_ip <= end_ip))
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
memset(&info, 0x00, sizeof(struct ip_info));
|
||||
tcpip_adapter_get_ip_info(WIFI_IF_AP, &info);
|
||||
softap_ip = htonl(info.ip.addr);
|
||||
start_ip = htonl(poll->start_ip.addr);
|
||||
end_ip = htonl(poll->end_ip.addr);
|
||||
|
||||
/*config ip information must be in the same segment as the local ip*/
|
||||
softap_ip >>= 8;
|
||||
if ((start_ip >> 8 != softap_ip)
|
||||
|| (end_ip >> 8 != softap_ip)) {
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
}
|
||||
/*config ip information can't contain local ip*/
|
||||
if ((start_ip <= softap_ip) && (softap_ip <= end_ip))
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
|
||||
if (end_ip - start_ip > DHCPS_MAX_LEASE)
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
/*config ip information must be in the same segment as the local ip*/
|
||||
softap_ip >>= 8;
|
||||
if ((start_ip >> 8 != softap_ip)
|
||||
|| (end_ip >> 8 != softap_ip)) {
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
memcpy(opt_info, opt_val, opt_len);
|
||||
}
|
||||
break;
|
||||
case ROUTER_SOLICITATION_ADDRESS:
|
||||
*(uint8_t *)opt_info = (*(uint8_t *)opt_val) & OFFER_ROUTER;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ESP_OK;
|
||||
if (end_ip - start_ip > DHCPS_MAX_LEASE) {
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
memcpy(opt_info, opt_val, opt_len);
|
||||
break;
|
||||
}
|
||||
case ROUTER_SOLICITATION_ADDRESS:
|
||||
{
|
||||
*(uint8_t *)opt_info = (*(uint8_t *)opt_val) & OFFER_ROUTER;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
return ESP_ERR_TCPIP_ADAPTER_INVALID_PARAMS;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if)
|
||||
@ -381,8 +402,8 @@ esp_err_t tcpip_adapter_dhcps_start(tcpip_adapter_if_t tcpip_if)
|
||||
struct netif *p_netif = esp_netif[tcpip_if];
|
||||
|
||||
if (p_netif != NULL && netif_is_up(p_netif)) {
|
||||
struct ip_info default_ip;
|
||||
tcpip_adapter_get_ip_info(WIFI_IF_AP, &default_ip);
|
||||
struct ip_info default_ip;
|
||||
tcpip_adapter_get_ip_info(WIFI_IF_AP, &default_ip);
|
||||
dhcps_start(p_netif, &default_ip);
|
||||
dhcps_status = TCPIP_ADAPTER_DHCP_STARTED;
|
||||
TCPIP_ADAPTER_DEBUG("dhcp server start successfully\n");
|
||||
@ -432,10 +453,10 @@ esp_err_t tcpip_adapter_dhcpc_get_status(tcpip_adapter_if_t tcpip_if, tcpip_adap
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t tcpip_adapter_dhcpc_option(uint8_t opt_op, uint8_t opt_id, void* opt_val, uint32_t opt_len)
|
||||
esp_err_t tcpip_adapter_dhcpc_option(tcpip_adapter_option_mode opt_op, tcpip_adapter_option_id opt_id, void *opt_val, uint32_t opt_len)
|
||||
{
|
||||
//TODO: when dhcp request timeout,change the retry count
|
||||
return ESP_OK;
|
||||
// TODO: when dhcp request timeout,change the retry count
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t tcpip_adapter_dhcpc_start(tcpip_adapter_if_t tcpip_if)
|
||||
|
Loading…
x
Reference in New Issue
Block a user