void MQTTClient_yield(void) { START_TIME_TYPE start = MQTTClient_start_clock(); unsigned long elapsed = 0L; unsigned long timeout = 100L; int rc = 0; FUNC_ENTRY; if (running) { MQTTClient_sleep(timeout); goto exit; } elapsed = MQTTClient_elapsed(start); do { int sock = -1; MQTTClient_cycle(&sock, (timeout > elapsed) ? timeout - elapsed : 0L, &rc); if (rc == SOCKET_ERROR && ListFindItem(handles, &sock, clientSockCompare)) { MQTTClients* m = (MQTTClient)(handles->current->content); if (m->c->connect_state != -2) MQTTClient_disconnect_internal(m, 0); } elapsed = MQTTClient_elapsed(start); } while (elapsed < timeout); exit: FUNC_EXIT; }
void MQTTClient_stop() { int rc = 0; FUNC_ENTRY; if (running == 1 && tostop == 0) { int conn_count = 0; ListElement* current = NULL; if (handles != NULL) { /* find out how many handles are still connected */ while (ListNextElement(handles, ¤t)) { if (((MQTTClients*)(current->content))->c->connect_state > 0 || ((MQTTClients*)(current->content))->c->connected) ++conn_count; } } Log(TRACE_MIN, -1, "Conn_count is %d", conn_count); /* stop the background thread, if we are the last one to be using it */ if (conn_count == 0) { int count = 0; tostop = 1; if (Thread_getid() != run_id) { while (running && ++count < 100) { Thread_unlock_mutex(mqttclient_mutex); Log(TRACE_MIN, -1, "sleeping"); MQTTClient_sleep(100L); Thread_lock_mutex(mqttclient_mutex); } } rc = 1; } } FUNC_EXIT_RC(rc); }
int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* options, const char* serverURI, int MQTTVersion, START_TIME_TYPE start, long millisecsTimeout) { MQTTClients* m = handle; int rc = SOCKET_ERROR; int sessionPresent = 0; FUNC_ENTRY; if (m->ma && !running) { Thread_start(MQTTClient_run, handle); if (MQTTClient_elapsed(start) >= millisecsTimeout) { rc = SOCKET_ERROR; goto exit; } MQTTClient_sleep(100L); } Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, MQTTVersion); #if defined(OPENSSL) rc = MQTTProtocol_connect(serverURI, m->c, m->ssl, MQTTVersion); #else rc = MQTTProtocol_connect(serverURI, m->c, MQTTVersion); #endif if (rc == SOCKET_ERROR) goto exit; if (m->c->connect_state == 0) { rc = SOCKET_ERROR; goto exit; } if (m->c->connect_state == 1) /* TCP connect started - wait for completion */ { Thread_unlock_mutex(mqttclient_mutex); MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start)); Thread_lock_mutex(mqttclient_mutex); if (rc != 0) { rc = SOCKET_ERROR; goto exit; } #if defined(OPENSSL) if (m->ssl) { if (SSLSocket_setSocketForSSL(&m->c->net, m->c->sslopts) != MQTTCLIENT_SUCCESS) { if (m->c->session != NULL) if ((rc = SSL_set_session(m->c->net.ssl, m->c->session)) != 1) Log(TRACE_MIN, -1, "Failed to set SSL session with stored data, non critical"); rc = SSLSocket_connect(m->c->net.ssl, m->c->net.socket); if (rc == TCPSOCKET_INTERRUPTED) m->c->connect_state = 2; /* the connect is still in progress */ else if (rc == SSL_FATAL) { rc = SOCKET_ERROR; goto exit; } else if (rc == 1) { rc = MQTTCLIENT_SUCCESS; m->c->connect_state = 3; if (MQTTPacket_send_connect(m->c, MQTTVersion) == SOCKET_ERROR) { rc = SOCKET_ERROR; goto exit; } if (!m->c->cleansession && m->c->session == NULL) m->c->session = SSL_get1_session(m->c->net.ssl); } } else { rc = SOCKET_ERROR; goto exit; } } else { #endif m->c->connect_state = 3; /* TCP connect completed, in which case send the MQTT connect packet */ if (MQTTPacket_send_connect(m->c, MQTTVersion) == SOCKET_ERROR) { rc = SOCKET_ERROR; goto exit; } #if defined(OPENSSL) } #endif } #if defined(OPENSSL) if (m->c->connect_state == 2) /* SSL connect sent - wait for completion */ { Thread_unlock_mutex(mqttclient_mutex); MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start)); Thread_lock_mutex(mqttclient_mutex); if (rc != 1) { rc = SOCKET_ERROR; goto exit; } if(!m->c->cleansession && m->c->session == NULL) m->c->session = SSL_get1_session(m->c->net.ssl); m->c->connect_state = 3; /* TCP connect completed, in which case send the MQTT connect packet */ if (MQTTPacket_send_connect(m->c, MQTTVersion) == SOCKET_ERROR) { rc = SOCKET_ERROR; goto exit; } } #endif if (m->c->connect_state == 3) /* MQTT connect sent - wait for CONNACK */ { MQTTPacket* pack = NULL; Thread_unlock_mutex(mqttclient_mutex); pack = MQTTClient_waitfor(handle, CONNACK, &rc, millisecsTimeout - MQTTClient_elapsed(start)); Thread_lock_mutex(mqttclient_mutex); if (pack == NULL) rc = SOCKET_ERROR; else { Connack* connack = (Connack*)pack; Log(TRACE_PROTOCOL, 1, NULL, m->c->net.socket, m->c->clientID, connack->rc); if ((rc = connack->rc) == MQTTCLIENT_SUCCESS) { m->c->connected = 1; m->c->good = 1; m->c->connect_state = 0; if (MQTTVersion == 4) sessionPresent = connack->flags.bits.sessionPresent; if (m->c->cleansession) rc = MQTTClient_cleanSession(m->c); if (m->c->outboundMsgs->count > 0) { ListElement* outcurrent = NULL; while (ListNextElement(m->c->outboundMsgs, &outcurrent)) { Messages* m = (Messages*)(outcurrent->content); m->lastTouch = 0; } MQTTProtocol_retry((time_t)0, 1, 1); if (m->c->connected != 1) rc = MQTTCLIENT_DISCONNECTED; } } free(connack); m->pack = NULL; } } exit: if (rc == MQTTCLIENT_SUCCESS) { if (options->struct_version == 4) /* means we have to fill out return values */ { options->returned.serverURI = serverURI; options->returned.MQTTVersion = MQTTVersion; options->returned.sessionPresent = sessionPresent; } } else { Thread_unlock_mutex(mqttclient_mutex); MQTTClient_disconnect1(handle, 0, 0, (MQTTVersion == 3)); /* not "internal" because we don't want to call connection lost */ Thread_lock_mutex(mqttclient_mutex); } FUNC_EXIT_RC(rc); return rc; }
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options) { MQTTClients* m = handle; int rc = SOCKET_ERROR; START_TIME_TYPE start; long millisecsTimeout = 30000L; FUNC_ENTRY; Thread_lock_mutex(mqttclient_mutex); if (options == NULL) { rc = MQTTCLIENT_NULL_PARAMETER; goto exit; } if (strncmp(options->struct_id, "MQTC", 4) != 0 || options->struct_version != 0) { rc = MQTTCLIENT_BAD_STRUCTURE; goto exit; } if (options->will) /* check validity of will options structure */ { if (strncmp(options->will->struct_id, "MQTW", 4) != 0 || options->will->struct_version != 0) { rc = MQTTCLIENT_BAD_STRUCTURE; goto exit; } } if ((options->username && !UTF8_validateString(options->username)) || (options->password && !UTF8_validateString(options->password))) { rc = MQTTCLIENT_BAD_UTF8_STRING; goto exit; } millisecsTimeout = options->connectTimeout * 1000; start = MQTTClient_start_clock(); if (m->ma && !running) { Thread_start(MQTTClient_run, handle); if (MQTTClient_elapsed(start) >= millisecsTimeout) { rc = SOCKET_ERROR; goto exit; } MQTTClient_sleep(100L); } m->c->keepAliveInterval = options->keepAliveInterval; m->c->cleansession = options->cleansession; m->c->maxInflightMessages = (options->reliable) ? 1 : 10; if (options->will && options->will->struct_version == 0) { m->c->will = malloc(sizeof(willMessages)); m->c->will->msg = options->will->message; m->c->will->qos = options->will->qos; m->c->will->retained = options->will->retained; m->c->will->topic = options->will->topicName; } m->c->username = options->username; m->c->password = options->password; m->c->retryInterval = options->retryInterval; m->c->connectOptionsVersion = options->struct_version; Log(TRACE_MIN, -1, "Connecting to serverURI %s", m->serverURI); rc = MQTTProtocol_connect(m->serverURI, m->c); if (m->c->connect_state == 0) { rc = SOCKET_ERROR; goto exit; } if (m->c->connect_state == 1) { Thread_unlock_mutex(mqttclient_mutex); MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start)); Thread_lock_mutex(mqttclient_mutex); if (rc != 0) { rc = SOCKET_ERROR; goto exit; } m->c->connect_state = 2; /* TCP connect completed, in which case send the MQTT connect packet */ if (MQTTPacket_send_connect(m->c) == SOCKET_ERROR) { rc = SOCKET_ERROR; goto exit; } } if (m->c->connect_state == 2) { MQTTPacket* pack = NULL; Thread_unlock_mutex(mqttclient_mutex); pack = MQTTClient_waitfor(handle, CONNACK, &rc, millisecsTimeout - MQTTClient_elapsed(start)); Thread_lock_mutex(mqttclient_mutex); if (pack == NULL) rc = SOCKET_ERROR; else { Connack* connack = (Connack*)pack; if ((rc = connack->rc) == MQTTCLIENT_SUCCESS) { m->c->connected = 1; m->c->good = 1; m->c->connect_state = 3; time(&(m->c->lastContact)); if (m->c->cleansession) rc = MQTTClient_cleanSession(m->c); if (m->c->outboundMsgs->count > 0) { ListElement* outcurrent = NULL; while (ListNextElement(m->c->outboundMsgs, &outcurrent)) { Messages* m = (Messages*)(outcurrent->content); m->lastTouch = 0; } MQTTProtocol_retry(m->c->lastContact, 1); if (m->c->connected != 1) rc = MQTTCLIENT_DISCONNECTED; } } free(connack); m->pack = NULL; } } if (rc == SOCKET_ERROR || rc == MQTTCLIENT_PERSISTENCE_ERROR) { Thread_unlock_mutex(mqttclient_mutex); MQTTClient_disconnect(handle, 0); /* not "internal" because we don't want to call connection lost */ Thread_lock_mutex(mqttclient_mutex); } exit: if (m->c->will) { free(m->c->will); m->c->will = NULL; } Thread_unlock_mutex(mqttclient_mutex); FUNC_EXIT_RC(rc); return rc; }
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options) { MQTTClients* m = handle; int rc = SOCKET_ERROR; START_TIME_TYPE start; long millisecsTimeout = 30000L; FUNC_ENTRY; Thread_lock_mutex(mqttclient_mutex); if (options == NULL) { rc = MQTTCLIENT_NULL_PARAMETER; goto exit; } if (strncmp(options->struct_id, "MQTC", 4) != 0 || (options->struct_version != 0 && options->struct_version != 1)) { rc = MQTTCLIENT_BAD_STRUCTURE; goto exit; } if (options->will) /* check validity of will options structure */ { if (strncmp(options->will->struct_id, "MQTW", 4) != 0 || options->will->struct_version != 0) { rc = MQTTCLIENT_BAD_STRUCTURE; goto exit; } } #if defined(OPENSSL) if (options->struct_version != 0 && options->ssl) /* check validity of SSL options structure */ { if (strncmp(options->ssl->struct_id, "MQTS", 4) != 0 || options->ssl->struct_version != 0) { rc = MQTTCLIENT_BAD_STRUCTURE; goto exit; } } #endif if ((options->username && !UTF8_validateString(options->username)) || (options->password && !UTF8_validateString(options->password))) { rc = MQTTCLIENT_BAD_UTF8_STRING; goto exit; } millisecsTimeout = options->connectTimeout * 1000; start = MQTTClient_start_clock(); if (m->ma && !running) { Thread_start(MQTTClient_run, handle); if (MQTTClient_elapsed(start) >= millisecsTimeout) { rc = SOCKET_ERROR; goto exit; } MQTTClient_sleep(100L); } m->c->keepAliveInterval = options->keepAliveInterval; m->c->cleansession = options->cleansession; m->c->maxInflightMessages = (options->reliable) ? 1 : 10; if (options->will && options->will->struct_version == 0) { m->c->will = malloc(sizeof(willMessages)); m->c->will->msg = options->will->message; m->c->will->qos = options->will->qos; m->c->will->retained = options->will->retained; m->c->will->topic = options->will->topicName; } #if defined(OPENSSL) if (options->struct_version != 0 && options->ssl) m->c->sslopts = options->ssl; #endif m->c->username = options->username; m->c->password = options->password; m->c->retryInterval = options->retryInterval; Log(TRACE_MIN, -1, "Connecting to serverURI %s", m->serverURI); #if defined(OPENSSL) rc = MQTTProtocol_connect(m->serverURI, m->c, m->ssl); #else rc = MQTTProtocol_connect(m->serverURI, m->c); #endif if (rc == SOCKET_ERROR) goto exit; if (m->c->connect_state == 0) { rc = SOCKET_ERROR; goto exit; } if (m->c->connect_state == 1) /* TCP connect started - wait for completion */ { Thread_unlock_mutex(mqttclient_mutex); MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start)); Thread_lock_mutex(mqttclient_mutex); if (rc != 0) { rc = SOCKET_ERROR; goto exit; } #if defined(OPENSSL) if (m->ssl) { if (SSLSocket_setSocketForSSL(&m->c->net, m->c->sslopts) != MQTTCLIENT_SUCCESS) { if (m->c->session != NULL) if ((rc = SSL_set_session(m->c->net.ssl, m->c->session)) != 1) Log(TRACE_MIN, -1, "Failed to set SSL session with stored data, non critical"); rc = SSLSocket_connect(m->c->net.ssl, m->c->net.socket); if (rc == -1) m->c->connect_state = 2; else if (rc == SSL_FATAL) { rc = SOCKET_ERROR; goto exit; } else if (rc == 1 && !m->c->cleansession && m->c->session == NULL) m->c->session = SSL_get1_session(m->c->net.ssl); } else { rc = SOCKET_ERROR; goto exit; } } else { #endif m->c->connect_state = 3; /* TCP connect completed, in which case send the MQTT connect packet */ if (MQTTPacket_send_connect(m->c) == SOCKET_ERROR) { rc = SOCKET_ERROR; goto exit; } #if defined(OPENSSL) } #endif } #if defined(OPENSSL) if (m->c->connect_state == 2) /* SSL connect sent - wait for completion */ { Thread_unlock_mutex(mqttclient_mutex); MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start)); Thread_lock_mutex(mqttclient_mutex); if (rc != 1) { rc = SOCKET_ERROR; goto exit; } m->c->connect_state = 3; /* TCP connect completed, in which case send the MQTT connect packet */ if (MQTTPacket_send_connect(m->c) == SOCKET_ERROR) { rc = SOCKET_ERROR; goto exit; } } #endif if (m->c->connect_state == 3) /* MQTT connect sent - wait for CONNACK */ { MQTTPacket* pack = NULL; Thread_unlock_mutex(mqttclient_mutex); pack = MQTTClient_waitfor(handle, CONNACK, &rc, millisecsTimeout - MQTTClient_elapsed(start)); Thread_lock_mutex(mqttclient_mutex); if (pack == NULL) rc = SOCKET_ERROR; else { Connack* connack = (Connack*)pack; Log(LOG_PROTOCOL, 1, NULL, m->c->net.socket, m->c->clientID, connack->rc); if ((rc = connack->rc) == MQTTCLIENT_SUCCESS) { m->c->connected = 1; m->c->good = 1; m->c->connect_state = 0; if (m->c->cleansession) rc = MQTTClient_cleanSession(m->c); if (m->c->outboundMsgs->count > 0) { ListElement* outcurrent = NULL; while (ListNextElement(m->c->outboundMsgs, &outcurrent)) { Messages* m = (Messages*)(outcurrent->content); m->lastTouch = 0; } MQTTProtocol_retry(m->c->net.lastContact, 1); if (m->c->connected != 1) rc = MQTTCLIENT_DISCONNECTED; } } free(connack); m->pack = NULL; } } exit: if (rc != MQTTCLIENT_SUCCESS) { Thread_unlock_mutex(mqttclient_mutex); MQTTClient_disconnect(handle, 0); /* not "internal" because we don't want to call connection lost */ Thread_lock_mutex(mqttclient_mutex); } if (m->c->will) { free(m->c->will); m->c->will = NULL; } Thread_unlock_mutex(mqttclient_mutex); FUNC_EXIT_RC(rc); return rc; }