From 9929d7d87162fc0a85fff7aa101a26c80bb031b5 Mon Sep 17 00:00:00 2001 From: ExequielGonzalez Date: Fri, 7 May 2021 10:26:45 -0300 Subject: [PATCH 1/2] Fixed some bugs with SSL certificate --- src/CloudIoTCoreDevice.cpp | 92 ++++++++----- src/CloudIoTCoreMqtt.cpp | 276 ++++++++++++++++++++++--------------- src/CloudIoTCoreMqtt.h | 74 +++++----- 3 files changed, 267 insertions(+), 175 deletions(-) diff --git a/src/CloudIoTCoreDevice.cpp b/src/CloudIoTCoreDevice.cpp index 4046cc42..aae1dfb0 100644 --- a/src/CloudIoTCoreDevice.cpp +++ b/src/CloudIoTCoreDevice.cpp @@ -21,7 +21,8 @@ CloudIoTCoreDevice::CloudIoTCoreDevice() {} CloudIoTCoreDevice::CloudIoTCoreDevice(const char *project_id, const char *location, const char *registry_id, - const char *device_id) { + const char *device_id) +{ setProjectId(project_id); setLocation(location); setRegistryId(registry_id); @@ -32,7 +33,8 @@ CloudIoTCoreDevice::CloudIoTCoreDevice(const char *project_id, const char *location, const char *registry_id, const char *device_id, - const char *private_key) { + const char *private_key) +{ setProjectId(project_id); setLocation(location); setRegistryId(registry_id); @@ -40,110 +42,135 @@ CloudIoTCoreDevice::CloudIoTCoreDevice(const char *project_id, setPrivateKey(private_key); } -unsigned long CloudIoTCoreDevice::getExpMillis() { +unsigned long CloudIoTCoreDevice::getExpMillis() +{ return exp_millis; } -int CloudIoTCoreDevice::getJwtExpSecs() { +int CloudIoTCoreDevice::getJwtExpSecs() +{ return jwt_exp_secs; } -String CloudIoTCoreDevice::createJWT(long long int current_time) { +String CloudIoTCoreDevice::createJWT(long long int current_time) +{ exp_millis = millis() + (jwt_exp_secs * 1000); jwt = CreateJwt(project_id, current_time, priv_key, this->jwt_exp_secs); return jwt; } -String CloudIoTCoreDevice::createJWT(long long int current_time, int exp_in_secs) { +String CloudIoTCoreDevice::createJWT(long long int current_time, int exp_in_secs) +{ jwt_exp_secs = exp_in_secs; exp_millis = millis() + (jwt_exp_secs * 1000); jwt = CreateJwt(project_id, current_time, priv_key, exp_in_secs); return jwt; } -String CloudIoTCoreDevice::getJWT() { +String CloudIoTCoreDevice::getJWT() +{ return jwt; } -String CloudIoTCoreDevice::getBasePath() { +String CloudIoTCoreDevice::getBasePath() +{ return String("/v1/projects/") + project_id + "/locations/" + location + "/registries/" + registry_id + "/devices/" + device_id; } -String CloudIoTCoreDevice::getClientId(){ +String CloudIoTCoreDevice::getClientId() +{ return String("projects/") + project_id + "/locations/" + location + "/registries/" + registry_id + "/devices/" + device_id; } -String CloudIoTCoreDevice::getConfigTopic(){ +String CloudIoTCoreDevice::getConfigTopic() +{ return String("/devices/") + device_id + "/config"; } -String CloudIoTCoreDevice::getCommandsTopic(){ +String CloudIoTCoreDevice::getCommandsTopic() +{ return String("/devices/") + device_id + "/commands/#"; } -String CloudIoTCoreDevice::getDeviceId(){ +String CloudIoTCoreDevice::getDeviceId() +{ return String(device_id); } -String CloudIoTCoreDevice::getEventsTopic(){ +String CloudIoTCoreDevice::getEventsTopic() +{ return String("/devices/") + device_id + "/events"; } -String CloudIoTCoreDevice::getStateTopic(){ +String CloudIoTCoreDevice::getStateTopic() +{ return String("/devices/") + device_id + "/state"; } -String CloudIoTCoreDevice::getConfigPath(int version) { +String CloudIoTCoreDevice::getConfigPath(int version) +{ return this->getBasePath() + "/config?local_version=" + version; } -String CloudIoTCoreDevice::getLastConfigPath() { +String CloudIoTCoreDevice::getLastConfigPath() +{ return this->getConfigPath(0); } -String CloudIoTCoreDevice::getSendTelemetryPath() { +String CloudIoTCoreDevice::getSendTelemetryPath() +{ return this->getBasePath() + ":publishEvent"; } -String CloudIoTCoreDevice::getSetStatePath() { +String CloudIoTCoreDevice::getSetStatePath() +{ return this->getBasePath() + ":setState"; } -void CloudIoTCoreDevice::setJwtExpSecs(int exp_in_secs) { +void CloudIoTCoreDevice::setJwtExpSecs(int exp_in_secs) +{ this->jwt_exp_secs = exp_in_secs; } -CloudIoTCoreDevice &CloudIoTCoreDevice::setProjectId(const char *project_id) { +CloudIoTCoreDevice &CloudIoTCoreDevice::setProjectId(const char *project_id) +{ this->project_id = project_id; return *this; } -CloudIoTCoreDevice &CloudIoTCoreDevice::setLocation(const char *location) { +CloudIoTCoreDevice &CloudIoTCoreDevice::setLocation(const char *location) +{ this->location = location; return *this; } -CloudIoTCoreDevice &CloudIoTCoreDevice::setRegistryId(const char *registry_id) { +CloudIoTCoreDevice &CloudIoTCoreDevice::setRegistryId(const char *registry_id) +{ this->registry_id = registry_id; return *this; } -CloudIoTCoreDevice &CloudIoTCoreDevice::setDeviceId(const char *device_id) { +CloudIoTCoreDevice &CloudIoTCoreDevice::setDeviceId(const char *device_id) +{ this->device_id = device_id; return *this; } -CloudIoTCoreDevice &CloudIoTCoreDevice::setPrivateKey(const char *private_key) { - if ( strlen(private_key) != (95) ) { +CloudIoTCoreDevice &CloudIoTCoreDevice::setPrivateKey(const char *private_key) +{ + if (strlen(private_key) != (95)) + { Serial.println("Warning: expected private key to be 95, was: " + - String(strlen(private_key))); + String(strlen(private_key))); } priv_key[8] = 0; - for (int i = 7; i >= 0; i--) { + for (int i = 7; i >= 0; i--) + { priv_key[i] = 0; - for (int byte_num = 0; byte_num < 4; byte_num++) { + for (int byte_num = 0; byte_num < 4; byte_num++) + { priv_key[i] = (priv_key[i] << 8) + strtoul(private_key, NULL, 16); private_key += 3; } @@ -151,10 +178,13 @@ CloudIoTCoreDevice &CloudIoTCoreDevice::setPrivateKey(const char *private_key) { return *this; } -CloudIoTCoreDevice &CloudIoTCoreDevice::setPrivateKey(const unsigned char *private_key) { +CloudIoTCoreDevice &CloudIoTCoreDevice::setPrivateKey(const unsigned char *private_key) +{ priv_key[8] = 0; - for (int i = 7; i >= 0; i--) { - for (int byte_num = 0; byte_num < 4; byte_num++) { + for (int i = 7; i >= 0; i--) + { + for (int byte_num = 0; byte_num < 4; byte_num++) + { priv_key[i] = (priv_key[i] << 8) + *private_key; ++private_key; } diff --git a/src/CloudIoTCoreMqtt.cpp b/src/CloudIoTCoreMqtt.cpp index b6cb0ba1..e09e38f4 100644 --- a/src/CloudIoTCoreMqtt.cpp +++ b/src/CloudIoTCoreMqtt.cpp @@ -20,19 +20,21 @@ void messageReceived(String &topic, String &payload); // callback for startMQTTAdvanced void messageReceivedAdvanced(MQTTClient *client, char topic[], char bytes[], int length); - /////////////////////////////// // MQTT common functions /////////////////////////////// CloudIoTCoreMqtt::CloudIoTCoreMqtt( - MQTTClient *_mqttClient, Client *_netClient, CloudIoTCoreDevice *_device){ + MQTTClient *_mqttClient, WiFiClientSecure *_netClient, CloudIoTCoreDevice *_device) +{ this->mqttClient = _mqttClient; this->netClient = _netClient; this->device = _device; } -boolean CloudIoTCoreMqtt::loop() { - if (millis() > device->getExpMillis() && mqttClient->connected()) { +boolean CloudIoTCoreMqtt::loop() +{ + if (millis() > device->getExpMillis() && mqttClient->connected()) + { // reconnect Serial.println("Reconnecting before JWT expiration"); mqttClient->disconnect(); @@ -40,10 +42,12 @@ boolean CloudIoTCoreMqtt::loop() { return this->mqttClient->loop(); } -void CloudIoTCoreMqtt::mqttConnect(bool skip) { +void CloudIoTCoreMqtt::mqttConnect(bool skip) +{ Serial.println("Connecting..."); bool keepgoing = true; - while (keepgoing) { + while (keepgoing) + { bool result = this->mqttClient->connect( device->getClientId().c_str(), @@ -51,7 +55,8 @@ void CloudIoTCoreMqtt::mqttConnect(bool skip) { getJwt().c_str(), skip); - if (this->mqttClient->lastError() != LWMQTT_SUCCESS && result){ + if (this->mqttClient->lastError() != LWMQTT_SUCCESS && result) + { // TODO: refactorme // Inform the client why it could not connect and help debugging. logError(); @@ -59,11 +64,13 @@ void CloudIoTCoreMqtt::mqttConnect(bool skip) { logConfiguration(false); // See https://cloud.google.com/iot/docs/how-tos/exponential-backoff - if (this->__backoff__ < this->__minbackoff__) { + if (this->__backoff__ < this->__minbackoff__) + { this->__backoff__ = this->__minbackoff__; } this->__backoff__ = (this->__backoff__ * this->__factor__) + random(this->__jitter__); - if (this->__backoff__ > this->__max_backoff__) { + if (this->__backoff__ > this->__max_backoff__) + { this->__backoff__ = this->__max_backoff__; } @@ -73,9 +80,12 @@ void CloudIoTCoreMqtt::mqttConnect(bool skip) { Serial.println("Delaying " + String(this->__backoff__) + "ms"); delay(this->__backoff__); keepgoing = true; - } else { + } + else + { Serial.println(mqttClient->connected() ? "connected" : "not connected"); - if (!mqttClient->connected()) { + if (!mqttClient->connected()) + { Serial.println("Settings incorrect or missing a cyper for SSL"); mqttClient->disconnect(); logConfiguration(false); @@ -83,7 +93,9 @@ void CloudIoTCoreMqtt::mqttConnect(bool skip) { keepgoing = true; Serial.println("Waiting 60 seconds, retry will likely fail"); delay(this->__max_backoff__); - } else { + } + else + { // We're now connected Serial.println("\nLibrary connected!"); keepgoing = false; @@ -100,7 +112,8 @@ void CloudIoTCoreMqtt::mqttConnect(bool skip) { onConnect(); } -void CloudIoTCoreMqtt::mqttConnectAsync(bool skip) { +void CloudIoTCoreMqtt::mqttConnectAsync(bool skip) +{ Serial.println("Connecting..."); bool result = @@ -110,7 +123,8 @@ void CloudIoTCoreMqtt::mqttConnectAsync(bool skip) { getJwt().c_str(), skip); - if (this->mqttClient->lastError() != LWMQTT_SUCCESS && result == true){ + if (this->mqttClient->lastError() != LWMQTT_SUCCESS && result == true) + { // TODO: refactorme // Inform the client why it could not connect and help debugging. logError(); @@ -118,11 +132,13 @@ void CloudIoTCoreMqtt::mqttConnectAsync(bool skip) { logConfiguration(false); // See https://cloud.google.com/iot/docs/how-tos/exponential-backoff - if (this->__backoff__ < this->__minbackoff__) { + if (this->__backoff__ < this->__minbackoff__) + { this->__backoff__ = this->__minbackoff__; } this->__backoff__ = (this->__backoff__ * this->__factor__) + random(this->__jitter__); - if (this->__backoff__ > this->__max_backoff__) { + if (this->__backoff__ > this->__max_backoff__) + { this->__backoff__ = this->__max_backoff__; } @@ -131,177 +147,217 @@ void CloudIoTCoreMqtt::mqttConnectAsync(bool skip) { skip = false; // Serial.println("Delaying " + String(this->__backoff__) + "ms"); // delay(this->__backoff__); - } else { + } + else + { Serial.println(mqttClient->connected() ? "connected" : "not connected"); - if (!mqttClient->connected()) { + if (!mqttClient->connected()) + { Serial.println("No internet or Settings incorrect or missing a cyper for SSL"); mqttClient->disconnect(); logConfiguration(false); skip = false; Serial.println("\naborting mqtt connection attempt, lets rety later...\tLibrary not connected!"); // delay(this->__max_backoff__); - } else { + } + else + { // We're now connected Serial.println("\nLibrary connected!"); this->__backoff__ = this->__minbackoff__; } } - // Set QoS to 1 (ack) for configuration messages this->mqttClient->subscribe(device->getConfigTopic(), 1); // QoS 0 (no ack) for commands this->mqttClient->subscribe(device->getCommandsTopic(), 0); + //!IMPORTANTE + //Messages published to this MQTT topic are forwarded to the corresponding registry's default telemetry topic. + // https: //cloud.google.com/iot/docs/how-tos/mqtt-bridge#publishing_telemetry_events + onConnect(); } -void CloudIoTCoreMqtt::startMQTT() { +void CloudIoTCoreMqtt::startMQTT() +{ this->mqttClient->begin(useLts ? CLOUD_IOT_CORE_MQTT_HOST_LTS : CLOUD_IOT_CORE_MQTT_HOST, - CLOUD_IOT_CORE_MQTT_PORT, *netClient); + CLOUD_IOT_CORE_MQTT_PORT, *netClient); this->mqttClient->onMessage(messageReceived); } -void CloudIoTCoreMqtt::startMQTTAdvanced() { +void CloudIoTCoreMqtt::startMQTTAdvanced() +{ this->mqttClient->begin(useLts ? CLOUD_IOT_CORE_MQTT_HOST_LTS : CLOUD_IOT_CORE_MQTT_HOST, - CLOUD_IOT_CORE_MQTT_PORT, *netClient); + CLOUD_IOT_CORE_MQTT_PORT, *netClient); this->mqttClient->onMessageAdvanced(messageReceivedAdvanced); } -bool CloudIoTCoreMqtt::publishTelemetry(const String &data) { +bool CloudIoTCoreMqtt::publishTelemetry(const String &data) +{ return this->mqttClient->publish(device->getEventsTopic(), data); } -bool CloudIoTCoreMqtt::publishTelemetry(const String &data, int qos) { +bool CloudIoTCoreMqtt::publishTelemetry(const String &data, int qos) +{ return this->mqttClient->publish(device->getEventsTopic(), data, false, qos); } -bool CloudIoTCoreMqtt::publishTelemetry(const char* data, int length) { +bool CloudIoTCoreMqtt::publishTelemetry(const char *data, int length) +{ return this->mqttClient->publish(device->getEventsTopic().c_str(), data, length); } -bool CloudIoTCoreMqtt::publishTelemetry(const String &subtopic, const String &data) { +bool CloudIoTCoreMqtt::publishTelemetry(const String &subtopic, const String &data) +{ return this->mqttClient->publish(device->getEventsTopic() + subtopic, data); } -bool CloudIoTCoreMqtt::publishTelemetry(const String &subtopic, const String &data, int qos) { +bool CloudIoTCoreMqtt::publishTelemetry(const String &subtopic, const String &data, int qos) +{ return this->mqttClient->publish(device->getEventsTopic() + subtopic, data, false, qos); } -bool CloudIoTCoreMqtt::publishTelemetry(const String &subtopic, const char* data, int length) { +bool CloudIoTCoreMqtt::publishTelemetry(const String &subtopic, const char *data, int length) +{ return this->mqttClient->publish(String(device->getEventsTopic() + subtopic).c_str(), data, length); } // Helper that just sends default sensor -bool CloudIoTCoreMqtt::publishState(const String &data) { +bool CloudIoTCoreMqtt::publishState(const String &data) +{ return this->mqttClient->publish(device->getStateTopic(), data); } -bool CloudIoTCoreMqtt::publishState(const char* data) { +bool CloudIoTCoreMqtt::publishState(const char *data) +{ return this->mqttClient->publish(device->getStateTopic().c_str(), data); } -bool CloudIoTCoreMqtt::publishState(const char* data, int length) { +bool CloudIoTCoreMqtt::publishState(const char *data, int length) +{ return this->mqttClient->publish(device->getStateTopic().c_str(), data, length); } -void CloudIoTCoreMqtt::logError() { +void CloudIoTCoreMqtt::logError() +{ Serial.println(this->mqttClient->lastError()); - switch(this->mqttClient->lastError()) { - case (LWMQTT_BUFFER_TOO_SHORT): - Serial.println("LWMQTT_BUFFER_TOO_SHORT"); - break; - case (LWMQTT_VARNUM_OVERFLOW): - Serial.println("LWMQTT_VARNUM_OVERFLOW"); - break; - case (LWMQTT_NETWORK_FAILED_CONNECT): - Serial.println("LWMQTT_NETWORK_FAILED_CONNECT"); - break; - case (LWMQTT_NETWORK_TIMEOUT): - Serial.println("LWMQTT_NETWORK_TIMEOUT"); - break; - case (LWMQTT_NETWORK_FAILED_READ): - Serial.println("LWMQTT_NETWORK_FAILED_READ"); - break; - case (LWMQTT_NETWORK_FAILED_WRITE): - Serial.println("LWMQTT_NETWORK_FAILED_WRITE"); - break; - case (LWMQTT_REMAINING_LENGTH_OVERFLOW): - Serial.println("LWMQTT_REMAINING_LENGTH_OVERFLOW"); - break; - case (LWMQTT_REMAINING_LENGTH_MISMATCH): - Serial.println("LWMQTT_REMAINING_LENGTH_MISMATCH"); - break; - case (LWMQTT_MISSING_OR_WRONG_PACKET): - Serial.println("LWMQTT_MISSING_OR_WRONG_PACKET"); - break; - case (LWMQTT_CONNECTION_DENIED): - Serial.println("LWMQTT_CONNECTION_DENIED"); - break; - case (LWMQTT_FAILED_SUBSCRIPTION): - Serial.println("LWMQTT_FAILED_SUBSCRIPTION"); - break; - case (LWMQTT_SUBACK_ARRAY_OVERFLOW): - Serial.println("LWMQTT_SUBACK_ARRAY_OVERFLOW"); - break; - case (LWMQTT_PONG_TIMEOUT): - Serial.println("LWMQTT_PONG_TIMEOUT"); - break; - default: - Serial.println("This error code should never be reached."); - break; + switch (this->mqttClient->lastError()) + { + case (LWMQTT_BUFFER_TOO_SHORT): + Serial.println("LWMQTT_BUFFER_TOO_SHORT"); + break; + case (LWMQTT_VARNUM_OVERFLOW): + Serial.println("LWMQTT_VARNUM_OVERFLOW"); + break; + case (LWMQTT_NETWORK_FAILED_CONNECT): + Serial.println("LWMQTT_NETWORK_FAILED_CONNECT"); + break; + case (LWMQTT_NETWORK_TIMEOUT): + Serial.println("LWMQTT_NETWORK_TIMEOUT"); + break; + case (LWMQTT_NETWORK_FAILED_READ): + Serial.println("LWMQTT_NETWORK_FAILED_READ"); + break; + case (LWMQTT_NETWORK_FAILED_WRITE): + Serial.println("LWMQTT_NETWORK_FAILED_WRITE"); + break; + case (LWMQTT_REMAINING_LENGTH_OVERFLOW): + Serial.println("LWMQTT_REMAINING_LENGTH_OVERFLOW"); + break; + case (LWMQTT_REMAINING_LENGTH_MISMATCH): + Serial.println("LWMQTT_REMAINING_LENGTH_MISMATCH"); + break; + case (LWMQTT_MISSING_OR_WRONG_PACKET): + Serial.println("LWMQTT_MISSING_OR_WRONG_PACKET"); + break; + case (LWMQTT_CONNECTION_DENIED): + Serial.println("LWMQTT_CONNECTION_DENIED"); + break; + case (LWMQTT_FAILED_SUBSCRIPTION): + Serial.println("LWMQTT_FAILED_SUBSCRIPTION"); + break; + case (LWMQTT_SUBACK_ARRAY_OVERFLOW): + Serial.println("LWMQTT_SUBACK_ARRAY_OVERFLOW"); + break; + case (LWMQTT_PONG_TIMEOUT): + Serial.println("LWMQTT_PONG_TIMEOUT"); + break; + default: + Serial.println("This error code should never be reached."); + break; } } -void CloudIoTCoreMqtt::logConfiguration(bool showJWT) { +void CloudIoTCoreMqtt::logConfiguration(bool showJWT) +{ Serial.println("Connect with " + String(useLts ? CLOUD_IOT_CORE_MQTT_HOST_LTS : CLOUD_IOT_CORE_MQTT_HOST) + - ":" + String(CLOUD_IOT_CORE_MQTT_PORT)); + ":" + String(CLOUD_IOT_CORE_MQTT_PORT)); Serial.println("ClientId: " + device->getClientId()); - if (showJWT) { + if (showJWT) + { Serial.println("JWT: " + getJwt()); } } -void CloudIoTCoreMqtt::logReturnCode() { +void CloudIoTCoreMqtt::logReturnCode() +{ Serial.println(this->mqttClient->returnCode()); - switch(this->mqttClient->returnCode()) { - case (LWMQTT_CONNECTION_ACCEPTED): - Serial.println("OK"); - break; - case (LWMQTT_UNACCEPTABLE_PROTOCOL): - Serial.println("LWMQTT_UNACCEPTABLE_PROTOCOLL"); - break; - case (LWMQTT_IDENTIFIER_REJECTED): - Serial.println("LWMQTT_IDENTIFIER_REJECTED"); - break; - case (LWMQTT_SERVER_UNAVAILABLE): - Serial.println("LWMQTT_SERVER_UNAVAILABLE"); - break; - case (LWMQTT_BAD_USERNAME_OR_PASSWORD): - Serial.println("LWMQTT_BAD_USERNAME_OR_PASSWORD"); - break; - case (LWMQTT_NOT_AUTHORIZED): - Serial.println("LWMQTT_NOT_AUTHORIZED"); - break; - case (LWMQTT_UNKNOWN_RETURN_CODE): - Serial.println("LWMQTT_UNKNOWN_RETURN_CODE"); - break; - default: - Serial.println("This return code should never be reached."); - break; + switch (this->mqttClient->returnCode()) + { + case (LWMQTT_CONNECTION_ACCEPTED): + Serial.println("OK"); + break; + case (LWMQTT_UNACCEPTABLE_PROTOCOL): + Serial.println("LWMQTT_UNACCEPTABLE_PROTOCOLL"); + break; + case (LWMQTT_IDENTIFIER_REJECTED): + Serial.println("LWMQTT_IDENTIFIER_REJECTED"); + break; + case (LWMQTT_SERVER_UNAVAILABLE): + Serial.println("LWMQTT_SERVER_UNAVAILABLE"); + break; + case (LWMQTT_BAD_USERNAME_OR_PASSWORD): + Serial.println("LWMQTT_BAD_USERNAME_OR_PASSWORD"); + break; + case (LWMQTT_NOT_AUTHORIZED): + Serial.println("LWMQTT_NOT_AUTHORIZED"); + break; + case (LWMQTT_UNKNOWN_RETURN_CODE): + Serial.println("LWMQTT_UNKNOWN_RETURN_CODE"); + break; + default: + Serial.println("This return code should never be reached."); + break; } } -void CloudIoTCoreMqtt::onConnect() { - if (logConnect) { +void CloudIoTCoreMqtt::onConnect() +{ + if (logConnect) + { publishState("connected"); } } -void CloudIoTCoreMqtt::setLogConnect(boolean enabled) { +void CloudIoTCoreMqtt::setLogConnect(boolean enabled) +{ this->logConnect = enabled; } -void CloudIoTCoreMqtt::setUseLts(boolean enabled) { +void CloudIoTCoreMqtt::setUseLts(boolean enabled) +{ this->useLts = enabled; } + +//implemented by Exe +String CloudIoTCoreMqtt::getDeviceId() +{ + return device->getDeviceId(); +} + +String CloudIoTCoreMqtt::getClientId() +{ + return device->getClientId(); +} diff --git a/src/CloudIoTCoreMqtt.h b/src/CloudIoTCoreMqtt.h index 0ac5974c..71e6ec21 100644 --- a/src/CloudIoTCoreMqtt.h +++ b/src/CloudIoTCoreMqtt.h @@ -19,46 +19,52 @@ #include "CloudIoTCoreDevice.h" #include #include +#include -class CloudIoTCoreMqtt { - private: - int __backoff__ = 1000; // current backoff, milliseconds - static const int __factor__ = 2.5f; - static const int __minbackoff__ = 1000; // minimum backoff, ms - static const int __max_backoff__ = 60000; // maximum backoff, ms - static const int __jitter__ = 500; // max random jitter, ms - boolean logConnect = true; - boolean useLts = false; +class CloudIoTCoreMqtt +{ +private: + int __backoff__ = 1000; // current backoff, milliseconds + static const int __factor__ = 2.5f; + static const int __minbackoff__ = 1000; // minimum backoff, ms + static const int __max_backoff__ = 60000; // maximum backoff, ms + static const int __jitter__ = 500; // max random jitter, ms + boolean logConnect = true; + boolean useLts = false; - MQTTClient *mqttClient; - Client *netClient; - CloudIoTCoreDevice *device; + MQTTClient *mqttClient; + WiFiClientSecure *netClient; + CloudIoTCoreDevice *device; - public: - CloudIoTCoreMqtt(MQTTClient *mqttClient, Client *netClient, CloudIoTCoreDevice *device); +public: + CloudIoTCoreMqtt(MQTTClient *mqttClient, WiFiClientSecure *netClient, CloudIoTCoreDevice *device); - boolean loop(); - void mqttConnect(bool skip = false); - void mqttConnectAsync(bool skip = false); - void startMQTT(); - void startMQTTAdvanced(); + boolean loop(); + void mqttConnect(bool skip = false); + void mqttConnectAsync(bool skip = false); + void startMQTT(); + void startMQTTAdvanced(); - bool publishTelemetry(const String &data); - bool publishTelemetry(const String &data, int qos); - bool publishTelemetry(const char* data, int length); - bool publishTelemetry(const String &subtopic, const String &data); - bool publishTelemetry(const String &subtopic, const String &data, int qos); - bool publishTelemetry(const String &subtopic, const char* data, int length); - bool publishState(const String &data); - bool publishState(const char* data); - bool publishState(const char* data, int length); + bool publishTelemetry(const String &data); + bool publishTelemetry(const String &data, int qos); + bool publishTelemetry(const char *data, int length); + bool publishTelemetry(const String &subtopic, const String &data); + bool publishTelemetry(const String &subtopic, const String &data, int qos); + bool publishTelemetry(const String &subtopic, const char *data, int length); + bool publishState(const String &data); + bool publishState(const char *data); + bool publishState(const char *data, int length); - void logConfiguration(bool showJWT); - void logError(); - void logReturnCode(); + void logConfiguration(bool showJWT); + void logError(); + void logReturnCode(); - void onConnect(); - void setLogConnect(boolean enabled); - void setUseLts(boolean enabled); + void onConnect(); + void setLogConnect(boolean enabled); + void setUseLts(boolean enabled); + + //implemented by Exe + String getDeviceId(); + String getClientId(); }; #endif // __CLOUDIOTCORE_MQTT_H__ From 7f24fa11931356170efaa57e04f50a383b39fdbb Mon Sep 17 00:00:00 2001 From: ExequielGonzalez Date: Mon, 10 May 2021 09:54:34 -0300 Subject: [PATCH 2/2] publishTelemetry now send to state topic by default --- src/CloudIoTCoreMqtt.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/CloudIoTCoreMqtt.cpp b/src/CloudIoTCoreMqtt.cpp index e09e38f4..aed530a3 100644 --- a/src/CloudIoTCoreMqtt.cpp +++ b/src/CloudIoTCoreMqtt.cpp @@ -196,7 +196,7 @@ void CloudIoTCoreMqtt::startMQTTAdvanced() bool CloudIoTCoreMqtt::publishTelemetry(const String &data) { - return this->mqttClient->publish(device->getEventsTopic(), data); + return this->mqttClient->publish(device->getStateTopic(), data); } bool CloudIoTCoreMqtt::publishTelemetry(const String &data, int qos)