Skip to content

Commit 8634963

Browse files
committed
Med fixes
1 parent 906e574 commit 8634963

File tree

5 files changed

+102
-90
lines changed

5 files changed

+102
-90
lines changed

examples/mqttnet.c

+6-4
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,8 @@ static int NetConnect(void *context, const char* host, word16 port,
528528
#ifdef WOLFMQTT_NONBLOCK
529529
{
530530
/* Check for error */
531-
GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR, so_error);
531+
(void)GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR,
532+
so_error);
532533
}
533534
if (
534535
#ifndef _WIN32
@@ -704,14 +705,15 @@ static int NetWrite(void *context, const byte* buf, int buf_len,
704705
#ifndef WOLFMQTT_NO_TIMEOUT
705706
/* Setup timeout */
706707
setup_timeout(&tv, timeout_ms);
707-
setsockopt(sock->fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv, sizeof(tv));
708+
(void)setsockopt(sock->fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,
709+
sizeof(tv));
708710
#endif
709711

710712
rc = (int)SOCK_SEND(sock->fd, buf, buf_len, 0);
711713
if (rc == -1) {
712714
{
713715
/* Get error */
714-
GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR, so_error);
716+
(void)GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR, so_error);
715717
}
716718
if (so_error == 0) {
717719
#if defined(USE_WINDOWS_API) && defined(WOLFMQTT_NONBLOCK)
@@ -906,7 +908,7 @@ static int NetRead_ex(void *context, byte* buf, int buf_len,
906908
else if (rc < 0) {
907909
{
908910
/* Get error */
909-
GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR, so_error);
911+
(void)GET_SOCK_ERROR(sock->fd, SOL_SOCKET, SO_ERROR, so_error);
910912
}
911913
if (so_error == 0) {
912914
rc = 0; /* Handle signal */

examples/multithread/multithread.c

+61-56
Original file line numberDiff line numberDiff line change
@@ -82,27 +82,30 @@ static MQTTCtx gMqttCtx;
8282

8383
static word16 mqtt_get_packetid_threadsafe(void)
8484
{
85-
word16 packet_id;
86-
wm_SemLock(&mtLock);
87-
packet_id = mqtt_get_packetid();
88-
wm_SemUnlock(&mtLock);
85+
word16 packet_id = 0;
86+
if (wm_SemLock(&mtLock) == 0) {
87+
packet_id = mqtt_get_packetid();
88+
wm_SemUnlock(&mtLock);
89+
}
8990
return packet_id;
9091
}
9192

9293
static void mqtt_stop_set(void)
9394
{
94-
wm_SemLock(&mtLock);
95-
PRINTF("MQTT Stopping");
96-
mStopRead = 1;
97-
wm_SemUnlock(&mtLock);
95+
if (wm_SemLock(&mtLock) == 0) {
96+
PRINTF("MQTT Stopping");
97+
mStopRead = 1;
98+
wm_SemUnlock(&mtLock);
99+
}
98100
}
99101

100102
static int mqtt_stop_get(void)
101103
{
102-
int rc;
103-
wm_SemLock(&mtLock);
104-
rc = mStopRead;
105-
wm_SemUnlock(&mtLock);
104+
int rc = 0;
105+
if (wm_SemLock(&mtLock) == 0) {
106+
rc = mStopRead;
107+
wm_SemUnlock(&mtLock);
108+
}
106109
return rc;
107110
}
108111

@@ -164,47 +167,48 @@ static int mqtt_message_cb(MqttClient *client, MqttMessage *msg,
164167
MQTTCtx* mqttCtx = (MQTTCtx*)client->ctx;
165168
(void)mqttCtx;
166169

167-
wm_SemLock(&mtLock);
168-
if (msg_new) {
169-
/* Determine min size to dump */
170-
len = msg->topic_name_len;
170+
if (wm_SemLock(&mtLock) == 0) {
171+
if (msg_new) {
172+
/* Determine min size to dump */
173+
len = msg->topic_name_len;
174+
if (len > PRINT_BUFFER_SIZE) {
175+
len = PRINT_BUFFER_SIZE;
176+
}
177+
XMEMCPY(buf, msg->topic_name, len);
178+
buf[len] = '\0'; /* Make sure its null terminated */
179+
180+
/* Print incoming message */
181+
PRINTF("MQTT Message: Topic %s, Qos %d, Id %d, Len %u, %u, %u",
182+
buf, msg->qos, msg->packet_id, msg->total_len, msg->buffer_len,
183+
msg->buffer_pos);
184+
}
185+
186+
/* Print message payload */
187+
len = msg->buffer_len;
171188
if (len > PRINT_BUFFER_SIZE) {
172189
len = PRINT_BUFFER_SIZE;
173190
}
174-
XMEMCPY(buf, msg->topic_name, len);
191+
XMEMCPY(buf, msg->buffer, len);
175192
buf[len] = '\0'; /* Make sure its null terminated */
193+
PRINTF("Payload (%d - %d) printing %d bytes:" LINE_END "%s",
194+
msg->buffer_pos, msg->buffer_pos + msg->buffer_len, len, buf);
176195

177-
/* Print incoming message */
178-
PRINTF("MQTT Message: Topic %s, Qos %d, Id %d, Len %u, %u, %u",
179-
buf, msg->qos, msg->packet_id, msg->total_len, msg->buffer_len, msg->buffer_pos);
180-
}
181-
182-
/* Print message payload */
183-
len = msg->buffer_len;
184-
if (len > PRINT_BUFFER_SIZE) {
185-
len = PRINT_BUFFER_SIZE;
186-
}
187-
XMEMCPY(buf, msg->buffer, len);
188-
buf[len] = '\0'; /* Make sure its null terminated */
189-
PRINTF("Payload (%d - %d) printing %d bytes:" LINE_END "%s",
190-
msg->buffer_pos, msg->buffer_pos + msg->buffer_len, len, buf);
191-
192-
if (msg_done) {
193-
/* for test mode: count the number of messages received */
194-
if (mqttCtx->test_mode) {
195-
if (msg->buffer_pos + msg->buffer_len ==
196-
(word32)sizeof(mTestMessage) &&
197-
XMEMCMP(&mTestMessage[msg->buffer_pos], msg->buffer,
198-
msg->buffer_len) == 0)
199-
{
200-
mNumMsgsRecvd++;
196+
if (msg_done) {
197+
/* for test mode: count the number of messages received */
198+
if (mqttCtx->test_mode) {
199+
if (msg->buffer_pos + msg->buffer_len ==
200+
(word32)sizeof(mTestMessage) &&
201+
XMEMCMP(&mTestMessage[msg->buffer_pos], msg->buffer,
202+
msg->buffer_len) == 0)
203+
{
204+
mNumMsgsRecvd++;
205+
}
201206
}
202-
}
203207

204-
PRINTF("MQTT Message: Done");
208+
PRINTF("MQTT Message: Done");
209+
}
210+
wm_SemUnlock(&mtLock);
205211
}
206-
wm_SemUnlock(&mtLock);
207-
208212
/* Return negative to terminate publish processing */
209213
return MQTT_CODE_SUCCESS;
210214
}
@@ -469,19 +473,20 @@ static int TestIsDone(int rc, MQTTCtx* mqttCtx)
469473
{
470474
int isDone = 0;
471475
/* check if we are in test mode and done */
472-
wm_SemLock(&mtLock);
473-
if ((rc == 0 || rc == MQTT_CODE_CONTINUE) && mqttCtx->test_mode &&
474-
mNumMsgsDone == (NUM_PUB_TASKS * NUM_PUB_PER_TASK) &&
475-
mNumMsgsRecvd == (NUM_PUB_TASKS * NUM_PUB_PER_TASK)
476-
#ifdef WOLFMQTT_NONBLOCK
477-
&& !MqttClient_IsMessageActive(&mqttCtx->client, NULL)
478-
#endif
479-
) {
476+
if (wm_SemLock(&mtLock) == 0) {
477+
if ((rc == 0 || rc == MQTT_CODE_CONTINUE) && mqttCtx->test_mode &&
478+
mNumMsgsDone == (NUM_PUB_TASKS * NUM_PUB_PER_TASK) &&
479+
mNumMsgsRecvd == (NUM_PUB_TASKS * NUM_PUB_PER_TASK)
480+
#ifdef WOLFMQTT_NONBLOCK
481+
&& !MqttClient_IsMessageActive(&mqttCtx->client, NULL)
482+
#endif
483+
) {
484+
wm_SemUnlock(&mtLock);
485+
mqtt_stop_set();
486+
isDone = 1; /* done */
487+
}
480488
wm_SemUnlock(&mtLock);
481-
mqtt_stop_set();
482-
isDone = 1; /* done */
483489
}
484-
wm_SemUnlock(&mtLock);
485490
return isDone;
486491
}
487492

examples/sn-client/sn-client_qos-1.c

-5
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ int sn_testQoSn1(MQTTCtx *mqttCtx)
111111
}
112112
}
113113

114-
/* Check for error */
115-
if (rc != MQTT_CODE_SUCCESS) {
116-
goto disconn;
117-
}
118-
119114
disconn:
120115

121116
rc = MqttClient_NetDisconnect(&mqttCtx->client);

examples/sn-client/sn-multithread.c

+8-5
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,11 @@ static MQTTCtx gMqttCtx;
7979

8080
static word16 mqtt_get_packetid_threadsafe(void)
8181
{
82-
word16 packet_id;
83-
wm_SemLock(&packetIdLock);
84-
packet_id = mqtt_get_packetid();
85-
wm_SemUnlock(&packetIdLock);
82+
word16 packet_id = 0;
83+
if (wm_SemLock(&packetIdLock) == 0) {
84+
packet_id = mqtt_get_packetid();
85+
wm_SemUnlock(&packetIdLock);
86+
}
8687
return packet_id;
8788
}
8889

@@ -243,7 +244,9 @@ static int multithread_test_init(MQTTCtx *mqttCtx)
243244
wm_SemFree(&packetIdLock);
244245
client_exit(mqttCtx);
245246
}
246-
wm_SemLock(&pingSignal); /* default to locked */
247+
if (wm_SemLock(&pingSignal) != 0) { /* default to locked */
248+
client_exit(mqttCtx);
249+
}
247250

248251
PRINTF("MQTT-SN Client: QoS %d, Use TLS %d", mqttCtx->qos,
249252
mqttCtx->use_tls);

src/mqtt_client.c

+27-20
Original file line numberDiff line numberDiff line change
@@ -2844,6 +2844,7 @@ int MqttClient_NetDisconnect(MqttClient *client)
28442844
{
28452845
#ifdef WOLFMQTT_MULTITHREAD
28462846
MqttPendResp *tmpResp;
2847+
int rc;
28472848
#endif
28482849

28492850
if (client == NULL) {
@@ -2852,24 +2853,28 @@ int MqttClient_NetDisconnect(MqttClient *client)
28522853

28532854
#ifdef WOLFMQTT_MULTITHREAD
28542855
/* Get client lock on to ensure no other threads are active */
2855-
wm_SemLock(&client->lockClient);
2856-
2857-
#ifdef WOLFMQTT_DEBUG_CLIENT
2858-
PRINTF("Net Disconnect: Removing pending responses");
2859-
#endif
2860-
for (tmpResp = client->firstPendResp;
2861-
tmpResp != NULL;
2862-
tmpResp = tmpResp->next) {
2856+
rc = wm_SemLock(&client->lockClient);
2857+
if (rc == 0) {
28632858
#ifdef WOLFMQTT_DEBUG_CLIENT
2864-
PRINTF("\tPendResp: %p (obj %p), Type %s (%d), ID %d, InProc %d, Done %d",
2865-
tmpResp, tmpResp->packet_obj,
2866-
MqttPacket_TypeDesc(tmpResp->packet_type),
2867-
tmpResp->packet_type, tmpResp->packet_id,
2868-
tmpResp->packetProcessing, tmpResp->packetDone);
2859+
PRINTF("Net Disconnect: Removing pending responses");
28692860
#endif
2870-
MqttClient_RespList_Remove(client, tmpResp);
2861+
for (tmpResp = client->firstPendResp;
2862+
tmpResp != NULL;
2863+
tmpResp = tmpResp->next) {
2864+
#ifdef WOLFMQTT_DEBUG_CLIENT
2865+
PRINTF("\tPendResp: %p (obj %p), Type %s (%d), ID %d, InProc %d, Done %d",
2866+
tmpResp, tmpResp->packet_obj,
2867+
MqttPacket_TypeDesc(tmpResp->packet_type),
2868+
tmpResp->packet_type, tmpResp->packet_id,
2869+
tmpResp->packetProcessing, tmpResp->packetDone);
2870+
#endif
2871+
MqttClient_RespList_Remove(client, tmpResp);
2872+
}
2873+
wm_SemUnlock(&client->lockClient);
2874+
}
2875+
else {
2876+
return rc;
28712877
}
2872-
wm_SemUnlock(&client->lockClient);
28732878
#endif
28742879

28752880
return MqttSocket_Disconnect(client);
@@ -3029,14 +3034,16 @@ word32 MqttClient_Flags(MqttClient *client, word32 mask, word32 flags)
30293034
if (client != NULL) {
30303035
#ifdef WOLFMQTT_MULTITHREAD
30313036
/* Get client lock on to ensure no other threads are active */
3032-
wm_SemLock(&client->lockClient);
3037+
if (wm_SemLock(&client->lockClient) == 0)
30333038
#endif
3034-
client->flags &= ~mask;
3035-
client->flags |= flags;
3036-
ret = client->flags;
3039+
{
3040+
client->flags &= ~mask;
3041+
client->flags |= flags;
3042+
ret = client->flags;
30373043
#ifdef WOLFMQTT_MULTITHREAD
3038-
wm_SemUnlock(&client->lockClient);
3044+
wm_SemUnlock(&client->lockClient);
30393045
#endif
3046+
}
30403047
}
30413048
return ret;
30423049
}

0 commit comments

Comments
 (0)