DeepCover Embedded Security in IoT: Public-key Secured Data Paths

Dependencies:   MaximInterface

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers WifiConnectWindow.cpp Source File

WifiConnectWindow.cpp

00001 /*******************************************************************************
00002 * Copyright (C) Maxim Integrated Products, Inc., All Rights Reserved.
00003 *
00004 * Permission is hereby granted, free of charge, to any person obtaining a
00005 * copy of this software and associated documentation files (the "Software"),
00006 * to deal in the Software without restriction, including without limitation
00007 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008 * and/or sell copies of the Software, and to permit persons to whom the
00009 * Software is furnished to do so, subject to the following conditions:
00010 *
00011 * The above copyright notice and this permission notice shall be included
00012 * in all copies or substantial portions of the Software.
00013 *
00014 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020 * OTHER DEALINGS IN THE SOFTWARE.
00021 *
00022 * Except as contained in this notice, the name of Maxim Integrated
00023 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024 * Products, Inc. Branding Policy.
00025 *
00026 * The mere transfer of this software does not imply any licenses
00027 * of trade secrets, proprietary technology, copyrights, patents,
00028 * trademarks, maskwork rights, or any other form of intellectual
00029 * property whatsoever. Maxim Integrated Products, Inc. retains all
00030 * ownership rights.
00031 *******************************************************************************/
00032 
00033 #include <memory>
00034 #include <mbed-os/features/netsocket/TCPSocket.h>
00035 #include <mbed-os/platform/mbed_wait_api.h>
00036 #include "CC3100.hpp"
00037 #include "DisplayIdWindow.hpp"
00038 #include "ErrorWindow.hpp"
00039 #include "MakeFunction.hpp"
00040 #include "NormalOperationWindow.hpp"
00041 #include "WifiConnectWindow.hpp"
00042 #include "WindowManager.hpp"
00043 
00044 static const char wifiSsid[] = "MAXREFDES155";
00045 static const char wifiPassword[] = "maxim1234";
00046 static const char serverAddress[] = "www.maxim-security.com";
00047 static const uint16_t serverPort = 3900;
00048 
00049 WifiConnectWindow::WifiConnectWindow() : state(NotStarted) {
00050   description.setParent(this);
00051   description.setText("WiFi SSID: " + std::string(wifiSsid) +
00052                       "\nWiFi Password: " + std::string(wifiPassword));
00053   description.setWordWrap(true);
00054   backButton.setParent(this);
00055   backButton.setText("Back");
00056   backButton.setClickedHandler(
00057       makeFunction(this, &WifiConnectWindow::backButtonClicked));
00058   continueButton.setParent(this);
00059   continueButton.setText("Connect to WiFi");
00060   continueButton.setClickedHandler(
00061       makeFunction(this, &WifiConnectWindow::continueButtonClicked));
00062   continueButton.setFocused();
00063 }
00064 
00065 void WifiConnectWindow::updated() {
00066   switch (state) {
00067   case NotStarted:
00068     break;
00069 
00070   case PreConnect:
00071     backButton.setParent (NULL);
00072     continueButton.setParent (NULL);
00073     description.setText("Connecting to WiFi network...");
00074     state = WiFiConnect;
00075     break;
00076 
00077   case WiFiConnect:
00078     // Connect to AP.
00079     if (CC3100::instance().connect(wifiSsid, wifiPassword,
00080                                    NSAPI_SECURITY_WPA2) != 0) {
00081       if (windowManager()) {
00082         std::auto_ptr<Window> window(new ErrorWindow("WiFi connect failed"));
00083         windowManager()->push(window);
00084       }
00085       break;
00086     }
00087 
00088     description.setText("Connecting to server...");
00089     state = ServerConnect;
00090     break;
00091 
00092   case ServerConnect: {
00093     // Get IP address from DNS lookup.
00094     SocketAddress socketAddress;
00095     if (CC3100::instance().gethostbyname(serverAddress, &socketAddress) != 0) {
00096       if (windowManager()) {
00097         std::auto_ptr<Window> window(new ErrorWindow("Hostname lookup failed"));
00098         windowManager()->push(window);
00099       }
00100       break;
00101     }
00102 
00103     // Open socket connection.
00104     std::auto_ptr<TCPSocket> socket(new TCPSocket(&CC3100::instance()));
00105     socket->set_blocking(false);
00106     socketAddress.set_port(serverPort);
00107     if (socket->connect(socketAddress) != 0) {
00108       if (windowManager()) {
00109         std::auto_ptr<Window> window(new ErrorWindow("Socket connect failed"));
00110         windowManager()->push(window);
00111       }
00112       break;
00113     }
00114 
00115     if (windowManager()) {
00116       windowManager()->pop();
00117       std::auto_ptr<Window> window(new NormalOperationWindow(socket));
00118       windowManager()->push(window);
00119     }
00120     break;
00121   }
00122   }
00123 }
00124 
00125 void WifiConnectWindow::resized() {
00126   backButton.resize(backButton.preferredWidth(),
00127                     backButton.preferredHeight());
00128   backButton.move(0, height() - backButton.height());
00129   continueButton.resize(continueButton.preferredWidth(),
00130                         continueButton.preferredHeight());
00131   continueButton.move(width() - continueButton.width(),
00132                       height() - continueButton.height());
00133   description.resize(width(),
00134                      std::min(backButton.y(), continueButton.y()));
00135 }
00136 
00137 bool WifiConnectWindow::doProcessKey(Key key) {
00138   bool handled;
00139   switch (key) {
00140   case LeftKey:
00141     backButton.setFocused();
00142     handled = true;
00143     break;
00144 
00145   case RightKey:
00146     continueButton.setFocused();
00147     handled = true;
00148     break;
00149 
00150   default:
00151     handled = false;
00152     break;
00153   }
00154   return handled;
00155 }
00156 
00157 void WifiConnectWindow::continueButtonClicked(Button *) {
00158   if (state == NotStarted) {
00159     state = PreConnect;
00160   }
00161 }
00162 
00163 void WifiConnectWindow::backButtonClicked(Button *) {
00164   if (windowManager()) {
00165     windowManager()->pop();
00166     std::auto_ptr<Window> window(
00167         new DisplayIdWindow(DisplayIdWindow::PreConnectMode));
00168     windowManager()->push(window);
00169   }
00170 }