ST / Mbed 2 deprecated TLS_HelloWorld_IDW01M1

Dependencies:   NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

Fork of HelloWorld_IDW01M1v2 by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* SpwfInterface NetworkSocketAPI Example Program
00002  * Copyright (c) 2015 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "SpwfInterface.h"
00019 #include "TCPSocket.h"
00020 
00021 //www.mbed.com CA certificate in PEM format
00022 char  CA_cert []="-----BEGIN CERTIFICATE-----\r\n"
00023 "MIIDVDCCAjygAwIBAgIDAjRWMA0GCSqGSIb3DQEBBQUAMEIxCzAJBgNVBAYTAlVT\r\n"
00024 "MRYwFAYDVQQKEw1HZW9UcnVzdCBJbmMuMRswGQYDVQQDExJHZW9UcnVzdCBHbG9i\r\n"
00025 "YWwgQ0EwHhcNMDIwNTIxMDQwMDAwWhcNMjIwNTIxMDQwMDAwWjBCMQswCQYDVQQG\r\n"
00026 "EwJVUzEWMBQGA1UEChMNR2VvVHJ1c3QgSW5jLjEbMBkGA1UEAxMSR2VvVHJ1c3Qg\r\n"
00027 "R2xvYmFsIENBMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA2swYYzD9\r\n"
00028 "9BcjGlZ+W988bDjkcbd4kdS8odhM+KhDtgPpTSEHCIjaWC9mOSm9BXiLnTjoBbdq\r\n"
00029 "fnGk5sRgprDvgOSJKA+eJdbtg/OtppHHmMlCGDUUna2YRpIuT8rxh0PBFpVXLVDv\r\n"
00030 "iS2Aelet8u5fa9IAjbkU+BQVNdnARqN7csiRv8lVK83Qlz6cJmTM386DGXHKTubU\r\n"
00031 "1XupGc1V3sjs0l44U+VcT4wt/lAjNvxm5suOpDkZALeVAjmRCw7+OC7RHQWa9k0+\r\n"
00032 "bw8HHa8sHo9gOeL6NlMTOdReJivbPagUvTLrGAMoUgRx5aszPeE4uwc2hGKceeoW\r\n"
00033 "MPRfwCvocWvk+QIDAQABo1MwUTAPBgNVHRMBAf8EBTADAQH/MB0GA1UdDgQWBBTA\r\n"
00034 "ephojYn7qwVkDBF9qn1luMrMTjAfBgNVHSMEGDAWgBTAephojYn7qwVkDBF9qn1l\r\n"
00035 "uMrMTjANBgkqhkiG9w0BAQUFAAOCAQEANeMpauUvXVSOKVCUn5kaFOSPeCpilKIn\r\n"
00036 "Z57QzxpeR+nBsqTP3UEaBU6bS+5Kb1VSsyShNwrrZHYqLizz/Tt1kL/6cdjHPTfS\r\n"
00037 "tQWVYrmm3ok9Nns4d0iXrKYgjy6myQzCsplFAMfOEVEiIuCl6rYVSAlk6l5PdPcF\r\n"
00038 "PseKUgzbFbS9bZvlxrFUaKnjaZC2mqUPuLk/IH2uSrW4nOQdtqvmlKXBx4Ot2/Un\r\n"
00039 "hw4EbNX/3aBd7YdStysVAq45pmp06drE57xNNB6pXE0zX5IJL4hmXXeXxx12E6nV\r\n"
00040 "5fEWCRE11azbJHFwLJhWC9kXtNHjUStedejV0NxPNO3CBWaAocvmMw==\r\n"
00041 "-----END CERTIFICATE-----\r\n";
00042 
00043 
00044 //------------------------------------
00045 // Hyperterminal configuration
00046 // 9600 bauds, 8-bit data, no parity
00047 //------------------------------------
00048 
00049 Serial pc(USBTX, USBRX);
00050 DigitalOut myled(LED1);
00051 
00052 /*************************************
00053 //FRDM-K64: D9->UART1_TX, D7->UART1_RX
00054 Pin connections:
00055     FRDM      IDW01M1
00056    ------    ---------
00057     +3v3 <--> +3v3
00058     GND  <--> GND
00059     D9   <--> D8
00060     D7   <--> D2
00061 
00062 SpwfSAInterface spwf(D9, D7, false);
00063 *************************************/
00064 /*************************************
00065 //LPCXpresso11U68: D9->UART1_TX, D7->UART1_RX
00066 Pin connections:
00067     LPC      IDW01M1
00068    ------    ---------
00069     +3v3 <--> +3v3
00070     GND  <--> GND
00071     A1   <--> D8
00072     A2   <--> D2
00073 
00074 SpwfSAInterface spwf(A1, A2, false);
00075 *************************************/
00076 
00077 //NUCLEO: D8->UART1_TX (PA_9), D2->UART1_RX (PA_10)
00078 SpwfSAInterface spwf(D8, D2, false);
00079 
00080 int main() {
00081     int err;    
00082     char * ssid = "STM";
00083     char * seckey = "STMDemo";  
00084       
00085     pc.printf("\r\nTLS_HelloWorld mbed Application\r\n");     
00086     pc.printf("\r\nconnecting to AP\r\n");
00087             
00088     if(spwf.connect(ssid, seckey, NSAPI_SECURITY_WPA2)) {      
00089         pc.printf("\r\nnow connected\r\n");
00090     } else {
00091         pc.printf("\r\nerror connecting to AP.\r\n");
00092         return -1;
00093     }   
00094 
00095 // Start Secure Socket connection test (one way server autentication only)   
00096     TCPSocket socket(&spwf);
00097     time_t ctTime;
00098     ctTime = time(NULL);             
00099     printf ("Start Secure Socket connection with one way server autentication test\n\r");                
00100     printf("Initial System Time is: %s\r\n", ctime(&ctTime));   
00101     printf("Need to adjust time? if yes enter time in seconds elapsed since Epoch (cmd: date +'%%s'), otherwise enter 0 ");                
00102     int t=0;
00103     scanf("%d",&t);
00104     printf ("entered time is: %d \n\r", t);
00105     if (t != 0) { time_t txTm = t; set_time(txTm); }  // set Nucleo system time
00106     ctTime = time(NULL);    
00107     printf ("The current system time is: %s", ctime (&ctTime));  // set WiFi module systm time
00108     if (!spwf.set_time(ctTime)) printf ("ERROR set_time\n\r");       
00109     if (!spwf.clean_TLS_certificate(ALL)) printf ("ERROR clean_TLS_certificate\n\r");
00110     if (!spwf.set_TLS_certificate(CA_cert, sizeof(CA_cert), FLASH_CA_ROOT_CERT)) printf ("ERROR set_TLS_certificate\n\r");
00111     if (!spwf.set_TLS_SRV_domain("*.mbed.com",FLASH_DOMAIN)) printf ("ERROR set_TLS_CA_domain\n\r");
00112 // NOTE:  the Wifi API set_secure_mode() and set_unsecure_mode() refers to the whole interface so the socket must be created / opened after 
00113 // having set the desidered mode (deft unsecure).
00114 //#define SEC_MODE1 // two possible ways to secure connect a socket are shown 
00115     SocketAddress addr(&spwf, "");
00116     spwf.gethostbyname(&addr,"www.mbed.com");  // ask DNS to resolve URL/IP 
00117     addr.set_port(443);     
00118     spwf.set_secure_mode();           
00119     socket.open(&spwf);
00120     spwf.set_unsecure_mode();    
00121     printf ("Connecting to www.mbed.com IP: %s ...\n\r", addr.get_ip_address());
00122 #ifdef SEC_MODE1    
00123     err = socket.connect(addr);
00124 #else
00125     err = socket.connect("www.mbed.com", 443);
00126 #endif    
00127     if (err != 0 )printf ("ERROR secure socket connection failed: %d\n\r", err);
00128     else printf ("--->>> Secure socket CONNECTED to: %s\n\r", addr.get_ip_address());
00129     socket.close();      
00130     printf ("Socket closed\n\r");
00131     spwf.disconnect();
00132     printf ("WIFI disconnected, exiting ...\n\r");
00133 
00134     while(1) { 
00135       wait(1);
00136       myled = !myled;
00137     }
00138 }