mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-21 02:15:55 +08:00
更新硬件SDK
This commit is contained in:
8
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/.gitignore
vendored
Normal file
8
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/.gitignore
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
/dep/
|
||||
/build/
|
||||
/build.paho/
|
||||
*.swp
|
||||
*.pyc
|
||||
/doc/MQTTClient/
|
||||
/doc/MQTTPacket/
|
||||
/doc/MQTTClient-C/
|
||||
21
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/CMakeLists.txt
vendored
Normal file
21
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2017 IBM Corp.
|
||||
#
|
||||
# All rights reserved. This program and the accompanying materials
|
||||
# are made available under the terms of the Eclipse Public License v1.0
|
||||
# and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
#
|
||||
# The Eclipse Public License is available at
|
||||
# http://www.eclipse.org/legal/epl-v10.html
|
||||
# and the Eclipse Distribution License is available at
|
||||
# http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
#
|
||||
# Contributors:
|
||||
# Ian Craggs - initial version
|
||||
#*******************************************************************************/
|
||||
|
||||
project("paho-mqttclient" C)
|
||||
|
||||
ADD_SUBDIRECTORY(src)
|
||||
ADD_SUBDIRECTORY(samples)
|
||||
ADD_SUBDIRECTORY(test)
|
||||
695
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c
vendored
Normal file
695
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.c
vendored
Normal file
@@ -0,0 +1,695 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2015 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - convert to FreeRTOS
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTFreeRTOS.h"
|
||||
|
||||
#include "debug_trace.h"
|
||||
#include DEBUG_LOG_HEADER_FILE
|
||||
|
||||
int FreeRTOSConnectTimeout(INT32 connectFd, UINT32 timeout)
|
||||
{
|
||||
fd_set writeSet;
|
||||
fd_set errorSet;
|
||||
FD_ZERO(&writeSet);
|
||||
FD_ZERO(&errorSet);
|
||||
FD_SET(connectFd,&writeSet);
|
||||
FD_SET(connectFd,&errorSet);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = timeout;
|
||||
tv.tv_usec = 0;
|
||||
|
||||
if(select(connectFd+1, NULL, &writeSet, &errorSet, &tv)<=0)
|
||||
{
|
||||
int mErr = sock_get_errno(connectFd);
|
||||
//////ECOMM_TRACE(UNILOG_MQTT, mqttConnectTimeout_1, P_WARNING, 1, "connect select<0 get errno=%d", mErr);
|
||||
if(mErr)
|
||||
{
|
||||
return MQTT_CONN;
|
||||
}
|
||||
else
|
||||
{
|
||||
return MQTT_TIMEOUT;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(FD_ISSET(connectFd, &errorSet))
|
||||
{
|
||||
int mErr = sock_get_errno(connectFd);
|
||||
//////ECOMM_TRACE(UNILOG_MQTT, mqttConnectTimeout_2, P_WARNING, 1, "select error fd set get errno=%d", mErr);
|
||||
if(mErr)
|
||||
{
|
||||
return MQTT_CONN;
|
||||
}
|
||||
}
|
||||
else if(FD_ISSET(connectFd, &writeSet))
|
||||
{
|
||||
//////ECOMM_TRACE(UNILOG_MQTT, mqttConnectTimeout_3, P_WARNING, 0, "errno=115(EINPROGRESS) connect success in time(10s)");
|
||||
}
|
||||
}
|
||||
|
||||
return MQTT_CONN_OK;
|
||||
|
||||
}
|
||||
|
||||
int ThreadStart(Thread* thread, void (*fn)(void*), void* arg)
|
||||
{
|
||||
int rc = 0;
|
||||
uint16_t usTaskStackSize = (configMINIMAL_STACK_SIZE * 5);
|
||||
UBaseType_t uxTaskPriority = uxTaskPriorityGet(NULL); /* set the priority as the same as the calling task*/
|
||||
|
||||
rc = xTaskCreate(fn, /* The function that implements the task. */
|
||||
"MQTTTask", /* Just a text name for the task to aid debugging. */
|
||||
usTaskStackSize, /* The stack size is defined in FreeRTOSIPConfig.h. */
|
||||
arg, /* The task parameter, not used in this case. */
|
||||
uxTaskPriority, /* The priority assigned to the task is defined in FreeRTOSConfig.h. */
|
||||
&thread->task); /* The task handle is not used. */
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
void MutexInit(Mutex* mutex)
|
||||
{
|
||||
mutex->sem = xSemaphoreCreateMutex();
|
||||
}
|
||||
|
||||
int MutexLock(Mutex* mutex)
|
||||
{
|
||||
return xSemaphoreTake(mutex->sem, portMAX_DELAY);
|
||||
}
|
||||
|
||||
int MutexUnlock(Mutex* mutex)
|
||||
{
|
||||
return xSemaphoreGive(mutex->sem);
|
||||
}
|
||||
|
||||
void TimerCountdownMS(Timer* timer, unsigned int timeout_ms)
|
||||
{
|
||||
timer->xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
|
||||
vTaskSetTimeOutState(&timer->xTimeOut); /* Record the time at which this function was entered. */
|
||||
}
|
||||
|
||||
void TimerCountdown(Timer* timer, unsigned int timeout)
|
||||
{
|
||||
TimerCountdownMS(timer, timeout * 1000);
|
||||
}
|
||||
|
||||
int TimerLeftMS(Timer* timer)
|
||||
{
|
||||
xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait); /* updates xTicksToWait to the number left */
|
||||
return (int)(((int)timer->xTicksToWait < 0) ? 0 : (timer->xTicksToWait * portTICK_PERIOD_MS));
|
||||
}
|
||||
|
||||
char TimerIsExpired(Timer* timer)
|
||||
{
|
||||
return xTaskCheckForTimeOut(&timer->xTimeOut, &timer->xTicksToWait) == pdTRUE;
|
||||
}
|
||||
|
||||
void TimerInit(Timer* timer)
|
||||
{
|
||||
timer->xTicksToWait = 0;
|
||||
memset(&timer->xTimeOut, '\0', sizeof(timer->xTimeOut));
|
||||
}
|
||||
|
||||
int socket_connect(Network* n, char* addr){
|
||||
int retVal = -1;
|
||||
INT32 errCode;
|
||||
INT32 flags = 0;
|
||||
struct sockaddr_in address;
|
||||
int rc = -1;
|
||||
sa_family_t family = AF_INET;
|
||||
struct addrinfo *result = NULL;
|
||||
struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL};
|
||||
|
||||
if((NetworkSetConnTimeout(n, 5000, 5000)) != 0){
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((rc = FreeRTOS_gethostbyname(addr, NULL, &hints, &result)) == 0)
|
||||
{
|
||||
struct addrinfo* res = result;
|
||||
/* prefer ip4 addresses */
|
||||
while (res)
|
||||
{
|
||||
if (res->ai_family == AF_INET)
|
||||
{
|
||||
result = res;
|
||||
break;
|
||||
}
|
||||
res = res->ai_next;
|
||||
}
|
||||
|
||||
if (result->ai_family == AF_INET)
|
||||
{
|
||||
address.sin_port = htons(n->port);
|
||||
address.sin_family = family = AF_INET;
|
||||
address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr;
|
||||
}
|
||||
else
|
||||
rc = -1;
|
||||
freeaddrinfo(result);
|
||||
}
|
||||
if(rc == 0)
|
||||
{
|
||||
flags = fcntl(n->my_socket, F_GETFL, 0);
|
||||
if ((retVal = FreeRTOS_connect(n->my_socket, (struct sockaddr *)&address, sizeof(address))) < 0)
|
||||
{
|
||||
errCode = sock_get_errno(n->my_socket);
|
||||
if(errCode == EINPROGRESS)
|
||||
{
|
||||
// ECOMM_TRACE(UNILOG_MQTT, mqttConnectSocket_2, P_ERROR, 0, "mqttConnectSocket connect is ongoing");
|
||||
retVal = FreeRTOSConnectTimeout(n->my_socket, 30); //for bearer suspend timeout is 25s
|
||||
if(retVal == 0)
|
||||
{
|
||||
// ECOMM_TRACE(UNILOG_MQTT, mqttConnectSocket_3, P_INFO, 0, "mqttConnectSocket connect success");
|
||||
}
|
||||
else
|
||||
{
|
||||
// ECOMM_TRACE(UNILOG_MQTT, mqttConnectSocket_4, P_ERROR, 1, "mqttConnectSocket connect fail,error code %d", errCode);
|
||||
if(socket_error_is_fatal(errCode))
|
||||
{
|
||||
retVal = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// DBG("sock_get_errno errCode:%d\n",errCode);
|
||||
// ECOMM_TRACE(UNILOG_MQTT, mqttConnectSocket_5, P_ERROR, 1, "mqttConnectSocket connect fail %d",errCode);
|
||||
retVal = 1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttConnectSocket_1, P_ERROR, 0, "mqttConnectSocket connect success");
|
||||
}
|
||||
fcntl(n->my_socket, F_SETFL, flags&~O_NONBLOCK);
|
||||
}
|
||||
return retVal;
|
||||
}
|
||||
|
||||
int socket_read(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
||||
{
|
||||
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
|
||||
TickType_t xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
|
||||
int netSecToWait = timeout_ms / 1000; /* seconds */
|
||||
#else
|
||||
TickType_t xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
|
||||
struct timeval netSecToWait;
|
||||
netSecToWait.tv_sec = timeout_ms / 1000; /* seconds */
|
||||
netSecToWait.tv_usec = 0;
|
||||
if(netSecToWait.tv_sec == 0)
|
||||
{
|
||||
netSecToWait.tv_sec = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
TimeOut_t xTimeOut;
|
||||
int recvLen = 0;
|
||||
|
||||
vTaskSetTimeOutState(&xTimeOut); /* Record the time at which this function was entered. */
|
||||
do
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
FreeRTOS_setsockopt(n->my_socket, SOL_SOCKET, FREERTOS_SO_RCVTIMEO, &netSecToWait, sizeof(netSecToWait));
|
||||
rc = FreeRTOS_recv(n->my_socket, buffer + recvLen, len - recvLen, 0);
|
||||
if (rc > 0)
|
||||
recvLen += rc;
|
||||
else if (rc < 0)
|
||||
{
|
||||
recvLen = rc;
|
||||
break;
|
||||
}
|
||||
else{
|
||||
int mErr = sock_get_errno(n->my_socket);
|
||||
if(socket_error_is_fatal(mErr)==1){
|
||||
return -2;
|
||||
}//maybe closed or reset by peer
|
||||
}
|
||||
} while (recvLen < len && xTaskCheckForTimeOut(&xTimeOut, &xTicksToWait) == pdFALSE);
|
||||
|
||||
return recvLen;
|
||||
}
|
||||
|
||||
int socket_write(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
||||
{
|
||||
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
|
||||
TickType_t xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
|
||||
int netSecToWait = timeout_ms / 1000; /* seconds */
|
||||
#else
|
||||
TickType_t xTicksToWait = timeout_ms / portTICK_PERIOD_MS; /* convert milliseconds to ticks */
|
||||
struct timeval netSecToWait;
|
||||
netSecToWait.tv_sec = timeout_ms / 1000; /* seconds */
|
||||
netSecToWait.tv_usec = 0;
|
||||
if(netSecToWait.tv_sec == 0)
|
||||
{
|
||||
netSecToWait.tv_sec = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
TimeOut_t xTimeOut;
|
||||
int sentLen = 0;
|
||||
|
||||
vTaskSetTimeOutState(&xTimeOut); /* Record the time at which this function was entered. */
|
||||
do
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
FreeRTOS_setsockopt(n->my_socket, SOL_SOCKET, FRERRTOS_SO_SNDTIMEO, &netSecToWait, sizeof(netSecToWait));
|
||||
rc = FreeRTOS_send(n->my_socket, buffer + sentLen, len - sentLen, 0);
|
||||
if (rc > 0)
|
||||
sentLen += rc;
|
||||
else if (rc < 0)
|
||||
{
|
||||
sentLen = rc;
|
||||
break;
|
||||
}
|
||||
} while (sentLen < len && xTaskCheckForTimeOut(&xTimeOut, &xTicksToWait) == pdFALSE);
|
||||
|
||||
return sentLen;
|
||||
}
|
||||
|
||||
int socket_disconnect(Network* n)
|
||||
{
|
||||
int ret;
|
||||
|
||||
//ECPLAT_PRINTF(UNILOG_CTWING, FreeRTOS_disconnect_0, P_INFO, "FreeRTOS_disconnect my_socket %d", n->my_socket);
|
||||
|
||||
ret = FreeRTOS_closesocket(n->my_socket);
|
||||
n->my_socket = -1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int mqttSslRandom(void *p_rng, unsigned char *output, size_t output_len){
|
||||
uint32_t rnglen = output_len;
|
||||
uint8_t rngoffset = 0;
|
||||
|
||||
while (rnglen > 0)
|
||||
{
|
||||
*(output + rngoffset) = (unsigned char)rand();
|
||||
rngoffset++;
|
||||
rnglen--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mqttSslDebug(void *ctx, int level, const char *file, int line, const char *str){
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTls_00, P_INFO, "%s(%d):%s", file, line, str);
|
||||
|
||||
// DBG("%s", str);
|
||||
}
|
||||
|
||||
int socket_ssl_connect(Network* n, char* addr){
|
||||
int value;
|
||||
mqttsClientSsl *ssl;
|
||||
const char *custom = "mqtts";
|
||||
char port[10] = {0};
|
||||
int authmode = MBEDTLS_SSL_VERIFY_NONE;
|
||||
uint32_t flag;
|
||||
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_2, P_INFO, "before ssl context malloc:%d", xBytesTaskMalloced);
|
||||
n->ssl = malloc(sizeof(mqttsClientSsl));
|
||||
ssl = n->ssl;
|
||||
|
||||
/*
|
||||
* 0. Initialize the RNG and the session data
|
||||
*/
|
||||
// #if defined(MBEDTLS_DEBUG_C)
|
||||
// mbedtls_debug_set_threshold((int)2);
|
||||
// #endif
|
||||
mbedtls_net_init(&ssl->netContext);
|
||||
mbedtls_ssl_init(&ssl->sslContext);
|
||||
mbedtls_ssl_config_init(&ssl->sslConfig);
|
||||
mbedtls_x509_crt_init(&ssl->caCert);
|
||||
mbedtls_x509_crt_init(&ssl->clientCert);
|
||||
mbedtls_pk_init(&ssl->pkContext);
|
||||
mbedtls_ctr_drbg_init(&ssl->ctrDrbgContext);
|
||||
mbedtls_entropy_init(&ssl->entropyContext);
|
||||
|
||||
if((value = mbedtls_ctr_drbg_seed(&ssl->ctrDrbgContext,
|
||||
mbedtls_entropy_func,
|
||||
&ssl->entropyContext,
|
||||
(const unsigned char*)custom,
|
||||
strlen(custom))) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_0, P_INFO, "mbedtls_ctr_drbg_seed failed, value:-0x%x.", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_3, P_INFO, "after ssl init:%d", xBytesTaskMalloced);
|
||||
/*
|
||||
* 0. Initialize certificates
|
||||
*/
|
||||
if(n->seclevel != 0){
|
||||
if (NULL != n->caCert) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_1, P_INFO, "STEP 0. Loading the CA root certificate ...");
|
||||
authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
||||
if (0 != (value = mbedtls_x509_crt_parse(&(ssl->caCert), (const unsigned char *)n->caCert, n->caCertLen))) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_2, P_INFO, "failed ! value:-0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
}
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_4, P_INFO, "after ca cert parse:%d", xBytesTaskMalloced);
|
||||
/* Setup Client Cert/Key */
|
||||
if(n->seclevel == 2){
|
||||
if (n->clientCert != NULL && n->clientPk != NULL) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_3, P_INFO, "STEP 0. start prepare client cert ...");
|
||||
value = mbedtls_x509_crt_parse(&(ssl->clientCert), (const unsigned char *) n->clientCert, n->clientCertLen);
|
||||
if (value != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_4, P_INFO, "failed! mbedtls_x509_crt_parse returned -0x%x\n", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_5, P_INFO, "n->clientPkLen=%d", n->clientPkLen);
|
||||
|
||||
|
||||
value = mbedtls_pk_parse_key(&ssl->pkContext, (const unsigned char *) n->clientPk, n->clientPkLen, NULL, 0);
|
||||
|
||||
if (value != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_6, P_INFO, "failed ! mbedtls_pk_parse_key returned -0x%x\n", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(n->seclevel == 0){
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_7, P_INFO, "user set verify none");
|
||||
authmode = MBEDTLS_SSL_VERIFY_NONE;
|
||||
}
|
||||
//ali mqtts is psk tls
|
||||
if((n->psk_key != NULL)&&(n->psk_identity != NULL))
|
||||
{
|
||||
mbedtls_ssl_conf_psk(&ssl->sslConfig, (const unsigned char *)n->psk_key, strlen(n->psk_key),
|
||||
(const unsigned char *)n->psk_identity, strlen(n->psk_identity));
|
||||
}
|
||||
|
||||
/*
|
||||
* 1. Start the connection
|
||||
*/
|
||||
snprintf(port, sizeof(port), "%d", n->port);
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_8_0, P_INFO, "STEP 1. host:%s", host);
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_8_1, P_INFO, "STEP 1. Connecting to PORT:%d",n->port);
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_8_2, P_INFO, "STEP 1. port:%s", port);
|
||||
if (0 != (value = mbedtls_net_connect(&ssl->netContext, addr, port, MBEDTLS_NET_PROTO_TCP, LWIP_PS_INVALID_CID))) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_9, P_INFO, " failed ! mbedtls_net_connect returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 2. Setup stuff
|
||||
*/
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_10, P_INFO, "STEP 2. Setting up the SSL/TLS structure...");
|
||||
if ((value = mbedtls_ssl_config_defaults(&(ssl->sslConfig), MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_11, P_INFO, " failed! mbedtls_ssl_config_defaults returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_6, P_INFO, "after net connect:%d", xBytesTaskMalloced);
|
||||
mbedtls_ssl_conf_max_version(&ssl->sslConfig, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
|
||||
mbedtls_ssl_conf_min_version(&ssl->sslConfig, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
|
||||
|
||||
memcpy(&(ssl->crtProfile), ssl->sslConfig.cert_profile, sizeof(mbedtls_x509_crt_profile));
|
||||
mbedtls_ssl_conf_authmode(&(ssl->sslConfig), authmode);
|
||||
|
||||
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
||||
if ((value = mbedtls_ssl_conf_max_frag_len(&(ssl->sslConfig), MBEDTLS_SSL_MAX_FRAG_LEN_4096)) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_12, P_INFO, " mbedtls_ssl_conf_max_frag_len returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_ssl_conf_cert_profile(&ssl->sslConfig, &ssl->crtProfile);
|
||||
mbedtls_ssl_conf_ca_chain(&(ssl->sslConfig), &(ssl->caCert), NULL);
|
||||
if(n->clientCert) {
|
||||
if ((value = mbedtls_ssl_conf_own_cert(&(ssl->sslConfig), &(ssl->clientCert), &(ssl->pkContext))) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_13, P_INFO, " failed! mbedtls_ssl_conf_own_cert returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if(n->ciphersuite[0] != 0xFFFF){
|
||||
mbedtls_ssl_conf_ciphersuites(&(ssl->sslConfig), (const int *)(n->ciphersuite));
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_14, P_INFO, "conf ciphersuite 0x%x", n->ciphersuite[0]);
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_rng(&(ssl->sslConfig), mqttSslRandom, &(ssl->ctrDrbgContext));
|
||||
mbedtls_ssl_conf_dbg(&(ssl->sslConfig), mqttSslDebug, NULL);
|
||||
|
||||
#if defined(MBEDTLS_SSL_ALPN)
|
||||
const char *alpn_list[] = { "http/1.1", NULL };
|
||||
mbedtls_ssl_conf_alpn_protocols(&(ssl->sslConfig),alpn_list);
|
||||
#endif
|
||||
|
||||
if(n->timeout_r > 0) {
|
||||
uint32_t recvTimeout;
|
||||
recvTimeout = n->timeout_r > MQTT_MAX_TIMEOUT ? MQTT_MAX_TIMEOUT * 1000 : n->timeout_r * 1000;
|
||||
mbedtls_ssl_conf_read_timeout(&(ssl->sslConfig), recvTimeout);
|
||||
}
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_7, P_INFO, "before ssl setup:%d", xBytesTaskMalloced);
|
||||
if ((value = mbedtls_ssl_setup(&(ssl->sslContext), &(ssl->sslConfig))) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_15, P_INFO, " failed! mbedtls_ssl_setup returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
if(n->hostName != NULL)
|
||||
{
|
||||
mbedtls_ssl_set_hostname(&(ssl->sslContext), n->hostName);
|
||||
}
|
||||
else
|
||||
{
|
||||
mbedtls_ssl_set_hostname(&(ssl->sslContext), addr);
|
||||
}
|
||||
mbedtls_ssl_set_bio(&(ssl->sslContext), &(ssl->netContext), (mbedtls_ssl_send_t*)mbedtls_net_send, (mbedtls_ssl_recv_t*)mbedtls_net_recv, (mbedtls_ssl_recv_timeout_t*)mbedtls_net_recv_timeout);
|
||||
|
||||
|
||||
/*
|
||||
* 3. Handshake
|
||||
*/
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_8, P_INFO, "after ssl setup before handshake:%d", xBytesTaskMalloced);
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_16, P_INFO, "STEP 3. Performing the SSL/TLS handshake...");
|
||||
|
||||
while ((value = mbedtls_ssl_handshake(&(ssl->sslContext))) != 0) {
|
||||
if ((value != MBEDTLS_ERR_SSL_WANT_READ) && (value != MBEDTLS_ERR_SSL_WANT_WRITE)) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_17, P_INFO, "failed ! mbedtls_ssl_handshake returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_9, P_INFO, "after handshake:%d", xBytesTaskMalloced);
|
||||
|
||||
/*
|
||||
* 4. Verify the server certificate
|
||||
*/
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_18, P_INFO, "STEP 4. Verifying peer X.509 certificate..");
|
||||
flag = mbedtls_ssl_get_verify_result(&(ssl->sslContext));
|
||||
if (flag != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_19, P_INFO, " failed ! verify result not confirmed.");
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_20, P_INFO, "caCert varification ok");
|
||||
|
||||
return MQTT_CONN_OK;
|
||||
}
|
||||
|
||||
int socket_ssl_read(Network *n, unsigned char *buffer, int len, int timeout_ms) {
|
||||
UINT32 readLen = 0;
|
||||
static int net_status = 0;
|
||||
INT32 ret = -1;
|
||||
char err_str[33];
|
||||
mqttsClientSsl *ssl = (mqttsClientSsl *)n->ssl;
|
||||
|
||||
mbedtls_ssl_conf_read_timeout(&(ssl->sslConfig), timeout_ms);
|
||||
while (readLen < len) {
|
||||
ret = mbedtls_ssl_read(&(ssl->sslContext), (unsigned char *)(buffer + readLen), (len - readLen));
|
||||
if (ret > 0) {
|
||||
readLen += ret;
|
||||
net_status = 0;
|
||||
} else if (ret == 0) {
|
||||
/* if ret is 0 and net_status is -2, indicate the connection is closed during last call */
|
||||
return (net_status == -2) ? net_status : readLen;
|
||||
} else {
|
||||
if (MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY == ret) {
|
||||
//mbedtls_strerror(ret, err_str, sizeof(err_str));
|
||||
printf("ssl recv error: code = -0x%04X, err_str = '%s'\n", -ret, err_str);
|
||||
net_status = -2; /* connection is closed */
|
||||
break;
|
||||
} else if ((MBEDTLS_ERR_SSL_TIMEOUT == ret)
|
||||
|| (MBEDTLS_ERR_SSL_CONN_EOF == ret)
|
||||
|| (MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED == ret)
|
||||
|| (MBEDTLS_ERR_SSL_NON_FATAL == ret)) {
|
||||
/* read already complete */
|
||||
/* if call mbedtls_ssl_read again, it will return 0 (means EOF) */
|
||||
|
||||
return readLen;
|
||||
} else {
|
||||
//mbedtls_strerror(ret, err_str, sizeof(err_str));
|
||||
printf("ssl recv error: code = -0x%04X, err_str = '%s'\n", -ret, err_str);
|
||||
net_status = -1;
|
||||
return -1; /* Connection error */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (readLen > 0) ? readLen : net_status;
|
||||
}
|
||||
|
||||
int socket_ssl_write(Network *n, unsigned char *buffer, int len, int timeout_ms) {
|
||||
INT32 waitToSend = len;
|
||||
INT32 hasSend = 0;
|
||||
mqttsClientSsl *ssl = (mqttsClientSsl *)n->ssl;
|
||||
do
|
||||
{
|
||||
hasSend = mbedtls_ssl_write(&(ssl->sslContext), (unsigned char *)(buffer + len - waitToSend), waitToSend);
|
||||
if(hasSend > 0)
|
||||
{
|
||||
waitToSend -= hasSend;
|
||||
}
|
||||
else if(hasSend == 0)
|
||||
{
|
||||
return len;
|
||||
}
|
||||
else
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_23, P_SIG, 0, "mqtt_client(ssl): send failed \n");
|
||||
return 0;
|
||||
}
|
||||
}while(waitToSend>0);
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
void socket_ssl_disconnect(Network *n) {
|
||||
|
||||
mqttsClientSsl *ssl = (mqttsClientSsl *)n->ssl;
|
||||
|
||||
if(ssl == NULL)
|
||||
return ;
|
||||
|
||||
mbedtls_ssl_close_notify(&(ssl->sslContext));
|
||||
mbedtls_net_free(&(ssl->netContext));
|
||||
mbedtls_x509_crt_free(&(ssl->caCert));
|
||||
mbedtls_x509_crt_free(&(ssl->clientCert));
|
||||
mbedtls_pk_free(&(ssl->pkContext));
|
||||
mbedtls_ssl_free(&(ssl->sslContext));
|
||||
mbedtls_ssl_config_free(&(ssl->sslConfig));
|
||||
mbedtls_ctr_drbg_free(&(ssl->ctrDrbgContext));
|
||||
mbedtls_entropy_free(&(ssl->entropyContext));
|
||||
|
||||
free(ssl);
|
||||
n->ssl = NULL;
|
||||
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_36, P_INFO, 0, "mqtt tls close ok");
|
||||
return ;
|
||||
}
|
||||
|
||||
int NetworkConnect(Network* n, char* addr, int port){
|
||||
n->addr = addr;
|
||||
n->port = port;
|
||||
if (n->isMqtts){
|
||||
if (socket_ssl_connect(n, addr)==MQTT_CONN_OK){
|
||||
mqttsClientSsl *ssl = (mqttsClientSsl *)n->ssl;
|
||||
n->my_socket = ssl->netContext.fd;
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
else
|
||||
return socket_connect(n, addr);
|
||||
}
|
||||
#include "luat_wdt.h"
|
||||
int FreeRTOS_read(Network* n, unsigned char* buffer, int len, int timeout_ms){
|
||||
luat_wdt_feed();
|
||||
if (n->isMqtts)
|
||||
return socket_ssl_read(n, buffer, len, timeout_ms);
|
||||
else
|
||||
return socket_read(n, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
int FreeRTOS_write(Network* n, unsigned char* buffer, int len, int timeout_ms){
|
||||
if (n->isMqtts)
|
||||
return socket_ssl_write(n, buffer, len, timeout_ms);
|
||||
else
|
||||
return socket_write(n, buffer, len, timeout_ms);
|
||||
}
|
||||
|
||||
int FreeRTOS_disconnect(Network* n){
|
||||
if (n->isMqtts)
|
||||
socket_ssl_disconnect(n);
|
||||
else
|
||||
socket_disconnect(n);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void NetworkInit(Network* n)
|
||||
{
|
||||
n->my_socket = -1;
|
||||
|
||||
n->mqttread = FreeRTOS_read;
|
||||
n->mqttwrite = FreeRTOS_write;
|
||||
n->disconnect = FreeRTOS_disconnect;
|
||||
}
|
||||
|
||||
int NetworkSetConnTimeout(Network* n, int send_timeout, int recv_timeout)
|
||||
{
|
||||
int ret = 0;
|
||||
#if LWIP_SO_SNDRCVTIMEO_NONSTANDARD
|
||||
int tx_timeout = send_timeout;
|
||||
int rx_timeout = recv_timeout;
|
||||
#else
|
||||
struct timeval tx_timeout;
|
||||
tx_timeout.tv_sec = send_timeout/1000;
|
||||
tx_timeout.tv_usec = (send_timeout%1000)*1000;
|
||||
struct timeval rx_timeout;
|
||||
rx_timeout.tv_sec = recv_timeout/1000;
|
||||
rx_timeout.tv_usec = (recv_timeout%1000)*1000;
|
||||
#endif
|
||||
|
||||
if(n->my_socket == -1)
|
||||
{
|
||||
if ((n->my_socket = FreeRTOS_socket(FREERTOS_AF_INET, FREERTOS_SOCK_STREAM, FREERTOS_IPPROTO_TCP)) < 0)
|
||||
{
|
||||
//ECPLAT_PRINTF(UNILOG_CTWING, NetworkSetConnTimeout_0, P_INFO, "NetworkSetConnTimeout my_socket fail %d", n->my_socket);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
ret = FreeRTOS_setsockopt(n->my_socket, SOL_SOCKET, SO_SNDTIMEO, &tx_timeout, sizeof(tx_timeout));
|
||||
if(ret != 0)
|
||||
{
|
||||
//ECPLAT_PRINTF(UNILOG_CTWING, NetworkSetConnTimeout_1, P_INFO, "NetworkSetConnTimeout send fail %d", ret);
|
||||
//return 1;
|
||||
}
|
||||
ret = FreeRTOS_setsockopt(n->my_socket, SOL_SOCKET, SO_RCVTIMEO, &rx_timeout, sizeof(rx_timeout));
|
||||
if(ret != 0)
|
||||
{
|
||||
//ECPLAT_PRINTF(UNILOG_CTWING, NetworkSetConnTimeout_2, P_INFO, "NetworkSetConnTimeout recv fail %d", ret);
|
||||
//return 1;
|
||||
}
|
||||
INT32 flags = fcntl(n->my_socket, F_GETFL, 0);
|
||||
if(flags < 0)
|
||||
{
|
||||
//ECPLAT_PRINTF(UNILOG_CTWING, NetworkSetConnTimeout_3, P_INFO, "NetworkSetConnTimeout fcntl fail %d", flags);
|
||||
close(n->my_socket);
|
||||
n->my_socket = -1;
|
||||
return 1;
|
||||
}
|
||||
|
||||
fcntl(n->my_socket, F_SETFL, flags|O_NONBLOCK); //set socket as nonblock for connect timeout
|
||||
|
||||
return 0;
|
||||
}
|
||||
174
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.h
vendored
Normal file
174
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/FreeRTOS/MQTTFreeRTOS.h
vendored
Normal file
@@ -0,0 +1,174 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2015 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(MQTTFreeRTOS_H)
|
||||
#define MQTTFreeRTOS_H
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "semphr.h"
|
||||
#include "task.h"
|
||||
|
||||
#include "common_api.h"
|
||||
|
||||
// #ifdef FEATURE_MQTT_TLS_ENABLE
|
||||
// #include "MQTTTls.h"
|
||||
// #endif
|
||||
|
||||
#include "sockets.h"
|
||||
#include "api.h"
|
||||
#include "netdb.h"
|
||||
|
||||
#ifdef FEATURE_MQTT_TLS_ENABLE
|
||||
#include "mbedtls/net.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/certs.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#endif
|
||||
|
||||
#include "sockets.h"
|
||||
#include "api.h"
|
||||
#include "netdb.h"
|
||||
#define FreeRTOS_setsockopt setsockopt
|
||||
#define FreeRTOS_getsockopt getsockopt
|
||||
#define FreeRTOS_recv recv
|
||||
#define FreeRTOS_closesocket close
|
||||
#define FreeRTOS_shutdownsocket shutdown
|
||||
#define FreeRTOS_socket socket
|
||||
#define FreeRTOS_connect connect
|
||||
#define FreeRTOS_gethostbyname getaddrinfo
|
||||
#define FreeRTOS_htons htons
|
||||
#define FreeRTOS_send send
|
||||
|
||||
#define FREERTOS_SO_RCVTIMEO SO_RCVTIMEO
|
||||
#define FRERRTOS_SO_SNDTIMEO SO_SNDTIMEO
|
||||
#define FRERRTOS_SO_ERROR SO_ERROR
|
||||
#define FREERTOS_AF_INET AF_INET
|
||||
#define FREERTOS_SOCK_STREAM SOCK_STREAM
|
||||
#define FREERTOS_IPPROTO_TCP IPPROTO_TCP
|
||||
#define FREERTOS_SOL_SOCKET SOL_SOCKET
|
||||
|
||||
#define freertos_sockaddr sockaddr_in
|
||||
|
||||
typedef int xSocket_t;
|
||||
|
||||
|
||||
///MQTT client results
|
||||
typedef enum {
|
||||
MQTT_CONN_OK = 0, ///<Success
|
||||
MQTT_PROCESSING, ///<Processing
|
||||
MQTT_PARSE, ///<url Parse error
|
||||
MQTT_DNS, ///<Could not resolve name
|
||||
MQTT_PRTCL, ///<Protocol error
|
||||
MQTT_NOTFOUND, ///<HTTP 404 Error
|
||||
MQTT_REFUSED, ///<HTTP 403 Error
|
||||
MQTT_ERROR, ///<HTTP xxx error
|
||||
MQTT_TIMEOUT, ///<Connection timeout
|
||||
MQTT_CONN, ///<Connection error
|
||||
MQTT_FATAL_ERROR, //fatal error when conenct
|
||||
MQTT_CLOSED, ///<Connection was closed by remote host
|
||||
MQTT_MOREDATA, ///<Need get more data
|
||||
MQTT_OVERFLOW, ///<Buffer overflow
|
||||
MQTT_MBEDTLS_ERR,
|
||||
|
||||
}MQTTResult;
|
||||
typedef struct Timer
|
||||
{
|
||||
TickType_t xTicksToWait;
|
||||
TimeOut_t xTimeOut;
|
||||
} Timer;
|
||||
|
||||
#define MQTT_MAX_TIMEOUT (10 * 60) //10 min
|
||||
|
||||
typedef struct mqttsClientSslTag
|
||||
{
|
||||
mbedtls_ssl_context sslContext;
|
||||
mbedtls_net_context netContext;
|
||||
mbedtls_ssl_config sslConfig;
|
||||
mbedtls_entropy_context entropyContext;
|
||||
mbedtls_ctr_drbg_context ctrDrbgContext;
|
||||
mbedtls_x509_crt_profile crtProfile;
|
||||
mbedtls_x509_crt caCert;
|
||||
mbedtls_x509_crt clientCert;
|
||||
mbedtls_pk_context pkContext;
|
||||
}mqttsClientSsl;
|
||||
|
||||
typedef struct Network Network;
|
||||
|
||||
struct Network
|
||||
{
|
||||
xSocket_t my_socket;
|
||||
|
||||
int timeout_r;
|
||||
char *addr;
|
||||
uint16_t port;
|
||||
bool isMqtts;
|
||||
#ifdef FEATURE_MQTT_TLS_ENABLE
|
||||
mqttsClientSsl * ssl;
|
||||
char *caCert;
|
||||
char *clientCert;
|
||||
char *clientPk;
|
||||
char *hostName;
|
||||
char *psk_key;
|
||||
char *psk_identity;
|
||||
int caCertLen;
|
||||
int clientCertLen;
|
||||
int clientPkLen;
|
||||
uint8_t seclevel;//0:no verify; 1:verify server; 2:both verify
|
||||
int32_t ciphersuite[2];//just like 0x0035 TLS_RSA_WITH_AES_256_CBC_SHA,ciphersuite[1] must NULL
|
||||
uint8_t pdpId;//pdp context id--cid--0 is default
|
||||
uint8_t cache;//0:no session resumption; 1:session resumption
|
||||
uint8_t sni;//0:no sni; 1:has sni
|
||||
uint8_t ignore;//0:not ignore; 1:ignore
|
||||
uint8_t saveMem;//0:disable; 1:enable
|
||||
#endif
|
||||
int (*mqttread) (Network*, unsigned char*, int, int);
|
||||
int (*mqttwrite) (Network*, unsigned char*, int, int);
|
||||
int (*disconnect) (Network*);
|
||||
};
|
||||
|
||||
void TimerInit(Timer*);
|
||||
char TimerIsExpired(Timer*);
|
||||
void TimerCountdownMS(Timer*, unsigned int);
|
||||
void TimerCountdown(Timer*, unsigned int);
|
||||
int TimerLeftMS(Timer*);
|
||||
|
||||
typedef struct Mutex
|
||||
{
|
||||
SemaphoreHandle_t sem;
|
||||
} Mutex;
|
||||
|
||||
void MutexInit(Mutex*);
|
||||
int MutexLock(Mutex*);
|
||||
int MutexUnlock(Mutex*);
|
||||
|
||||
typedef struct Thread
|
||||
{
|
||||
TaskHandle_t task;
|
||||
} Thread;
|
||||
|
||||
int ThreadStart(Thread*, void (*fn)(void*), void* arg);
|
||||
|
||||
int FreeRTOS_read(Network* n, unsigned char* buffer, int len, int timeout_ms);
|
||||
int FreeRTOS_write(Network* n, unsigned char* buffer, int len, int timeout_ms);
|
||||
int FreeRTOS_disconnect(Network* n);
|
||||
|
||||
void NetworkInit(Network* n);
|
||||
int NetworkConnect(Network* n, char* addr, int port);
|
||||
int NetworkSetConnTimeout(Network* n, int send_timeout, int recv_timeout);
|
||||
/*int NetworkConnectTLS(Network*, char*, int, SlSockSecureFiles_t*, unsigned char, unsigned int, char);*/
|
||||
|
||||
#endif
|
||||
860
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/MQTTClient.c
vendored
Normal file
860
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/MQTTClient.c
vendored
Normal file
@@ -0,0 +1,860 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2017 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - fix for #96 - check rem_len in readPacket
|
||||
* Ian Craggs - add ability to set message handler separately #6
|
||||
*******************************************************************************/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdarg.h>
|
||||
#include "commonTypedef.h"
|
||||
#include "MQTTClient.h"
|
||||
#include "luat_debug.h"
|
||||
#include "debug_trace.h"
|
||||
#include DEBUG_LOG_HEADER_FILE
|
||||
|
||||
// QueueHandle_t mqttSendMsgHandle = NULL;
|
||||
|
||||
// void mqttDefMessageArrived(MessageData* data)
|
||||
// {
|
||||
// char *bufTemp = NULL;
|
||||
// bufTemp = malloc(data->message->payloadlen+1);
|
||||
// memset(bufTemp, 0, data->message->payloadlen+1);
|
||||
// memcpy(bufTemp, data->message->payload, data->message->payloadlen);
|
||||
// ////ECPLAT_PRINTF(UNILOG_MQTT, mqttRecvTask_2, P_SIG, ".........MQTT topic is:%s", (const uint8_t *)data->topicName->lenstring.data);
|
||||
// ////ECPLAT_PRINTF(UNILOG_MQTT, mqttRecvTask_1, P_SIG, ".........MQTT_messageArrived is:%s", (const uint8_t *)bufTemp);
|
||||
// free(bufTemp);
|
||||
// }
|
||||
|
||||
static void NewMessageData(MessageData* md, MQTTString* aTopicName, MQTTMessage* aMessage) {
|
||||
md->topicName = aTopicName;
|
||||
md->message = aMessage;
|
||||
}
|
||||
|
||||
|
||||
static int getNextPacketId(MQTTClient *c) {
|
||||
return c->next_packetid = (c->next_packetid == MAX_PACKET_ID) ? 1 : c->next_packetid + 1;
|
||||
}
|
||||
|
||||
static int sendPacket(MQTTClient* c, int length, Timer* timer)
|
||||
{
|
||||
int rc = FAILURE,
|
||||
sent = 0;
|
||||
|
||||
while (sent < length && !TimerIsExpired(timer))
|
||||
{
|
||||
rc = c->ipstack->mqttwrite(c->ipstack, &c->sendbuf[sent], length, TimerLeftMS(timer));
|
||||
if (rc < 0) // there was an error writing the data
|
||||
break;
|
||||
sent += rc;
|
||||
}
|
||||
if (sent == length)
|
||||
{
|
||||
TimerCountdown(&c->last_sent, c->keepAliveInterval); // record the fact that we have successfully sent the packet
|
||||
rc = SUCCESS;
|
||||
}
|
||||
else
|
||||
rc = FAILURE;
|
||||
return rc;
|
||||
}
|
||||
|
||||
void MQTTClientInit(MQTTClient* c, Network* network, unsigned int command_timeout_ms,
|
||||
unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size)
|
||||
{
|
||||
int i;
|
||||
c->ipstack = network;
|
||||
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
|
||||
c->messageHandlers[i].topicFilter = 0;
|
||||
c->command_timeout_ms = command_timeout_ms;
|
||||
c->sendbuf = sendbuf;
|
||||
c->sendbuf_size = sendbuf_size;
|
||||
c->readbuf = readbuf;
|
||||
c->readbuf_size = readbuf_size;
|
||||
c->mqtt_keepalive_retry_count = 0;
|
||||
c->isconnected = 0;
|
||||
c->cleansession = 0;
|
||||
c->ping_outstanding = 0;
|
||||
c->defaultMessageHandler = NULL;
|
||||
c->next_packetid = 1;
|
||||
TimerInit(&c->last_sent);
|
||||
TimerInit(&c->last_received);
|
||||
#if defined(MQTT_TASK)
|
||||
MutexInit(&c->mutex);
|
||||
#endif
|
||||
}
|
||||
|
||||
static int decodePacket(MQTTClient* c, int* value, int timeout)
|
||||
{
|
||||
unsigned char i;
|
||||
int multiplier = 1;
|
||||
int len = 0;
|
||||
const int MAX_NO_OF_REMAINING_LENGTH_BYTES = 4;
|
||||
|
||||
*value = 0;
|
||||
do
|
||||
{
|
||||
int rc = MQTTPACKET_READ_ERROR;
|
||||
|
||||
if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
|
||||
{
|
||||
rc = MQTTPACKET_READ_ERROR; /* bad data */
|
||||
goto exit;
|
||||
}
|
||||
rc = c->ipstack->mqttread(c->ipstack, &i, 1, timeout);
|
||||
if (rc != 1)
|
||||
goto exit;
|
||||
*value += (i & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
} while ((i & 128) != 0);
|
||||
exit:
|
||||
return len;
|
||||
}
|
||||
|
||||
static int readPacket(MQTTClient* c, Timer* timer)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int len = 0;
|
||||
int rem_len = 0;
|
||||
|
||||
/* 1. read the header byte. This has the packet type in it */
|
||||
int rc = c->ipstack->mqttread(c->ipstack, c->readbuf, 1, TimerLeftMS(timer));
|
||||
if (rc != 1)
|
||||
goto exit;
|
||||
|
||||
len = 1;
|
||||
/* 2. read the remaining length. This is variable in itself */
|
||||
decodePacket(c, &rem_len, TimerLeftMS(timer));
|
||||
len += MQTTPacket_encode(c->readbuf + 1, rem_len); /* put the original remaining length back into the buffer */
|
||||
|
||||
if (rem_len > (c->readbuf_size - len))
|
||||
{
|
||||
rc = BUFFER_OVERFLOW;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
/* 3. read the rest of the buffer using a callback to supply the rest of the data */
|
||||
if (rem_len > 0 && (c->ipstack->mqttread(c->ipstack, c->readbuf + len, rem_len, TimerLeftMS(timer)) != rem_len)) {
|
||||
rc = 0;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = c->readbuf[0];
|
||||
rc = header.bits.type;
|
||||
if (c->keepAliveInterval > 0)
|
||||
TimerCountdown(&c->last_received, c->keepAliveInterval); // record the fact that we have successfully received a packet
|
||||
exit:
|
||||
return rc;
|
||||
}
|
||||
|
||||
// assume topic filter and name is in correct format
|
||||
// # can only be at end
|
||||
// + and # can only be next to separator
|
||||
static char isTopicMatched(char* topicFilter, MQTTString* topicName)
|
||||
{
|
||||
char* curf = topicFilter;
|
||||
char* curn = topicName->lenstring.data;
|
||||
char* curn_end = curn + topicName->lenstring.len;
|
||||
|
||||
while (*curf && curn < curn_end)
|
||||
{
|
||||
if (*curn == '/' && *curf != '/')
|
||||
break;
|
||||
if (*curf != '+' && *curf != '#' && *curf != *curn)
|
||||
break;
|
||||
if (*curf == '+')
|
||||
{ // skip until we meet the next separator, or end of string
|
||||
char* nextpos = curn + 1;
|
||||
while (nextpos < curn_end && *nextpos != '/')
|
||||
nextpos = ++curn + 1;
|
||||
}
|
||||
else if (*curf == '#')
|
||||
curn = curn_end - 1; // skip until end of string
|
||||
curf++;
|
||||
curn++;
|
||||
};
|
||||
|
||||
return (curn == curn_end) && (*curf == '\0');
|
||||
}
|
||||
|
||||
int deliverMessage(MQTTClient* c, MQTTString* topicName, MQTTMessage* message)
|
||||
{
|
||||
int i;
|
||||
int rc = FAILURE;
|
||||
//ECOMM_TRACE(UNILOG_MQTT, deliverMessage1, P_SIG, 0, "....1....deliverMessage..");
|
||||
|
||||
// we have to find the right message handler - indexed by topic
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
|
||||
{
|
||||
if (c->messageHandlers[i].topicFilter != 0 && (MQTTPacket_equals(topicName, (char*)c->messageHandlers[i].topicFilter) ||
|
||||
isTopicMatched((char*)c->messageHandlers[i].topicFilter, topicName)))
|
||||
{
|
||||
if (c->messageHandlers[i].fp != NULL)
|
||||
{
|
||||
MessageData md;
|
||||
NewMessageData(&md, topicName, message);
|
||||
c->messageHandlers[i].fp(&md);
|
||||
rc = SUCCESS;
|
||||
//ECOMM_TRACE(UNILOG_MQTT, deliverMessage2, P_SIG, 0, "....2....deliverMessage..");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rc == FAILURE && c->defaultMessageHandler != NULL)
|
||||
{
|
||||
MessageData md;
|
||||
//ECOMM_TRACE(UNILOG_MQTT, deliverMessage3, P_SIG, 0, "....3....deliverMessage..");
|
||||
NewMessageData(&md, topicName, message);
|
||||
c->defaultMessageHandler(&md);
|
||||
rc = SUCCESS;
|
||||
//ECOMM_TRACE(UNILOG_MQTT, deliverMessage4, P_SIG, 0, "....4....deliverMessage..");
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int keepalive(MQTTClient* c)
|
||||
{
|
||||
int rc = SUCCESS;
|
||||
|
||||
if (c->keepAliveInterval == 0)
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (TimerIsExpired(&c->last_sent) || TimerIsExpired(&c->last_received))
|
||||
{
|
||||
if (c->ping_outstanding)
|
||||
{
|
||||
c->mqtt_keepalive_retry_count++;
|
||||
//ECOMM_TRACE(UNILOG_MQTT, keepalive_0, P_SIG, 0, "....keepalive....ping_outstanding..=1..");
|
||||
rc = FAILURE; /* PINGRESP not received in keepalive interval */
|
||||
}
|
||||
else
|
||||
{
|
||||
Timer timer;
|
||||
TimerInit(&timer);
|
||||
TimerCountdownMS(&timer, 1000);
|
||||
|
||||
memset(c->sendbuf, 0, c->sendbuf_size);
|
||||
memset(c->readbuf, 0, c->readbuf_size);
|
||||
int len = MQTTSerialize_pingreq(c->sendbuf, c->sendbuf_size);
|
||||
|
||||
//ECOMM_TRACE(UNILOG_MQTT, keepalive_1, P_SIG, 0, "....keepalive....send packet..");
|
||||
if (len > 0 && (rc = sendPacket(c, len, &timer)) == SUCCESS) // send the ping packet
|
||||
c->ping_outstanding = 1;
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
return rc;
|
||||
}
|
||||
|
||||
int keepaliveRetry(MQTTClient* c)
|
||||
{
|
||||
int rc = SUCCESS;
|
||||
|
||||
if (c->keepAliveInterval == 0)
|
||||
{
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (TimerIsExpired(&c->last_sent) || TimerIsExpired(&c->last_received))
|
||||
{
|
||||
{
|
||||
Timer timer;
|
||||
TimerInit(&timer);
|
||||
TimerCountdownMS(&timer, 1000);
|
||||
|
||||
memset(c->sendbuf, 0, c->sendbuf_size);
|
||||
memset(c->readbuf, 0, c->readbuf_size);
|
||||
int len = MQTTSerialize_pingreq(c->sendbuf, c->sendbuf_size);
|
||||
|
||||
//ECOMM_TRACE(UNILOG_MQTT, keepalive_1pp, P_SIG, 0, "....keepalive....send packet..");
|
||||
if (len > 0 && (rc = sendPacket(c, len, &timer)) == SUCCESS) // send the ping packet
|
||||
c->ping_outstanding = 1;
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
return rc;
|
||||
}
|
||||
|
||||
void MQTTCleanSession(MQTTClient* c)
|
||||
{
|
||||
int i = 0;
|
||||
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
|
||||
c->messageHandlers[i].topicFilter = NULL;
|
||||
}
|
||||
|
||||
void MQTTCloseSession(MQTTClient* c)
|
||||
{
|
||||
c->ping_outstanding = 0;
|
||||
c->isconnected = 0;
|
||||
if (c->cleansession)
|
||||
MQTTCleanSession(c);
|
||||
}
|
||||
|
||||
|
||||
int cycle(MQTTClient* c, Timer* timer)
|
||||
{
|
||||
int len = 0,
|
||||
rc = SUCCESS;
|
||||
|
||||
int socket_stat = sock_get_errno(c->ipstack->my_socket);
|
||||
int socket_err = socket_error_is_fatal(socket_stat);
|
||||
if (socket_err == 1){
|
||||
return -2;
|
||||
}
|
||||
|
||||
int packet_type = readPacket(c, timer); /* read the socket, see what work is due */
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttRecvTask_0001, P_SIG, 1, ".....mqttRecvTask..packet_type=%d....",packet_type);
|
||||
////ECPLAT_PRINTF(UNILOG_DM1, cycle0, P_SIG, ".....autoReg..packet_type=%d ",packet_type);
|
||||
|
||||
switch (packet_type)
|
||||
{
|
||||
default:
|
||||
/* no more data to read, unrecoverable. Or read packet fails due to unexpected network error */
|
||||
rc = packet_type;
|
||||
break;
|
||||
case 0: /* timed out reading packet */
|
||||
break;
|
||||
case CONNACK:
|
||||
case PUBACK:
|
||||
case SUBACK:
|
||||
case UNSUBACK:
|
||||
if(packet_type == SUBACK)
|
||||
{
|
||||
//rc = packet_type;
|
||||
}
|
||||
break;
|
||||
case PUBLISH:
|
||||
{
|
||||
MQTTString topicName;
|
||||
MQTTMessage msg;
|
||||
int intQoS;
|
||||
msg.payloadlen = 0; /* this is a size_t, but deserialize publish sets this as int */
|
||||
if (MQTTDeserialize_publish(&msg.dup, &intQoS, &msg.retained, &msg.id, &topicName,
|
||||
(unsigned char**)&msg.payload, (int*)&msg.payloadlen, c->readbuf, c->readbuf_size) != 1)
|
||||
goto exit;
|
||||
msg.qos = (enum QoS)intQoS;
|
||||
deliverMessage(c, &topicName, &msg);
|
||||
if (msg.qos != QOS0)
|
||||
{
|
||||
if (msg.qos == QOS1)
|
||||
len = MQTTSerialize_ack(c->sendbuf, c->sendbuf_size, PUBACK, 0, msg.id);
|
||||
else if (msg.qos == QOS2)
|
||||
len = MQTTSerialize_ack(c->sendbuf, c->sendbuf_size, PUBREC, 0, msg.id);
|
||||
if (len <= 0)
|
||||
rc = FAILURE;
|
||||
else
|
||||
rc = sendPacket(c, len, timer);
|
||||
if (rc == FAILURE)
|
||||
goto exit; // there was a problem
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
{
|
||||
unsigned short mypacketid;
|
||||
unsigned char dup, type;
|
||||
if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
|
||||
rc = FAILURE;
|
||||
else if ((len = MQTTSerialize_ack(c->sendbuf, c->sendbuf_size,
|
||||
(packet_type == PUBREC) ? PUBREL : PUBCOMP, 0, mypacketid)) <= 0)
|
||||
rc = FAILURE;
|
||||
else if ((rc = sendPacket(c, len, timer)) != SUCCESS) // send the PUBREL packet
|
||||
rc = FAILURE; // there was a problem
|
||||
if (rc == FAILURE)
|
||||
goto exit; // there was a problem
|
||||
break;
|
||||
}
|
||||
|
||||
case PUBCOMP:
|
||||
break;
|
||||
case PINGRESP:
|
||||
c->ping_outstanding = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (keepalive(c) != SUCCESS) {
|
||||
int socket_stat = 0;
|
||||
mqttSendMsg mqttMsg;
|
||||
//check only keepalive FAILURE status so that previous FAILURE status can be considered as FAULT
|
||||
rc = FAILURE;
|
||||
socket_stat = sock_get_errno(c->ipstack->my_socket);
|
||||
if((socket_stat == MQTT_ERR_ABRT)||(socket_stat == MQTT_ERR_RST)||(socket_stat == MQTT_ERR_CLSD)||(socket_stat == MQTT_ERR_BADE))
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttRecvTask_0, P_INFO, 0, ".....now, need reconnect.......");
|
||||
/* send reconnect msg to send task */
|
||||
memset(&mqttMsg, 0, sizeof(mqttMsg));
|
||||
mqttMsg.cmdType = MQTT_DEMO_MSG_RECONNECT;
|
||||
|
||||
// xQueueSend(mqttSendMsgHandle, &mqttMsg, MQTT_MSG_TIMEOUT);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(c->mqtt_keepalive_retry_count>3)
|
||||
{
|
||||
c->mqtt_keepalive_retry_count = 0;
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttRecvTask_ee0, P_INFO, 0, ".....now, need reconnect.......");
|
||||
/* send reconnect msg to send task */
|
||||
memset(&mqttMsg, 0, sizeof(mqttMsg));
|
||||
mqttMsg.cmdType = MQTT_DEMO_MSG_RECONNECT;
|
||||
|
||||
// xQueueSend(mqttSendMsgHandle, &mqttMsg, MQTT_MSG_TIMEOUT);
|
||||
}
|
||||
else
|
||||
{
|
||||
keepaliveRetry(c);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
if (rc == SUCCESS)
|
||||
rc = packet_type;
|
||||
else if (c->isconnected)
|
||||
;//MQTTCloseSession(c);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTYield(MQTTClient* c, int timeout_ms)
|
||||
{
|
||||
int rc = SUCCESS;
|
||||
Timer timer;
|
||||
|
||||
TimerInit(&timer);
|
||||
TimerCountdownMS(&timer, timeout_ms);
|
||||
|
||||
do
|
||||
{
|
||||
if (cycle(c, &timer) < 0)
|
||||
{
|
||||
rc = FAILURE;
|
||||
break;
|
||||
}
|
||||
} while (!TimerIsExpired(&timer));
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTIsConnected(MQTTClient* client)
|
||||
{
|
||||
return client->isconnected;
|
||||
}
|
||||
|
||||
void MQTTRun(void* parm)
|
||||
{
|
||||
Timer timer;
|
||||
|
||||
MQTTClient* c = (MQTTClient*)parm;
|
||||
// if(mqttSendMsgHandle == NULL)
|
||||
// {
|
||||
// mqttSendMsgHandle = xQueueCreate(16, sizeof(mqttSendMsg));
|
||||
// }
|
||||
|
||||
|
||||
TimerInit(&timer);
|
||||
|
||||
while (1)
|
||||
{
|
||||
#if defined(MQTT_TASK)
|
||||
MutexLock(&c->mutex);
|
||||
#endif
|
||||
|
||||
TimerCountdownMS(&timer, 1500); /* Don't wait too long if no traffic is incoming */
|
||||
int rc = cycle(c, &timer);
|
||||
if (rc == -2){
|
||||
c->isconnected = 0;
|
||||
}
|
||||
#if defined(MQTT_TASK)
|
||||
MutexUnlock(&c->mutex);
|
||||
#endif
|
||||
osDelay(200);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
int MQTTStartTask(MQTTClient* client)
|
||||
{
|
||||
return ThreadStart(&client->thread, &MQTTRun, client);
|
||||
}
|
||||
#endif
|
||||
|
||||
int waitfor(MQTTClient* c, int packet_type, Timer* timer)
|
||||
{
|
||||
int rc = FAILURE;
|
||||
|
||||
do
|
||||
{
|
||||
if (TimerIsExpired(timer))
|
||||
break; // we timed out
|
||||
rc = cycle(c, timer);
|
||||
}
|
||||
while (rc != packet_type && rc >= 0);
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTConnectWithResults(MQTTClient* c, MQTTPacket_connectData* options, MQTTConnackData* data)
|
||||
{
|
||||
Timer connect_timer;
|
||||
int rc = FAILURE;
|
||||
MQTTPacket_connectData default_options = MQTTPacket_connectData_initializer;
|
||||
int len = 0;
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
MutexLock(&c->mutex);
|
||||
#endif
|
||||
if (c->isconnected) /* don't send connect packet again if we are already connected */
|
||||
goto exit;
|
||||
|
||||
TimerInit(&connect_timer);
|
||||
TimerCountdownMS(&connect_timer, c->command_timeout_ms);
|
||||
|
||||
if (options == 0)
|
||||
options = &default_options; /* set default options if none were supplied */
|
||||
|
||||
c->keepAliveInterval = options->keepAliveInterval;
|
||||
c->cleansession = options->cleansession;
|
||||
TimerCountdown(&c->last_received, c->keepAliveInterval);
|
||||
if ((len = MQTTSerialize_connect(c->sendbuf, c->sendbuf_size, options)) <= 0)
|
||||
goto exit;
|
||||
if ((rc = sendPacket(c, len, &connect_timer)) != SUCCESS) // send the connect packet
|
||||
goto exit; // there was a problem
|
||||
// this will be a blocking call, wait for the connack
|
||||
if (waitfor(c, CONNACK, &connect_timer) == CONNACK)
|
||||
{
|
||||
data->rc = 0;
|
||||
data->sessionPresent = 0;
|
||||
if ( MQTTDeserialize_connack(&data->sessionPresent, &data->rc, c->readbuf, c->readbuf_size) == 1)
|
||||
rc = data->rc;
|
||||
else
|
||||
rc = FAILURE;
|
||||
}
|
||||
else
|
||||
rc = FAILURE;
|
||||
exit:
|
||||
if (rc == SUCCESS)
|
||||
{
|
||||
c->isconnected = 1;
|
||||
c->ping_outstanding = 0;
|
||||
}
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
MutexUnlock(&c->mutex);
|
||||
#endif
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTReConnect(MQTTClient* client, MQTTPacket_connectData* connectData)
|
||||
{
|
||||
int ret = FAILURE;
|
||||
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttSendTask_13hh0, P_INFO, 0, "...start tcp disconnect ..");
|
||||
client->ipstack->disconnect(client->ipstack);
|
||||
|
||||
client->isconnected = 0;
|
||||
if((NetworkConnect(client->ipstack, client->ipstack->addr, client->ipstack->port)) < 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("NetworkConnect fail");
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttSendTask_17hh4, P_INFO, 0, "...tcp reconnect fail!!!...\r\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttSendTask_18hh5, P_INFO, 0, "...start mqtt connect ..");
|
||||
if ((MQTTConnect(client, connectData)) != 0)
|
||||
{
|
||||
LUAT_DEBUG_PRINT("MQTTConnect fail");
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttSendTask_19hh6, P_INFO, 0, "...mqtt reconnect fial!!!...");
|
||||
}
|
||||
else
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttSendTask_20hh7, P_INFO, 0, "...mqtt reconnect ok!!!...");
|
||||
ret = SUCCESS;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int MQTTConnect(MQTTClient* c, MQTTPacket_connectData* options)
|
||||
{
|
||||
MQTTConnackData data;
|
||||
return MQTTConnectWithResults(c, options, &data);
|
||||
}
|
||||
|
||||
int MQTTSetMessageHandler(MQTTClient* c, const char* topicFilter, messageHandler messageHandler)
|
||||
{
|
||||
int rc = FAILURE;
|
||||
int i = -1;
|
||||
|
||||
/* first check for an existing matching slot */
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
|
||||
{
|
||||
if (c->messageHandlers[i].topicFilter != NULL && strcmp(c->messageHandlers[i].topicFilter, topicFilter) == 0)
|
||||
{
|
||||
if (messageHandler == NULL) /* remove existing */
|
||||
{
|
||||
c->messageHandlers[i].topicFilter = NULL;
|
||||
c->messageHandlers[i].fp = NULL;
|
||||
}
|
||||
rc = SUCCESS; /* return i when adding new subscription */
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* if no existing, look for empty slot (unless we are removing) */
|
||||
if (messageHandler != NULL) {
|
||||
if (rc == FAILURE)
|
||||
{
|
||||
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i)
|
||||
{
|
||||
if (c->messageHandlers[i].topicFilter == NULL)
|
||||
{
|
||||
rc = SUCCESS;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (i < MAX_MESSAGE_HANDLERS)
|
||||
{
|
||||
c->messageHandlers[i].topicFilter = topicFilter;
|
||||
c->messageHandlers[i].fp = messageHandler;
|
||||
}
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTSubscribeWithResults(MQTTClient* c, const char* topicFilter, enum QoS qos,
|
||||
messageHandler messageHandler, MQTTSubackData* data)
|
||||
{
|
||||
int rc = FAILURE;
|
||||
Timer timer;
|
||||
int len = 0;
|
||||
int mqttQos = (int)qos;
|
||||
MQTTString topic = MQTTString_initializer;
|
||||
topic.cstring = (char *)topicFilter;
|
||||
#if defined(MQTT_TASK)
|
||||
MutexLock(&c->mutex);
|
||||
#endif
|
||||
if (!c->isconnected)
|
||||
goto exit;
|
||||
TimerInit(&timer);
|
||||
TimerCountdownMS(&timer, c->command_timeout_ms);
|
||||
len = MQTTSerialize_subscribe(c->sendbuf, c->sendbuf_size, 0, getNextPacketId(c), 1, &topic, (int*)&mqttQos);
|
||||
if (len <= 0)
|
||||
goto exit;
|
||||
|
||||
if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
|
||||
goto exit; // there was a problem
|
||||
|
||||
if (waitfor(c, SUBACK, &timer) == SUBACK) // wait for suback
|
||||
{
|
||||
int count = 0;
|
||||
unsigned short mypacketid;
|
||||
//data->grantedQoS = QOS0;
|
||||
mqttQos = QOS0;
|
||||
if (MQTTDeserialize_suback(&mypacketid, 1, &count, (int*)&mqttQos, c->readbuf, c->readbuf_size) == 1)
|
||||
{
|
||||
if (mqttQos != 0x80){
|
||||
rc = MQTTSetMessageHandler(c, topicFilter, messageHandler);
|
||||
}else{
|
||||
rc = FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
rc = FAILURE;
|
||||
DBG("tick5:%llu ",soc_get_poweron_time_tick());
|
||||
exit:
|
||||
if (rc == FAILURE)
|
||||
;//MQTTCloseSession(c);
|
||||
#if defined(MQTT_TASK)
|
||||
MutexUnlock(&c->mutex);
|
||||
#endif
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTSubscribe(MQTTClient* c, const char* topicFilter, enum QoS qos,
|
||||
messageHandler messageHandler)
|
||||
{
|
||||
MQTTSubackData data;
|
||||
return MQTTSubscribeWithResults(c, topicFilter, qos, messageHandler, &data);
|
||||
}
|
||||
|
||||
int MQTTUnsubscribe(MQTTClient* c, const char* topicFilter)
|
||||
{
|
||||
int rc = FAILURE;
|
||||
Timer timer;
|
||||
MQTTString topic = MQTTString_initializer;
|
||||
topic.cstring = (char *)topicFilter;
|
||||
int len = 0;
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
MutexLock(&c->mutex);
|
||||
#endif
|
||||
if (!c->isconnected)
|
||||
goto exit;
|
||||
|
||||
TimerInit(&timer);
|
||||
TimerCountdownMS(&timer, c->command_timeout_ms);
|
||||
|
||||
if ((len = MQTTSerialize_unsubscribe(c->sendbuf, c->sendbuf_size, 0, getNextPacketId(c), 1, &topic)) <= 0)
|
||||
goto exit;
|
||||
if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
|
||||
goto exit; // there was a problem
|
||||
|
||||
if (waitfor(c, UNSUBACK, &timer) == UNSUBACK)
|
||||
{
|
||||
unsigned short mypacketid; // should be the same as the packetid above
|
||||
if (MQTTDeserialize_unsuback(&mypacketid, c->readbuf, c->readbuf_size) == 1)
|
||||
{
|
||||
/* remove the subscription message handler associated with this topic, if there is one */
|
||||
MQTTSetMessageHandler(c, topicFilter, NULL);
|
||||
}
|
||||
}
|
||||
else
|
||||
rc = FAILURE;
|
||||
|
||||
exit:
|
||||
if (rc == FAILURE)
|
||||
;//MQTTCloseSession(c);
|
||||
#if defined(MQTT_TASK)
|
||||
MutexUnlock(&c->mutex);
|
||||
#endif
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTPublish(MQTTClient* c, const char* topicName, MQTTMessage* message)
|
||||
{
|
||||
int rc = FAILURE;
|
||||
Timer timer;
|
||||
MQTTString topic = MQTTString_initializer;
|
||||
topic.cstring = (char *)topicName;
|
||||
int len = 0;
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
MutexLock(&c->mutex);
|
||||
#endif
|
||||
if (!c->isconnected)
|
||||
goto exit;
|
||||
|
||||
TimerInit(&timer);
|
||||
TimerCountdownMS(&timer, c->command_timeout_ms);
|
||||
|
||||
if (message->qos == QOS1 || message->qos == QOS2)
|
||||
message->id = getNextPacketId(c);
|
||||
|
||||
len = MQTTSerialize_publish(c->sendbuf, c->sendbuf_size, 0, message->qos, message->retained, message->id,
|
||||
topic, (unsigned char*)message->payload, message->payloadlen);
|
||||
if (len <= 0)
|
||||
goto exit;
|
||||
if ((rc = sendPacket(c, len, &timer)) != SUCCESS) // send the subscribe packet
|
||||
goto exit; // there was a problem
|
||||
|
||||
if (message->qos == QOS1)
|
||||
{
|
||||
if (waitfor(c, PUBACK, &timer) == PUBACK)
|
||||
{
|
||||
unsigned short mypacketid;
|
||||
unsigned char dup, type;
|
||||
if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
|
||||
rc = FAILURE;
|
||||
}
|
||||
else
|
||||
rc = FAILURE;
|
||||
}
|
||||
else if (message->qos == QOS2)
|
||||
{
|
||||
if (waitfor(c, PUBCOMP, &timer) == PUBCOMP)
|
||||
{
|
||||
unsigned short mypacketid;
|
||||
unsigned char dup, type;
|
||||
if (MQTTDeserialize_ack(&type, &dup, &mypacketid, c->readbuf, c->readbuf_size) != 1)
|
||||
rc = FAILURE;
|
||||
}
|
||||
else
|
||||
rc = FAILURE;
|
||||
}
|
||||
|
||||
exit:
|
||||
if (rc == FAILURE)
|
||||
;//MQTTCloseSession(c);
|
||||
#if defined(MQTT_TASK)
|
||||
MutexUnlock(&c->mutex);
|
||||
#endif
|
||||
return rc;
|
||||
}
|
||||
|
||||
int MQTTDisconnect(MQTTClient* c)
|
||||
{
|
||||
int rc = FAILURE;
|
||||
Timer timer; // we might wait for incomplete incoming publishes to complete
|
||||
int len = 0;
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
MutexLock(&c->mutex);
|
||||
#endif
|
||||
TimerInit(&timer);
|
||||
TimerCountdownMS(&timer, c->command_timeout_ms);
|
||||
|
||||
len = MQTTSerialize_disconnect(c->sendbuf, c->sendbuf_size);
|
||||
if (len > 0)
|
||||
rc = sendPacket(c, len, &timer); // send the disconnect packet
|
||||
MQTTCloseSession(c);
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
MutexUnlock(&c->mutex);
|
||||
#endif
|
||||
return rc;
|
||||
}
|
||||
|
||||
int mqtt_connect(MQTTClient* c,Network *n,char *serverAddr, int port, MQTTPacket_connectData* connData)
|
||||
{
|
||||
unsigned char * mqttSendbuf = malloc(MQTT_SEND_BUFF_LEN);
|
||||
unsigned char * mqttReadbuf = malloc(MQTT_RECV_BUFF_LEN);
|
||||
memset(mqttSendbuf, 0, MQTT_SEND_BUFF_LEN);
|
||||
memset(mqttReadbuf, 0, MQTT_RECV_BUFF_LEN);
|
||||
|
||||
NetworkInit(n);
|
||||
MQTTClientInit(c, n, 40000, mqttSendbuf, MQTT_SEND_BUFF_LEN, mqttReadbuf, MQTT_RECV_BUFF_LEN);
|
||||
|
||||
if ((NetworkConnect(n, serverAddr, port)) != 0){
|
||||
c->keepAliveInterval = connData->keepAliveInterval;
|
||||
c->ping_outstanding = 1;
|
||||
return 1;
|
||||
}else{
|
||||
if ((MQTTConnect(c, connData)) != 0){
|
||||
c->ping_outstanding = 1;
|
||||
return 1;
|
||||
}else{
|
||||
#if defined(MQTT_TASK)
|
||||
if ((MQTTStartTask(c)) != pdPASS){
|
||||
return 1;
|
||||
}else{
|
||||
return 0;
|
||||
}
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
289
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/MQTTClient.h
vendored
Normal file
289
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/MQTTClient.h
vendored
Normal file
@@ -0,0 +1,289 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2017 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - documentation and platform specific header
|
||||
* Ian Craggs - add setMessageHandler function
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(MQTT_CLIENT_H)
|
||||
#define MQTT_CLIENT_H
|
||||
|
||||
#if defined(__cplusplus)
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(WIN32_DLL) || defined(WIN64_DLL)
|
||||
#define DLLImport __declspec(dllimport)
|
||||
#define DLLExport __declspec(dllexport)
|
||||
#elif defined(LINUX_SO)
|
||||
#define DLLImport extern
|
||||
#define DLLExport __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define DLLImport
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "MQTTFreeRTOS.h"
|
||||
|
||||
#if defined(MQTTCLIENT_PLATFORM_HEADER)
|
||||
/* The following sequence of macros converts the MQTTCLIENT_PLATFORM_HEADER value
|
||||
* into a string constant suitable for use with include.
|
||||
*/
|
||||
#define xstr(s) str(s)
|
||||
#define str(s) #s
|
||||
#include xstr(MQTTCLIENT_PLATFORM_HEADER)
|
||||
#endif
|
||||
|
||||
#define MAX_PACKET_ID 65535 /* according to the MQTT specification - do not change! */
|
||||
|
||||
#if !defined(MAX_MESSAGE_HANDLERS)
|
||||
#define MAX_MESSAGE_HANDLERS 5 /* redefinable - how many subscriptions do you want? */
|
||||
#endif
|
||||
|
||||
enum QoS { QOS0, QOS1, QOS2, SUBFAIL=0x80 };
|
||||
|
||||
/* all failure return codes must be negative */
|
||||
enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESS = 0 };
|
||||
|
||||
/* The Platform specific header must define the Network and Timer structures and functions
|
||||
* which operate on them.
|
||||
*
|
||||
typedef struct Network
|
||||
{
|
||||
int (*mqttread)(Network*, unsigned char* read_buffer, int, int);
|
||||
int (*mqttwrite)(Network*, unsigned char* send_buffer, int, int);
|
||||
} Network;*/
|
||||
|
||||
/* The Timer structure must be defined in the platform specific header,
|
||||
* and have the following functions to operate on it. */
|
||||
extern void TimerInit(Timer*);
|
||||
extern char TimerIsExpired(Timer*);
|
||||
extern void TimerCountdownMS(Timer*, unsigned int);
|
||||
extern void TimerCountdown(Timer*, unsigned int);
|
||||
extern int TimerLeftMS(Timer*);
|
||||
|
||||
typedef struct MQTTMessage
|
||||
{
|
||||
enum QoS qos;
|
||||
unsigned char retained;
|
||||
unsigned char dup;
|
||||
unsigned short id;
|
||||
void *payload;
|
||||
size_t payloadlen;
|
||||
} MQTTMessage;
|
||||
|
||||
typedef struct MessageData
|
||||
{
|
||||
MQTTMessage* message;
|
||||
MQTTString* topicName;
|
||||
} MessageData;
|
||||
|
||||
typedef struct MQTTConnackData
|
||||
{
|
||||
unsigned char rc;
|
||||
unsigned char sessionPresent;
|
||||
} MQTTConnackData;
|
||||
|
||||
typedef struct MQTTSubackData
|
||||
{
|
||||
enum QoS grantedQoS;
|
||||
} MQTTSubackData;
|
||||
|
||||
typedef void (*messageHandler)(MessageData*);
|
||||
typedef struct MQTTClient
|
||||
{
|
||||
unsigned int next_packetid,command_timeout_ms;
|
||||
size_t sendbuf_size,readbuf_size;
|
||||
unsigned char *sendbuf,*readbuf;
|
||||
unsigned int keepAliveInterval;
|
||||
int mqtt_keepalive_retry_count;
|
||||
char ping_outstanding;
|
||||
int isconnected;
|
||||
int cleansession;
|
||||
struct MessageHandlers{
|
||||
const char* topicFilter;
|
||||
void (*fp) (MessageData*);
|
||||
} messageHandlers[MAX_MESSAGE_HANDLERS]; /* Message handlers are indexed by subscription topic */
|
||||
|
||||
void (*defaultMessageHandler) (MessageData*);
|
||||
|
||||
Network* ipstack;
|
||||
Timer last_sent, last_received;
|
||||
#if defined(MQTT_TASK)
|
||||
Mutex mutex;
|
||||
Thread thread;
|
||||
#endif
|
||||
} MQTTClient;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int cmdType;
|
||||
char *topic;
|
||||
int topicLen;
|
||||
MQTTMessage message;
|
||||
}mqttSendMsg;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int cmdType;
|
||||
int ret;
|
||||
char *data;
|
||||
int dataLen;
|
||||
}mqttDataMsg;
|
||||
enum MQTT_MSG_CMD_
|
||||
{
|
||||
MQTT_DEMO_MSG_PUBLISH = 1,
|
||||
MQTT_DEMO_MSG_PUBLISH_ACK = 2,
|
||||
MQTT_DEMO_MSG_SUB,
|
||||
MQTT_DEMO_MSG_UNSUB,
|
||||
MQTT_DEMO_MSG_RECONNECT,
|
||||
};
|
||||
|
||||
#define DefaultClient {0, 0, 0, 0, NULL, NULL, 0, 0, 0}
|
||||
|
||||
#define ALI_SHA1_KEY_IOPAD_SIZE (64)
|
||||
#define ALI_SHA1_DIGEST_SIZE (20)
|
||||
|
||||
#define ALI_SHA256_KEY_IOPAD_SIZE (64)
|
||||
#define ALI_SHA256_DIGEST_SIZE (32)
|
||||
|
||||
#define ALI_MD5_KEY_IOPAD_SIZE (64)
|
||||
#define ALI_MD5_DIGEST_SIZE (16)
|
||||
|
||||
#define ALI_HMAC_USED (1)
|
||||
#define ALI_HMAC_NOT_USED (0)
|
||||
|
||||
#define MQTT_DEMO_TASK_STACK_SIZE 2048
|
||||
|
||||
#define MQTT_SEND_BUFF_LEN (1024)
|
||||
#define MQTT_RECV_BUFF_LEN (1024)
|
||||
|
||||
#define MQTT_SERVER_URI "183.230.40.39"
|
||||
|
||||
#define MQTT_SERVER_PORT (6002)
|
||||
#define MQTT_SERVER_TOPIC_0 "$dp"
|
||||
#define MQTT_SERVER_TOPIC_1 "$dp"
|
||||
#define MQTT_SERVER_TOPIC_2 "$dp"
|
||||
|
||||
#define MQTT_MSG_TIMEOUT 1000
|
||||
|
||||
#define MQTT_ERR_ABRT (-13)
|
||||
#define MQTT_ERR_RST (-14)
|
||||
#define MQTT_ERR_CLSD (-15)
|
||||
#define MQTT_ERR_BADE (9)
|
||||
|
||||
#define MQTT_SEND_TIMEOUT 5000
|
||||
#define MQTT_RECV_TIMEOUT 5000
|
||||
|
||||
/**
|
||||
* Create an MQTT client object
|
||||
* @param client
|
||||
* @param network
|
||||
* @param command_timeout_ms
|
||||
* @param
|
||||
*/
|
||||
DLLExport void MQTTClientInit(MQTTClient* client, Network* network, unsigned int command_timeout_ms,
|
||||
unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size);
|
||||
|
||||
/** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
|
||||
* The nework object must be connected to the network endpoint before calling this
|
||||
* @param options - connect options
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTConnectWithResults(MQTTClient* client, MQTTPacket_connectData* options,
|
||||
MQTTConnackData* data);
|
||||
|
||||
/** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
|
||||
* The nework object must be connected to the network endpoint before calling this
|
||||
* @param options - connect options
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTConnect(MQTTClient* client, MQTTPacket_connectData* options);
|
||||
|
||||
/** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
|
||||
* @param client - the client object to use
|
||||
* @param topic - the topic to publish to
|
||||
* @param message - the message to send
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTPublish(MQTTClient* client, const char*, MQTTMessage*);
|
||||
|
||||
/** MQTT SetMessageHandler - set or remove a per topic message handler
|
||||
* @param client - the client object to use
|
||||
* @param topicFilter - the topic filter set the message handler for
|
||||
* @param messageHandler - pointer to the message handler function or NULL to remove
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTSetMessageHandler(MQTTClient* c, const char* topicFilter, messageHandler messageHandler);
|
||||
|
||||
/** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning.
|
||||
* @param client - the client object to use
|
||||
* @param topicFilter - the topic filter to subscribe to
|
||||
* @param message - the message to send
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTSubscribe(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler);
|
||||
|
||||
/** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning.
|
||||
* @param client - the client object to use
|
||||
* @param topicFilter - the topic filter to subscribe to
|
||||
* @param message - the message to send
|
||||
* @param data - suback granted QoS returned
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTSubscribeWithResults(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler, MQTTSubackData* data);
|
||||
|
||||
/** MQTT Subscribe - send an MQTT unsubscribe packet and wait for unsuback before returning.
|
||||
* @param client - the client object to use
|
||||
* @param topicFilter - the topic filter to unsubscribe from
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTUnsubscribe(MQTTClient* client, const char* topicFilter);
|
||||
|
||||
/** MQTT Disconnect - send an MQTT disconnect packet and close the connection
|
||||
* @param client - the client object to use
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTDisconnect(MQTTClient* client);
|
||||
|
||||
/** MQTT Yield - MQTT background
|
||||
* @param client - the client object to use
|
||||
* @param time - the time, in milliseconds, to yield for
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTYield(MQTTClient* client, int time);
|
||||
|
||||
/** MQTT isConnected
|
||||
* @param client - the client object to use
|
||||
* @return truth value indicating whether the client is connected to the server
|
||||
*/
|
||||
DLLExport int MQTTIsConnected(MQTTClient* client);
|
||||
|
||||
#if defined(MQTT_TASK)
|
||||
/** MQTT start background thread for a client. After this, MQTTYield should not be called.
|
||||
* @param client - the client object to use
|
||||
* @return success code
|
||||
*/
|
||||
DLLExport int MQTTStartTask(MQTTClient* client);
|
||||
DLLExport int MQTTStopTask(MQTTClient* client);
|
||||
#endif
|
||||
|
||||
int mqtt_connect(MQTTClient* c,Network *n, char *serverAddr, int port, MQTTPacket_connectData* connData);
|
||||
|
||||
#if defined(__cplusplus)
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
196
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/cc3200/MQTTCC3200.c
vendored
Normal file
196
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/cc3200/MQTTCC3200.c
vendored
Normal file
@@ -0,0 +1,196 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTCC3200.h"
|
||||
|
||||
unsigned long MilliTimer;
|
||||
|
||||
void SysTickIntHandler(void) {
|
||||
MilliTimer++;
|
||||
}
|
||||
|
||||
char expired(Timer* timer) {
|
||||
long left = timer->end_time - MilliTimer;
|
||||
return (left < 0);
|
||||
}
|
||||
|
||||
|
||||
void countdown_ms(Timer* timer, unsigned int timeout) {
|
||||
timer->end_time = MilliTimer + timeout;
|
||||
}
|
||||
|
||||
|
||||
void countdown(Timer* timer, unsigned int timeout) {
|
||||
timer->end_time = MilliTimer + (timeout * 1000);
|
||||
}
|
||||
|
||||
|
||||
int left_ms(Timer* timer) {
|
||||
long left = timer->end_time - MilliTimer;
|
||||
return (left < 0) ? 0 : left;
|
||||
}
|
||||
|
||||
|
||||
void InitTimer(Timer* timer) {
|
||||
timer->end_time = 0;
|
||||
}
|
||||
|
||||
|
||||
int cc3200_read(Network* n, unsigned char* buffer, int len, int timeout_ms) {
|
||||
SlTimeval_t timeVal;
|
||||
SlFdSet_t fdset;
|
||||
int rc = 0;
|
||||
int recvLen = 0;
|
||||
|
||||
SL_FD_ZERO(&fdset);
|
||||
SL_FD_SET(n->my_socket, &fdset);
|
||||
|
||||
timeVal.tv_sec = 0;
|
||||
timeVal.tv_usec = timeout_ms * 1000;
|
||||
if (sl_Select(n->my_socket + 1, &fdset, NULL, NULL, &timeVal) == 1) {
|
||||
do {
|
||||
rc = sl_Recv(n->my_socket, buffer + recvLen, len - recvLen, 0);
|
||||
recvLen += rc;
|
||||
} while(recvLen < len);
|
||||
}
|
||||
return recvLen;
|
||||
}
|
||||
|
||||
|
||||
int cc3200_write(Network* n, unsigned char* buffer, int len, int timeout_ms) {
|
||||
SlTimeval_t timeVal;
|
||||
SlFdSet_t fdset;
|
||||
int rc = 0;
|
||||
int readySock;
|
||||
|
||||
SL_FD_ZERO(&fdset);
|
||||
SL_FD_SET(n->my_socket, &fdset);
|
||||
|
||||
timeVal.tv_sec = 0;
|
||||
timeVal.tv_usec = timeout_ms * 1000;
|
||||
do {
|
||||
readySock = sl_Select(n->my_socket + 1, NULL, &fdset, NULL, &timeVal);
|
||||
} while(readySock != 1);
|
||||
rc = sl_Send(n->my_socket, buffer, len, 0);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
void cc3200_disconnect(Network* n) {
|
||||
sl_Close(n->my_socket);
|
||||
}
|
||||
|
||||
|
||||
void NewNetwork(Network* n) {
|
||||
n->my_socket = 0;
|
||||
n->mqttread = cc3200_read;
|
||||
n->mqttwrite = cc3200_write;
|
||||
n->disconnect = cc3200_disconnect;
|
||||
}
|
||||
|
||||
int TLSConnectNetwork(Network *n, char* addr, int port, SlSockSecureFiles_t* certificates, unsigned char sec_method, unsigned int cipher, char server_verify) {
|
||||
SlSockAddrIn_t sAddr;
|
||||
int addrSize;
|
||||
int retVal;
|
||||
unsigned long ipAddress;
|
||||
|
||||
retVal = sl_NetAppDnsGetHostByName(addr, strlen(addr), &ipAddress, AF_INET);
|
||||
if (retVal < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
sAddr.sin_family = AF_INET;
|
||||
sAddr.sin_port = sl_Htons((unsigned short)port);
|
||||
sAddr.sin_addr.s_addr = sl_Htonl(ipAddress);
|
||||
|
||||
addrSize = sizeof(SlSockAddrIn_t);
|
||||
|
||||
n->my_socket = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, SL_SEC_SOCKET);
|
||||
if (n->my_socket < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
SlSockSecureMethod method;
|
||||
method.secureMethod = sec_method;
|
||||
retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECMETHOD, &method, sizeof(method));
|
||||
if (retVal < 0) {
|
||||
return retVal;
|
||||
}
|
||||
|
||||
SlSockSecureMask mask;
|
||||
mask.secureMask = cipher;
|
||||
retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECURE_MASK, &mask, sizeof(mask));
|
||||
if (retVal < 0) {
|
||||
return retVal;
|
||||
}
|
||||
|
||||
if (certificates != NULL) {
|
||||
retVal = sl_SetSockOpt(n->my_socket, SL_SOL_SOCKET, SL_SO_SECURE_FILES, certificates->secureFiles, sizeof(SlSockSecureFiles_t));
|
||||
if(retVal < 0)
|
||||
{
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
|
||||
retVal = sl_Connect(n->my_socket, ( SlSockAddr_t *)&sAddr, addrSize);
|
||||
if( retVal < 0 ) {
|
||||
if (server_verify || retVal != -453) {
|
||||
sl_Close(n->my_socket);
|
||||
return retVal;
|
||||
}
|
||||
}
|
||||
|
||||
SysTickIntRegister(SysTickIntHandler);
|
||||
SysTickPeriodSet(80000);
|
||||
SysTickEnable();
|
||||
|
||||
return retVal;
|
||||
}
|
||||
|
||||
int ConnectNetwork(Network* n, char* addr, int port)
|
||||
{
|
||||
SlSockAddrIn_t sAddr;
|
||||
int addrSize;
|
||||
int retVal;
|
||||
unsigned long ipAddress;
|
||||
|
||||
sl_NetAppDnsGetHostByName(addr, strlen(addr), &ipAddress, AF_INET);
|
||||
|
||||
sAddr.sin_family = AF_INET;
|
||||
sAddr.sin_port = sl_Htons((unsigned short)port);
|
||||
sAddr.sin_addr.s_addr = sl_Htonl(ipAddress);
|
||||
|
||||
addrSize = sizeof(SlSockAddrIn_t);
|
||||
|
||||
n->my_socket = sl_Socket(SL_AF_INET,SL_SOCK_STREAM, 0);
|
||||
if( n->my_socket < 0 ) {
|
||||
// error
|
||||
return -1;
|
||||
}
|
||||
|
||||
retVal = sl_Connect(n->my_socket, ( SlSockAddr_t *)&sAddr, addrSize);
|
||||
if( retVal < 0 ) {
|
||||
// error
|
||||
sl_Close(n->my_socket);
|
||||
return retVal;
|
||||
}
|
||||
|
||||
SysTickIntRegister(SysTickIntHandler);
|
||||
SysTickPeriodSet(80000);
|
||||
SysTickEnable();
|
||||
|
||||
return retVal;
|
||||
}
|
||||
58
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/cc3200/MQTTCC3200.h
vendored
Normal file
58
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/cc3200/MQTTCC3200.h
vendored
Normal file
@@ -0,0 +1,58 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __MQTT_CC3200_
|
||||
#define __MQTT_CC3200_
|
||||
|
||||
#include "simplelink.h"
|
||||
#include "netapp.h"
|
||||
#include "socket.h"
|
||||
#include "hw_types.h"
|
||||
#include "systick.h"
|
||||
|
||||
typedef struct Timer Timer;
|
||||
|
||||
struct Timer {
|
||||
unsigned long systick_period;
|
||||
unsigned long end_time;
|
||||
};
|
||||
|
||||
typedef struct Network Network;
|
||||
|
||||
struct Network
|
||||
{
|
||||
int my_socket;
|
||||
int (*mqttread) (Network*, unsigned char*, int, int);
|
||||
int (*mqttwrite) (Network*, unsigned char*, int, int);
|
||||
void (*disconnect) (Network*);
|
||||
};
|
||||
|
||||
char expired(Timer*);
|
||||
void countdown_ms(Timer*, unsigned int);
|
||||
void countdown(Timer*, unsigned int);
|
||||
int left_ms(Timer*);
|
||||
|
||||
void InitTimer(Timer*);
|
||||
|
||||
int cc3200_read(Network*, unsigned char*, int, int);
|
||||
int cc3200_write(Network*, unsigned char*, int, int);
|
||||
void cc3200_disconnect(Network*);
|
||||
void NewNetwork(Network*);
|
||||
|
||||
int ConnectNetwork(Network*, char*, int);
|
||||
int TLSConnectNetwork(Network*, char*, int, SlSockSecureFiles_t*, unsigned char, unsigned int, char);
|
||||
|
||||
#endif
|
||||
602
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/eigencomm/MQTTTls.c
vendored
Normal file
602
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/eigencomm/MQTTTls.c
vendored
Normal file
@@ -0,0 +1,602 @@
|
||||
|
||||
#ifdef FEATURE_MBEDTLS_ENABLE
|
||||
#include "sha1.h"
|
||||
#include "sha256.h"
|
||||
#include "md5.h"
|
||||
#endif
|
||||
|
||||
#include "MQTTTls.h"
|
||||
#include "MQTTFreeRTOS.h"
|
||||
#include "error.h"
|
||||
|
||||
|
||||
int mqttSslRandom(void *p_rng, unsigned char *output, size_t output_len)
|
||||
{
|
||||
uint32_t rnglen = output_len;
|
||||
uint8_t rngoffset = 0;
|
||||
|
||||
while (rnglen > 0)
|
||||
{
|
||||
*(output + rngoffset) = (unsigned char)rand();
|
||||
rngoffset++;
|
||||
rnglen--;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void mqttSslDebug(void *ctx, int level, const char *file, int line, const char *str)
|
||||
{
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTls_00, P_INFO, "%s(%d):%s", file, line, str);
|
||||
|
||||
// DBG("%s", str);
|
||||
}
|
||||
|
||||
int mqttSslNonblockRecv(void *netContext, UINT8 *buf, size_t len)
|
||||
{
|
||||
int ret;
|
||||
int fd = ((mbedtls_net_context *)netContext)->fd;
|
||||
|
||||
if(fd < 0)
|
||||
{
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
ret = (int)recv(fd, buf, len, MSG_DONTWAIT);
|
||||
if(ret<0)
|
||||
{
|
||||
if( errno == EPIPE || errno == ECONNRESET)
|
||||
{
|
||||
return (MBEDTLS_ERR_NET_CONN_RESET);
|
||||
}
|
||||
|
||||
if( errno == EINTR )
|
||||
{
|
||||
return (MBEDTLS_ERR_SSL_WANT_READ);
|
||||
}
|
||||
|
||||
if(ret == -1 && errno == EWOULDBLOCK)
|
||||
{
|
||||
return ret;
|
||||
}
|
||||
return (MBEDTLS_ERR_NET_RECV_FAILED);
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
extern void mbedtls_debug_set_threshold( int threshold );
|
||||
|
||||
int mqttSslConn_new(mqttsClientContext* context, char* host)
|
||||
{
|
||||
int value;
|
||||
mqttsClientSsl *ssl;
|
||||
const char *custom = "mqtts";
|
||||
char port[10] = {0};
|
||||
int authmode = MBEDTLS_SSL_VERIFY_NONE;
|
||||
uint32_t flag;
|
||||
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_2, P_INFO, "before ssl context malloc:%d", xBytesTaskMalloced);
|
||||
context->ssl = malloc(sizeof(mqttsClientSsl));
|
||||
ssl = context->ssl;
|
||||
|
||||
/*
|
||||
* 0. Initialize the RNG and the session data
|
||||
*/
|
||||
#if defined(MBEDTLS_DEBUG_C)
|
||||
mbedtls_debug_set_threshold((int)2);
|
||||
#endif
|
||||
mbedtls_net_init(&ssl->netContext);
|
||||
mbedtls_ssl_init(&ssl->sslContext);
|
||||
mbedtls_ssl_config_init(&ssl->sslConfig);
|
||||
mbedtls_x509_crt_init(&ssl->caCert);
|
||||
mbedtls_x509_crt_init(&ssl->clientCert);
|
||||
mbedtls_pk_init(&ssl->pkContext);
|
||||
mbedtls_ctr_drbg_init(&ssl->ctrDrbgContext);
|
||||
mbedtls_entropy_init(&ssl->entropyContext);
|
||||
|
||||
if((value = mbedtls_ctr_drbg_seed(&ssl->ctrDrbgContext,
|
||||
mbedtls_entropy_func,
|
||||
&ssl->entropyContext,
|
||||
(const unsigned char*)custom,
|
||||
strlen(custom))) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_0, P_INFO, "mbedtls_ctr_drbg_seed failed, value:-0x%x.", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_3, P_INFO, "after ssl init:%d", xBytesTaskMalloced);
|
||||
/*
|
||||
* 0. Initialize certificates
|
||||
*/
|
||||
if(context->seclevel != 0){
|
||||
if (NULL != context->caCert) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_1, P_INFO, "STEP 0. Loading the CA root certificate ...");
|
||||
authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
||||
if (0 != (value = mbedtls_x509_crt_parse(&(ssl->caCert), (const unsigned char *)context->caCert, context->caCertLen))) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_2, P_INFO, "failed ! value:-0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
}
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_4, P_INFO, "after ca cert parse:%d", xBytesTaskMalloced);
|
||||
/* Setup Client Cert/Key */
|
||||
if(context->seclevel == 2){
|
||||
if (context->clientCert != NULL && context->clientPk != NULL) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_3, P_INFO, "STEP 0. start prepare client cert ...");
|
||||
value = mbedtls_x509_crt_parse(&(ssl->clientCert), (const unsigned char *) context->clientCert, context->clientCertLen);
|
||||
if (value != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_4, P_INFO, "failed! mbedtls_x509_crt_parse returned -0x%x\n", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_5, P_INFO, "context->clientPkLen=%d", context->clientPkLen);
|
||||
|
||||
|
||||
value = mbedtls_pk_parse_key(&ssl->pkContext, (const unsigned char *) context->clientPk, context->clientPkLen, NULL, 0);
|
||||
|
||||
if (value != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_6, P_INFO, "failed ! mbedtls_pk_parse_key returned -0x%x\n", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(context->seclevel == 0){
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_7, P_INFO, "user set verify none");
|
||||
authmode = MBEDTLS_SSL_VERIFY_NONE;
|
||||
}
|
||||
//ali mqtts is psk tls
|
||||
if((context->psk_key != NULL)&&(context->psk_identity != NULL))
|
||||
{
|
||||
mbedtls_ssl_conf_psk(&ssl->sslConfig, (const unsigned char *)context->psk_key, strlen(context->psk_key),
|
||||
(const unsigned char *)context->psk_identity, strlen(context->psk_identity));
|
||||
}
|
||||
|
||||
/*
|
||||
* 1. Start the connection
|
||||
*/
|
||||
snprintf(port, sizeof(port), "%d", context->port);
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_8_0, P_INFO, "STEP 1. host:%s", host);
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_8_1, P_INFO, "STEP 1. Connecting to PORT:%d",context->port);
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_8_2, P_INFO, "STEP 1. port:%s", port);
|
||||
if (0 != (value = mbedtls_net_connect(&ssl->netContext, host, port, MBEDTLS_NET_PROTO_TCP, LWIP_PS_INVALID_CID))) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_9, P_INFO, " failed ! mbedtls_net_connect returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* 2. Setup stuff
|
||||
*/
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_10, P_INFO, "STEP 2. Setting up the SSL/TLS structure...");
|
||||
if ((value = mbedtls_ssl_config_defaults(&(ssl->sslConfig), MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||
MBEDTLS_SSL_PRESET_DEFAULT)) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_11, P_INFO, " failed! mbedtls_ssl_config_defaults returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_6, P_INFO, "after net connect:%d", xBytesTaskMalloced);
|
||||
mbedtls_ssl_conf_max_version(&ssl->sslConfig, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
|
||||
mbedtls_ssl_conf_min_version(&ssl->sslConfig, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
|
||||
|
||||
memcpy(&(ssl->crtProfile), ssl->sslConfig.cert_profile, sizeof(mbedtls_x509_crt_profile));
|
||||
mbedtls_ssl_conf_authmode(&(ssl->sslConfig), authmode);
|
||||
|
||||
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
||||
if ((value = mbedtls_ssl_conf_max_frag_len(&(ssl->sslConfig), MBEDTLS_SSL_MAX_FRAG_LEN_4096)) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_12, P_INFO, " mbedtls_ssl_conf_max_frag_len returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_ssl_conf_cert_profile(&ssl->sslConfig, &ssl->crtProfile);
|
||||
mbedtls_ssl_conf_ca_chain(&(ssl->sslConfig), &(ssl->caCert), NULL);
|
||||
if(context->clientCert) {
|
||||
if ((value = mbedtls_ssl_conf_own_cert(&(ssl->sslConfig), &(ssl->clientCert), &(ssl->pkContext))) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_13, P_INFO, " failed! mbedtls_ssl_conf_own_cert returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if(context->ciphersuite[0] != 0xFFFF){
|
||||
mbedtls_ssl_conf_ciphersuites(&(ssl->sslConfig), (const int *)(context->ciphersuite));
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_14, P_INFO, "conf ciphersuite 0x%x", context->ciphersuite[0]);
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_rng(&(ssl->sslConfig), mqttSslRandom, &(ssl->ctrDrbgContext));
|
||||
mbedtls_ssl_conf_dbg(&(ssl->sslConfig), mqttSslDebug, NULL);
|
||||
|
||||
#if defined(MBEDTLS_SSL_ALPN)
|
||||
const char *alpn_list[] = { "http/1.1", NULL };
|
||||
mbedtls_ssl_conf_alpn_protocols(&(ssl->sslConfig),alpn_list);
|
||||
#endif
|
||||
|
||||
if(context->timeout_r > 0) {
|
||||
uint32_t recvTimeout;
|
||||
recvTimeout = context->timeout_r > MQTT_MAX_TIMEOUT ? MQTT_MAX_TIMEOUT * 1000 : context->timeout_r * 1000;
|
||||
mbedtls_ssl_conf_read_timeout(&(ssl->sslConfig), recvTimeout);
|
||||
}
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_7, P_INFO, "before ssl setup:%d", xBytesTaskMalloced);
|
||||
if ((value = mbedtls_ssl_setup(&(ssl->sslContext), &(ssl->sslConfig))) != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_15, P_INFO, " failed! mbedtls_ssl_setup returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
if(context->hostName != NULL)
|
||||
{
|
||||
mbedtls_ssl_set_hostname(&(ssl->sslContext), context->hostName);
|
||||
}
|
||||
else
|
||||
{
|
||||
mbedtls_ssl_set_hostname(&(ssl->sslContext), host);
|
||||
}
|
||||
mbedtls_ssl_set_bio(&(ssl->sslContext), &(ssl->netContext), (mbedtls_ssl_send_t*)mbedtls_net_send, (mbedtls_ssl_recv_t*)mbedtls_net_recv, (mbedtls_ssl_recv_timeout_t*)mbedtls_net_recv_timeout);
|
||||
|
||||
|
||||
/*
|
||||
* 3. Handshake
|
||||
*/
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_8, P_INFO, "after ssl setup before handshake:%d", xBytesTaskMalloced);
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_16, P_INFO, "STEP 3. Performing the SSL/TLS handshake...");
|
||||
|
||||
while ((value = mbedtls_ssl_handshake(&(ssl->sslContext))) != 0) {
|
||||
if ((value != MBEDTLS_ERR_SSL_WANT_READ) && (value != MBEDTLS_ERR_SSL_WANT_WRITE)) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_17, P_INFO, "failed ! mbedtls_ssl_handshake returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
////ECPLAT_PRINTF(UNILOG_HTTP, sslMEM_9, P_INFO, "after handshake:%d", xBytesTaskMalloced);
|
||||
|
||||
/*
|
||||
* 4. Verify the server certificate
|
||||
*/
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_18, P_INFO, "STEP 4. Verifying peer X.509 certificate..");
|
||||
flag = mbedtls_ssl_get_verify_result(&(ssl->sslContext));
|
||||
if (flag != 0) {
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_19, P_INFO, " failed ! verify result not confirmed.");
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTlsConn_20, P_INFO, "caCert varification ok");
|
||||
|
||||
return MQTT_CONN_OK;
|
||||
}
|
||||
|
||||
int mqttSslConn_old(mqttsClientContext* context, char* host)
|
||||
{
|
||||
INT32 value;
|
||||
mqttsClientSsl *ssl;
|
||||
const char *custom = "mqtts";
|
||||
char port[10] = {0};
|
||||
INT32 authmode = MBEDTLS_SSL_VERIFY_NONE;
|
||||
UINT32 flag;
|
||||
|
||||
context->ssl = malloc(sizeof(mqttsClientSsl));
|
||||
ssl = context->ssl;
|
||||
|
||||
/*
|
||||
* 0. Initialize the RNG and the session data
|
||||
*/
|
||||
#if defined(MBEDTLS_DEBUG_C)
|
||||
mbedtls_debug_set_threshold((int)2);
|
||||
#endif
|
||||
mbedtls_net_init(&ssl->netContext);
|
||||
mbedtls_ssl_init(&ssl->sslContext);
|
||||
mbedtls_ssl_config_init(&ssl->sslConfig);
|
||||
mbedtls_x509_crt_init(&ssl->caCert);
|
||||
mbedtls_x509_crt_init(&ssl->clientCert);
|
||||
mbedtls_pk_init(&ssl->pkContext);
|
||||
mbedtls_ctr_drbg_init(&ssl->ctrDrbgContext);
|
||||
mbedtls_entropy_init(&ssl->entropyContext);
|
||||
if((context->psk_key != NULL)&&(context->psk_identity != NULL))
|
||||
{
|
||||
mbedtls_ssl_conf_psk(&ssl->sslConfig, (const unsigned char *)context->psk_key, strlen(context->psk_key),
|
||||
(const unsigned char *)context->psk_identity, strlen(context->psk_identity));
|
||||
}
|
||||
if((value = mbedtls_ctr_drbg_seed(&ssl->ctrDrbgContext,
|
||||
mbedtls_entropy_func,
|
||||
&ssl->entropyContext,
|
||||
(const unsigned char*)custom,
|
||||
strlen(custom))) != 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_0, P_SIG, 1, "mbedtls_ctr_drbg_seed failed, value:-0x%x.", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
/*
|
||||
* 0. Initialize certificates
|
||||
*/
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_1, P_SIG, 0, "STEP 0. Loading the CA root certificate ...");
|
||||
if (NULL != context->caCert)
|
||||
{
|
||||
//authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
|
||||
if (0 != (value = mbedtls_x509_crt_parse(&(ssl->caCert), (const unsigned char *)context->caCert, context->caCertLen)))
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_2, P_SIG, 1, "failed ! value:-0x%x", -value);
|
||||
//return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_3, P_SIG, 1, " ok (%d skipped)", value);
|
||||
}
|
||||
|
||||
/* Setup Client Cert/Key */
|
||||
if (context->clientCert != NULL && context->clientPk != NULL)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_4, P_SIG, 0, "STEP 0. start prepare client cert ...");
|
||||
value = mbedtls_x509_crt_parse(&(ssl->clientCert), (const unsigned char *) context->clientCert, context->clientCertLen);
|
||||
if (value != 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_5, P_SIG, 1, " failed! mbedtls_x509_crt_parse returned -0x%x\n", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_6, P_SIG, 1, "STEP 0. start mbedtls_pk_parse_key[%s]", context->clientPk);
|
||||
value = mbedtls_pk_parse_key(&ssl->pkContext, (const unsigned char *) context->clientPk, context->clientPkLen, NULL, 0);
|
||||
if (value != 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_7, P_SIG, 1, " failed\n ! mbedtls_pk_parse_key returned -0x%x\n", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* 1. Start the connection
|
||||
*/
|
||||
snprintf(port, sizeof(port), "%d", context->port);
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_8, P_SIG, 2, "STEP 1. Connecting to /%s/%s...", host, port);
|
||||
if (0 != (value = mbedtls_net_connect(&ssl->netContext, host, port, MBEDTLS_NET_PROTO_TCP, LWIP_PS_INVALID_CID)))
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_9, P_SIG, 1, " failed ! mbedtls_net_connect returned -0x%x", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_10, P_SIG, 0, " ok");
|
||||
|
||||
/*
|
||||
* 2. Setup stuff
|
||||
*/
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_11, P_SIG, 0, "STEP 2. Setting up the SSL/TLS structure...");
|
||||
if ((value = mbedtls_ssl_config_defaults(&(ssl->sslConfig), MBEDTLS_SSL_IS_CLIENT, MBEDTLS_SSL_TRANSPORT_STREAM,
|
||||
MBEDTLS_SSL_PRESET_DEFAULT)) != 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_12, P_SIG, 1, " failed! mbedtls_ssl_config_defaults returned %d", value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_max_version(&ssl->sslConfig, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
|
||||
mbedtls_ssl_conf_min_version(&ssl->sslConfig, MBEDTLS_SSL_MAJOR_VERSION_3, MBEDTLS_SSL_MINOR_VERSION_3);
|
||||
|
||||
memcpy(&(ssl->crtProfile), ssl->sslConfig.cert_profile, sizeof(mbedtls_x509_crt_profile));
|
||||
mbedtls_ssl_conf_authmode(&(ssl->sslConfig), authmode);
|
||||
|
||||
#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
|
||||
if ((mbedtls_ssl_conf_max_frag_len(&(ssl->sslConfig), MBEDTLS_SSL_MAX_FRAG_LEN_1024)) != 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_13, P_SIG, 0, "mbedtls_ssl_conf_max_frag_len returned\r\n");
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_ssl_conf_cert_profile(&ssl->sslConfig, &ssl->crtProfile);
|
||||
mbedtls_ssl_conf_ca_chain(&(ssl->sslConfig), &(ssl->caCert), NULL);
|
||||
if(context->clientCert)
|
||||
{
|
||||
if ((value = mbedtls_ssl_conf_own_cert(&(ssl->sslConfig), &(ssl->clientCert), &(ssl->pkContext))) != 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_14, P_SIG, 1, " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n", value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
if(context->ciphersuite[0] != 0xFFFF){
|
||||
mbedtls_ssl_conf_ciphersuites(&(ssl->sslConfig), (const int *)(context->ciphersuite));
|
||||
//ECPLAT_PRINTF(UNILOG_MQTT, mqttTls_14_1, P_INFO, "conf ciphersuite 0x%x", context->ciphersuite[0]);
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_rng(&(ssl->sslConfig), mqttSslRandom, &(ssl->ctrDrbgContext));
|
||||
mbedtls_ssl_conf_dbg(&(ssl->sslConfig), mqttSslDebug, NULL);
|
||||
|
||||
if(context->timeout_r > 0)
|
||||
{
|
||||
UINT32 recvTimeout;
|
||||
recvTimeout = context->timeout_r > MQTT_MAX_TIMEOUT ? MQTT_MAX_TIMEOUT * 1000 : context->timeout_r * 1000;
|
||||
mbedtls_ssl_conf_read_timeout(&(ssl->sslConfig), recvTimeout);
|
||||
}
|
||||
if ((value = mbedtls_ssl_setup(&(ssl->sslContext), &(ssl->sslConfig))) != 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_15, P_SIG, 1, "failed! mbedtls_ssl_setup returned %d", value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
|
||||
if(context->hostName != NULL)
|
||||
{
|
||||
mbedtls_ssl_set_hostname(&(ssl->sslContext), context->hostName);
|
||||
//mbedtls_ssl_set_hostname(&(ssl->sslContext), "OneNET MQTTS");
|
||||
}
|
||||
else
|
||||
{
|
||||
mbedtls_ssl_set_hostname(&(ssl->sslContext), host);
|
||||
}
|
||||
|
||||
mbedtls_ssl_set_bio(&(ssl->sslContext), &(ssl->netContext), mbedtls_net_send, mbedtls_net_recv, mbedtls_net_recv_timeout);
|
||||
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_16, P_SIG, 0, " ok");
|
||||
|
||||
/*
|
||||
* 3. Handshake
|
||||
*/
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_17, P_SIG, 0, "STEP 3. Performing the SSL/TLS handshake...");
|
||||
while ((value = mbedtls_ssl_handshake(&(ssl->sslContext))) != 0)
|
||||
{
|
||||
if ((value != MBEDTLS_ERR_SSL_WANT_READ) && (value != MBEDTLS_ERR_SSL_WANT_WRITE))
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_18, P_SIG, 1, "failed ! mbedtls_ssl_handshake returned -0x%x\n", -value);
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
}
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_19, P_SIG, 0, " ok");
|
||||
|
||||
/*
|
||||
* 4. Verify the server certificate
|
||||
*/
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_20, P_SIG, 0, "STEP 4. Verifying peer X.509 certificate..");
|
||||
flag = mbedtls_ssl_get_verify_result(&(ssl->sslContext));
|
||||
if (flag != 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_21, P_SIG, 0, " failed ! verify result not confirmed.");
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
}
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_22, P_SIG, 0, "caCert varification ok");
|
||||
|
||||
return MQTT_CONN_OK;
|
||||
}
|
||||
|
||||
//INT32 mqttSslSend(mbedtls_ssl_context* sslContext, const char* buf, UINT16 len)
|
||||
int mqttSslSend(mqttsClientContext* context, unsigned char* buf, int len)
|
||||
{
|
||||
INT32 waitToSend = len;
|
||||
INT32 hasSend = 0;
|
||||
|
||||
do
|
||||
{
|
||||
hasSend = mbedtls_ssl_write(&(context->ssl->sslContext), (unsigned char *)(buf + len - waitToSend), waitToSend);
|
||||
if(hasSend > 0)
|
||||
{
|
||||
waitToSend -= hasSend;
|
||||
}
|
||||
else if(hasSend == 0)
|
||||
{
|
||||
return MQTT_CONN_OK;
|
||||
}
|
||||
else
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_23, P_SIG, 0, "mqtt_client(ssl): send failed \n");
|
||||
return MQTT_CONN;
|
||||
}
|
||||
}while(waitToSend>0);
|
||||
|
||||
return MQTT_CONN_OK;
|
||||
}
|
||||
|
||||
int mqttSslRecv(mqttsClientContext* context, unsigned char* buf, int minLen, int maxLen, int* pReadLen) //0 on success, err code on failure
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_24, P_INFO, 2, "Trying to read between %d and %d bytes", minLen, maxLen);
|
||||
INT32 readLen = 0;
|
||||
INT32 ret;
|
||||
|
||||
while (readLen < maxLen)
|
||||
{
|
||||
mqttsClientSsl *ssl = (mqttsClientSsl *)context->ssl;
|
||||
if (readLen < minLen)
|
||||
{
|
||||
mbedtls_ssl_set_bio(&(ssl->sslContext), &(ssl->netContext), mbedtls_net_send, mbedtls_net_recv, NULL);
|
||||
ret = mbedtls_ssl_read(&(ssl->sslContext), (unsigned char *)(buf+readLen), minLen-readLen);
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_30, P_INFO, 1, "mbedtls_ssl_read [blocking] return:0x%x", ret);
|
||||
if(ret == 0)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_31, P_INFO, 0, "mbedtls_ssl_read [blocking] return 0 connect error");
|
||||
return MQTT_CONN;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
mbedtls_ssl_set_bio(&(ssl->sslContext), &(ssl->netContext), mbedtls_net_send, mqttSslNonblockRecv, NULL);
|
||||
ret = mbedtls_ssl_read(&(ssl->sslContext), (unsigned char*)(buf+readLen), maxLen-readLen);
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_32, P_INFO, 1, "mbedtls_ssl_read [not blocking] return:0x%x", ret);
|
||||
if(ret == -1 && errno == EWOULDBLOCK)
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_33, P_INFO, 0, "mbedtls_ssl_read [not blocking] errno == EWOULDBLOCK");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(ret == MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY)
|
||||
{
|
||||
return MQTT_CLOSED;
|
||||
}
|
||||
|
||||
if (ret > 0)
|
||||
{
|
||||
readLen += ret;
|
||||
}
|
||||
else if ( ret == 0 )
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_34, P_INFO, 1, "Connection error (recv returned %d)", ret);
|
||||
*pReadLen = readLen;
|
||||
return MQTT_CONN;
|
||||
}
|
||||
}
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_35, P_INFO, 1, "Read %d bytes", readLen);
|
||||
buf[readLen] = '\0'; // DS makes it easier to see what's new.
|
||||
*pReadLen = readLen;
|
||||
return MQTT_CONN_OK;
|
||||
}
|
||||
|
||||
int mqttSslRead(mqttsClientContext* context, unsigned char *buffer, int len, int timeout_ms)
|
||||
{
|
||||
UINT32 readLen = 0;
|
||||
static int net_status = 0;
|
||||
INT32 ret = -1;
|
||||
char err_str[33];
|
||||
mqttsClientSsl *ssl = (mqttsClientSsl *)context->ssl;
|
||||
|
||||
mbedtls_ssl_conf_read_timeout(&(ssl->sslConfig), timeout_ms);
|
||||
while (readLen < len) {
|
||||
ret = mbedtls_ssl_read(&(ssl->sslContext), (unsigned char *)(buffer + readLen), (len - readLen));
|
||||
if (ret > 0) {
|
||||
readLen += ret;
|
||||
net_status = 0;
|
||||
} else if (ret == 0) {
|
||||
/* if ret is 0 and net_status is -2, indicate the connection is closed during last call */
|
||||
return (net_status == -2) ? net_status : readLen;
|
||||
} else {
|
||||
if (MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY == ret) {
|
||||
//mbedtls_strerror(ret, err_str, sizeof(err_str));
|
||||
printf("ssl recv error: code = -0x%04X, err_str = '%s'\n", -ret, err_str);
|
||||
net_status = -2; /* connection is closed */
|
||||
break;
|
||||
} else if ((MBEDTLS_ERR_SSL_TIMEOUT == ret)
|
||||
|| (MBEDTLS_ERR_SSL_CONN_EOF == ret)
|
||||
|| (MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED == ret)
|
||||
|| (MBEDTLS_ERR_SSL_NON_FATAL == ret)) {
|
||||
/* read already complete */
|
||||
/* if call mbedtls_ssl_read again, it will return 0 (means EOF) */
|
||||
|
||||
return readLen;
|
||||
} else {
|
||||
//mbedtls_strerror(ret, err_str, sizeof(err_str));
|
||||
printf("ssl recv error: code = -0x%04X, err_str = '%s'\n", -ret, err_str);
|
||||
net_status = -1;
|
||||
return -1; /* Connection error */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return (readLen > 0) ? readLen : net_status;
|
||||
}
|
||||
|
||||
int mqttSslClose(mqttsClientContext* context)
|
||||
{
|
||||
mqttsClientSsl *ssl = (mqttsClientSsl *)context->ssl;
|
||||
/*context->clientCert = NULL;
|
||||
context->caCert = NULL;
|
||||
context->clientPk = NULL; let up level free it*/
|
||||
if(ssl == NULL)
|
||||
return MQTT_MBEDTLS_ERR;
|
||||
|
||||
mbedtls_ssl_close_notify(&(ssl->sslContext));
|
||||
mbedtls_net_free(&(ssl->netContext));
|
||||
mbedtls_x509_crt_free(&(ssl->caCert));
|
||||
mbedtls_x509_crt_free(&(ssl->clientCert));
|
||||
mbedtls_pk_free(&(ssl->pkContext));
|
||||
mbedtls_ssl_free(&(ssl->sslContext));
|
||||
mbedtls_ssl_config_free(&(ssl->sslConfig));
|
||||
mbedtls_ctr_drbg_free(&(ssl->ctrDrbgContext));
|
||||
mbedtls_entropy_free(&(ssl->entropyContext));
|
||||
|
||||
free(ssl);
|
||||
context->ssl = NULL;
|
||||
|
||||
//ECOMM_TRACE(UNILOG_MQTT, mqttTls_36, P_INFO, 0, "mqtt tls close ok");
|
||||
return MQTT_CONN_OK;
|
||||
}
|
||||
|
||||
|
||||
72
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/eigencomm/MQTTTls.h
vendored
Normal file
72
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/eigencomm/MQTTTls.h
vendored
Normal file
@@ -0,0 +1,72 @@
|
||||
|
||||
#ifndef MQTT_DTLS_H
|
||||
#define MQTT_DTLS_H
|
||||
|
||||
#include "commonTypedef.h"
|
||||
|
||||
#include "mbedtls/net.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/certs.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
|
||||
|
||||
|
||||
|
||||
#define MQTT_MAX_TIMEOUT (10 * 60) //10 min
|
||||
|
||||
|
||||
typedef struct mqttsClientSslTag
|
||||
{
|
||||
mbedtls_ssl_context sslContext;
|
||||
mbedtls_net_context netContext;
|
||||
mbedtls_ssl_config sslConfig;
|
||||
mbedtls_entropy_context entropyContext;
|
||||
mbedtls_ctr_drbg_context ctrDrbgContext;
|
||||
mbedtls_x509_crt_profile crtProfile;
|
||||
mbedtls_x509_crt caCert;
|
||||
mbedtls_x509_crt clientCert;
|
||||
mbedtls_pk_context pkContext;
|
||||
}mqttsClientSsl;
|
||||
|
||||
typedef struct mqttsClientContextTag
|
||||
{
|
||||
int socket;
|
||||
int timeout_s;
|
||||
int timeout_r;
|
||||
int isMqtts;
|
||||
int method;
|
||||
UINT16 port;
|
||||
unsigned int keepAliveInterval;
|
||||
size_t sendBufSize;
|
||||
size_t readBufSize;
|
||||
unsigned char *sendBuf;
|
||||
unsigned char *readBuf;
|
||||
|
||||
mqttsClientSsl * ssl;
|
||||
char *caCert;
|
||||
char *clientCert;
|
||||
char *clientPk;
|
||||
char *hostName;
|
||||
char *psk_key;
|
||||
char *psk_identity;
|
||||
int caCertLen;
|
||||
int clientCertLen;
|
||||
int clientPkLen;
|
||||
uint8_t seclevel;//0:no verify; 1:verify server; 2:both verify
|
||||
int32_t ciphersuite[2];//just like 0x0035 TLS_RSA_WITH_AES_256_CBC_SHA,ciphersuite[1] must NULL
|
||||
uint8_t pdpId;//pdp context id--cid--0 is default
|
||||
|
||||
}mqttsClientContext;
|
||||
|
||||
|
||||
|
||||
int mqttSslConn_old(mqttsClientContext* context, char* host);
|
||||
int mqttSslSend(mqttsClientContext* context, unsigned char* buf, int len);
|
||||
int mqttSslRecv(mqttsClientContext* context, unsigned char* buf, int minLen, int maxLen, int* pReadLen);
|
||||
int mqttSslRead(mqttsClientContext* context, unsigned char *buffer, int len, int timeout_ms);
|
||||
int mqttSslClose(mqttsClientContext* context);
|
||||
int mqttSslConn_new(mqttsClientContext* context, char* host);
|
||||
|
||||
#endif
|
||||
|
||||
168
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/linux/MQTTLinux.c
vendored
Normal file
168
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/linux/MQTTLinux.c
vendored
Normal file
@@ -0,0 +1,168 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2017 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - return codes from linux_read
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTLinux.h"
|
||||
|
||||
void TimerInit(Timer* timer)
|
||||
{
|
||||
timer->end_time = (struct timeval){0, 0};
|
||||
}
|
||||
|
||||
char TimerIsExpired(Timer* timer)
|
||||
{
|
||||
struct timeval now, res;
|
||||
gettimeofday(&now, NULL);
|
||||
timersub(&timer->end_time, &now, &res);
|
||||
return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0);
|
||||
}
|
||||
|
||||
|
||||
void TimerCountdownMS(Timer* timer, unsigned int timeout)
|
||||
{
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
struct timeval interval = {timeout / 1000, (timeout % 1000) * 1000};
|
||||
timeradd(&now, &interval, &timer->end_time);
|
||||
}
|
||||
|
||||
|
||||
void TimerCountdown(Timer* timer, unsigned int timeout)
|
||||
{
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
struct timeval interval = {timeout, 0};
|
||||
timeradd(&now, &interval, &timer->end_time);
|
||||
}
|
||||
|
||||
|
||||
int TimerLeftMS(Timer* timer)
|
||||
{
|
||||
struct timeval now, res;
|
||||
gettimeofday(&now, NULL);
|
||||
timersub(&timer->end_time, &now, &res);
|
||||
//printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
|
||||
return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
|
||||
}
|
||||
|
||||
|
||||
int linux_read(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
||||
{
|
||||
struct timeval interval = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
|
||||
if (interval.tv_sec < 0 || (interval.tv_sec == 0 && interval.tv_usec <= 0))
|
||||
{
|
||||
interval.tv_sec = 0;
|
||||
interval.tv_usec = 100;
|
||||
}
|
||||
|
||||
setsockopt(n->my_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&interval, sizeof(struct timeval));
|
||||
|
||||
int bytes = 0;
|
||||
while (bytes < len)
|
||||
{
|
||||
int rc = recv(n->my_socket, &buffer[bytes], (size_t)(len - bytes), 0);
|
||||
if (rc == -1)
|
||||
{
|
||||
if (errno != EAGAIN && errno != EWOULDBLOCK)
|
||||
bytes = -1;
|
||||
break;
|
||||
}
|
||||
else if (rc == 0)
|
||||
{
|
||||
bytes = 0;
|
||||
break;
|
||||
}
|
||||
else
|
||||
bytes += rc;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
||||
int linux_write(Network* n, unsigned char* buffer, int len, int timeout_ms)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
tv.tv_sec = 0; /* 30 Secs Timeout */
|
||||
tv.tv_usec = timeout_ms * 1000; // Not init'ing this can cause strange errors
|
||||
|
||||
setsockopt(n->my_socket, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
|
||||
int rc = write(n->my_socket, buffer, len);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
void NetworkInit(Network* n)
|
||||
{
|
||||
n->my_socket = 0;
|
||||
n->mqttread = linux_read;
|
||||
n->mqttwrite = linux_write;
|
||||
}
|
||||
|
||||
|
||||
int NetworkConnect(Network* n, char* addr, int port)
|
||||
{
|
||||
int type = SOCK_STREAM;
|
||||
struct sockaddr_in address;
|
||||
int rc = -1;
|
||||
sa_family_t family = AF_INET;
|
||||
struct addrinfo *result = NULL;
|
||||
struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL};
|
||||
|
||||
if ((rc = getaddrinfo(addr, NULL, &hints, &result)) == 0)
|
||||
{
|
||||
struct addrinfo* res = result;
|
||||
|
||||
/* prefer ip4 addresses */
|
||||
while (res)
|
||||
{
|
||||
if (res->ai_family == AF_INET)
|
||||
{
|
||||
result = res;
|
||||
break;
|
||||
}
|
||||
res = res->ai_next;
|
||||
}
|
||||
|
||||
if (result->ai_family == AF_INET)
|
||||
{
|
||||
address.sin_port = htons(port);
|
||||
address.sin_family = family = AF_INET;
|
||||
address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr;
|
||||
}
|
||||
else
|
||||
rc = -1;
|
||||
|
||||
freeaddrinfo(result);
|
||||
}
|
||||
|
||||
if (rc == 0)
|
||||
{
|
||||
n->my_socket = socket(family, type, 0);
|
||||
if (n->my_socket != -1)
|
||||
rc = connect(n->my_socket, (struct sockaddr*)&address, sizeof(address));
|
||||
else
|
||||
rc = -1;
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
void NetworkDisconnect(Network* n)
|
||||
{
|
||||
close(n->my_socket);
|
||||
}
|
||||
74
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/linux/MQTTLinux.h
vendored
Normal file
74
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient-C/src/linux/MQTTLinux.h
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Allan Stockdill-Mander - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(__MQTT_LINUX_)
|
||||
#define __MQTT_LINUX_
|
||||
|
||||
#if defined(WIN32_DLL) || defined(WIN64_DLL)
|
||||
#define DLLImport __declspec(dllimport)
|
||||
#define DLLExport __declspec(dllexport)
|
||||
#elif defined(LINUX_SO)
|
||||
#define DLLImport extern
|
||||
#define DLLExport __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define DLLImport
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
typedef struct Timer
|
||||
{
|
||||
struct timeval end_time;
|
||||
} Timer;
|
||||
|
||||
void TimerInit(Timer*);
|
||||
char TimerIsExpired(Timer*);
|
||||
void TimerCountdownMS(Timer*, unsigned int);
|
||||
void TimerCountdown(Timer*, unsigned int);
|
||||
int TimerLeftMS(Timer*);
|
||||
|
||||
typedef struct Network
|
||||
{
|
||||
int my_socket;
|
||||
int (*mqttread) (struct Network*, unsigned char*, int, int);
|
||||
int (*mqttwrite) (struct Network*, unsigned char*, int, int);
|
||||
} Network;
|
||||
|
||||
int linux_read(Network*, unsigned char*, int, int);
|
||||
int linux_write(Network*, unsigned char*, int, int);
|
||||
|
||||
DLLExport void NetworkInit(Network*);
|
||||
DLLExport int NetworkConnect(Network*, char*, int);
|
||||
DLLExport void NetworkDisconnect(Network*);
|
||||
|
||||
#endif
|
||||
21
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/CMakeLists.txt
vendored
Normal file
21
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2017 IBM Corp.
|
||||
#
|
||||
# All rights reserved. This program and the accompanying materials
|
||||
# are made available under the terms of the Eclipse Public License v1.0
|
||||
# and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
#
|
||||
# The Eclipse Public License is available at
|
||||
# http://www.eclipse.org/legal/epl-v10.html
|
||||
# and the Eclipse Distribution License is available at
|
||||
# http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
#
|
||||
# Contributors:
|
||||
# Ian Craggs - initial version
|
||||
#*******************************************************************************/
|
||||
|
||||
project("paho-mqttclient" C)
|
||||
|
||||
#ADD_SUBDIRECTORY(src)
|
||||
ADD_SUBDIRECTORY(samples)
|
||||
ADD_SUBDIRECTORY(test)
|
||||
17
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/CMakeLists.txt
vendored
Normal file
17
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,17 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2017 IBM Corp.
|
||||
#
|
||||
# All rights reserved. This program and the accompanying materials
|
||||
# are made available under the terms of the Eclipse Public License v1.0
|
||||
# and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
#
|
||||
# The Eclipse Public License is available at
|
||||
# http://www.eclipse.org/legal/epl-v10.html
|
||||
# and the Eclipse Distribution License is available at
|
||||
# http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
#
|
||||
# Contributors:
|
||||
# Ian Craggs - initial version
|
||||
#*******************************************************************************/
|
||||
|
||||
ADD_SUBDIRECTORY(linux)
|
||||
131
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/arduino/Hello/Hello.ino
vendored
Normal file
131
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/arduino/Hello/Hello.ino
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp. and others
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial contribution
|
||||
* Benjamin Cabe - adapt to IPStack, and add Yun instructions
|
||||
*******************************************************************************/
|
||||
|
||||
#define MQTTCLIENT_QOS2 1
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
#include <IPStack.h>
|
||||
#include <Countdown.h>
|
||||
#include <MQTTClient.h>
|
||||
|
||||
char printbuf[100];
|
||||
|
||||
int arrivedcount = 0;
|
||||
|
||||
void messageArrived(MQTT::MessageData& md)
|
||||
{
|
||||
MQTT::Message &message = md.message;
|
||||
|
||||
sprintf(printbuf, "Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n",
|
||||
++arrivedcount, message.qos, message.retained, message.dup, message.id);
|
||||
Serial.print(printbuf);
|
||||
sprintf(printbuf, "Payload %s\n", (char*)message.payload);
|
||||
Serial.print(printbuf);
|
||||
}
|
||||
|
||||
EthernetClient c; // replace by a YunClient if running on a Yun
|
||||
IPStack ipstack(c);
|
||||
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
|
||||
|
||||
byte mac[] = { 0x00, 0x11, 0x22, 0x33, 0x44, 0x55 }; // replace with your device's MAC
|
||||
const char* topic = "arduino-sample";
|
||||
|
||||
void connect()
|
||||
{
|
||||
char hostname[] = "iot.eclipse.org";
|
||||
int port = 1883;
|
||||
sprintf(printbuf, "Connecting to %s:%d\n", hostname, port);
|
||||
Serial.print(printbuf);
|
||||
int rc = ipstack.connect(hostname, port);
|
||||
if (rc != 1)
|
||||
{
|
||||
sprintf(printbuf, "rc from TCP connect is %d\n", rc);
|
||||
Serial.print(printbuf);
|
||||
}
|
||||
|
||||
Serial.println("MQTT connecting");
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
data.MQTTVersion = 3;
|
||||
data.clientID.cstring = (char*)"arduino-sample";
|
||||
rc = client.connect(data);
|
||||
if (rc != 0)
|
||||
{
|
||||
sprintf(printbuf, "rc from MQTT connect is %d\n", rc);
|
||||
Serial.print(printbuf);
|
||||
}
|
||||
Serial.println("MQTT connected");
|
||||
|
||||
rc = client.subscribe(topic, MQTT::QOS2, messageArrived);
|
||||
if (rc != 0)
|
||||
{
|
||||
sprintf(printbuf, "rc from MQTT subscribe is %d\n", rc);
|
||||
Serial.print(printbuf);
|
||||
}
|
||||
Serial.println("MQTT subscribed");
|
||||
}
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Ethernet.begin(mac); // replace by Bridge.begin() if running on a Yun
|
||||
Serial.println("MQTT Hello example");
|
||||
connect();
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (!client.isConnected())
|
||||
connect();
|
||||
|
||||
MQTT::Message message;
|
||||
|
||||
arrivedcount = 0;
|
||||
|
||||
// Send and receive QoS 0 message
|
||||
char buf[100];
|
||||
sprintf(buf, "Hello World! QoS 0 message");
|
||||
Serial.println(buf);
|
||||
message.qos = MQTT::QOS0;
|
||||
message.retained = false;
|
||||
message.dup = false;
|
||||
message.payload = (void*)buf;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
int rc = client.publish(topic, message);
|
||||
while (arrivedcount == 0)
|
||||
client.yield(1000);
|
||||
|
||||
// Send and receive QoS 1 message
|
||||
sprintf(buf, "Hello World! QoS 1 message");
|
||||
Serial.println(buf);
|
||||
message.qos = MQTT::QOS1;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
rc = client.publish(topic, message);
|
||||
while (arrivedcount == 1)
|
||||
client.yield(1000);
|
||||
|
||||
// Send and receive QoS 2 message
|
||||
sprintf(buf, "Hello World! QoS 2 message");
|
||||
Serial.println(buf);
|
||||
message.qos = MQTT::QOS2;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
rc = client.publish(topic, message);
|
||||
while (arrivedcount == 2)
|
||||
client.yield(1000);
|
||||
|
||||
delay(2000);
|
||||
}
|
||||
31
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/CMakeLists.txt
vendored
Normal file
31
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2017 IBM Corp.
|
||||
#
|
||||
# All rights reserved. This program and the accompanying materials
|
||||
# are made available under the terms of the Eclipse Public License v1.0
|
||||
# and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
#
|
||||
# The Eclipse Public License is available at
|
||||
# http://www.eclipse.org/legal/epl-v10.html
|
||||
# and the Eclipse Distribution License is available at
|
||||
# http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
#
|
||||
# Contributors:
|
||||
# Ian Craggs - initial version
|
||||
#*******************************************************************************/
|
||||
|
||||
# Samples
|
||||
|
||||
add_executable(
|
||||
hello
|
||||
hello.cpp
|
||||
)
|
||||
target_include_directories(hello PRIVATE "../../src" "../../src/linux")
|
||||
target_link_libraries(hello MQTTPacketClient)
|
||||
|
||||
add_executable(
|
||||
stdoutsub
|
||||
stdoutsub.cpp
|
||||
)
|
||||
target_include_directories(stdoutsub PRIVATE "../../src" "../../src/linux")
|
||||
target_link_libraries(stdoutsub paho-embed-mqtt3c)
|
||||
3
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/build.sh
vendored
Normal file
3
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/build.sh
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
g++ hello.cpp -I ../../src/ -I ../../src/linux -I ../../../MQTTPacket/src ../../../MQTTPacket/src/MQTTPacket.c ../../../MQTTPacket/src/MQTTDeserializePublish.c ../../../MQTTPacket/src/MQTTConnectClient.c ../../../MQTTPacket/src/MQTTSubscribeClient.c ../../../MQTTPacket/src/MQTTSerializePublish.c ../../../MQTTPacket/src/MQTTUnsubscribeClient.c -o hello
|
||||
|
||||
g++ -g stdoutsub.cpp -I ../../src -I ../../src/linux -I ../../../MQTTPacket/src ../../../MQTTPacket/src/MQTTFormat.c ../../../MQTTPacket/src/MQTTPacket.c ../../../MQTTPacket/src/MQTTDeserializePublish.c ../../../MQTTPacket/src/MQTTConnectClient.c ../../../MQTTPacket/src/MQTTSubscribeClient.c ../../../MQTTPacket/src/MQTTSerializePublish.c -o stdoutsub ../../../MQTTPacket/src/MQTTConnectServer.c ../../../MQTTPacket/src/MQTTSubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeServer.c ../../../MQTTPacket/src/MQTTUnsubscribeClient.c
|
||||
104
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/hello.cpp
vendored
Normal file
104
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/hello.cpp
vendored
Normal file
@@ -0,0 +1,104 @@
|
||||
#define MQTTCLIENT_QOS2 1
|
||||
|
||||
#include <memory.h>
|
||||
|
||||
#include "MQTTClient.h"
|
||||
|
||||
#define DEFAULT_STACK_SIZE -1
|
||||
|
||||
#include "linux.cpp"
|
||||
|
||||
int arrivedcount = 0;
|
||||
|
||||
void messageArrived(MQTT::MessageData& md)
|
||||
{
|
||||
MQTT::Message &message = md.message;
|
||||
|
||||
printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n",
|
||||
++arrivedcount, message.qos, message.retained, message.dup, message.id);
|
||||
printf("Payload %.*s\n", (int)message.payloadlen, (char*)message.payload);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
IPStack ipstack = IPStack();
|
||||
float version = 0.3;
|
||||
const char* topic = "mbed-sample";
|
||||
|
||||
printf("Version is %f\n", version);
|
||||
|
||||
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
|
||||
|
||||
const char* hostname = "iot.eclipse.org";
|
||||
int port = 1883;
|
||||
printf("Connecting to %s:%d\n", hostname, port);
|
||||
int rc = ipstack.connect(hostname, port);
|
||||
if (rc != 0)
|
||||
printf("rc from TCP connect is %d\n", rc);
|
||||
|
||||
printf("MQTT connecting\n");
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
data.MQTTVersion = 3;
|
||||
data.clientID.cstring = (char*)"mbed-icraggs";
|
||||
rc = client.connect(data);
|
||||
if (rc != 0)
|
||||
printf("rc from MQTT connect is %d\n", rc);
|
||||
printf("MQTT connected\n");
|
||||
|
||||
rc = client.subscribe(topic, MQTT::QOS2, messageArrived);
|
||||
if (rc != 0)
|
||||
printf("rc from MQTT subscribe is %d\n", rc);
|
||||
|
||||
MQTT::Message message;
|
||||
|
||||
// QoS 0
|
||||
char buf[100];
|
||||
sprintf(buf, "Hello World! QoS 0 message from app version %f", version);
|
||||
message.qos = MQTT::QOS0;
|
||||
message.retained = false;
|
||||
message.dup = false;
|
||||
message.payload = (void*)buf;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
rc = client.publish(topic, message);
|
||||
if (rc != 0)
|
||||
printf("Error %d from sending QoS 0 message\n", rc);
|
||||
else while (arrivedcount == 0)
|
||||
client.yield(100);
|
||||
|
||||
// QoS 1
|
||||
printf("Now QoS 1\n");
|
||||
sprintf(buf, "Hello World! QoS 1 message from app version %f", version);
|
||||
message.qos = MQTT::QOS1;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
rc = client.publish(topic, message);
|
||||
if (rc != 0)
|
||||
printf("Error %d from sending QoS 1 message\n", rc);
|
||||
else while (arrivedcount == 1)
|
||||
client.yield(100);
|
||||
|
||||
// QoS 2
|
||||
sprintf(buf, "Hello World! QoS 2 message from app version %f", version);
|
||||
message.qos = MQTT::QOS2;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
rc = client.publish(topic, message);
|
||||
if (rc != 0)
|
||||
printf("Error %d from sending QoS 2 message\n", rc);
|
||||
while (arrivedcount == 2)
|
||||
client.yield(100);
|
||||
|
||||
rc = client.unsubscribe(topic);
|
||||
if (rc != 0)
|
||||
printf("rc from unsubscribe was %d\n", rc);
|
||||
|
||||
rc = client.disconnect();
|
||||
if (rc != 0)
|
||||
printf("rc from disconnect was %d\n", rc);
|
||||
|
||||
ipstack.disconnect();
|
||||
|
||||
printf("Finishing with %d messages received\n", arrivedcount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
291
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/main.cpp
vendored
Normal file
291
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/main.cpp
vendored
Normal file
@@ -0,0 +1,291 @@
|
||||
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
#include "MQTTClient.h"
|
||||
//#include "FP.cpp"
|
||||
|
||||
#define DEFAULT_STACK_SIZE -1
|
||||
|
||||
|
||||
class IPStack
|
||||
{
|
||||
public:
|
||||
IPStack()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int Socket_error(const char* aString)
|
||||
{
|
||||
|
||||
if (errno != EINTR && errno != EAGAIN && errno != EINPROGRESS && errno != EWOULDBLOCK)
|
||||
{
|
||||
if (strcmp(aString, "shutdown") != 0 || (errno != ENOTCONN && errno != ECONNRESET))
|
||||
printf("Socket error %s in %s for socket %d\n", strerror(errno), aString, mysock);
|
||||
}
|
||||
return errno;
|
||||
}
|
||||
|
||||
int connect(const char* hostname, int port)
|
||||
{
|
||||
int type = SOCK_STREAM;
|
||||
struct sockaddr_in address;
|
||||
int rc = -1;
|
||||
sa_family_t family = AF_INET;
|
||||
struct addrinfo *result = NULL;
|
||||
struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL};
|
||||
|
||||
if ((rc = getaddrinfo(hostname, NULL, &hints, &result)) == 0)
|
||||
{
|
||||
struct addrinfo* res = result;
|
||||
|
||||
/* prefer ip4 addresses */
|
||||
while (res)
|
||||
{
|
||||
if (res->ai_family == AF_INET)
|
||||
{
|
||||
result = res;
|
||||
break;
|
||||
}
|
||||
res = res->ai_next;
|
||||
}
|
||||
|
||||
if (result->ai_family == AF_INET)
|
||||
{
|
||||
address.sin_port = htons(port);
|
||||
address.sin_family = family = AF_INET;
|
||||
address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr;
|
||||
}
|
||||
else
|
||||
rc = -1;
|
||||
|
||||
freeaddrinfo(result);
|
||||
}
|
||||
|
||||
if (rc == 0)
|
||||
{
|
||||
mysock = socket(family, type, 0);
|
||||
if (mysock != -1)
|
||||
{
|
||||
int opt = 1;
|
||||
|
||||
//if (setsockopt(mysock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0)
|
||||
// printf("Could not set SO_NOSIGPIPE for socket %d", mysock);
|
||||
|
||||
rc = ::connect(mysock, (struct sockaddr*)&address, sizeof(address));
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
int read(unsigned char* buffer, int len, int timeout_ms)
|
||||
{
|
||||
struct timeval interval = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
|
||||
if (interval.tv_sec < 0 || (interval.tv_sec == 0 && interval.tv_usec <= 0))
|
||||
{
|
||||
interval.tv_sec = 0;
|
||||
interval.tv_usec = 100;
|
||||
}
|
||||
|
||||
setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&interval, sizeof(struct timeval));
|
||||
|
||||
//printf("reading %d bytes\n", len);
|
||||
int rc = ::recv(mysock, buffer, (size_t)len, 0);
|
||||
if (rc == -1)
|
||||
Socket_error("read");
|
||||
//printf("read %d bytes\n", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int write(unsigned char* buffer, int len, int timeout)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
tv.tv_sec = 0; /* 30 Secs Timeout */
|
||||
tv.tv_usec = timeout * 1000; // Not init'ing this can cause strange errors
|
||||
|
||||
setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv,sizeof(struct timeval));
|
||||
int rc = ::write(mysock, buffer, len);
|
||||
//printf("write rc %d\n", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int disconnect()
|
||||
{
|
||||
return ::close(mysock);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
int mysock;
|
||||
|
||||
};
|
||||
|
||||
|
||||
class Countdown
|
||||
{
|
||||
public:
|
||||
Countdown()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Countdown(int ms)
|
||||
{
|
||||
countdown_ms(ms);
|
||||
}
|
||||
|
||||
|
||||
bool expired()
|
||||
{
|
||||
struct timeval now, res;
|
||||
gettimeofday(&now, NULL);
|
||||
timersub(&end_time, &now, &res);
|
||||
//printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
|
||||
//if (res.tv_sec > 0 || res.tv_usec > 0)
|
||||
// printf("expired %d %d\n", res.tv_sec, res.tv_usec);
|
||||
return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0);
|
||||
}
|
||||
|
||||
|
||||
void countdown_ms(int ms)
|
||||
{
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
struct timeval interval = {ms / 1000, (ms % 1000) * 1000};
|
||||
//printf("interval %d %d\n", interval.tv_sec, interval.tv_usec);
|
||||
timeradd(&now, &interval, &end_time);
|
||||
}
|
||||
|
||||
|
||||
void countdown(int seconds)
|
||||
{
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
struct timeval interval = {seconds, 0};
|
||||
timeradd(&now, &interval, &end_time);
|
||||
}
|
||||
|
||||
|
||||
int left_ms()
|
||||
{
|
||||
struct timeval now, res;
|
||||
gettimeofday(&now, NULL);
|
||||
timersub(&end_time, &now, &res);
|
||||
//printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
|
||||
return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct timeval end_time;
|
||||
};
|
||||
|
||||
|
||||
int arrivedcount = 0;
|
||||
|
||||
void messageArrived(MQTT::MessageData& md)
|
||||
{
|
||||
MQTT::Message &message = md.message;
|
||||
|
||||
printf("Message %d arrived: qos %d, retained %d, dup %d, packetid %d\n",
|
||||
++arrivedcount, message.qos, message.retained, message.dup, message.id);
|
||||
printf("Payload %.*s\n", message.payloadlen, (char*)message.payload);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
IPStack ipstack = IPStack();
|
||||
float version = 0.3;
|
||||
const char* topic = "mbed-sample";
|
||||
|
||||
printf("Version is %f\n", version);
|
||||
|
||||
MQTT::Client<IPStack, Countdown> client = MQTT::Client<IPStack, Countdown>(ipstack);
|
||||
|
||||
const char* hostname = "localhost"; //"m2m.eclipse.org";
|
||||
int port = 1883;
|
||||
printf("Connecting to %s:%d\n", hostname, port);
|
||||
int rc = ipstack.connect(hostname, port);
|
||||
if (rc != 0)
|
||||
printf("rc from TCP connect is %d\n", rc);
|
||||
|
||||
printf("MQTT connecting\n");
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
data.MQTTVersion = 3;
|
||||
data.clientID.cstring = (char*)"mbed-icraggs";
|
||||
rc = client.connect(&data);
|
||||
if (rc != 0)
|
||||
printf("rc from MQTT connect is %d\n", rc);
|
||||
printf("MQTT connected\n");
|
||||
|
||||
rc = client.subscribe("+", MQTT::QOS2, messageArrived);
|
||||
if (rc != 0)
|
||||
printf("rc from MQTT subscribe is %d\n", rc);
|
||||
|
||||
MQTT::Message message;
|
||||
|
||||
// QoS 0
|
||||
char buf[100];
|
||||
sprintf(buf, "Hello World! QoS 0 message from app version %f", version);
|
||||
message.qos = MQTT::QOS0;
|
||||
message.retained = false;
|
||||
message.dup = false;
|
||||
message.payload = (void*)buf;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
rc = client.publish(topic, &message);
|
||||
while (arrivedcount == 0)
|
||||
client.yield(100);
|
||||
|
||||
// QoS 1
|
||||
printf("Now QoS 1\n");
|
||||
sprintf(buf, "Hello World! QoS 1 message from app version %f", version);
|
||||
message.qos = MQTT::QOS1;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
rc = client.publish(topic, &message);
|
||||
while (arrivedcount == 1)
|
||||
client.yield(100);
|
||||
|
||||
// QoS 2
|
||||
sprintf(buf, "Hello World! QoS 2 message from app version %f", version);
|
||||
message.qos = MQTT::QOS2;
|
||||
message.payloadlen = strlen(buf)+1;
|
||||
rc = client.publish(topic, &message);
|
||||
while (arrivedcount == 2)
|
||||
client.yield(100);
|
||||
|
||||
rc = client.unsubscribe(topic);
|
||||
if (rc != 0)
|
||||
printf("rc from unsubscribe was %d\n", rc);
|
||||
|
||||
rc = client.disconnect();
|
||||
if (rc != 0)
|
||||
printf("rc from disconnect was %d\n", rc);
|
||||
|
||||
ipstack.disconnect();
|
||||
|
||||
printf("Finishing with %d messages received\n", arrivedcount);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
265
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/stdoutsub.cpp
vendored
Normal file
265
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/samples/linux/stdoutsub.cpp
vendored
Normal file
@@ -0,0 +1,265 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2012, 2013 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial contribution
|
||||
* Ian Craggs - change delimiter option from char to string
|
||||
*******************************************************************************/
|
||||
|
||||
/*
|
||||
|
||||
stdout subscriber
|
||||
|
||||
compulsory parameters:
|
||||
|
||||
topic to subscribe to
|
||||
|
||||
defaulted parameters:
|
||||
|
||||
--host localhost
|
||||
--port 1883
|
||||
--qos 2
|
||||
--delimiter \n
|
||||
--clientid stdout_subscriber
|
||||
|
||||
--userid none
|
||||
--password none
|
||||
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <memory.h>
|
||||
#define MQTT_DEBUG 1
|
||||
#include "MQTTClient.h"
|
||||
|
||||
#define DEFAULT_STACK_SIZE -1
|
||||
|
||||
#include "linux.cpp"
|
||||
|
||||
#include <signal.h>
|
||||
#include <sys/time.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
volatile int toStop = 0;
|
||||
|
||||
|
||||
void usage()
|
||||
{
|
||||
printf("MQTT stdout subscriber\n");
|
||||
printf("Usage: stdoutsub topicname <options>, where options are:\n");
|
||||
printf(" --host <hostname> (default is localhost)\n");
|
||||
printf(" --port <port> (default is 1883)\n");
|
||||
printf(" --qos <qos> (default is 2)\n");
|
||||
printf(" --delimiter <delim> (default is \\n)\n");
|
||||
printf(" --clientid <clientid> (default is hostname+timestamp)\n");
|
||||
printf(" --username none\n");
|
||||
printf(" --password none\n");
|
||||
printf(" --showtopics <on or off> (default is on if the topic has a wildcard, else off)\n");
|
||||
exit(-1);
|
||||
}
|
||||
|
||||
|
||||
void cfinish(int sig)
|
||||
{
|
||||
signal(SIGINT, NULL);
|
||||
toStop = 1;
|
||||
}
|
||||
|
||||
|
||||
struct opts_struct
|
||||
{
|
||||
char* clientid;
|
||||
int nodelimiter;
|
||||
char* delimiter;
|
||||
MQTT::QoS qos;
|
||||
char* username;
|
||||
char* password;
|
||||
char* host;
|
||||
int port;
|
||||
int showtopics;
|
||||
} opts =
|
||||
{
|
||||
(char*)"stdout-subscriber", 0, (char*)"\n", MQTT::QOS2, NULL, NULL, (char*)"localhost", 1883, 0
|
||||
};
|
||||
|
||||
|
||||
void getopts(int argc, char** argv)
|
||||
{
|
||||
int count = 2;
|
||||
|
||||
while (count < argc)
|
||||
{
|
||||
if (strcmp(argv[count], "--qos") == 0)
|
||||
{
|
||||
if (++count < argc)
|
||||
{
|
||||
if (strcmp(argv[count], "0") == 0)
|
||||
opts.qos = MQTT::QOS0;
|
||||
else if (strcmp(argv[count], "1") == 0)
|
||||
opts.qos = MQTT::QOS1;
|
||||
else if (strcmp(argv[count], "2") == 0)
|
||||
opts.qos = MQTT::QOS2;
|
||||
else
|
||||
usage();
|
||||
}
|
||||
else
|
||||
usage();
|
||||
}
|
||||
else if (strcmp(argv[count], "--host") == 0)
|
||||
{
|
||||
if (++count < argc)
|
||||
opts.host = argv[count];
|
||||
else
|
||||
usage();
|
||||
}
|
||||
else if (strcmp(argv[count], "--port") == 0)
|
||||
{
|
||||
if (++count < argc)
|
||||
opts.port = atoi(argv[count]);
|
||||
else
|
||||
usage();
|
||||
}
|
||||
else if (strcmp(argv[count], "--clientid") == 0)
|
||||
{
|
||||
if (++count < argc)
|
||||
opts.clientid = argv[count];
|
||||
else
|
||||
usage();
|
||||
}
|
||||
else if (strcmp(argv[count], "--username") == 0)
|
||||
{
|
||||
if (++count < argc)
|
||||
opts.username = argv[count];
|
||||
else
|
||||
usage();
|
||||
}
|
||||
else if (strcmp(argv[count], "--password") == 0)
|
||||
{
|
||||
if (++count < argc)
|
||||
opts.password = argv[count];
|
||||
else
|
||||
usage();
|
||||
}
|
||||
else if (strcmp(argv[count], "--delimiter") == 0)
|
||||
{
|
||||
if (++count < argc)
|
||||
opts.delimiter = argv[count];
|
||||
else
|
||||
opts.nodelimiter = 1;
|
||||
}
|
||||
else if (strcmp(argv[count], "--showtopics") == 0)
|
||||
{
|
||||
if (++count < argc)
|
||||
{
|
||||
if (strcmp(argv[count], "on") == 0)
|
||||
opts.showtopics = 1;
|
||||
else if (strcmp(argv[count], "off") == 0)
|
||||
opts.showtopics = 0;
|
||||
else
|
||||
usage();
|
||||
}
|
||||
else
|
||||
usage();
|
||||
}
|
||||
count++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void myconnect(IPStack& ipstack, MQTT::Client<IPStack, Countdown, 1000>& client, MQTTPacket_connectData& data)
|
||||
{
|
||||
printf("Connecting to %s:%d\n", opts.host, opts.port);
|
||||
int rc = ipstack.connect(opts.host, opts.port);
|
||||
if (rc != 0)
|
||||
printf("rc from TCP connect is %d\n", rc);
|
||||
|
||||
rc = client.connect(data);
|
||||
if (rc != 0)
|
||||
{
|
||||
printf("Failed to connect, return code %d\n", rc);
|
||||
exit(-1);
|
||||
}
|
||||
printf("Connected\n");
|
||||
}
|
||||
|
||||
|
||||
void messageArrived(MQTT::MessageData& md)
|
||||
{
|
||||
MQTT::Message &message = md.message;
|
||||
|
||||
if (opts.showtopics)
|
||||
printf("%.*s\t", md.topicName.lenstring.len, md.topicName.lenstring.data);
|
||||
if (opts.nodelimiter)
|
||||
printf("%.*s", (int)message.payloadlen, (char*)message.payload);
|
||||
else
|
||||
printf("%.*s%s", (int)message.payloadlen, (char*)message.payload, opts.delimiter);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (argc < 2)
|
||||
usage();
|
||||
|
||||
char* topic = argv[1];
|
||||
|
||||
if (strchr(topic, '#') || strchr(topic, '+'))
|
||||
opts.showtopics = 1;
|
||||
if (opts.showtopics)
|
||||
printf("topic is %s\n", topic);
|
||||
|
||||
getopts(argc, argv);
|
||||
|
||||
IPStack ipstack = IPStack();
|
||||
MQTT::Client<IPStack, Countdown, 1000> client = MQTT::Client<IPStack, Countdown, 1000>(ipstack);
|
||||
|
||||
signal(SIGINT, cfinish);
|
||||
signal(SIGTERM, cfinish);
|
||||
|
||||
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
|
||||
data.willFlag = 0;
|
||||
data.MQTTVersion = 3;
|
||||
data.clientID.cstring = opts.clientid;
|
||||
data.username.cstring = opts.username;
|
||||
data.password.cstring = opts.password;
|
||||
|
||||
data.keepAliveInterval = 10;
|
||||
data.cleansession = 1;
|
||||
printf("will flag %d\n", data.willFlag);
|
||||
|
||||
myconnect(ipstack, client, data);
|
||||
|
||||
rc = client.subscribe(topic, opts.qos, messageArrived);
|
||||
printf("Subscribed %d\n", rc);
|
||||
|
||||
while (!toStop)
|
||||
{
|
||||
client.yield(1000);
|
||||
|
||||
//if (!client.isconnected)
|
||||
// myconnect(ipstack, client, data);
|
||||
}
|
||||
|
||||
printf("Stopping\n");
|
||||
|
||||
rc = client.disconnect();
|
||||
|
||||
ipstack.disconnect();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
208
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/FP.h
vendored
Normal file
208
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/FP.h
vendored
Normal file
@@ -0,0 +1,208 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2013, 2014
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Sam Grove - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - added attached and detached member functions
|
||||
* Sam Grove - removed need for FP.cpp
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef FP_H
|
||||
#define FP_H
|
||||
|
||||
/** Example using the FP Class with global functions
|
||||
* @code
|
||||
* #include "mbed.h"
|
||||
* #include "FP.h"
|
||||
*
|
||||
* FP<void,bool>fp;
|
||||
* DigitalOut myled(LED1);
|
||||
*
|
||||
* void handler(bool value)
|
||||
* {
|
||||
* myled = value;
|
||||
* return;
|
||||
* }
|
||||
*
|
||||
* int main()
|
||||
* {
|
||||
* fp.attach(&handler);
|
||||
*
|
||||
* while(1)
|
||||
* {
|
||||
* fp(1);
|
||||
* wait(0.2);
|
||||
* fp(0);
|
||||
* wait(0.2);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
/** Example using the FP Class with different class member functions
|
||||
* @code
|
||||
* #include "mbed.h"
|
||||
* #include "FP.h"
|
||||
*
|
||||
* FP<void,bool>fp;
|
||||
* DigitalOut myled(LED4);
|
||||
*
|
||||
* class Wrapper
|
||||
* {
|
||||
* public:
|
||||
* Wrapper(){}
|
||||
*
|
||||
* void handler(bool value)
|
||||
* {
|
||||
* myled = value;
|
||||
* return;
|
||||
* }
|
||||
* };
|
||||
*
|
||||
* int main()
|
||||
* {
|
||||
* Wrapper wrapped;
|
||||
* fp.attach(&wrapped, &Wrapper::handler);
|
||||
*
|
||||
* while(1)
|
||||
* {
|
||||
* fp(1);
|
||||
* wait(0.2);
|
||||
* fp(0);
|
||||
* wait(0.2);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
/** Example using the FP Class with member FP and member function
|
||||
* @code
|
||||
* #include "mbed.h"
|
||||
* #include "FP.h"
|
||||
*
|
||||
* DigitalOut myled(LED2);
|
||||
*
|
||||
* class Wrapper
|
||||
* {
|
||||
* public:
|
||||
* Wrapper()
|
||||
* {
|
||||
* fp.attach(this, &Wrapper::handler);
|
||||
* }
|
||||
*
|
||||
* void handler(bool value)
|
||||
* {
|
||||
* myled = value;
|
||||
* return;
|
||||
* }
|
||||
*
|
||||
* FP<void,bool>fp;
|
||||
* };
|
||||
*
|
||||
* int main()
|
||||
* {
|
||||
* Wrapper wrapped;
|
||||
*
|
||||
* while(1)
|
||||
* {
|
||||
* wrapped.fp(1);
|
||||
* wait(0.2);
|
||||
* wrapped.fp(0);
|
||||
* wait(0.2);
|
||||
* }
|
||||
* }
|
||||
* @endcode
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class FP
|
||||
* @brief API for managing Function Pointers
|
||||
*/
|
||||
template<class retT, class argT>
|
||||
class FP
|
||||
{
|
||||
public:
|
||||
/** Create the FP object - only one callback can be attached to the object, that is
|
||||
* a member function or a global function, not both at the same time
|
||||
*/
|
||||
FP()
|
||||
{
|
||||
obj_callback = 0;
|
||||
c_callback = 0;
|
||||
}
|
||||
|
||||
/** Add a callback function to the object
|
||||
* @param item - Address of the initialized object
|
||||
* @param member - Address of the member function (dont forget the scope that the function is defined in)
|
||||
*/
|
||||
template<class T>
|
||||
void attach(T *item, retT (T::*method)(argT))
|
||||
{
|
||||
obj_callback = (FPtrDummy *)(item);
|
||||
method_callback = (retT (FPtrDummy::*)(argT))(method);
|
||||
return;
|
||||
}
|
||||
|
||||
/** Add a callback function to the object
|
||||
* @param function - The address of a globally defined function
|
||||
*/
|
||||
void attach(retT (*function)(argT))
|
||||
{
|
||||
c_callback = function;
|
||||
}
|
||||
|
||||
/** Invoke the function attached to the class
|
||||
* @param arg - An argument that is passed into the function handler that is called
|
||||
* @return The return from the function hanlder called by this class
|
||||
*/
|
||||
retT operator()(argT arg) const
|
||||
{
|
||||
if( 0 != c_callback ) {
|
||||
return obj_callback ? (obj_callback->*method_callback)(arg) : (*c_callback)(arg);
|
||||
}
|
||||
return (retT)0;
|
||||
}
|
||||
|
||||
/** Determine if an callback is currently hooked
|
||||
* @return 1 if a method is hooked, 0 otherwise
|
||||
*/
|
||||
bool attached()
|
||||
{
|
||||
return obj_callback || c_callback;
|
||||
}
|
||||
|
||||
/** Release a function from the callback hook
|
||||
*/
|
||||
void detach()
|
||||
{
|
||||
obj_callback = 0;
|
||||
c_callback = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
// empty type used for casting
|
||||
class FPtrDummy;
|
||||
|
||||
FPtrDummy *obj_callback;
|
||||
|
||||
/**
|
||||
* @union Funciton
|
||||
* @brief Member or global callback function
|
||||
*/
|
||||
union {
|
||||
retT (*c_callback)(argT); /*!< Footprint for a global function */
|
||||
retT (FPtrDummy::*method_callback)(argT); /*!< Footprint for a member function */
|
||||
};
|
||||
};
|
||||
|
||||
#endif
|
||||
1052
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/MQTTClient.h
vendored
Normal file
1052
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/MQTTClient.h
vendored
Normal file
File diff suppressed because it is too large
Load Diff
55
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/MQTTLogging.h
vendored
Normal file
55
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/MQTTLogging.h
vendored
Normal file
@@ -0,0 +1,55 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(MQTT_LOGGING_H)
|
||||
#define MQTT_LOGGING_H
|
||||
|
||||
#define STREAM stdout
|
||||
#if !defined(DEBUG)
|
||||
#define DEBUG(...) \
|
||||
{\
|
||||
fprintf(STREAM, "DEBUG: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \
|
||||
fprintf(STREAM, ##__VA_ARGS__); \
|
||||
fflush(STREAM); \
|
||||
}
|
||||
#endif
|
||||
#if !defined(LOG)
|
||||
#define LOG(...) \
|
||||
{\
|
||||
fprintf(STREAM, "LOG: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \
|
||||
fprintf(STREAM, ##__VA_ARGS__); \
|
||||
fflush(STREAM); \
|
||||
}
|
||||
#endif
|
||||
#if !defined(WARN)
|
||||
#define WARN(...) \
|
||||
{ \
|
||||
fprintf(STREAM, "WARN: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \
|
||||
fprintf(STREAM, ##__VA_ARGS__); \
|
||||
fflush(STREAM); \
|
||||
}
|
||||
#endif
|
||||
#if !defined(ERROR)
|
||||
#define ERROR(...) \
|
||||
{ \
|
||||
fprintf(STREAM, "ERROR: %s L#%d ", __PRETTY_FUNCTION__, __LINE__); \
|
||||
fprintf(STREAM, ##__VA_ARGS__); \
|
||||
fflush(STREAM); \
|
||||
exit(1); \
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
57
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/arduino/Countdown.h
vendored
Normal file
57
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/arduino/Countdown.h
vendored
Normal file
@@ -0,0 +1,57 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(COUNTDOWN_H)
|
||||
#define COUNTDOWN_H
|
||||
|
||||
class Countdown
|
||||
{
|
||||
public:
|
||||
Countdown()
|
||||
{
|
||||
interval_end_ms = 0L;
|
||||
}
|
||||
|
||||
Countdown(int ms)
|
||||
{
|
||||
countdown_ms(ms);
|
||||
}
|
||||
|
||||
bool expired()
|
||||
{
|
||||
return (interval_end_ms > 0L) && (millis() >= interval_end_ms);
|
||||
}
|
||||
|
||||
void countdown_ms(unsigned long ms)
|
||||
{
|
||||
interval_end_ms = millis() + ms;
|
||||
}
|
||||
|
||||
void countdown(int seconds)
|
||||
{
|
||||
countdown_ms((unsigned long)seconds * 1000L);
|
||||
}
|
||||
|
||||
int left_ms()
|
||||
{
|
||||
return interval_end_ms - millis();
|
||||
}
|
||||
|
||||
private:
|
||||
unsigned long interval_end_ms;
|
||||
};
|
||||
|
||||
#endif
|
||||
79
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/arduino/IPStack.h
vendored
Normal file
79
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/arduino/IPStack.h
vendored
Normal file
@@ -0,0 +1,79 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2017 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Benjamin Cabe - generic IPStack
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(IPSTACK_H)
|
||||
#define IPSTACK_H
|
||||
|
||||
#ifndef WiFi_h
|
||||
#include <SPI.h>
|
||||
#endif
|
||||
|
||||
#include <Client.h>
|
||||
|
||||
class IPStack
|
||||
{
|
||||
public:
|
||||
IPStack(Client& client) : client(&client)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int connect(char* hostname, int port)
|
||||
{
|
||||
return client->connect(hostname, port);
|
||||
}
|
||||
|
||||
int connect(uint32_t hostname, int port)
|
||||
{
|
||||
return client->connect(hostname, port);
|
||||
}
|
||||
|
||||
int read(unsigned char* buffer, int len, int timeout)
|
||||
{
|
||||
int interval = 10; // all times are in milliseconds
|
||||
int total = 0, rc = -1;
|
||||
|
||||
if (timeout < 30)
|
||||
interval = 2;
|
||||
while (client->available() < len && total < timeout)
|
||||
{
|
||||
delay(interval);
|
||||
total += interval;
|
||||
}
|
||||
if (client->available() >= len)
|
||||
rc = client->readBytes((char*)buffer, len);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int write(unsigned char* buffer, int len, int timeout)
|
||||
{
|
||||
client->setTimeout(timeout);
|
||||
return client->write((uint8_t*)buffer, len);
|
||||
}
|
||||
|
||||
int disconnect()
|
||||
{
|
||||
client->stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
Client* client;
|
||||
};
|
||||
|
||||
#endif
|
||||
69
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/arduino/WifiIPStack.h
vendored
Normal file
69
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/arduino/WifiIPStack.h
vendored
Normal file
@@ -0,0 +1,69 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef ARDUINOWIFIIPSTACK_H
|
||||
#define ARDUINOWIFIIPSTACK_H
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
class WifiIPStack
|
||||
{
|
||||
public:
|
||||
WifiIPStack()
|
||||
{
|
||||
//WiFi.begin(); // Use DHCP
|
||||
iface.setTimeout(1000); // 1 second Timeout
|
||||
}
|
||||
|
||||
int connect(char* hostname, int port)
|
||||
{
|
||||
return iface.connect(hostname, port);
|
||||
}
|
||||
|
||||
int connect(uint32_t hostname, int port)
|
||||
{
|
||||
return iface.connect(hostname, port);
|
||||
}
|
||||
|
||||
int read(char* buffer, int len, int timeout)
|
||||
{
|
||||
iface.setTimeout(timeout);
|
||||
while(!iface.available());
|
||||
return iface.readBytes(buffer, len);
|
||||
}
|
||||
|
||||
int write(char* buffer, int len, int timeout)
|
||||
{
|
||||
iface.setTimeout(timeout);
|
||||
return iface.write((uint8_t*)buffer, len);
|
||||
}
|
||||
|
||||
int disconnect()
|
||||
{
|
||||
iface.stop();
|
||||
return 0;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
WiFiClient iface;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
213
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/linux/linux.cpp
vendored
Normal file
213
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/linux/linux.cpp
vendored
Normal file
@@ -0,0 +1,213 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2017 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - ensure read returns if no bytes read
|
||||
*******************************************************************************/
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/param.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/select.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <netdb.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
|
||||
|
||||
class IPStack
|
||||
{
|
||||
public:
|
||||
IPStack()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
int connect(const char* hostname, int port)
|
||||
{
|
||||
int type = SOCK_STREAM;
|
||||
struct sockaddr_in address;
|
||||
int rc = -1;
|
||||
sa_family_t family = AF_INET;
|
||||
struct addrinfo *result = NULL;
|
||||
struct addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, 0, NULL, NULL, NULL};
|
||||
|
||||
if ((rc = getaddrinfo(hostname, NULL, &hints, &result)) == 0)
|
||||
{
|
||||
struct addrinfo* res = result;
|
||||
|
||||
/* prefer ip4 addresses */
|
||||
while (res)
|
||||
{
|
||||
if (res->ai_family == AF_INET)
|
||||
{
|
||||
result = res;
|
||||
break;
|
||||
}
|
||||
res = res->ai_next;
|
||||
}
|
||||
|
||||
if (result->ai_family == AF_INET)
|
||||
{
|
||||
address.sin_port = htons(port);
|
||||
address.sin_family = family = AF_INET;
|
||||
address.sin_addr = ((struct sockaddr_in*)(result->ai_addr))->sin_addr;
|
||||
}
|
||||
else
|
||||
rc = -1;
|
||||
|
||||
freeaddrinfo(result);
|
||||
}
|
||||
|
||||
if (rc == 0)
|
||||
{
|
||||
mysock = socket(family, type, 0);
|
||||
if (mysock != -1)
|
||||
{
|
||||
int opt = 1;
|
||||
|
||||
//if (setsockopt(mysock, SOL_SOCKET, SO_NOSIGPIPE, (void*)&opt, sizeof(opt)) != 0)
|
||||
// printf("Could not set SO_NOSIGPIPE for socket %d", mysock);
|
||||
|
||||
rc = ::connect(mysock, (struct sockaddr*)&address, sizeof(address));
|
||||
}
|
||||
}
|
||||
|
||||
return rc;
|
||||
}
|
||||
|
||||
// return -1 on error, or the number of bytes read
|
||||
// which could be 0 on a read timeout
|
||||
int read(unsigned char* buffer, int len, int timeout_ms)
|
||||
{
|
||||
struct timeval interval = {timeout_ms / 1000, (timeout_ms % 1000) * 1000};
|
||||
if (interval.tv_sec < 0 || (interval.tv_sec == 0 && interval.tv_usec <= 0))
|
||||
{
|
||||
interval.tv_sec = 0;
|
||||
interval.tv_usec = 100;
|
||||
}
|
||||
|
||||
setsockopt(mysock, SOL_SOCKET, SO_RCVTIMEO, (char *)&interval, sizeof(struct timeval));
|
||||
|
||||
int bytes = 0;
|
||||
int i = 0; const int max_tries = 10;
|
||||
while (bytes < len)
|
||||
{
|
||||
int rc = ::recv(mysock, &buffer[bytes], (size_t)(len - bytes), 0);
|
||||
if (rc == -1)
|
||||
{
|
||||
if (errno != EAGAIN && errno != EWOULDBLOCK)
|
||||
bytes = -1;
|
||||
break;
|
||||
}
|
||||
else
|
||||
bytes += rc;
|
||||
if (++i >= max_tries)
|
||||
break;
|
||||
if (rc == 0)
|
||||
break;
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
int write(unsigned char* buffer, int len, int timeout)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
tv.tv_sec = 0; /* 30 Secs Timeout */
|
||||
tv.tv_usec = timeout * 1000; // Not init'ing this can cause strange errors
|
||||
|
||||
setsockopt(mysock, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
|
||||
int rc = ::write(mysock, buffer, len);
|
||||
//printf("write rc %d\n", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int disconnect()
|
||||
{
|
||||
return ::close(mysock);
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
int mysock;
|
||||
};
|
||||
|
||||
|
||||
class Countdown
|
||||
{
|
||||
public:
|
||||
Countdown()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Countdown(int ms)
|
||||
{
|
||||
countdown_ms(ms);
|
||||
}
|
||||
|
||||
|
||||
bool expired()
|
||||
{
|
||||
struct timeval now, res;
|
||||
gettimeofday(&now, NULL);
|
||||
timersub(&end_time, &now, &res);
|
||||
//printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
|
||||
//if (res.tv_sec > 0 || res.tv_usec > 0)
|
||||
// printf("expired %d %d\n", res.tv_sec, res.tv_usec);
|
||||
return res.tv_sec < 0 || (res.tv_sec == 0 && res.tv_usec <= 0);
|
||||
}
|
||||
|
||||
|
||||
void countdown_ms(int ms)
|
||||
{
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
struct timeval interval = {ms / 1000, (ms % 1000) * 1000};
|
||||
//printf("interval %d %d\n", interval.tv_sec, interval.tv_usec);
|
||||
timeradd(&now, &interval, &end_time);
|
||||
}
|
||||
|
||||
|
||||
void countdown(int seconds)
|
||||
{
|
||||
struct timeval now;
|
||||
gettimeofday(&now, NULL);
|
||||
struct timeval interval = {seconds, 0};
|
||||
timeradd(&now, &interval, &end_time);
|
||||
}
|
||||
|
||||
|
||||
int left_ms()
|
||||
{
|
||||
struct timeval now, res;
|
||||
gettimeofday(&now, NULL);
|
||||
timersub(&end_time, &now, &res);
|
||||
//printf("left %d ms\n", (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000);
|
||||
return (res.tv_sec < 0) ? 0 : res.tv_sec * 1000 + res.tv_usec / 1000;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
struct timeval end_time;
|
||||
};
|
||||
29
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/mbed/MQTTEthernet.h
vendored
Normal file
29
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/mbed/MQTTEthernet.h
vendored
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
#if !defined(MQTTETHERNET_H)
|
||||
#define MQTTETHERNET_H
|
||||
|
||||
#include "MQTTmbed.h"
|
||||
#include "EthernetInterface.h"
|
||||
#include "MQTTSocket.h"
|
||||
|
||||
class MQTTEthernet : public MQTTSocket
|
||||
{
|
||||
public:
|
||||
MQTTEthernet() : MQTTSocket(ð)
|
||||
{
|
||||
eth.connect();
|
||||
}
|
||||
|
||||
EthernetInterface& getEth()
|
||||
{
|
||||
return eth;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
EthernetInterface eth;
|
||||
|
||||
};
|
||||
|
||||
|
||||
#endif
|
||||
97
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/mbed/MQTTSocket.h
vendored
Normal file
97
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/mbed/MQTTSocket.h
vendored
Normal file
@@ -0,0 +1,97 @@
|
||||
#if !defined(MQTTSOCKET_H)
|
||||
#define MQTTSOCKET_H
|
||||
|
||||
#include "MQTTmbed.h"
|
||||
#include <EthernetInterface.h>
|
||||
#include <Timer.h>
|
||||
|
||||
class MQTTSocket
|
||||
{
|
||||
public:
|
||||
MQTTSocket(EthernetInterface *anet)
|
||||
{
|
||||
net = anet;
|
||||
open = false;
|
||||
}
|
||||
|
||||
int connect(char* hostname, int port, int timeout=1000)
|
||||
{
|
||||
if (open)
|
||||
disconnect();
|
||||
nsapi_error_t rc = mysock.open(net);
|
||||
open = true;
|
||||
mysock.set_blocking(true);
|
||||
mysock.set_timeout((unsigned int)timeout);
|
||||
rc = mysock.connect(hostname, port);
|
||||
mysock.set_blocking(false); // blocking timeouts seem not to work
|
||||
return rc;
|
||||
}
|
||||
|
||||
// common read/write routine, avoiding blocking timeouts
|
||||
int common(unsigned char* buffer, int len, int timeout, bool read)
|
||||
{
|
||||
timer.start();
|
||||
mysock.set_blocking(false); // blocking timeouts seem not to work
|
||||
int bytes = 0;
|
||||
bool first = true;
|
||||
do
|
||||
{
|
||||
if (first)
|
||||
first = false;
|
||||
else
|
||||
wait_ms(timeout < 100 ? timeout : 100);
|
||||
int rc;
|
||||
if (read)
|
||||
rc = mysock.recv((char*)buffer, len);
|
||||
else
|
||||
rc = mysock.send((char*)buffer, len);
|
||||
if (rc < 0)
|
||||
{
|
||||
if (rc != NSAPI_ERROR_WOULD_BLOCK)
|
||||
{
|
||||
bytes = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
bytes += rc;
|
||||
}
|
||||
while (bytes < len && timer.read_ms() < timeout);
|
||||
timer.stop();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/* returns the number of bytes read, which could be 0.
|
||||
-1 if there was an error on the socket
|
||||
*/
|
||||
int read(unsigned char* buffer, int len, int timeout)
|
||||
{
|
||||
return common(buffer, len, timeout, true);
|
||||
}
|
||||
|
||||
int write(unsigned char* buffer, int len, int timeout)
|
||||
{
|
||||
return common(buffer, len, timeout, false);
|
||||
}
|
||||
|
||||
int disconnect()
|
||||
{
|
||||
open = false;
|
||||
return mysock.close();
|
||||
}
|
||||
|
||||
/*bool is_connected()
|
||||
{
|
||||
return mysock.is_connected();
|
||||
}*/
|
||||
|
||||
private:
|
||||
|
||||
bool open;
|
||||
TCPSocket mysock;
|
||||
EthernetInterface *net;
|
||||
Timer timer;
|
||||
|
||||
};
|
||||
|
||||
#endif
|
||||
65
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/mbed/MQTTmbed.h
vendored
Normal file
65
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTClient/src/mbed/MQTTmbed.h
vendored
Normal file
@@ -0,0 +1,65 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2017 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - change Timer member initialization to avoid copy constructor
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(MQTT_MBED_H)
|
||||
#define MQTT_MBED_H
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
class Countdown
|
||||
{
|
||||
public:
|
||||
Countdown() : t()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
Countdown(int ms) : t()
|
||||
{
|
||||
countdown_ms(ms);
|
||||
}
|
||||
|
||||
|
||||
bool expired()
|
||||
{
|
||||
return t.read_ms() >= interval_end_ms;
|
||||
}
|
||||
|
||||
void countdown_ms(unsigned long ms)
|
||||
{
|
||||
t.stop();
|
||||
interval_end_ms = ms;
|
||||
t.reset();
|
||||
t.start();
|
||||
}
|
||||
|
||||
void countdown(int seconds)
|
||||
{
|
||||
countdown_ms((unsigned long)seconds * 1000L);
|
||||
}
|
||||
|
||||
int left_ms()
|
||||
{
|
||||
return interval_end_ms - t.read_ms();
|
||||
}
|
||||
|
||||
private:
|
||||
Timer t;
|
||||
unsigned long interval_end_ms;
|
||||
};
|
||||
|
||||
#endif
|
||||
21
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/CMakeLists.txt
vendored
Normal file
21
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,21 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2017 IBM Corp.
|
||||
#
|
||||
# All rights reserved. This program and the accompanying materials
|
||||
# are made available under the terms of the Eclipse Public License v1.0
|
||||
# and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
#
|
||||
# The Eclipse Public License is available at
|
||||
# http://www.eclipse.org/legal/epl-v10.html
|
||||
# and the Eclipse Distribution License is available at
|
||||
# http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
#
|
||||
# Contributors:
|
||||
# Ian Craggs - initial version
|
||||
#*******************************************************************************/
|
||||
|
||||
project("paho-mqttpacket" C)
|
||||
|
||||
ADD_SUBDIRECTORY(src)
|
||||
ADD_SUBDIRECTORY(samples)
|
||||
ADD_SUBDIRECTORY(test)
|
||||
31
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/CMakeLists.txt
vendored
Normal file
31
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/CMakeLists.txt
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
#*******************************************************************************
|
||||
# Copyright (c) 2017 IBM Corp.
|
||||
#
|
||||
# All rights reserved. This program and the accompanying materials
|
||||
# are made available under the terms of the Eclipse Public License v1.0
|
||||
# and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
#
|
||||
# The Eclipse Public License is available at
|
||||
# http://www.eclipse.org/legal/epl-v10.html
|
||||
# and the Eclipse Distribution License is available at
|
||||
# http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
#
|
||||
# Contributors:
|
||||
# Ian Craggs - initial version
|
||||
#*******************************************************************************/
|
||||
|
||||
# MQTTPacket Library
|
||||
file(GLOB SOURCES "*.c")
|
||||
add_library(paho-embed-mqtt3c SHARED ${SOURCES})
|
||||
install(TARGETS paho-embed-mqtt3c DESTINATION /usr/lib)
|
||||
target_compile_definitions(paho-embed-mqtt3c PRIVATE MQTT_SERVER MQTT_CLIENT)
|
||||
|
||||
add_library(MQTTPacketClient SHARED MQTTFormat MQTTPacket
|
||||
MQTTSerializePublish MQTTDeserializePublish
|
||||
MQTTConnectClient MQTTSubscribeClient MQTTUnsubscribeClient)
|
||||
target_compile_definitions(MQTTPacketClient PRIVATE MQTT_CLIENT)
|
||||
|
||||
add_library(MQTTPacketServer SHARED MQTTFormat MQTTPacket
|
||||
MQTTSerializePublish MQTTDeserializePublish
|
||||
MQTTConnectServer MQTTSubscribeServer MQTTUnsubscribeServer)
|
||||
target_compile_definitions(MQTTPacketServer PRIVATE MQTT_SERVER)
|
||||
148
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTConnect.h
vendored
Normal file
148
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTConnect.h
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014, 2017 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - add connack return code definitions
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
* Ian Craggs - fix for issue #64, bit order in connack response
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTCONNECT_H_
|
||||
#define MQTTCONNECT_H_
|
||||
|
||||
enum connack_return_codes
|
||||
{
|
||||
MQTT_CONNECTION_ACCEPTED = 0,
|
||||
MQTT_UNNACCEPTABLE_PROTOCOL = 1,
|
||||
MQTT_CLIENTID_REJECTED = 2,
|
||||
MQTT_SERVER_UNAVAILABLE = 3,
|
||||
MQTT_BAD_USERNAME_OR_PASSWORD = 4,
|
||||
MQTT_NOT_AUTHORIZED = 5,
|
||||
};
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
|
||||
typedef union
|
||||
{
|
||||
unsigned char all; /**< all connect flags */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int username : 1; /**< 3.1 user name */
|
||||
unsigned int password : 1; /**< 3.1 password */
|
||||
unsigned int willRetain : 1; /**< will retain setting */
|
||||
unsigned int willQoS : 2; /**< will QoS value */
|
||||
unsigned int will : 1; /**< will flag */
|
||||
unsigned int cleansession : 1; /**< clean session flag */
|
||||
unsigned int : 1; /**< unused */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int : 1; /**< unused */
|
||||
unsigned int cleansession : 1; /**< cleansession flag */
|
||||
unsigned int will : 1; /**< will flag */
|
||||
unsigned int willQoS : 2; /**< will QoS value */
|
||||
unsigned int willRetain : 1; /**< will retain setting */
|
||||
unsigned int password : 1; /**< 3.1 password */
|
||||
unsigned int username : 1; /**< 3.1 user name */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTConnectFlags; /**< connect flags byte */
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Defines the MQTT "Last Will and Testament" (LWT) settings for
|
||||
* the connect packet.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
/** The eyecatcher for this structure. must be MQTW. */
|
||||
char struct_id[4];
|
||||
/** The version number of this structure. Must be 0 */
|
||||
int struct_version;
|
||||
/** The LWT topic to which the LWT message will be published. */
|
||||
MQTTString topicName;
|
||||
/** The LWT payload. */
|
||||
MQTTString message;
|
||||
/**
|
||||
* The retained flag for the LWT message (see MQTTAsync_message.retained).
|
||||
*/
|
||||
unsigned char retained;
|
||||
/**
|
||||
* The quality of service setting for the LWT message (see
|
||||
* MQTTAsync_message.qos and @ref qos).
|
||||
*/
|
||||
char qos;
|
||||
} MQTTPacket_willOptions;
|
||||
|
||||
|
||||
#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 }
|
||||
|
||||
|
||||
typedef struct
|
||||
{
|
||||
/** The eyecatcher for this structure. must be MQTC. */
|
||||
char struct_id[4];
|
||||
/** The version number of this structure. Must be 0 */
|
||||
int struct_version;
|
||||
/** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1
|
||||
*/
|
||||
unsigned char MQTTVersion;
|
||||
MQTTString clientID;
|
||||
unsigned short keepAliveInterval;
|
||||
unsigned char cleansession;
|
||||
unsigned char willFlag;
|
||||
MQTTPacket_willOptions will;
|
||||
MQTTString username;
|
||||
MQTTString password;
|
||||
} MQTTPacket_connectData;
|
||||
|
||||
typedef union
|
||||
{
|
||||
unsigned char all; /**< all connack flags */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int reserved : 7; /**< unused */
|
||||
unsigned int sessionpresent : 1; /**< session present flag */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int sessionpresent : 1; /**< session present flag */
|
||||
unsigned int reserved: 7; /**< unused */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTConnackFlags; /**< connack flags byte */
|
||||
|
||||
#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \
|
||||
MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} }
|
||||
|
||||
DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options);
|
||||
DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent);
|
||||
DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen);
|
||||
|
||||
DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen);
|
||||
DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen);
|
||||
|
||||
#endif /* MQTTCONNECT_H_ */
|
||||
214
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTConnectClient.c
vendored
Normal file
214
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTConnectClient.c
vendored
Normal file
@@ -0,0 +1,214 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT connect packet that would be produced using the supplied connect options.
|
||||
* @param options the options to be used to build the connect packet
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_connectLength(MQTTPacket_connectData* options)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
|
||||
if (options->MQTTVersion == 3)
|
||||
len = 12; /* variable depending on MQTT or MQIsdp */
|
||||
else if (options->MQTTVersion == 4)
|
||||
len = 10;
|
||||
|
||||
len += MQTTstrlen(options->clientID)+2;
|
||||
if (options->willFlag)
|
||||
len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2;
|
||||
if (options->username.cstring || options->username.lenstring.data)
|
||||
len += MQTTstrlen(options->username)+2;
|
||||
if (options->password.cstring || options->password.lenstring.data)
|
||||
len += MQTTstrlen(options->password)+2;
|
||||
|
||||
FUNC_EXIT_RC(len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the connect options into the buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param len the length in bytes of the supplied buffer
|
||||
* @param options the options to be used to build the connect packet
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options)
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
MQTTConnectFlags flags = {0};
|
||||
int len = 0;
|
||||
int rc = -1;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = CONNECT;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, len); /* write remaining length */
|
||||
|
||||
if (options->MQTTVersion == 4)
|
||||
{
|
||||
writeCString(&ptr, "MQTT");
|
||||
writeChar(&ptr, (char) 4);
|
||||
}
|
||||
else
|
||||
{
|
||||
writeCString(&ptr, "MQIsdp");
|
||||
writeChar(&ptr, (char) 3);
|
||||
}
|
||||
|
||||
flags.all = 0;
|
||||
flags.bits.cleansession = options->cleansession;
|
||||
flags.bits.will = (options->willFlag) ? 1 : 0;
|
||||
if (flags.bits.will)
|
||||
{
|
||||
flags.bits.willQoS = options->will.qos;
|
||||
flags.bits.willRetain = options->will.retained;
|
||||
}
|
||||
|
||||
if (options->username.cstring || options->username.lenstring.data)
|
||||
flags.bits.username = 1;
|
||||
if (options->password.cstring || options->password.lenstring.data)
|
||||
flags.bits.password = 1;
|
||||
|
||||
writeChar(&ptr, flags.all);
|
||||
writeInt(&ptr, options->keepAliveInterval);
|
||||
writeMQTTString(&ptr, options->clientID);
|
||||
if (options->willFlag)
|
||||
{
|
||||
writeMQTTString(&ptr, options->will.topicName);
|
||||
writeMQTTString(&ptr, options->will.message);
|
||||
}
|
||||
if (flags.bits.username)
|
||||
writeMQTTString(&ptr, options->username);
|
||||
if (flags.bits.password)
|
||||
writeMQTTString(&ptr, options->password);
|
||||
|
||||
rc = ptr - buf;
|
||||
|
||||
exit: FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into connack data - return code
|
||||
* @param sessionPresent the session present flag returned (only for MQTT 3.1.1)
|
||||
* @param connack_rc returned integer value of the connack return code
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param len the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
MQTTConnackFlags flags = {0};
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != CONNACK)
|
||||
goto exit;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
|
||||
flags.all = readChar(&curdata);
|
||||
*sessionPresent = flags.bits.sessionpresent;
|
||||
*connack_rc = readChar(&curdata);
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a 0-length packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @param packettype the message type
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = -1;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = packettype;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_disconnect(unsigned char* buf, int buflen)
|
||||
{
|
||||
return MQTTSerialize_zero(buf, buflen, DISCONNECT);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a disconnect packet into the supplied buffer, ready for writing to a socket
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer, to avoid overruns
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pingreq(unsigned char* buf, int buflen)
|
||||
{
|
||||
return MQTTSerialize_zero(buf, buflen, PINGREQ);
|
||||
}
|
||||
148
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTConnectServer.c
vendored
Normal file
148
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTConnectServer.c
vendored
Normal file
@@ -0,0 +1,148 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
#include <string.h>
|
||||
|
||||
#define min(a, b) ((a < b) ? a : b)
|
||||
|
||||
|
||||
/**
|
||||
* Validates MQTT protocol name and version combinations
|
||||
* @param protocol the MQTT protocol name as an MQTTString
|
||||
* @param version the MQTT protocol version number, as in the connect packet
|
||||
* @return correct MQTT combination? 1 is true, 0 is false
|
||||
*/
|
||||
int MQTTPacket_checkVersion(MQTTString* protocol, int version)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (version == 3 && memcmp(protocol->lenstring.data, "MQIsdp",
|
||||
min(6, protocol->lenstring.len)) == 0)
|
||||
rc = 1;
|
||||
else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT",
|
||||
min(4, protocol->lenstring.len)) == 0)
|
||||
rc = 1;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into connect data structure
|
||||
* @param data the connect data structure to be filled out
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param len the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
MQTTConnectFlags flags = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = &buf[len];
|
||||
int rc = 0;
|
||||
MQTTString Protocol;
|
||||
int version;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != CONNECT)
|
||||
goto exit;
|
||||
|
||||
curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */
|
||||
|
||||
if (!readMQTTLenString(&Protocol, &curdata, enddata) ||
|
||||
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
|
||||
goto exit;
|
||||
|
||||
version = (int)readChar(&curdata); /* Protocol version */
|
||||
/* If we don't recognize the protocol version, we don't parse the connect packet on the
|
||||
* basis that we don't know what the format will be.
|
||||
*/
|
||||
if (MQTTPacket_checkVersion(&Protocol, version))
|
||||
{
|
||||
flags.all = readChar(&curdata);
|
||||
data->cleansession = flags.bits.cleansession;
|
||||
data->keepAliveInterval = readInt(&curdata);
|
||||
if (!readMQTTLenString(&data->clientID, &curdata, enddata))
|
||||
goto exit;
|
||||
data->willFlag = flags.bits.will;
|
||||
if (flags.bits.will)
|
||||
{
|
||||
data->will.qos = flags.bits.willQoS;
|
||||
data->will.retained = flags.bits.willRetain;
|
||||
if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) ||
|
||||
!readMQTTLenString(&data->will.message, &curdata, enddata))
|
||||
goto exit;
|
||||
}
|
||||
if (flags.bits.username)
|
||||
{
|
||||
if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata))
|
||||
goto exit; /* username flag set, but no username supplied - invalid */
|
||||
if (flags.bits.password &&
|
||||
(enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata)))
|
||||
goto exit; /* password flag set, but no password supplied - invalid */
|
||||
}
|
||||
else if (flags.bits.password)
|
||||
goto exit; /* password flag set without username - invalid */
|
||||
rc = 1;
|
||||
}
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the connack packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param connack_rc the integer connack return code to be used
|
||||
* @param sessionPresent the MQTT 3.1.1 sessionPresent flag
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
MQTTConnackFlags flags = {0};
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = CONNACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
|
||||
flags.all = 0;
|
||||
flags.bits.sessionpresent = sessionPresent;
|
||||
writeChar(&ptr, flags.all);
|
||||
writeChar(&ptr, connack_rc);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
107
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTDeserializePublish.c
vendored
Normal file
107
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTDeserializePublish.c
vendored
Normal file
@@ -0,0 +1,107 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
#include <string.h>
|
||||
|
||||
#define min(a, b) ((a < b) ? 1 : 0)
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into publish data
|
||||
* @param dup returned integer - the MQTT dup flag
|
||||
* @param qos returned integer - the MQTT QoS value
|
||||
* @param retained returned integer - the MQTT retained flag
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param topicName returned MQTTString - the MQTT topic in the publish
|
||||
* @param payload returned byte buffer - the MQTT publish payload
|
||||
* @param payloadlen returned integer - the length of the MQTT payload
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success
|
||||
*/
|
||||
int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
|
||||
unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != PUBLISH)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
*qos = header.bits.qos;
|
||||
*retained = header.bits.retain;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
if (!readMQTTLenString(topicName, &curdata, enddata) ||
|
||||
enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */
|
||||
goto exit;
|
||||
|
||||
if (*qos > 0)
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*payloadlen = enddata - curdata;
|
||||
*payload = curdata;
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into an ack
|
||||
* @param packettype returned integer - the MQTT packet type
|
||||
* @param dup returned integer - the MQTT dup flag
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
*dup = header.bits.dup;
|
||||
*packettype = header.bits.type;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
262
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTFormat.c
vendored
Normal file
262
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTFormat.c
vendored
Normal file
@@ -0,0 +1,262 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
const char* MQTTPacket_names[] =
|
||||
{
|
||||
"RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL",
|
||||
"PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK",
|
||||
"PINGREQ", "PINGRESP", "DISCONNECT"
|
||||
};
|
||||
|
||||
|
||||
const char* MQTTPacket_getName(unsigned short packetid)
|
||||
{
|
||||
return MQTTPacket_names[packetid];
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data)
|
||||
{
|
||||
int strindex = 0;
|
||||
|
||||
strindex = snprintf(strbuf, strbuflen,
|
||||
"CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d",
|
||||
(int)data->MQTTVersion, data->clientID.lenstring.len, data->clientID.lenstring.data,
|
||||
(int)data->cleansession, data->keepAliveInterval);
|
||||
if (data->willFlag)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", will QoS %d, will retain %d, will topic %.*s, will message %.*s",
|
||||
data->will.qos, data->will.retained,
|
||||
data->will.topicName.lenstring.len, data->will.topicName.lenstring.data,
|
||||
data->will.message.lenstring.len, data->will.message.lenstring.data);
|
||||
if (data->username.lenstring.data && data->username.lenstring.len > 0)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", user name %.*s", data->username.lenstring.len, data->username.lenstring.data);
|
||||
if (data->password.lenstring.data && data->password.lenstring.len > 0)
|
||||
strindex += snprintf(&strbuf[strindex], strbuflen - strindex,
|
||||
", password %.*s", data->password.lenstring.len, data->password.lenstring.data);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent, connack_rc);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
|
||||
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen,
|
||||
"PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s",
|
||||
dup, qos, retained, packetid,
|
||||
(topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data,
|
||||
payloadlen, (payloadlen < 20) ? payloadlen : 20, payload);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid);
|
||||
if (dup)
|
||||
strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup);
|
||||
return strindex;
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[])
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d",
|
||||
dup, packetid, count,
|
||||
topicFilters[0].lenstring.len, topicFilters[0].lenstring.data,
|
||||
requestedQoSs[0]);
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs)
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"SUBACK packet id %d count %d granted qos %d", packetid, count, grantedQoSs[0]);
|
||||
}
|
||||
|
||||
|
||||
int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[])
|
||||
{
|
||||
return snprintf(strbuf, strbuflen,
|
||||
"UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s",
|
||||
dup, packetid, count,
|
||||
topicFilters[0].lenstring.len, topicFilters[0].lenstring.data);
|
||||
}
|
||||
|
||||
|
||||
#if defined(MQTT_CLIENT)
|
||||
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
|
||||
{
|
||||
int index = 0;
|
||||
int rem_length = 0;
|
||||
MQTTHeader header = {0};
|
||||
int strindex = 0;
|
||||
|
||||
header.byte = buf[index++];
|
||||
index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
|
||||
|
||||
switch (header.bits.type)
|
||||
{
|
||||
|
||||
case CONNACK:
|
||||
{
|
||||
unsigned char sessionPresent, connack_rc;
|
||||
if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent);
|
||||
}
|
||||
break;
|
||||
case PUBLISH:
|
||||
{
|
||||
unsigned char dup, retained, *payload;
|
||||
unsigned short packetid;
|
||||
int qos, payloadlen;
|
||||
MQTTString topicName = MQTTString_initializer;
|
||||
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
|
||||
&payload, &payloadlen, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
|
||||
topicName, payload, payloadlen);
|
||||
}
|
||||
break;
|
||||
case PUBACK:
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
case PUBCOMP:
|
||||
{
|
||||
unsigned char packettype, dup;
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
|
||||
}
|
||||
break;
|
||||
case SUBACK:
|
||||
{
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
int grantedQoSs[1];
|
||||
if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs);
|
||||
}
|
||||
break;
|
||||
case UNSUBACK:
|
||||
{
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid);
|
||||
}
|
||||
break;
|
||||
case PINGREQ:
|
||||
case PINGRESP:
|
||||
case DISCONNECT:
|
||||
strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
|
||||
break;
|
||||
}
|
||||
return strbuf;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MQTT_SERVER)
|
||||
char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen)
|
||||
{
|
||||
int index = 0;
|
||||
int rem_length = 0;
|
||||
MQTTHeader header = {0};
|
||||
int strindex = 0;
|
||||
|
||||
header.byte = buf[index++];
|
||||
index += MQTTPacket_decodeBuf(&buf[index], &rem_length);
|
||||
|
||||
switch (header.bits.type)
|
||||
{
|
||||
case CONNECT:
|
||||
{
|
||||
MQTTPacket_connectData data;
|
||||
int rc;
|
||||
if ((rc = MQTTDeserialize_connect(&data, buf, buflen)) == 1)
|
||||
strindex = MQTTStringFormat_connect(strbuf, strbuflen, &data);
|
||||
}
|
||||
break;
|
||||
case PUBLISH:
|
||||
{
|
||||
unsigned char dup, retained, *payload;
|
||||
unsigned short packetid;
|
||||
int qos, payloadlen;
|
||||
MQTTString topicName = MQTTString_initializer;
|
||||
if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName,
|
||||
&payload, &payloadlen, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid,
|
||||
topicName, payload, payloadlen);
|
||||
}
|
||||
break;
|
||||
case PUBACK:
|
||||
case PUBREC:
|
||||
case PUBREL:
|
||||
case PUBCOMP:
|
||||
{
|
||||
unsigned char packettype, dup;
|
||||
unsigned short packetid;
|
||||
if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid);
|
||||
}
|
||||
break;
|
||||
case SUBSCRIBE:
|
||||
{
|
||||
unsigned char dup;
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
MQTTString topicFilters[1];
|
||||
int requestedQoSs[1];
|
||||
if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count,
|
||||
topicFilters, requestedQoSs, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);;
|
||||
}
|
||||
break;
|
||||
case UNSUBSCRIBE:
|
||||
{
|
||||
unsigned char dup;
|
||||
unsigned short packetid;
|
||||
int maxcount = 1, count = 0;
|
||||
MQTTString topicFilters[1];
|
||||
if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1)
|
||||
strindex = MQTTStringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters);
|
||||
}
|
||||
break;
|
||||
case PINGREQ:
|
||||
case PINGRESP:
|
||||
case DISCONNECT:
|
||||
strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]);
|
||||
break;
|
||||
}
|
||||
strbuf[strbuflen] = '\0';
|
||||
return strbuf;
|
||||
}
|
||||
#endif
|
||||
37
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTFormat.h
vendored
Normal file
37
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTFormat.h
vendored
Normal file
@@ -0,0 +1,37 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#if !defined(MQTTFORMAT_H)
|
||||
#define MQTTFORMAT_H
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
const char* MQTTPacket_getName(unsigned short packetid);
|
||||
int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data);
|
||||
int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent);
|
||||
int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained,
|
||||
unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen);
|
||||
int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid);
|
||||
int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[]);
|
||||
int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs);
|
||||
int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[]);
|
||||
char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
|
||||
char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen);
|
||||
|
||||
#endif
|
||||
412
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTPacket.c
vendored
Normal file
412
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTPacket.c
vendored
Normal file
@@ -0,0 +1,412 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Sergio R. Caprile - non-blocking packet read functions for stream transport
|
||||
*******************************************************************************/
|
||||
|
||||
#include "StackTrace.h"
|
||||
#include "MQTTPacket.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Encodes the message length according to the MQTT algorithm
|
||||
* @param buf the buffer into which the encoded data is written
|
||||
* @param length the length to be encoded
|
||||
* @return the number of bytes written to buffer
|
||||
*/
|
||||
int MQTTPacket_encode(unsigned char* buf, int length)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
do
|
||||
{
|
||||
char d = length % 128;
|
||||
length /= 128;
|
||||
/* if there are more digits to encode, set the top bit of this digit */
|
||||
if (length > 0)
|
||||
d |= 0x80;
|
||||
buf[rc++] = d;
|
||||
} while (length > 0);
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Decodes the message length according to the MQTT algorithm
|
||||
* @param getcharfn pointer to function to read the next character from the data source
|
||||
* @param value the decoded length returned
|
||||
* @return the number of bytes read from the socket
|
||||
*/
|
||||
int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value)
|
||||
{
|
||||
unsigned char c;
|
||||
int multiplier = 1;
|
||||
int len = 0;
|
||||
#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4
|
||||
|
||||
FUNC_ENTRY;
|
||||
*value = 0;
|
||||
do
|
||||
{
|
||||
int rc = MQTTPACKET_READ_ERROR;
|
||||
|
||||
if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES)
|
||||
{
|
||||
rc = MQTTPACKET_READ_ERROR; /* bad data */
|
||||
goto exit;
|
||||
}
|
||||
rc = (*getcharfn)(&c, 1);
|
||||
if (rc != 1)
|
||||
goto exit;
|
||||
*value += (c & 127) * multiplier;
|
||||
multiplier *= 128;
|
||||
} while ((c & 128) != 0);
|
||||
exit:
|
||||
FUNC_EXIT_RC(len);
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
int MQTTPacket_len(int rem_len)
|
||||
{
|
||||
rem_len += 1; /* header byte */
|
||||
|
||||
/* now remaining_length field */
|
||||
if (rem_len < 128)
|
||||
rem_len += 1;
|
||||
else if (rem_len < 16384)
|
||||
rem_len += 2;
|
||||
else if (rem_len < 2097151)
|
||||
rem_len += 3;
|
||||
else
|
||||
rem_len += 4;
|
||||
return rem_len;
|
||||
}
|
||||
|
||||
|
||||
static unsigned char* bufptr;
|
||||
|
||||
int bufchar(unsigned char* c, int count)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
*c = *bufptr++;
|
||||
return count;
|
||||
}
|
||||
|
||||
|
||||
int MQTTPacket_decodeBuf(unsigned char* buf, int* value)
|
||||
{
|
||||
bufptr = buf;
|
||||
return MQTTPacket_decode(bufchar, value);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Calculates an integer from two bytes read from the input buffer
|
||||
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
|
||||
* @return the integer value calculated
|
||||
*/
|
||||
int readInt(unsigned char** pptr)
|
||||
{
|
||||
unsigned char* ptr = *pptr;
|
||||
int len = 256*(*ptr) + (*(ptr+1));
|
||||
*pptr += 2;
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reads one character from the input buffer.
|
||||
* @param pptr pointer to the input buffer - incremented by the number of bytes used & returned
|
||||
* @return the character read
|
||||
*/
|
||||
char readChar(unsigned char** pptr)
|
||||
{
|
||||
char c = **pptr;
|
||||
(*pptr)++;
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes one character to an output buffer.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param c the character to write
|
||||
*/
|
||||
void writeChar(unsigned char** pptr, char c)
|
||||
{
|
||||
**pptr = c;
|
||||
(*pptr)++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes an integer as 2 bytes to an output buffer.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param anInt the integer to write
|
||||
*/
|
||||
void writeInt(unsigned char** pptr, int anInt)
|
||||
{
|
||||
**pptr = (unsigned char)(anInt / 256);
|
||||
(*pptr)++;
|
||||
**pptr = (unsigned char)(anInt % 256);
|
||||
(*pptr)++;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Writes a "UTF" string to an output buffer. Converts C string to length-delimited.
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param string the C string to write
|
||||
*/
|
||||
void writeCString(unsigned char** pptr, const char* string)
|
||||
{
|
||||
int len = strlen(string);
|
||||
writeInt(pptr, len);
|
||||
memcpy(*pptr, string, len);
|
||||
*pptr += len;
|
||||
}
|
||||
|
||||
|
||||
int getLenStringLen(char* ptr)
|
||||
{
|
||||
int len = 256*((unsigned char)(*ptr)) + (unsigned char)(*(ptr+1));
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
void writeMQTTString(unsigned char** pptr, MQTTString mqttstring)
|
||||
{
|
||||
if (mqttstring.lenstring.len > 0)
|
||||
{
|
||||
writeInt(pptr, mqttstring.lenstring.len);
|
||||
memcpy(*pptr, mqttstring.lenstring.data, mqttstring.lenstring.len);
|
||||
*pptr += mqttstring.lenstring.len;
|
||||
}
|
||||
else if (mqttstring.cstring)
|
||||
writeCString(pptr, mqttstring.cstring);
|
||||
else
|
||||
writeInt(pptr, 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mqttstring the MQTTString structure into which the data is to be read
|
||||
* @param pptr pointer to the output buffer - incremented by the number of bytes used & returned
|
||||
* @param enddata pointer to the end of the data: do not read beyond
|
||||
* @return 1 if successful, 0 if not
|
||||
*/
|
||||
int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
/* the first two bytes are the length of the string */
|
||||
if (enddata - (*pptr) > 1) /* enough length to read the integer? */
|
||||
{
|
||||
mqttstring->lenstring.len = readInt(pptr); /* increments pptr to point past length */
|
||||
if (&(*pptr)[mqttstring->lenstring.len] <= enddata)
|
||||
{
|
||||
mqttstring->lenstring.data = (char*)*pptr;
|
||||
*pptr += mqttstring->lenstring.len;
|
||||
rc = 1;
|
||||
}
|
||||
}
|
||||
mqttstring->cstring = NULL;
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string
|
||||
* @param mqttstring the string to return the length of
|
||||
* @return the length of the string
|
||||
*/
|
||||
int MQTTstrlen(MQTTString mqttstring)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
if (mqttstring.cstring)
|
||||
rc = strlen(mqttstring.cstring);
|
||||
else
|
||||
rc = mqttstring.lenstring.len;
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Compares an MQTTString to a C string
|
||||
* @param a the MQTTString to compare
|
||||
* @param bptr the C string to compare
|
||||
* @return boolean - equal or not
|
||||
*/
|
||||
int MQTTPacket_equals(MQTTString* a, char* bptr)
|
||||
{
|
||||
int alen = 0,
|
||||
blen = 0;
|
||||
char *aptr;
|
||||
|
||||
if (a->cstring)
|
||||
{
|
||||
aptr = a->cstring;
|
||||
alen = strlen(a->cstring);
|
||||
}
|
||||
else
|
||||
{
|
||||
aptr = a->lenstring.data;
|
||||
alen = a->lenstring.len;
|
||||
}
|
||||
blen = strlen(bptr);
|
||||
|
||||
return (alen == blen) && (strncmp(aptr, bptr, alen) == 0);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Helper function to read packet data from some source into a buffer
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param getfn pointer to a function which will read any number of bytes from the needed source
|
||||
* @return integer MQTT packet type, or -1 on error
|
||||
* @note the whole message must fit into the caller's buffer
|
||||
*/
|
||||
int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int))
|
||||
{
|
||||
int rc = -1;
|
||||
MQTTHeader header = {0};
|
||||
int len = 0;
|
||||
int rem_len = 0;
|
||||
|
||||
/* 1. read the header byte. This has the packet type in it */
|
||||
if ((*getfn)(buf, 1) != 1)
|
||||
goto exit;
|
||||
|
||||
len = 1;
|
||||
/* 2. read the remaining length. This is variable in itself */
|
||||
MQTTPacket_decode(getfn, &rem_len);
|
||||
len += MQTTPacket_encode(buf + 1, rem_len); /* put the original remaining length back into the buffer */
|
||||
|
||||
/* 3. read the rest of the buffer using a callback to supply the rest of the data */
|
||||
if((rem_len + len) > buflen)
|
||||
goto exit;
|
||||
if (rem_len && ((*getfn)(buf + len, rem_len) != rem_len))
|
||||
goto exit;
|
||||
|
||||
header.byte = buf[0];
|
||||
rc = header.bits.type;
|
||||
exit:
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Decodes the message length according to the MQTT algorithm, non-blocking
|
||||
* @param trp pointer to a transport structure holding what is needed to solve getting data from it
|
||||
* @param value the decoded length returned
|
||||
* @return integer the number of bytes read from the socket, 0 for call again, or -1 on error
|
||||
*/
|
||||
static int MQTTPacket_decodenb(MQTTTransport *trp)
|
||||
{
|
||||
unsigned char c;
|
||||
int rc = MQTTPACKET_READ_ERROR;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if(trp->len == 0){ /* initialize on first call */
|
||||
trp->multiplier = 1;
|
||||
trp->rem_len = 0;
|
||||
}
|
||||
do {
|
||||
int frc;
|
||||
if (trp->len >= MAX_NO_OF_REMAINING_LENGTH_BYTES)
|
||||
goto exit;
|
||||
if ((frc=(*trp->getfn)(trp->sck, &c, 1)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0){
|
||||
rc = 0;
|
||||
goto exit;
|
||||
}
|
||||
++(trp->len);
|
||||
trp->rem_len += (c & 127) * trp->multiplier;
|
||||
trp->multiplier *= 128;
|
||||
} while ((c & 128) != 0);
|
||||
rc = trp->len;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to read packet data from some source into a buffer, non-blocking
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param trp pointer to a transport structure holding what is needed to solve getting data from it
|
||||
* @return integer MQTT packet type, 0 for call again, or -1 on error
|
||||
* @note the whole message must fit into the caller's buffer
|
||||
*/
|
||||
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp)
|
||||
{
|
||||
int rc = -1, frc;
|
||||
MQTTHeader header = {0};
|
||||
|
||||
switch(trp->state){
|
||||
default:
|
||||
trp->state = 0;
|
||||
/*FALLTHROUGH*/
|
||||
case 0:
|
||||
/* read the header byte. This has the packet type in it */
|
||||
if ((frc=(*trp->getfn)(trp->sck, buf, 1)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0)
|
||||
return 0;
|
||||
trp->len = 0;
|
||||
++trp->state;
|
||||
/*FALLTHROUGH*/
|
||||
/* read the remaining length. This is variable in itself */
|
||||
case 1:
|
||||
if((frc=MQTTPacket_decodenb(trp)) == MQTTPACKET_READ_ERROR)
|
||||
goto exit;
|
||||
if(frc == 0)
|
||||
return 0;
|
||||
trp->len = 1 + MQTTPacket_encode(buf + 1, trp->rem_len); /* put the original remaining length back into the buffer */
|
||||
if((trp->rem_len + trp->len) > buflen)
|
||||
goto exit;
|
||||
++trp->state;
|
||||
/*FALLTHROUGH*/
|
||||
case 2:
|
||||
if(trp->rem_len){
|
||||
/* read the rest of the buffer using a callback to supply the rest of the data */
|
||||
if ((frc=(*trp->getfn)(trp->sck, buf + trp->len, trp->rem_len)) == -1)
|
||||
goto exit;
|
||||
if (frc == 0)
|
||||
return 0;
|
||||
trp->rem_len -= frc;
|
||||
trp->len += frc;
|
||||
if(trp->rem_len)
|
||||
return 0;
|
||||
}
|
||||
header.byte = buf[0];
|
||||
rc = header.bits.type;
|
||||
break;
|
||||
}
|
||||
|
||||
exit:
|
||||
trp->state = 0;
|
||||
return rc;
|
||||
}
|
||||
|
||||
133
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTPacket.h
vendored
Normal file
133
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTPacket.h
vendored
Normal file
@@ -0,0 +1,133 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTPACKET_H_
|
||||
#define MQTTPACKET_H_
|
||||
|
||||
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#if defined(WIN32_DLL) || defined(WIN64_DLL)
|
||||
#define DLLImport __declspec(dllimport)
|
||||
#define DLLExport __declspec(dllexport)
|
||||
#elif defined(LINUX_SO)
|
||||
#define DLLImport extern
|
||||
#define DLLExport __attribute__ ((visibility ("default")))
|
||||
#else
|
||||
#define DLLImport
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
enum errors
|
||||
{
|
||||
MQTTPACKET_BUFFER_TOO_SHORT = -2,
|
||||
MQTTPACKET_READ_ERROR = -1,
|
||||
MQTTPACKET_READ_COMPLETE
|
||||
};
|
||||
|
||||
enum msgTypes
|
||||
{
|
||||
CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL,
|
||||
PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK,
|
||||
PINGREQ, PINGRESP, DISCONNECT
|
||||
};
|
||||
|
||||
/**
|
||||
* Bitfields for the MQTT header byte.
|
||||
*/
|
||||
typedef union
|
||||
{
|
||||
unsigned char byte; /**< the whole byte */
|
||||
#if defined(REVERSED)
|
||||
struct
|
||||
{
|
||||
unsigned int type : 4; /**< message type nibble */
|
||||
unsigned int dup : 1; /**< DUP flag bit */
|
||||
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
||||
unsigned int retain : 1; /**< retained flag bit */
|
||||
} bits;
|
||||
#else
|
||||
struct
|
||||
{
|
||||
unsigned int retain : 1; /**< retained flag bit */
|
||||
unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */
|
||||
unsigned int dup : 1; /**< DUP flag bit */
|
||||
unsigned int type : 4; /**< message type nibble */
|
||||
} bits;
|
||||
#endif
|
||||
} MQTTHeader;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
int len;
|
||||
char* data;
|
||||
} MQTTLenString;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
char* cstring;
|
||||
MQTTLenString lenstring;
|
||||
} MQTTString;
|
||||
|
||||
#define MQTTString_initializer {NULL, {0, NULL}}
|
||||
|
||||
int MQTTstrlen(MQTTString mqttstring);
|
||||
|
||||
#include "MQTTConnect.h"
|
||||
#include "MQTTPublish.h"
|
||||
#include "MQTTSubscribe.h"
|
||||
#include "MQTTUnsubscribe.h"
|
||||
#include "MQTTFormat.h"
|
||||
|
||||
DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid);
|
||||
DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen);
|
||||
|
||||
int MQTTPacket_len(int rem_len);
|
||||
DLLExport int MQTTPacket_equals(MQTTString* a, char* b);
|
||||
|
||||
DLLExport int MQTTPacket_encode(unsigned char* buf, int length);
|
||||
int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value);
|
||||
int MQTTPacket_decodeBuf(unsigned char* buf, int* value);
|
||||
|
||||
int readInt(unsigned char** pptr);
|
||||
char readChar(unsigned char** pptr);
|
||||
void writeChar(unsigned char** pptr, char c);
|
||||
void writeInt(unsigned char** pptr, int anInt);
|
||||
int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata);
|
||||
void writeCString(unsigned char** pptr, const char* string);
|
||||
void writeMQTTString(unsigned char** pptr, MQTTString mqttstring);
|
||||
|
||||
DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int));
|
||||
|
||||
typedef struct {
|
||||
int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */
|
||||
void *sck; /* pointer to whatever the system may use to identify the transport */
|
||||
int multiplier;
|
||||
int rem_len;
|
||||
int len;
|
||||
char state;
|
||||
}MQTTTransport;
|
||||
|
||||
int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp);
|
||||
|
||||
#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* MQTTPACKET_H_ */
|
||||
38
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTPublish.h
vendored
Normal file
38
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTPublish.h
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTPUBLISH_H_
|
||||
#define MQTTPUBLISH_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
|
||||
MQTTString topicName, unsigned char* payload, int payloadlen);
|
||||
|
||||
DLLExport int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName,
|
||||
unsigned char** payload, int* payloadlen, unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
DLLExport int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid);
|
||||
DLLExport int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
|
||||
#endif /* MQTTPUBLISH_H_ */
|
||||
169
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTSerializePublish.c
vendored
Normal file
169
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTSerializePublish.c
vendored
Normal file
@@ -0,0 +1,169 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT publish packet that would be produced using the supplied parameters
|
||||
* @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0)
|
||||
* @param topicName the topic name to be used in the publish
|
||||
* @param payloadlen the length of the payload to be sent
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen)
|
||||
{
|
||||
int len = 0;
|
||||
|
||||
len += 2 + MQTTstrlen(topicName) + payloadlen;
|
||||
if (qos > 0)
|
||||
len += 2; /* packetid */
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied publish data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param qos integer - the MQTT QoS value
|
||||
* @param retained integer - the MQTT retained flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param topicName MQTTString - the MQTT topic in the publish
|
||||
* @param payload byte buffer - the MQTT publish payload
|
||||
* @param payloadlen integer - the length of the MQTT payload
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid,
|
||||
MQTTString topicName, unsigned char* payload, int payloadlen)
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.bits.type = PUBLISH;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = qos;
|
||||
header.bits.retain = retained;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeMQTTString(&ptr, topicName);
|
||||
|
||||
if (qos > 0)
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
memcpy(ptr, payload, payloadlen);
|
||||
ptr += payloadlen;
|
||||
|
||||
rc = ptr - buf;
|
||||
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the ack packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param type the MQTT packet type
|
||||
* @param dup the MQTT dup flag
|
||||
* @param packetid the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 4)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.bits.type = packettype;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = (packettype == PUBREL) ? 1 : 0;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
writeInt(&ptr, packetid);
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a puback packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a pubrel packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes a pubrel packet into the supplied buffer.
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return serialized length, or error if 0
|
||||
*/
|
||||
int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid);
|
||||
}
|
||||
|
||||
|
||||
39
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTSubscribe.h
vendored
Normal file
39
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTSubscribe.h
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTSUBSCRIBE_H_
|
||||
#define MQTTSUBSCRIBE_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[], int requestedQoSs[]);
|
||||
|
||||
DLLExport int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid,
|
||||
int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs);
|
||||
|
||||
DLLExport int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int len);
|
||||
|
||||
|
||||
#endif /* MQTTSUBSCRIBE_H_ */
|
||||
137
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTSubscribeClient.c
vendored
Normal file
137
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTSubscribeClient.c
vendored
Normal file
@@ -0,0 +1,137 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters
|
||||
* @param count the number of topic filter strings in topicFilters
|
||||
* @param topicFilters the array of topic filter strings to be used in the publish
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[])
|
||||
{
|
||||
int i;
|
||||
int len = 2; /* packetid */
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied subscribe data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied bufferr
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the topicFilters and reqQos arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param requestedQoSs - array of requested QoS
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count,
|
||||
MQTTString topicFilters[], int requestedQoSs[])
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = 0;
|
||||
int i = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = SUBSCRIBE;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = 1;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
{
|
||||
writeMQTTString(&ptr, topicFilters[i]);
|
||||
writeChar(&ptr, requestedQoSs[i]);
|
||||
}
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into suback data
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the grantedQoSs array
|
||||
* @param count returned integer - number of members in the grantedQoSs array
|
||||
* @param grantedQoSs returned array of integers - the granted qualities of service
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != SUBACK)
|
||||
goto exit;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
if (enddata - curdata < 2)
|
||||
goto exit;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (*count > maxcount)
|
||||
{
|
||||
rc = -1;
|
||||
goto exit;
|
||||
}
|
||||
grantedQoSs[(*count)++] = readChar(&curdata);
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
112
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTSubscribeServer.c
vendored
Normal file
112
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTSubscribeServer.c
vendored
Normal file
@@ -0,0 +1,112 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into subscribe data
|
||||
* @param dup integer returned - the MQTT dup flag
|
||||
* @param packetid integer returned - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
|
||||
* @param count - number of members in the topicFilters and requestedQoSs arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param requestedQoSs - array of requested QoS
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
|
||||
int requestedQoSs[], unsigned char* buf, int buflen)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = -1;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != SUBSCRIBE)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
|
||||
goto exit;
|
||||
if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */
|
||||
goto exit;
|
||||
requestedQoSs[*count] = readChar(&curdata);
|
||||
(*count)++;
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied suback data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the grantedQoSs array
|
||||
* @param grantedQoSs - array of granted QoS
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = -1;
|
||||
unsigned char *ptr = buf;
|
||||
int i;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2 + count)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = SUBACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
writeChar(&ptr, grantedQoSs[i]);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
38
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTUnsubscribe.h
vendored
Normal file
38
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTUnsubscribe.h
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Xiang Rong - 442039 Add makefile to Embedded C client
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef MQTTUNSUBSCRIBE_H_
|
||||
#define MQTTUNSUBSCRIBE_H_
|
||||
|
||||
#if !defined(DLLImport)
|
||||
#define DLLImport
|
||||
#endif
|
||||
#if !defined(DLLExport)
|
||||
#define DLLExport
|
||||
#endif
|
||||
|
||||
DLLExport int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[]);
|
||||
|
||||
DLLExport int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int max_count, int* count, MQTTString topicFilters[],
|
||||
unsigned char* buf, int len);
|
||||
|
||||
DLLExport int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid);
|
||||
|
||||
DLLExport int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int len);
|
||||
|
||||
#endif /* MQTTUNSUBSCRIBE_H_ */
|
||||
106
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTUnsubscribeClient.c
vendored
Normal file
106
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTUnsubscribeClient.c
vendored
Normal file
@@ -0,0 +1,106 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
/**
|
||||
* Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters
|
||||
* @param count the number of topic filter strings in topicFilters
|
||||
* @param topicFilters the array of topic filter strings to be used in the publish
|
||||
* @return the length of buffer needed to contain the serialized version of the packet
|
||||
*/
|
||||
int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[])
|
||||
{
|
||||
int i;
|
||||
int len = 2; /* packetid */
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied unsubscribe data into the supplied buffer, ready for sending
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @param dup integer - the MQTT dup flag
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @param count - number of members in the topicFilters array
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid,
|
||||
int count, MQTTString topicFilters[])
|
||||
{
|
||||
unsigned char *ptr = buf;
|
||||
MQTTHeader header = {0};
|
||||
int rem_len = 0;
|
||||
int rc = -1;
|
||||
int i = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
header.byte = 0;
|
||||
header.bits.type = UNSUBSCRIBE;
|
||||
header.bits.dup = dup;
|
||||
header.bits.qos = 1;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */;
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
for (i = 0; i < count; ++i)
|
||||
writeMQTTString(&ptr, topicFilters[i]);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into unsuback data
|
||||
* @param packetid returned integer - the MQTT packet identifier
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return error code. 1 is success, 0 is failure
|
||||
*/
|
||||
int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen)
|
||||
{
|
||||
unsigned char type = 0;
|
||||
unsigned char dup = 0;
|
||||
int rc = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen);
|
||||
if (type == UNSUBACK)
|
||||
rc = 1;
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
102
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTUnsubscribeServer.c
vendored
Normal file
102
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/MQTTUnsubscribeServer.c
vendored
Normal file
@@ -0,0 +1,102 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
*******************************************************************************/
|
||||
|
||||
#include "MQTTPacket.h"
|
||||
#include "StackTrace.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
|
||||
/**
|
||||
* Deserializes the supplied (wire) buffer into unsubscribe data
|
||||
* @param dup integer returned - the MQTT dup flag
|
||||
* @param packetid integer returned - the MQTT packet identifier
|
||||
* @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays
|
||||
* @param count - number of members in the topicFilters and requestedQoSs arrays
|
||||
* @param topicFilters - array of topic filter names
|
||||
* @param buf the raw buffer data, of the correct length determined by the remaining length field
|
||||
* @param buflen the length in bytes of the data in the supplied buffer
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[],
|
||||
unsigned char* buf, int len)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
unsigned char* curdata = buf;
|
||||
unsigned char* enddata = NULL;
|
||||
int rc = 0;
|
||||
int mylen = 0;
|
||||
|
||||
FUNC_ENTRY;
|
||||
header.byte = readChar(&curdata);
|
||||
if (header.bits.type != UNSUBSCRIBE)
|
||||
goto exit;
|
||||
*dup = header.bits.dup;
|
||||
|
||||
curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */
|
||||
enddata = curdata + mylen;
|
||||
|
||||
*packetid = readInt(&curdata);
|
||||
|
||||
*count = 0;
|
||||
while (curdata < enddata)
|
||||
{
|
||||
if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata))
|
||||
goto exit;
|
||||
(*count)++;
|
||||
}
|
||||
|
||||
rc = 1;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Serializes the supplied unsuback data into the supplied buffer, ready for sending
|
||||
* @param buf the buffer into which the packet will be serialized
|
||||
* @param buflen the length in bytes of the supplied buffer
|
||||
* @param packetid integer - the MQTT packet identifier
|
||||
* @return the length of the serialized data. <= 0 indicates error
|
||||
*/
|
||||
int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid)
|
||||
{
|
||||
MQTTHeader header = {0};
|
||||
int rc = 0;
|
||||
unsigned char *ptr = buf;
|
||||
|
||||
FUNC_ENTRY;
|
||||
if (buflen < 2)
|
||||
{
|
||||
rc = MQTTPACKET_BUFFER_TOO_SHORT;
|
||||
goto exit;
|
||||
}
|
||||
header.byte = 0;
|
||||
header.bits.type = UNSUBACK;
|
||||
writeChar(&ptr, header.byte); /* write header */
|
||||
|
||||
ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */
|
||||
|
||||
writeInt(&ptr, packetid);
|
||||
|
||||
rc = ptr - buf;
|
||||
exit:
|
||||
FUNC_EXIT_RC(rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
|
||||
78
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/StackTrace.h
vendored
Normal file
78
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/MQTTPacket/src/StackTrace.h
vendored
Normal file
@@ -0,0 +1,78 @@
|
||||
/*******************************************************************************
|
||||
* Copyright (c) 2014 IBM Corp.
|
||||
*
|
||||
* All rights reserved. This program and the accompanying materials
|
||||
* are made available under the terms of the Eclipse Public License v1.0
|
||||
* and Eclipse Distribution License v1.0 which accompany this distribution.
|
||||
*
|
||||
* The Eclipse Public License is available at
|
||||
* http://www.eclipse.org/legal/epl-v10.html
|
||||
* and the Eclipse Distribution License is available at
|
||||
* http://www.eclipse.org/org/documents/edl-v10.php.
|
||||
*
|
||||
* Contributors:
|
||||
* Ian Craggs - initial API and implementation and/or initial documentation
|
||||
* Ian Craggs - fix for bug #434081
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef STACKTRACE_H_
|
||||
#define STACKTRACE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#define NOSTACKTRACE 1
|
||||
|
||||
#if defined(NOSTACKTRACE)
|
||||
#define FUNC_ENTRY
|
||||
#define FUNC_ENTRY_NOLOG
|
||||
#define FUNC_ENTRY_MED
|
||||
#define FUNC_ENTRY_MAX
|
||||
#define FUNC_EXIT
|
||||
#define FUNC_EXIT_NOLOG
|
||||
#define FUNC_EXIT_MED
|
||||
#define FUNC_EXIT_MAX
|
||||
#define FUNC_EXIT_RC(x)
|
||||
#define FUNC_EXIT_MED_RC(x)
|
||||
#define FUNC_EXIT_MAX_RC(x)
|
||||
|
||||
#else
|
||||
|
||||
#if defined(WIN32)
|
||||
#define inline __inline
|
||||
#define FUNC_ENTRY StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MINIMUM)
|
||||
#define FUNC_ENTRY_NOLOG StackTrace_entry(__FUNCTION__, __LINE__, -1)
|
||||
#define FUNC_ENTRY_MED StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MEDIUM)
|
||||
#define FUNC_ENTRY_MAX StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_NOLOG StackTrace_exit(__FUNCTION__, __LINE__, -1)
|
||||
#define FUNC_EXIT_MED StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MAXIMUM)
|
||||
#else
|
||||
#define FUNC_ENTRY StackTrace_entry(__func__, __LINE__, TRACE_MINIMUM)
|
||||
#define FUNC_ENTRY_NOLOG StackTrace_entry(__func__, __LINE__, -1)
|
||||
#define FUNC_ENTRY_MED StackTrace_entry(__func__, __LINE__, TRACE_MEDIUM)
|
||||
#define FUNC_ENTRY_MAX StackTrace_entry(__func__, __LINE__, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT StackTrace_exit(__func__, __LINE__, NULL, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_NOLOG StackTrace_exit(__func__, __LINE__, NULL, -1)
|
||||
#define FUNC_EXIT_MED StackTrace_exit(__func__, __LINE__, NULL, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX StackTrace_exit(__func__, __LINE__, NULL, TRACE_MAXIMUM)
|
||||
#define FUNC_EXIT_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MINIMUM)
|
||||
#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MEDIUM)
|
||||
#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MAXIMUM)
|
||||
|
||||
void StackTrace_entry(const char* name, int line, int trace);
|
||||
void StackTrace_exit(const char* name, int line, void* return_value, int trace);
|
||||
|
||||
void StackTrace_printStack(FILE* dest);
|
||||
char* StackTrace_get(unsigned long);
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* STACKTRACE_H_ */
|
||||
23
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/xmake.lua
vendored
Normal file
23
sdk/合宙/air780e/csdk/luatos-soc-2022/thirdparty/mqtt/xmake.lua
vendored
Normal file
@@ -0,0 +1,23 @@
|
||||
|
||||
target("mqtt")
|
||||
local LIB_DIR = "$(buildir)/mqtt/"
|
||||
set_kind("static")
|
||||
set_targetdir(LIB_DIR)
|
||||
|
||||
add_defines("FEATURE_MQTT_TLS_ENABLE",{public = true})
|
||||
|
||||
--加入代码和头文件
|
||||
add_includedirs("./",
|
||||
SDK_TOP .. "thirdparty/mbedtls/include/mbedtls",
|
||||
SDK_TOP .. "thirdparty/cjson",
|
||||
"./MQTTPacket/src",
|
||||
"./MQTTClient-C/src/FreeRTOS",
|
||||
{public = true})
|
||||
|
||||
add_files("./MQTTPacket/src/*.c",
|
||||
"./MQTTClient-C/src/FreeRTOS/*.c",
|
||||
{public = true})
|
||||
|
||||
--自动链接
|
||||
LIB_USER = LIB_USER .. SDK_TOP .. LIB_DIR .. "libmqtt.a "
|
||||
target_end()
|
||||
Reference in New Issue
Block a user