Пример #1
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));
}
Пример #2
0
// warn: each IP in the range requires memory!
void STC_FLASHMEM DHCPServer::configure(char const *startIP, char const *endIP) {
  wifi_softap_dhcps_stop();

  dhcps_lease lease;
  lease.start_ip = IPAddress(startIP).get_ip_addr_t();
  lease.end_ip = IPAddress(endIP).get_ip_addr_t();
  wifi_softap_set_dhcps_lease(&lease);

  uint8_t mode = 1;
  wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode);

  wifi_softap_dhcps_start();
}
Пример #3
0
void ICACHE_FLASH_ATTR init_dns()
{
   //set softAP DHCP server router info
   uint8_t mode = 1;
   wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode);

   //1:station; 2:soft-AP, 3:station+soft-AP
   wifi_set_broadcast_if(3);
   //  espconn_disconnect(&dnsConn);
   espconn_delete(&dnsConn);

   dnsConn.type=ESPCONN_UDP;
   dnsConn.state=ESPCONN_NONE;
   dnsUdp.local_port= 53;
   dnsConn.proto.udp=&dnsUdp;
   espconn_regist_recvcb(&dnsConn, dnsQueryReceived);

   int res = espconn_create(&dnsConn);

   NODE_DBG("DNS server init, conn=%p , status=%d", &dnsConn,res);
}
Пример #4
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;
}
Пример #5
0
/**
 * Configure access point
 * @param local_ip      access point IP
 * @param gateway       gateway IP
 * @param subnet        subnet mask
 */
bool ESP8266WiFiAPClass::softAPConfig(IPAddress local_ip, IPAddress gateway, IPAddress subnet) {
    DEBUG_WIFI("[APConfig] local_ip: %s gateway: %s subnet: %s\n", local_ip.toString().c_str(), gateway.toString().c_str(), subnet.toString().c_str());
    if(!WiFi.enableAP(true)) {
        // enable AP failed
        DEBUG_WIFI("[APConfig] enableAP failed!\n");
        return false;
    }
    bool ret = true;

    struct ip_info info;
    info.ip.addr = static_cast<uint32_t>(local_ip);
    info.gw.addr = static_cast<uint32_t>(gateway);
    info.netmask.addr = static_cast<uint32_t>(subnet);

    if(!wifi_softap_dhcps_stop()) {
        DEBUG_WIFI("[APConfig] wifi_softap_dhcps_stop failed!\n");
    }

    if(!wifi_set_ip_info(SOFTAP_IF, &info)) {
        DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
        ret = false;
    }

    struct dhcps_lease dhcp_lease;
    IPAddress ip = local_ip;
    ip[3] += 99;
    dhcp_lease.start_ip.addr = static_cast<uint32_t>(ip);
    DEBUG_WIFI("[APConfig] DHCP IP start: %s\n", ip.toString().c_str());

    ip[3] += 100;
    dhcp_lease.end_ip.addr = static_cast<uint32_t>(ip);
    DEBUG_WIFI("[APConfig] DHCP IP end: %s\n", ip.toString().c_str());

    if(!wifi_softap_set_dhcps_lease(&dhcp_lease)) {
        DEBUG_WIFI("[APConfig] wifi_set_ip_info failed!\n");
        ret = false;
    }

    // set lease time to 720min --> 12h
    if(!wifi_softap_set_dhcps_lease_time(720)) {
        DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_lease_time failed!\n");
        ret = false;
    }

    uint8 mode = 1;
    if(!wifi_softap_set_dhcps_offer_option(OFFER_ROUTER, &mode)) {
        DEBUG_WIFI("[APConfig] wifi_softap_set_dhcps_offer_option failed!\n");
        ret = false;
    }

    if(!wifi_softap_dhcps_start()) {
        DEBUG_WIFI("[APConfig] wifi_softap_dhcps_start failed!\n");
        ret = false;
    }

    // check config
    if(wifi_get_ip_info(SOFTAP_IF, &info)) {
        if(info.ip.addr == 0x00000000) {
            DEBUG_WIFI("[APConfig] IP config Invalid?!\n");
            ret = false;
        } else if(local_ip != info.ip.addr) {
            ip = info.ip.addr;
            DEBUG_WIFI("[APConfig] IP config not set correct?! new IP: %s\n", ip.toString().c_str());
            ret = false;
        }
    } else {
        DEBUG_WIFI("[APConfig] wifi_get_ip_info failed!\n");
        ret = false;
    }

    return ret;
}
Пример #6
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();
	}
}