Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: WNCInterface mbed-rtos mbed
Diff: main.cpp
- Revision:
- 12:419514568fef
- Parent:
- 9:1ac74f2d7bda
diff -r bbdd078dac9e -r 419514568fef main.cpp
--- a/main.cpp Wed Oct 05 05:15:17 2016 +0100
+++ b/main.cpp Wed Nov 02 16:31:57 2016 +0000
@@ -36,10 +36,11 @@
#define UNSAFE 0
#include "mbed.h"
-#include "NetworkStack.h"
+#include "rtos.h"
+//#include "NetworkInterface.h"
-#include "EthernetInterface.h"
-#include "TCPSocket.h"
+#include "WNCInterface.h"
+#include "TCPSocketConnection.h"
#include "mbedtls/platform.h"
#include "mbedtls/ssl.h"
@@ -57,7 +58,7 @@
const int RECV_BUFFER_SIZE = 600;
const char HTTPS_PATH[] = "/media/uploads/mbed_official/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 */
const char *HTTPS_OK_STR = "200 OK";
@@ -140,7 +141,7 @@
* @param[in] domain The domain name to fetch from
* @param[in] port The port of the HTTPS server
*/
- HelloHTTPS(const char * domain, const uint16_t port, NetworkInterface *net_iface) :
+ HelloHTTPS(const char * domain, const uint16_t port, WNCInterface *net_iface) :
_domain(domain), _port(port)
{
@@ -149,7 +150,7 @@
_got200 = false;
_bpos = 0;
_request_sent = 0;
- _tcpsocket = new TCPSocket(net_iface);
+ _tcpsocket = new TCPSocketConnection;
mbedtls_entropy_init(&_entropy);
mbedtls_ctr_drbg_init(&_ctr_drbg);
@@ -240,14 +241,14 @@
/* Connect to the server */
mbedtls_printf("Connecting with %s\r\n", _domain);
- _tcpsocket->connect( _domain, _port );
+ ret = _tcpsocket->connect( _domain, _port );
+ _tcpsocket->set_blocking (false,1500); //allow 1.5sec for responses from WNC
/* Start the handshake, the rest will be done in onReceive() */
mbedtls_printf("Starting the TLS handshake...\r\n");
ret = mbedtls_ssl_handshake(&_ssl);
if (ret < 0) {
- if (ret != MBEDTLS_ERR_SSL_WANT_READ &&
- ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
+ if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
print_mbedtls_error("mbedtls_ssl_handshake", ret);
onError(_tcpsocket, -1 );
}
@@ -265,24 +266,27 @@
}
/* It also means the handshake is done, time to print info */
- printf("TLS connection to %s established\r\n", HTTPS_SERVER_NAME);
+ mbedtls_printf("TLS connection to %s established\r\n", HTTPS_SERVER_NAME);
const uint32_t buf_size = 1024;
char *buf = new char[buf_size];
mbedtls_x509_crt_info(buf, buf_size, "\r ",
mbedtls_ssl_get_peer_cert(&_ssl));
- mbedtls_printf("Server certificate:\r\n%s\r", buf);
+ mbedtls_printf("Server certificate:\r\n", buf);
+ for( int i = 0; buf[i]; i++ )
+ mbedtls_printf("%c",buf[i]);
+ mbedtls_printf("\r\n");
#if defined(UNSAFE)
uint32_t flags = mbedtls_ssl_get_verify_result(&_ssl);
if( flags != 0 )
{
mbedtls_x509_crt_verify_info(buf, buf_size, "\r ! ", flags);
- printf("Certificate verification failed:\r\n%s\r\r\n", buf);
+ mbedtls_printf("Certificate verification failed:\r\n%s\r\r\n", buf);
}
else
#endif
- printf("Certificate verification passed\r\n\r\n");
+ mbedtls_printf("Certificate verification passed\r\n\r\n");
/* Read data out of the socket */
@@ -374,22 +378,24 @@
static int my_verify(void *data, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
{
const uint32_t buf_size = 1024;
- char *buf = new char[buf_size];
+ string buf; // = new char[buf_size];
+ int i;
(void) data;
-
+ buf.reserve(buf_size);
+ //memset(buf,0x00,buf_size);
mbedtls_printf("\nVerifying certificate at depth %d:\n", depth);
- mbedtls_x509_crt_info(buf, buf_size - 1, " ", crt);
- mbedtls_printf("%s", buf);
+ mbedtls_x509_crt_info(&buf[0], buf_size - 1, " ", crt);
+ mbedtls_printf("%s", buf.c_str());
if (*flags == 0)
- mbedtls_printf("No verification issue for this certificate\n");
+ mbedtls_printf("No verification issue for this certificate\r\n");
else
{
- mbedtls_x509_crt_verify_info(buf, buf_size, " ! ", *flags);
- mbedtls_printf("%s\n", buf);
+ mbedtls_x509_crt_verify_info(&buf[0], buf_size, " ! ", *flags);
+ mbedtls_printf("%s\n", buf.c_str());
}
- delete[] buf;
+ //delete[] buf;
return 0;
}
#endif
@@ -399,12 +405,10 @@
*/
static int ssl_recv(void *ctx, unsigned char *buf, size_t len) {
int recv = -1;
- TCPSocket *socket = static_cast<TCPSocket *>(ctx);
- recv = socket->recv(buf, len);
+ TCPSocketConnection *socket = static_cast<TCPSocketConnection *>(ctx);
+ recv = socket->receive((char*)buf, len);
- if(NSAPI_ERROR_WOULD_BLOCK == recv){
- return MBEDTLS_ERR_SSL_WANT_READ;
- }else if(recv < 0){
+ if(recv < 0){
return -1;
}else{
return recv;
@@ -416,26 +420,24 @@
*/
static int ssl_send(void *ctx, const unsigned char *buf, size_t len) {
int size = -1;
- TCPSocket *socket = static_cast<TCPSocket *>(ctx);
- size = socket->send(buf, len);
+ TCPSocketConnection *socket = static_cast<TCPSocketConnection *>(ctx);
+ size = socket->send((char*)buf, len);
- if(NSAPI_ERROR_WOULD_BLOCK == size){
- return len;
- }else if(size < 0){
+ if(size < 0){
return -1;
}else{
return size;
}
}
- void onError(TCPSocket *s, int error) {
- printf("MBED: Socket Error: %d\r\n", error);
+ void onError(TCPSocketConnection *s, int error) {
+ mbedtls_printf("MBED: Socket Error: %d\r\n", error);
s->close();
_error = true;
}
protected:
- TCPSocket* _tcpsocket;
+ TCPSocketConnection* _tcpsocket;
const char *_domain; /**< The domain name of the HTTPS server */
const uint16_t _port; /**< The HTTPS server port */
@@ -454,18 +456,42 @@
mbedtls_ssl_config _ssl_conf;
};
-/**
- * The main loop of the HTTPS Hello World test
- */
-int main() {
+MODSERIAL pc(USBTX,USBRX,256,256);
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int myprintf( const char* format, ... ) {
+ char buff[1024];
+ va_list valist;
+
+
+ va_start(valist, format);
+ vsprintf(buff,format,valist);
+ pc.puts(buff);
+ va_end(valist);
+ return 0;
+}
+
+#ifdef __cplusplus
+}
+#endif
+
+
+void ssl_test(void const *args) {
/* The default 9600 bps is too slow to print full TLS debug info and could
* cause the other party to time out. */
+ pc.baud(115200);
+ mbedtls_printf("SSL/TLS Test Using WNC Data Module\r\n");
+
/* Inititalise with DHCP, connect, and start up the stack */
- EthernetInterface eth_iface;
- eth_iface.connect();
- mbedtls_printf("Using Ethernet LWIP\r\n");
- const char *ip_addr = eth_iface.get_ip_address();
+ WNCInterface eth_iface;
+ eth_iface.init(NULL,&pc);
+ eth_iface.doDebug(0);
+
+ const char *ip_addr = eth_iface.getIPAddress();
if (ip_addr) {
mbedtls_printf("Client IP Address is %s\r\n", ip_addr);
} else {
@@ -474,5 +500,17 @@
HelloHTTPS *hello = new HelloHTTPS(HTTPS_SERVER_NAME, HTTPS_SERVER_PORT, ð_iface);
hello->startTest(HTTPS_PATH);
+ mbedtls_printf(">>> All Done <<<\r\n");
delete hello;
+ }
+
+/**
+ * The main loop of the HTTPS Hello World test
+ */
+
+osThreadDef(ssl_test, osPriorityNormal, DEFAULT_STACK_SIZE*8);
+
+int main() {
+ osThreadCreate(osThread(ssl_test), NULL);
+ while(1) osDelay(500);
}