result of success : 1 to 0, max msg size: 127B to 16383B, fixed existing bugs
Fork of MQTTClient by
MQTTClient.cpp@7:c12d2bfe40e2, 2012-04-04 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Wed Apr 04 20:59:13 2012 +0000
- Revision:
- 7:c12d2bfe40e2
- Parent:
- 4:9e25b78e0352
- Child:
- 8:f7afec229461
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 4:9e25b78e0352 | 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 | 7:c12d2bfe40e2 | 209 | i = 0; |
SomeRandomBloke | 0:260fb10c0755 | 210 | while (!sessionOpened) { |
SomeRandomBloke | 0:260fb10c0755 | 211 | Net::poll(); |
SomeRandomBloke | 0:260fb10c0755 | 212 | wait(1); |
SomeRandomBloke | 7:c12d2bfe40e2 | 213 | if (!connected || ++i >30) { |
SomeRandomBloke | 0:260fb10c0755 | 214 | break; |
SomeRandomBloke | 0:260fb10c0755 | 215 | } |
SomeRandomBloke | 0:260fb10c0755 | 216 | printf("Wait for session..\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 217 | } |
SomeRandomBloke | 0:260fb10c0755 | 218 | if (!connected) { |
SomeRandomBloke | 0:260fb10c0755 | 219 | return -2; |
SomeRandomBloke | 0:260fb10c0755 | 220 | } |
SomeRandomBloke | 0:260fb10c0755 | 221 | lastActivity = timer.read_ms(); |
SomeRandomBloke | 0:260fb10c0755 | 222 | return 1; |
SomeRandomBloke | 0:260fb10c0755 | 223 | } |
SomeRandomBloke | 0:260fb10c0755 | 224 | |
SomeRandomBloke | 1:a50b6c866a3b | 225 | /** Publish a message on a topic. |
SomeRandomBloke | 1:a50b6c866a3b | 226 | * |
SomeRandomBloke | 1:a50b6c866a3b | 227 | * @param pub_topic The topic name the massage will be publish on. |
SomeRandomBloke | 1:a50b6c866a3b | 228 | * @param msg The massage to be published. |
SomeRandomBloke | 1:a50b6c866a3b | 229 | * @returns -1: Failed to publish message. 1: Publish sucessed. |
SomeRandomBloke | 1:a50b6c866a3b | 230 | */ |
SomeRandomBloke | 0:260fb10c0755 | 231 | int MQTTClient::publish(char* pub_topic, char* msg) { |
SomeRandomBloke | 0:260fb10c0755 | 232 | uint8_t var_header_pub[strlen(pub_topic)+3]; |
SomeRandomBloke | 0:260fb10c0755 | 233 | strcpy((char *)&var_header_pub[2], pub_topic); |
SomeRandomBloke | 0:260fb10c0755 | 234 | var_header_pub[0] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 235 | var_header_pub[1] = strlen(pub_topic); |
SomeRandomBloke | 0:260fb10c0755 | 236 | var_header_pub[sizeof(var_header_pub)-1] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 237 | |
SomeRandomBloke | 0:260fb10c0755 | 238 | uint8_t fixed_header_pub[] = {MQTTPUBLISH,sizeof(var_header_pub)+strlen(msg)}; |
SomeRandomBloke | 0:260fb10c0755 | 239 | |
SomeRandomBloke | 0:260fb10c0755 | 240 | uint8_t packet_pub[sizeof(fixed_header_pub)+sizeof(var_header_pub)+strlen(msg)]; |
SomeRandomBloke | 0:260fb10c0755 | 241 | memset(packet_pub,0,sizeof(packet_pub)); |
SomeRandomBloke | 0:260fb10c0755 | 242 | memcpy(packet_pub,fixed_header_pub,sizeof(fixed_header_pub)); |
SomeRandomBloke | 0:260fb10c0755 | 243 | memcpy(packet_pub+sizeof(fixed_header_pub),var_header_pub,sizeof(var_header_pub)); |
SomeRandomBloke | 0:260fb10c0755 | 244 | memcpy(packet_pub+sizeof(fixed_header_pub)+sizeof(var_header_pub),msg,strlen(msg)); |
SomeRandomBloke | 0:260fb10c0755 | 245 | |
SomeRandomBloke | 0:260fb10c0755 | 246 | if (!send_data((char*)packet_pub, sizeof(packet_pub))) { |
SomeRandomBloke | 0:260fb10c0755 | 247 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 248 | } |
SomeRandomBloke | 0:260fb10c0755 | 249 | return 1; |
SomeRandomBloke | 0:260fb10c0755 | 250 | } |
SomeRandomBloke | 0:260fb10c0755 | 251 | |
SomeRandomBloke | 1:a50b6c866a3b | 252 | /** Disconnect from server |
SomeRandomBloke | 1:a50b6c866a3b | 253 | */ |
SomeRandomBloke | 0:260fb10c0755 | 254 | void MQTTClient::disconnect() { |
SomeRandomBloke | 0:260fb10c0755 | 255 | char packet_224[] = {0xe0, 0x00}; |
SomeRandomBloke | 0:260fb10c0755 | 256 | send_data((char*)packet_224, 2); |
SomeRandomBloke | 0:260fb10c0755 | 257 | |
SomeRandomBloke | 0:260fb10c0755 | 258 | connected = false; |
SomeRandomBloke | 0:260fb10c0755 | 259 | } |
SomeRandomBloke | 0:260fb10c0755 | 260 | |
SomeRandomBloke | 1:a50b6c866a3b | 261 | /** Read data from receive packet |
SomeRandomBloke | 1:a50b6c866a3b | 262 | * Determine what needs to be done with packet. |
SomeRandomBloke | 1:a50b6c866a3b | 263 | */ |
SomeRandomBloke | 0:260fb10c0755 | 264 | void MQTTClient::read_data() { |
SomeRandomBloke | 0:260fb10c0755 | 265 | char buffer[1024]; |
SomeRandomBloke | 0:260fb10c0755 | 266 | int len = 0, readLen; |
SomeRandomBloke | 0:260fb10c0755 | 267 | |
SomeRandomBloke | 0:260fb10c0755 | 268 | while ((readLen = pTCPSocket->recv(buffer, 1024)) != 0) { |
SomeRandomBloke | 0:260fb10c0755 | 269 | len += readLen; |
SomeRandomBloke | 0:260fb10c0755 | 270 | } |
SomeRandomBloke | 0:260fb10c0755 | 271 | |
SomeRandomBloke | 0:260fb10c0755 | 272 | buffer[len] = '\0'; |
SomeRandomBloke | 0:260fb10c0755 | 273 | |
SomeRandomBloke | 0:260fb10c0755 | 274 | |
SomeRandomBloke | 0:260fb10c0755 | 275 | printf("Read length: %d %d\r\n", len, readLen); |
SomeRandomBloke | 0:260fb10c0755 | 276 | |
SomeRandomBloke | 0:260fb10c0755 | 277 | for (int i = 0; i < len; i++) { |
SomeRandomBloke | 0:260fb10c0755 | 278 | printf("%2X ", buffer[i]); |
SomeRandomBloke | 0:260fb10c0755 | 279 | } |
SomeRandomBloke | 0:260fb10c0755 | 280 | printf("\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 281 | |
SomeRandomBloke | 0:260fb10c0755 | 282 | char type = buffer[0]>>4; |
SomeRandomBloke | 0:260fb10c0755 | 283 | if ( type == 2 ) { // CONNACK |
SomeRandomBloke | 0:260fb10c0755 | 284 | printf("CONNACK\n"); |
SomeRandomBloke | 0:260fb10c0755 | 285 | |
SomeRandomBloke | 0:260fb10c0755 | 286 | } else if (type == 3) { // PUBLISH |
SomeRandomBloke | 0:260fb10c0755 | 287 | if (callback_server) { |
SomeRandomBloke | 0:260fb10c0755 | 288 | short index = 1; |
SomeRandomBloke | 0:260fb10c0755 | 289 | short multiplier = 1; |
SomeRandomBloke | 0:260fb10c0755 | 290 | short value = 0; |
SomeRandomBloke | 0:260fb10c0755 | 291 | uint8_t digit; |
SomeRandomBloke | 0:260fb10c0755 | 292 | do { |
SomeRandomBloke | 0:260fb10c0755 | 293 | digit = buffer[index++]; |
SomeRandomBloke | 0:260fb10c0755 | 294 | value += (digit & 127) * multiplier; |
SomeRandomBloke | 0:260fb10c0755 | 295 | multiplier *= 128; |
SomeRandomBloke | 0:260fb10c0755 | 296 | } while ((digit & 128) != 0); |
SomeRandomBloke | 0:260fb10c0755 | 297 | printf( "variable length %d, index %d\n", value, index ); |
SomeRandomBloke | 0:260fb10c0755 | 298 | |
SomeRandomBloke | 0:260fb10c0755 | 299 | // uint8_t tl = (buffer[2]<<3)+buffer[3]; |
SomeRandomBloke | 0:260fb10c0755 | 300 | uint8_t tl = (buffer[index]<<3)+buffer[index+1]; |
SomeRandomBloke | 0:260fb10c0755 | 301 | printf("Topic len %d\n",tl); |
SomeRandomBloke | 0:260fb10c0755 | 302 | char topic[tl+1]; |
SomeRandomBloke | 0:260fb10c0755 | 303 | for (int i=0; i<tl; i++) { |
SomeRandomBloke | 0:260fb10c0755 | 304 | topic[i] = buffer[index+2+i]; |
SomeRandomBloke | 0:260fb10c0755 | 305 | } |
SomeRandomBloke | 0:260fb10c0755 | 306 | topic[tl] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 307 | // ignore msgID - only support QoS 0 subs |
SomeRandomBloke | 0:260fb10c0755 | 308 | char *payload = buffer+index+2+tl; |
SomeRandomBloke | 0:260fb10c0755 | 309 | callback_server(topic,(char*)payload); |
SomeRandomBloke | 0:260fb10c0755 | 310 | } |
SomeRandomBloke | 0:260fb10c0755 | 311 | } else if (type == 12) { // PINGREG -- Ask for alive |
SomeRandomBloke | 0:260fb10c0755 | 312 | char packet_208[] = {0xd0, 0x00}; |
SomeRandomBloke | 0:260fb10c0755 | 313 | send_data((char*)packet_208, 2); |
SomeRandomBloke | 0:260fb10c0755 | 314 | lastActivity = timer.read_ms(); |
SomeRandomBloke | 0:260fb10c0755 | 315 | } |
SomeRandomBloke | 0:260fb10c0755 | 316 | } |
SomeRandomBloke | 0:260fb10c0755 | 317 | |
SomeRandomBloke | 1:a50b6c866a3b | 318 | /** Check for session opened |
SomeRandomBloke | 1:a50b6c866a3b | 319 | */ |
SomeRandomBloke | 0:260fb10c0755 | 320 | void MQTTClient::read_open_session() { |
SomeRandomBloke | 0:260fb10c0755 | 321 | char buffer[32]; |
SomeRandomBloke | 0:260fb10c0755 | 322 | int len = 0, readLen; |
SomeRandomBloke | 0:260fb10c0755 | 323 | |
SomeRandomBloke | 0:260fb10c0755 | 324 | while ((readLen = pTCPSocket->recv(buffer, 32)) != 0) { |
SomeRandomBloke | 0:260fb10c0755 | 325 | len += readLen; |
SomeRandomBloke | 0:260fb10c0755 | 326 | } |
SomeRandomBloke | 0:260fb10c0755 | 327 | |
SomeRandomBloke | 0:260fb10c0755 | 328 | if (len == 4 && buffer[3] == 0) { |
SomeRandomBloke | 0:260fb10c0755 | 329 | printf("Session opened\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 330 | sessionOpened = true; |
SomeRandomBloke | 0:260fb10c0755 | 331 | } |
SomeRandomBloke | 0:260fb10c0755 | 332 | } |
SomeRandomBloke | 0:260fb10c0755 | 333 | |
SomeRandomBloke | 1:a50b6c866a3b | 334 | /** Subscribe to a topic |
SomeRandomBloke | 1:a50b6c866a3b | 335 | * |
SomeRandomBloke | 1:a50b6c866a3b | 336 | * @param topic The topic name to be subscribed. |
SomeRandomBloke | 1:a50b6c866a3b | 337 | * @returns -1: Failed to subscribe to topic. 1: Subscribe sucessed. |
SomeRandomBloke | 1:a50b6c866a3b | 338 | */ |
SomeRandomBloke | 0:260fb10c0755 | 339 | int MQTTClient::subscribe(char* topic) { |
SomeRandomBloke | 0:260fb10c0755 | 340 | |
SomeRandomBloke | 0:260fb10c0755 | 341 | if (connected) { |
SomeRandomBloke | 0:260fb10c0755 | 342 | uint8_t var_header_topic[] = {0,10}; |
SomeRandomBloke | 0:260fb10c0755 | 343 | uint8_t fixed_header_topic[] = {MQTTSUBSCRIBE,sizeof(var_header_topic)+strlen(topic)+3}; |
SomeRandomBloke | 0:260fb10c0755 | 344 | |
SomeRandomBloke | 0:260fb10c0755 | 345 | //utf topic |
SomeRandomBloke | 0:260fb10c0755 | 346 | uint8_t utf_topic[strlen(topic)+3]; |
SomeRandomBloke | 0:260fb10c0755 | 347 | strcpy((char *)&utf_topic[2], topic); |
SomeRandomBloke | 0:260fb10c0755 | 348 | |
SomeRandomBloke | 0:260fb10c0755 | 349 | utf_topic[0] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 350 | utf_topic[1] = strlen(topic); |
SomeRandomBloke | 0:260fb10c0755 | 351 | utf_topic[sizeof(utf_topic)-1] = 0; |
SomeRandomBloke | 0:260fb10c0755 | 352 | |
SomeRandomBloke | 0:260fb10c0755 | 353 | char packet_topic[sizeof(var_header_topic)+sizeof(fixed_header_topic)+strlen(topic)+3]; |
SomeRandomBloke | 0:260fb10c0755 | 354 | memset(packet_topic,0,sizeof(packet_topic)); |
SomeRandomBloke | 0:260fb10c0755 | 355 | memcpy(packet_topic,fixed_header_topic,sizeof(fixed_header_topic)); |
SomeRandomBloke | 0:260fb10c0755 | 356 | memcpy(packet_topic+sizeof(fixed_header_topic),var_header_topic,sizeof(var_header_topic)); |
SomeRandomBloke | 0:260fb10c0755 | 357 | memcpy(packet_topic+sizeof(fixed_header_topic)+sizeof(var_header_topic),utf_topic,sizeof(utf_topic)); |
SomeRandomBloke | 0:260fb10c0755 | 358 | |
SomeRandomBloke | 0:260fb10c0755 | 359 | if (!send_data(packet_topic, sizeof(packet_topic))) { |
SomeRandomBloke | 0:260fb10c0755 | 360 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 361 | } |
SomeRandomBloke | 0:260fb10c0755 | 362 | return 1; |
SomeRandomBloke | 0:260fb10c0755 | 363 | } |
SomeRandomBloke | 0:260fb10c0755 | 364 | return -1; |
SomeRandomBloke | 0:260fb10c0755 | 365 | } |
SomeRandomBloke | 0:260fb10c0755 | 366 | |
SomeRandomBloke | 1:a50b6c866a3b | 367 | /** Send heartbeat/keep-alive message |
SomeRandomBloke | 1:a50b6c866a3b | 368 | * |
SomeRandomBloke | 1:a50b6c866a3b | 369 | */ |
SomeRandomBloke | 0:260fb10c0755 | 370 | void MQTTClient::live() { |
SomeRandomBloke | 0:260fb10c0755 | 371 | if (connected) { |
SomeRandomBloke | 0:260fb10c0755 | 372 | int t = timer.read_ms(); |
SomeRandomBloke | 0:260fb10c0755 | 373 | if (t - lastActivity > KEEPALIVE) { |
SomeRandomBloke | 0:260fb10c0755 | 374 | //Send 192 0 to broker |
SomeRandomBloke | 0:260fb10c0755 | 375 | printf("Send 192\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 376 | char packet_192[] = {0xc0, 0x00}; |
SomeRandomBloke | 0:260fb10c0755 | 377 | send_data((char*)packet_192, 2); |
SomeRandomBloke | 0:260fb10c0755 | 378 | lastActivity = t; |
SomeRandomBloke | 0:260fb10c0755 | 379 | } |
SomeRandomBloke | 0:260fb10c0755 | 380 | } |
SomeRandomBloke | 0:260fb10c0755 | 381 | } |
SomeRandomBloke | 0:260fb10c0755 | 382 | |
SomeRandomBloke | 1:a50b6c866a3b | 383 | /** TCP Socket event handling |
SomeRandomBloke | 1:a50b6c866a3b | 384 | * |
SomeRandomBloke | 1:a50b6c866a3b | 385 | * @param e Event object |
SomeRandomBloke | 1:a50b6c866a3b | 386 | */ |
SomeRandomBloke | 0:260fb10c0755 | 387 | void MQTTClient::onTCPSocketEvent(TCPSocketEvent e) { |
SomeRandomBloke | 0:260fb10c0755 | 388 | switch (e) { |
SomeRandomBloke | 0:260fb10c0755 | 389 | case TCPSOCKET_ACCEPT: |
SomeRandomBloke | 0:260fb10c0755 | 390 | printf("New TCPSocketEvent: TCPSOCKET_ACCEPT\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 391 | break; |
SomeRandomBloke | 0:260fb10c0755 | 392 | case TCPSOCKET_CONNECTED: |
SomeRandomBloke | 0:260fb10c0755 | 393 | printf("New TCPSocketEvent: TCPSOCKET_CONNECTED\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 394 | connected = true; |
SomeRandomBloke | 0:260fb10c0755 | 395 | break; |
SomeRandomBloke | 0:260fb10c0755 | 396 | case TCPSOCKET_WRITEABLE: |
SomeRandomBloke | 0:260fb10c0755 | 397 | printf("New TCPSocketEvent: TCPSOCKET_WRITEABLE\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 398 | break; |
SomeRandomBloke | 0:260fb10c0755 | 399 | case TCPSOCKET_READABLE: |
SomeRandomBloke | 0:260fb10c0755 | 400 | printf("New TCPSocketEvent: TCPSOCKET_READABLE\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 401 | if (!sessionOpened) { |
SomeRandomBloke | 0:260fb10c0755 | 402 | read_open_session(); |
SomeRandomBloke | 0:260fb10c0755 | 403 | } |
SomeRandomBloke | 0:260fb10c0755 | 404 | read_data(); |
SomeRandomBloke | 0:260fb10c0755 | 405 | break; |
SomeRandomBloke | 0:260fb10c0755 | 406 | case TCPSOCKET_CONTIMEOUT: |
SomeRandomBloke | 0:260fb10c0755 | 407 | printf("New TCPSocketEvent: TCPSOCKET_CONTIMEOUT\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 408 | break; |
SomeRandomBloke | 0:260fb10c0755 | 409 | case TCPSOCKET_CONRST: |
SomeRandomBloke | 0:260fb10c0755 | 410 | printf("New TCPSocketEvent: TCPSOCKET_CONRST\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 411 | break; |
SomeRandomBloke | 0:260fb10c0755 | 412 | case TCPSOCKET_CONABRT: |
SomeRandomBloke | 0:260fb10c0755 | 413 | printf("New TCPSocketEvent: TCPSOCKET_CONABRT\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 414 | break; |
SomeRandomBloke | 0:260fb10c0755 | 415 | case TCPSOCKET_ERROR: |
SomeRandomBloke | 0:260fb10c0755 | 416 | printf("New TCPSocketEvent: TCPSOCKET_ERROR\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 417 | break; |
SomeRandomBloke | 0:260fb10c0755 | 418 | case TCPSOCKET_DISCONNECTED: |
SomeRandomBloke | 0:260fb10c0755 | 419 | printf("New TCPSocketEvent: TCPSOCKET_DISCONNECTED\r\n"); |
SomeRandomBloke | 0:260fb10c0755 | 420 | pTCPSocket->close(); |
SomeRandomBloke | 0:260fb10c0755 | 421 | connected = false; |
SomeRandomBloke | 0:260fb10c0755 | 422 | break; |
SomeRandomBloke | 0:260fb10c0755 | 423 | } |
SomeRandomBloke | 0:260fb10c0755 | 424 | } |