Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MQTT - fix crash on reconnect and incoming message #15

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/AgrirouterClient/inc/AgrirouterClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class AgrirouterClient {
explicit AgrirouterClient(Settings *settings, uint32_t chunkSize);
~AgrirouterClient();

void renewConnection();

void registerDeviceWithRegCode(const std::string& registrationCode, AgrirouterSettings& agrirouterSettings);

// Messages without specific recipients
Expand All @@ -36,7 +38,7 @@ class AgrirouterClient {
void sendTaskdataZip(Addressing& addressing, std::string *messageId, const std::string& teamsetId, char *taskdataZip, int size, const std::string& fileName = "");
void sendChunk(AgrirouterMessage& message);

AgrirouterMessage createChunkMessage(std::string *messageId, Addressing& addressing, uint16_t numberOfChunk, uint16_t numberOfChunks,
AgrirouterMessage createChunkMessage(std::string *messageId, Addressing& addressing, uint16_t numberOfChunk, uint16_t numberOfChunks,
const std::string& teamSetContextId, const std::string& chunkContextId, const std::string& data,
uint32_t size, const std::string& technicalMessageType, const std::string& fileName = "");

Expand Down
2 changes: 2 additions & 0 deletions lib/AgrirouterClient/inc/ConnectionProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class ConnectionProvider

virtual ~ConnectionProvider() {}

virtual void renewConnection() {}

// Function pointer for callback functions
typedef size_t (*Callback)(char *content, size_t size, size_t nmemb, void *member);

Expand Down
4 changes: 2 additions & 2 deletions lib/AgrirouterClient/inc/MqttConnectionClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ class MqttConnectionClient {
void *m_member = nullptr;
Settings *m_settings = nullptr;

MqttCallback m_mqttCallback;
MqttErrorCallback m_mqttErrorCallback;
MqttCallback m_mqttCallback = NULL;
MqttErrorCallback m_mqttErrorCallback = NULL;

static int onPWCallback(char *buf, int size, int rwflag, void *userdata);
static void connectCallback(struct mosquitto *mosq, void *obj, int result);
Expand Down
4 changes: 3 additions & 1 deletion lib/AgrirouterClient/inc/MqttConnectionProvider.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ class MqttConnectionProvider : public ConnectionProvider
explicit MqttConnectionProvider(Settings *settings);
~MqttConnectionProvider();

void renewConnection() override;

// Struct to use curl chunked callbacks
typedef struct MemoryStruct
typedef struct MemoryStruct
{
char *memory = nullptr;
size_t size = 0;
Expand Down
21 changes: 11 additions & 10 deletions lib/AgrirouterClient/src/AgrirouterClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ void AgrirouterClient::init(Settings *settings)
m_messageProvider = nullptr;
}
m_messageProvider = new MessageProvider(settings, m_chunkSize);

if(m_connectionProvider != nullptr)
{
delete m_connectionProvider;
m_connectionProvider = nullptr;
}

if (settings->getConnectionType() == Settings::HTTP)
{
{
m_connectionProvider = new CurlConnectionProvider(settings);
}
else if (settings->getConnectionType() == Settings::MQTT)
Expand Down Expand Up @@ -83,14 +82,9 @@ void AgrirouterClient::registerDeviceWithRegCode(const std::string& registration
void AgrirouterClient::registrationCallback(bool success, void *member)
{
AgrirouterClient *self = static_cast<AgrirouterClient*>(member);
if(success)
if(success)
{
// reinit connection provider after onboard to new subscribe to the new topics
if (self->m_connectionProvider != nullptr)
{
delete self->m_connectionProvider;
self->m_connectionProvider = nullptr;
}
self->init(self->m_settings);
}
else
Expand All @@ -114,6 +108,14 @@ int32_t AgrirouterClient::getNextSeqNo()
return m_seqNo;
}

void AgrirouterClient::renewConnection()
{
if (m_settings->getConnectionType() == Settings::MQTT)
{
m_connectionProvider->renewConnection();
}
}

void AgrirouterClient::sendCapabilities(std::string *messageId, CapabilitySpecification *capabilities)
{
AgrirouterMessage message = m_messageProvider->getCapabilityMessage(messageId, getNextSeqNo(), "", capabilities);
Expand Down Expand Up @@ -220,10 +222,9 @@ void AgrirouterClient::requestMessages()

size_t AgrirouterClient::requestMessagesCallback(char *content, size_t size, size_t nmemb, void *member)
{
size_t realsize = size * nmemb;

AgrirouterClient *self = static_cast<AgrirouterClient *>(member);

size_t realsize = size * nmemb;
std::string message(content, realsize);

std::list<Response> responseList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,6 @@ void MqttConnectionClient::disconnectCallback(struct mosquitto *mosq, void *obj,
self->m_settings->callOnLog(MG_LFL_ERR, errorMessage);
(self->m_mqttErrorCallback) (reasonCode, errorMessage, "", self->m_member);


// try to reconnect
int reconn = mosquitto_reconnect(self->m_mosq);
if (reconn == MOSQ_ERR_SUCCESS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ void MqttConnectionProvider::init()
}
}

void MqttConnectionProvider::renewConnection()
{
this->init();
}

void MqttConnectionProvider::requestMqttErrorCallback(int errorCode, std::string message, std::string content, void *member)
{
MqttConnectionProvider *self = static_cast<MqttConnectionProvider *>(member);
Expand All @@ -61,7 +66,7 @@ void MqttConnectionProvider::requestMqttCallback(char *topic, void *payload, int
{
MqttConnectionProvider *self = static_cast<MqttConnectionProvider *>(member);
char* msg = (char*) payload;

if(msg)
{
// If msg starts with '{', it is not an array as it comes from curl, so add the square brackets
Expand Down