diff --git a/MQTTClient-C/samples/linux/build.sh b/MQTTClient-C/samples/linux/build.sh index 9ac19213..ce5d9e95 100755 --- a/MQTTClient-C/samples/linux/build.sh +++ b/MQTTClient-C/samples/linux/build.sh @@ -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 diff --git a/MQTTClient-C/samples/linux/stdoutsub.c b/MQTTClient-C/samples/linux/stdoutsub.c index 3521d95d..31f30971 100644 --- a/MQTTClient-C/samples/linux/stdoutsub.c +++ b/MQTTClient-C/samples/linux/stdoutsub.c @@ -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; diff --git a/MQTTClient-C/src/MQTTClient.c b/MQTTClient-C/src/MQTTClient.c index 84dd02e9..5350cf8a 100755 --- a/MQTTClient-C/src/MQTTClient.c +++ b/MQTTClient-C/src/MQTTClient.c @@ -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 @@ -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); } } @@ -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 @@ -303,7 +304,7 @@ int MQTTYield(MQTTClient* c, int timeout_ms) break; } } while (!TimerIsExpired(timer)); - + destroyTimer(timer); return rc; } @@ -313,7 +314,7 @@ void MQTTRun(void* parm) void *timer; MQTTClient* c = (MQTTClient*)parm; - TimerInit(timer); + timer = TimerInit(); while (1) { @@ -326,6 +327,7 @@ void MQTTRun(void* parm) MutexUnlock(&c->mutex); #endif } + destroyTimer(timer); } @@ -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) @@ -390,7 +392,8 @@ int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options) } else rc = FAILURE; - + + destroyTimer(connect_timer); exit: if (rc == SUCCESS) c->isconnected = 1; @@ -398,7 +401,6 @@ int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options) #if defined(MQTT_TASK) MutexUnlock(&c->mutex); #endif - return rc; } @@ -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); @@ -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); @@ -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) @@ -489,6 +492,7 @@ int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter) else rc = FAILURE; + destroyTimer(timer); exit: #if defined(MQTT_TASK) MutexUnlock(&c->mutex); @@ -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) @@ -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); @@ -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); @@ -575,6 +579,7 @@ int MQTTDisconnect(MQTTClient* c) c->isconnected = 0; + destroyTimer(timer); #if defined(MQTT_TASK) MutexUnlock(&c->mutex); #endif diff --git a/MQTTClient-C/src/Network.h b/MQTTClient-C/src/Network.h index 237ba209..35943a9c 100644 --- a/MQTTClient-C/src/Network.h +++ b/MQTTClient-C/src/Network.h @@ -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 diff --git a/MQTTClient-C/src/Timer.h b/MQTTClient-C/src/Timer.h index 764b679a..ca465edb 100644 --- a/MQTTClient-C/src/Timer.h +++ b/MQTTClient-C/src/Timer.h @@ -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); diff --git a/MQTTClient-C/src/linux/MQTTLinux.c b/MQTTClient-C/src/linux/MQTTLinux.c index 5ad17308..b279e196 100644 --- a/MQTTClient-C/src/linux/MQTTLinux.c +++ b/MQTTClient-C/src/linux/MQTTLinux.c @@ -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) @@ -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; }