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