Beispiel #1
0
/**
 * Initializes DHCP server
 */
LOCAL void ICACHE_FLASH_ATTR esp_wifi_setup_dhcp_server( void )
{
    struct dhcps_lease admin_dhcp_config;
    uint8_t dhcp_server = 0x01;
    uint8_t dhcp_start_ip[ 4 ] = { WIFI_DHCP_SERVER_START_IP };
    uint8_t dhcp_end_ip[ 4 ] = { WIFI_DHCP_SERVER_END_IP };
    // check if parameters don't exist
   /* if( esp_flash_read_parameter( ESP_WIFI_APMODE_DHCP_ENABLE_ID, &dhcp_server ) == 0x00 )
    {
        // save defaults in flash
        esp_flash_save_parameter( ESP_WIFI_APMODE_DHCP_ENABLE_ID, &dhcp_server, 0x01 );
        esp_flash_save_parameter( ESP_WIFI_APMODE_DHCP_START_IP_ID, dhcp_start_ip, 0x04 );
        esp_flash_save_parameter( ESP_WIFI_APMODE_DHCP_END_IP_ID, dhcp_end_ip, 0x04 );
    } else {*/
        // stop the DHCP
        if( wifi_softap_dhcps_status() == DHCP_STARTED )
            wifi_softap_dhcps_stop();
        // check if DHCP is enabled
       // if( dhcp_server == 0x01 )         
       // {
            // read configuration details
            //esp_flash_read_parameter( ESP_WIFI_APMODE_DHCP_START_IP_ID, dhcp_start_ip );
           // esp_flash_read_parameter( ESP_WIFI_APMODE_DHCP_END_IP_ID, dhcp_end_ip );
            // configure and turn on DHCP
            IP4_ADDR( &admin_dhcp_config.start_ip, dhcp_start_ip[ 0 ], dhcp_start_ip[ 1 ], dhcp_start_ip[ 2 ], dhcp_start_ip[ 3 ] );
            IP4_ADDR( &admin_dhcp_config.end_ip, dhcp_end_ip[ 0 ], dhcp_end_ip[ 1 ], dhcp_end_ip[ 2 ], dhcp_end_ip[ 3 ] );
            wifi_softap_set_dhcps_lease( &admin_dhcp_config );
            wifi_softap_dhcps_start();
       // }
  //  }
}
Beispiel #2
0
bool ICACHE_FLASH_ATTR wifi_softap_set_dhcps_offer_option(uint8 level, void* optarg)
{
	bool offer_flag = true;
	if (optarg == NULL && wifi_softap_dhcps_status() == false)
		return false;

	if (level <= OFFER_START || level >= OFFER_END)
		return false;

	switch (level){
		case OFFER_ROUTER:
			offer = (*(uint8 *)optarg) & 0x01;
			offer_flag = true;
			break;
		default :
			offer_flag = false;
			break;
	}
	return offer_flag;
}
Beispiel #3
0
/**
 * Set up an access point
 * @param ssid              Pointer to the SSID (max 63 char).
 * @param passphrase        (for WPA2 min 8 char, for open use NULL)
 * @param channel           WiFi channel number, 1 - 13.
 * @param ssid_hidden       Network cloaking (0 = broadcast SSID, 1 = hide SSID)
 * @param max_connection    Max simultaneous connected clients, 1 - 4.
 */
bool ESP8266WiFiAPClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden, int max_connection) {

    if(!WiFi.enableAP(true)) {
        // enable AP failed
        DEBUG_WIFI("[AP] enableAP failed!\n");
        return false;
    }

    if(!ssid || strlen(ssid) == 0 || strlen(ssid) > 31) {
        // fail SSID too long or missing!
        DEBUG_WIFI("[AP] SSID too long or missing!\n");
        return false;
    }

    if(passphrase && strlen(passphrase) > 0 && (strlen(passphrase) > 63 || strlen(passphrase) < 8)) {
        // fail passphrase to long or short!
        DEBUG_WIFI("[AP] fail passphrase to long or short!\n");
        return false;
    }

    bool ret = true;

    struct softap_config conf;
    strcpy(reinterpret_cast<char*>(conf.ssid), ssid);
    conf.channel = channel;
    conf.ssid_len = strlen(ssid);
    conf.ssid_hidden = ssid_hidden;
    conf.max_connection = max_connection;
    conf.beacon_interval = 100;

    if(!passphrase || strlen(passphrase) == 0) {
        conf.authmode = AUTH_OPEN;
        *conf.password = 0;
    } else {
        conf.authmode = AUTH_WPA2_PSK;
        strcpy(reinterpret_cast<char*>(conf.password), passphrase);
    }

    struct softap_config conf_compare;
    if(WiFi._persistent){
        wifi_softap_get_config_default(&conf_compare);
    }
    else {
        wifi_softap_get_config(&conf_compare);
    }

    if(!softap_config_equal(conf, conf_compare)) {

        ETS_UART_INTR_DISABLE();
        if(WiFi._persistent) {
            ret = wifi_softap_set_config(&conf);
        } else {
            ret = wifi_softap_set_config_current(&conf);
        }
        ETS_UART_INTR_ENABLE();

        if(!ret) {
            DEBUG_WIFI("[AP] set_config failed!\n");
            return false;
        }

    } else {
        DEBUG_WIFI("[AP] softap config unchanged\n");
    }

    if(wifi_softap_dhcps_status() != DHCP_STARTED) {
        DEBUG_WIFI("[AP] DHCP not started, starting...\n");
        if(!wifi_softap_dhcps_start()) {
            DEBUG_WIFI("[AP] wifi_softap_dhcps_start failed!\n");
            ret = false;
        }
    }

    // check IP config
    struct ip_info ip;
    if(wifi_get_ip_info(SOFTAP_IF, &ip)) {
        if(ip.ip.addr == 0x00000000) {
            // Invalid config
            DEBUG_WIFI("[AP] IP config Invalid resetting...\n");
            //192.168.244.1 , 192.168.244.1 , 255.255.255.0
            ret = softAPConfig(0x01F4A8C0, 0x01F4A8C0, 0x00FFFFFF);
            if(!ret) {
                DEBUG_WIFI("[AP] softAPConfig failed!\n");
                ret = false;
            }
        }
    } else {
        DEBUG_WIFI("[AP] wifi_get_ip_info failed!\n");
        ret = false;
    }

    return ret;
}