Maxim Integrated / Mbed OS MAXREFDES155#

Dependencies:   MaximInterface

WifiConnectWindow.cpp

Committer:
IanBenzMaxim
Date:
2017-04-06
Revision:
8:a0d75dff3c9b
Parent:
7:66c5dedc750b
Child:
13:6a6225690c2e

File content as of revision 8:a0d75dff3c9b:

/*******************************************************************************
* 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[] = "MAXREFDES155";
static const char wifiPassword[] = "maxim1234";
static const char serverAddress[] = "www.maxim-security.com";
static const uint16_t serverPort = 3900;

WifiConnectWindow::WifiConnectWindow() : m_state(NotStarted)
{
    m_description.setParent(this);
    m_description.setText("WiFi SSID: " + std::string(wifiSsid) + "\nWiFi Password: " + std::string(wifiPassword));
    m_description.setWordWrap(true);
    m_backButton.setParent(this);
    m_backButton.setText("Back");
    m_backButton.setClickedHandler(Button::EventHandler(this, &WifiConnectWindow::backButtonClicked));
    m_continueButton.setParent(this);
    m_continueButton.setText("Connect to WiFi");
    m_continueButton.setClickedHandler(Button::EventHandler(this, &WifiConnectWindow::continueButtonClicked));
    m_continueButton.setFocused();
}

void WifiConnectWindow::updated()
{    
    switch (m_state)
    {
    case NotStarted:
        break;
        
    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();
                    std::auto_ptr<Window> window(new ErrorWindow("WiFi connect failed"));
                    windowManager()->push(window);
                }
                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();
                    std::auto_ptr<Window> window(new ErrorWindow("Hostname lookup failed"));
                    windowManager()->push(window);
                }
                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();
                    std::auto_ptr<Window> window(new ErrorWindow("Socket connect failed"));
                    windowManager()->push(window);
                }
                break;
            }
            
            if (windowManager() != NULL)
            {
                windowManager()->pop();
                std::auto_ptr<Window> window(new NormalOperationWindow(socket));
                windowManager()->push(window);
            }
        }
        break;
    }
}

void WifiConnectWindow::resized()
{
    m_backButton.resize(m_backButton.preferredWidth(), m_backButton.preferredHeight());
    m_backButton.move(0, height() - m_backButton.height());
    m_continueButton.resize(m_continueButton.preferredWidth(), m_continueButton.preferredHeight());
    m_continueButton.move(width() - m_continueButton.width(), height() - m_continueButton.height());
    m_description.resize(width(), 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();
        std::auto_ptr<Window> window(new DisplayIdWindow(DisplayIdWindow::PreConnectMode));
        windowManager()->push(window);
    }
}