Fix mbed OS TLS client example

This commit is contained in:
Manuel Pégourié-Gonnard 2015-08-07 14:35:12 +02:00
parent e33316c607
commit 8356fc212d

View File

@ -63,11 +63,11 @@ int main() {
#include "lwipv4_init.h" #include "lwipv4_init.h"
namespace { namespace {
const char *HTTPS_SERVER_NAME = "developer.mbed.org"; const char *HTTPS_SERVER_NAME = "mbed.org";
const int HTTPS_SERVER_PORT = 443; const int HTTPS_SERVER_PORT = 443;
const int RECV_BUFFER_SIZE = 600; const int RECV_BUFFER_SIZE = 600;
const char HTTPS_PATH[] = "/media/uploads/mbed_official/hello.txt"; const char HTTPS_PATH[] = "/assets/uploads/hello.txt";
const size_t HTTPS_PATH_LEN = sizeof(HTTPS_PATH) - 1; const size_t HTTPS_PATH_LEN = sizeof(HTTPS_PATH) - 1;
/* Test related data */ /* Test related data */
@ -149,7 +149,7 @@ public:
* @param[in] path The path of the file to fetch from the HTTPS server * @param[in] path The path of the file to fetch from the HTTPS server
* @return SOCKET_ERROR_NONE on success, or an error code on failure * @return SOCKET_ERROR_NONE on success, or an error code on failure
*/ */
socket_error_t startTest(const char *path) { void startTest(const char *path) {
/* Initialize the flags */ /* Initialize the flags */
_got200 = false; _got200 = false;
_gothello = false; _gothello = false;
@ -167,13 +167,15 @@ public:
(const unsigned char *) DRBG_PERS, (const unsigned char *) DRBG_PERS,
sizeof (DRBG_PERS))) != 0) { sizeof (DRBG_PERS))) != 0) {
print_mbedtls_error("mbedtls_crt_drbg_init", ret); print_mbedtls_error("mbedtls_crt_drbg_init", ret);
return SOCKET_ERROR_UNKNOWN; _error = true;
return;
} }
if ((ret = mbedtls_x509_crt_parse(&_cacert, (const unsigned char *) SSL_CA_PEM, if ((ret = mbedtls_x509_crt_parse(&_cacert, (const unsigned char *) SSL_CA_PEM,
sizeof (SSL_CA_PEM))) != 0) { sizeof (SSL_CA_PEM))) != 0) {
print_mbedtls_error("mbedtls_x509_crt_parse", ret); print_mbedtls_error("mbedtls_x509_crt_parse", ret);
return SOCKET_ERROR_UNKNOWN; _error = true;
return;
} }
if ((ret = mbedtls_ssl_config_defaults(&_ssl_conf, if ((ret = mbedtls_ssl_config_defaults(&_ssl_conf,
@ -181,7 +183,8 @@ public:
MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_TRANSPORT_STREAM,
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
print_mbedtls_error("mbedtls_ssl_config_defaults", ret); print_mbedtls_error("mbedtls_ssl_config_defaults", ret);
return SOCKET_ERROR_UNKNOWN; _error = true;
return;
} }
mbedtls_ssl_conf_ca_chain(&_ssl_conf, &_cacert, NULL); mbedtls_ssl_conf_ca_chain(&_ssl_conf, &_cacert, NULL);
@ -199,7 +202,8 @@ public:
if ((ret = mbedtls_ssl_setup(&_ssl, &_ssl_conf)) != 0) { if ((ret = mbedtls_ssl_setup(&_ssl, &_ssl_conf)) != 0) {
print_mbedtls_error("mbedtls_ssl_setup", ret); print_mbedtls_error("mbedtls_ssl_setup", ret);
return SOCKET_ERROR_UNKNOWN; _error = true;
return;
} }
mbedtls_ssl_set_hostname(&_ssl, HTTPS_SERVER_NAME); mbedtls_ssl_set_hostname(&_ssl, HTTPS_SERVER_NAME);
@ -212,7 +216,8 @@ public:
printf("Connecting to %s:%d\r\n", _domain, _port); printf("Connecting to %s:%d\r\n", _domain, _port);
/* 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));
return err; if(err != SOCKET_ERROR_NONE)
_error = true;
} }
/** /**
* Check if the test has completed. * Check if the test has completed.
@ -477,19 +482,15 @@ int example_client() {
printf("Client IP Address is %s\r\n", eth.getIPAddress()); printf("Client IP Address is %s\r\n", eth.getIPAddress());
HelloHTTPS hello(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT); HelloHTTPS hello(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
socket_error_t rc = hello.startTest(HTTPS_PATH); {
if (rc != SOCKET_ERROR_NONE) { mbed::FunctionPointer1<void, const char*> fp(&hello, &HelloHTTPS::startTest);
return 1; minar::Scheduler::postCallback(fp.bind(HTTPS_PATH));
} }
while (!hello.done()) {
__WFI(); minar::Scheduler::start();
}
if (hello.error()) {
printf("Failed to fetch %s from %s:%d\r\n", HTTPS_PATH, HTTPS_SERVER_NAME, HTTPS_SERVER_PORT);
}
/* Shut down the socket before the ethernet interface */
hello.close();
eth.disconnect(); eth.disconnect();
return static_cast<int>(hello.error()); return static_cast<int>(hello.error());
} }