Skip to content

Commit

Permalink
Added publish-mode/subscribe-mode (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Sercan Uzun <sercan.uzun@campus.tu-berlin.de>
  • Loading branch information
srcnuzn and Sercan Uzun authored Jun 24, 2021
1 parent 868eb79 commit 58138eb
Show file tree
Hide file tree
Showing 6 changed files with 748 additions and 287 deletions.
5 changes: 5 additions & 0 deletions GoogleTest/Tests/mqtt_tests/mocks/mqtt_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
// The required functions, that are implemented in this source-file.
#include "mqtt_required.h"

/*********************************************************************************************/
/* Private defines --------------------------------------------------------------*/

#define MQTT_SUBTOPIC_1 "mc-labor/soenke/test"

/*********************************************************************************************/
/* Private function prototypes --------------------------------------------------------------*/

Expand Down
489 changes: 350 additions & 139 deletions STM32CubeIDE/Application/src-gen/MqttClientStatemachine.c

Large diffs are not rendered by default.

44 changes: 33 additions & 11 deletions STM32CubeIDE/Application/src-gen/MqttClientStatemachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extern "C" {
*/

/*! Define number of states in the state enum */
#define MQTTCLIENTSTATEMACHINE_STATE_COUNT 20
#define MQTTCLIENTSTATEMACHINE_STATE_COUNT 22

/*! Define dimension of the state configuration vector for orthogonal states. */
#define MQTTCLIENTSTATEMACHINE_MAX_ORTHOGONAL_STATES 1
Expand All @@ -64,10 +64,12 @@ extern "C" {
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_UNDEFINED_R1_START 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_UNDEFINED_R1_STOP 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_CONNECTTOBROKER 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_ENTRY 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_PUBLISHTOPIC 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_WAIT 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_PUBLISHMESSAGES 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_PUBLISHMESSAGES_R1_PUBLISHTOPIC 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_PUBLISHMESSAGES_R1_WAIT 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_CONNECTTOBROKER 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_ONLINE_R1_RECEIVEMESSAGES 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_OFFLINE 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_OFFLINE_R1_SETTOPIC 0
#define SCVI_MQTTCLIENTSTATEMACHINE_MAIN_REGION_OFFLINE_R1_ENTRY 0
Expand All @@ -89,10 +91,12 @@ typedef enum
MqttClientStatemachine_main_region_Undefined_r1_Start,
MqttClientStatemachine_main_region_Undefined_r1_Stop,
MqttClientStatemachine_main_region_Online,
MqttClientStatemachine_main_region_Online_r1_ConnectToBroker,
MqttClientStatemachine_main_region_Online_r1_Entry,
MqttClientStatemachine_main_region_Online_r1_PublishTopic,
MqttClientStatemachine_main_region_Online_r1_Wait,
MqttClientStatemachine_main_region_Online_r1_PublishMessages,
MqttClientStatemachine_main_region_Online_r1_PublishMessages_r1_PublishTopic,
MqttClientStatemachine_main_region_Online_r1_PublishMessages_r1_Wait,
MqttClientStatemachine_main_region_Online_r1_ConnectToBroker,
MqttClientStatemachine_main_region_Online_r1_ReceiveMessages,
MqttClientStatemachine_main_region_Offline,
MqttClientStatemachine_main_region_Offline_r1_SetTopic,
MqttClientStatemachine_main_region_Offline_r1_Entry,
Expand All @@ -108,6 +112,7 @@ struct MqttClientStatemachineIface
{
sc_integer dT;
sc_integer publishInterval;
sc_boolean publishingEnabled;
sc_integer watchdogTimer;
};

Expand Down Expand Up @@ -143,6 +148,8 @@ struct MqttClientStatemachineIfaceWizFi360
sc_boolean testModule_raised;
sc_observable restartModule;
sc_boolean restartModule_raised;
sc_observable ledOn;
sc_boolean ledOn_raised;
sc_observable resetModule;
sc_boolean resetModule_raised;
sc_observable start;
Expand All @@ -158,6 +165,7 @@ struct MqttClientStatemachineIfaceWizFi360
sc_integer testFailCounter;
sc_integer restartFailCounter;
sc_integer resetFailCounter;
sc_boolean ledState;
};


Expand All @@ -169,9 +177,9 @@ struct MqttClientStatemachineTimeEvents
sc_boolean mqttClientStatemachine_main_region_Undefined_r1_ResetModule_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Undefined_r1_TestModule_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Undefined_r1_RestartModule_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Online_r1_PublishMessages_r1_PublishTopic_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Online_r1_PublishMessages_r1_Wait_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Online_r1_ConnectToBroker_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Online_r1_PublishTopic_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Online_r1_Wait_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Offline_r1_SetTopic_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Offline_r1_Entry_tev0_raised;
sc_boolean mqttClientStatemachine_main_region_Offline_r1_SetStationMode_tev0_raised;
Expand All @@ -198,9 +206,9 @@ struct MqttClientStatemachineTimeEventsEvBuf {
sc_boolean MqttClientStatemachine_main_region_Undefined_r1_ResetModule_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Undefined_r1_TestModule_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Undefined_r1_RestartModule_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Online_r1_PublishMessages_r1_PublishTopic_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Online_r1_PublishMessages_r1_Wait_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Online_r1_ConnectToBroker_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Online_r1_PublishTopic_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Online_r1_Wait_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Offline_r1_SetTopic_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Offline_r1_Entry_time_event_0_raised;
sc_boolean MqttClientStatemachine_main_region_Offline_r1_SetStationMode_time_event_0_raised;
Expand Down Expand Up @@ -259,6 +267,10 @@ extern void mqttClientStatemachine_set_dT(MqttClientStatemachine* handle, sc_int
extern sc_integer mqttClientStatemachine_get_publishInterval(const MqttClientStatemachine* handle);
/*! Sets the value of the variable 'publishInterval' that is defined in the default interface scope. */
extern void mqttClientStatemachine_set_publishInterval(MqttClientStatemachine* handle, sc_integer value);
/*! Gets the value of the variable 'publishingEnabled' that is defined in the default interface scope. */
extern sc_boolean mqttClientStatemachine_get_publishingEnabled(const MqttClientStatemachine* handle);
/*! Sets the value of the variable 'publishingEnabled' that is defined in the default interface scope. */
extern void mqttClientStatemachine_set_publishingEnabled(MqttClientStatemachine* handle, sc_boolean value);
/*! Gets the value of the variable 'watchdogTimer' that is defined in the default interface scope. */
extern sc_integer mqttClientStatemachine_get_watchdogTimer(const MqttClientStatemachine* handle);
/*! Sets the value of the variable 'watchdogTimer' that is defined in the default interface scope. */
Expand Down Expand Up @@ -323,6 +335,12 @@ extern sc_observable* mqttClientStatemachine_WizFi360_get_restartModule(MqttClie
/*! Checks if the out event 'restartModule' that is defined in the interface scope 'WizFi360' has been raised. */
extern sc_boolean mqttClientStatemachine_WizFi360_is_raised_restartModule(const MqttClientStatemachine* handle);

/*! Returns the observable for the out event 'ledOn' that is defined in the interface scope 'WizFi360'. */
extern sc_observable* mqttClientStatemachine_WizFi360_get_ledOn(MqttClientStatemachine* handle);

/*! Checks if the out event 'ledOn' that is defined in the interface scope 'WizFi360' has been raised. */
extern sc_boolean mqttClientStatemachine_WizFi360_is_raised_ledOn(const MqttClientStatemachine* handle);

/*! Returns the observable for the out event 'resetModule' that is defined in the interface scope 'WizFi360'. */
extern sc_observable* mqttClientStatemachine_WizFi360_get_resetModule(MqttClientStatemachine* handle);

Expand Down Expand Up @@ -369,6 +387,10 @@ extern void mqttClientStatemachine_WizFi360_set_restartFailCounter(MqttClientSta
extern sc_integer mqttClientStatemachine_WizFi360_get_resetFailCounter(const MqttClientStatemachine* handle);
/*! Sets the value of the variable 'resetFailCounter' that is defined in the interface scope 'WizFi360'. */
extern void mqttClientStatemachine_WizFi360_set_resetFailCounter(MqttClientStatemachine* handle, sc_integer value);
/*! Gets the value of the variable 'ledState' that is defined in the interface scope 'WizFi360'. */
extern sc_boolean mqttClientStatemachine_WizFi360_get_ledState(const MqttClientStatemachine* handle);
/*! Sets the value of the variable 'ledState' that is defined in the interface scope 'WizFi360'. */
extern void mqttClientStatemachine_WizFi360_set_ledState(MqttClientStatemachine* handle, sc_boolean value);

/*!
* Checks whether the state machine is active (until 2.4.1 this method was used for states).
Expand Down
51 changes: 42 additions & 9 deletions STM32CubeIDE/Application/src/app/mqtt_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,41 @@
// The required functions, that are implemented in this source-file.
#include "mqtt_required.h"

/*********************************************************************************************/
/* Private defines --------------------------------------------------------------*/

#ifdef MQTT_SUBSCRIBE_MODE
#define MQTT_SUBTOPIC_1 "mc-labor/soenke/test"
#define MQTT_SUBTOPIC_2 "mc-labor/soenke/test2"
#define MQTT_SUBTOPIC_3 "mc-labor/soenke/test3"
#endif

/*********************************************************************************************/
/* Private function prototypes --------------------------------------------------------------*/

#ifdef MQTT_SUBSCRIBE_MODE
static void SubTopic1Received(const char* message);
static void SubTopic2Received(const char* message);
static void SubTopic3Received(const char* message);
#endif

/*********************************************************************************************/
/* Private variables --------------------------------------------------------------*/

#ifdef MQTT_SUBSCRIBE_MODE

#define MAX_STRING_SIZE 64

static uint32_t SubTopic1ReceivedCounter = 0;
static uint32_t SubTopic2ReceivedCounter = 0;
static uint32_t SubTopic3ReceivedCounter = 0;

static int SubTopic1ReceivedInteger = 0;
static double SubTopic1ReceivedDouble = 0.0;
static char SubTopic1ReceivedString[MAX_STRING_SIZE];
static int SubTopic1ReceivedStringSuccessfully = 0;

#endif

/*********************************************************************************************/
/* Implemetation of required application-specific functions ---------------------------------*/
Expand All @@ -42,9 +71,11 @@ static void SubTopic3Received(const char* message);
*/
void MqttClient_Publish()
{
#ifdef MQTT_PUBLISH_MODE
MqttClient_PublishInteger("int", HAL_GetTick());
MqttClient_PublishDouble("double", 0.5467*(double)(HAL_GetTick()));
MqttClient_PublishString("string", "this is a string");
#endif
}

/*
Expand All @@ -56,14 +87,17 @@ void MqttClient_Publish()
*/
void MqttClient_RegisterUserCallbacks()
{
#ifdef MQTT_SUBSCRIBE_MODE
MqttClient_RegisterSubscribeCallback(MQTT_SUBTOPIC_1, SubTopic1Received);
MqttClient_RegisterSubscribeCallback(MQTT_SUBTOPIC_2, SubTopic2Received);
MqttClient_RegisterSubscribeCallback(MQTT_SUBTOPIC_3, SubTopic3Received);
#endif
}

/*********************************************************************************************/
/* Private function definitions -------------------------------------------------------------*/

#ifdef MQTT_SUBSCRIBE_MODE
/*
* @brief Callback is fired, when MQTT_SUBTOPIC_1 is received.
* @note This is an example function to demonstrate how the callback registration works.
Expand All @@ -77,11 +111,11 @@ void MqttClient_RegisterUserCallbacks()
*/
static void SubTopic1Received(const char* message)
{
int i = MqttClient_ReadInteger(message, "int");
double d = MqttClient_ReadDouble(message, "double");
const char s[64];
int stringSuccess = MqttClient_ReadString(message, "string", 64, s);
__NOP();
SubTopic1ReceivedCounter++;
SubTopic1ReceivedInteger = MqttClient_ReadInteger(message, "int");
SubTopic1ReceivedDouble = MqttClient_ReadDouble(message, "double");
SubTopic1ReceivedStringSuccessfully = MqttClient_ReadString(message,
"string", MAX_STRING_SIZE, SubTopic1ReceivedString);
}

/*
Expand All @@ -97,8 +131,7 @@ static void SubTopic1Received(const char* message)
*/
static void SubTopic2Received(const char* message)
{
// int a = MqttClient_ReadInteger(message, description);
__NOP();
SubTopic2ReceivedCounter++;
}

/*
Expand All @@ -114,7 +147,7 @@ static void SubTopic2Received(const char* message)
*/
static void SubTopic3Received(const char* message)
{
// int a = MqttClient_ReadInteger(message, description);
__NOP();
SubTopic3ReceivedCounter++;
}
/*********************************************************************************************/
#endif
67 changes: 54 additions & 13 deletions STM32CubeIDE/Application/src/mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,39 @@
// Sample time of mqtt process in milliseconds
#define SAMPLE_TIME 100

#ifdef MQTT_SUBSCRIBE_MODE
#ifdef MQTT_PUBLISH_MODE
#error("Only select one mode! Cannot publish and subscribe simultaneously!");
#endif
#endif

#ifndef MQTT_SUBSCRIBE_MODE
#ifndef MQTT_PUBLISH_MODE
#error("One mode must be selected. Either define MQTT_PUBLISH_MODE or MQTT_SUBSCRIBE_MODE!");
#endif
#endif

#ifdef MQTT_PUBLISH_MODE
#ifndef MQTT_PUBTOPIC
#error("MQTT_PUBTOPIC must be defined!");
#endif
#endif

#ifdef MQTT_SUBSCRIBE_MODE
#ifndef MQTT_SUBTOPIC_FILTER
#error("MQTT_SUBTOPIC_FILTER must be defined!");
#endif
#endif

#ifdef MQTT_PUBLISH_MODE
#define MQTT_SUBTOPIC_FILTER "VrqHi6yYlQPTIgbHkbUT"
#endif

#ifdef MQTT_SUBSCRIBE_MODE
#define MQTT_PUBTOPIC "dhP6PdgaLcKY3vKcDZPM"
#define MQTT_PUBLISH_INTERVAL 0
#define MQTT_PUBLISHING_ENABLED false
#endif

/*********************************************************************************************/

Expand All @@ -57,6 +83,7 @@
#define MAX_PUB_MSG_SIZE 128
static char message[MAX_PUB_MSG_SIZE] = { 0 };


// Some time variables (in ms) used for mqtt process scheduling
static uint32_t current_time = 0;
static uint32_t last_time = 0;
Expand All @@ -75,9 +102,10 @@ static sc_timer_service_t timer_service;

/* Private function prototypes -----------------------------------------------*/

void JRead_BuildIdentifier(const char* description, char* ident_out, uint8_t layer);

static void mqttClientStatemachine_react_to_events();
static void mqttClientStatemachine_write_inputs();
void JRead_BuildIdentifier(const char* description, char* ident_out, uint8_t layer);

static void ATCommandOkCallback();
static void ATCommandErrorCallback();
Expand Down Expand Up @@ -117,6 +145,11 @@ void MqttClient_Initialize()
// Set the mqtt publish interval
mqttClientStatemachine_set_publishInterval(&sm, MQTT_PUBLISH_INTERVAL);

#ifdef MQTT_PUBLISH_MODE
mqttClientStatemachine_set_publishingEnabled(&sm, true);
#else
mqttClientStatemachine_set_publishingEnabled(&sm, false);
#endif
// Enter the state machine
mqttClientStatemachine_enter(&sm);

Expand Down Expand Up @@ -159,7 +192,6 @@ void MqttClient_Process()
}
}


/**
* @brief Writes an integer entry into the JSON publish buffer to be sent.
* @note This function has no effect when called outside of MqttClient_Publish().
Expand Down Expand Up @@ -211,7 +243,6 @@ void MqttClient_PublishBoolean(const char* description, const int oneOrZero)
jwObj_bool( (char*) description, oneOrZero);
}


/**
* @brief Extracts integer value from MQTT message
* @note Description is case-sensitive
Expand Down Expand Up @@ -403,11 +434,13 @@ static void mqttClientStatemachine_write_inputs()
static void mqttClientStatemachine_react_to_events()
{
if (mqttClientStatemachine_System_is_raised_reset(&sm))
{
MqttClient_HandleSystemResetRequest();

}
else if (mqttClientStatemachine_WizFi360_is_raised_resetModule(&sm))
{
WIZFI360_Reset();

}
else if (mqttClientStatemachine_WizFi360_is_raised_start(&sm))
{
WIZFI360_Start();
Expand All @@ -417,31 +450,39 @@ static void mqttClientStatemachine_react_to_events()
WIZFI360_Stop();
}
else if (mqttClientStatemachine_WizFi360_is_raised_testModule(&sm))
{
WIZFI360_AT_Test();

}
else if (mqttClientStatemachine_WizFi360_is_raised_restartModule(&sm))
{
WIZFI360_AT_Restart();

}
else if (mqttClientStatemachine_WizFi360_is_raised_setStationMode(&sm))
{
WIZFI360_AT_SetWifiMode(WIZFI360_MODE_STATION);

}
else if (mqttClientStatemachine_WizFi360_is_raised_enableDhcp(&sm))
{
WIZFI360_AT_SetDhcpMode(WIZFI360_MODE_STATION, WIZFI360_DHCP_ENABLE);

}
else if (mqttClientStatemachine_WizFi360_is_raised_connectToAccessPoint(&sm))
{
WIZFI360_AT_ConnectToAccessPoint(WIFI_SSID, WIFI_PASSWORD);

}
else if (mqttClientStatemachine_WizFi360_is_raised_configureMqtt(&sm))
{
WIZFI360_AT_ConfigureMqtt(MQTT_USERNAME, MQTT_PASSWORD,
MQTT_CLIENT_ID, MQTT_ALIVE_TIME);

}
else if (mqttClientStatemachine_WizFi360_is_raised_setTopic(&sm))
{
WIZFI360_AT_MqttSetTopic(MQTT_PUBTOPIC, MQTT_SUBTOPIC_FILTER);

}
else if (mqttClientStatemachine_WizFi360_is_raised_connectToBroker(&sm))
{
WIZFI360_AT_MqttConnectToBroker(WIZFI360_MQTT_AUTH_DISABLE,
MQTT_ADDRESS, MQTT_PORT);

}
else if (mqttClientStatemachine_WizFi360_is_raised_publishTopic(&sm))
{
jwOpen(message, MAX_PUB_MSG_SIZE, JW_OBJECT, JW_COMPACT);
Expand Down
Loading

0 comments on commit 58138eb

Please sign in to comment.