Local copy
Dependencies: C12832_lcd ConfigFile EthernetInterface LM75B MMA7660 MQTTPacket mbed-rtos mbed
Fork of IBMIoTClientExampleForLPC1768 by
src/QuickstartClient.cpp@12:e9ff8869a99d, 2014-07-02 (annotated)
- Committer:
- rajathishere
- Date:
- Wed Jul 02 12:02:47 2014 +0000
- Revision:
- 12:e9ff8869a99d
- Parent:
- 9:cbabd85f2d5d
Added auth-method support
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samdanbury | 0:6276e9f72327 | 1 | /******************************************************************************* |
samdanbury | 0:6276e9f72327 | 2 | * Copyright (c) 2014 IBM Corporation and other Contributors. |
samdanbury | 0:6276e9f72327 | 3 | * |
samdanbury | 0:6276e9f72327 | 4 | * All rights reserved. This program and the accompanying materials |
samdanbury | 0:6276e9f72327 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
samdanbury | 0:6276e9f72327 | 6 | * which accompanies this distribution, and is available at |
samdanbury | 0:6276e9f72327 | 7 | * http://www.eclipse.org/legal/epl-v10.html |
samdanbury | 0:6276e9f72327 | 8 | * |
samdanbury | 0:6276e9f72327 | 9 | * Contributors: Sam Danbury |
samdanbury | 0:6276e9f72327 | 10 | * IBM - Initial Contribution |
samdanbury | 0:6276e9f72327 | 11 | *******************************************************************************/ |
samdanbury | 0:6276e9f72327 | 12 | |
samdanbury | 0:6276e9f72327 | 13 | #include "QuickstartClient.h" |
samdanbury | 0:6276e9f72327 | 14 | |
rajathishere | 6:a022f983f94b | 15 | int i =0; |
samdanbury | 0:6276e9f72327 | 16 | QuickstartClient::QuickstartClient(string mac) { |
samdanbury | 2:25ddff75a8c7 | 17 | quickstartMode = true; |
samdanbury | 0:6276e9f72327 | 18 | connected = false; |
samdanbury | 0:6276e9f72327 | 19 | macAddress = mac; |
samdanbury | 0:6276e9f72327 | 20 | |
rajathishere | 6:a022f983f94b | 21 | //Generate topic string for publish |
samdanbury | 3:ca5b84eb8f3b | 22 | memcpy(topic, "iot-2/evt/status/fmt/json", 25); |
samdanbury | 3:ca5b84eb8f3b | 23 | topic[25] = '\0'; |
samdanbury | 2:25ddff75a8c7 | 24 | |
rajathishere | 6:a022f983f94b | 25 | //Generate topic string for subscribe |
rajathishere | 6:a022f983f94b | 26 | memcpy(subscribeTopic, "iot-2/cmd/blink/fmt/json",24); |
rajathishere | 6:a022f983f94b | 27 | subscribeTopic[24] = '\0'; |
rajathishere | 6:a022f983f94b | 28 | |
samdanbury | 2:25ddff75a8c7 | 29 | loadConfig(); |
samdanbury | 0:6276e9f72327 | 30 | |
samdanbury | 0:6276e9f72327 | 31 | tryMqttConnect(); |
samdanbury | 0:6276e9f72327 | 32 | } |
samdanbury | 0:6276e9f72327 | 33 | |
samdanbury | 2:25ddff75a8c7 | 34 | void QuickstartClient::loadConfig() { |
samdanbury | 2:25ddff75a8c7 | 35 | |
samdanbury | 2:25ddff75a8c7 | 36 | ConfigFile cfg; |
samdanbury | 2:25ddff75a8c7 | 37 | |
samdanbury | 3:ca5b84eb8f3b | 38 | char value[30]; |
samdanbury | 3:ca5b84eb8f3b | 39 | char value1[30]; |
samdanbury | 3:ca5b84eb8f3b | 40 | char value2[30]; |
samdanbury | 3:ca5b84eb8f3b | 41 | char value3[30]; |
rajathishere | 12:e9ff8869a99d | 42 | char value4[30]; |
samdanbury | 2:25ddff75a8c7 | 43 | if (cfg.read("/local/device.cfg")) { |
samdanbury | 2:25ddff75a8c7 | 44 | quickstartMode = false; |
samdanbury | 2:25ddff75a8c7 | 45 | |
samdanbury | 3:ca5b84eb8f3b | 46 | if (cfg.getValue("org", value, sizeof(value))) { |
samdanbury | 2:25ddff75a8c7 | 47 | stringstream ss(value); |
samdanbury | 2:25ddff75a8c7 | 48 | ss >> org; |
samdanbury | 2:25ddff75a8c7 | 49 | } else { |
samdanbury | 2:25ddff75a8c7 | 50 | lcd.printf("No org defined in config\n"); |
samdanbury | 2:25ddff75a8c7 | 51 | } |
samdanbury | 2:25ddff75a8c7 | 52 | |
samdanbury | 3:ca5b84eb8f3b | 53 | if (cfg.getValue("type", value1, sizeof(value1))) { |
samdanbury | 3:ca5b84eb8f3b | 54 | stringstream ss(value1); |
samdanbury | 2:25ddff75a8c7 | 55 | ss >> type; |
samdanbury | 2:25ddff75a8c7 | 56 | } else { |
samdanbury | 2:25ddff75a8c7 | 57 | lcd.printf("No type defined in config\n"); |
samdanbury | 2:25ddff75a8c7 | 58 | } |
samdanbury | 2:25ddff75a8c7 | 59 | |
samdanbury | 3:ca5b84eb8f3b | 60 | if (cfg.getValue("id", value2, sizeof(value2))) { |
samdanbury | 3:ca5b84eb8f3b | 61 | stringstream ss(value2); |
samdanbury | 2:25ddff75a8c7 | 62 | ss >> id; |
samdanbury | 2:25ddff75a8c7 | 63 | } else { |
samdanbury | 2:25ddff75a8c7 | 64 | lcd.printf("No id defined in config\n"); |
samdanbury | 2:25ddff75a8c7 | 65 | } |
samdanbury | 2:25ddff75a8c7 | 66 | |
rajathishere | 12:e9ff8869a99d | 67 | if (cfg.getValue("auth-method", value3, sizeof(value3))) { |
samdanbury | 3:ca5b84eb8f3b | 68 | stringstream ss(value3); |
rajathishere | 12:e9ff8869a99d | 69 | ss >> authMethod; |
rajathishere | 12:e9ff8869a99d | 70 | } else { |
rajathishere | 12:e9ff8869a99d | 71 | lcd.printf("No auth method defined in config\n"); |
rajathishere | 12:e9ff8869a99d | 72 | } |
rajathishere | 12:e9ff8869a99d | 73 | |
rajathishere | 12:e9ff8869a99d | 74 | if (cfg.getValue("auth-token", value4, sizeof(value4))) { |
rajathishere | 12:e9ff8869a99d | 75 | stringstream ss(value4); |
samdanbury | 2:25ddff75a8c7 | 76 | ss >> token; |
samdanbury | 2:25ddff75a8c7 | 77 | } else { |
samdanbury | 2:25ddff75a8c7 | 78 | lcd.printf("No token defined in config\n"); |
samdanbury | 2:25ddff75a8c7 | 79 | } |
samdanbury | 3:ca5b84eb8f3b | 80 | |
samdanbury | 2:25ddff75a8c7 | 81 | } else { |
samdanbury | 2:25ddff75a8c7 | 82 | org = "quickstart"; |
samdanbury | 2:25ddff75a8c7 | 83 | type = "iotsample-mbed-lpc1768"; |
samdanbury | 2:25ddff75a8c7 | 84 | id = macAddress; |
samdanbury | 2:25ddff75a8c7 | 85 | } |
rajathishere | 6:a022f983f94b | 86 | wait(5.0); |
samdanbury | 2:25ddff75a8c7 | 87 | |
samdanbury | 2:25ddff75a8c7 | 88 | } |
samdanbury | 2:25ddff75a8c7 | 89 | |
samdanbury | 0:6276e9f72327 | 90 | int QuickstartClient::reconnectDelay(int i) { |
samdanbury | 0:6276e9f72327 | 91 | if (i < 10) { |
samdanbury | 0:6276e9f72327 | 92 | return 3; //First 10 attempts try within 3 seconds |
samdanbury | 0:6276e9f72327 | 93 | } else if (i < 20) { |
samdanbury | 0:6276e9f72327 | 94 | return 60; //Next 10 attempts retry after every 1 minute |
samdanbury | 0:6276e9f72327 | 95 | } else { |
samdanbury | 0:6276e9f72327 | 96 | return 600; //After 20 attempts, retry every 10 minutes |
samdanbury | 0:6276e9f72327 | 97 | } |
samdanbury | 0:6276e9f72327 | 98 | } |
samdanbury | 0:6276e9f72327 | 99 | |
samdanbury | 0:6276e9f72327 | 100 | void QuickstartClient::tryMqttConnect() { |
samdanbury | 0:6276e9f72327 | 101 | int retryAttempt = 0; |
samdanbury | 0:6276e9f72327 | 102 | |
samdanbury | 0:6276e9f72327 | 103 | //Reinstantiate TCP socket connection object |
samdanbury | 0:6276e9f72327 | 104 | mysock = TCPSocketConnection(); |
samdanbury | 0:6276e9f72327 | 105 | |
samdanbury | 0:6276e9f72327 | 106 | while (connected == false) { |
samdanbury | 0:6276e9f72327 | 107 | lcd.cls(); |
samdanbury | 0:6276e9f72327 | 108 | lcd.locate(0,0); |
samdanbury | 0:6276e9f72327 | 109 | lcd.printf("Trying to connect..."); |
samdanbury | 0:6276e9f72327 | 110 | |
samdanbury | 0:6276e9f72327 | 111 | //Based on number of connection attempts, determine timeout |
samdanbury | 0:6276e9f72327 | 112 | int connDelayTimeout = reconnectDelay(++retryAttempt); |
samdanbury | 0:6276e9f72327 | 113 | |
samdanbury | 0:6276e9f72327 | 114 | //Attempt to reconnect |
samdanbury | 0:6276e9f72327 | 115 | connect(); |
samdanbury | 0:6276e9f72327 | 116 | |
samdanbury | 0:6276e9f72327 | 117 | //If connection was not established, continue retry |
samdanbury | 0:6276e9f72327 | 118 | if (connected == false) { |
samdanbury | 0:6276e9f72327 | 119 | wait(connDelayTimeout); |
samdanbury | 0:6276e9f72327 | 120 | } else { |
samdanbury | 0:6276e9f72327 | 121 | break; |
samdanbury | 0:6276e9f72327 | 122 | } |
samdanbury | 0:6276e9f72327 | 123 | } |
samdanbury | 0:6276e9f72327 | 124 | } |
samdanbury | 0:6276e9f72327 | 125 | |
samdanbury | 0:6276e9f72327 | 126 | void QuickstartClient::connect() { |
samdanbury | 0:6276e9f72327 | 127 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
samdanbury | 0:6276e9f72327 | 128 | int rc = 0; |
samdanbury | 0:6276e9f72327 | 129 | int len = 0; |
samdanbury | 0:6276e9f72327 | 130 | char buf[200]; |
samdanbury | 0:6276e9f72327 | 131 | int buflen = sizeof(buf); |
samdanbury | 0:6276e9f72327 | 132 | |
samdanbury | 0:6276e9f72327 | 133 | //Connect to TCP socket |
samdanbury | 0:6276e9f72327 | 134 | mysock.connect(IBM_IOT_BROKER, IBM_IOT_PORT); |
samdanbury | 0:6276e9f72327 | 135 | |
samdanbury | 2:25ddff75a8c7 | 136 | //Construct client ID |
samdanbury | 2:25ddff75a8c7 | 137 | string str = string("d:") + org + ":" + type + ":" + id; |
rajathishere | 8:e58e10ca4352 | 138 | char clientId[str.size()+1]; |
samdanbury | 3:ca5b84eb8f3b | 139 | memcpy(clientId, str.c_str(), str.size() + 1); |
samdanbury | 0:6276e9f72327 | 140 | |
samdanbury | 0:6276e9f72327 | 141 | //Set MQTT connect options |
samdanbury | 0:6276e9f72327 | 142 | data.clientID.cstring = clientId; |
samdanbury | 0:6276e9f72327 | 143 | data.keepAliveInterval = 20; |
samdanbury | 0:6276e9f72327 | 144 | data.cleansession = 1; |
samdanbury | 0:6276e9f72327 | 145 | data.MQTTVersion = 3; |
samdanbury | 5:1b54a0b7b39d | 146 | if (!quickstartMode) { |
rajathishere | 12:e9ff8869a99d | 147 | if ( ! authMethod.compare("token") ) { |
rajathishere | 12:e9ff8869a99d | 148 | data.username.cstring = clientId; |
rajathishere | 12:e9ff8869a99d | 149 | char* authToken= new char[token.size()]; |
rajathishere | 12:e9ff8869a99d | 150 | memcpy(authToken, token.c_str(), token.size()+1); |
rajathishere | 12:e9ff8869a99d | 151 | data.password.cstring = authToken; |
rajathishere | 12:e9ff8869a99d | 152 | } |
rajathishere | 12:e9ff8869a99d | 153 | else { |
rajathishere | 12:e9ff8869a99d | 154 | lcd.printf("Exiting\n"); |
rajathishere | 12:e9ff8869a99d | 155 | exit(-1); |
rajathishere | 12:e9ff8869a99d | 156 | } |
samdanbury | 5:1b54a0b7b39d | 157 | } |
samdanbury | 0:6276e9f72327 | 158 | |
samdanbury | 0:6276e9f72327 | 159 | //Attempt MQTT connect |
samdanbury | 0:6276e9f72327 | 160 | len = MQTTSerialize_connect(buf, buflen, &data); |
samdanbury | 0:6276e9f72327 | 161 | rc = 0; |
samdanbury | 0:6276e9f72327 | 162 | while (rc < len) { |
samdanbury | 0:6276e9f72327 | 163 | int rc1 = mysock.send(buf, len); |
samdanbury | 0:6276e9f72327 | 164 | if (rc1 == -1) { |
samdanbury | 0:6276e9f72327 | 165 | connected = false; |
samdanbury | 0:6276e9f72327 | 166 | break; |
samdanbury | 0:6276e9f72327 | 167 | } else { |
samdanbury | 0:6276e9f72327 | 168 | rc += rc1; |
samdanbury | 0:6276e9f72327 | 169 | } |
samdanbury | 0:6276e9f72327 | 170 | } |
samdanbury | 0:6276e9f72327 | 171 | if (rc == len) { |
samdanbury | 0:6276e9f72327 | 172 | connected = true; |
samdanbury | 0:6276e9f72327 | 173 | } |
rajathishere | 6:a022f983f94b | 174 | wait(0.2); |
samdanbury | 0:6276e9f72327 | 175 | } |
samdanbury | 0:6276e9f72327 | 176 | |
samdanbury | 0:6276e9f72327 | 177 | void QuickstartClient::publish(string thePayload) { |
samdanbury | 0:6276e9f72327 | 178 | int rc = 0; |
samdanbury | 0:6276e9f72327 | 179 | int len = 0; |
samdanbury | 0:6276e9f72327 | 180 | char buf[250]; |
samdanbury | 0:6276e9f72327 | 181 | int buflen = sizeof(buf); |
rajathishere | 6:a022f983f94b | 182 | |
samdanbury | 0:6276e9f72327 | 183 | MQTTString topicString = MQTTString_initializer; |
samdanbury | 0:6276e9f72327 | 184 | |
samdanbury | 0:6276e9f72327 | 185 | topicString.cstring = topic; |
rajathishere | 8:e58e10ca4352 | 186 | |
samdanbury | 0:6276e9f72327 | 187 | //Convert payload from string to char array |
samdanbury | 0:6276e9f72327 | 188 | char* payload = new char [thePayload.length()+1]; |
samdanbury | 0:6276e9f72327 | 189 | std::strcpy (payload, thePayload.c_str()); |
samdanbury | 0:6276e9f72327 | 190 | int payloadlen = strlen(payload); |
samdanbury | 0:6276e9f72327 | 191 | //Attempt MQTT publish |
samdanbury | 0:6276e9f72327 | 192 | len = MQTTSerialize_publish(buf, buflen, 0, 0, 0, 0, topicString, payload, payloadlen); |
samdanbury | 0:6276e9f72327 | 193 | rc = 0; |
samdanbury | 0:6276e9f72327 | 194 | while (rc < len) { |
samdanbury | 0:6276e9f72327 | 195 | int rc1 = mysock.send(buf, len); |
rajathishere | 6:a022f983f94b | 196 | wait(3.0); |
samdanbury | 0:6276e9f72327 | 197 | if (rc1 == -1) { |
samdanbury | 0:6276e9f72327 | 198 | //If return code from MQTT publish is -1, attempt reconnect |
samdanbury | 0:6276e9f72327 | 199 | connected = false; |
samdanbury | 0:6276e9f72327 | 200 | tryMqttConnect(); |
samdanbury | 0:6276e9f72327 | 201 | break; |
samdanbury | 0:6276e9f72327 | 202 | } else { |
samdanbury | 0:6276e9f72327 | 203 | rc += rc1; |
samdanbury | 0:6276e9f72327 | 204 | } |
samdanbury | 0:6276e9f72327 | 205 | } |
samdanbury | 0:6276e9f72327 | 206 | wait(0.2); |
samdanbury | 0:6276e9f72327 | 207 | |
samdanbury | 0:6276e9f72327 | 208 | if (payload) { |
samdanbury | 0:6276e9f72327 | 209 | delete payload; |
samdanbury | 0:6276e9f72327 | 210 | } |
rajathishere | 6:a022f983f94b | 211 | } |
rajathishere | 6:a022f983f94b | 212 | |
rajathishere | 9:cbabd85f2d5d | 213 | int QuickstartClient::subscribe() { |
rajathishere | 6:a022f983f94b | 214 | int rc = 0; |
rajathishere | 6:a022f983f94b | 215 | int len = 0; |
rajathishere | 6:a022f983f94b | 216 | char buf[250]; |
rajathishere | 6:a022f983f94b | 217 | int buflen = sizeof(buf); |
rajathishere | 6:a022f983f94b | 218 | |
rajathishere | 6:a022f983f94b | 219 | MQTTString topicString = MQTTString_initializer; |
rajathishere | 6:a022f983f94b | 220 | |
rajathishere | 6:a022f983f94b | 221 | topicString.cstring = subscribeTopic; |
rajathishere | 6:a022f983f94b | 222 | |
rajathishere | 6:a022f983f94b | 223 | //Attempt MQTT subscribe |
rajathishere | 9:cbabd85f2d5d | 224 | len = MQTTSerialize_subscribe(buf, buflen, 0, 1, 1, &topicString, 0); |
rajathishere | 6:a022f983f94b | 225 | rc = 0; |
rajathishere | 6:a022f983f94b | 226 | while (rc < len) { |
rajathishere | 6:a022f983f94b | 227 | int rc1 = mysock.send(buf, len); |
rajathishere | 6:a022f983f94b | 228 | if (rc1 == -1) { |
rajathishere | 6:a022f983f94b | 229 | break; |
rajathishere | 6:a022f983f94b | 230 | } |
rajathishere | 6:a022f983f94b | 231 | else { |
rajathishere | 6:a022f983f94b | 232 | rc += rc1; |
rajathishere | 6:a022f983f94b | 233 | } |
rajathishere | 6:a022f983f94b | 234 | } |
rajathishere | 6:a022f983f94b | 235 | wait(0.2); |
rajathishere | 9:cbabd85f2d5d | 236 | return rc; |
rajathishere | 6:a022f983f94b | 237 | } |