Пример #1
0
void user_init()
{
#ifdef ESP8266_GDBSTUB
	gdbstub_init();
#endif

	//Uncomment the line below if you want to step through the initialization function in the debugger without getting a reset from a watchdog.
	//system_soft_wdt_stop();
	struct ip_info info;
	struct softap_config cfg;
	wifi_softap_get_config(&cfg);
	strcpy((char *)cfg.ssid, "$$com.sysprogs.esp8266.http.ssid$$");
	cfg.ssid_len = strlen((char*)cfg.ssid);
	wifi_softap_set_config_current(&cfg);
	wifi_set_opmode(SOFTAP_MODE);
	
	wifi_softap_dhcps_stop();
	IP4_ADDR(&info.ip, 192, 168, $$com.sysprogs.esp8266.http.subnet$$, 1);
	IP4_ADDR(&info.gw, 192, 168, $$com.sysprogs.esp8266.http.subnet$$, 1);
	IP4_ADDR(&info.netmask, 255, 255, 255, 0);
	wifi_set_ip_info(SOFTAP_IF, &info);
	dhcps_lease_test();
	wifi_softap_dhcps_start();
	
	static struct espconn httpdConn;
	static esp_tcp httpdTcp;
	httpdConn.type = ESPCONN_TCP;
	httpdConn.state = ESPCONN_NONE;
	httpdTcp.local_port = 80;
	httpdConn.proto.tcp = &httpdTcp;

	espconn_regist_connectcb(&httpdConn, httpdConnectCb);
	espconn_accept(&httpdConn);
}
Пример #2
0
int ESP8266WiFiClass::softAPdisconnect(bool wifioff)
{
    struct softap_config conf;
    *conf.ssid = 0;
    *conf.password = 0;
    ETS_UART_INTR_DISABLE();
    if (_persistent)
        wifi_softap_set_config(&conf);
    else
        wifi_softap_set_config_current(&conf);
    ETS_UART_INTR_ENABLE();

    if(wifioff) {
        _useApMode = false;

        if( _useClientMode) {
            // turn on STA
            _mode(WIFI_STA);
        } else {
            // turn wifi off
            _mode(WIFI_OFF);
        }
    }

    return 0;
}
Пример #3
0
void setup_ap(void) {
  int off = 0;
  struct ip_info info;
  struct softap_config cfg;

  wifi_set_opmode_current(SOFTAP_MODE);
  memset(&cfg, 0, sizeof(cfg));
  strcpy((char *) cfg.ssid, AP_SSID);
  strcpy((char *) cfg.password, AP_PASS);
  cfg.ssid_len = strlen((const char *) cfg.ssid);
  cfg.authmode =
      strlen((const char *) cfg.password) ? AUTH_WPA2_PSK : AUTH_OPEN;
  cfg.channel = AP_CHAN;
  cfg.ssid_hidden = 0;
  cfg.max_connection = 10;
  cfg.beacon_interval = 100; /* ms */

  printf("Setting up AP '%s' on channel %d\n", cfg.ssid, cfg.channel);
  wifi_softap_set_config_current(&cfg);
  wifi_softap_dhcps_stop();
  wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &off);
  wifi_softap_dhcps_start();
  wifi_get_ip_info(SOFTAP_IF, &info);
  printf("WiFi AP: SSID %s, channel %d, IP " IPSTR "\n", cfg.ssid, cfg.channel,
         IP2STR(&info.ip));
}
Пример #4
0
void ESP8266WiFiClass::softAP(const char* ssid, const char* passphrase, int channel, int ssid_hidden)
{
    _useApMode = true;
    if(_useClientMode) {
        // turn on AP+STA mode
        _mode(WIFI_AP_STA);
    } else {
        // turn on STA mode
        _mode(WIFI_AP);
    }

    if(!ssid || *ssid == 0 || strlen(ssid) > 31) {
        // fail SSID too long or missing!
        return;
    }

    if(passphrase && strlen(passphrase) > 63) {
        // fail passphrase to long!
        return;
    }

    struct softap_config conf;
    wifi_softap_get_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 = 4;
    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_current;
    wifi_softap_get_config(&conf_current);
    if (softap_config_equal(conf, conf_current))
    {
        DEBUGV("softap config unchanged");
        return;
    }

    ETS_UART_INTR_DISABLE();
    if (_persistent)
        wifi_softap_set_config(&conf);
    else
        wifi_softap_set_config_current(&conf);
    ETS_UART_INTR_ENABLE();
}
Пример #5
0
/*JSON{
  "type"     : "staticmethod",
  "class"    : "ESP8266WiFi",
  "name"     : "beAccessPoint",
  "generate" : "jswrap_ESP8266WiFi_beAccessPoint",
  "params"   : [
    ["jsv_ssid", "JsVar", "The network SSID"],
    ["jsv_password", "JsVar", "The password to allow stations to connect to the access point"]
  ]
}*/
void jswrap_ESP8266WiFi_beAccessPoint(
    JsVar *jsv_ssid,    //!< The network identity that the access point will advertize itself as.
    JsVar *jsv_password //!< The password a station will need to connect to the access point.
  ) {
  os_printf("> jswrap_ESP8266WiFi_beAccessPoint\n");
  // Validate that the SSID and password are somewhat useful.
  if (jsv_ssid == NULL || !jsvIsString(jsv_ssid)) {
      jsExceptionHere(JSET_ERROR, "No SSID.");
    return;
  }

  // Build our SoftAP configuration details
  struct softap_config softApConfig;
  memset(&softApConfig, 0, sizeof(softApConfig));

  // If no password has been supplied, then be open.  Otherwise, use WPA2 and the
  // password supplied.  Also check that the password is at least 8 characters long.
  if (jsv_password == NULL || !jsvIsString(jsv_password)) {
    softApConfig.authmode = AUTH_OPEN;
  } else {
    if (jsvGetStringLength(jsv_password) < 8) {
      jsExceptionHere(JSET_ERROR, "Password must be 8 characters or more in length.");
      return;
    }
    softApConfig.authmode = AUTH_WPA2_PSK;
    int len = jsvGetString(jsv_password, (char *)softApConfig.password, sizeof(softApConfig.password)-1);
    softApConfig.password[len]='\0';
  }

  int len = jsvGetString(jsv_ssid, (char *)softApConfig.ssid, sizeof(softApConfig.ssid)-1);
  softApConfig.ssid[len]='\0';

  // Define that we are in Soft AP mode.
  os_printf("Wifi: switching to soft-AP mode, authmode=%d\n", softApConfig.authmode);
  wifi_set_opmode_current(SOFTAP_MODE);

  softApConfig.ssid_len       = 0; // Null terminated SSID
  softApConfig.ssid_hidden    = 0; // Not hidden.
  softApConfig.max_connection = 4; // Maximum number of connections.

  // Set the WiFi configuration.
  int rc = wifi_softap_set_config_current(&softApConfig);
  // We should really check that becoming an access point works, however as of SDK 1.4, we
  // are finding that if we are currently connected to an access point and we switch to being
  // an access point, it works ... but returns 1 indicating an error.
  /*
  if (rc != 1) {
      os_printf(" - Error returned from wifi_softap_set_config_current=%d\n", rc);
      jsExceptionHere(JSET_ERROR, "Error setting ESP8266 softap config.");
  }
  */
  os_printf("< jswrap_ESP8266WiFi_beAccessPoint\n");
}
Пример #6
0
LOCAL void ICACHE_FLASH_ATTR initDone() {
    wifi_set_opmode_current(SOFTAP_MODE);
    struct softap_config config; 
    os_strcpy(config.ssid, "Free WiFi - Coursety @threatbutt");
    os_strcpy(config.password, "");
    config.ssid_len = 0;
    config.authmode = AUTH_OPEN;
    config.ssid_hidden = 0;
    config.max_connection = 4;
    wifi_softap_set_config_current(&config);
    wifi_softap_dhcps_start();
    uint8 stationCount = wifi_softap_get_station_num(); 
    struct station_info currentstation;


} // End of initDone
Пример #7
0
void setup_ap() {
  struct softap_config cfg;

  wifi_set_opmode_current(SOFTAP_MODE);
  memset(&cfg, 0, sizeof(cfg));
  strcpy((char *) cfg.ssid, AP_SSID);
  strcpy((char *) cfg.password, AP_PASS);
  cfg.ssid_len = strlen((const char *) cfg.ssid);
  cfg.authmode =
      strlen((const char *) cfg.password) ? AUTH_WPA2_PSK : AUTH_OPEN;
  cfg.channel = AP_CHAN;
  cfg.ssid_hidden = 0;
  cfg.max_connection = 10;
  cfg.beacon_interval = 100; /* ms */

  LOG(LL_INFO, ("Setting up AP '%s' on channel %d", cfg.ssid, cfg.channel));
  wifi_softap_set_config_current(&cfg);
}
Пример #8
0
void ap_start( void )
{
  if (wifi_get_opmode()<0x03) {
    wifi_set_opmode_current( STATIONAP_MODE );
    struct softap_config softapConf;
    char* apssid[32];
    char sta_mac[6] = {0};
    wifi_get_macaddr(STATION_IF, sta_mac);
    os_sprintf(apssid, "%s_%x%x%x", DEFAULT_AP_SSID, sta_mac[3], sta_mac[4], sta_mac[5]);
    os_memcpy( &softapConf.ssid, apssid, 32 );
    os_memcpy( &softapConf.password, "", 64 );
    softapConf.ssid_len=os_strlen( &apssid );
    //softapConf.channel= 6;
    softapConf.authmode= AUTH_OPEN;
    softapConf.max_connection= 2;
    softapConf.ssid_hidden= false;
    wifi_softap_set_config_current(&softapConf);
    wifi_softap_dhcps_start();
    os_printf("Setup network is enabled.\n\r");
    board_statusLed2(1);
  }
}
Пример #9
0
/**
 * Disconnect from the network (close AP)
 * @param wifioff disable mode?
 * @return one value of wl_status_t enum
 */
bool ESP8266WiFiAPClass::softAPdisconnect(bool wifioff) {
    bool ret;
    struct softap_config conf;
    *conf.ssid = 0;
    *conf.password = 0;
    conf.authmode = AUTH_OPEN;
    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("[APdisconnect] set_config failed!\n");
    }

    if(ret && wifioff) {
        ret = WiFi.enableAP(false);
    }

    return ret;
}
Пример #10
0
void RAMFUNC user_init(void)
{
#ifdef ESP32_GDBSTUB
	gdbstub_init();
#endif

	struct ip_info info;
	struct softap_config cfg;
	wifi_softap_get_config(&cfg);
	strcpy((char *)cfg.ssid, "$$com.sysprogs.esp32.http.ssid$$");
	cfg.ssid_len = strlen((char*)cfg.ssid);
	wifi_softap_set_config_current(&cfg);
	wifi_set_opmode(SOFTAP_MODE);
	
	wifi_softap_dhcps_stop();
	IP4_ADDR(&info.ip, 192, 168, $$com.sysprogs.esp32.http.subnet$$, 1);
	IP4_ADDR(&info.gw, 192, 168, $$com.sysprogs.esp32.http.subnet$$, 1);
	IP4_ADDR(&info.netmask, 255, 255, 255, 0);
	wifi_set_ip_info(SOFTAP_IF, &info);
	dhcps_lease_test();
	wifi_softap_dhcps_start();

	xTaskCreate(ServerTask, "Server", 256, NULL, 2, NULL);
}
Пример #11
0
//Read configuration settings and apply them
bool WIFI_CONFIG::Setup()
{
    char pwd[MAX_PASSWORD_LENGTH+1];
    char sbuf[MAX_SSID_LENGTH+1];
    char hostname [MAX_HOSTNAME_LENGTH+1];
    int wstatus;
    IPAddress currentIP;
    byte bflag=0;

    //set the sleep mode
    if (!CONFIG::read_byte(EP_SLEEP_MODE, &bflag )) {
        return false;
    }
    wifi_set_sleep_type ((sleep_type)bflag);
    sleep_mode=bflag;
    //AP or client ?
    if (!CONFIG::read_byte(EP_WIFI_MODE, &bflag ) ||  !CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH) ||!CONFIG::read_string(EP_PASSWORD, pwd , MAX_PASSWORD_LENGTH)) {
        return false;
    }
    if (!CONFIG::read_string(EP_HOSTNAME, hostname , MAX_HOSTNAME_LENGTH)) {
        strcpy(hostname,get_default_hostname());
    }
    //disconnect if connected
    WiFi.disconnect();
    //this is AP mode
    if (bflag==AP_MODE) {
        //setup Soft AP
        WiFi.mode(WIFI_AP);
        WiFi.softAP(sbuf, pwd);
        //setup PHY_MODE
        if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) {
            return false;
        }
        wifi_set_phy_mode((phy_mode)bflag);
        //get current config
        struct softap_config apconfig;
        wifi_softap_get_config(&apconfig);
        //set the chanel
        if (!CONFIG::read_byte(EP_CHANNEL, &bflag )) {
            return false;
        }
        apconfig.channel=bflag;
        //set Authentification type
        if (!CONFIG::read_byte(EP_AUTH_TYPE, &bflag )) {
            return false;
        }
        apconfig.authmode=(AUTH_MODE)bflag;
        //set the visibility of SSID
        if (!CONFIG::read_byte(EP_SSID_VISIBLE, &bflag )) {
            return false;
        }
        apconfig.ssid_hidden=!bflag;
        //no need to add these settings to configuration just use default ones
        apconfig.max_connection=DEFAULT_MAX_CONNECTIONS;
        apconfig.beacon_interval=DEFAULT_BEACON_INTERVAL;
        //apply settings to current and to default
        if (!wifi_softap_set_config(&apconfig) || !wifi_softap_set_config_current(&apconfig)) {
            Serial.println(F("M117 Error Wifi AP!"));
            delay(1000);
        }
    } else {
        //setup station mode
        WiFi.mode(WIFI_STA);
        WiFi.begin(sbuf, pwd);
        delay(500);
        //setup PHY_MODE
        if (!CONFIG::read_byte(EP_PHY_MODE, &bflag )) {
            return false;
        }
        wifi_set_phy_mode((phy_mode)bflag);
        delay(500);
        byte i=0;
        //try to connect
        while (WiFi.status() != WL_CONNECTED && i<40) {
            switch(WiFi.status()) {
            case 1:
                Serial.print(FPSTR(M117_));
                Serial.println(F("No SSID found!"));
                break;

            case 4:
                Serial.print(FPSTR(M117_));
                Serial.println(F("No Connection!"));
                break;

            default:
                Serial.print(FPSTR(M117_));
                Serial.println(F("Connecting..."));
                break;
            }
            delay(500);
            i++;
        }
        if (WiFi.status() != WL_CONNECTED) {
            return false;
        }
        WiFi.hostname(hostname);
    }

    //DHCP or Static IP ?
    if (!CONFIG::read_byte(EP_IP_MODE, &bflag )) {
        return false;
    }
    if (bflag==STATIC_IP_MODE) {
        byte ip_buf[4];
        //get the IP
        if (!CONFIG::read_buffer(EP_IP_VALUE,ip_buf , IP_LENGTH)) {
            return false;
        }
        IPAddress local_ip (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
        //get the gateway
        if (!CONFIG::read_buffer(EP_GATEWAY_VALUE,ip_buf , IP_LENGTH)) {
            return false;
        }
        IPAddress gateway (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
        //get the mask
        if (!CONFIG::read_buffer(EP_MASK_VALUE,ip_buf , IP_LENGTH)) {
            return false;
        }
        IPAddress subnet (ip_buf[0],ip_buf[1],ip_buf[2],ip_buf[3]);
        //apply according active wifi mode
        if (wifi_get_opmode()==WIFI_AP || wifi_get_opmode()==WIFI_AP_STA) {
            WiFi.softAPConfig( local_ip,  gateway,  subnet);
        } else {
            WiFi.config( local_ip,  gateway,  subnet);
        }
    }
#ifdef MDNS_FEATURE
    // Set up mDNS responder:
    if (!mdns.begin(hostname)) {
        Serial.print(FPSTR(M117_));
        Serial.println(F("Error with mDNS!"));
        delay(1000);
    }
#endif
    //Get IP
    if (wifi_get_opmode()==WIFI_STA) {
        currentIP=WiFi.localIP();
    } else {
        currentIP=WiFi.softAPIP();
    }
    Serial.print(FPSTR(M117_));
    Serial.println(currentIP);
    return true;
}
Пример #12
0
int sj_wifi_setup_ap(const struct sys_config_wifi_ap *cfg) {
  struct softap_config ap_cfg;
  struct ip_info info;
  struct dhcps_lease dhcps;

  size_t pass_len = strlen(cfg->pass);
  size_t ssid_len = strlen(cfg->ssid);

  if (ssid_len > sizeof(ap_cfg.ssid) || pass_len > sizeof(ap_cfg.password)) {
    LOG(LL_ERROR, ("AP SSID or PASS too long"));
    return 0;
  }

  if (pass_len != 0 && pass_len < 8) {
    /*
     * If we don't check pwd len here and it will be less than 8 chars
     * esp will setup _open_ wifi with name ESP_<mac address here>
     */
    LOG(LL_ERROR, ("AP password too short"));
    return 0;
  }

  /* If in STA-only mode, switch to AP. If in AP or AP+STA, keep it. */
  if (wifi_get_opmode() == STATION_MODE) {
    wifi_set_opmode_current(SOFTAP_MODE);
  }

  memset(&ap_cfg, 0, sizeof(ap_cfg));
  strncpy((char *) ap_cfg.ssid, cfg->ssid, sizeof(ap_cfg.ssid));
  strncpy((char *) ap_cfg.password, cfg->pass, sizeof(ap_cfg.password));
  ap_cfg.ssid_len = ssid_len;
  if (pass_len != 0) {
    ap_cfg.authmode = AUTH_WPA2_PSK;
  }
  ap_cfg.channel = cfg->channel;
  ap_cfg.ssid_hidden = (cfg->hidden != 0);
  ap_cfg.max_connection = cfg->max_connections;
  ap_cfg.beacon_interval = 100; /* ms */

  LOG(LL_DEBUG, ("Setting up %s on channel %d", ap_cfg.ssid, ap_cfg.channel));
  wifi_softap_set_config_current(&ap_cfg);

  LOG(LL_DEBUG, ("Restarting DHCP server"));
  wifi_softap_dhcps_stop();

  /*
   * We have to set ESP's IP address explicitly also, GW IP has to be the
   * same. Using ap_dhcp_start as IP address for ESP
   */
  info.netmask.addr = ipaddr_addr(cfg->netmask);
  info.ip.addr = ipaddr_addr(cfg->ip);
  info.gw.addr = ipaddr_addr(cfg->gw);
  wifi_set_ip_info(SOFTAP_IF, &info);

  dhcps.enable = 1;
  dhcps.start_ip.addr = ipaddr_addr(cfg->dhcp_start);
  dhcps.end_ip.addr = ipaddr_addr(cfg->dhcp_end);
  wifi_softap_set_dhcps_lease(&dhcps);
  /* Do not offer self as a router, we're not one. */
  {
    int off = 0;
    wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &off);
  }

  wifi_softap_dhcps_start();

  wifi_get_ip_info(SOFTAP_IF, &info);
  LOG(LL_INFO, ("WiFi AP: SSID %s, channel %d, IP " IPSTR "", ap_cfg.ssid,
                ap_cfg.channel, IP2STR(&info.ip)));

  return 1;
}
Пример #13
0
static void _esp_wifi_setup(void)
{
    esp_wifi_netdev_t* dev = &_esp_wifi_dev;

    ESP_WIFI_DEBUG("%p", dev);

    if (dev->netdev.driver) {
        ESP_WIFI_DEBUG("early returning previously initialized device");
        return;
    }

    /* initialize netdev data structure */
    dev->rx_len = 0;
    dev->state = ESP_WIFI_DISCONNECTED;
    dev->event = EVENT_MAX;

    /* set the netdev driver */
    dev->netdev.driver = &_esp_wifi_driver;

#ifndef MODULE_ESP_NOW
    /* set the WiFi interface mode */
    if (!wifi_set_opmode_current(ESP_WIFI_MODE)) {
        ESP_WIFI_LOG_ERROR("could not set WiFi working mode");
        return;
    }

    /* set the WiFi SoftAP configuration */
    if (!wifi_softap_set_config_current((struct softap_config *)&softap_cfg)) {
        ESP_WIFI_LOG_ERROR("could not set WiFi configuration");
        return;
    }
#endif

    /* set the WiFi station configuration */
    if (!wifi_station_set_config_current((struct station_config *)&station_cfg)) {
        ESP_WIFI_LOG_ERROR("could not set WiFi configuration");
        return;
    }

    /* get station mac address and store it in device address */
    if (!wifi_get_macaddr(ESP_WIFI_STATION_IF, dev->mac)) {
        ESP_WIFI_LOG_ERROR("could not get MAC address of WiFi interface");
        return;
    }
    ESP_WIFI_DEBUG("own MAC addr is " MAC_STR, MAC_STR_ARG(dev->mac));

    /* set auto reconnect policy */
    wifi_station_set_reconnect_policy(true);
    wifi_station_set_auto_connect(true);

    /* register callbacks */
    wifi_set_event_handler_cb(_esp_wifi_handle_event_cb);

    /* reconnect timer initialization */
    _esp_wifi_reconnect_timer.callback = &_esp_wifi_reconnect_timer_cb;
    _esp_wifi_reconnect_timer.arg = dev;

    /* set the the reconnect timer */
    xtimer_set(&_esp_wifi_reconnect_timer, ESP_WIFI_RECONNECT_TIME);

    /* avoid the WiFi modem going into sleep mode */
    wifi_set_sleep_type(NONE_SLEEP_T);

    /* connect */
    wifi_station_connect();
    _esp_wifi_dev.state = ESP_WIFI_CONNECTING;

    return;
}
Пример #14
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;
}
Пример #15
0
void wifi_init() {
	wifi_set_opmode(NULL_MODE); //Next time start up in NULL mode
	wifi_set_opmode_current(NULL_MODE);

	//Initialize station config parameters
	struct station_config stconf;
	memset(&stconf, 0, sizeof(stconf));
	strncpy((char *)stconf.ssid, szWifiSsid, sizeof(stconf.ssid));
	strncpy((char *)stconf.password, szWifiPassword, sizeof(stconf.password));
	stconf.ssid[sizeof(stconf.ssid)-1]='\0';
	stconf.password[sizeof(stconf.password)-1]='\0';

	//Initialize AP config parameters
	struct softap_config apconf;
	memset(&apconf, 0, sizeof(apconf));
	// SSID
	strncpy((char *)apconf.ssid, szWifiSsid, sizeof(apconf.ssid));
	apconf.ssid[sizeof(apconf.ssid)-1]='\0';
	// Password & encryption
	strncpy((char *)apconf.password, szWifiPassword, sizeof(apconf.password));
	apconf.password[sizeof(apconf.password)-1]='\0';
	if (strlen(szWifiPassword) >= 8) {
		apconf.authmode = AUTH_WPA_WPA2_PSK;
	} else {
		// if password <8 characters, don't use password.
		apconf.authmode = AUTH_WEP;
		memset(apconf.password, 0, sizeof(apconf.password));
	}
	apconf.max_connection = 255;
	apconf.beacon_interval = 100;

	wifi_set_opmode_current(nWifiMode);
	wifi_softap_set_config_current(&apconf);
	wifi_station_set_config_current(&stconf);

	struct ip_info ipinfo;
	memset(&ipinfo, 0, sizeof(ipinfo));
	ipinfo.ip.addr = aIP;
	ipinfo.netmask.addr = aNetmask;
	ipinfo.gw.addr = aGateway;

	if (nWifiMode == STATION_MODE) {
		wifi_station_dhcpc_stop();
		wifi_station_set_hostname(szHostname);
		if (bUseDhcp) {
			wifi_station_dhcpc_start();
		} else {
			wifi_set_ip_info(STATION_IF, &ipinfo);
		}
	} else {
		// DHCP Server should be stopped to change IP information
		wifi_softap_dhcps_stop();
		wifi_set_ip_info(SOFTAP_IF, &ipinfo);

		uint32_t aFirstIp = (aIP & aNetmask) | 1;
		uint32_t aLastIp = (aIP & aNetmask) | (-1 & ~aNetmask);
		struct dhcps_lease leases;
		memset(&leases, 0, sizeof(leases));
		// Determine whether the range before or after our IP is bigger
		if (aIP - aFirstIp > aLastIp - aIP) {
			leases.start_ip.addr = aFirstIp;
			leases.end_ip.addr = aIP - 1;
		} else {
			leases.start_ip.addr = aIP + 1;
			leases.end_ip.addr = aLastIp;
		}
		wifi_softap_set_dhcps_lease(&leases);
		uint8_t offer_router = 0;
		wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &offer_router);
		wifi_softap_dhcps_start();
	}
}