Fix usage of minar in example-tls-client

Also sync with mbed-example-network's helloworld-tcpclient while at it.
This commit is contained in:
Manuel Pégourié-Gonnard 2015-08-18 18:18:16 +02:00
parent ee4cb7d5aa
commit bd6d0aba25

View file

@ -48,10 +48,13 @@ int main() {
#define UNSAFE 0 #define UNSAFE 0
#include "mbed.h" #include "mbed.h"
#include "sal-iface-eth/EthernetInterface.h" #include "EthernetInterface.h"
#include "mbed-net-sockets/TCPStream.h" #include "mbed-net-sockets/TCPStream.h"
#include "test_env.h"
#include "minar/minar.h" #include "minar/minar.h"
#include "lwipv4_init.h"
#include "mbedtls/ssl.h" #include "mbedtls/ssl.h"
#include "mbedtls/entropy.h" #include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h" #include "mbedtls/ctr_drbg.h"
@ -60,8 +63,6 @@ int main() {
#include "mbedtls/debug.h" #include "mbedtls/debug.h"
#endif #endif
#include "lwipv4_init.h"
namespace { namespace {
const char *HTTPS_SERVER_NAME = "developer.mbed.org"; const char *HTTPS_SERVER_NAME = "developer.mbed.org";
const int HTTPS_SERVER_PORT = 443; const int HTTPS_SERVER_PORT = 443;
@ -213,11 +214,10 @@ public:
/* Connect to the server */ /* Connect to the server */
printf("Connecting to %s:%d\r\n", _domain, _port); printf("Starting DNS lookup for %s\r\n", _domain);
/* Resolve the domain name: */ /* Resolve the domain name: */
socket_error_t err = _stream.resolve(_domain, TCPStream::DNSHandler_t(this, &HelloHTTPS::onDNS)); socket_error_t err = _stream.resolve(_domain, TCPStream::DNSHandler_t(this, &HelloHTTPS::onDNS));
if(err != SOCKET_ERROR_NONE) _stream.error_check(err);
_error = true;
} }
/** /**
* Check if the test has completed. * Check if the test has completed.
@ -327,23 +327,28 @@ protected:
printf("MBED: Socket Error: %s (%d)\r\n", socket_strerror(err), err); printf("MBED: Socket Error: %s (%d)\r\n", socket_strerror(err), err);
_stream.close(); _stream.close();
_error = true; _error = true;
minar::Scheduler::stop(); MBED_HOSTTEST_RESULT(false);
} }
/** /**
* On Connect handler * On Connect handler
* Starts the TLS handshake * Starts the TLS handshake
*/ */
void onConnect(TCPStream *s) { void onConnect(TCPStream *s) {
char buf[16];
_remoteAddr.fmtIPv4(buf,sizeof(buf));
printf("Connected to %s:%d\r\n", buf, _port);
s->setOnReadable(TCPStream::ReadableHandler_t(this, &HelloHTTPS::onReceive)); s->setOnReadable(TCPStream::ReadableHandler_t(this, &HelloHTTPS::onReceive));
s->setOnDisconnect(TCPStream::DisconnectHandler_t(this, &HelloHTTPS::onDisconnect)); s->setOnDisconnect(TCPStream::DisconnectHandler_t(this, &HelloHTTPS::onDisconnect));
/* Start the handshake, the rest will be done in onReceive() */ /* Start the handshake, the rest will be done in onReceive() */
printf("Starting the TLS handshake...\r\n");
int ret = mbedtls_ssl_handshake(&_ssl); int ret = mbedtls_ssl_handshake(&_ssl);
if (ret < 0) { if (ret < 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ && if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE) { ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
print_mbedtls_error("mbedtls_ssl_handshake", ret); print_mbedtls_error("mbedtls_ssl_handshake", ret);
_error = true; onError(s, SOCKET_ERROR_UNKNOWN);
} }
return; return;
} }
@ -353,9 +358,6 @@ protected:
* Parses the response from the server, to check for the HTTPS 200 status code and the expected response ("Hello World!") * Parses the response from the server, to check for the HTTPS 200 status code and the expected response ("Hello World!")
*/ */
void onReceive(Socket *s) { void onReceive(Socket *s) {
if (_error)
return;
/* Send request if not done yet */ /* Send request if not done yet */
if (!_request_sent) { if (!_request_sent) {
int ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) _buffer, _bpos); int ret = mbedtls_ssl_write(&_ssl, (const unsigned char *) _buffer, _bpos);
@ -363,7 +365,7 @@ protected:
if (ret != MBEDTLS_ERR_SSL_WANT_READ && if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
ret != MBEDTLS_ERR_SSL_WANT_WRITE) { ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
print_mbedtls_error("mbedtls_ssl_write", ret); print_mbedtls_error("mbedtls_ssl_write", ret);
_error = true; onError(s, SOCKET_ERROR_UNKNOWN);
} }
return; return;
} }
@ -395,10 +397,9 @@ protected:
/* Read data out of the socket */ /* Read data out of the socket */
int ret = mbedtls_ssl_read(&_ssl, (unsigned char *) _buffer, sizeof(_buffer)); int ret = mbedtls_ssl_read(&_ssl, (unsigned char *) _buffer, sizeof(_buffer));
if (ret < 0) { if (ret < 0) {
if (ret != MBEDTLS_ERR_SSL_WANT_READ && if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
print_mbedtls_error("mbedtls_ssl_read", ret); print_mbedtls_error("mbedtls_ssl_read", ret);
_error = true; onError(s, SOCKET_ERROR_UNKNOWN);
} }
return; return;
} }
@ -435,7 +436,8 @@ protected:
char buf[16]; char buf[16];
_remoteAddr.setAddr(&addr); _remoteAddr.setAddr(&addr);
_remoteAddr.fmtIPv4(buf,sizeof(buf)); _remoteAddr.fmtIPv4(buf,sizeof(buf));
printf("%s address: %s\r\n",domain, buf); printf("DNS Response Received:\r\n%s: %s\r\n", domain, buf);
printf("Connecting to %s:%d\r\n", buf, _port);
socket_error_t err = _stream.connect(_remoteAddr, _port, TCPStream::ConnectHandler_t(this, &HelloHTTPS::onConnect)); socket_error_t err = _stream.connect(_remoteAddr, _port, TCPStream::ConnectHandler_t(this, &HelloHTTPS::onConnect));
if (err != SOCKET_ERROR_NONE) { if (err != SOCKET_ERROR_NONE) {
@ -445,7 +447,7 @@ protected:
} }
void onDisconnect(TCPStream *s) { void onDisconnect(TCPStream *s) {
s->close(); s->close();
minar::Scheduler::stop(); MBED_HOSTTEST_RESULT(!error());
} }
protected: protected:
@ -471,33 +473,10 @@ protected:
/** /**
* The main loop of the HTTPS Hello World test * The main loop of the HTTPS Hello World test
*/ */
int example_client() {
EthernetInterface eth; EthernetInterface eth;
/* Initialise with DHCP, connect, and start up the stack */ HelloHTTPS *hello;
eth.init();
eth.connect();
lwipv4_socket_init();
printf("\r\n\r\n"); void app_start(int, char*[]) {
printf("Client IP Address is %s\r\n", eth.getIPAddress());
HelloHTTPS hello(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
{
mbed::FunctionPointer1<void, const char*> fp(&hello, &HelloHTTPS::startTest);
minar::Scheduler::postCallback(fp.bind(HTTPS_PATH));
}
minar::Scheduler::start();
eth.disconnect();
return static_cast<int>(hello.error());
}
#include "mbed/test_env.h"
#include "minar/minar.h"
static void run() {
/* The default 9600 bps is too slow to print full TLS debug info and could /* The default 9600 bps is too slow to print full TLS debug info and could
* cause the other party to time out. Select a higher baud rate for * cause the other party to time out. Select a higher baud rate for
* printf(), regardless of debug level for the sake of uniformity. */ * printf(), regardless of debug level for the sake of uniformity. */
@ -508,11 +487,19 @@ static void run() {
MBED_HOSTTEST_SELECT(default); MBED_HOSTTEST_SELECT(default);
MBED_HOSTTEST_DESCRIPTION(mbed TLS example HTTPS client); MBED_HOSTTEST_DESCRIPTION(mbed TLS example HTTPS client);
MBED_HOSTTEST_START("MBEDTLS_EX_HTTPS_CLIENT"); MBED_HOSTTEST_START("MBEDTLS_EX_HTTPS_CLIENT");
MBED_HOSTTEST_RESULT(example_client() == 0);
}
void app_start(int, char*[]) { /* Initialise with DHCP, connect, and start up the stack */
minar::Scheduler::postCallback(FunctionPointer0<void>(run).bind()); eth.init();
eth.connect();
lwipv4_socket_init();
hello = new HelloHTTPS(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
printf("\r\n\r\n");
printf("Client IP Address is %s\r\n", eth.getIPAddress());
mbed::FunctionPointer1<void, const char*> fp(hello, &HelloHTTPS::startTest);
minar::Scheduler::postCallback(fp.bind(HTTPS_PATH));
} }
#endif /* TARGET_LIKE_MBED */ #endif /* TARGET_LIKE_MBED */