Commit bccc6e9d authored by Sjoerd Crooijmans's avatar Sjoerd Crooijmans
Browse files

Merge branch 'improved-code-style' into 'master'

Improved code style

See merge request clean-and-itasks/contrib/mqttclient!7
parents f6f1f763 97633821
Pipeline #46358 passed with stages
in 2 minutes and 41 seconds
......@@ -10,5 +10,12 @@ Clean System Files
*.prj
*.prp
# Object files
*.o
*.obj
# System specific files
src/_MQTT.*
# Other
.clang-format
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <errno.h>
#ifdef _WIN32
#include <winsock2.h>
#else
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/socket.h>
#endif
#include "wolfmqtt/mqtt_client.h"
#include "Clean.h"
#include "wolfmqtt/mqtt_client.h"
#define BUFFER_SIZE 2048
......@@ -22,12 +22,12 @@ typedef struct Msg {
byte *topic_buf;
int payload_len;
byte *payload_buf;
struct Msg* next;
struct Msg *next;
} Msg;
typedef struct MqttCtx {
Msg* queue_head;
Msg* queue_tail;
Msg *queue_head;
Msg *queue_tail;
uint16_t packetId;
int fd;
} MqttCtx;
......@@ -40,31 +40,32 @@ int rc = 0;
#define MQTT_CODE_AUTH_FAILED -702
#define MQTT_CODE_QUEUE_EMPTY -800
static int mqtt_message_cb(MqttClient *client, MqttMessage *mqqt_msg, byte msg_new, byte msg_done)
static int mqtt_message_cb(MqttClient *client, MqttMessage *mqqt_msg,
byte msg_new, byte msg_done)
{
MqttCtx* ctx = (MqttCtx*) client->ctx;
MqttCtx *ctx = (MqttCtx *)client->ctx;
if (msg_new) {
Msg* msg = malloc(sizeof(Msg));
Msg *msg = malloc(sizeof(Msg));
if (msg == NULL) {
return MQTT_CODE_ERROR_MEMORY;
}
/* Topic */
byte* topic_buf = malloc(mqqt_msg->topic_name_len);
byte *topic_buf = malloc(mqqt_msg->topic_name_len);
if (topic_buf == NULL) {
return MQTT_CODE_ERROR_MEMORY;
}
memcpy(topic_buf, mqqt_msg->topic_name, mqqt_msg->topic_name_len);
memcpy(topic_buf, mqqt_msg->topic_name,
mqqt_msg->topic_name_len);
msg->topic_len = mqqt_msg->topic_name_len;
msg->topic_buf = topic_buf;
/* Payload */
byte* payload_buf = malloc(mqqt_msg->total_len);
byte *payload_buf = malloc(mqqt_msg->total_len);
if (payload_buf == NULL) {
return MQTT_CODE_ERROR_MEMORY;
......@@ -85,11 +86,12 @@ static int mqtt_message_cb(MqttClient *client, MqttMessage *mqqt_msg, byte msg_n
ctx->queue_head = msg;
if(ctx->queue_tail == NULL) {
if (ctx->queue_tail == NULL) {
ctx->queue_tail = msg;
}
}else {
memcpy(ctx->queue_head->payload_buf + mqqt_msg->buffer_pos, mqqt_msg->buffer, mqqt_msg->buffer_len);
} else {
memcpy(ctx->queue_head->payload_buf + mqqt_msg->buffer_pos,
mqqt_msg->buffer, mqqt_msg->buffer_len);
}
if (msg_done) {
......@@ -99,25 +101,22 @@ static int mqtt_message_cb(MqttClient *client, MqttMessage *mqqt_msg, byte msg_n
return MQTT_CODE_SUCCESS;
}
static int read_cb(void *context, byte* buf, int buf_len, int timeout_ms)
static int read_cb(void *context, byte *buf, int buf_len, int timeout_ms)
{
int rc;
MqttCtx *ctx = (MqttCtx*) context;
MqttCtx *ctx = (MqttCtx *)context;
fd_set set;
int b = 0;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = timeout_ms * 1000;
FD_ZERO (&set);
FD_SET (ctx->fd, &set);
FD_ZERO(&set);
FD_SET(ctx->fd, &set);
while (b < buf_len) {
/* Wait till there is data to read */
if (select(FD_SETSIZE, &set, NULL, NULL, &tv) == 0) {
if (b == 0) {
return MQTT_CODE_ERROR_TIMEOUT;
}
......@@ -136,7 +135,7 @@ static int read_cb(void *context, byte* buf, int buf_len, int timeout_ms)
if (rc < 0) { // Error occured
/* continue if it is non-blocking error */
if (errno == EAGAIN || errno == EWOULDBLOCK){
if (errno == EAGAIN || errno == EWOULDBLOCK) {
continue;
}
#endif
......@@ -149,9 +148,9 @@ static int read_cb(void *context, byte* buf, int buf_len, int timeout_ms)
return b;
}
static int write_cb(void *context, const byte* buf, int buf_len, int timeout_ms)
static int write_cb(void *context, const byte *buf, int buf_len, int timeout_ms)
{
MqttCtx *ctx = (MqttCtx*) context;
MqttCtx *ctx = (MqttCtx *)context;
int totalSend = 0;
while (totalSend < buf_len) {
......@@ -167,7 +166,8 @@ static int write_cb(void *context, const byte* buf, int buf_len, int timeout_ms)
return rc;
}
static int connect_cb(void *context, const char* host, word16 port, int timeout_ms)
static int connect_cb(void *context, const char *host, word16 port,
int timeout_ms)
{
/* Connection is made in Clean */
return MQTT_CODE_SUCCESS;
......@@ -179,21 +179,11 @@ static int disconnect_cb(void *context)
return MQTT_CODE_SUCCESS;
}
MqttClient* mqtt_connect(
int fd,
CleanCharArray client_id,
int keep_alive,
int clean_session,
CleanCharArray username,
CleanCharArray password,
CleanCharArray lw_topic,
CleanCharArray lw_msg,
int qos,
int retain,
int *ret
)
MqttClient *mqtt_connect(int fd, CleanCharArray client_id, int keep_alive,
int clean_session, CleanCharArray username,
CleanCharArray password, CleanCharArray lw_topic,
CleanCharArray lw_msg, int qos, int retain, int *ret)
{
MqttCtx *ctx = calloc(1, sizeof(MqttCtx));
if (ctx == NULL) {
......@@ -219,8 +209,8 @@ MqttClient* mqtt_connect(
byte *txBuf = NULL;
byte *rxBuf = NULL;
txBuf = (byte*) malloc(BUFFER_SIZE);
rxBuf = (byte*) malloc(BUFFER_SIZE);
txBuf = (byte *)malloc(BUFFER_SIZE);
rxBuf = (byte *)malloc(BUFFER_SIZE);
if (txBuf == NULL || rxBuf == NULL) {
*ret = MQTT_CODE_ERROR_MEMORY;
......@@ -235,8 +225,8 @@ MqttClient* mqtt_connect(
return NULL;
}
*ret = MqttClient_Init(mqttClient, net, mqtt_message_cb, txBuf, BUFFER_SIZE,
rxBuf, BUFFER_SIZE, 1000);
*ret = MqttClient_Init(mqttClient, net, mqtt_message_cb, txBuf,
BUFFER_SIZE, rxBuf, BUFFER_SIZE, 1000);
if (*ret != MQTT_CODE_SUCCESS) {
return NULL;
......@@ -248,12 +238,12 @@ MqttClient* mqtt_connect(
MqttConnect connect;
memset(&connect, 0, sizeof(connect));
connect.keep_alive_sec = keep_alive;
connect.client_id = (char*) client_id;
connect.client_id = (char *)client_id;
connect.clean_session = clean_session;
connect.username = (char*) username;
connect.password = (char*) password;
connect.username = (char *)username;
connect.password = (char *)password;
if (strlen((char*) lw_topic) > 0) {
if (strlen((char *)lw_topic) > 0) {
MqttMessage lwt_msg;
memset(&lwt_msg, 0, sizeof(lwt_msg));
......@@ -262,9 +252,9 @@ MqttClient* mqtt_connect(
lwt_msg.qos = qos;
lwt_msg.retain = retain;
lwt_msg.topic_name = (char*) lw_topic;
lwt_msg.buffer = (byte*) lw_msg;
lwt_msg.total_len = (word16)strlen((char*) lw_msg);
lwt_msg.topic_name = (char *)lw_topic;
lwt_msg.buffer = (byte *)lw_msg;
lwt_msg.total_len = (word16)strlen((char *)lw_msg);
}
*ret = MqttClient_Connect(mqttClient, &connect);
......@@ -296,23 +286,18 @@ MqttClient* mqtt_connect(
return mqttClient;
}
MqttClient* mqtt_connect_without_auth (
int fd,
CleanCharArray client_id,
int keep_alive,
int clean_session,
MqttClient *mqtt_connect_without_auth(int fd, CleanCharArray client_id,
int keep_alive, int clean_session,
CleanCharArray lw_topic,
CleanCharArray lw_msg,
int qos,
int retain,
int *ret
) {
return mqtt_connect(fd, client_id, keep_alive, clean_session, NULL, NULL, lw_topic, lw_msg, qos, retain, ret);
CleanCharArray lw_msg, int qos,
int retain, int *ret)
{
return mqtt_connect(fd, client_id, keep_alive, clean_session, NULL,
NULL, lw_topic, lw_msg, qos, retain, ret);
}
int mqtt_disconnect(MqttClient* mqttClient)
int mqtt_disconnect(MqttClient *mqttClient)
{
rc = MqttClient_Disconnect(mqttClient);
if (rc != 0) {
return rc;
......@@ -322,10 +307,10 @@ int mqtt_disconnect(MqttClient* mqttClient)
free(mqttClient->tx_buf);
free(mqttClient->rx_buf);
MqttCtx* ctx = (MqttCtx*) mqttClient->ctx;
Msg* msg = ctx->queue_tail;
MqttCtx *ctx = (MqttCtx *)mqttClient->ctx;
Msg *msg = ctx->queue_tail;
while (msg != NULL) {
Msg* tmp = msg;
Msg *tmp = msg;
msg = msg->next;
free(msg->topic_buf);
free(msg->payload_buf);
......@@ -341,22 +326,23 @@ int mqtt_disconnect(MqttClient* mqttClient)
return rc;
}
int mqtt_ping(MqttClient* mqttClient)
int mqtt_ping(MqttClient *mqttClient)
{
return MqttClient_Ping(mqttClient);
}
int mqtt_publish(MqttClient* mqttClient, CleanCharArray topic, CleanCharArray msg, int msgLen, int qos, int retain)
int mqtt_publish(MqttClient *mqttClient, CleanCharArray topic,
CleanCharArray msg, int msgLen, int qos, int retain)
{
MqttCtx* ctx = (MqttCtx*) mqttClient->ctx;
MqttCtx *ctx = (MqttCtx *)mqttClient->ctx;
MqttPublish publish;
memset(&publish, 0, sizeof(MqttPublish));
publish.qos = qos;
publish.retain = retain;
publish.topic_name = (char*) topic;
publish.topic_name = (char *)topic;
publish.packet_id = ++(ctx->packetId);
publish.buffer = (byte*) msg;
publish.buffer = (byte *)msg;
publish.total_len = msgLen;
rc = MqttClient_Publish(mqttClient, &publish);
......@@ -364,14 +350,14 @@ int mqtt_publish(MqttClient* mqttClient, CleanCharArray topic, CleanCharArray ms
return rc;
}
int mqtt_subscribe(MqttClient* mqttClient, CleanCharArray topicName, int qos)
int mqtt_subscribe(MqttClient *mqttClient, CleanCharArray topicName, int qos)
{
MqttCtx* ctx = (MqttCtx*) mqttClient->ctx;
MqttCtx *ctx = (MqttCtx *)mqttClient->ctx;
MqttSubscribe subscribe;
MqttTopic topics[1];
topics[0].topic_filter = (char*) topicName;
topics[0].topic_filter = (char *)topicName;
topics[0].qos = qos;
memset(&subscribe, 0, sizeof(subscribe));
......@@ -383,15 +369,14 @@ int mqtt_subscribe(MqttClient* mqttClient, CleanCharArray topicName, int qos)
return rc;
}
int mqtt_unsubscribe(MqttClient* mqttClient, CleanCharArray topicName)
int mqtt_unsubscribe(MqttClient *mqttClient, CleanCharArray topicName)
{
MqttCtx* ctx = (MqttCtx*) mqttClient->ctx;
MqttCtx *ctx = (MqttCtx *)mqttClient->ctx;
MqttUnsubscribe unsubscribe;
MqttTopic topics[1];
topics[0].topic_filter = (char*) topicName;
topics[0].topic_filter = (char *)topicName;
topics[0].qos = 1;
memset(&unsubscribe, 0, sizeof(unsubscribe));
......@@ -403,15 +388,16 @@ int mqtt_unsubscribe(MqttClient* mqttClient, CleanCharArray topicName)
return rc;
}
int mqtt_process(MqttClient* mqttClient)
int mqtt_process(MqttClient *mqttClient)
{
rc = MqttClient_WaitMessage(mqttClient, 1000);
return rc;
}
void mqtt_receive(MqttClient* mqttClient, int *ret, CleanString *topic, CleanString *payload, int *qos, int *retain)
void mqtt_receive(MqttClient *mqttClient, int *ret, CleanString *topic,
CleanString *payload, int *qos, int *retain)
{
MqttCtx* ctx = (MqttCtx*) mqttClient->ctx;
MqttCtx *ctx = (MqttCtx *)mqttClient->ctx;
if (ctx->queue_tail == NULL || !ctx->queue_tail->done) {
*ret = MQTT_CODE_QUEUE_EMPTY;
......@@ -419,8 +405,8 @@ void mqtt_receive(MqttClient* mqttClient, int *ret, CleanString *topic, CleanStr
size_t empty_topic = 0;
size_t empty_payload = 0;
*topic = (CleanString) &empty_topic;
*payload = (CleanString) &empty_payload;
*topic = (CleanString)&empty_topic;
*payload = (CleanString)&empty_payload;
*qos = 0;
*retain = 0;
return;
......@@ -429,20 +415,22 @@ void mqtt_receive(MqttClient* mqttClient, int *ret, CleanString *topic, CleanStr
CleanStringVariable(msg_topic, ctx->queue_tail->topic_len);
CleanStringLength(msg_topic) = ctx->queue_tail->topic_len;
char *topic_characters = CleanStringCharacters(msg_topic);
memcpy(topic_characters, ctx->queue_tail->topic_buf, ctx->queue_tail->topic_len);
*topic = (CleanString) msg_topic;
memcpy(topic_characters, ctx->queue_tail->topic_buf,
ctx->queue_tail->topic_len);
*topic = (CleanString)msg_topic;
CleanStringVariable(msg_payload, ctx->queue_tail->payload_len);
CleanStringLength(msg_payload) = ctx->queue_tail->payload_len;
char *payload_characters = CleanStringCharacters(msg_payload);
memcpy(payload_characters, ctx->queue_tail->payload_buf, ctx->queue_tail->payload_len);
*payload = (CleanString) msg_payload;
memcpy(payload_characters, ctx->queue_tail->payload_buf,
ctx->queue_tail->payload_len);
*payload = (CleanString)msg_payload;
*qos = ctx->queue_tail->qos;
*retain = ctx->queue_tail->retain;
*ret = 0;
Msg* tmp = ctx->queue_tail;
Msg *tmp = ctx->queue_tail;
ctx->queue_tail = ctx->queue_tail->next;
// Buffer is empty
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment