Skip to content

Commit

Permalink
mqtt
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Dec 17, 2019
1 parent e37fba4 commit 0845a61
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 35 deletions.
70 changes: 36 additions & 34 deletions src/eez/mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,6 @@ using namespace psu;

namespace mqtt {

#if defined(EEZ_PLATFORM_STM32)
static const char *CLIENT_ID = "BB3_STM32";
#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
static const char *CLIENT_ID = "BB3_Simulator";
#endif

static const uint32_t RECONNECT_AFTER_ERROR_MS = 1000;

static const size_t MAX_PUB_TOPIC_LENGTH = 50;
Expand Down Expand Up @@ -185,7 +177,7 @@ void onIncomingPublish(const char *topic, const char *payload) {

#if defined(EEZ_PLATFORM_STM32)
static ip_addr_t g_ipaddr;
static mqtt_client_t *g_client;
static mqtt_client_t g_client;
static const size_t MAX_TOPIC_LEN = 128;
static char g_topic[MAX_TOPIC_LEN + 1];
static const size_t MAX_PAYLOAD_LEN = 128;
Expand Down Expand Up @@ -253,7 +245,7 @@ void incomingPublishCallback(void** unused, struct mqtt_response_publish *publis

bool publish(char *topic, char *payload, bool retain) {
#if defined(EEZ_PLATFORM_STM32)
err_t result = mqtt_publish(g_client, topic, payload, strlen(payload), 0, retain ? 1 : 0, requestCallback, nullptr);
err_t result = mqtt_publish(&g_client, topic, payload, strlen(payload), 0, retain ? 1 : 0, requestCallback, nullptr);
if (result != ERR_OK) {
if (result != ERR_MEM) {
DebugTrace("mqtt publish error: %d\n", (int)result);
Expand Down Expand Up @@ -304,11 +296,28 @@ const char *getSubTopic() {
return g_subTopic;
}

const char *getClientId() {
static char g_clientId[50 + 1] = { 0 };

if (!g_clientId[0]) {
#if defined(EEZ_PLATFORM_STM32)
sprintf(g_clientId, "BB3_STM32_%s", getSerialNumber());
#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
static const char *CLIENT_ID = "BB3_Simulator";
sprintf(g_clientId, "BB3_Simulator_%s", getSerialNumber());
#endif
}

return g_clientId;
}

void setState(ConnectionState connectionState) {
if (connectionState == CONNECTION_STATE_CONNECTED) {
#if defined(EEZ_PLATFORM_STM32)
mqtt_set_inpub_callback(g_client, incomingPublishCallback, incomingDataCallback, nullptr);
mqtt_subscribe(g_client, getSubTopic(), 0, requestCallback, nullptr);
mqtt_set_inpub_callback(&g_client, incomingPublishCallback, incomingDataCallback, nullptr);
mqtt_subscribe(&g_client, getSubTopic(), 0, requestCallback, nullptr);
#endif

#if defined(EEZ_PLATFORM_SIMULATOR)
Expand Down Expand Up @@ -388,8 +397,8 @@ void tick(uint32_t tickCount) {

else if (g_connectionState == CONNECTION_STATE_DISCONNECT || g_connectionState == CONNECTION_STATE_RECONNECT) {
#if defined(EEZ_PLATFORM_STM32)
if (mqtt_client_is_connected(g_client)) {
mqtt_disconnect(g_client);
if (mqtt_client_is_connected(&g_client)) {
mqtt_disconnect(&g_client);
}
#endif

Expand All @@ -414,29 +423,22 @@ void tick(uint32_t tickCount) {

#if defined(EEZ_PLATFORM_STM32)
else if (g_connectionState == CONNECTION_STATE_DNS_FOUND) {
if (!g_client) {
g_client = mqtt_client_new();
}
if (g_client) {
mqtt_connect_client_info_t clientInfo;
clientInfo.client_id = CLIENT_ID;
clientInfo.client_user = persist_conf::devConf.mqttUsername;
clientInfo.client_pass = persist_conf::devConf.mqttPassword;
clientInfo.keep_alive = 60; // seconds
clientInfo.will_topic = nullptr; // not used

err_t result = mqtt_client_connect(g_client, &g_ipaddr, persist_conf::devConf.mqttPort, connectCallback, nullptr, &clientInfo);
if (result == ERR_OK) {
setState(CONNECTION_STATE_CONNECTING);
} else {
setState(CONNECTION_STATE_ERROR);
DebugTrace("mqtt connect error: %d\n", (int)result);
}
mqtt_connect_client_info_t clientInfo;
clientInfo.client_id = CLIENT_ID;
clientInfo.client_user = persist_conf::devConf.mqttUsername;
clientInfo.client_pass = persist_conf::devConf.mqttPassword;
clientInfo.keep_alive = 60; // seconds
clientInfo.will_topic = nullptr; // not used

memset(&g_client, 0, sizeof(g_client));

err_t result = mqtt_client_connect(&g_client, &g_ipaddr, persist_conf::devConf.mqttPort, connectCallback, nullptr, &clientInfo);
if (result == ERR_OK) {
setState(CONNECTION_STATE_CONNECTING);
} else {
setState(CONNECTION_STATE_ERROR);
DebugTrace("mqtt error: failed to create a client\n");
DebugTrace("mqtt connect error: %d\n", (int)result);
}
return;
}
#endif

Expand Down
4 changes: 4 additions & 0 deletions src/third_party/stm32_truestudio/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
</option>
<option id="com.atollic.truestudio.gcc.symbols.defined.1670122205" name="Defined symbols" superClass="com.atollic.truestudio.gcc.symbols.defined" useByScannerDiscovery="false" valueType="definedSymbols">
<listOptionValue builtIn="false" value="USE_HAL_DRIVER"/>
<listOptionValue builtIn="false" value="MQTT_OUTPUT_RINGBUF_SIZE=4096"/>
<listOptionValue builtIn="false" value="MQTT_REQ_MAX_IN_FLIGHT=32"/>
<listOptionValue builtIn="false" value="EEZ_BP3C_REVISION_R3B1=1"/>
<listOptionValue builtIn="false" value="OPTION_SDRAM=1"/>
<listOptionValue builtIn="false" value="LWIP_NETIF_HOSTNAME=1"/>
Expand Down Expand Up @@ -132,6 +134,8 @@
<tool id="com.atollic.truestudio.exe.debug.toolchain.gpp.981298185" name="C++ Compiler" superClass="com.atollic.truestudio.exe.debug.toolchain.gpp">
<option id="com.atollic.truestudio.gpp.symbols.defined.1619552487" name="Defined symbols" superClass="com.atollic.truestudio.gpp.symbols.defined" useByScannerDiscovery="false" valueType="stringList">
<listOptionValue builtIn="false" value="STM32F769xx"/>
<listOptionValue builtIn="false" value="MQTT_OUTPUT_RINGBUF_SIZE=4096"/>
<listOptionValue builtIn="false" value="MQTT_REQ_MAX_IN_FLIGHT=32"/>
<listOptionValue builtIn="false" value="EEZ_BP3C_REVISION_R3B1=1"/>
<listOptionValue builtIn="false" value="OPTION_SDRAM=1"/>
<listOptionValue builtIn="false" value="EEZ_MCU_REVISION_R1B5=1"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<extension point="org.eclipse.cdt.core.LanguageSettingsProvider">
<provider copy-of="extension" id="org.eclipse.cdt.ui.UserLanguageSettingsProvider"/>
<provider-reference id="org.eclipse.cdt.managedbuilder.core.MBSLanguageSettingsProvider" ref="shared-provider"/>
<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="-1714863121677792765" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<provider class="com.atollic.truestudio.mbs.GCCSpecsDetectorAtollicArm" console="false" env-hash="-549647885749173183" id="com.atollic.truestudio.mbs.provider" keep-relative-paths="false" name="Atollic ARM Tools Language Settings" parameter="${COMMAND} -E -P -v -dD &quot;${INPUTS}&quot;" prefer-non-shared="true">
<language-scope id="org.eclipse.cdt.core.gcc"/>
<language-scope id="org.eclipse.cdt.core.g++"/>
</provider>
Expand Down

0 comments on commit 0845a61

Please sign in to comment.