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.
Diff: MQTTNetwork.h
- Revision:
- 25:f4727705353b
- Parent:
- 24:8a0b0c410820
- Child:
- 26:3cfeb898b183
--- a/MQTTNetwork.h Tue Mar 10 23:22:53 2020 -0400 +++ b/MQTTNetwork.h Sat Mar 21 15:36:30 2020 -0400 @@ -35,7 +35,9 @@ //Include certificates header #include "Certificates.h" -extern RawSerial pc; +#include "Logging.h" + +//extern RawSerial pc; namespace IoTF { @@ -48,37 +50,36 @@ //Poniter mebers for mbedTLS structures mbedtls_entropy_context* _entropy; mbedtls_ctr_drbg_context* _ctr_drbg; - mbedtls_x509_crt* _cacert; mbedtls_ssl_context* _ssl; mbedtls_ssl_config* _ssl_conf; public: //Pointer member for ethernet interface - //NetworkInterface *network; - EthernetInterface *net; - //Pointer member to represent socket - TCPSocket socket; - + NetworkInterface *network; + + + //EthernetInterface network; + //Create socket with created ethernet interface + TCPSocket socket; + //Default constructor to initialize network, mbedTLS structures MQTTNetwork(): serverPort(1883){ +<<<<<<< working copy +======= pc.printf("serverPort\r\n"); net = new EthernetInterface; //network->set_blocking(false); //net->set_blocking(false); //pc.printf("set_blocking(false)\r\n"); +>>>>>>> destination //Instantiate new ethernet interface //network = new EthernetInterface(); - pc.printf("Network\r\n"); //Connect to ethernet interface - //nsapi_error_t err = network->connect(); - nsapi_error_t err = net->connect(); - pc.printf("Connected %d\r\n",err); - //Create socket with created ethernet interface - //TCPSocket* socket; - //socket = new TCPSocket(network); - //socket.open(network); - socket.open(net); - pc.printf("Socket\r\n"); + network = NetworkInterface::get_default_instance(); + + LOG("%d\r\n",network->connect()); + LOG("%d\r\n",socket.open(network)); + //Instantiate mbedTLS structures _entropy = new mbedtls_entropy_context(); _ctr_drbg = new mbedtls_ctr_drbg_context(); @@ -110,9 +111,8 @@ mbedtls_ssl_config_free(_ssl_conf); //Free the allocated memory for socket and network pointers + //delete network; //delete socket; - //delete network; - delete net; //Free the allocated memory for mbedTLS structures delete _entropy; @@ -128,25 +128,31 @@ int connect(const char*host, int port){ int rc = -1; - //socket.open(network); - socket.open(net); + + LOG("%d\r\n",socket.open(network)); + serverPort = port; - if(port == 1883) + if(port == 1883) { //Establish unsecured socket connection - + rc = socket.connect(host, port); - else{ + + } else { //Establish secure socket connection using SSL/TLS //Perform mbedTLS initialization if((rc = initializeTLS(host)) == 0){ //if((rc = socket->connect(host, port))==0){ - if((rc = socket.connect(host, port))==0){ - pc.printf("Socket connection to %s:%d successful...\r\n",host,port); + rc = socket.connect(host, port); + //LOG("%d\r\n",rc); + if((rc)==0){ + LOG("Socket connection to %s:%d successful...\r\n",host,port); //Perform SSL handshake - if ((rc = mbedtls_ssl_handshake(_ssl)) < 0) { + rc = mbedtls_ssl_handshake(_ssl); + //LOG("%d\r\n",rc); + if ((rc) < 0) { if (rc != MBEDTLS_ERR_SSL_WANT_READ && rc != MBEDTLS_ERR_SSL_WANT_WRITE) { - pc.printf("mbedtls_ssl_handshake failed - 0x%x\r\n", -rc); + LOG("mbedtls_ssl_handshake failed - 0x%x\r\n", -rc); } goto exit; } @@ -161,67 +167,77 @@ //Method to initialize mbedTLS structures int initializeTLS(const char* hostName){ int rc = 0; +LOG("initializeTLS(%s)\r\n",hostName); //Initialize entropy seeding - if ((rc = mbedtls_ctr_drbg_seed(_ctr_drbg, mbedtls_entropy_func, _entropy, + rc = mbedtls_ctr_drbg_seed(_ctr_drbg, mbedtls_entropy_func, _entropy, (const unsigned char *) tlsClientName, - sizeof (tlsClientName))) != 0) { - pc.printf("mbedtls_crt_drbg_init failed - 0x%x\r\n", -rc); + sizeof (tlsClientName)); + //LOG("%d\r\n",rc); + if ((rc) != 0) { + LOG("mbedtls_crt_drbg_init failed - 0x%x\r\n", -rc); goto exit; } //Parse certificates into mbedTLS structure - if ((rc = mbedtls_x509_crt_parse(_cacert, (const unsigned char *) serverCert, - sizeof(serverCert))) != 0) { - pc.printf("mbedtls_x509_crt_parse for serverCert failed - 0x%x\r\n", -rc); + rc = mbedtls_x509_crt_parse(_cacert, (const unsigned char *) serverCert, + sizeof(serverCert)); + //LOG("%d\r\n",rc); + if ((rc) != 0) { + LOG("mbedtls_x509_crt_parse for serverCert failed - 0x%x\r\n", -rc); goto exit; } //Set ssl config details - if ((rc = mbedtls_ssl_config_defaults(_ssl_conf, + rc = mbedtls_ssl_config_defaults(_ssl_conf, MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM, - MBEDTLS_SSL_PRESET_DEFAULT)) != 0) { - pc.printf("mbedtls_ssl_config_defaults failed - 0x%x\r\n", -rc); + MBEDTLS_SSL_PRESET_DEFAULT); + //LOG("%d\r\n",rc); + if ((rc) != 0) { + LOG("mbedtls_ssl_config_defaults failed - 0x%x\r\n", -rc); goto exit; } //Add parsed server certificate to ssl config mbedtls_ssl_conf_ca_chain(_ssl_conf, _cacert, NULL); - +//LOG("mbedtls_ssl_conf_ca_chain(_ssl_conf, _cacert, NULL)\r\n"); mbedtls_ssl_conf_rng(_ssl_conf, mbedtls_ctr_drbg_random, _ctr_drbg); - +//LOG("mbedtls_ssl_conf_rng(_ssl_conf, mbedtls_ctr_drbg_random, _ctr_drbg)\r\n"); //Set Server Certificate authorization mode mbedtls_ssl_conf_authmode(_ssl_conf, MBEDTLS_SSL_VERIFY_NONE); - +//LOG("mbedtls_ssl_conf_authmode(_ssl_conf, MBEDTLS_SSL_VERIFY_NONE)\r\n"); //Perform mbedTLS ssl setup - if ((rc = mbedtls_ssl_setup(_ssl, _ssl_conf)) != 0) { - pc.printf("mbedtls_ssl_setup failed - 0x%x\r\n", -rc); + rc = mbedtls_ssl_setup(_ssl, _ssl_conf); + //LOG("%d\r\n",rc); + if ((rc) != 0) { + LOG("mbedtls_ssl_setup failed - 0x%x\r\n", -rc); goto exit; } //Set hostname to establish SSL connection mbedtls_ssl_set_hostname(_ssl, hostName); - +//LOG("mbedtls_ssl_set_hostname(_ssl, hostName)\r\n"); //Set buffer I/O methods for SSL connection mbedtls_ssl_set_bio(_ssl, static_cast<void *>(&socket), tlsWrite, tlsRead, NULL ); - +//LOG("mbedtls_ssl_set_bio(_ssl, static_cast<void *>(&socket),tlsWrite, tlsRead, NULL )\r\n"); exit: + LOG("%d\r\n", rc); return rc; } //Method to read from SSL socket static int tlsRead(void *ctx,unsigned char* buffer, size_t len) { - TCPSocket *socket = static_cast<TCPSocket *>(ctx); - int rc = socket->recv(buffer, len); + TCPSocket *readsocket = static_cast<TCPSocket *>(ctx); + int rc = readsocket->recv(buffer, len); return rc; } //Method to write to SSL socket static int tlsWrite(void *ctx,const unsigned char* buffer, size_t len) { - TCPSocket *socket = static_cast<TCPSocket *>(ctx); - int rc = socket->send(buffer, len); + TCPSocket *wrsocket = static_cast<TCPSocket *>(ctx); + int rc = wrsocket->send(buffer, len); return rc; }