Andrew Reed
/
CITY1082-i2c_master_wifi_mqtt
CITY3032-wifi-mqtt
src/MQTTNetwork.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 _MQTTNETWORK_H_ |
reedas | 3:62825c5f3cd7 | 19 | #define _MQTTNETWORK_H_ |
reedas | 3:62825c5f3cd7 | 20 | |
reedas | 3:62825c5f3cd7 | 21 | #include "NetworkInterface.h" |
reedas | 3:62825c5f3cd7 | 22 | #include "TCPSocket.h" |
reedas | 3:62825c5f3cd7 | 23 | |
reedas | 3:62825c5f3cd7 | 24 | class MQTTNetwork { |
reedas | 3:62825c5f3cd7 | 25 | public: |
reedas | 3:62825c5f3cd7 | 26 | MQTTNetwork(NetworkInterface *aNetwork) : network(aNetwork) |
reedas | 3:62825c5f3cd7 | 27 | { |
reedas | 3:62825c5f3cd7 | 28 | socket = new TCPSocket(); |
reedas | 3:62825c5f3cd7 | 29 | } |
reedas | 3:62825c5f3cd7 | 30 | |
reedas | 3:62825c5f3cd7 | 31 | ~MQTTNetwork() |
reedas | 3:62825c5f3cd7 | 32 | { |
reedas | 3:62825c5f3cd7 | 33 | delete socket; |
reedas | 3:62825c5f3cd7 | 34 | } |
reedas | 3:62825c5f3cd7 | 35 | |
reedas | 3:62825c5f3cd7 | 36 | int read(unsigned char *buffer, int len, int timeout) |
reedas | 3:62825c5f3cd7 | 37 | { |
reedas | 3:62825c5f3cd7 | 38 | int ret = socket->recv(buffer, len); |
reedas | 3:62825c5f3cd7 | 39 | if (ret == 0) { |
reedas | 3:62825c5f3cd7 | 40 | // A receive size of 0 indicates that the socket |
reedas | 3:62825c5f3cd7 | 41 | // was successfully closed so indicate this to MQTTClient |
reedas | 3:62825c5f3cd7 | 42 | ret = -1; |
reedas | 3:62825c5f3cd7 | 43 | } |
reedas | 3:62825c5f3cd7 | 44 | return ret; |
reedas | 3:62825c5f3cd7 | 45 | } |
reedas | 3:62825c5f3cd7 | 46 | |
reedas | 3:62825c5f3cd7 | 47 | int write(unsigned char *buffer, int len, int timeout) |
reedas | 3:62825c5f3cd7 | 48 | { |
reedas | 3:62825c5f3cd7 | 49 | int ret = socket->send(buffer, len); |
reedas | 3:62825c5f3cd7 | 50 | if (ret == 0) { |
reedas | 3:62825c5f3cd7 | 51 | // The socket is closed so indicate this to MQTTClient |
reedas | 3:62825c5f3cd7 | 52 | return -1; |
reedas | 3:62825c5f3cd7 | 53 | } |
reedas | 3:62825c5f3cd7 | 54 | return ret; |
reedas | 3:62825c5f3cd7 | 55 | } |
reedas | 3:62825c5f3cd7 | 56 | |
reedas | 3:62825c5f3cd7 | 57 | int connect(const char *hostname, int port) |
reedas | 3:62825c5f3cd7 | 58 | { |
reedas | 3:62825c5f3cd7 | 59 | int ret = NSAPI_ERROR_OK; |
reedas | 3:62825c5f3cd7 | 60 | if ((ret = socket->open(network)) != NSAPI_ERROR_OK) { |
reedas | 3:62825c5f3cd7 | 61 | return ret; |
reedas | 3:62825c5f3cd7 | 62 | } |
reedas | 3:62825c5f3cd7 | 63 | SocketAddress addr; |
reedas | 3:62825c5f3cd7 | 64 | if (network->gethostbyname(hostname, &addr) != NSAPI_ERROR_OK) { |
reedas | 3:62825c5f3cd7 | 65 | return NSAPI_ERROR_DNS_FAILURE; |
reedas | 3:62825c5f3cd7 | 66 | } |
reedas | 3:62825c5f3cd7 | 67 | addr.set_port(port); |
reedas | 3:62825c5f3cd7 | 68 | return socket->connect(addr); |
reedas | 3:62825c5f3cd7 | 69 | } |
reedas | 3:62825c5f3cd7 | 70 | |
reedas | 3:62825c5f3cd7 | 71 | int disconnect() |
reedas | 3:62825c5f3cd7 | 72 | { |
reedas | 3:62825c5f3cd7 | 73 | return socket->close(); |
reedas | 3:62825c5f3cd7 | 74 | } |
reedas | 3:62825c5f3cd7 | 75 | |
reedas | 3:62825c5f3cd7 | 76 | private: |
reedas | 3:62825c5f3cd7 | 77 | NetworkInterface *network; |
reedas | 3:62825c5f3cd7 | 78 | TCPSocket *socket; |
reedas | 3:62825c5f3cd7 | 79 | }; |
reedas | 3:62825c5f3cd7 | 80 | |
reedas | 3:62825c5f3cd7 | 81 | #endif // _MQTTNETWORK_H_ |