Had to fork with a different name, because of some incompatibility issues.
DeviceClient.cpp@7:b14763b63562, 2018-06-09 (annotated)
- Committer:
- lamell
- Date:
- Sat Jun 09 23:26:43 2018 +0000
- Revision:
- 7:b14763b63562
- Parent:
- 5:ea9f483e0294
- Child:
- 8:cb2c98efdca8
No changes
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sathipal | 0:f86732d81998 | 1 | /******************************************************************************* |
sathipal | 0:f86732d81998 | 2 | * Copyright (c) 2015 IBM Corp. |
sathipal | 0:f86732d81998 | 3 | * |
sathipal | 0:f86732d81998 | 4 | * All rights reserved. This program and the accompanying materials |
sathipal | 0:f86732d81998 | 5 | * are made available under the terms of the Eclipse Public License v1.0 |
sathipal | 0:f86732d81998 | 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. |
sathipal | 0:f86732d81998 | 7 | * |
sathipal | 0:f86732d81998 | 8 | * The Eclipse Public License is available at |
sathipal | 0:f86732d81998 | 9 | * http://www.eclipse.org/legal/epl-v10.html |
sathipal | 0:f86732d81998 | 10 | * and the Eclipse Distribution License is available at |
sathipal | 0:f86732d81998 | 11 | * http://www.eclipse.org/org/documents/edl-v10.php. |
sathipal | 0:f86732d81998 | 12 | * |
sathipal | 0:f86732d81998 | 13 | * Contributors: |
sathipal | 0:f86732d81998 | 14 | * Sathisumar Palaniappan - initial implementation |
sathipal | 3:3d91bf839b49 | 15 | * Sathisumar Palaniappan - added reconnect logic and isConnected() method |
lokeshhk | 5:ea9f483e0294 | 16 | * Lokesh K Haralakatta - Port to mbed OS 5 support |
lokeshhk | 5:ea9f483e0294 | 17 | * Lokesh K Haralakatta - Added SSL/TLS Support |
sathipal | 0:f86732d81998 | 18 | *******************************************************************************/ |
sathipal | 0:f86732d81998 | 19 | #include "MQTTClient.h" |
sathipal | 0:f86732d81998 | 20 | #include "DeviceClient.h" |
sathipal | 0:f86732d81998 | 21 | |
lamell | 7:b14763b63562 | 22 | static uint32_t linkStatus(void) |
lamell | 7:b14763b63562 | 23 | { |
lamell | 7:b14763b63562 | 24 | return (1); |
lamell | 7:b14763b63562 | 25 | } |
lamell | 7:b14763b63562 | 26 | |
sathipal | 3:3d91bf839b49 | 27 | // need a wrapper since K64F and LPC1768 wont have the same name for mii read methods |
lamell | 7:b14763b63562 | 28 | #if defined(TARGET_UBLOX_C027) || defined(TARGET_K64F) |
sathipal | 3:3d91bf839b49 | 29 | |
sathipal | 3:3d91bf839b49 | 30 | static uint32_t linkStatus(void) |
sathipal | 3:3d91bf839b49 | 31 | { |
sathipal | 3:3d91bf839b49 | 32 | return (1); |
sathipal | 3:3d91bf839b49 | 33 | } |
sathipal | 3:3d91bf839b49 | 34 | #elif defined(TARGET_LPC1768) |
sathipal | 3:3d91bf839b49 | 35 | #include "lpc_phy.h" |
sathipal | 3:3d91bf839b49 | 36 | |
sathipal | 3:3d91bf839b49 | 37 | static uint32_t linkStatus(void) |
sathipal | 3:3d91bf839b49 | 38 | { |
sathipal | 3:3d91bf839b49 | 39 | return (lpc_mii_read_data() & 1); |
sathipal | 3:3d91bf839b49 | 40 | } |
sathipal | 3:3d91bf839b49 | 41 | #endif |
sathipal | 0:f86732d81998 | 42 | using namespace IoTF; |
sathipal | 0:f86732d81998 | 43 | |
sathipal | 0:f86732d81998 | 44 | CommandHandler handler = NULL; |
sathipal | 0:f86732d81998 | 45 | void msgArrived(MQTT::MessageData& md); |
sathipal | 0:f86732d81998 | 46 | |
sathipal | 0:f86732d81998 | 47 | /** |
sathipal | 0:f86732d81998 | 48 | * A client, used by device, that handles connections with the IBM Internet of Things Foundation. |
sathipal | 0:f86732d81998 | 49 | * This class allows device to publish events and receive commands to/from IBM IoT Foundation wtih simple function calls. |
sathipal | 0:f86732d81998 | 50 | */ |
sathipal | 2:199ddea804cd | 51 | DeviceClient::DeviceClient():org(NULL),deviceType(NULL),deviceId(NULL), |
lokeshhk | 5:ea9f483e0294 | 52 | authMethod(NULL),authToken(NULL),mqttNetwork(NULL),mqttClient(NULL),connected(false),port(0) |
sathipal | 2:199ddea804cd | 53 | { |
lokeshhk | 5:ea9f483e0294 | 54 | LOG("Constructor#1 called::\r\n"); |
sathipal | 0:f86732d81998 | 55 | } |
sathipal | 0:f86732d81998 | 56 | |
lokeshhk | 5:ea9f483e0294 | 57 | DeviceClient::DeviceClient(char *orgId, char *typeId, char *id, int port):org(orgId),deviceType(typeId), |
lokeshhk | 5:ea9f483e0294 | 58 | deviceId(id),authMethod(NULL),authToken(NULL),connected(false), port(port) |
sathipal | 2:199ddea804cd | 59 | { |
lokeshhk | 5:ea9f483e0294 | 60 | LOG("Constructor#2 called:: org=%s, type=%s, id=%s\r\n", (org==NULL)?"NULL":org, |
sathipal | 0:f86732d81998 | 61 | (deviceType==NULL)?"NULL":deviceType, (deviceId==NULL)?"NULL":deviceId); |
lokeshhk | 5:ea9f483e0294 | 62 | |
sathipal | 0:f86732d81998 | 63 | if(strcmp(this->org, QUICKSTART) != 0) { |
lokeshhk | 5:ea9f483e0294 | 64 | WARN("Registered flow must provide valid token\r\n"); |
sathipal | 0:f86732d81998 | 65 | } |
lokeshhk | 5:ea9f483e0294 | 66 | |
lokeshhk | 5:ea9f483e0294 | 67 | mqttNetwork = new MQTTNetwork(); |
lokeshhk | 5:ea9f483e0294 | 68 | mqttClient = new MQTT::Client<MQTTNetwork, Countdown>(*mqttNetwork); |
lokeshhk | 5:ea9f483e0294 | 69 | |
sathipal | 0:f86732d81998 | 70 | } |
sathipal | 0:f86732d81998 | 71 | |
lokeshhk | 5:ea9f483e0294 | 72 | DeviceClient::DeviceClient(char *orgId, char *typeId,char *id, char *method, char *token, int port):org(orgId), |
lokeshhk | 5:ea9f483e0294 | 73 | deviceType(typeId),deviceId(id),authMethod(method),authToken(token),connected(false), port(port) |
sathipal | 2:199ddea804cd | 74 | { |
sathipal | 0:f86732d81998 | 75 | // Don't print token for security reasons |
lokeshhk | 5:ea9f483e0294 | 76 | LOG("Constructor#3 called:: org=%s, type=%s, id=%s\r\n", (org==NULL)?"NULL":org, |
sathipal | 0:f86732d81998 | 77 | (deviceType==NULL)?"NULL":deviceType, (deviceId==NULL)?"NULL":deviceId); |
lokeshhk | 5:ea9f483e0294 | 78 | |
lokeshhk | 5:ea9f483e0294 | 79 | mqttNetwork = new MQTTNetwork(); |
lokeshhk | 5:ea9f483e0294 | 80 | mqttClient = new MQTT::Client<MQTTNetwork, Countdown>(*mqttNetwork); |
sathipal | 0:f86732d81998 | 81 | } |
sathipal | 0:f86732d81998 | 82 | |
sathipal | 0:f86732d81998 | 83 | /** |
sathipal | 0:f86732d81998 | 84 | * Connect to the IBM Internet of Things Foundation |
lokeshhk | 5:ea9f483e0294 | 85 | */ |
sathipal | 0:f86732d81998 | 86 | bool DeviceClient::connect() |
lokeshhk | 5:ea9f483e0294 | 87 | { |
sathipal | 0:f86732d81998 | 88 | char *organizationName, *typeId, *id; |
lokeshhk | 5:ea9f483e0294 | 89 | bool rc = false; |
sathipal | 0:f86732d81998 | 90 | // Check if any organization is set |
lokeshhk | 5:ea9f483e0294 | 91 | if(this->org == NULL || (strcmp("", this->org) == 0)) |
sathipal | 3:3d91bf839b49 | 92 | { |
sathipal | 0:f86732d81998 | 93 | organizationName = QUICKSTART; |
sathipal | 0:f86732d81998 | 94 | } else { |
sathipal | 0:f86732d81998 | 95 | organizationName = this->org; |
sathipal | 0:f86732d81998 | 96 | } |
lokeshhk | 5:ea9f483e0294 | 97 | |
sathipal | 0:f86732d81998 | 98 | // Check if device type is already mentioned |
lokeshhk | 5:ea9f483e0294 | 99 | if(this->deviceType == NULL || (strcmp("", this->deviceType) == 0)) |
sathipal | 3:3d91bf839b49 | 100 | { |
sathipal | 0:f86732d81998 | 101 | typeId = "iotsample-mbed"; |
sathipal | 0:f86732d81998 | 102 | } else { |
sathipal | 0:f86732d81998 | 103 | typeId = this->deviceType; |
sathipal | 0:f86732d81998 | 104 | } |
lokeshhk | 5:ea9f483e0294 | 105 | |
sathipal | 0:f86732d81998 | 106 | char hostname[strlen(organizationName) + strlen(IBM_IOT_MESSAGING) + 1]; |
sathipal | 0:f86732d81998 | 107 | sprintf(hostname, "%s%s", organizationName, IBM_IOT_MESSAGING); |
lokeshhk | 5:ea9f483e0294 | 108 | |
lokeshhk | 5:ea9f483e0294 | 109 | NetworkInterface* net = mqttNetwork->getEth(); |
lokeshhk | 5:ea9f483e0294 | 110 | const char* ip = net->get_ip_address(); |
sathipal | 0:f86732d81998 | 111 | |
sathipal | 0:f86732d81998 | 112 | // Get devices MAC address if deviceId is not set already |
lokeshhk | 5:ea9f483e0294 | 113 | if(this->deviceId == NULL || (strcmp("", this->deviceId) == 0)) |
sathipal | 3:3d91bf839b49 | 114 | { |
sathipal | 0:f86732d81998 | 115 | char tmpBuf[50]; |
sathipal | 0:f86732d81998 | 116 | id = getMac(tmpBuf, sizeof(tmpBuf)); |
sathipal | 0:f86732d81998 | 117 | } else { |
sathipal | 0:f86732d81998 | 118 | id = this->deviceId; |
sathipal | 0:f86732d81998 | 119 | } |
lokeshhk | 5:ea9f483e0294 | 120 | |
sathipal | 0:f86732d81998 | 121 | // Construct clientId - d:org:type:id |
sathipal | 0:f86732d81998 | 122 | char clientId[strlen(organizationName) + strlen(typeId) + strlen(id) + 5]; |
sathipal | 0:f86732d81998 | 123 | sprintf(clientId, "d:%s:%s:%s", organizationName, typeId, id); |
lokeshhk | 5:ea9f483e0294 | 124 | |
sathipal | 0:f86732d81998 | 125 | // Initialize MQTT Connect |
sathipal | 0:f86732d81998 | 126 | MQTTPacket_connectData data = MQTTPacket_connectData_initializer; |
sathipal | 0:f86732d81998 | 127 | data.MQTTVersion = 4; |
sathipal | 0:f86732d81998 | 128 | data.clientID.cstring = clientId; |
lokeshhk | 5:ea9f483e0294 | 129 | |
sathipal | 0:f86732d81998 | 130 | int quickstartMode = (strcmp(organizationName, QUICKSTART) == 0); |
lokeshhk | 5:ea9f483e0294 | 131 | |
lokeshhk | 5:ea9f483e0294 | 132 | if (!quickstartMode) |
lokeshhk | 5:ea9f483e0294 | 133 | { |
sathipal | 0:f86732d81998 | 134 | data.username.cstring = "use-token-auth"; |
sathipal | 0:f86732d81998 | 135 | data.password.cstring = this->authToken; |
lokeshhk | 5:ea9f483e0294 | 136 | |
lokeshhk | 5:ea9f483e0294 | 137 | //Check and initialize appropriate port |
lokeshhk | 5:ea9f483e0294 | 138 | if(port == 1883) |
lokeshhk | 5:ea9f483e0294 | 139 | port = MQTT_TLS_PORT; |
sathipal | 0:f86732d81998 | 140 | } |
lokeshhk | 5:ea9f483e0294 | 141 | |
lokeshhk | 5:ea9f483e0294 | 142 | logData(net, hostname, clientId); |
lokeshhk | 5:ea9f483e0294 | 143 | |
lokeshhk | 5:ea9f483e0294 | 144 | if(ip){ |
lokeshhk | 5:ea9f483e0294 | 145 | rc = tryConnect(hostname, data); |
lokeshhk | 5:ea9f483e0294 | 146 | // By default subscribe to commands if we are in registered flow |
lokeshhk | 5:ea9f483e0294 | 147 | if(rc == true && !quickstartMode) |
lokeshhk | 5:ea9f483e0294 | 148 | { |
lokeshhk | 5:ea9f483e0294 | 149 | subscribeToCommands(); |
lokeshhk | 5:ea9f483e0294 | 150 | } |
lokeshhk | 5:ea9f483e0294 | 151 | if(rc == true) |
lokeshhk | 5:ea9f483e0294 | 152 | { |
lokeshhk | 5:ea9f483e0294 | 153 | connected = true; |
lokeshhk | 5:ea9f483e0294 | 154 | LOG("Device Client Connected to %s:%d\r\n",hostname,port); |
lokeshhk | 5:ea9f483e0294 | 155 | } |
sathipal | 3:3d91bf839b49 | 156 | } |
lokeshhk | 5:ea9f483e0294 | 157 | else |
lokeshhk | 5:ea9f483e0294 | 158 | LOG("No IP Assigned to Network Interface...\r\n"); |
lokeshhk | 5:ea9f483e0294 | 159 | |
sathipal | 0:f86732d81998 | 160 | return rc; |
sathipal | 0:f86732d81998 | 161 | } |
sathipal | 0:f86732d81998 | 162 | |
sathipal | 3:3d91bf839b49 | 163 | /** |
sathipal | 3:3d91bf839b49 | 164 | * Reconnect when the connection is lost. This method disconnects the active connection if any |
lokeshhk | 5:ea9f483e0294 | 165 | * and tries to initiate a fresh connection. |
sathipal | 3:3d91bf839b49 | 166 | * This method uses the Ethernet Link status wherever applicable while reconnecting. i.e, tries to |
sathipal | 3:3d91bf839b49 | 167 | * initiate the connection only when the Ethernet cable is plugged in. |
sathipal | 3:3d91bf839b49 | 168 | */ |
lokeshhk | 5:ea9f483e0294 | 169 | bool DeviceClient::reConnect() |
sathipal | 3:3d91bf839b49 | 170 | { |
lokeshhk | 5:ea9f483e0294 | 171 | LOG("DeviceClient::reConnect() entry and connected = %s\r\n",(connected == true)?"true":"false"); |
sathipal | 3:3d91bf839b49 | 172 | if(connected == true) |
sathipal | 3:3d91bf839b49 | 173 | { |
sathipal | 3:3d91bf839b49 | 174 | disconnect(); |
sathipal | 3:3d91bf839b49 | 175 | } |
sathipal | 3:3d91bf839b49 | 176 | |
sathipal | 3:3d91bf839b49 | 177 | if(linkStatus()) |
sathipal | 3:3d91bf839b49 | 178 | { |
lokeshhk | 5:ea9f483e0294 | 179 | NetworkInterface* net = mqttNetwork->getEth(); |
lokeshhk | 5:ea9f483e0294 | 180 | if(net->connect() == 0) |
sathipal | 3:3d91bf839b49 | 181 | { |
sathipal | 3:3d91bf839b49 | 182 | bool status = connect(); |
lokeshhk | 5:ea9f483e0294 | 183 | if(status == false) |
sathipal | 3:3d91bf839b49 | 184 | { |
lokeshhk | 5:ea9f483e0294 | 185 | net->disconnect(); |
sathipal | 3:3d91bf839b49 | 186 | } |
sathipal | 3:3d91bf839b49 | 187 | return status; |
sathipal | 3:3d91bf839b49 | 188 | } |
sathipal | 3:3d91bf839b49 | 189 | } |
sathipal | 3:3d91bf839b49 | 190 | return false; |
sathipal | 3:3d91bf839b49 | 191 | } |
sathipal | 3:3d91bf839b49 | 192 | |
lokeshhk | 5:ea9f483e0294 | 193 | bool DeviceClient::tryConnect(char *hostname, MQTTPacket_connectData &data) |
sathipal | 2:199ddea804cd | 194 | { |
sathipal | 0:f86732d81998 | 195 | int rc = -1; |
sathipal | 0:f86732d81998 | 196 | int retryAttempt = 0; |
lokeshhk | 5:ea9f483e0294 | 197 | do { |
lokeshhk | 5:ea9f483e0294 | 198 | rc = mqttNetwork->connect(hostname, port); |
lokeshhk | 5:ea9f483e0294 | 199 | if (rc != 0) |
sathipal | 3:3d91bf839b49 | 200 | { |
lokeshhk | 5:ea9f483e0294 | 201 | WARN("mqttNetwork connect returned: %d\r\n", rc); |
sathipal | 0:f86732d81998 | 202 | } |
sathipal | 0:f86732d81998 | 203 | |
lokeshhk | 5:ea9f483e0294 | 204 | // MQTT connect |
lokeshhk | 5:ea9f483e0294 | 205 | if (rc == 0 && (rc = mqttClient->connect(data)) != 0) |
sathipal | 3:3d91bf839b49 | 206 | { |
lokeshhk | 5:ea9f483e0294 | 207 | WARN("MQTT connect returned %d\r\n", rc); |
sathipal | 0:f86732d81998 | 208 | if (rc == MQTT_NOT_AUTHORIZED || rc == MQTT_BAD_USERNAME_OR_PASSWORD) |
sathipal | 0:f86732d81998 | 209 | return false; // don't reattempt to connect if credentials are wrong |
sathipal | 0:f86732d81998 | 210 | } else if (rc == MQTT_CONNECTION_ACCEPTED) { |
sathipal | 0:f86732d81998 | 211 | return true; |
sathipal | 0:f86732d81998 | 212 | } |
lokeshhk | 5:ea9f483e0294 | 213 | |
sathipal | 0:f86732d81998 | 214 | int timeout = getConnTimeout(++retryAttempt); |
lokeshhk | 5:ea9f483e0294 | 215 | |
lokeshhk | 5:ea9f483e0294 | 216 | WARN("Retry attempt number %d waiting %d\r\n", retryAttempt, timeout); |
lokeshhk | 5:ea9f483e0294 | 217 | |
sathipal | 2:199ddea804cd | 218 | // enough retry is done - return to application |
lamell | 7:b14763b63562 | 219 | if (retryAttempt == 5){ |
lamell | 7:b14763b63562 | 220 | |
lamell | 7:b14763b63562 | 221 | //Here's my modification. If not connected, RESET the board. |
lamell | 7:b14763b63562 | 222 | NVIC_SystemReset(); |
sathipal | 2:199ddea804cd | 223 | return false; |
lamell | 7:b14763b63562 | 224 | } else { |
sathipal | 0:f86732d81998 | 225 | wait(timeout); |
lamell | 7:b14763b63562 | 226 | } |
sathipal | 0:f86732d81998 | 227 | } while(true); |
sathipal | 0:f86732d81998 | 228 | } |
sathipal | 0:f86732d81998 | 229 | |
lokeshhk | 5:ea9f483e0294 | 230 | void DeviceClient::logData(NetworkInterface* net, char *hostname, char *clientId) |
sathipal | 2:199ddea804cd | 231 | { |
lokeshhk | 5:ea9f483e0294 | 232 | // Network debug statements |
lokeshhk | 5:ea9f483e0294 | 233 | LOG("=====================================\r\n"); |
lokeshhk | 5:ea9f483e0294 | 234 | LOG("Connection Config Details:\r\n"); |
lokeshhk | 5:ea9f483e0294 | 235 | LOG("IP ADDRESS: %s\r\n", net->get_ip_address()); |
lokeshhk | 5:ea9f483e0294 | 236 | LOG("MAC ADDRESS: %s\r\n", net->get_mac_address()); |
lokeshhk | 5:ea9f483e0294 | 237 | LOG("Gateway: %s\r\n", net->get_gateway()); |
lokeshhk | 5:ea9f483e0294 | 238 | LOG("Network Mask: %s\r\n", net->get_netmask()); |
lokeshhk | 5:ea9f483e0294 | 239 | LOG("Server Hostname: %s\r\n", hostname); |
lokeshhk | 5:ea9f483e0294 | 240 | LOG("Server Port: %d\r\n", port); |
lokeshhk | 5:ea9f483e0294 | 241 | LOG("Client ID: %s\r\n", clientId); |
lokeshhk | 5:ea9f483e0294 | 242 | LOG("=====================================\r\n"); |
sathipal | 0:f86732d81998 | 243 | } |
sathipal | 0:f86732d81998 | 244 | |
sathipal | 0:f86732d81998 | 245 | int DeviceClient::getConnTimeout(int attemptNumber) |
sathipal | 0:f86732d81998 | 246 | { |
sathipal | 0:f86732d81998 | 247 | // Try to increase the timeout every time |
sathipal | 0:f86732d81998 | 248 | return (attemptNumber * attemptNumber * 5); |
sathipal | 0:f86732d81998 | 249 | } |
sathipal | 0:f86732d81998 | 250 | |
sathipal | 3:3d91bf839b49 | 251 | /** |
sathipal | 3:3d91bf839b49 | 252 | * Returns the connection status, connected or disconnected. |
sathipal | 3:3d91bf839b49 | 253 | */ |
sathipal | 3:3d91bf839b49 | 254 | bool DeviceClient::isConnected() { |
lokeshhk | 5:ea9f483e0294 | 255 | return mqttClient->isConnected(); |
sathipal | 3:3d91bf839b49 | 256 | } |
sathipal | 0:f86732d81998 | 257 | |
sathipal | 0:f86732d81998 | 258 | /** |
lokeshhk | 5:ea9f483e0294 | 259 | * Publish data to the IBM Internet of Things Foundation. Note that data is published |
lokeshhk | 5:ea9f483e0294 | 260 | * by default at Quality of Service (QoS) 0, which means that a successful send |
sathipal | 0:f86732d81998 | 261 | * does not guarantee receipt even if the publish has been successful. |
sathipal | 0:f86732d81998 | 262 | */ |
sathipal | 0:f86732d81998 | 263 | bool DeviceClient::publishEvent(char *eventName, char *data, MQTT::QoS qos) |
sathipal | 0:f86732d81998 | 264 | { |
lokeshhk | 5:ea9f483e0294 | 265 | if(!mqttClient->isConnected()) |
sathipal | 3:3d91bf839b49 | 266 | { |
lokeshhk | 5:ea9f483e0294 | 267 | WARN("Client is not connected \r\n"); |
sathipal | 0:f86732d81998 | 268 | return false; |
sathipal | 0:f86732d81998 | 269 | } |
lokeshhk | 5:ea9f483e0294 | 270 | |
sathipal | 0:f86732d81998 | 271 | MQTT::Message message; |
sathipal | 0:f86732d81998 | 272 | /* Topic format must be iot-2/evt/<eventName>/fmt/json (let us stick to json format for now) |
sathipal | 0:f86732d81998 | 273 | * |
sathipal | 0:f86732d81998 | 274 | * So length must be 10 + strlen(eventName) + 9 + 1 |
sathipal | 0:f86732d81998 | 275 | * iot-2/evt/ = 10 |
sathipal | 0:f86732d81998 | 276 | * /fmt/json = 9 |
sathipal | 0:f86732d81998 | 277 | * NULL char = 1 |
sathipal | 0:f86732d81998 | 278 | */ |
lokeshhk | 5:ea9f483e0294 | 279 | |
sathipal | 0:f86732d81998 | 280 | char topic[10 + strlen(eventName) + 9 + 1]; |
sathipal | 0:f86732d81998 | 281 | sprintf(topic, "%s%s%s", "iot-2/evt/", eventName, "/fmt/json"); |
lokeshhk | 5:ea9f483e0294 | 282 | |
sathipal | 0:f86732d81998 | 283 | message.qos = qos; |
sathipal | 0:f86732d81998 | 284 | message.retained = false; |
sathipal | 0:f86732d81998 | 285 | message.dup = false; |
sathipal | 0:f86732d81998 | 286 | message.payload = (void*)data; |
sathipal | 0:f86732d81998 | 287 | message.payloadlen = strlen(data); |
lokeshhk | 5:ea9f483e0294 | 288 | |
lokeshhk | 5:ea9f483e0294 | 289 | LOG("Publishing %s\r\n", data); |
lokeshhk | 5:ea9f483e0294 | 290 | int rc = mqttClient->publish(topic, message); |
sathipal | 0:f86732d81998 | 291 | return rc == 0; |
sathipal | 0:f86732d81998 | 292 | } |
sathipal | 0:f86732d81998 | 293 | |
lokeshhk | 5:ea9f483e0294 | 294 | void DeviceClient::setCommandCallback(CommandHandler callbackFunc) |
sathipal | 2:199ddea804cd | 295 | { |
sathipal | 0:f86732d81998 | 296 | handler = callbackFunc; |
sathipal | 0:f86732d81998 | 297 | } |
sathipal | 0:f86732d81998 | 298 | /** |
sathipal | 0:f86732d81998 | 299 | * Subscribe to commands from the application. This will be executed only for |
sathipal | 0:f86732d81998 | 300 | * registered flow (quickstart flow does not support command publish) |
sathipal | 0:f86732d81998 | 301 | */ |
lokeshhk | 5:ea9f483e0294 | 302 | int DeviceClient::subscribeToCommands() |
sathipal | 2:199ddea804cd | 303 | { |
sathipal | 0:f86732d81998 | 304 | int rc = 0; |
sathipal | 0:f86732d81998 | 305 | // iot-2/cmd/+/fmt/+ |
lokeshhk | 5:ea9f483e0294 | 306 | if ((rc = mqttClient->subscribe("iot-2/cmd/+/fmt/+", MQTT::QOS2, msgArrived)) != 0) |
lokeshhk | 5:ea9f483e0294 | 307 | WARN("rc from MQTT subscribe is %d\r\n", rc); |
sathipal | 0:f86732d81998 | 308 | return rc; |
sathipal | 0:f86732d81998 | 309 | } |
sathipal | 0:f86732d81998 | 310 | |
sathipal | 0:f86732d81998 | 311 | /** |
lokeshhk | 5:ea9f483e0294 | 312 | * Callback method to be registered with MQTT::Client. MQTT::Client calls whenever |
sathipal | 0:f86732d81998 | 313 | * any command is published to the topic subscribed earlier. |
sathipal | 0:f86732d81998 | 314 | */ |
sathipal | 0:f86732d81998 | 315 | void msgArrived(MQTT::MessageData& md) |
sathipal | 0:f86732d81998 | 316 | { |
sathipal | 0:f86732d81998 | 317 | // check whether callback is registered by the client code |
lokeshhk | 5:ea9f483e0294 | 318 | if(handler == NULL) |
sathipal | 3:3d91bf839b49 | 319 | { |
sathipal | 0:f86732d81998 | 320 | return; |
sathipal | 0:f86732d81998 | 321 | } |
lokeshhk | 5:ea9f483e0294 | 322 | |
sathipal | 0:f86732d81998 | 323 | MQTT::Message &message = md.message; |
sathipal | 0:f86732d81998 | 324 | char topic[md.topicName.lenstring.len + 1]; |
lokeshhk | 5:ea9f483e0294 | 325 | |
sathipal | 0:f86732d81998 | 326 | sprintf(topic, "%.*s", md.topicName.lenstring.len, md.topicName.lenstring.data); |
lokeshhk | 5:ea9f483e0294 | 327 | |
lokeshhk | 5:ea9f483e0294 | 328 | LOG("Message arrived on topic %s: %.*s\r\n", topic, message.payloadlen, message.payload); |
lokeshhk | 5:ea9f483e0294 | 329 | |
sathipal | 0:f86732d81998 | 330 | // Command topic: iot-2/cmd/blink/fmt/json - cmd is the string between cmd/ and /fmt/ |
sathipal | 0:f86732d81998 | 331 | char* start = strstr(topic, "/cmd/") + 5; |
sathipal | 0:f86732d81998 | 332 | int len = strstr(topic, "/fmt/") - start; |
sathipal | 0:f86732d81998 | 333 | |
sathipal | 0:f86732d81998 | 334 | char name[len + 1]; |
lokeshhk | 5:ea9f483e0294 | 335 | |
sathipal | 0:f86732d81998 | 336 | memcpy(name, start, len); |
sathipal | 0:f86732d81998 | 337 | name[len] = NULL; |
lokeshhk | 5:ea9f483e0294 | 338 | |
sathipal | 0:f86732d81998 | 339 | start = strstr(topic, "/fmt/") + 5; |
lokeshhk | 5:ea9f483e0294 | 340 | |
sathipal | 0:f86732d81998 | 341 | char format[20]; // ToDO: need to find the length of the format |
sathipal | 0:f86732d81998 | 342 | strcpy(format, start); |
lokeshhk | 5:ea9f483e0294 | 343 | |
sathipal | 0:f86732d81998 | 344 | char payload[message.payloadlen + 1]; |
sathipal | 0:f86732d81998 | 345 | sprintf(payload, "%.*s", message.payloadlen, (char*)message.payload); |
lokeshhk | 5:ea9f483e0294 | 346 | |
sathipal | 0:f86732d81998 | 347 | IoTF::Command cmd(name, format, payload); |
sathipal | 0:f86732d81998 | 348 | (*handler)(cmd); |
sathipal | 0:f86732d81998 | 349 | } |
sathipal | 0:f86732d81998 | 350 | |
sathipal | 3:3d91bf839b49 | 351 | /** |
sathipal | 3:3d91bf839b49 | 352 | * Disconnects the connection in order. |
sathipal | 3:3d91bf839b49 | 353 | */ |
lokeshhk | 5:ea9f483e0294 | 354 | bool DeviceClient::disconnect() |
sathipal | 2:199ddea804cd | 355 | { |
sathipal | 3:3d91bf839b49 | 356 | int rc = 0; |
lokeshhk | 5:ea9f483e0294 | 357 | if(mqttClient->isConnected()) |
sathipal | 3:3d91bf839b49 | 358 | { |
lokeshhk | 5:ea9f483e0294 | 359 | rc = mqttClient->disconnect(); |
sathipal | 0:f86732d81998 | 360 | } |
lokeshhk | 5:ea9f483e0294 | 361 | |
lokeshhk | 5:ea9f483e0294 | 362 | NetworkInterface* net = mqttNetwork->getEth(); |
lokeshhk | 5:ea9f483e0294 | 363 | mqttNetwork->disconnect(); |
lokeshhk | 5:ea9f483e0294 | 364 | net->disconnect(); |
sathipal | 3:3d91bf839b49 | 365 | connected = false; |
sathipal | 3:3d91bf839b49 | 366 | return rc == 0; |
sathipal | 0:f86732d81998 | 367 | } |
sathipal | 0:f86732d81998 | 368 | |
sathipal | 0:f86732d81998 | 369 | // Yield to allow MQTT client to process the command |
lokeshhk | 5:ea9f483e0294 | 370 | void DeviceClient::yield(int ms) |
sathipal | 2:199ddea804cd | 371 | { |
lokeshhk | 5:ea9f483e0294 | 372 | if(mqttClient->isConnected()) |
sathipal | 3:3d91bf839b49 | 373 | { |
lokeshhk | 5:ea9f483e0294 | 374 | mqttClient->yield(ms); |
sathipal | 0:f86732d81998 | 375 | } |
sathipal | 0:f86732d81998 | 376 | } |
sathipal | 0:f86732d81998 | 377 | |
sathipal | 2:199ddea804cd | 378 | // Obtain DeviceId address |
lokeshhk | 5:ea9f483e0294 | 379 | char* DeviceClient::getDeviceId(char* buf, int buflen) |
lokeshhk | 5:ea9f483e0294 | 380 | { |
lokeshhk | 5:ea9f483e0294 | 381 | if(this->deviceId == NULL || (strcmp("", this->deviceId) == 0)) |
sathipal | 3:3d91bf839b49 | 382 | { |
sathipal | 2:199ddea804cd | 383 | return getMac(buf, buflen); |
sathipal | 2:199ddea804cd | 384 | } else { |
sathipal | 2:199ddea804cd | 385 | return strncpy(buf, this->deviceId, buflen); |
sathipal | 2:199ddea804cd | 386 | } |
sathipal | 2:199ddea804cd | 387 | } |
sathipal | 0:f86732d81998 | 388 | // Obtain MAC address |
lokeshhk | 5:ea9f483e0294 | 389 | char* DeviceClient::getMac(char* buf, int buflen) |
lokeshhk | 5:ea9f483e0294 | 390 | { |
lokeshhk | 5:ea9f483e0294 | 391 | NetworkInterface* net = mqttNetwork->getEth(); |
lokeshhk | 5:ea9f483e0294 | 392 | strncpy(buf, net->get_mac_address(), buflen); |
sathipal | 0:f86732d81998 | 393 | |
sathipal | 0:f86732d81998 | 394 | char* pos; // Remove colons from mac address |
sathipal | 0:f86732d81998 | 395 | while ((pos = strchr(buf, ':')) != NULL) |
sathipal | 0:f86732d81998 | 396 | memmove(pos, pos + 1, strlen(pos) + 1); |
sathipal | 0:f86732d81998 | 397 | return buf; |
lokeshhk | 5:ea9f483e0294 | 398 | } |