MQTTClient
MQTTClient.cpp@3:ac326c5b065c, 2012-03-21 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Wed Mar 21 21:52:42 2012 +0000
- Revision:
- 3:ac326c5b065c
- Parent:
- 2:92b9dd336375
- Child:
- 4:9e25b78e0352
updated comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 2:92b9dd336375 | 1 | /** |
SomeRandomBloke | 0:260fb10c0755 | 2 | MQTTClient.cpp |
SomeRandomBloke | 0:260fb10c0755 | 3 | Based on MQTTClient from http://ceit.uq.edu.au/content/mqttclient-mbed-version-20 |
SomeRandomBloke | 0:260fb10c0755 | 4 | A simple MQTT client for mbed, version 2.0 |
SomeRandomBloke | 0:260fb10c0755 | 5 | By Yilun FAN, @CEIT, @JAN 2011 |
SomeRandomBloke | 0:260fb10c0755 | 6 | |
SomeRandomBloke | 0:260fb10c0755 | 7 | Bug fixes and additions by Andrew Lindsay (andrew [at] thiseldo [dot] co [dot] uk) |
SomeRandomBloke | 0:260fb10c0755 | 8 | |
SomeRandomBloke | 0:260fb10c0755 | 9 | Permission is hereby granted, free of charge, to any person obtaining a copy |
SomeRandomBloke | 0:260fb10c0755 | 10 | of this software and associated documentation files (the "Software"), to deal |
SomeRandomBloke | 0:260fb10c0755 | 11 | in the Software without restriction, including without limitation the rights |
SomeRandomBloke | 0:260fb10c0755 | 12 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
SomeRandomBloke | 0:260fb10c0755 | 13 | copies of the Software, and to permit persons to whom the Software is |
SomeRandomBloke | 0:260fb10c0755 | 14 | furnished to do so, subject to the following conditions: |
SomeRandomBloke | 0:260fb10c0755 | 15 | |
SomeRandomBloke | 0:260fb10c0755 | 16 | The above copyright notice and this permission notice shall be included in |
SomeRandomBloke | 0:260fb10c0755 | 17 | all copies or substantial portions of the Software. |
SomeRandomBloke | 0:260fb10c0755 | 18 | |
SomeRandomBloke | 0:260fb10c0755 | 19 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
SomeRandomBloke | 0:260fb10c0755 | 20 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
SomeRandomBloke | 0:260fb10c0755 | 21 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
SomeRandomBloke | 0:260fb10c0755 | 22 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
SomeRandomBloke | 0:260fb10c0755 | 23 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
SomeRandomBloke | 0:260fb10c0755 | 24 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
SomeRandomBloke | 0:260fb10c0755 | 25 | THE SOFTWARE. |
SomeRandomBloke | 0:260fb10c0755 | 26 | */ |
SomeRandomBloke | 0:260fb10c0755 | 27 | |
SomeRandomBloke | 0:260fb10c0755 | 28 | #include "MQTTClient.h" |
SomeRandomBloke | 0:260fb10c0755 | 29 | |
SomeRandomBloke | 1:a50b6c866a3b | 30 | /** Default Constructor |
SomeRandomBloke | 1:a50b6c866a3b | 31 | */ |
SomeRandomBloke | 1:a50b6c866a3b | 32 | MQTTClient::MQTTClient() {} |
SomeRandomBloke | 1:a50b6c866a3b | 33 | |
SomeRandomBloke | 1:a50b6c866a3b | 34 | /** Default Destructor |
SomeRandomBloke | 1:a50b6c866a3b | 35 | */ |
SomeRandomBloke | 1:a50b6c866a3b | 36 | MQTTClient::~MQTTClient() {} |
SomeRandomBloke | 1:a50b6c866a3b | 37 | |
SomeRandomBloke | 3:ac326c5b065c | 38 | /** Alternative Constructor with parameters |
SomeRandomBloke | 1:a50b6c866a3b | 39 | * |
SomeRandomBloke | 1:a50b6c866a3b | 40 | * Allow object to be constructed with minimum parameters. |
SomeRandomBloke | 1:a50b6c866a3b | 41 | * |
SomeRandomBloke | 1:a50b6c866a3b | 42 | * @param server The IP address of the server to connect to |
SomeRandomBloke | 1:a50b6c866a3b | 43 | * @param port The TCP/IP port on the server to connect to |
SomeRandomBloke | 1:a50b6c866a3b | 44 | * @param callback Callback function to handle subscription to topics |
SomeRandomBloke | 1:a50b6c866a3b | 45 | */ |
SomeRandomBloke | 0:260fb10c0755 | 46 | MQTTClient::MQTTClient(IpAddr server, int port, void (*callback)(char*, char*)) { |
SomeRandomBloke | 0:260fb10c0755 | 47 | this->port = port; |
SomeRandomBloke | 0:260fb10c0755 | 48 | callback_server = callback; |
SomeRandomBloke | 0:260fb10c0755 | 49 | serverIp = server; |
SomeRandomBloke | 0:260fb10c0755 | 50 | connected = false; |
SomeRandomBloke | 0:260fb10c0755 | 51 | sessionOpened = false; |
SomeRandomBloke | 0:260fb10c0755 | 52 | timer.start(); |
SomeRandomBloke | 0:260fb10c0755 | 53 | } |
SomeRandomBloke | 0:260fb10c0755 | 54 | |
SomeRandomBloke | 0:260fb10c0755 | 55 | |
SomeRandomBloke | 1:a50b6c866a3b | 56 | /** MQTT initialisation method |
SomeRandomBloke | 1:a50b6c866a3b | 57 | * |
SomeRandomBloke | 1:a50b6c866a3b | 58 | * Used when default constructor used and need to specify parameters at runtime |
SomeRandomBloke | 1:a50b6c866a3b | 59 | * |
SomeRandomBloke | 1:a50b6c866a3b | 60 | * @param server The IP address of the server to connect to |
SomeRandomBloke | 1:a50b6c866a3b | 61 | * @param port The TCP/IP port on the server to connect to |
SomeRandomBloke | 1:a50b6c866a3b | 62 | * @param callback Callback function to handle subscription to topics |
SomeRandomBloke | 1:a50b6c866a3b | 63 | */ |
SomeRandomBloke | 0:260fb10c0755 | 64 | void MQTTClient::init(IpAddr *server, int port, void (*callback)(char*, char*)) { |
SomeRandomBloke | 0:260fb10c0755 | 65 | this->port = port; |
SomeRandomBloke | 0:260fb10c0755 | 66 | callback_server = callback; |
SomeRandomBloke | 0:260fb10c0755 | 67 | serverIp = *server; |
SomeRandomBloke | 0:260fb10c0755 | 68 | this->userName = NULL; |
SomeRandomBloke | 0:260fb10c0755 | 69 | this->password = NULL; |
SomeRandomBloke | 0:260fb10c0755 | 70 | connected = false; |
SomeRandomBloke | 0:260fb10c0755 | 71 | sessionOpened = false; |
SomeRandomBloke | 0:260fb10c0755 | 72 | timer.start(); |
SomeRandomBloke | 0:260fb10c0755 | 73 | } |
SomeRandomBloke | 0:260fb10c0755 | 74 | |
SomeRandomBloke | 1:a50b6c866a3b | 75 | /** A brief description of the function foo |
SomeRandomBloke | 1:a50b6c866a3b | 76 | * |
SomeRandomBloke | 1:a50b6c866a3b | 77 | * More details about the function goes here |
SomeRandomBloke | 1:a50b6c866a3b | 78 | * and here |
SomeRandomBloke | 1:a50b6c866a3b | 79 | * |
SomeRandomBloke | 1:a50b6c866a3b | 80 | * @param server The IP address of the server to connect to |
SomeRandomBloke | 1:a50b6c866a3b | 81 | * @param port The TCP/IP port on the server to connect to |
SomeRandomBloke | 1:a50b6c866a3b | 82 | * @param userName Pointer to username string, zero terminated. Null for not used |
SomeRandomBloke | 1:a50b6c866a3b | 83 | * @param password Pointer to password string, zero terminated. Null for not used |
SomeRandomBloke | 1:a50b6c866a3b | 84 | * @param callback Callback function to handle subscription to topics |
SomeRandomBloke | 1:a50b6c866a3b | 85 | */ |
SomeRandomBloke | 0:260fb10c0755 | 86 | void MQTTClient::init(IpAddr *server, int port, char *userName, char *password, void (*callback)(char*, char*)) { |
SomeRandomBloke | 0:260fb10c0755 | 87 | this->port = port; |
SomeRandomBloke | 0:260fb10c0755 | 88 | callback_server = callback; |
SomeRandomBloke | 0:260fb10c0755 | 89 | serverIp = *server; |
SomeRandomBloke | 0:260fb10c0755 | 90 | this->userName = userName; |
SomeRandomBloke | 0:260fb10c0755 | 91 | this->password = password; |
SomeRandomBloke | 0:260fb10c0755 | 92 | connected = false; |
SomeRandomBloke | 0:260fb10c0755 | 93 | sessionOpened = false; |
SomeRandomBloke | 0:260fb10c0755 | 94 | timer.start(); |
SomeRandomBloke | 0:260fb10c0755 | 95 | } |
SomeRandomBloke | 0:260fb10c0755 | 96 | |
SomeRandomBloke | 1:a50b6c866a3b | 97 | /** Send a message of specified size |
SomeRandomBloke | 1:a50b6c866a3b | 98 | * |
SomeRandomBloke | 1:a50b6c866a3b | 99 | * @param msg message string |
SomeRandomBloke | 1:a50b6c866a3b | 100 | * @param size Size of the message to send |
SomeRandomBloke | 1:a50b6c866a3b | 101 | * @returns value to indicate message sent successfully or not, -1 for error, 1 for success. |
SomeRandomBloke | 1:a50b6c866a3b | 102 | */ |
SomeRandomBloke | 0:260fb10c0755 | 103 | int MQTTClient::send_data(const char* msg, int size) { |
SomeRandomBloke | 0:260fb10c0755 | 104 | int transLen = pTCPSocket->send(msg, size); |
SomeRandomBloke | 0:260fb10c0755 | 105 | |
SomeRandomBloke | 0:260fb10c0755 | 106 | /*Check send length matches the message length*/ |
SomeRandomBloke | 0:260fb10c0755 | 107 | if (transLen != size) { |
SomeRandomBloke | 0:260fb10c0755 | 108 | for (int i = 0; i < size; i++) { |
SomeRandomBloke | 0:260fb10c0755 | 109 | printf("%x, ", msg[i]); |
SomeRandomBloke | 0:260fb10c0755 | 110 | } |
SomeRandomBloke | 0:260fb10c0755 | 111 | printf("Error on send.\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 112 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 113 | } |
SomeRandomBloke | 0:260fb10c0755 | 114 | return 1; |
SomeRandomBloke | 0:260fb10c0755 | 115 | } |
SomeRandomBloke | 0:260fb10c0755 | 116 | |
SomeRandomBloke | 1:a50b6c866a3b | 117 | /** Start a MQTT session, build CONNECT packet |
SomeRandomBloke | 1:a50b6c866a3b | 118 | * |
SomeRandomBloke | 1:a50b6c866a3b | 119 | * @param id The client name shown on MQTT server. |
SomeRandomBloke | 1:a50b6c866a3b | 120 | * @returns -1 for error, 1 for success |
SomeRandomBloke | 1:a50b6c866a3b | 121 | */ |
SomeRandomBloke | 0:260fb10c0755 | 122 | int MQTTClient::open_session(char* id) { |
SomeRandomBloke | 0:260fb10c0755 | 123 | /*variable header*/ |
SomeRandomBloke | 0:260fb10c0755 | 124 | char var_header[] = {0x00,0x06,0x4d,0x51,0x49,0x73,0x64,0x70,0x03,0x02,0x00,KEEPALIVE/500,0x00,strlen(id)}; |
SomeRandomBloke | 0:260fb10c0755 | 125 | |
SomeRandomBloke | 0:260fb10c0755 | 126 | /*fixed header: 2 bytes, big endian*/ |
SomeRandomBloke | 0:260fb10c0755 | 127 | char fixed_header[] = {MQTTCONNECT,12+strlen(id)+2}; |
SomeRandomBloke | 0:260fb10c0755 | 128 | short usernameLen = strlen( userName ); |
SomeRandomBloke | 0:260fb10c0755 | 129 | short passwordLen = strlen( password ); |
SomeRandomBloke | 0:260fb10c0755 | 130 | var_header[9] |= (usernameLen > 0 ? 0x80 : 0x00 ); |
SomeRandomBloke | 0:260fb10c0755 | 131 | var_header[9] |= (passwordLen > 0 ? 0x40 : 0x00 ); |
SomeRandomBloke | 0:260fb10c0755 | 132 | |
SomeRandomBloke | 0:260fb10c0755 | 133 | // printf("fixed %d, var %d id %d, username %d, password %d\n",sizeof(fixed_header), sizeof(var_header), sizeof(id), usernameLen, passwordLen ); |
SomeRandomBloke | 0:260fb10c0755 | 134 | char packet[sizeof(fixed_header) + sizeof(var_header) + sizeof(id) + usernameLen + (usernameLen > 0 ? 2 : 0) + passwordLen + (passwordLen > 0 ? 2 : 0) ]; |
SomeRandomBloke | 0:260fb10c0755 | 135 | |
SomeRandomBloke | 0:260fb10c0755 | 136 | memset(packet,0,sizeof(packet)); |
SomeRandomBloke | 0:260fb10c0755 | 137 | memcpy(packet,fixed_header,sizeof(fixed_header)); |
SomeRandomBloke | 0:260fb10c0755 | 138 | memcpy(packet+sizeof(fixed_header),var_header,sizeof(var_header)); |
SomeRandomBloke | 0:260fb10c0755 | 139 | memcpy(packet+sizeof(fixed_header)+sizeof(var_header), id, strlen(id)); |
SomeRandomBloke | 0:260fb10c0755 | 140 | if ( usernameLen > 0 ) { |
SomeRandomBloke | 0:260fb10c0755 | 141 | packet[sizeof(fixed_header)+sizeof(var_header)+strlen(id)] = 0x00; |
SomeRandomBloke | 0:260fb10c0755 | 142 | packet[sizeof(fixed_header)+sizeof(var_header)+strlen(id)+1] = usernameLen; |
SomeRandomBloke | 0:260fb10c0755 | 143 | memcpy(packet+sizeof(fixed_header)+sizeof(var_header)+strlen(id)+2, userName, usernameLen); |
SomeRandomBloke | 0:260fb10c0755 | 144 | packet[1] += (usernameLen + 2); |
SomeRandomBloke | 0:260fb10c0755 | 145 | } |
SomeRandomBloke | 0:260fb10c0755 | 146 | if ( passwordLen > 0 ) { |
SomeRandomBloke | 0:260fb10c0755 | 147 | packet[sizeof(fixed_header)+sizeof(var_header)+strlen(id) + usernameLen + (usernameLen > 0 ? 2 : 0) ] = 0x00; |
SomeRandomBloke | 0:260fb10c0755 | 148 | packet[sizeof(fixed_header)+sizeof(var_header)+strlen(id) + usernameLen + (usernameLen > 0 ? 2 : 0)+1] = passwordLen; |
SomeRandomBloke | 0:260fb10c0755 | 149 | memcpy(packet+sizeof(fixed_header)+sizeof(var_header)+strlen(id)+ usernameLen + (usernameLen > 0 ? 2 : 0)+2, password, passwordLen); |
SomeRandomBloke | 0:260fb10c0755 | 150 | packet[1] += (passwordLen + 2); |
SomeRandomBloke | 0:260fb10c0755 | 151 | } |
SomeRandomBloke | 0:260fb10c0755 | 152 | |
SomeRandomBloke | 0:260fb10c0755 | 153 | /* printf("Packet length %d\n", sizeof(packet)); |
SomeRandomBloke | 0:260fb10c0755 | 154 | for (int i = 0; i < sizeof(packet); i++) { |
SomeRandomBloke | 0:260fb10c0755 | 155 | printf("%x, ", packet[i]); |
SomeRandomBloke | 0:260fb10c0755 | 156 | } |
SomeRandomBloke | 0:260fb10c0755 | 157 | printf("\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 158 | */ |
SomeRandomBloke | 0:260fb10c0755 | 159 | if (!send_data(packet, sizeof(packet))) { |
SomeRandomBloke | 0:260fb10c0755 | 160 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 161 | } |
SomeRandomBloke | 0:260fb10c0755 | 162 | // printf("Sent\n"); |
SomeRandomBloke | 0:260fb10c0755 | 163 | return 1; |
SomeRandomBloke | 0:260fb10c0755 | 164 | } |
SomeRandomBloke | 0:260fb10c0755 | 165 | |
SomeRandomBloke | 1:a50b6c866a3b | 166 | /** Open TCP port, connect to server on given IP address. |
SomeRandomBloke | 1:a50b6c866a3b | 167 | * |
SomeRandomBloke | 1:a50b6c866a3b | 168 | * @param id The client name shown on MQTT server. |
SomeRandomBloke | 1:a50b6c866a3b | 169 | * @returns -1: If connect to server failed. -2: Failed to open session on server. 1: Connection accessed. |
SomeRandomBloke | 1:a50b6c866a3b | 170 | */ |
SomeRandomBloke | 0:260fb10c0755 | 171 | int MQTTClient::connect(char* id) { |
SomeRandomBloke | 0:260fb10c0755 | 172 | clientId = id; |
SomeRandomBloke | 0:260fb10c0755 | 173 | |
SomeRandomBloke | 0:260fb10c0755 | 174 | /*Initial TCP socket*/ |
SomeRandomBloke | 0:260fb10c0755 | 175 | pTCPSocket = new TCPSocket; |
SomeRandomBloke | 0:260fb10c0755 | 176 | pTCPSocket->setOnEvent(this, &MQTTClient::onTCPSocketEvent); |
SomeRandomBloke | 0:260fb10c0755 | 177 | |
SomeRandomBloke | 0:260fb10c0755 | 178 | host.setPort(port); |
SomeRandomBloke | 0:260fb10c0755 | 179 | host.setIp(serverIp); |
SomeRandomBloke | 0:260fb10c0755 | 180 | host.setName("localhost"); |
SomeRandomBloke | 0:260fb10c0755 | 181 | |
SomeRandomBloke | 0:260fb10c0755 | 182 | /*Trying to connect to host*/ |
SomeRandomBloke | 0:260fb10c0755 | 183 | printf("Trying to connect to host..\r\n\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 184 | TCPSocketErr err = pTCPSocket->connect(host); |
SomeRandomBloke | 0:260fb10c0755 | 185 | |
SomeRandomBloke | 0:260fb10c0755 | 186 | Net::poll(); |
SomeRandomBloke | 0:260fb10c0755 | 187 | if (err) { |
SomeRandomBloke | 0:260fb10c0755 | 188 | printf("Error connecting to host [%d]\r\n", (int) err); |
SomeRandomBloke | 0:260fb10c0755 | 189 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 190 | } |
SomeRandomBloke | 0:260fb10c0755 | 191 | printf("Connect to host sucessed..\r\n\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 192 | |
SomeRandomBloke | 0:260fb10c0755 | 193 | /*Wait TCP connection with server to be established*/ |
SomeRandomBloke | 0:260fb10c0755 | 194 | int i = 0; |
SomeRandomBloke | 0:260fb10c0755 | 195 | while (!connected) { |
SomeRandomBloke | 0:260fb10c0755 | 196 | Net::poll(); |
SomeRandomBloke | 0:260fb10c0755 | 197 | wait(1); |
SomeRandomBloke | 0:260fb10c0755 | 198 | i++; |
SomeRandomBloke | 0:260fb10c0755 | 199 | printf("Wait for connections %d..\r\n", i); |
SomeRandomBloke | 0:260fb10c0755 | 200 | if (i == 35) {//If wait too long, give up. |
SomeRandomBloke | 0:260fb10c0755 | 201 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 202 | } |
SomeRandomBloke | 0:260fb10c0755 | 203 | } |
SomeRandomBloke | 0:260fb10c0755 | 204 | |
SomeRandomBloke | 0:260fb10c0755 | 205 | /*Send open session message to server*/ |
SomeRandomBloke | 0:260fb10c0755 | 206 | open_session(id); |
SomeRandomBloke | 0:260fb10c0755 | 207 | |
SomeRandomBloke | 0:260fb10c0755 | 208 | /*Wait server notice of open sesion*/ |
SomeRandomBloke | 0:260fb10c0755 | 209 | while (!sessionOpened) { |
SomeRandomBloke | 0:260fb10c0755 | 210 | Net::poll(); |
SomeRandomBloke | 0:260fb10c0755 | 211 | wait(1); |
SomeRandomBloke | 0:260fb10c0755 | 212 | if (!connected) { |
SomeRandomBloke | 0:260fb10c0755 | 213 | break; |
SomeRandomBloke | 0:260fb10c0755 | 214 | } |
SomeRandomBloke | 0:260fb10c0755 | 215 | printf("Wait for session..\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 216 | } |
SomeRandomBloke | 0:260fb10c0755 | 217 | if (!connected) { |
SomeRandomBloke | 0:260fb10c0755 | 218 | return -2; |
SomeRandomBloke | 0:260fb10c0755 | 219 | } |
SomeRandomBloke | 0:260fb10c0755 | 220 | lastActivity = timer.read_ms(); |
SomeRandomBloke | 0:260fb10c0755 | 221 | return 1; |
SomeRandomBloke | 0:260fb10c0755 | 222 | } |
SomeRandomBloke | 0:260fb10c0755 | 223 | |
SomeRandomBloke | 1:a50b6c866a3b | 224 | /** Publish a message on a topic. |
SomeRandomBloke | 1:a50b6c866a3b | 225 | * |
SomeRandomBloke | 1:a50b6c866a3b | 226 | * @param pub_topic The topic name the massage will be publish on. |
SomeRandomBloke | 1:a50b6c866a3b | 227 | * @param msg The massage to be published. |
SomeRandomBloke | 1:a50b6c866a3b | 228 | * @returns -1: Failed to publish message. 1: Publish sucessed. |
SomeRandomBloke | 1:a50b6c866a3b | 229 | */ |
SomeRandomBloke | 0:260fb10c0755 | 230 | int MQTTClient::publish(char* pub_topic, char* msg) { |
SomeRandomBloke | 0:260fb10c0755 | 231 | uint8_t var_header_pub[strlen(pub_topic)+3]; |
SomeRandomBloke | 0:260fb10c0755 | 232 | strcpy((char *)&var_header_pub[2], pub_topic); |
SomeRandomBloke | 0:260fb10c0755 | 233 | var_header_pub[0] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 234 | var_header_pub[1] = strlen(pub_topic); |
SomeRandomBloke | 0:260fb10c0755 | 235 | var_header_pub[sizeof(var_header_pub)-1] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 236 | |
SomeRandomBloke | 0:260fb10c0755 | 237 | uint8_t fixed_header_pub[] = {MQTTPUBLISH,sizeof(var_header_pub)+strlen(msg)}; |
SomeRandomBloke | 0:260fb10c0755 | 238 | |
SomeRandomBloke | 0:260fb10c0755 | 239 | uint8_t packet_pub[sizeof(fixed_header_pub)+sizeof(var_header_pub)+strlen(msg)]; |
SomeRandomBloke | 0:260fb10c0755 | 240 | memset(packet_pub,0,sizeof(packet_pub)); |
SomeRandomBloke | 0:260fb10c0755 | 241 | memcpy(packet_pub,fixed_header_pub,sizeof(fixed_header_pub)); |
SomeRandomBloke | 0:260fb10c0755 | 242 | memcpy(packet_pub+sizeof(fixed_header_pub),var_header_pub,sizeof(var_header_pub)); |
SomeRandomBloke | 0:260fb10c0755 | 243 | memcpy(packet_pub+sizeof(fixed_header_pub)+sizeof(var_header_pub),msg,strlen(msg)); |
SomeRandomBloke | 0:260fb10c0755 | 244 | |
SomeRandomBloke | 0:260fb10c0755 | 245 | if (!send_data((char*)packet_pub, sizeof(packet_pub))) { |
SomeRandomBloke | 0:260fb10c0755 | 246 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 247 | } |
SomeRandomBloke | 0:260fb10c0755 | 248 | return 1; |
SomeRandomBloke | 0:260fb10c0755 | 249 | } |
SomeRandomBloke | 0:260fb10c0755 | 250 | |
SomeRandomBloke | 1:a50b6c866a3b | 251 | /** Disconnect from server |
SomeRandomBloke | 1:a50b6c866a3b | 252 | */ |
SomeRandomBloke | 0:260fb10c0755 | 253 | void MQTTClient::disconnect() { |
SomeRandomBloke | 0:260fb10c0755 | 254 | char packet_224[] = {0xe0, 0x00}; |
SomeRandomBloke | 0:260fb10c0755 | 255 | send_data((char*)packet_224, 2); |
SomeRandomBloke | 0:260fb10c0755 | 256 | |
SomeRandomBloke | 0:260fb10c0755 | 257 | connected = false; |
SomeRandomBloke | 0:260fb10c0755 | 258 | } |
SomeRandomBloke | 0:260fb10c0755 | 259 | |
SomeRandomBloke | 1:a50b6c866a3b | 260 | /** Read data from receive packet |
SomeRandomBloke | 1:a50b6c866a3b | 261 | * Determine what needs to be done with packet. |
SomeRandomBloke | 1:a50b6c866a3b | 262 | */ |
SomeRandomBloke | 0:260fb10c0755 | 263 | void MQTTClient::read_data() { |
SomeRandomBloke | 0:260fb10c0755 | 264 | char buffer[1024]; |
SomeRandomBloke | 0:260fb10c0755 | 265 | int len = 0, readLen; |
SomeRandomBloke | 0:260fb10c0755 | 266 | |
SomeRandomBloke | 0:260fb10c0755 | 267 | while ((readLen = pTCPSocket->recv(buffer, 1024)) != 0) { |
SomeRandomBloke | 0:260fb10c0755 | 268 | len += readLen; |
SomeRandomBloke | 0:260fb10c0755 | 269 | } |
SomeRandomBloke | 0:260fb10c0755 | 270 | |
SomeRandomBloke | 0:260fb10c0755 | 271 | buffer[len] = '\0'; |
SomeRandomBloke | 0:260fb10c0755 | 272 | |
SomeRandomBloke | 0:260fb10c0755 | 273 | |
SomeRandomBloke | 0:260fb10c0755 | 274 | printf("Read length: %d %d\r\n", len, readLen); |
SomeRandomBloke | 0:260fb10c0755 | 275 | |
SomeRandomBloke | 0:260fb10c0755 | 276 | for (int i = 0; i < len; i++) { |
SomeRandomBloke | 0:260fb10c0755 | 277 | printf("%2X ", buffer[i]); |
SomeRandomBloke | 0:260fb10c0755 | 278 | } |
SomeRandomBloke | 0:260fb10c0755 | 279 | printf("\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 280 | |
SomeRandomBloke | 0:260fb10c0755 | 281 | char type = buffer[0]>>4; |
SomeRandomBloke | 0:260fb10c0755 | 282 | if ( type == 2 ) { // CONNACK |
SomeRandomBloke | 0:260fb10c0755 | 283 | printf("CONNACK\n"); |
SomeRandomBloke | 0:260fb10c0755 | 284 | |
SomeRandomBloke | 0:260fb10c0755 | 285 | } else if (type == 3) { // PUBLISH |
SomeRandomBloke | 0:260fb10c0755 | 286 | if (callback_server) { |
SomeRandomBloke | 0:260fb10c0755 | 287 | short index = 1; |
SomeRandomBloke | 0:260fb10c0755 | 288 | short multiplier = 1; |
SomeRandomBloke | 0:260fb10c0755 | 289 | short value = 0; |
SomeRandomBloke | 0:260fb10c0755 | 290 | uint8_t digit; |
SomeRandomBloke | 0:260fb10c0755 | 291 | do { |
SomeRandomBloke | 0:260fb10c0755 | 292 | digit = buffer[index++]; |
SomeRandomBloke | 0:260fb10c0755 | 293 | value += (digit & 127) * multiplier; |
SomeRandomBloke | 0:260fb10c0755 | 294 | multiplier *= 128; |
SomeRandomBloke | 0:260fb10c0755 | 295 | } while ((digit & 128) != 0); |
SomeRandomBloke | 0:260fb10c0755 | 296 | printf( "variable length %d, index %d\n", value, index ); |
SomeRandomBloke | 0:260fb10c0755 | 297 | |
SomeRandomBloke | 0:260fb10c0755 | 298 | // uint8_t tl = (buffer[2]<<3)+buffer[3]; |
SomeRandomBloke | 0:260fb10c0755 | 299 | uint8_t tl = (buffer[index]<<3)+buffer[index+1]; |
SomeRandomBloke | 0:260fb10c0755 | 300 | printf("Topic len %d\n",tl); |
SomeRandomBloke | 0:260fb10c0755 | 301 | char topic[tl+1]; |
SomeRandomBloke | 0:260fb10c0755 | 302 | for (int i=0; i<tl; i++) { |
SomeRandomBloke | 0:260fb10c0755 | 303 | topic[i] = buffer[index+2+i]; |
SomeRandomBloke | 0:260fb10c0755 | 304 | } |
SomeRandomBloke | 0:260fb10c0755 | 305 | topic[tl] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 306 | // ignore msgID - only support QoS 0 subs |
SomeRandomBloke | 0:260fb10c0755 | 307 | char *payload = buffer+index+2+tl; |
SomeRandomBloke | 0:260fb10c0755 | 308 | callback_server(topic,(char*)payload); |
SomeRandomBloke | 0:260fb10c0755 | 309 | } |
SomeRandomBloke | 0:260fb10c0755 | 310 | } else if (type == 12) { // PINGREG -- Ask for alive |
SomeRandomBloke | 0:260fb10c0755 | 311 | char packet_208[] = {0xd0, 0x00}; |
SomeRandomBloke | 0:260fb10c0755 | 312 | send_data((char*)packet_208, 2); |
SomeRandomBloke | 0:260fb10c0755 | 313 | lastActivity = timer.read_ms(); |
SomeRandomBloke | 0:260fb10c0755 | 314 | } |
SomeRandomBloke | 0:260fb10c0755 | 315 | } |
SomeRandomBloke | 0:260fb10c0755 | 316 | |
SomeRandomBloke | 1:a50b6c866a3b | 317 | /** Check for session opened |
SomeRandomBloke | 1:a50b6c866a3b | 318 | */ |
SomeRandomBloke | 0:260fb10c0755 | 319 | void MQTTClient::read_open_session() { |
SomeRandomBloke | 0:260fb10c0755 | 320 | char buffer[32]; |
SomeRandomBloke | 0:260fb10c0755 | 321 | int len = 0, readLen; |
SomeRandomBloke | 0:260fb10c0755 | 322 | |
SomeRandomBloke | 0:260fb10c0755 | 323 | while ((readLen = pTCPSocket->recv(buffer, 32)) != 0) { |
SomeRandomBloke | 0:260fb10c0755 | 324 | len += readLen; |
SomeRandomBloke | 0:260fb10c0755 | 325 | } |
SomeRandomBloke | 0:260fb10c0755 | 326 | |
SomeRandomBloke | 0:260fb10c0755 | 327 | if (len == 4 && buffer[3] == 0) { |
SomeRandomBloke | 0:260fb10c0755 | 328 | printf("Session opened\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 329 | sessionOpened = true; |
SomeRandomBloke | 0:260fb10c0755 | 330 | } |
SomeRandomBloke | 0:260fb10c0755 | 331 | } |
SomeRandomBloke | 0:260fb10c0755 | 332 | |
SomeRandomBloke | 1:a50b6c866a3b | 333 | /** Subscribe to a topic |
SomeRandomBloke | 1:a50b6c866a3b | 334 | * |
SomeRandomBloke | 1:a50b6c866a3b | 335 | * @param topic The topic name to be subscribed. |
SomeRandomBloke | 1:a50b6c866a3b | 336 | * @returns -1: Failed to subscribe to topic. 1: Subscribe sucessed. |
SomeRandomBloke | 1:a50b6c866a3b | 337 | */ |
SomeRandomBloke | 0:260fb10c0755 | 338 | int MQTTClient::subscribe(char* topic) { |
SomeRandomBloke | 0:260fb10c0755 | 339 | |
SomeRandomBloke | 0:260fb10c0755 | 340 | if (connected) { |
SomeRandomBloke | 0:260fb10c0755 | 341 | uint8_t var_header_topic[] = {0,10}; |
SomeRandomBloke | 0:260fb10c0755 | 342 | uint8_t fixed_header_topic[] = {MQTTSUBSCRIBE,sizeof(var_header_topic)+strlen(topic)+3}; |
SomeRandomBloke | 0:260fb10c0755 | 343 | |
SomeRandomBloke | 0:260fb10c0755 | 344 | //utf topic |
SomeRandomBloke | 0:260fb10c0755 | 345 | uint8_t utf_topic[strlen(topic)+3]; |
SomeRandomBloke | 0:260fb10c0755 | 346 | strcpy((char *)&utf_topic[2], topic); |
SomeRandomBloke | 0:260fb10c0755 | 347 | |
SomeRandomBloke | 0:260fb10c0755 | 348 | utf_topic[0] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 349 | utf_topic[1] = strlen(topic); |
SomeRandomBloke | 0:260fb10c0755 | 350 | utf_topic[sizeof(utf_topic)-1] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 351 | |
SomeRandomBloke | 0:260fb10c0755 | 352 | char packet_topic[sizeof(var_header_topic)+sizeof(fixed_header_topic)+strlen(topic)+3]; |
SomeRandomBloke | 0:260fb10c0755 | 353 | memset(packet_topic,0,sizeof(packet_topic)); |
SomeRandomBloke | 0:260fb10c0755 | 354 | memcpy(packet_topic,fixed_header_topic,sizeof(fixed_header_topic)); |
SomeRandomBloke | 0:260fb10c0755 | 355 | memcpy(packet_topic+sizeof(fixed_header_topic),var_header_topic,sizeof(var_header_topic)); |
SomeRandomBloke | 0:260fb10c0755 | 356 | memcpy(packet_topic+sizeof(fixed_header_topic)+sizeof(var_header_topic),utf_topic,sizeof(utf_topic)); |
SomeRandomBloke | 0:260fb10c0755 | 357 | |
SomeRandomBloke | 0:260fb10c0755 | 358 | if (!send_data(packet_topic, sizeof(packet_topic))) { |
SomeRandomBloke | 0:260fb10c0755 | 359 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 360 | } |
SomeRandomBloke | 0:260fb10c0755 | 361 | return 1; |
SomeRandomBloke | 0:260fb10c0755 | 362 | } |
SomeRandomBloke | 0:260fb10c0755 | 363 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 364 | } |
SomeRandomBloke | 0:260fb10c0755 | 365 | |
SomeRandomBloke | 1:a50b6c866a3b | 366 | /** Send heartbeat/keep-alive message |
SomeRandomBloke | 1:a50b6c866a3b | 367 | * |
SomeRandomBloke | 1:a50b6c866a3b | 368 | */ |
SomeRandomBloke | 0:260fb10c0755 | 369 | void MQTTClient::live() { |
SomeRandomBloke | 0:260fb10c0755 | 370 | if (connected) { |
SomeRandomBloke | 0:260fb10c0755 | 371 | int t = timer.read_ms(); |
SomeRandomBloke | 0:260fb10c0755 | 372 | if (t - lastActivity > KEEPALIVE) { |
SomeRandomBloke | 0:260fb10c0755 | 373 | //Send 192 0 to broker |
SomeRandomBloke | 0:260fb10c0755 | 374 | printf("Send 192\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 375 | char packet_192[] = {0xc0, 0x00}; |
SomeRandomBloke | 0:260fb10c0755 | 376 | send_data((char*)packet_192, 2); |
SomeRandomBloke | 0:260fb10c0755 | 377 | lastActivity = t; |
SomeRandomBloke | 0:260fb10c0755 | 378 | } |
SomeRandomBloke | 0:260fb10c0755 | 379 | } |
SomeRandomBloke | 0:260fb10c0755 | 380 | } |
SomeRandomBloke | 0:260fb10c0755 | 381 | |
SomeRandomBloke | 1:a50b6c866a3b | 382 | /** TCP Socket event handling |
SomeRandomBloke | 1:a50b6c866a3b | 383 | * |
SomeRandomBloke | 1:a50b6c866a3b | 384 | * @param e Event object |
SomeRandomBloke | 1:a50b6c866a3b | 385 | */ |
SomeRandomBloke | 0:260fb10c0755 | 386 | void MQTTClient::onTCPSocketEvent(TCPSocketEvent e) { |
SomeRandomBloke | 0:260fb10c0755 | 387 | switch (e) { |
SomeRandomBloke | 0:260fb10c0755 | 388 | case TCPSOCKET_ACCEPT: |
SomeRandomBloke | 0:260fb10c0755 | 389 | printf("New TCPSocketEvent: TCPSOCKET_ACCEPT\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 390 | break; |
SomeRandomBloke | 0:260fb10c0755 | 391 | case TCPSOCKET_CONNECTED: |
SomeRandomBloke | 0:260fb10c0755 | 392 | printf("New TCPSocketEvent: TCPSOCKET_CONNECTED\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 393 | connected = true; |
SomeRandomBloke | 0:260fb10c0755 | 394 | break; |
SomeRandomBloke | 0:260fb10c0755 | 395 | case TCPSOCKET_WRITEABLE: |
SomeRandomBloke | 0:260fb10c0755 | 396 | printf("New TCPSocketEvent: TCPSOCKET_WRITEABLE\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 397 | break; |
SomeRandomBloke | 0:260fb10c0755 | 398 | case TCPSOCKET_READABLE: |
SomeRandomBloke | 0:260fb10c0755 | 399 | printf("New TCPSocketEvent: TCPSOCKET_READABLE\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 400 | if (!sessionOpened) { |
SomeRandomBloke | 0:260fb10c0755 | 401 | read_open_session(); |
SomeRandomBloke | 0:260fb10c0755 | 402 | } |
SomeRandomBloke | 0:260fb10c0755 | 403 | read_data(); |
SomeRandomBloke | 0:260fb10c0755 | 404 | break; |
SomeRandomBloke | 0:260fb10c0755 | 405 | case TCPSOCKET_CONTIMEOUT: |
SomeRandomBloke | 0:260fb10c0755 | 406 | printf("New TCPSocketEvent: TCPSOCKET_CONTIMEOUT\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 407 | break; |
SomeRandomBloke | 0:260fb10c0755 | 408 | case TCPSOCKET_CONRST: |
SomeRandomBloke | 0:260fb10c0755 | 409 | printf("New TCPSocketEvent: TCPSOCKET_CONRST\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 410 | break; |
SomeRandomBloke | 0:260fb10c0755 | 411 | case TCPSOCKET_CONABRT: |
SomeRandomBloke | 0:260fb10c0755 | 412 | printf("New TCPSocketEvent: TCPSOCKET_CONABRT\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 413 | break; |
SomeRandomBloke | 0:260fb10c0755 | 414 | case TCPSOCKET_ERROR: |
SomeRandomBloke | 0:260fb10c0755 | 415 | printf("New TCPSocketEvent: TCPSOCKET_ERROR\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 416 | break; |
SomeRandomBloke | 0:260fb10c0755 | 417 | case TCPSOCKET_DISCONNECTED: |
SomeRandomBloke | 0:260fb10c0755 | 418 | printf("New TCPSocketEvent: TCPSOCKET_DISCONNECTED\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 419 | pTCPSocket->close(); |
SomeRandomBloke | 0:260fb10c0755 | 420 | connected = false; |
SomeRandomBloke | 0:260fb10c0755 | 421 | break; |
SomeRandomBloke | 0:260fb10c0755 | 422 | } |
SomeRandomBloke | 0:260fb10c0755 | 423 | } |