![](/media/cache/profiles/20200518_163046.jpg.50x50_q85.jpg)
CITY3032-wifi-mqtt
src/MQTTNetworkTLS.h@5:f62a9e4a499a, 2021-11-13 (annotated)
- Committer:
- reedas
- Date:
- Sat Nov 13 12:02:49 2021 +0000
- Revision:
- 5:f62a9e4a499a
- Parent:
- 3:62825c5f3cd7
trying to include mbed
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
reedas | 3:62825c5f3cd7 | 1 | /* |
reedas | 3:62825c5f3cd7 | 2 | * Copyright (c) 2019, ARM Limited, All Rights Reserved |
reedas | 3:62825c5f3cd7 | 3 | * SPDX-License-Identifier: Apache-2.0 |
reedas | 3:62825c5f3cd7 | 4 | * |
reedas | 3:62825c5f3cd7 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
reedas | 3:62825c5f3cd7 | 6 | * not use this file except in compliance with the License. |
reedas | 3:62825c5f3cd7 | 7 | * You may obtain a copy of the License at |
reedas | 3:62825c5f3cd7 | 8 | * |
reedas | 3:62825c5f3cd7 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
reedas | 3:62825c5f3cd7 | 10 | * |
reedas | 3:62825c5f3cd7 | 11 | * Unless required by applicable law or agreed to in writing, software |
reedas | 3:62825c5f3cd7 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
reedas | 3:62825c5f3cd7 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
reedas | 3:62825c5f3cd7 | 14 | * See the License for the specific language governing permissions and |
reedas | 3:62825c5f3cd7 | 15 | * limitations under the License. |
reedas | 3:62825c5f3cd7 | 16 | */ |
reedas | 3:62825c5f3cd7 | 17 | |
reedas | 3:62825c5f3cd7 | 18 | #ifndef _MQTTNETWORKTLS_H_ |
reedas | 3:62825c5f3cd7 | 19 | #define _MQTTNETWORKTLS_H_ |
reedas | 3:62825c5f3cd7 | 20 | |
reedas | 3:62825c5f3cd7 | 21 | #include "NetworkInterface.h" |
reedas | 3:62825c5f3cd7 | 22 | #include "TLSSocket.h" |
reedas | 3:62825c5f3cd7 | 23 | |
reedas | 3:62825c5f3cd7 | 24 | #if defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) |
reedas | 3:62825c5f3cd7 | 25 | |
reedas | 3:62825c5f3cd7 | 26 | class MQTTNetworkTLS { |
reedas | 3:62825c5f3cd7 | 27 | public: |
reedas | 3:62825c5f3cd7 | 28 | MQTTNetworkTLS(NetworkInterface *aNetwork) : network(aNetwork) |
reedas | 3:62825c5f3cd7 | 29 | { |
reedas | 3:62825c5f3cd7 | 30 | socket = new TLSSocket(); |
reedas | 3:62825c5f3cd7 | 31 | } |
reedas | 3:62825c5f3cd7 | 32 | |
reedas | 3:62825c5f3cd7 | 33 | ~MQTTNetworkTLS() |
reedas | 3:62825c5f3cd7 | 34 | { |
reedas | 3:62825c5f3cd7 | 35 | delete socket; |
reedas | 3:62825c5f3cd7 | 36 | } |
reedas | 3:62825c5f3cd7 | 37 | |
reedas | 3:62825c5f3cd7 | 38 | int read(unsigned char *buffer, int len, int timeout) |
reedas | 3:62825c5f3cd7 | 39 | { |
reedas | 3:62825c5f3cd7 | 40 | int ret = socket->recv(buffer, len); |
reedas | 3:62825c5f3cd7 | 41 | if (ret == 0) { |
reedas | 3:62825c5f3cd7 | 42 | // A receive size of 0 indicates that the socket |
reedas | 3:62825c5f3cd7 | 43 | // was successfully closed so indicate this to MQTTClient |
reedas | 3:62825c5f3cd7 | 44 | ret = -1; |
reedas | 3:62825c5f3cd7 | 45 | } |
reedas | 3:62825c5f3cd7 | 46 | return ret; |
reedas | 3:62825c5f3cd7 | 47 | } |
reedas | 3:62825c5f3cd7 | 48 | |
reedas | 3:62825c5f3cd7 | 49 | int write(unsigned char *buffer, int len, int timeout) |
reedas | 3:62825c5f3cd7 | 50 | { |
reedas | 3:62825c5f3cd7 | 51 | int ret = socket->send(buffer, len); |
reedas | 3:62825c5f3cd7 | 52 | if (ret == 0) { |
reedas | 3:62825c5f3cd7 | 53 | // The socket is closed so indicate this to MQTTClient |
reedas | 3:62825c5f3cd7 | 54 | return -1; |
reedas | 3:62825c5f3cd7 | 55 | } |
reedas | 3:62825c5f3cd7 | 56 | return ret; |
reedas | 3:62825c5f3cd7 | 57 | } |
reedas | 3:62825c5f3cd7 | 58 | |
reedas | 3:62825c5f3cd7 | 59 | int connect(const char *hostname, int port, const char *ssl_ca_pem = NULL, |
reedas | 3:62825c5f3cd7 | 60 | const char *ssl_cli_pem = NULL, const char *ssl_pk_pem = NULL) |
reedas | 3:62825c5f3cd7 | 61 | { |
reedas | 3:62825c5f3cd7 | 62 | int ret = socket->open(network); |
reedas | 3:62825c5f3cd7 | 63 | if (ret < 0) { |
reedas | 3:62825c5f3cd7 | 64 | return ret; |
reedas | 3:62825c5f3cd7 | 65 | } |
reedas | 3:62825c5f3cd7 | 66 | SocketAddress addr; |
reedas | 3:62825c5f3cd7 | 67 | ret = network->gethostbyname(hostname, &addr); |
reedas | 3:62825c5f3cd7 | 68 | if (ret < 0) { |
reedas | 3:62825c5f3cd7 | 69 | return ret; |
reedas | 3:62825c5f3cd7 | 70 | } |
reedas | 3:62825c5f3cd7 | 71 | addr.set_port(port); |
reedas | 3:62825c5f3cd7 | 72 | ret = socket->set_root_ca_cert(ssl_ca_pem); |
reedas | 3:62825c5f3cd7 | 73 | if (ret < 0) { |
reedas | 3:62825c5f3cd7 | 74 | return ret; |
reedas | 3:62825c5f3cd7 | 75 | } |
reedas | 3:62825c5f3cd7 | 76 | if (ssl_cli_pem != NULL && ssl_pk_pem != NULL) { |
reedas | 3:62825c5f3cd7 | 77 | ret = socket->set_client_cert_key(ssl_cli_pem, ssl_pk_pem); |
reedas | 3:62825c5f3cd7 | 78 | if (ret < 0) { |
reedas | 3:62825c5f3cd7 | 79 | return ret; |
reedas | 3:62825c5f3cd7 | 80 | } |
reedas | 3:62825c5f3cd7 | 81 | } |
reedas | 3:62825c5f3cd7 | 82 | return socket->connect(addr); |
reedas | 3:62825c5f3cd7 | 83 | } |
reedas | 3:62825c5f3cd7 | 84 | |
reedas | 3:62825c5f3cd7 | 85 | int disconnect() |
reedas | 3:62825c5f3cd7 | 86 | { |
reedas | 3:62825c5f3cd7 | 87 | return socket->close(); |
reedas | 3:62825c5f3cd7 | 88 | } |
reedas | 3:62825c5f3cd7 | 89 | |
reedas | 3:62825c5f3cd7 | 90 | int set_root_ca_cert(const char *root_ca_pem) |
reedas | 3:62825c5f3cd7 | 91 | { |
reedas | 3:62825c5f3cd7 | 92 | return socket->set_root_ca_cert(root_ca_pem); |
reedas | 3:62825c5f3cd7 | 93 | } |
reedas | 3:62825c5f3cd7 | 94 | |
reedas | 3:62825c5f3cd7 | 95 | private: |
reedas | 3:62825c5f3cd7 | 96 | NetworkInterface *network; |
reedas | 3:62825c5f3cd7 | 97 | TLSSocket *socket; |
reedas | 3:62825c5f3cd7 | 98 | }; |
reedas | 3:62825c5f3cd7 | 99 | |
reedas | 3:62825c5f3cd7 | 100 | #endif // defined(MBEDTLS_SSL_CLI_C) || defined(DOXYGEN_ONLY) |
reedas | 3:62825c5f3cd7 | 101 | #endif // _MQTTNETWORKTLS_H_ |