Update version of mbed-net-socket

This commit is contained in:
Manuel Pégourié-Gonnard 2015-08-04 14:10:28 +02:00 committed by Manuel Pégourié-Gonnard
parent 0adc7f7b0f
commit ae5398a0cf
2 changed files with 38 additions and 29 deletions

View File

@ -34,8 +34,9 @@ int main() {
#define UNSAFE 0 #define UNSAFE 0
#include "mbed.h" #include "mbed.h"
#include <mbed-net-lwip-eth/EthernetInterface.h> #include "mbed-net-lwip-eth/EthernetInterface.h"
#include <mbed-net-sockets/TCPStream.h> #include "mbed-net-sockets/TCPStream.h"
#include "minar/minar.h"
#include "mbedtls/ssl.h" #include "mbedtls/ssl.h"
#include "mbedtls/entropy.h" #include "mbedtls/entropy.h"
@ -90,6 +91,8 @@ const char SSL_CA_PEM[] =
"-----END CERTIFICATE-----\n"; "-----END CERTIFICATE-----\n";
} }
using namespace mbed::Sockets::v0;
/** /**
* \brief HelloHTTPS implements the logic for fetching a file from a webserver * \brief HelloHTTPS implements the logic for fetching a file from a webserver
* using a TCP socket and parsing the result. * using a TCP socket and parsing the result.
@ -194,7 +197,7 @@ public:
/* Connect to the server */ /* Connect to the server */
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, handler_t(this, &HelloHTTPS::onDNS)); socket_error_t err = _stream.resolve(_domain, TCPStream::DNSHandler_t(this, &HelloHTTPS::onDNS));
return err; return err;
} }
/** /**
@ -271,7 +274,7 @@ protected:
* Receive callback for mbed TLS * Receive callback for mbed TLS
*/ */
static int ssl_recv(void *ctx, unsigned char *buf, size_t len) { static int ssl_recv(void *ctx, unsigned char *buf, size_t len) {
mbed::TCPStream *stream = static_cast<mbed::TCPStream *>(ctx); TCPStream *stream = static_cast<TCPStream *>(ctx);
socket_error_t err = stream->recv(buf, &len); socket_error_t err = stream->recv(buf, &len);
if (err == SOCKET_ERROR_NONE) { if (err == SOCKET_ERROR_NONE) {
@ -287,7 +290,7 @@ protected:
* Send callback for mbed TLS * Send callback for mbed TLS
*/ */
static int ssl_send(void *ctx, const unsigned char *buf, size_t len) { static int ssl_send(void *ctx, const unsigned char *buf, size_t len) {
mbed::TCPStream *stream = static_cast<mbed::TCPStream *>(ctx); TCPStream *stream = static_cast<TCPStream *>(ctx);
socket_error_t err = stream->send(buf, len); socket_error_t err = stream->send(buf, len);
@ -300,15 +303,20 @@ protected:
} }
} }
void onError(Socket *s, socket_error_t err) {
(void) s;
printf("MBED: Socket Error: %s (%d)\r\n", socket_strerror(err), err);
_stream.close();
_error = true;
minar::Scheduler::stop();
}
/** /**
* On Connect handler * On Connect handler
* Sends the request which was generated in startTest * Starts the TLS handshake
*/ */
void onConnect(socket_error_t err) { void onConnect(TCPStream *s) {
(void) err; s->setOnReadable(TCPStream::ReadableHandler_t(this, &HelloHTTPS::onReceive));
s->setOnDisconnect(TCPStream::DisconnectHandler_t(this, &HelloHTTPS::onDisconnect));
_stream.setOnReadable(handler_t(this, &HelloHTTPS::onReceive));
_stream.setOnDisconnect(handler_t(this, &HelloHTTPS::onDisconnect));
/* Start the handshake, the rest will be done in onReceive() */ /* Start the handshake, the rest will be done in onReceive() */
int ret = mbedtls_ssl_handshake(&_ssl); int ret = mbedtls_ssl_handshake(&_ssl);
@ -325,9 +333,7 @@ protected:
* On Receive handler * On Receive handler
* 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_error_t err) { void onReceive(Socket *s) {
(void) err;
if (_error) if (_error)
return; return;
@ -392,41 +398,44 @@ protected:
printf("HTTPS: Received message:\r\n\r\n"); printf("HTTPS: Received message:\r\n\r\n");
printf("%s", _buffer); printf("%s", _buffer);
_error = !(_got200 && _gothello); _error = !(_got200 && _gothello);
s->close();
} }
/** /**
* On DNS Handler * On DNS Handler
* Reads the address returned by DNS, then starts the connect process. * Reads the address returned by DNS, then starts the connect process.
*/ */
void onDNS(socket_error_t err) { void onDNS(Socket *s, struct socket_addr addr, const char *domain) {
socket_event_t *e = _stream.getEvent();
/* Check that the result is a valid DNS response */ /* Check that the result is a valid DNS response */
if (socket_addr_is_any(&e->i.d.addr)) { if (socket_addr_is_any(&addr)) {
/* Could not find DNS entry */ /* Could not find DNS entry */
_error = true;
printf("Could not find DNS entry for %s", HTTPS_SERVER_NAME); printf("Could not find DNS entry for %s", HTTPS_SERVER_NAME);
return; onError(s, SOCKET_ERROR_DNS_FAILED);
} else { } else {
/* Start connecting to the remote host */ /* Start connecting to the remote host */
_remoteAddr.setAddr(&e->i.d.addr); char buf[16];
err = _stream.connect(&_remoteAddr, _port, handler_t(this, &HelloHTTPS::onConnect)); _remoteAddr.setAddr(&addr);
_remoteAddr.fmtIPv4(buf,sizeof(buf));
printf("%s address: %s\r\n",domain, buf);
socket_error_t err = _stream.connect(_remoteAddr, _port, TCPStream::ConnectHandler_t(this, &HelloHTTPS::onConnect));
if (err != SOCKET_ERROR_NONE) { if (err != SOCKET_ERROR_NONE) {
_error = true; onError(s, err);
} }
} }
} }
void onDisconnect(socket_error_t err) { void onDisconnect(TCPStream *s) {
(void) err; s->close();
_disconnected = true; minar::Scheduler::stop();
} }
protected: protected:
mbed::TCPStream _stream; /**< The TCP Socket */ TCPStream _stream; /**< The TCP Socket */
const char *_domain; /**< The domain name of the HTTPS server */ const char *_domain; /**< The domain name of the HTTPS server */
const uint16_t _port; /**< The HTTPS server port */ const uint16_t _port; /**< The HTTPS server port */
char _buffer[RECV_BUFFER_SIZE]; /**< The response buffer */ char _buffer[RECV_BUFFER_SIZE]; /**< The response buffer */
size_t _bpos; /**< The current offset in the response buffer */ size_t _bpos; /**< The current offset in the response buffer */
mbed::SocketAddr _remoteAddr; /**< The remote address */ SocketAddr _remoteAddr; /**< The remote address */
volatile bool _got200; /**< Status flag for HTTPS 200 */ volatile bool _got200; /**< Status flag for HTTPS 200 */
volatile bool _gothello; /**< Status flag for finding the test string */ volatile bool _gothello; /**< Status flag for finding the test string */
volatile bool _error; /**< Status flag for an error */ volatile bool _error; /**< Status flag for an error */

View File

@ -5,9 +5,9 @@
"license": "GPL-2.0", "license": "GPL-2.0",
"dependencies": {}, "dependencies": {},
"targetDependencies": { "targetDependencies": {
"mbed": { "cmsis-core": "~0.2.1" } "mbed": { "cmsis-core": "~0.2.3" }
}, },
"testTargetDependencies": { "testTargetDependencies": {
"mbed": { "mbed-net-sockets": "~0.1.3" } "mbed": { "mbed-net-sockets": "~0.2.0" }
} }
} }