Exemplo n.º 1
0
 internal static bool IsSetupAP(APDetails ap)
 {
     if (ap == null)
     {
         return(false);
     }
     return(ap == APDetails.SetupSecretAPNew || ap == APDetails.SetupSecretAP || ap == APDetails.SetupOpenAP);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Should be called by an application when the home network credentials should be forgotten, e.g. after a reset button is held down.
        /// </summary>
        public void ResetNetworkCredentials()
        {
            Debug.Print("Forgetting home wireless network credentials");

            wifiNetworks.Clear();
            if (EnableWifiSetupNetworks)
            {
                APDetails.AddSetupAPs(wifiNetworks);
            }
            SaveData();

            WebServer.StopLocalServer();
            currentAP = null;
            SetLed();
            wifi.Interface.Disconnect();
        }
Exemplo n.º 3
0
        private void LoadData()
        {
            ConfigEWR          = ExtendedWeakReference.RecoverOrCreate(typeof(Config), 0, ExtendedWeakReference.c_SurviveBoot | ExtendedWeakReference.c_SurvivePowerdown);
            ConfigEWR.Priority = (int)ExtendedWeakReference.PriorityLevel.Critical;

            wifiNetworks.Clear();
            if (EnableWifiSetupNetworks)
            {
                APDetails.AddSetupAPs(wifiNetworks);
            }

            Config config = ConfigEWR.Target as Config;

            if (config == null)
            {
                config          = new Config();
                config.UniqueID = "";
                var random = new Random();
                for (int i = 0; i < 20; i++)
                {
                    config.UniqueID += random.Next(10).ToString();
                }
                ConfigEWR.Target = config;
                Debug.Print("Created new configuration in flash");
            }
            else
            {
                if (config.APDetails != null)
                {
                    for (var i = 0; i < config.APDetails.Length; i++)
                    {
                        wifiNetworks.Insert(i, config.APDetails[i]);
                        Debug.Print("Loaded WiFi config for SSID " + config.APDetails[i].SSID);
                    }
                }
            }
            DeviceUniqueID = config.UniqueID;
            Debug.Print("Device unique ID is " + DeviceUniqueID);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Causes the led to be controlled, abstractly showing the current WiFi status.  The screen is not touched if the DoNotControlLed delegate returns true.
        /// This method is called internally by the library, but is exposed for use when the conditions for DoNotControlLed stop, i.e. when control is
        /// returned to this library from the application.
        /// </summary>
        public void SetLed()
        {
            if (led == null)
            {
                return;
            }
            if (DoNotControlLed())
            {
                return;
            }

            if (currentAP == null)
            {
                // no suitable AP seen
                if (WiFiCredentialsKnown)
                {
                    led.FadeRepeatedly(GT.Color.Blue);
                }
                else
                {
                    led.FadeRepeatedly(GT.Color.Red);
                }
            }
            else if (APDetails.IsSetupAP(currentAP))
            {
                if (wifiConnected)
                {
                    // joined setup AP
                    if (WiFiCredentialsKnown)
                    {
                        led.TurnBlue();
                    }
                    else
                    {
                        led.TurnRed();
                    }
                }
                else
                {
                    // trying to join setup AP
                    if (WiFiCredentialsKnown)
                    {
                        led.BlinkRepeatedly(GT.Color.Blue, blinkTime, GT.Color.Black, blinkTime);
                    }
                    else
                    {
                        led.BlinkRepeatedly(GT.Color.Red, blinkTime, GT.Color.Black, blinkTime);
                    }
                }
            }
            else
            {
                if (wifiConnected)
                {
                    // joined a real AP
                    led.TurnGreen();
                }
                else
                {
                    // trying to join a real AP
                    led.BlinkRepeatedly(GT.Color.Green, blinkTime, GT.Color.Black, blinkTime);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Causes the screen to be redrawn, showing the current WiFi status.  The screen is not touched if the DoNotControlScreen delegate returns true.
        /// This method is called internally by the library, but is exposed for use when the conditions for DoNotControlScreen stop, i.e. when control is
        /// returned to this library from the application.
        /// </summary>
        public void SetScreen()
        {
            if (display == null)
            {
                return;
            }
            if (DoNotControlScreen())
            {
                return;
            }

            //GT.Color border = GT.Color.Green; // can press button
            string text1 = "", text2 = "", text3 = "";

            //Bitmap image;
            if (currentAP == null)
            {
                // no suitable AP seen
                if (WiFiCredentialsKnown)
                {
                    //image = uiScanningForHomeAP;
                    text1 = "Searching for";
                    text2 = "home AP";
                }
                else
                {
                    if (EnableWifiSetupNetworks)
                    {
                        //image = uiScanningForSetupAP;
                        text1 = "Searching for";
                        text2 = "setup AP";
                    }
                    else
                    {
                        text1 = "Waiting for";
                        text2 = "credentials";
                    }
                }
            }
            else if (APDetails.IsSetupAP(currentAP))
            {
                if (wifiConnected)
                {
                    // joined setup AP
                    if (WiFiCredentialsKnown)
                    {
                        //image = uiReceivedHomeAPDetails;
                        text1 = "Got credentials";
                        text2 = "for AP:";
                        text3 = ((APDetails)wifiNetworks[0]).SSID;
                    }
                    else
                    {
                        //image = uiJoinedSetupAP;
                        text1 = "Joined";
                        text2 = "setup AP";
                    }
                }
                else
                {
                    //image = uiJoiningSetupNetwork;
                    text1 = "Joining";
                    text2 = "setup AP";
                    //border = GT.Color.Gray;
                }
            }
            else
            {
                if (wifiConnected)
                {
                    //image = uiJoinedHomeAP;
                    // joined a real AP
                    text1 = "Joined AP:";
                    text2 = currentAP.SSID;
                }
                else
                {
                    // trying to join a real AP
                    //image = uiJoiningHomeNetwork;
                    text1 = "Joining AP:";
                    text2 = currentAP.SSID;
                    //border = GT.Color.Gray;
                }
            }
            //oledDisplay.SimpleGraphics.DisplayRectangle(border, 10, GT.Color.Black, 0, 0, 128, 128);
            display.SimpleGraphics.Clear();
            display.SimpleGraphics.DisplayText(text1, font, GT.Color.White, 20, 20);
            if (text2 != "")
            {
                display.SimpleGraphics.DisplayText(text2, font, GT.Color.White, 20, 50);
            }
            if (text3 != "")
            {
                display.SimpleGraphics.DisplayText(text3, font, GT.Color.White, 20, 80);
            }
            //oledDisplay.SimpleGraphics.DisplayImage(image, 0, 0);
            display.SimpleGraphics.Redraw();
        }
Exemplo n.º 6
0
        void wifiTimer_Tick(GT.Timer timer)
        {
            Debug.Print("wifiTimer start - currentAP " + (currentAP != null ? currentAP.SSID : "(null)") + " wifiUp " + wifiConnected + (wifiConnected ? " IP " + wifi.Interface.NetworkInterface.IPAddress : ""));
            if (sp != null)
            {
                var spMessage = BroadcastMessageWithNewline;
                sp.Write(spMessage, 0, spMessage.Length);
            }
            if (SkipWifiTimer())
            {
                Debug.Print("aborting wifitimer tick since SkipWifiTimer delegate is true");
                return;
            }

            if (wifiConnected && currentAP == null)
            {
                Debug.Print("ERROR: wifi is up but current AP is null! Disconnecting.");
                currentAP     = null;
                wifiConnected = false;
                SetLed();
                SetScreen();
                wifi.Interface.Disconnect();
                WebServer.StopLocalServer();
                return;
            }

            if (currentAP != null && !wifiConnected)
            {
                wifiUpTime++;
                if (wifiUpTime == wifiUpTimeout)
                {
                    Debug.Print("WARN: starting wifi for " + currentAP.SSID + " timeout - resetting currentAP");
                    currentAP     = null;
                    wifiConnected = false;
                    SetLed();
                    SetScreen();
                    wifi.Interface.Disconnect();
                    WebServer.StopLocalServer();
                    return;
                }
            }

            if (wifiConnected && currentAP != null && APDetails.IsSetupAP(currentAP))
            {
                if (GT.Timer.GetMachineTime() - joinTime > maxTimeOnSetupNetwork)
                {
                    Debug.Print("Disconnecting from setup AP due to timeout (setup should not take this long)");
                    currentAP     = null;
                    wifiConnected = false;
                    SetLed();
                    SetScreen();
                    wifi.Interface.Disconnect();
                    WebServer.StopLocalServer();
                    return;
                }
                else
                {
                    // only sending unsolicited beacons when we're not on the home network
                    Debug.Print("Sending unsolicited UDP beacon since we're on a setup network");
                    SendUDPBroadcast(BeaconPort);
                }
            }

            if (wifiConnected && currentAP != null)
            {
                if (!WebServer.IsRunning())
                {
                    WebServer.StartLocalServer(wifi.Interface.NetworkInterface.IPAddress, 80);
                }
            }

            // skip scanning when connected since it fails
            if (currentAP == null)
            {
                try
                {
                    var scan = wifi.Interface.Scan();
                    Thread.Sleep(1);
                    foreach (var ap in scan)
                    {
                        Debug.Print("Scanned AP " + ap.SSID + " rssi " + ap.RSSI);
                    }
                    if (scan != null)
                    {
                        foreach (APDetails knownAP in wifiNetworks)
                        {
                            // APs are in priority order, so break once we see the current ap
                            if (knownAP.Matches(currentAP))
                            {
                                break;
                            }
                            WiFiNetworkInfo joinAP = null;
                            foreach (var scanAP in scan)
                            {
                                if (!knownAP.Matches(scanAP))
                                {
                                    continue;
                                }
                                if (joinAP == null)
                                {
                                    joinAP = scanAP;
                                }
                                if (joinAP.RSSI > scanAP.RSSI)
                                {
                                    joinAP = scanAP;                            // lower RSSI is better
                                }
                            }
                            if (joinAP == null)
                            {
                                continue;
                            }

                            try
                            {
                                if (currentAP != null)
                                {
                                    Debug.Print("Disconnecting from WiFi network " + currentAP + " to join " + joinAP.SSID);
                                    wifi.Interface.Disconnect();
                                }

                                //// stop pictimeout and any streaming since wifi join operation hogs processor
                                //picTimeout.Stop();
                                //camera.StopStreamingBitmaps();

                                joinTime = GT.Timer.GetMachineTime();
                                Debug.Print("Joining WiFi network " + joinAP.SSID + " rssi " + joinAP.RSSI);
                                //wifi.Interface.();
                                currentAP = joinAP;
                                SetLed();
                                SetScreen();
                                wifi.Interface.Join(joinAP, knownAP.Key);
                                //Debug.Print("Joined WiFi network " + scanAP.SSID);
                                wifiUpTime = 0;
                                break;
                            }
                            catch
                            {
                                Debug.Print("Error joining wifi network: " + joinAP.SSID);
                                wifi.Interface.Disconnect();
                                timer.Stop();
                                timer.Start();
                                currentAP = null;
                                SetLed();
                                SetScreen();
                                break;
                            }
                        }
                    }
                }
                catch (GHI.Premium.Net.NetworkInterfaceExtensionException wex)
                {
                    wifi.Interface.Disconnect();
                    // restart the timer to prevent it immediately firing again
                    timer.Stop();
                    timer.Start();
                    Debug.Print("Error scanning wifi networks: " + wex);
                    currentAP = null;
                    setLedError();
                    setScreenError();
                }
            }
            Debug.Print("wifiTimer end - currentAP " + (currentAP != null ? currentAP.SSID : "(null)") + " wifiUp " + wifiConnected);
        }
 internal static bool IsSetupAP(APDetails ap)
 {
     if (ap == null) return false;
     return ap == APDetails.SetupSecretAPNew || ap == APDetails.SetupSecretAP || ap == APDetails.SetupOpenAP;
 }