From 69076011ae63b7292744c99dc9a6f103d588cd13 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Mon, 28 Aug 2023 07:31:03 +0300 Subject: [PATCH] Format code Signed-off-by: 1998-felix --- targets/stm32/mqtt/include/MQTTClientapp.h | 19 +++--- targets/stm32/mqtt/src/MQTTClientapp.c | 16 +++-- targets/stm32/mqtt/src/MQTTInterface.c | 75 ++++++++++++++-------- 3 files changed, 68 insertions(+), 42 deletions(-) diff --git a/targets/stm32/mqtt/include/MQTTClientapp.h b/targets/stm32/mqtt/include/MQTTClientapp.h index 71945c9..5d71636 100644 --- a/targets/stm32/mqtt/include/MQTTClientapp.h +++ b/targets/stm32/mqtt/include/MQTTClientapp.h @@ -1,27 +1,26 @@ - #ifndef MQTT_CLIENT_APP_H_ #define MQTT_CLIENT_APP_H_ +#include + #include "MQTTClient.h" #include "MQTTInterface.h" #include "cmsis_os.h" #include "config.h" -#include - -#define MQTT_PORT 1883 -#define MQTT_BUFSIZE 1024 +#define MQTT_PORT 1883 +#define MQTT_BUFSIZE 1024 -Network net; -MQTTClient mqttClient; +Network net; +MQTTClient mqttClient; -uint8_t sndBuffer[MQTT_BUFSIZE]; +uint8_t sndBuffer[MQTT_BUFSIZE]; uint8_t rcvBuffer[MQTT_BUFSIZE]; uint8_t msgBuffer[MQTT_BUFSIZE]; void mqttClientSubTask(void const *argument); void mqttClientPubTask(void const *argument); -int mqttConnectBroker(void); -void mqttMessageArrived(MessageData* msg); +int mqttConnectBroker(void); +void mqttMessageArrived(MessageData *msg); #endif diff --git a/targets/stm32/mqtt/src/MQTTClientapp.c b/targets/stm32/mqtt/src/MQTTClientapp.c index a1e84b8..616562e 100644 --- a/targets/stm32/mqtt/src/MQTTClientapp.c +++ b/targets/stm32/mqtt/src/MQTTClientapp.c @@ -1,6 +1,10 @@ #include "main.h" #include "MQTTClientapp.h" +#define MESSAGE_DELAY 1000 +#define OS_DELAY 100 +#define KEEP_ALIVE_INT 60 + void createMainfluxChannel(void) { const char *_preId = "channels/"; @@ -18,12 +22,12 @@ void mqttClientSubTask(void const *argument) { MQTTDisconnect(&mqttClient); mqttConnectBroker(); - osDelay(1000); + osDelay(MESSAGE_DELAY); } else { - MQTTYield(&mqttClient, 1000); - osDelay(100); + MQTTYield(&mqttClient, MESSAGE_DELAY); + osDelay(OS_DELAY); } } } @@ -42,7 +46,7 @@ void mqttClientPubTask(void const *argument) MQTTPublish(&mqttClient, mfTopic, &message); } - osDelay(1000); + osDelay(MESSAGE_DELAY); } } @@ -65,7 +69,7 @@ int mqttConnectBroker() return -1; } - MQTTClientInit(&mqttClient, &net, 1000, sndBuffer, sizeof(sndBuffer), rcvBuffer, sizeof(rcvBuffer)); + MQTTClientInit(&mqttClient, &net, MESSAGE_DELAY, sndBuffer, sizeof(sndBuffer), rcvBuffer, sizeof(rcvBuffer)); createMainfluxChannel(); MQTTPacket_connectData data = MQTTPacket_connectData_initializer; @@ -74,7 +78,7 @@ int mqttConnectBroker() data.clientID.cstring = "STM32F4"; data.username.cstring = mfThingId; data.password.cstring = mfThingPass; - data.keepAliveInterval = 60; + data.keepAliveInterval = KEEP_ALIVE_INT; data.cleansession = 1; ret = MQTTConnect(&mqttClient, &data); diff --git a/targets/stm32/mqtt/src/MQTTInterface.c b/targets/stm32/mqtt/src/MQTTInterface.c index 2ff714a..d1f2613 100644 --- a/targets/stm32/mqtt/src/MQTTInterface.c +++ b/targets/stm32/mqtt/src/MQTTInterface.c @@ -6,34 +6,39 @@ #include "stm32f4xx_hal.h" #include "MQTTInterface.h" - -#define MQTT_PORT 1883 +#define MQTT_PORT 1883 uint32_t MilliTimer; -char timerIsExpired(Timer *timer) { +char timerIsExpired(Timer *timer) +{ long left = timer->end_time - MilliTimer; return (left < 0); } -void timerCountDownMS(Timer *timer, unsigned int timeout) { +void timerCountDownMS(Timer *timer, unsigned int timeout) +{ timer->end_time = MilliTimer + timeout; } -void timerCountDown(Timer *timer, unsigned int timeout) { +void timerCountDown(Timer *timer, unsigned int timeout) +{ timer->end_time = MilliTimer + (timeout * 1000); } -int timerLeftMS(Timer *timer) { +int timerLeftMS(Timer *timer) +{ long left = timer->end_time - MilliTimer; return (left < 0) ? 0 : left; } -void initTimer(Timer *timer) { +void initTimer(Timer *timer) +{ timer->end_time = 0; } -void newNetwork(Network *n) { +void newNetwork(Network *n) +{ n->conn = NULL; n->buf = NULL; n->offset = 0; @@ -42,14 +47,17 @@ void newNetwork(Network *n) { n->disconnect = netDisconnect; } -int connectNetwork(Network *n, char *ip, int port) { +int connectNetwork(Network *n, char *ip, int port) +{ err_t err; n->conn = netconn_new(NETCONN_TCP); - if (n->conn != NULL) { + if (n->conn != NULL) + { err = netconn_connect(n->conn, &ip, port); - if (err != ERR_OK) { + if (err != ERR_OK) + { netconn_delete(n->conn); return -1; } @@ -58,36 +66,48 @@ int connectNetwork(Network *n, char *ip, int port) { return 0; } -int netRead(Network *n, unsigned char *buffer, int len, int timeout_ms) { +int netRead(Network *n, unsigned char *buffer, int len, int timeout_ms) +{ int rc; struct netbuf *inbuf; int offset = 0; int bytes = 0; - while(bytes < len) { - if(n->buf != NULL) { + while (bytes < len) + { + if (n->buf != NULL) + { inbuf = n->buf; offset = n->offset; rc = ERR_OK; - } else { + } + else + { rc = netconn_recv(n->conn, &inbuf); offset = 0; } - if(rc != ERR_OK) { - if(rc != ERR_TIMEOUT) { + if (rc != ERR_OK) + { + if (rc != ERR_TIMEOUT) + { bytes = -1; } break; - } else { + } + else + { int nblen = netbuf_len(inbuf) - offset; - if((bytes+nblen) > len) { - netbuf_copy_partial(inbuf, buffer+bytes, len-bytes,offset); + if ((bytes + nblen) > len) + { + netbuf_copy_partial(inbuf, buffer + bytes, len - bytes, offset); n->buf = inbuf; n->offset = offset + len - bytes; bytes = len; - } else { - netbuf_copy_partial(inbuf, buffer+bytes, nblen, offset); + } + else + { + netbuf_copy_partial(inbuf, buffer + bytes, nblen, offset); bytes += nblen; netbuf_delete(inbuf); n->buf = NULL; @@ -98,14 +118,17 @@ int netRead(Network *n, unsigned char *buffer, int len, int timeout_ms) { return bytes; } -int netWrite(Network *n, unsigned char *buffer, int len, int timeout_ms) { +int netWrite(Network *n, unsigned char *buffer, int len, int timeout_ms) +{ int rc = netconn_write(n->conn, buffer, len, NETCONN_NOCOPY); - if(rc != ERR_OK) return -1; + if (rc != ERR_OK) + return -1; return len; } -void netDisconnect(Network *n) { +void netDisconnect(Network *n) +{ netconn_close(n->conn); - netconn_delete(n->conn); + netconn_delete(n->conn); n->conn = NULL; } \ No newline at end of file