fix(mqtt): Small fixes and upgrade.

o Upgrading to latest upstream git version(29ab2aa). Mostly
  updating FreeRTOS types.
o Adding `MQTTPacket_msgTypesToString` for debugging.
o Adding `taskYIELD` in `MutexUnlock`. This was required for my
  sending thread to be able to get in between calls to `cycle` in
  `MQTTRun`.
o Using CPPFLAGS to control which platform include to use. Make
  the code closed to upstream's version.
o Replacing the few remaining tabs with spaces.

Merges https://github.com/espressif/ESP8266_RTOS_SDK/pull/203
This commit is contained in:
Trygve Laugstøl
2018-06-01 15:54:27 +02:00
committed by Wu Jian Gang
parent 1dfd1b19fc
commit 656078b270
8 changed files with 88 additions and 61 deletions

View File

@ -21,6 +21,8 @@
## nopoll(websocket) ## nopoll(websocket)
## paho MQTT ## paho MQTT
- Source: https://github.com/eclipse/paho.mqtt.embedded-c
- Version: 29ab2aa
## spiffs ## spiffs

View File

@ -1 +1 @@
CPPFLAGS += -DMQTT_TASK CPPFLAGS += -DMQTT_TASK -DMQTTCLIENT_PLATFORM_HEADER=MQTTFreeRTOS.h

View File

@ -15,17 +15,16 @@
* Ian Craggs - convert to FreeRTOS * Ian Craggs - convert to FreeRTOS
*******************************************************************************/ *******************************************************************************/
#include <string.h>
#include <netdb.h>
#include "MQTTFreeRTOS.h" #include "MQTTFreeRTOS.h"
#include <string.h>
#include <netdb.h>
int ThreadStart(Thread* thread, void (*fn)(void*), void* arg) int ThreadStart(Thread* thread, void (*fn)(void*), void* arg)
{ {
int rc = 0; int rc = 0;
uint16_t usTaskStackSize = (configMINIMAL_STACK_SIZE * 5); uint16_t usTaskStackSize = (configMINIMAL_STACK_SIZE * 5);
unsigned portBASE_TYPE uxTaskPriority = uxTaskPriorityGet(NULL); /* set the priority as the same as the calling task*/ UBaseType_t uxTaskPriority = uxTaskPriorityGet(NULL); /* set the priority as the same as the calling task*/
rc = xTaskCreate(fn, /* The function that implements the task. */ rc = xTaskCreate(fn, /* The function that implements the task. */
"MQTTTask", /* Just a text name for the task to aid debugging. */ "MQTTTask", /* Just a text name for the task to aid debugging. */
@ -43,21 +42,22 @@ void MutexInit(Mutex* mutex)
mutex->sem = xSemaphoreCreateMutex(); mutex->sem = xSemaphoreCreateMutex();
} }
int MutexLock(Mutex* mutex) int MutexLock(Mutex* mutex)
{ {
return xSemaphoreTake(mutex->sem, portMAX_DELAY); return xSemaphoreTake(mutex->sem, portMAX_DELAY);
} }
int MutexUnlock(Mutex* mutex) int MutexUnlock(Mutex* mutex)
{ {
return xSemaphoreGive(mutex->sem); int x = xSemaphoreGive(mutex->sem);
taskYIELD();
return x;
} }
void TimerCountdownMS(Timer* timer, unsigned int timeout_ms) void TimerCountdownMS(Timer* timer, unsigned int timeout_ms)
{ {
timer->xTicksToWait = timeout_ms / portTICK_RATE_MS; /* convert milliseconds to ticks */ timer->xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
vTaskSetTimeOutState(&timer->xTimeOut); /* Record the time at which this function was entered. */ vTaskSetTimeOutState(&timer->xTimeOut); /* Record the time at which this function was entered. */
} }
@ -71,7 +71,7 @@ void TimerCountdown(Timer* timer, unsigned int timeout)
int TimerLeftMS(Timer* timer) int TimerLeftMS(Timer* timer)
{ {
xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait); /* updates xTicksToWait to the number left */ xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait); /* updates xTicksToWait to the number left */
return timer->xTicksToWait * portTICK_RATE_MS; return timer->xTicksToWait * portTICK_PERIOD_MS;
} }

View File

@ -25,14 +25,16 @@
#include "openssl/ssl.h" #include "openssl/ssl.h"
#endif #endif
typedef struct Timer { typedef struct Timer
portTickType xTicksToWait; {
xTimeOutType xTimeOut; TickType_t xTicksToWait;
TimeOut_t xTimeOut;
} Timer; } Timer;
typedef struct Network Network; typedef struct Network Network;
struct Network { struct Network
{
int my_socket; int my_socket;
int (*mqttread)(Network*, unsigned char*, unsigned int, unsigned int); int (*mqttread)(Network*, unsigned char*, unsigned int, unsigned int);
int (*mqttwrite)(Network*, unsigned char*, unsigned int, unsigned int); int (*mqttwrite)(Network*, unsigned char*, unsigned int, unsigned int);
@ -51,16 +53,18 @@ void TimerCountdownMS(Timer*, unsigned int);
void TimerCountdown(Timer*, unsigned int); void TimerCountdown(Timer*, unsigned int);
int TimerLeftMS(Timer*); int TimerLeftMS(Timer*);
typedef struct Mutex { typedef struct Mutex
xSemaphoreHandle sem; {
SemaphoreHandle_t sem;
} Mutex; } Mutex;
void MutexInit(Mutex*); void MutexInit(Mutex*);
int MutexLock(Mutex*); int MutexLock(Mutex*);
int MutexUnlock(Mutex*); int MutexUnlock(Mutex*);
typedef struct Thread { typedef struct Thread
xTaskHandle task; {
TaskHandle_t task;
} Thread; } Thread;
int ThreadStart(Thread*, void (*fn)(void*), void* arg); int ThreadStart(Thread*, void (*fn)(void*), void* arg);
@ -85,6 +89,7 @@ void NetworkInit(Network*);
*/ */
int NetworkConnect(Network* n, char* addr, int port); int NetworkConnect(Network* n, char* addr, int port);
#ifdef CONFIG_SSL_USING_MBEDTLS
typedef struct ssl_ca_crt_key { typedef struct ssl_ca_crt_key {
unsigned char* cacrt; unsigned char* cacrt;
unsigned int cacrt_len; unsigned int cacrt_len;
@ -94,7 +99,6 @@ typedef struct ssl_ca_crt_key {
unsigned int key_len; unsigned int key_len;
} ssl_ca_crt_key_t; } ssl_ca_crt_key_t;
#ifdef CONFIG_SSL_USING_MBEDTLS
/** /**
* @brief Initialize the network structure for SSL connection * @brief Initialize the network structure for SSL connection
* *

View File

@ -358,7 +358,6 @@ int MQTTYield(MQTTClient* c, int timeout_ms)
return rc; return rc;
} }
void MQTTRun(void* parm) void MQTTRun(void* parm)
{ {
Timer timer; Timer timer;
@ -368,10 +367,10 @@ void MQTTRun(void* parm)
while (1) while (1)
{ {
TimerCountdownMS(&timer, 500); /* Don't wait too long if no traffic is incoming */
#if defined(MQTT_TASK) #if defined(MQTT_TASK)
MutexLock(&c->mutex); MutexLock(&c->mutex);
#endif #endif
TimerCountdownMS(&timer, 500); /* Don't wait too long if no traffic is incoming */
cycle(c, &timer); cycle(c, &timer);
#if defined(MQTT_TASK) #if defined(MQTT_TASK)
MutexUnlock(&c->mutex); MutexUnlock(&c->mutex);
@ -549,8 +548,9 @@ int MQTTSubscribeWithResults(MQTTClient* c, const char* topicFilter, enum QoS qo
rc = FAILURE; rc = FAILURE;
exit: exit:
if (rc == FAILURE) if (rc == FAILURE) {
MQTTCloseSession(c); MQTTCloseSession(c);
}
#if defined(MQTT_TASK) #if defined(MQTT_TASK)
MutexUnlock(&c->mutex); MutexUnlock(&c->mutex);
#endif #endif

View File

@ -16,8 +16,8 @@
* Ian Craggs - add setMessageHandler function * Ian Craggs - add setMessageHandler function
*******************************************************************************/ *******************************************************************************/
#if !defined(__MQTT_CLIENT_C_) #if !defined(MQTT_CLIENT_H)
#define __MQTT_CLIENT_C_ #define MQTT_CLIENT_H
#if defined(__cplusplus) #if defined(__cplusplus)
extern "C" { extern "C" {
@ -35,8 +35,6 @@
#endif #endif
#include "MQTTPacket.h" #include "MQTTPacket.h"
#include "stdio.h"
#include "MQTTFreeRTOS.h"
#if defined(MQTTCLIENT_PLATFORM_HEADER) #if defined(MQTTCLIENT_PLATFORM_HEADER)
/* The following sequence of macros converts the MQTTCLIENT_PLATFORM_HEADER value /* The following sequence of macros converts the MQTTCLIENT_PLATFORM_HEADER value

View File

@ -410,3 +410,24 @@ exit:
return rc; return rc;
} }
const char* MQTTPacket_msgTypesToString(enum msgTypes msgType)
{
switch (msgType)
{
case CONNECT: return "CONNECT";
case CONNACK: return "CONNACK";
case PUBLISH: return "PUBLISH";
case PUBACK: return "PUBACK";
case PUBREC: return "PUBREC";
case PUBREL: return "PUBREL";
case PUBCOMP: return "PUBCOMP";
case SUBSCRIBE: return "SUBSCRIBE";
case SUBACK: return "SUBACK";
case UNSUBSCRIBE: return "UNSUBSCRIBE";
case UNSUBACK: return "UNSUBACK";
case PINGREQ: return "PINGREQ";
case PINGRESP: return "PINGRESP";
case DISCONNECT: return "DISCONNECT";
default: return NULL;
}
}

View File

@ -125,6 +125,8 @@ typedef struct {
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp);
const char* MQTTPacket_msgTypesToString(enum msgTypes);
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
} }
#endif #endif