MQTT Client: Use LWIP socket options instead of select()
This commit is contained in:
parent
42880fded5
commit
b73b93c58c
2 changed files with 50 additions and 116 deletions
|
@ -1,15 +1,8 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file MQTTESP8266.c
|
||||
* @author Baoshi <mail(at)ba0sh1(dot)com>
|
||||
* @version 0.1
|
||||
* @date Sep 9, 2015
|
||||
* @brief Eclipse Paho ported to ESP8266 RTOS
|
||||
* Paho Embedded MQTT client, esp-open-rtos support.
|
||||
*
|
||||
******************************************************************************
|
||||
* @copyright
|
||||
*
|
||||
* Copyright (c) 2015, Baoshi Zhu. All rights reserved.
|
||||
* Copyright (c) 2015, Baoshi Zhu & 2016, Angus Gratton
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE.txt file.
|
||||
*
|
||||
|
@ -62,114 +55,64 @@ void InitTimer(Timer* timer)
|
|||
timer->end_time = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int mqtt_esp_read(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
||||
static int mqtt_esp_read(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
||||
{
|
||||
struct timeval tv;
|
||||
fd_set fdset;
|
||||
int rc = 0;
|
||||
int rcvd = 0;
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(n->my_socket, &fdset);
|
||||
tv.tv_sec = timeout_ms / 1000;
|
||||
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
||||
rc = select(n->my_socket + 1, &fdset, 0, 0, &tv);
|
||||
if ((rc > 0) && (FD_ISSET(n->my_socket, &fdset)))
|
||||
{
|
||||
rcvd = recv(n->my_socket, buffer, len, 0);
|
||||
/* set SO_RCVTIMEO if timeout_ms > 0 otherwise O_NONBLOCK */
|
||||
lwip_setsockopt(n->socket, SOL_SOCKET, SO_RCVTIMEO, &timeout_ms, sizeof(int));
|
||||
lwip_fcntl(n->socket, F_SETFL, (timeout_ms) > 0 ? 0 : O_NONBLOCK);
|
||||
int r = recv(n->socket, buffer, len, 0);
|
||||
if(r == 0) {
|
||||
r = -1; /* 0 indicates timeout */
|
||||
}
|
||||
else
|
||||
{
|
||||
// select fail
|
||||
return -1;
|
||||
}
|
||||
return rcvd;
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
int mqtt_esp_write(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
||||
static int mqtt_esp_write(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
||||
{
|
||||
struct timeval tv;
|
||||
fd_set fdset;
|
||||
int rc = 0;
|
||||
|
||||
FD_ZERO(&fdset);
|
||||
FD_SET(n->my_socket, &fdset);
|
||||
tv.tv_sec = timeout_ms / 1000;
|
||||
tv.tv_usec = (timeout_ms % 1000) * 1000;
|
||||
rc = select(n->my_socket + 1, 0, &fdset, 0, &tv);
|
||||
if ((rc > 0) && (FD_ISSET(n->my_socket, &fdset)))
|
||||
{
|
||||
rc = send(n->my_socket, buffer, len, 0);
|
||||
lwip_setsockopt(n->socket, SOL_SOCKET, SO_SNDTIMEO, &timeout_ms, sizeof(int));
|
||||
lwip_fcntl(n->socket, F_SETFL, (timeout_ms > 0) ? 0 : O_NONBLOCK);
|
||||
int r = send(n->socket, buffer, len, 0);
|
||||
if(r == 0) {
|
||||
r = -1; /* 0 indicates timeout */
|
||||
}
|
||||
else
|
||||
{
|
||||
// select fail
|
||||
return -1;
|
||||
return r;
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void NewNetwork(Network* n)
|
||||
{
|
||||
n->my_socket = -1;
|
||||
n->socket = -1;
|
||||
n->mqttread = mqtt_esp_read;
|
||||
n->mqttwrite = mqtt_esp_write;
|
||||
}
|
||||
|
||||
static int host2addr(const char *hostname , struct in_addr *in)
|
||||
{
|
||||
struct addrinfo hints, *servinfo, *p;
|
||||
struct sockaddr_in *h;
|
||||
int rv;
|
||||
|
||||
memset(&hints, 0, sizeof(hints));
|
||||
hints.ai_family = AF_INET;
|
||||
hints.ai_socktype = SOCK_STREAM;
|
||||
rv = getaddrinfo(hostname, 0 , &hints , &servinfo);
|
||||
if (rv != 0)
|
||||
{
|
||||
return rv;
|
||||
}
|
||||
|
||||
// loop through all the results and get the first resolve
|
||||
for (p = servinfo; p != 0; p = p->ai_next)
|
||||
{
|
||||
h = (struct sockaddr_in *)p->ai_addr;
|
||||
in->s_addr = h->sin_addr.s_addr;
|
||||
}
|
||||
freeaddrinfo(servinfo); // all done with this structure
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
int ConnectNetwork(Network* n, const char* host, int port)
|
||||
{
|
||||
struct sockaddr_in addr;
|
||||
ip_addr_t ipaddr;
|
||||
int ret;
|
||||
|
||||
if (host2addr(host, &(addr.sin_addr)) != 0)
|
||||
{
|
||||
return -1;
|
||||
err_t e = netconn_gethostbyname(host, &ipaddr);
|
||||
if(e) {
|
||||
return e;
|
||||
}
|
||||
|
||||
inet_addr_from_ipaddr(&(addr.sin_addr), &ipaddr);
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_port = htons(port);
|
||||
|
||||
n->my_socket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if( n->my_socket < 0 )
|
||||
{
|
||||
// error
|
||||
return -1;
|
||||
}
|
||||
ret = connect(n->my_socket, ( struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
||||
ret = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (ret < 0)
|
||||
{
|
||||
// error
|
||||
close(n->my_socket);
|
||||
return ret;
|
||||
}
|
||||
n->socket = ret;
|
||||
|
||||
ret = connect(n->socket, ( struct sockaddr *)&addr, sizeof(struct sockaddr_in));
|
||||
if (ret < 0)
|
||||
{
|
||||
close(n->socket);
|
||||
n->socket = -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -179,7 +122,7 @@ int ConnectNetwork(Network* n, const char* host, int port)
|
|||
|
||||
int DisconnectNetwork(Network* n)
|
||||
{
|
||||
close(n->my_socket);
|
||||
n->my_socket = -1;
|
||||
close(n->socket);
|
||||
n->socket = -1;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -1,15 +1,8 @@
|
|||
/**
|
||||
******************************************************************************
|
||||
* @file MQTTESP8266.h
|
||||
* @author Baoshi <mail(at)ba0sh1(dot)com>
|
||||
* @version 0.1
|
||||
* @date Sep 9, 2015
|
||||
* @brief Eclipse Paho ported to ESP8266 RTOS
|
||||
* Paho Embedded MQTT client, esp-open-rtos support.
|
||||
*
|
||||
******************************************************************************
|
||||
* @copyright
|
||||
*
|
||||
* Copyright (c) 2015, Baoshi Zhu. All rights reserved.
|
||||
* Copyright (c) 2015, Baoshi Zhu & 2016, Angus Gratton.
|
||||
* All rights reserved.
|
||||
* Use of this source code is governed by a BSD-style license that can be
|
||||
* found in the LICENSE.txt file.
|
||||
*
|
||||
|
@ -24,18 +17,20 @@
|
|||
#include <FreeRTOS.h>
|
||||
#include <portmacro.h>
|
||||
|
||||
#include <lwip/api.h>
|
||||
|
||||
typedef struct Timer Timer;
|
||||
|
||||
typedef struct Network Network;
|
||||
|
||||
struct Timer
|
||||
{
|
||||
portTickType end_time;
|
||||
};
|
||||
|
||||
typedef struct Network Network;
|
||||
|
||||
struct Network
|
||||
{
|
||||
int my_socket;
|
||||
int socket;
|
||||
int (*mqttread) (Network*, unsigned char*, int, int);
|
||||
int (*mqttwrite) (Network*, unsigned char*, int, int);
|
||||
};
|
||||
|
@ -47,10 +42,6 @@ int left_ms(Timer*);
|
|||
|
||||
void InitTimer(Timer*);
|
||||
|
||||
int mqtt_esp_read(Network*, unsigned char*, int, int);
|
||||
int mqtt_esp_write(Network*, unsigned char*, int, int);
|
||||
void mqtt_esp_disconnect(Network*);
|
||||
|
||||
void NewNetwork(Network* n);
|
||||
int ConnectNetwork(Network* n, const char* host, int port);
|
||||
int DisconnectNetwork(Network* n);
|
||||
|
|
Loading…
Reference in a new issue