Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MaximInterface
Diff: WifiConnectWindow.cpp
- Revision:
- 0:33d4e66780c0
- Child:
- 3:d2799d8497c0
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WifiConnectWindow.cpp	Fri Feb 24 11:23:12 2017 -0600
@@ -0,0 +1,175 @@
+/*******************************************************************************
+* Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
+*
+* Permission is hereby granted, free of charge, to any person obtaining a
+* copy of this software and associated documentation files (the "Software"),
+* to deal in the Software without restriction, including without limitation
+* the rights to use, copy, modify, merge, publish, distribute, sublicense,
+* and/or sell copies of the Software, and to permit persons to whom the
+* Software is furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included
+* in all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+* IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
+* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+* OTHER DEALINGS IN THE SOFTWARE.
+*
+* Except as contained in this notice, the name of Maxim Integrated
+* Products, Inc. shall not be used except as stated in the Maxim Integrated
+* Products, Inc. Branding Policy.
+*
+* The mere transfer of this software does not imply any licenses
+* of trade secrets, proprietary technology, copyrights, patents,
+* trademarks, maskwork rights, or any other form of intellectual
+* property whatsoever. Maxim Integrated Products, Inc. retains all
+* ownership rights.
+*******************************************************************************/
+
+#include <memory>
+#include <wait_api.h>
+#include <TCPSocket.h>
+#include "CC3100.hpp"
+#include "WindowManager.hpp"
+#include "DisplayIdWindow.hpp"
+#include "NormalOperationWindow.hpp"
+#include "ErrorWindow.hpp"
+#include "WifiConnectWindow.hpp"
+
+static const char wifiSsid[] = "MXIM_SECURITY_GRP";
+static const char wifiPassword[] = "DEEPCOVER";
+static const char serverAddress[] = "www.maxim-security.com";
+static const uint16_t serverPort = 3900;
+
+WifiConnectWindow::WifiConnectWindow(Graphic * parent) : Window(parent), m_state(NotStarted),
+    m_description("WiFi SSID: " + std::string(wifiSsid) + "\nWiFi Pass: " + std::string(wifiPassword), this),
+    m_backButton("Back", this), m_continueButton("Connect to WiFi", this)
+{
+    m_description.setWordWrap(true);
+    m_backButton.setClickedHandler(Button::EventHandler(this, &WifiConnectWindow::backButtonClicked));
+    m_continueButton.setClickedHandler(Button::EventHandler(this, &WifiConnectWindow::continueButtonClicked));
+    m_continueButton.setFocused();
+}
+
+void WifiConnectWindow::doPostLayout()
+{    
+    switch (m_state)
+    {
+    case PreConnect:
+        m_backButton.setParent(NULL);
+        m_continueButton.setParent(NULL);
+        m_description.setText("Connecting to WiFi network...");
+        m_state = WiFiConnect;
+        break;
+        
+    case WiFiConnect:
+        {
+            // Connect to AP.
+            int result = CC3100::instance().connect(wifiSsid, wifiPassword, NSAPI_SECURITY_WPA2);
+            if (result != 0)
+            {
+                if (windowManager() != NULL)
+                {
+                    windowManager()->pop();
+                    windowManager()->push(new ErrorWindow("WiFi connect failed"));
+                }
+                break;
+            }
+            
+            m_description.setText("Connecting to server...");
+            m_state = ServerConnect;
+        }
+        break;
+        
+    case ServerConnect:
+        {
+            // Get IP address from DNS lookup.
+            SocketAddress socketAddress;
+            int result = CC3100::instance().gethostbyname(serverAddress, &socketAddress);
+            if (result != 0)
+            {
+                if (windowManager() != NULL)
+                {
+                    windowManager()->pop();
+                    windowManager()->push(new ErrorWindow("Hostname lookup failed"));
+                }
+                break;
+            }
+            
+            // Open socket connection.
+            std::auto_ptr<TCPSocket> socket(new TCPSocket(&CC3100::instance()));
+            socket->set_blocking(false);
+            socketAddress.set_port(serverPort);
+            result = socket->connect(socketAddress);
+            if (result != 0)
+            {
+                if (windowManager() != NULL)
+                {
+                    windowManager()->pop();
+                    windowManager()->push(new ErrorWindow("Socket connect failed"));
+                }
+                break;
+            }
+            
+            if (windowManager() != NULL)
+            {
+                windowManager()->pop();
+                windowManager()->push(new NormalOperationWindow(socket.release()));
+            }
+        }
+        break;
+    }
+}
+
+void WifiConnectWindow::doLayout()
+{
+    m_backButton.setX(0);
+    m_backButton.setY(height() - m_backButton.height());
+    m_continueButton.setX(width() - m_continueButton.width());
+    m_continueButton.setY(height() - m_continueButton.height());
+    m_description.setWidth(width());
+    m_description.setHeight(std::min(m_backButton.y(), m_continueButton.y()));
+}
+
+bool WifiConnectWindow::doProcessKey(Key key)
+{
+    bool handled;
+    switch (key)
+    {
+    case LeftKey:
+        m_backButton.setFocused();
+        handled = true;
+        break;
+        
+    case RightKey:
+        m_continueButton.setFocused();
+        handled = true;
+        break;
+        
+    default:
+        handled = false;
+        break;
+    }
+    return handled;
+}
+
+void WifiConnectWindow::continueButtonClicked()
+{
+    if (m_state == NotStarted)
+    {
+        m_state = PreConnect;
+    }
+}
+
+void WifiConnectWindow::backButtonClicked()
+{
+    if (windowManager() != NULL)
+    {
+        windowManager()->pop();
+        windowManager()->push(new DisplayIdWindow(DisplayIdWindow::PreConnectMode));
+    }
+}