11/* 
22 *  Hello world example of a TLS client: fetch an HTTPS page 
33 * 
4-  *  Copyright (C) 2006-2015 , ARM Limited, All Rights Reserved 
4+  *  Copyright (C) 2006-2016 , ARM Limited, All Rights Reserved 
55 *  SPDX-License-Identifier: Apache-2.0 
66 * 
77 *  Licensed under the Apache License, Version 2.0 (the "License"); you may 
1919 *  This file is part of mbed TLS (https://tls.mbed.org) 
2020*/ 
2121
22- #if  !defined(TARGET_LIKE_MBED)
23- 
24- #include  < stdio.h> 
25- 
26- int  main () {
27-     printf (" This program only works on mbed OS.\n " 
28-     return  0 ;
29- }
30- 
31- #else 
32- 
3322/* * \file main.cpp
3423 *  \brief An example TLS Client application 
3524 *  This application sends an HTTPS request to developer.mbed.org and searches for a string in 
@@ -48,12 +37,9 @@ int main() {
4837
4938#include  " mbed.h" 
5039#include  " NetworkStack.h" 
51- #include  " LWIPInterface.h" 
5240
53- // #include "EthernetInterface.h"
41+ #include  " EthernetInterface.h" 
5442#include  " TCPSocket.h" 
55- #include  " test_env.h" 
56- // #include "lwipv4_init.h"
5743
5844#include  " mbedtls/platform.h" 
5945#include  " mbedtls/ssl.h" 
@@ -66,9 +52,6 @@ int main() {
6652
6753namespace  {
6854
69- Serial output (USBTX, USBRX);    
70- NetworkStack *network_stack = NULL ;
71-     
7255const  char  *HTTPS_SERVER_NAME = " developer.mbed.org" 
7356const  int  HTTPS_SERVER_PORT = 443 ;
7457const  int  RECV_BUFFER_SIZE = 600 ;
@@ -144,8 +127,6 @@ const char SSL_CA_PEM[] =
144127#endif 
145128}
146129
147- // using namespace mbed::Sockets::v0;
148- 
149130/* *
150131 * \brief HelloHTTPS implements the logic for fetching a file from a webserver 
151132 * using a TCP socket and parsing the result. 
@@ -159,7 +140,7 @@ class HelloHTTPS {
159140     * @param[in] domain The domain name to fetch from 
160141     * @param[in] port The port of the HTTPS server 
161142     */  
162-     HelloHTTPS (const  char  * domain, const  uint16_t  port) :
143+     HelloHTTPS (const  char  * domain, const  uint16_t  port, NetworkInterface *net_iface ) :
163144            _domain (domain), _port(port)
164145    {
165146
@@ -168,7 +149,7 @@ class HelloHTTPS {
168149        _got200 = false ;
169150        _bpos = 0 ;
170151        _request_sent = 0 ;
171-         _tcpsocket = new  TCPSocket (network_stack );
152+         _tcpsocket = new  TCPSocket (net_iface );
172153
173154        mbedtls_entropy_init (&_entropy);
174155        mbedtls_ctr_drbg_init (&_ctr_drbg);
@@ -258,11 +239,11 @@ class HelloHTTPS {
258239
259240
260241        /*  Connect to the server */ 
261-         output. printf (" Connecting with %s\r\n " 
242+         mbedtls_printf (" Connecting with %s\r\n " 
262243        _tcpsocket->connect ( _domain, _port );
263244
264245       /*  Start the handshake, the rest will be done in onReceive() */ 
265-         output. printf (" Starting the TLS handshake...\r\n " 
246+         mbedtls_printf (" Starting the TLS handshake...\r\n " 
266247        ret = mbedtls_ssl_handshake (&_ssl);
267248        if  (ret < 0 ) {
268249            if  (ret != MBEDTLS_ERR_SSL_WANT_READ &&
@@ -289,7 +270,7 @@ class HelloHTTPS {
289270        char  buf[1024 ];
290271        mbedtls_x509_crt_info (buf, sizeof (buf), " \r     " 
291272                        mbedtls_ssl_get_peer_cert (&_ssl));
292-         output. printf (" Server certificate:\r\n %s\r " 
273+         mbedtls_printf (" Server certificate:\r\n %s\r " 
293274
294275#if  defined(UNSAFE)
295276        uint32_t  flags = mbedtls_ssl_get_verify_result (&_ssl);
@@ -321,15 +302,14 @@ class HelloHTTPS {
321302        _gothello = _gothello || strstr (_buffer, HTTPS_HELLO_STR) != NULL ;
322303
323304        /*  Print status messages */ 
324-         output. printf (" HTTPS: Received %d chars from server\r\n " 
325-         output. printf (" HTTPS: Received 200 OK status ... %s\r\n " " [OK]" " [FAIL]" 
326-         output. printf (" HTTPS: Received '%s' status ... %s\r\n " " [OK]" " [FAIL]" 
327-         output. printf (" HTTPS: Received message:\r\n\r\n " 
328-         output. printf (" %s" 
305+         mbedtls_printf (" HTTPS: Received %d chars from server\r\n " 
306+         mbedtls_printf (" HTTPS: Received 200 OK status ... %s\r\n " " [OK]" " [FAIL]" 
307+         mbedtls_printf (" HTTPS: Received '%s' status ... %s\r\n " " [OK]" " [FAIL]" 
308+         mbedtls_printf (" HTTPS: Received message:\r\n\r\n " 
309+         mbedtls_printf (" %s" 
329310        _error = !(_got200 && _gothello);
330311
331312        _tcpsocket->close ();
332- //         MBED_HOSTTEST_RESULT(!error());
333313    }
334314    /* *
335315     * Check if the test has completed. 
@@ -360,7 +340,7 @@ class HelloHTTPS {
360340    static  void  print_mbedtls_error (const  char  *name, int  err) {
361341        char  buf[128 ];
362342        mbedtls_strerror (err, buf, sizeof  (buf));
363-         output. printf (" %s() failed: -0x%04x (%d): %s\r\n " 
343+         mbedtls_printf (" %s() failed: -0x%04x (%d): %s\r\n " 
364344    }
365345
366346#if  DEBUG_LEVEL > 0
@@ -381,7 +361,7 @@ class HelloHTTPS {
381361            }
382362        }
383363
384-         output. printf (" %s:%04d: |%d| %s" 
364+         mbedtls_printf (" %s:%04d: |%d| %s" 
385365    }
386366
387367    /* *
@@ -393,16 +373,16 @@ class HelloHTTPS {
393373        char  buf[1024 ];
394374        (void ) data;
395375
396-         output. printf (" \n Verifying certificate at depth %d:\n " 
376+         mbedtls_printf (" \n Verifying certificate at depth %d:\n " 
397377        mbedtls_x509_crt_info (buf, sizeof  (buf) - 1 , "   " 
398-         output. printf (" %s" 
378+         mbedtls_printf (" %s" 
399379
400380        if  (*flags == 0 )
401-             output. printf (" No verification issue for this certificate\n " 
381+             mbedtls_printf (" No verification issue for this certificate\n " 
402382        else 
403383        {
404384            mbedtls_x509_crt_verify_info (buf, sizeof  (buf), "   ! " 
405-             output. printf (" %s\n " 
385+             mbedtls_printf (" %s\n " 
406386        }
407387
408388        return  0 ;
@@ -447,17 +427,8 @@ class HelloHTTPS {
447427        printf (" MBED: Socket Error: %d\r\n " 
448428        s->close ();
449429        _error = true ;
450- //         MBED_HOSTTEST_RESULT(false);
451-     }
452- 
453- #if  0 
454-     void onDisconnect(TCPStream *s) {
455-         s->close();
456-         MBED_HOSTTEST_RESULT(!error());
457430    }
458431
459- #endif 
460- 
461432protected: 
462433    TCPSocket* _tcpsocket;
463434
@@ -483,34 +454,19 @@ class HelloHTTPS {
483454 */  
484455int  main () {
485456    /*  The default 9600 bps is too slow to print full TLS debug info and could
486-      * cause the other party to time out. Select a higher baud rate for 
487-      * printf(), regardless of debug level for the sake of uniformity. */  
488- 
489-     //  Sets the console baud-rate
490-     output.baud (115200 );
491- 
492- //     MBED_HOSTTEST_TIMEOUT(120);
493- //     MBED_HOSTTEST_SELECT(_default);
494- //     MBED_HOSTTEST_DESCRIPTION(mbed TLS example HTTPS client);
495- //     MBED_HOSTTEST_START("MBEDTLS_EX_HTTPS_CLIENT");
457+      * cause the other party to time out. */  
496458
497-     /*  Initialise with DHCP, connect, and start up the stack */ 
498-     LWIPInterface lwip;
499- 
500-     lwip.connect ();
501-     output.printf (" Using Ethernet LWIP\r\n " 
502-     network_stack = &lwip;
503- 
504-     const  char  *ip_addr = network_stack->get_ip_address ();
459+     /*  Inititalise with DHCP, connect, and start up the stack */ 
460+     EthernetInterface eth_iface;
461+     eth_iface.connect ();
462+     mbedtls_printf (" Using Ethernet LWIP\r\n " 
463+     const  char  *ip_addr = eth_iface.get_ip_address ();
505464    if  (ip_addr) {
506-         output. printf (" Client IP Address is %s\r\n " 
465+         mbedtls_printf (" Client IP Address is %s\r\n "   ip_addr);
507466    } else  {
508-         output. printf (" No Client IP Address\r\n " 
467+         mbedtls_printf (" No Client IP Address\r\n " 
509468    }
510469
511- 
512-     HelloHTTPS hello (HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
470+     HelloHTTPS hello (HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, ð_iface);
513471    hello.startTest (HTTPS_PATH);
514472}
515- 
516- #endif  /*  TARGET_LIKE_MBED */ 
0 commit comments