Пример #1
0
void test_wlan_scan(int argc, char **argv)
{
	if (wlan_scan(__scan_cb))
		wmprintf("Error: scan request failed\r\n");
	else
		wmprintf("Scan scheduled...\r\n");
}
Пример #2
0
void handle_sysevt(VMINT message, VMINT param)
{
    switch (message) 
    {
        case VM_EVENT_CREATE:
            wlan_scan();
            break;

        case VM_EVENT_QUIT:
            break;
    }
}
Пример #3
0
void WiFiSetupConsole::handle(char c)
{
    if ('w' == c)
    {
        memset(ssid, 0, sizeof(ssid));
        memset(password, 0, sizeof(password));
        memset(security_type_string, 0, sizeof(security_type_string));
        security_ = WLAN_SEC_NOT_SET;
        cipher_ = WLAN_CIPHER_NOT_SET;

#if Wiring_WpaEnterprise == 1
        spark::WiFiAllocatedCredentials credentials;
        memset(eap_type_string, 0, sizeof(eap_type_string));
#else
        spark::WiFiCredentials credentials;
#endif
        WLanCredentials creds;

        do {
        print("SSID: ");
        read_line(ssid, 32);
        } while (strlen(ssid) == 0);

        wlan_scan([](WiFiAccessPoint* ap, void* ptr) {
            if (ptr) {
                WiFiSetupConsole* self = reinterpret_cast<WiFiSetupConsole*>(ptr);
                if (ap) {
                    if (ap->ssidLength && !strncmp(self->ssid, ap->ssid, std::max((size_t)ap->ssidLength, (size_t)strlen(self->ssid)))) {
                        self->security_ = ap->security;
                        self->cipher_ = ap->cipher;
                    }
                }
            }
        }, this);

        if (security_ == WLAN_SEC_NOT_SET)
        {
        do
        {
#if Wiring_WpaEnterprise == 0
            print("Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2: ");
            read_line(security_type_string, 1);
        }
        while ('0' > security_type_string[0] || '3' < security_type_string[0]);
#else
                print("Security 0=unsecured, 1=WEP, 2=WPA, 3=WPA2, 4=WPA Enterprise, 5=WPA2 Enterprise: ");
                read_line(security_type_string, 1);
            }
            while ('0' > security_type_string[0] || '5' < security_type_string[0]);
#endif
            security_ = (WLanSecurityType)(security_type_string[0] - '0');
        }

#if PLATFORM_ID<3
        if (security_ == WLAN_SEC_WEP)
        {
            print("\r\n ** Even though the CC3000 supposedly supports WEP,");
            print("\r\n ** we at Spark have never seen it work.");
            print("\r\n ** If you control the network, we recommend changing it to WPA2.\r\n");
        }
#endif

        if (security_ != WLAN_SEC_UNSEC)
            password[0] = '1'; // non-empty password so security isn't set to None

        credentials.setSsid(ssid);
        credentials.setPassword(password);
        credentials.setSecurity(security_);
        credentials.setCipher(cipher_);

        // dry run
        creds = credentials.getHalCredentials();
        if (this->config.connect_callback2(this->config.connect_callback_data, &creds, true)==WLAN_SET_CREDENTIALS_CIPHER_REQUIRED ||
            (cipher_ == WLAN_CIPHER_NOT_SET))
        {
            do
            {
                print("Security Cipher 1=AES, 2=TKIP, 3=AES+TKIP: ");
                read_line(security_type_string, 1);
            }
            while ('1' > security_type_string[0] || '3' < security_type_string[0]);
            switch (security_type_string[0]-'0') {
                case 1: cipher_ = WLAN_CIPHER_AES; break;
                case 2: cipher_ = WLAN_CIPHER_TKIP; break;
                case 3: cipher_ = WLAN_CIPHER_AES_TKIP; break;
            }
            credentials.setCipher(cipher_);
        }

        if (0 < security_ && security_ <= 3)
        {
            print("Password: "******"EAP Type 0=PEAP/MSCHAPv2, 1=EAP-TLS: ");
                read_line(eap_type_string, 1);
            }
            while ('0' > eap_type_string[0] || '1' < eap_type_string[0]);
            int eap_type = eap_type_string[0] - '0';

            if (!tmp_) {
                tmp_.reset(new (std::nothrow) char[CERTIFICATE_SIZE]);
            }
            if (!tmp_) {
                print("Error while preparing to store enterprise credentials.\r\n\r\n");
                return;
            }

            if (eap_type == 1) {
                // EAP-TLS
                // Required:
                // - client certificate
                // - private key
                // Optional:
                // - root CA
                // - outer identity
                credentials.setEapType(WLAN_EAP_TYPE_TLS);

                memset(tmp_.get(), 0, CERTIFICATE_SIZE);
                print("Client certificate in PEM format:\r\n");
                read_multiline((char*)tmp_.get(), CERTIFICATE_SIZE - 1);
                {
                    uint8_t* der = NULL;
                    size_t der_len = 0;
                    if (!mbedtls_x509_crt_pem_to_der((const char*)tmp_.get(), strnlen(tmp_.get(), CERTIFICATE_SIZE - 1) + 1, &der, &der_len)) {
                        credentials.setClientCertificate(der, der_len);
                        free(der);
                    }
                }

                memset(tmp_.get(), 0, CERTIFICATE_SIZE);
                print("Private key in PEM format:\r\n");
                read_multiline((char*)tmp_.get(), PRIVATE_KEY_SIZE - 1);
                {
                    uint8_t* der = NULL;
                    size_t der_len = 0;
                    if (!mbedtls_pk_pem_to_der((const char*)tmp_.get(), strnlen(tmp_.get(), PRIVATE_KEY_SIZE - 1) + 1, &der, &der_len)) {
                        credentials.setPrivateKey(der, der_len);
                        free(der);
                    }
                }
            } else {
                // PEAP/MSCHAPv2
                // Required:
                // - inner identity
                // - password
                // Optional:
                // - root CA
                // - outer identity
                credentials.setEapType(WLAN_EAP_TYPE_PEAP);

                memset(tmp_.get(), 0, CERTIFICATE_SIZE);
                print("Username: "******"Password: "******"Outer identity (optional): ");
            read_line((char*)tmp_.get(), 64);
            if (strlen(tmp_.get())) {
                credentials.setOuterIdentity((const char*)tmp_.get());
            }

            memset(tmp_.get(), 0, CERTIFICATE_SIZE);
            print("Root CA in PEM format (optional):\r\n");
            read_multiline((char*)tmp_.get(), CERTIFICATE_SIZE - 1);
            if (strlen(tmp_.get()) && !is_empty(tmp_.get())) {
                uint8_t* der = NULL;
                size_t der_len = 0;
                if (!mbedtls_x509_crt_pem_to_der((const char*)tmp_.get(), strnlen(tmp_.get(), CERTIFICATE_SIZE - 1) + 1, &der, &der_len)) {
                    credentials.setRootCertificate(der, der_len);
                    free(der);
                }
            }
            tmp_.reset();
        }
#endif

        print("Thanks! Wait "
#if PLATFORM_ID<3
    "about 7 seconds "
#endif
            "while I save those credentials...\r\n\r\n");
        creds = credentials.getHalCredentials();
        if (this->config.connect_callback2(this->config.connect_callback_data, &creds, false)==0)
        {
            print("Awesome. Now we'll connect!\r\n\r\n");
            print("If you see a pulsing cyan light, your "
    #if PLATFORM_ID==0
                "Spark Core"
    #else
                "device"
    #endif
                "\r\n");
            print("has connected to the Cloud and is ready to go!\r\n\r\n");
            print("If your LED flashes red or you encounter any other problems,\r\n");
            print("visit https://www.particle.io/support to debug.\r\n\r\n");
            print("    Particle <3 you!\r\n\r\n");
        }
        else
        {
            print("Derp. Sorry, we couldn't save the credentials.\r\n\r\n");
        }
        cleanup();
    }