Skip to content

Commit

Permalink
Format code
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <felix.gateru@gmail.com>
  • Loading branch information
felixgateru committed Aug 28, 2023
1 parent ab2d352 commit 6907601
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 42 deletions.
19 changes: 9 additions & 10 deletions targets/stm32/mqtt/include/MQTTClientapp.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@

#ifndef MQTT_CLIENT_APP_H_
#define MQTT_CLIENT_APP_H_

#include <string.h>

#include "MQTTClient.h"
#include "MQTTInterface.h"
#include "cmsis_os.h"
#include "config.h"

#include <string.h>

#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
16 changes: 10 additions & 6 deletions targets/stm32/mqtt/src/MQTTClientapp.c
Original file line number Diff line number Diff line change
@@ -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/";
Expand All @@ -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);
}
}
}
Expand All @@ -42,7 +46,7 @@ void mqttClientPubTask(void const *argument)

MQTTPublish(&mqttClient, mfTopic, &message);
}
osDelay(1000);
osDelay(MESSAGE_DELAY);
}
}

Expand All @@ -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;
Expand All @@ -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);
Expand Down
75 changes: 49 additions & 26 deletions targets/stm32/mqtt/src/MQTTInterface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}

0 comments on commit 6907601

Please sign in to comment.