Skip to content

Commit

Permalink
Fixed the object creation
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Greblikas <[email protected]>
  • Loading branch information
Lukas Greblikas committed Jul 27, 2016
1 parent 9a9ed9b commit 8d1402a
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 29 deletions.
2 changes: 1 addition & 1 deletion MQTTClient-C/samples/linux/build.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
gcc stdoutsub.c -I ../../src -I ../../src/linux -I ../../../MQTTPacket/src ../../src/MQTTClient.c ../../src/linux/MQTTLinux.c ../../../MQTTPacket/src/MQTTFormat.c ../../../MQTTPacket/src/MQTTPacket.c ../../../MQTTPacket/src/MQTTDeserializePublish.c ../../../MQTTPacket/src/MQTTConnectClient.c ../../../MQTTPacket/src/MQTTSubscribeClient.c ../../../MQTTPacket/src/MQTTSerializePublish.c -o stdoutsub ../../../MQTTPacket/src/MQTTConnectServer.c ../../../MQTTPacket/src/MQTTSubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeClient.c
gcc stdoutsub.c -I ../../src/linux -I ../../src -I ../../../MQTTPacket/src ../../src/MQTTClient.c ../../src/linux/MQTTLinux.c ../../../MQTTPacket/src/MQTTFormat.c ../../../MQTTPacket/src/MQTTPacket.c ../../../MQTTPacket/src/MQTTDeserializePublish.c ../../../MQTTPacket/src/MQTTConnectClient.c ../../../MQTTPacket/src/MQTTSubscribeClient.c ../../../MQTTPacket/src/MQTTSerializePublish.c -o stdoutsub ../../../MQTTPacket/src/MQTTConnectServer.c ../../../MQTTPacket/src/MQTTSubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeClient.c
4 changes: 2 additions & 2 deletions MQTTClient-C/samples/linux/stdoutsub.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,8 @@ int main(int argc, char** argv)
signal(SIGINT, cfinish);
signal(SIGTERM, cfinish);

NewNetwork(n);
ConnectNetwork(n, opts.host, opts.port);
n = NetworkInit();
NetworkConnect(n, opts.host, opts.port);
MQTTClientInit(&c, n, 1000, buf, 100, readbuf, 100);

MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
Expand Down
33 changes: 19 additions & 14 deletions MQTTClient-C/src/MQTTClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void MQTTClientInit(MQTTClient* c, void* network, unsigned int command_timeout_m
c->ping_outstanding = 0;
c->defaultMessageHandler = NULL;
c->next_packetid = 1;
TimerInit(&c->ping_timer);
c->ping_timer = TimerInit();
#if defined(MQTT_TASK)
MutexInit(&c->mutex);
#endif
Expand Down Expand Up @@ -207,11 +207,12 @@ int keepalive(MQTTClient* c)
if (!c->ping_outstanding)
{
void *timer;
TimerInit(timer);
timer = TimerInit();
TimerCountdownMS(timer, 1000);
int len = MQTTSerialize_pingreq(c->buf, c->buf_size);
if (len > 0 && (rc = sendPacket(c, len, timer)) == SUCCESS) // send the ping packet
c->ping_outstanding = 1;
destroyTimer(timer);
}
}

Expand Down Expand Up @@ -292,7 +293,7 @@ int MQTTYield(MQTTClient* c, int timeout_ms)
int rc = SUCCESS;
void *timer;

TimerInit(timer);
timer = TimerInit();
TimerCountdownMS(timer, timeout_ms);

do
Expand All @@ -303,7 +304,7 @@ int MQTTYield(MQTTClient* c, int timeout_ms)
break;
}
} while (!TimerIsExpired(timer));
destroyTimer(timer);
return rc;
}

Expand All @@ -313,7 +314,7 @@ void MQTTRun(void* parm)
void *timer;
MQTTClient* c = (MQTTClient*)parm;

TimerInit(timer);
timer = TimerInit();

while (1)
{
Expand All @@ -326,6 +327,7 @@ void MQTTRun(void* parm)
MutexUnlock(&c->mutex);
#endif
}
destroyTimer(timer);
}


Expand Down Expand Up @@ -365,7 +367,7 @@ int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
if (c->isconnected) /* don't send connect packet again if we are already connected */
goto exit;

TimerInit(connect_timer);
connect_timer = TimerInit();
TimerCountdownMS(connect_timer, c->command_timeout_ms);

if (options == 0)
Expand All @@ -390,15 +392,15 @@ int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
}
else
rc = FAILURE;


destroyTimer(connect_timer);
exit:
if (rc == SUCCESS)
c->isconnected = 1;

#if defined(MQTT_TASK)
MutexUnlock(&c->mutex);
#endif

return rc;
}

Expand All @@ -417,7 +419,7 @@ int MQTTSubscribe(MQTTClient* c, const char* topicFilter, enum QoS qos, messageH
if (!c->isconnected)
goto exit;

TimerInit(timer);
timer = TimerInit();
TimerCountdownMS(timer, c->command_timeout_ms);

len = MQTTSerialize_subscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic, (int*)&qos);
Expand Down Expand Up @@ -449,7 +451,8 @@ int MQTTSubscribe(MQTTClient* c, const char* topicFilter, enum QoS qos, messageH
}
else
rc = FAILURE;


destroyTimer(timer);
exit:
#if defined(MQTT_TASK)
MutexUnlock(&c->mutex);
Expand All @@ -472,7 +475,7 @@ int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter)
if (!c->isconnected)
goto exit;

TimerInit(timer);
timer = TimerInit();
TimerCountdownMS(timer, c->command_timeout_ms);

if ((len = MQTTSerialize_unsubscribe(c->buf, c->buf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
Expand All @@ -489,6 +492,7 @@ int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter)
else
rc = FAILURE;

destroyTimer(timer);
exit:
#if defined(MQTT_TASK)
MutexUnlock(&c->mutex);
Expand All @@ -511,7 +515,7 @@ int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message)
if (!c->isconnected)
goto exit;

TimerInit(timer);
timer = TimerInit();
TimerCountdownMS(timer, c->command_timeout_ms);

if (message->qos == QOS1 || message->qos == QOS2)
Expand Down Expand Up @@ -548,7 +552,7 @@ int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message)
else
rc = FAILURE;
}

destroyTimer(timer);
exit:
#if defined(MQTT_TASK)
MutexUnlock(&c->mutex);
Expand All @@ -566,7 +570,7 @@ int MQTTDisconnect(MQTTClient* c)
#if defined(MQTT_TASK)
MutexLock(&c->mutex);
#endif
TimerInit(timer);
timer = TimerInit();
TimerCountdownMS(timer, c->command_timeout_ms);

len = MQTTSerialize_disconnect(c->buf, c->buf_size);
Expand All @@ -575,6 +579,7 @@ int MQTTDisconnect(MQTTClient* c)

c->isconnected = 0;

destroyTimer(timer);
#if defined(MQTT_TASK)
MutexUnlock(&c->mutex);
#endif
Expand Down
9 changes: 5 additions & 4 deletions MQTTClient-C/src/Network.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#if !defined(MQTT_CLIENT_C_NETWORK_H)
#ifndef MQTT_CLIENT_C_NETWORK_H
#define MQTT_CLIENT_C_NETWORK_H

int mqttread(void *network, unsigned char* read_buffer, int, int);
int mqttwrite(void *network, unsigned char* send_buffer, int, int);

DLLExport void NetworkInit(void *network);
DLLExport int NetworkConnect(void *network, char*, int);
DLLExport void NetworkDisconnect(void *network);
void *NetworkInit();
int NetworkConnect(void *network, char*, int);
void NetworkDisconnect(void *network);
void destroyNetwork(void *network);

#endif
4 changes: 2 additions & 2 deletions MQTTClient-C/src/Timer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#if !defined(MQTT_CLIENT_C_TIMER_H)
#ifndef MQTT_CLIENT_C_TIMER_H
#define MQTT_CLIENT_C_TIMER_H

void TimerInit(void *timer);
void *TimerInit();
char TimerIsExpired(void *timer);
void TimerCountdownMS(void *timer, unsigned int);
void TimerCountdown(void *timer, unsigned int);
Expand Down
12 changes: 6 additions & 6 deletions MQTTClient-C/src/linux/MQTTLinux.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

#include "MQTTLinux.h"

void TimerInit(void *t)
void *TimerInit()
{
Timer *timer = t;
timer = (Timer *)malloc(sizeof(Timer));
Timer *timer = (Timer *)malloc(sizeof(Timer));
timer->end_time = (struct timeval){0, 0};
return (void *)timer;
}

char TimerIsExpired(void *t)
Expand Down Expand Up @@ -114,11 +114,11 @@ int mqttwrite(void *network, unsigned char* buffer, int len, int timeout_ms)
}


void NetworkInit(void *network)
void *NetworkInit()
{
Network *n = (Network *)network;
n = (Network *)malloc(sizeof(Network));
Network *n = (Network *)malloc(sizeof(Network));
n->my_socket = 0;
return (void *)n;
}


Expand Down

0 comments on commit 8d1402a

Please sign in to comment.