Skip to content

Commit

Permalink
provide port as string
Browse files Browse the repository at this point in the history
  • Loading branch information
256dpi committed Mar 22, 2018
1 parent 23dbe3e commit 227ab12
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 13 deletions.
6 changes: 2 additions & 4 deletions esp_lwmqtt.c
Expand Up @@ -21,17 +21,16 @@ int32_t esp_lwmqtt_timer_get(void *ref) {
return (int32_t)t->deadline - (int32_t)(xTaskGetTickCount() * portTICK_PERIOD_MS);
}

lwmqtt_err_t esp_lwmqtt_network_connect(esp_lwmqtt_network_t *network, char *host, int port) {
lwmqtt_err_t esp_lwmqtt_network_connect(esp_lwmqtt_network_t *network, char *host, char *port) {
// disconnect if not already the case
esp_lwmqtt_network_disconnect(network);

// prepare hints
struct addrinfo hints = {.ai_family = AF_INET, .ai_socktype = SOCK_STREAM};

// lookup ip address
char buf[33];
struct addrinfo *res;
int r = getaddrinfo(host, itoa(port, buf, 10), &hints, &res);
int r = getaddrinfo(host, port, &hints, &res);
if (r != 0 || res == NULL) {
return LWMQTT_NETWORK_FAILED_CONNECT;
}
Expand All @@ -46,7 +45,6 @@ lwmqtt_err_t esp_lwmqtt_network_connect(esp_lwmqtt_network_t *network, char *hos
// connect socket
r = connect(network->socket, res->ai_addr, res->ai_addrlen);
if (r < 0) {
printf("... socket connect failed errno=%d\n", errno);
close(network->socket);
freeaddrinfo(res);
return LWMQTT_NETWORK_FAILED_CONNECT;
Expand Down
6 changes: 3 additions & 3 deletions esp_lwmqtt.h
@@ -1,7 +1,6 @@
#ifndef ESP_LWMQTT_H
#define ESP_LWMQTT_H

#include <lwip/api.h>
#include <lwmqtt.h>

/**
Expand Down Expand Up @@ -31,7 +30,7 @@ typedef struct {
/**
* Initiate a connection to the specified remote hose.
*/
lwmqtt_err_t esp_lwmqtt_network_connect(esp_lwmqtt_network_t *network, char *host, int port);
lwmqtt_err_t esp_lwmqtt_network_connect(esp_lwmqtt_network_t *network, char *host, char *port);

/**
* Terminate the connection.
Expand All @@ -44,14 +43,15 @@ void esp_lwmqtt_network_disconnect(esp_lwmqtt_network_t *network);
lwmqtt_err_t esp_lwmqtt_network_peek(esp_lwmqtt_network_t *network, size_t *available);

/**
* Function to wait for a socket until data is available or the timeout has been reached.
* Will wait for a socket until data is available or the timeout has been reached.
*/
lwmqtt_err_t esp_lwmqtt_network_select(esp_lwmqtt_network_t *network, bool *available, uint32_t timeout);

/**
* The lwmqtt network read callback for the esp platform.
*/
lwmqtt_err_t esp_lwmqtt_network_read(void *ref, uint8_t *buf, size_t len, size_t *read, uint32_t timeout);

/**
* The lwmqtt network write callback for the esp platform.
*/
Expand Down
19 changes: 14 additions & 5 deletions esp_mqtt.c
Expand Up @@ -25,11 +25,11 @@ static uint32_t esp_mqtt_command_timeout;

static struct {
char *host;
int port;
char *port;
char *client_id;
char *username;
char *password;
} esp_mqtt_config = {.host = NULL, .port = 1883, .client_id = NULL, .username = NULL, .password = NULL};
} esp_mqtt_config = {.host = NULL, .port = NULL, .client_id = NULL, .username = NULL, .password = NULL};

static bool esp_mqtt_running = false;
static bool esp_mqtt_connected = false;
Expand Down Expand Up @@ -248,7 +248,8 @@ static void esp_mqtt_process(void *p) {
vTaskDelete(NULL);
}

void esp_mqtt_start(const char *host, int port, const char *client_id, const char *username, const char *password) {
void esp_mqtt_start(const char *host, const char *port, const char *client_id, const char *username,
const char *password) {
// acquire mutex
ESP_MQTT_LOCK();

Expand All @@ -265,6 +266,12 @@ void esp_mqtt_start(const char *host, int port, const char *client_id, const cha
esp_mqtt_config.host = NULL;
}

// free port if set
if (esp_mqtt_config.port != NULL) {
free(esp_mqtt_config.port);
esp_mqtt_config.port = NULL;
}

// free client id if set
if (esp_mqtt_config.client_id != NULL) {
free(esp_mqtt_config.client_id);
Expand All @@ -288,8 +295,10 @@ void esp_mqtt_start(const char *host, int port, const char *client_id, const cha
esp_mqtt_config.host = strdup(host);
}

// set port
esp_mqtt_config.port = port;
// set port if provided
if (port != NULL) {
esp_mqtt_config.port = strdup(port);
}

// set client id if provided
if (client_id != NULL) {
Expand Down
3 changes: 2 additions & 1 deletion esp_mqtt.h
Expand Up @@ -41,7 +41,8 @@ void esp_mqtt_init(esp_mqtt_status_callback_t scb, esp_mqtt_message_callback_t m
* @param username - The client username.
* @param password - The client password.
*/
void esp_mqtt_start(const char *host, int port, const char *client_id, const char *username, const char *password);
void esp_mqtt_start(const char *host, const char *port, const char *client_id, const char *username,
const char *password);

/**
* Subscribe to specified topic.
Expand Down

0 comments on commit 227ab12

Please sign in to comment.