the push is modified as follows:
1. delete some junk files. 2. mqttclient added comments and updated to v1.0.2. 3. update README.md. 4. update the author's own server certificate. 5. minor changes to salof.
This commit is contained in:
@@ -1,48 +0,0 @@
|
||||
// File: STM32F405_415_407_417_427_437_429_439.dbgconf
|
||||
// Version: 1.0.0
|
||||
// Note: refer to STM32F405/415 STM32F407/417 STM32F427/437 STM32F429/439 reference manual (RM0090)
|
||||
// refer to STM32F40x STM32F41x datasheets
|
||||
// refer to STM32F42x STM32F43x datasheets
|
||||
|
||||
// <<< Use Configuration Wizard in Context Menu >>>
|
||||
|
||||
// <h> Debug MCU configuration register (DBGMCU_CR)
|
||||
// <o.2> DBG_STANDBY <i> Debug Standby Mode
|
||||
// <o.1> DBG_STOP <i> Debug Stop Mode
|
||||
// <o.0> DBG_SLEEP <i> Debug Sleep Mode
|
||||
// </h>
|
||||
DbgMCU_CR = 0x00000007;
|
||||
|
||||
// <h> Debug MCU APB1 freeze register (DBGMCU_APB1_FZ)
|
||||
// <i> Reserved bits must be kept at reset value
|
||||
// <o.26> DBG_CAN2_STOP <i> CAN2 stopped when core is halted
|
||||
// <o.25> DBG_CAN1_STOP <i> CAN2 stopped when core is halted
|
||||
// <o.23> DBG_I2C3_SMBUS_TIMEOUT <i> I2C3 SMBUS timeout mode stopped when core is halted
|
||||
// <o.22> DBG_I2C2_SMBUS_TIMEOUT <i> I2C2 SMBUS timeout mode stopped when core is halted
|
||||
// <o.21> DBG_I2C1_SMBUS_TIMEOUT <i> I2C1 SMBUS timeout mode stopped when core is halted
|
||||
// <o.12> DBG_IWDG_STOP <i> Independent watchdog stopped when core is halted
|
||||
// <o.11> DBG_WWDG_STOP <i> Window watchdog stopped when core is halted
|
||||
// <o.10> DBG_RTC_STOP <i> RTC stopped when core is halted
|
||||
// <o.8> DBG_TIM14_STOP <i> TIM14 counter stopped when core is halted
|
||||
// <o.7> DBG_TIM13_STOP <i> TIM13 counter stopped when core is halted
|
||||
// <o.6> DBG_TIM12_STOP <i> TIM12 counter stopped when core is halted
|
||||
// <o.5> DBG_TIM7_STOP <i> TIM7 counter stopped when core is halted
|
||||
// <o.4> DBG_TIM6_STOP <i> TIM6 counter stopped when core is halted
|
||||
// <o.3> DBG_TIM5_STOP <i> TIM5 counter stopped when core is halted
|
||||
// <o.2> DBG_TIM4_STOP <i> TIM4 counter stopped when core is halted
|
||||
// <o.1> DBG_TIM3_STOP <i> TIM3 counter stopped when core is halted
|
||||
// <o.0> DBG_TIM2_STOP <i> TIM2 counter stopped when core is halted
|
||||
// </h>
|
||||
DbgMCU_APB1_Fz = 0x00000000;
|
||||
|
||||
// <h> Debug MCU APB2 freeze register (DBGMCU_APB2_FZ)
|
||||
// <i> Reserved bits must be kept at reset value
|
||||
// <o.18> DBG_TIM11_STOP <i> TIM11 counter stopped when core is halted
|
||||
// <o.17> DBG_TIM10_STOP <i> TIM10 counter stopped when core is halted
|
||||
// <o.16> DBG_TIM9_STOP <i> TIM9 counter stopped when core is halted
|
||||
// <o.1> DBG_TIM8_STOP <i> TIM8 counter stopped when core is halted
|
||||
// <o.0> DBG_TIM1_STOP <i> TIM1 counter stopped when core is halted
|
||||
// </h>
|
||||
DbgMCU_APB2_Fz = 0x00000000;
|
||||
|
||||
// <<< end of configuration section >>>
|
@@ -1,16 +0,0 @@
|
||||
; *************************************************************
|
||||
; *** Scatter-Loading Description File generated by uVision ***
|
||||
; *************************************************************
|
||||
|
||||
LR_IROM1 0x08000000 0x00100000 { ; load region size_region
|
||||
ER_IROM1 0x08000000 0x00100000 { ; load address = execution address
|
||||
*.o (RESET, +First)
|
||||
*(InRoot$$Sections)
|
||||
.ANY (+RO)
|
||||
.ANY (+XO)
|
||||
}
|
||||
RW_IRAM1 0x20000000 0x00030000 { ; RW data
|
||||
.ANY (+RW +ZI)
|
||||
}
|
||||
}
|
||||
|
@@ -1,20 +0,0 @@
|
||||
|
||||
/*
|
||||
* Auto generated Run-Time-Environment Component Configuration File
|
||||
* *** Do not modify ! ***
|
||||
*
|
||||
* Project: 'TencentOS_tiny'
|
||||
* Target: 'TencentOS_tiny'
|
||||
*/
|
||||
|
||||
#ifndef RTE_COMPONENTS_H
|
||||
#define RTE_COMPONENTS_H
|
||||
|
||||
|
||||
/*
|
||||
* Define the Device Header File:
|
||||
*/
|
||||
#define CMSIS_device_header "stm32f4xx.h"
|
||||
|
||||
|
||||
#endif /* RTE_COMPONENTS_H */
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@@ -16,7 +16,7 @@
|
||||
<TargetCommonOption>
|
||||
<Device>STM32F429IGTx</Device>
|
||||
<Vendor>STMicroelectronics</Vendor>
|
||||
<PackID>Keil.STM32F4xx_DFP.2.13.0</PackID>
|
||||
<PackID>Keil.STM32F4xx_DFP.2.9.0</PackID>
|
||||
<PackURL>http://www.keil.com/pack</PackURL>
|
||||
<Cpu>IRAM(0x20000000-0x2002FFFF) IRAM2(0x10000000-0x1000FFFF) IROM(0x8000000-0x80FFFFF) CLOCK(25000000) FPU2 CPUTYPE("Cortex-M4")</Cpu>
|
||||
<FlashUtilSpec></FlashUtilSpec>
|
||||
|
@@ -1,48 +0,0 @@
|
||||
// File: STM32F405_415_407_417_427_437_429_439.dbgconf
|
||||
// Version: 1.0.0
|
||||
// Note: refer to STM32F405/415 STM32F407/417 STM32F427/437 STM32F429/439 reference manual (RM0090)
|
||||
// refer to STM32F40x STM32F41x datasheets
|
||||
// refer to STM32F42x STM32F43x datasheets
|
||||
|
||||
// <<< Use Configuration Wizard in Context Menu >>>
|
||||
|
||||
// <h> Debug MCU configuration register (DBGMCU_CR)
|
||||
// <o.2> DBG_STANDBY <i> Debug Standby Mode
|
||||
// <o.1> DBG_STOP <i> Debug Stop Mode
|
||||
// <o.0> DBG_SLEEP <i> Debug Sleep Mode
|
||||
// </h>
|
||||
DbgMCU_CR = 0x00000007;
|
||||
|
||||
// <h> Debug MCU APB1 freeze register (DBGMCU_APB1_FZ)
|
||||
// <i> Reserved bits must be kept at reset value
|
||||
// <o.26> DBG_CAN2_STOP <i> CAN2 stopped when core is halted
|
||||
// <o.25> DBG_CAN1_STOP <i> CAN2 stopped when core is halted
|
||||
// <o.23> DBG_I2C3_SMBUS_TIMEOUT <i> I2C3 SMBUS timeout mode stopped when core is halted
|
||||
// <o.22> DBG_I2C2_SMBUS_TIMEOUT <i> I2C2 SMBUS timeout mode stopped when core is halted
|
||||
// <o.21> DBG_I2C1_SMBUS_TIMEOUT <i> I2C1 SMBUS timeout mode stopped when core is halted
|
||||
// <o.12> DBG_IWDG_STOP <i> Independent watchdog stopped when core is halted
|
||||
// <o.11> DBG_WWDG_STOP <i> Window watchdog stopped when core is halted
|
||||
// <o.10> DBG_RTC_STOP <i> RTC stopped when core is halted
|
||||
// <o.8> DBG_TIM14_STOP <i> TIM14 counter stopped when core is halted
|
||||
// <o.7> DBG_TIM13_STOP <i> TIM13 counter stopped when core is halted
|
||||
// <o.6> DBG_TIM12_STOP <i> TIM12 counter stopped when core is halted
|
||||
// <o.5> DBG_TIM7_STOP <i> TIM7 counter stopped when core is halted
|
||||
// <o.4> DBG_TIM6_STOP <i> TIM6 counter stopped when core is halted
|
||||
// <o.3> DBG_TIM5_STOP <i> TIM5 counter stopped when core is halted
|
||||
// <o.2> DBG_TIM4_STOP <i> TIM4 counter stopped when core is halted
|
||||
// <o.1> DBG_TIM3_STOP <i> TIM3 counter stopped when core is halted
|
||||
// <o.0> DBG_TIM2_STOP <i> TIM2 counter stopped when core is halted
|
||||
// </h>
|
||||
DbgMCU_APB1_Fz = 0x00000000;
|
||||
|
||||
// <h> Debug MCU APB2 freeze register (DBGMCU_APB2_FZ)
|
||||
// <i> Reserved bits must be kept at reset value
|
||||
// <o.18> DBG_TIM11_STOP <i> TIM11 counter stopped when core is halted
|
||||
// <o.17> DBG_TIM10_STOP <i> TIM10 counter stopped when core is halted
|
||||
// <o.16> DBG_TIM9_STOP <i> TIM9 counter stopped when core is halted
|
||||
// <o.1> DBG_TIM8_STOP <i> TIM8 counter stopped when core is halted
|
||||
// <o.0> DBG_TIM1_STOP <i> TIM1 counter stopped when core is halted
|
||||
// </h>
|
||||
DbgMCU_APB2_Fz = 0x00000000;
|
||||
|
||||
// <<< end of configuration section >>>
|
@@ -1,16 +0,0 @@
|
||||
; *************************************************************
|
||||
; *** Scatter-Loading Description File generated by uVision ***
|
||||
; *************************************************************
|
||||
|
||||
LR_IROM1 0x08000000 0x00100000 { ; load region size_region
|
||||
ER_IROM1 0x08000000 0x00100000 { ; load address = execution address
|
||||
*.o (RESET, +First)
|
||||
*(InRoot$$Sections)
|
||||
.ANY (+RO)
|
||||
.ANY (+XO)
|
||||
}
|
||||
RW_IRAM1 0x20000000 0x00030000 { ; RW data
|
||||
.ANY (+RW +ZI)
|
||||
}
|
||||
}
|
||||
|
@@ -1,20 +0,0 @@
|
||||
|
||||
/*
|
||||
* Auto generated Run-Time-Environment Component Configuration File
|
||||
* *** Do not modify ! ***
|
||||
*
|
||||
* Project: 'TencentOS_tiny'
|
||||
* Target: 'TencentOS_tiny'
|
||||
*/
|
||||
|
||||
#ifndef RTE_COMPONENTS_H
|
||||
#define RTE_COMPONENTS_H
|
||||
|
||||
|
||||
/*
|
||||
* Define the Device Header File:
|
||||
*/
|
||||
#define CMSIS_device_header "stm32f4xx.h"
|
||||
|
||||
|
||||
#endif /* RTE_COMPONENTS_H */
|
@@ -2,43 +2,46 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-15 00:42:16
|
||||
* @LastEditTime : 2020-01-16 00:39:23
|
||||
* @LastEditTime: 2020-02-25 09:25:35
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _MQTT_CONFIG_H_
|
||||
#define _MQTT_CONFIG_H_
|
||||
|
||||
#define MQTT_NO 0
|
||||
#define MQTT_YES 1
|
||||
#define LOG_IS_SALOF
|
||||
|
||||
#define LOG_LEVEL DEBUG_LEVEL //WARN_LEVEL DEBUG_LEVEL
|
||||
|
||||
#ifdef LOG_IS_SALOF
|
||||
#define USE_LOG (1U)
|
||||
#define USE_SALOF (1U)
|
||||
#define SALOF_OS USE_TENCENTOS
|
||||
#define USE_IDLE_HOOK (0U)
|
||||
#define LOG_COLOR (0U)
|
||||
#define LOG_TS (0U)
|
||||
#define LOG_TAR (0U)
|
||||
#define SALOF_BUFF_SIZE (512U)
|
||||
#define SALOF_FIFO_SIZE (1024U)
|
||||
#define SALOF_TASK_STACK_SIZE (1024U)
|
||||
#define SALOF_TASK_TICK (50U)
|
||||
#endif
|
||||
|
||||
|
||||
#define MQTT_MAX_PACKET_ID (0xFFFF - 1)
|
||||
#define MQTT_TOPIC_LEN_MAX 128
|
||||
#define MQTT_TOPIC_LEN_MAX 64
|
||||
#define MQTT_ACK_HANDLER_NUM_MAX 64
|
||||
#define MQTT_DEFAULT_BUF_SIZE 1024
|
||||
#define MQTT_DEFAULT_CMD_TIMEOUT 4000
|
||||
#define MQTT_MAX_CMD_TIMEOUT 20000
|
||||
#define MQTT_MIN_CMD_TIMEOUT 1000
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 20 // unit: second
|
||||
#define MQTT_VERSION 4 // 4 is mqtt 3.1.1
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 100 // unit: second
|
||||
#define MQTT_VERSION 4 // 4 is mqtt 3.1.1
|
||||
#define MQTT_RECONNECT_DEFAULT_DURATION 1000
|
||||
#define MQTT_THREAD_STACK_SIZE 2048
|
||||
#define MQTT_THREAD_PRIO 5
|
||||
#define MQTT_THREAD_TICK 50
|
||||
|
||||
|
||||
#define MQTT_NETWORK_TYPE_TLS MQTT_NO
|
||||
|
||||
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
|
||||
#define MQTT_TLS_HANDSHAKE_TIMEOUT (5 * 1000)
|
||||
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/net_sockets.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/debug.h"
|
||||
#endif /* MQTT_NETWORK_TYPE_TLS */
|
||||
//#define MQTT_NETWORK_TYPE_TLS
|
||||
|
||||
#endif /* _MQTT_CONFIG_H_ */
|
||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
@@ -16,7 +16,7 @@
|
||||
<TargetCommonOption>
|
||||
<Device>STM32F429IGTx</Device>
|
||||
<Vendor>STMicroelectronics</Vendor>
|
||||
<PackID>Keil.STM32F4xx_DFP.2.13.0</PackID>
|
||||
<PackID>Keil.STM32F4xx_DFP.2.9.0</PackID>
|
||||
<PackURL>http://www.keil.com/pack</PackURL>
|
||||
<Cpu>IRAM(0x20000000-0x2002FFFF) IRAM2(0x10000000-0x1000FFFF) IROM(0x8000000-0x80FFFFF) CLOCK(25000000) FPU2 CPUTYPE("Cortex-M4")</Cpu>
|
||||
<FlashUtilSpec></FlashUtilSpec>
|
||||
|
@@ -2,14 +2,30 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-15 00:42:16
|
||||
* @LastEditTime : 2020-01-16 00:39:23
|
||||
* @LastEditTime: 2020-02-25 09:25:35
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _MQTT_CONFIG_H_
|
||||
#define _MQTT_CONFIG_H_
|
||||
|
||||
#define MQTT_NO 0
|
||||
#define MQTT_YES 1
|
||||
#define LOG_IS_SALOF
|
||||
|
||||
#define LOG_LEVEL DEBUG_LEVEL //WARN_LEVEL DEBUG_LEVEL
|
||||
|
||||
#ifdef LOG_IS_SALOF
|
||||
#define USE_LOG (1U)
|
||||
#define USE_SALOF (1U)
|
||||
#define SALOF_OS USE_TENCENTOS
|
||||
#define USE_IDLE_HOOK (0U)
|
||||
#define LOG_COLOR (0U)
|
||||
#define LOG_TS (0U)
|
||||
#define LOG_TAR (0U)
|
||||
#define SALOF_BUFF_SIZE (512U)
|
||||
#define SALOF_FIFO_SIZE (1024U)
|
||||
#define SALOF_TASK_STACK_SIZE (1024U)
|
||||
#define SALOF_TASK_TICK (50U)
|
||||
#endif
|
||||
|
||||
|
||||
#define MQTT_MAX_PACKET_ID (0xFFFF - 1)
|
||||
#define MQTT_TOPIC_LEN_MAX 64
|
||||
@@ -18,27 +34,14 @@
|
||||
#define MQTT_DEFAULT_CMD_TIMEOUT 4000
|
||||
#define MQTT_MAX_CMD_TIMEOUT 20000
|
||||
#define MQTT_MIN_CMD_TIMEOUT 1000
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 20 // unit: second
|
||||
#define MQTT_VERSION 4 // 4 is mqtt 3.1.1
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 100 // unit: second
|
||||
#define MQTT_VERSION 4 // 4 is mqtt 3.1.1
|
||||
#define MQTT_RECONNECT_DEFAULT_DURATION 1000
|
||||
#define MQTT_THREAD_STACK_SIZE 2048
|
||||
#define MQTT_THREAD_PRIO 5
|
||||
#define MQTT_THREAD_TICK 50
|
||||
|
||||
|
||||
#define MQTT_NETWORK_TYPE_TLS MQTT_YES
|
||||
|
||||
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
|
||||
#define MQTT_TLS_HANDSHAKE_TIMEOUT (5 * 1000)
|
||||
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/net_sockets.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/debug.h"
|
||||
#endif /* MQTT_NETWORK_TYPE_TLS */
|
||||
#define MQTT_NETWORK_TYPE_TLS
|
||||
|
||||
#endif /* _MQTT_CONFIG_H_ */
|
||||
|
@@ -17,7 +17,7 @@
|
||||
|
||||
#define TOS_CFG_MMHEAP_EN 1u
|
||||
|
||||
#define TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE 0x12000
|
||||
#define TOS_CFG_MMHEAP_DEFAULT_POOL_SIZE 0x13000
|
||||
|
||||
#define TOS_CFG_TASK_STACK_DRAUGHT_DEPTH_DETACT_EN 1u
|
||||
|
||||
|
@@ -1,674 +1,201 @@
|
||||
GNU GENERAL PUBLIC LICENSE
|
||||
Version 3, 29 June 2007
|
||||
|
||||
Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
|
||||
Everyone is permitted to copy and distribute verbatim copies
|
||||
of this license document, but changing it is not allowed.
|
||||
|
||||
Preamble
|
||||
|
||||
The GNU General Public License is a free, copyleft license for
|
||||
software and other kinds of works.
|
||||
|
||||
The licenses for most software and other practical works are designed
|
||||
to take away your freedom to share and change the works. By contrast,
|
||||
the GNU General Public License is intended to guarantee your freedom to
|
||||
share and change all versions of a program--to make sure it remains free
|
||||
software for all its users. We, the Free Software Foundation, use the
|
||||
GNU General Public License for most of our software; it applies also to
|
||||
any other work released this way by its authors. You can apply it to
|
||||
your programs, too.
|
||||
|
||||
When we speak of free software, we are referring to freedom, not
|
||||
price. Our General Public Licenses are designed to make sure that you
|
||||
have the freedom to distribute copies of free software (and charge for
|
||||
them if you wish), that you receive source code or can get it if you
|
||||
want it, that you can change the software or use pieces of it in new
|
||||
free programs, and that you know you can do these things.
|
||||
|
||||
To protect your rights, we need to prevent others from denying you
|
||||
these rights or asking you to surrender the rights. Therefore, you have
|
||||
certain responsibilities if you distribute copies of the software, or if
|
||||
you modify it: responsibilities to respect the freedom of others.
|
||||
|
||||
For example, if you distribute copies of such a program, whether
|
||||
gratis or for a fee, you must pass on to the recipients the same
|
||||
freedoms that you received. You must make sure that they, too, receive
|
||||
or can get the source code. And you must show them these terms so they
|
||||
know their rights.
|
||||
|
||||
Developers that use the GNU GPL protect your rights with two steps:
|
||||
(1) assert copyright on the software, and (2) offer you this License
|
||||
giving you legal permission to copy, distribute and/or modify it.
|
||||
|
||||
For the developers' and authors' protection, the GPL clearly explains
|
||||
that there is no warranty for this free software. For both users' and
|
||||
authors' sake, the GPL requires that modified versions be marked as
|
||||
changed, so that their problems will not be attributed erroneously to
|
||||
authors of previous versions.
|
||||
|
||||
Some devices are designed to deny users access to install or run
|
||||
modified versions of the software inside them, although the manufacturer
|
||||
can do so. This is fundamentally incompatible with the aim of
|
||||
protecting users' freedom to change the software. The systematic
|
||||
pattern of such abuse occurs in the area of products for individuals to
|
||||
use, which is precisely where it is most unacceptable. Therefore, we
|
||||
have designed this version of the GPL to prohibit the practice for those
|
||||
products. If such problems arise substantially in other domains, we
|
||||
stand ready to extend this provision to those domains in future versions
|
||||
of the GPL, as needed to protect the freedom of users.
|
||||
|
||||
Finally, every program is threatened constantly by software patents.
|
||||
States should not allow patents to restrict development and use of
|
||||
software on general-purpose computers, but in those that do, we wish to
|
||||
avoid the special danger that patents applied to a free program could
|
||||
make it effectively proprietary. To prevent this, the GPL assures that
|
||||
patents cannot be used to render the program non-free.
|
||||
|
||||
The precise terms and conditions for copying, distribution and
|
||||
modification follow.
|
||||
|
||||
TERMS AND CONDITIONS
|
||||
|
||||
0. Definitions.
|
||||
|
||||
"This License" refers to version 3 of the GNU General Public License.
|
||||
|
||||
"Copyright" also means copyright-like laws that apply to other kinds of
|
||||
works, such as semiconductor masks.
|
||||
|
||||
"The Program" refers to any copyrightable work licensed under this
|
||||
License. Each licensee is addressed as "you". "Licensees" and
|
||||
"recipients" may be individuals or organizations.
|
||||
|
||||
To "modify" a work means to copy from or adapt all or part of the work
|
||||
in a fashion requiring copyright permission, other than the making of an
|
||||
exact copy. The resulting work is called a "modified version" of the
|
||||
earlier work or a work "based on" the earlier work.
|
||||
|
||||
A "covered work" means either the unmodified Program or a work based
|
||||
on the Program.
|
||||
|
||||
To "propagate" a work means to do anything with it that, without
|
||||
permission, would make you directly or secondarily liable for
|
||||
infringement under applicable copyright law, except executing it on a
|
||||
computer or modifying a private copy. Propagation includes copying,
|
||||
distribution (with or without modification), making available to the
|
||||
public, and in some countries other activities as well.
|
||||
|
||||
To "convey" a work means any kind of propagation that enables other
|
||||
parties to make or receive copies. Mere interaction with a user through
|
||||
a computer network, with no transfer of a copy, is not conveying.
|
||||
|
||||
An interactive user interface displays "Appropriate Legal Notices"
|
||||
to the extent that it includes a convenient and prominently visible
|
||||
feature that (1) displays an appropriate copyright notice, and (2)
|
||||
tells the user that there is no warranty for the work (except to the
|
||||
extent that warranties are provided), that licensees may convey the
|
||||
work under this License, and how to view a copy of this License. If
|
||||
the interface presents a list of user commands or options, such as a
|
||||
menu, a prominent item in the list meets this criterion.
|
||||
|
||||
1. Source Code.
|
||||
|
||||
The "source code" for a work means the preferred form of the work
|
||||
for making modifications to it. "Object code" means any non-source
|
||||
form of a work.
|
||||
|
||||
A "Standard Interface" means an interface that either is an official
|
||||
standard defined by a recognized standards body, or, in the case of
|
||||
interfaces specified for a particular programming language, one that
|
||||
is widely used among developers working in that language.
|
||||
|
||||
The "System Libraries" of an executable work include anything, other
|
||||
than the work as a whole, that (a) is included in the normal form of
|
||||
packaging a Major Component, but which is not part of that Major
|
||||
Component, and (b) serves only to enable use of the work with that
|
||||
Major Component, or to implement a Standard Interface for which an
|
||||
implementation is available to the public in source code form. A
|
||||
"Major Component", in this context, means a major essential component
|
||||
(kernel, window system, and so on) of the specific operating system
|
||||
(if any) on which the executable work runs, or a compiler used to
|
||||
produce the work, or an object code interpreter used to run it.
|
||||
|
||||
The "Corresponding Source" for a work in object code form means all
|
||||
the source code needed to generate, install, and (for an executable
|
||||
work) run the object code and to modify the work, including scripts to
|
||||
control those activities. However, it does not include the work's
|
||||
System Libraries, or general-purpose tools or generally available free
|
||||
programs which are used unmodified in performing those activities but
|
||||
which are not part of the work. For example, Corresponding Source
|
||||
includes interface definition files associated with source files for
|
||||
the work, and the source code for shared libraries and dynamically
|
||||
linked subprograms that the work is specifically designed to require,
|
||||
such as by intimate data communication or control flow between those
|
||||
subprograms and other parts of the work.
|
||||
|
||||
The Corresponding Source need not include anything that users
|
||||
can regenerate automatically from other parts of the Corresponding
|
||||
Source.
|
||||
|
||||
The Corresponding Source for a work in source code form is that
|
||||
same work.
|
||||
|
||||
2. Basic Permissions.
|
||||
|
||||
All rights granted under this License are granted for the term of
|
||||
copyright on the Program, and are irrevocable provided the stated
|
||||
conditions are met. This License explicitly affirms your unlimited
|
||||
permission to run the unmodified Program. The output from running a
|
||||
covered work is covered by this License only if the output, given its
|
||||
content, constitutes a covered work. This License acknowledges your
|
||||
rights of fair use or other equivalent, as provided by copyright law.
|
||||
|
||||
You may make, run and propagate covered works that you do not
|
||||
convey, without conditions so long as your license otherwise remains
|
||||
in force. You may convey covered works to others for the sole purpose
|
||||
of having them make modifications exclusively for you, or provide you
|
||||
with facilities for running those works, provided that you comply with
|
||||
the terms of this License in conveying all material for which you do
|
||||
not control copyright. Those thus making or running the covered works
|
||||
for you must do so exclusively on your behalf, under your direction
|
||||
and control, on terms that prohibit them from making any copies of
|
||||
your copyrighted material outside their relationship with you.
|
||||
|
||||
Conveying under any other circumstances is permitted solely under
|
||||
the conditions stated below. Sublicensing is not allowed; section 10
|
||||
makes it unnecessary.
|
||||
|
||||
3. Protecting Users' Legal Rights From Anti-Circumvention Law.
|
||||
|
||||
No covered work shall be deemed part of an effective technological
|
||||
measure under any applicable law fulfilling obligations under article
|
||||
11 of the WIPO copyright treaty adopted on 20 December 1996, or
|
||||
similar laws prohibiting or restricting circumvention of such
|
||||
measures.
|
||||
|
||||
When you convey a covered work, you waive any legal power to forbid
|
||||
circumvention of technological measures to the extent such circumvention
|
||||
is effected by exercising rights under this License with respect to
|
||||
the covered work, and you disclaim any intention to limit operation or
|
||||
modification of the work as a means of enforcing, against the work's
|
||||
users, your or third parties' legal rights to forbid circumvention of
|
||||
technological measures.
|
||||
|
||||
4. Conveying Verbatim Copies.
|
||||
|
||||
You may convey verbatim copies of the Program's source code as you
|
||||
receive it, in any medium, provided that you conspicuously and
|
||||
appropriately publish on each copy an appropriate copyright notice;
|
||||
keep intact all notices stating that this License and any
|
||||
non-permissive terms added in accord with section 7 apply to the code;
|
||||
keep intact all notices of the absence of any warranty; and give all
|
||||
recipients a copy of this License along with the Program.
|
||||
|
||||
You may charge any price or no price for each copy that you convey,
|
||||
and you may offer support or warranty protection for a fee.
|
||||
|
||||
5. Conveying Modified Source Versions.
|
||||
|
||||
You may convey a work based on the Program, or the modifications to
|
||||
produce it from the Program, in the form of source code under the
|
||||
terms of section 4, provided that you also meet all of these conditions:
|
||||
|
||||
a) The work must carry prominent notices stating that you modified
|
||||
it, and giving a relevant date.
|
||||
|
||||
b) The work must carry prominent notices stating that it is
|
||||
released under this License and any conditions added under section
|
||||
7. This requirement modifies the requirement in section 4 to
|
||||
"keep intact all notices".
|
||||
|
||||
c) You must license the entire work, as a whole, under this
|
||||
License to anyone who comes into possession of a copy. This
|
||||
License will therefore apply, along with any applicable section 7
|
||||
additional terms, to the whole of the work, and all its parts,
|
||||
regardless of how they are packaged. This License gives no
|
||||
permission to license the work in any other way, but it does not
|
||||
invalidate such permission if you have separately received it.
|
||||
|
||||
d) If the work has interactive user interfaces, each must display
|
||||
Appropriate Legal Notices; however, if the Program has interactive
|
||||
interfaces that do not display Appropriate Legal Notices, your
|
||||
work need not make them do so.
|
||||
|
||||
A compilation of a covered work with other separate and independent
|
||||
works, which are not by their nature extensions of the covered work,
|
||||
and which are not combined with it such as to form a larger program,
|
||||
in or on a volume of a storage or distribution medium, is called an
|
||||
"aggregate" if the compilation and its resulting copyright are not
|
||||
used to limit the access or legal rights of the compilation's users
|
||||
beyond what the individual works permit. Inclusion of a covered work
|
||||
in an aggregate does not cause this License to apply to the other
|
||||
parts of the aggregate.
|
||||
|
||||
6. Conveying Non-Source Forms.
|
||||
|
||||
You may convey a covered work in object code form under the terms
|
||||
of sections 4 and 5, provided that you also convey the
|
||||
machine-readable Corresponding Source under the terms of this License,
|
||||
in one of these ways:
|
||||
|
||||
a) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by the
|
||||
Corresponding Source fixed on a durable physical medium
|
||||
customarily used for software interchange.
|
||||
|
||||
b) Convey the object code in, or embodied in, a physical product
|
||||
(including a physical distribution medium), accompanied by a
|
||||
written offer, valid for at least three years and valid for as
|
||||
long as you offer spare parts or customer support for that product
|
||||
model, to give anyone who possesses the object code either (1) a
|
||||
copy of the Corresponding Source for all the software in the
|
||||
product that is covered by this License, on a durable physical
|
||||
medium customarily used for software interchange, for a price no
|
||||
more than your reasonable cost of physically performing this
|
||||
conveying of source, or (2) access to copy the
|
||||
Corresponding Source from a network server at no charge.
|
||||
|
||||
c) Convey individual copies of the object code with a copy of the
|
||||
written offer to provide the Corresponding Source. This
|
||||
alternative is allowed only occasionally and noncommercially, and
|
||||
only if you received the object code with such an offer, in accord
|
||||
with subsection 6b.
|
||||
|
||||
d) Convey the object code by offering access from a designated
|
||||
place (gratis or for a charge), and offer equivalent access to the
|
||||
Corresponding Source in the same way through the same place at no
|
||||
further charge. You need not require recipients to copy the
|
||||
Corresponding Source along with the object code. If the place to
|
||||
copy the object code is a network server, the Corresponding Source
|
||||
may be on a different server (operated by you or a third party)
|
||||
that supports equivalent copying facilities, provided you maintain
|
||||
clear directions next to the object code saying where to find the
|
||||
Corresponding Source. Regardless of what server hosts the
|
||||
Corresponding Source, you remain obligated to ensure that it is
|
||||
available for as long as needed to satisfy these requirements.
|
||||
|
||||
e) Convey the object code using peer-to-peer transmission, provided
|
||||
you inform other peers where the object code and Corresponding
|
||||
Source of the work are being offered to the general public at no
|
||||
charge under subsection 6d.
|
||||
|
||||
A separable portion of the object code, whose source code is excluded
|
||||
from the Corresponding Source as a System Library, need not be
|
||||
included in conveying the object code work.
|
||||
|
||||
A "User Product" is either (1) a "consumer product", which means any
|
||||
tangible personal property which is normally used for personal, family,
|
||||
or household purposes, or (2) anything designed or sold for incorporation
|
||||
into a dwelling. In determining whether a product is a consumer product,
|
||||
doubtful cases shall be resolved in favor of coverage. For a particular
|
||||
product received by a particular user, "normally used" refers to a
|
||||
typical or common use of that class of product, regardless of the status
|
||||
of the particular user or of the way in which the particular user
|
||||
actually uses, or expects or is expected to use, the product. A product
|
||||
is a consumer product regardless of whether the product has substantial
|
||||
commercial, industrial or non-consumer uses, unless such uses represent
|
||||
the only significant mode of use of the product.
|
||||
|
||||
"Installation Information" for a User Product means any methods,
|
||||
procedures, authorization keys, or other information required to install
|
||||
and execute modified versions of a covered work in that User Product from
|
||||
a modified version of its Corresponding Source. The information must
|
||||
suffice to ensure that the continued functioning of the modified object
|
||||
code is in no case prevented or interfered with solely because
|
||||
modification has been made.
|
||||
|
||||
If you convey an object code work under this section in, or with, or
|
||||
specifically for use in, a User Product, and the conveying occurs as
|
||||
part of a transaction in which the right of possession and use of the
|
||||
User Product is transferred to the recipient in perpetuity or for a
|
||||
fixed term (regardless of how the transaction is characterized), the
|
||||
Corresponding Source conveyed under this section must be accompanied
|
||||
by the Installation Information. But this requirement does not apply
|
||||
if neither you nor any third party retains the ability to install
|
||||
modified object code on the User Product (for example, the work has
|
||||
been installed in ROM).
|
||||
|
||||
The requirement to provide Installation Information does not include a
|
||||
requirement to continue to provide support service, warranty, or updates
|
||||
for a work that has been modified or installed by the recipient, or for
|
||||
the User Product in which it has been modified or installed. Access to a
|
||||
network may be denied when the modification itself materially and
|
||||
adversely affects the operation of the network or violates the rules and
|
||||
protocols for communication across the network.
|
||||
|
||||
Corresponding Source conveyed, and Installation Information provided,
|
||||
in accord with this section must be in a format that is publicly
|
||||
documented (and with an implementation available to the public in
|
||||
source code form), and must require no special password or key for
|
||||
unpacking, reading or copying.
|
||||
|
||||
7. Additional Terms.
|
||||
|
||||
"Additional permissions" are terms that supplement the terms of this
|
||||
License by making exceptions from one or more of its conditions.
|
||||
Additional permissions that are applicable to the entire Program shall
|
||||
be treated as though they were included in this License, to the extent
|
||||
that they are valid under applicable law. If additional permissions
|
||||
apply only to part of the Program, that part may be used separately
|
||||
under those permissions, but the entire Program remains governed by
|
||||
this License without regard to the additional permissions.
|
||||
|
||||
When you convey a copy of a covered work, you may at your option
|
||||
remove any additional permissions from that copy, or from any part of
|
||||
it. (Additional permissions may be written to require their own
|
||||
removal in certain cases when you modify the work.) You may place
|
||||
additional permissions on material, added by you to a covered work,
|
||||
for which you have or can give appropriate copyright permission.
|
||||
|
||||
Notwithstanding any other provision of this License, for material you
|
||||
add to a covered work, you may (if authorized by the copyright holders of
|
||||
that material) supplement the terms of this License with terms:
|
||||
|
||||
a) Disclaiming warranty or limiting liability differently from the
|
||||
terms of sections 15 and 16 of this License; or
|
||||
|
||||
b) Requiring preservation of specified reasonable legal notices or
|
||||
author attributions in that material or in the Appropriate Legal
|
||||
Notices displayed by works containing it; or
|
||||
|
||||
c) Prohibiting misrepresentation of the origin of that material, or
|
||||
requiring that modified versions of such material be marked in
|
||||
reasonable ways as different from the original version; or
|
||||
|
||||
d) Limiting the use for publicity purposes of names of licensors or
|
||||
authors of the material; or
|
||||
|
||||
e) Declining to grant rights under trademark law for use of some
|
||||
trade names, trademarks, or service marks; or
|
||||
|
||||
f) Requiring indemnification of licensors and authors of that
|
||||
material by anyone who conveys the material (or modified versions of
|
||||
it) with contractual assumptions of liability to the recipient, for
|
||||
any liability that these contractual assumptions directly impose on
|
||||
those licensors and authors.
|
||||
|
||||
All other non-permissive additional terms are considered "further
|
||||
restrictions" within the meaning of section 10. If the Program as you
|
||||
received it, or any part of it, contains a notice stating that it is
|
||||
governed by this License along with a term that is a further
|
||||
restriction, you may remove that term. If a license document contains
|
||||
a further restriction but permits relicensing or conveying under this
|
||||
License, you may add to a covered work material governed by the terms
|
||||
of that license document, provided that the further restriction does
|
||||
not survive such relicensing or conveying.
|
||||
|
||||
If you add terms to a covered work in accord with this section, you
|
||||
must place, in the relevant source files, a statement of the
|
||||
additional terms that apply to those files, or a notice indicating
|
||||
where to find the applicable terms.
|
||||
|
||||
Additional terms, permissive or non-permissive, may be stated in the
|
||||
form of a separately written license, or stated as exceptions;
|
||||
the above requirements apply either way.
|
||||
|
||||
8. Termination.
|
||||
|
||||
You may not propagate or modify a covered work except as expressly
|
||||
provided under this License. Any attempt otherwise to propagate or
|
||||
modify it is void, and will automatically terminate your rights under
|
||||
this License (including any patent licenses granted under the third
|
||||
paragraph of section 11).
|
||||
|
||||
However, if you cease all violation of this License, then your
|
||||
license from a particular copyright holder is reinstated (a)
|
||||
provisionally, unless and until the copyright holder explicitly and
|
||||
finally terminates your license, and (b) permanently, if the copyright
|
||||
holder fails to notify you of the violation by some reasonable means
|
||||
prior to 60 days after the cessation.
|
||||
|
||||
Moreover, your license from a particular copyright holder is
|
||||
reinstated permanently if the copyright holder notifies you of the
|
||||
violation by some reasonable means, this is the first time you have
|
||||
received notice of violation of this License (for any work) from that
|
||||
copyright holder, and you cure the violation prior to 30 days after
|
||||
your receipt of the notice.
|
||||
|
||||
Termination of your rights under this section does not terminate the
|
||||
licenses of parties who have received copies or rights from you under
|
||||
this License. If your rights have been terminated and not permanently
|
||||
reinstated, you do not qualify to receive new licenses for the same
|
||||
material under section 10.
|
||||
|
||||
9. Acceptance Not Required for Having Copies.
|
||||
|
||||
You are not required to accept this License in order to receive or
|
||||
run a copy of the Program. Ancillary propagation of a covered work
|
||||
occurring solely as a consequence of using peer-to-peer transmission
|
||||
to receive a copy likewise does not require acceptance. However,
|
||||
nothing other than this License grants you permission to propagate or
|
||||
modify any covered work. These actions infringe copyright if you do
|
||||
not accept this License. Therefore, by modifying or propagating a
|
||||
covered work, you indicate your acceptance of this License to do so.
|
||||
|
||||
10. Automatic Licensing of Downstream Recipients.
|
||||
|
||||
Each time you convey a covered work, the recipient automatically
|
||||
receives a license from the original licensors, to run, modify and
|
||||
propagate that work, subject to this License. You are not responsible
|
||||
for enforcing compliance by third parties with this License.
|
||||
|
||||
An "entity transaction" is a transaction transferring control of an
|
||||
organization, or substantially all assets of one, or subdividing an
|
||||
organization, or merging organizations. If propagation of a covered
|
||||
work results from an entity transaction, each party to that
|
||||
transaction who receives a copy of the work also receives whatever
|
||||
licenses to the work the party's predecessor in interest had or could
|
||||
give under the previous paragraph, plus a right to possession of the
|
||||
Corresponding Source of the work from the predecessor in interest, if
|
||||
the predecessor has it or can get it with reasonable efforts.
|
||||
|
||||
You may not impose any further restrictions on the exercise of the
|
||||
rights granted or affirmed under this License. For example, you may
|
||||
not impose a license fee, royalty, or other charge for exercise of
|
||||
rights granted under this License, and you may not initiate litigation
|
||||
(including a cross-claim or counterclaim in a lawsuit) alleging that
|
||||
any patent claim is infringed by making, using, selling, offering for
|
||||
sale, or importing the Program or any portion of it.
|
||||
|
||||
11. Patents.
|
||||
|
||||
A "contributor" is a copyright holder who authorizes use under this
|
||||
License of the Program or a work on which the Program is based. The
|
||||
work thus licensed is called the contributor's "contributor version".
|
||||
|
||||
A contributor's "essential patent claims" are all patent claims
|
||||
owned or controlled by the contributor, whether already acquired or
|
||||
hereafter acquired, that would be infringed by some manner, permitted
|
||||
by this License, of making, using, or selling its contributor version,
|
||||
but do not include claims that would be infringed only as a
|
||||
consequence of further modification of the contributor version. For
|
||||
purposes of this definition, "control" includes the right to grant
|
||||
patent sublicenses in a manner consistent with the requirements of
|
||||
this License.
|
||||
|
||||
Each contributor grants you a non-exclusive, worldwide, royalty-free
|
||||
patent license under the contributor's essential patent claims, to
|
||||
make, use, sell, offer for sale, import and otherwise run, modify and
|
||||
propagate the contents of its contributor version.
|
||||
|
||||
In the following three paragraphs, a "patent license" is any express
|
||||
agreement or commitment, however denominated, not to enforce a patent
|
||||
(such as an express permission to practice a patent or covenant not to
|
||||
sue for patent infringement). To "grant" such a patent license to a
|
||||
party means to make such an agreement or commitment not to enforce a
|
||||
patent against the party.
|
||||
|
||||
If you convey a covered work, knowingly relying on a patent license,
|
||||
and the Corresponding Source of the work is not available for anyone
|
||||
to copy, free of charge and under the terms of this License, through a
|
||||
publicly available network server or other readily accessible means,
|
||||
then you must either (1) cause the Corresponding Source to be so
|
||||
available, or (2) arrange to deprive yourself of the benefit of the
|
||||
patent license for this particular work, or (3) arrange, in a manner
|
||||
consistent with the requirements of this License, to extend the patent
|
||||
license to downstream recipients. "Knowingly relying" means you have
|
||||
actual knowledge that, but for the patent license, your conveying the
|
||||
covered work in a country, or your recipient's use of the covered work
|
||||
in a country, would infringe one or more identifiable patents in that
|
||||
country that you have reason to believe are valid.
|
||||
|
||||
If, pursuant to or in connection with a single transaction or
|
||||
arrangement, you convey, or propagate by procuring conveyance of, a
|
||||
covered work, and grant a patent license to some of the parties
|
||||
receiving the covered work authorizing them to use, propagate, modify
|
||||
or convey a specific copy of the covered work, then the patent license
|
||||
you grant is automatically extended to all recipients of the covered
|
||||
work and works based on it.
|
||||
|
||||
A patent license is "discriminatory" if it does not include within
|
||||
the scope of its coverage, prohibits the exercise of, or is
|
||||
conditioned on the non-exercise of one or more of the rights that are
|
||||
specifically granted under this License. You may not convey a covered
|
||||
work if you are a party to an arrangement with a third party that is
|
||||
in the business of distributing software, under which you make payment
|
||||
to the third party based on the extent of your activity of conveying
|
||||
the work, and under which the third party grants, to any of the
|
||||
parties who would receive the covered work from you, a discriminatory
|
||||
patent license (a) in connection with copies of the covered work
|
||||
conveyed by you (or copies made from those copies), or (b) primarily
|
||||
for and in connection with specific products or compilations that
|
||||
contain the covered work, unless you entered into that arrangement,
|
||||
or that patent license was granted, prior to 28 March 2007.
|
||||
|
||||
Nothing in this License shall be construed as excluding or limiting
|
||||
any implied license or other defenses to infringement that may
|
||||
otherwise be available to you under applicable patent law.
|
||||
|
||||
12. No Surrender of Others' Freedom.
|
||||
|
||||
If conditions are imposed on you (whether by court order, agreement or
|
||||
otherwise) that contradict the conditions of this License, they do not
|
||||
excuse you from the conditions of this License. If you cannot convey a
|
||||
covered work so as to satisfy simultaneously your obligations under this
|
||||
License and any other pertinent obligations, then as a consequence you may
|
||||
not convey it at all. For example, if you agree to terms that obligate you
|
||||
to collect a royalty for further conveying from those to whom you convey
|
||||
the Program, the only way you could satisfy both those terms and this
|
||||
License would be to refrain entirely from conveying the Program.
|
||||
|
||||
13. Use with the GNU Affero General Public License.
|
||||
|
||||
Notwithstanding any other provision of this License, you have
|
||||
permission to link or combine any covered work with a work licensed
|
||||
under version 3 of the GNU Affero General Public License into a single
|
||||
combined work, and to convey the resulting work. The terms of this
|
||||
License will continue to apply to the part which is the covered work,
|
||||
but the special requirements of the GNU Affero General Public License,
|
||||
section 13, concerning interaction through a network will apply to the
|
||||
combination as such.
|
||||
|
||||
14. Revised Versions of this License.
|
||||
|
||||
The Free Software Foundation may publish revised and/or new versions of
|
||||
the GNU General Public License from time to time. Such new versions will
|
||||
be similar in spirit to the present version, but may differ in detail to
|
||||
address new problems or concerns.
|
||||
|
||||
Each version is given a distinguishing version number. If the
|
||||
Program specifies that a certain numbered version of the GNU General
|
||||
Public License "or any later version" applies to it, you have the
|
||||
option of following the terms and conditions either of that numbered
|
||||
version or of any later version published by the Free Software
|
||||
Foundation. If the Program does not specify a version number of the
|
||||
GNU General Public License, you may choose any version ever published
|
||||
by the Free Software Foundation.
|
||||
|
||||
If the Program specifies that a proxy can decide which future
|
||||
versions of the GNU General Public License can be used, that proxy's
|
||||
public statement of acceptance of a version permanently authorizes you
|
||||
to choose that version for the Program.
|
||||
|
||||
Later license versions may give you additional or different
|
||||
permissions. However, no additional obligations are imposed on any
|
||||
author or copyright holder as a result of your choosing to follow a
|
||||
later version.
|
||||
|
||||
15. Disclaimer of Warranty.
|
||||
|
||||
THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
|
||||
APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
|
||||
HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
|
||||
OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
|
||||
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||||
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
|
||||
|
||||
16. Limitation of Liability.
|
||||
|
||||
IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
|
||||
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
|
||||
THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
|
||||
GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
|
||||
USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
|
||||
DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
|
||||
PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
|
||||
EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
|
||||
SUCH DAMAGES.
|
||||
|
||||
17. Interpretation of Sections 15 and 16.
|
||||
|
||||
If the disclaimer of warranty and limitation of liability provided
|
||||
above cannot be given local legal effect according to their terms,
|
||||
reviewing courts shall apply local law that most closely approximates
|
||||
an absolute waiver of all civil liability in connection with the
|
||||
Program, unless a warranty or assumption of liability accompanies a
|
||||
copy of the Program in return for a fee.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
How to Apply These Terms to Your New Programs
|
||||
|
||||
If you develop a new program, and you want it to be of the greatest
|
||||
possible use to the public, the best way to achieve this is to make it
|
||||
free software which everyone can redistribute and change under these terms.
|
||||
|
||||
To do so, attach the following notices to the program. It is safest
|
||||
to attach them to the start of each source file to most effectively
|
||||
state the exclusion of warranty; and each file should have at least
|
||||
the "copyright" line and a pointer to where the full notice is found.
|
||||
|
||||
<one line to give the program's name and a brief idea of what it does.>
|
||||
Copyright (C) <year> <name of author>
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
Also add information on how to contact you by electronic and paper mail.
|
||||
|
||||
If the program does terminal interaction, make it output a short
|
||||
notice like this when it starts in an interactive mode:
|
||||
|
||||
<program> Copyright (C) <year> <name of author>
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
|
||||
This is free software, and you are welcome to redistribute it
|
||||
under certain conditions; type `show c' for details.
|
||||
|
||||
The hypothetical commands `show w' and `show c' should show the appropriate
|
||||
parts of the General Public License. Of course, your program's commands
|
||||
might be different; for a GUI interface, you would use an "about box".
|
||||
|
||||
You should also get your employer (if you work as a programmer) or school,
|
||||
if any, to sign a "copyright disclaimer" for the program, if necessary.
|
||||
For more information on this, and how to apply and follow the GNU GPL, see
|
||||
<https://www.gnu.org/licenses/>.
|
||||
|
||||
The GNU General Public License does not permit incorporating your program
|
||||
into proprietary programs. If your program is a subroutine library, you
|
||||
may consider it more useful to permit linking proprietary applications with
|
||||
the library. If this is what you want to do, use the GNU Lesser General
|
||||
Public License instead of this License. But first, please read
|
||||
<https://www.gnu.org/licenses/why-not-lgpl.html>.
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
@@ -1,10 +1,65 @@
|
||||
# mqttclient
|
||||
|
||||
**一个基于socket API之上的跨平台MQTT客户端**
|
||||
|
||||
## 整体框架
|
||||

|
||||
基于socket API的MQTT客户端,拥有非常简洁的API接口,以极少的资源实现QOS2的服务质量,并且无缝衔接了mbedtls加密库。
|
||||
|
||||
> 备注:目前只支持linux及TencentOS tiny系统
|
||||
## 优势:
|
||||
- **基于标准BSD socket之上开发**,只要是兼容BSD socket的系统均可使用。
|
||||
|
||||
- **稳定**:无论是`掉线重连`,`丢包重发`,都是严格`遵循MQTT协议标准`执行,除此之外对**大数据量**的测试无论是收是发,都是非常稳定(一次发送`135K`数据,3秒一次),高频测试也是非常稳定(7个主题同时收发,每秒一次,也就是1秒14个mqtt报文,服务质量QoS0、QoS1、QoS2都有)。因为作者以极少的资源设计了`记录机制`,对采用QoS1服务质量的报文必须保证到达一次,当发布的主题(qos1、qos2都适用)没有被服务器收到时会自动重发,而对QoS2服务质量的报文保证有且只有处理一次(如果不相信它稳定性的同学可以自己去修改源码,专门为QoS2服务质量去做测试,故意不回复`PUBREC`包,让服务器重发QoS2报文,且看看客户端是否有且只有处理一次),而对于掉线重连的稳定性,这种则是**基本操作**了,没啥好说的,在自动重连后还会自动重新订阅主题,保证主题不会丢失,因此在测试中稳定性极好。
|
||||
|
||||
- **轻量级**:整个代码工程极其简单,不使用mbedtls情况下,占用资源极少,作者曾使用esp8266模组与云端通信,整个工程代码消耗的RAM不足15k(包括系统占用的开销,对数据的处理开销,而此次还是未优化的情况下,还依旧完美保留了掉线重连的稳定性,但是对应qos1、qos2服务质量的报文则未做测试,因为STM32F103C8T6芯片资源实在是太少了,折腾不起)。
|
||||
|
||||
- **无缝衔接mbedtls加密传输**,让网络传输更加安全,而且接口层完全不需要用户理会,无论是否加密,mqttclient对用户提供的API接口是**没有变化**的,这就很好的兼容了一套代应用层的码可以加密传输也可以不加密传输。
|
||||
|
||||
- **拥有极简的API接口**,总的来说,mqttclient的配置都有默认值,基本无需配置都能使用的,也可以随意配置,对配置都有健壮性检测,这样子设计的API接口也是非常简单。
|
||||
|
||||
- **有非常好的代码风格与思想**:整个代码采用分层式设计,代码实现采用异步处理的思想,降低耦合,提高性能,具体体现在什么地方呢?很简单,目前市面上很多MQTT客户端发布主题都是要阻塞等待ack,这是非常暴力的行为,阻塞当前线程等待服务器的应答,那如果我想要发送数据怎么办,或者我要重复检测数据怎么办,你可能会说,指定阻塞时间等待,那如果网络延迟,ack迟迟不来,我就白等了吗,对于qos1、qos2的服务质量怎么办,所以说这种还是要异步处理的思想,我发布主题,那我发布出去就好了,不需要等待,对于qos1、qos2服务质量的MQTT报文,如果服务器没收到,那我重发就可以,这种重发也是异步的处理,完全不会阻塞当前线程。
|
||||
|
||||
- **MQTT协议支持主题通配符`“#”、“+”`。**
|
||||
|
||||
- **订阅的主题与消息处理完全分离**,让编程逻辑更加简单易用,用户无需理会错综复杂的逻辑关系。
|
||||
|
||||
- **mqttclient内部已实现保活处理机制**,无需用户过多关心理会,用户只需专心处理应用功能即可。
|
||||
|
||||
- **无缝衔接salof**:它是一个同步异步日志输出框架,在空闲时候输出对应的日志信息,也可以将信息写入flash中保存,方便调试。
|
||||
|
||||
- **不对外产生依赖。**
|
||||
|
||||
## 整体框架
|
||||
|
||||
拥有非常明确的分层框架。
|
||||
|
||||

|
||||
|
||||
**目前已适配了Linux、TencentOS tiny平台,除此之外,TencentOS tiny的AT框架亦可以使用(RAM消耗不足15K),并且稳定性极好!**
|
||||
|
||||
|
||||
| 平台 | 代码位置 |
|
||||
| -------------- | -------- |
|
||||
| Linux | [https://github.com/jiejieTop/mqttclient](https://github.com/jiejieTop/mqttclient) |
|
||||
| TencentOS tiny | [https://github.com/Tencent/TencentOS-tiny/tree/master/board/Fire_STM32F429](https://github.com/Tencent/TencentOS-tiny/tree/master/board/Fire_STM32F429) |
|
||||
|
||||
|
||||
|
||||
|
||||
## 版本
|
||||
|
||||
| 发布版本 | 描述 |
|
||||
| --- | --- |
|
||||
| [v1.0.0] | 初次发布,完成基本概念及其稳定性验证 |
|
||||
| [v1.0.1] | 修复主动与服务器断开连接时的逻辑 |
|
||||
|
||||
## 问题
|
||||
|
||||
欢迎以 [GitHub Issues](https://github.com/jiejieTop/mqttclient/issues) 的形式提交问题和bug报告
|
||||
|
||||
## 版权和许可
|
||||
|
||||
mqttclient 由 [Apache License v2.0](https://github.com/jiejieTop/mqttclient/blob/master/LICENSE) 许可 提供。
|
||||
|
||||
简单谈谈关于开源协议的描述,[Apache License v2.0](https://github.com/jiejieTop/mqttclient/blob/master/LICENSE) 鼓励代码共享和尊重原作者的著作权,可以自由的使用,修改源代码,也可以将修改后的代码作为开源或者专有软件再发布(作为开源或闭源商业软件),**但本源码必须保留作者版权声明**。
|
||||
|
||||
## linux平台下测试使用
|
||||
### 安装cmake:
|
||||
@@ -17,20 +72,20 @@ sudo apt-get install cmake
|
||||
```c
|
||||
init_params.connect_params.network_params.network_ssl_params.ca_crt = test_ca_get(); /* CA证书 */
|
||||
init_params.connect_params.network_params.addr = "xxxxxxx"; /* 服务器域名 */
|
||||
init_params.connect_params.network_params.port = "8883";
|
||||
init_params.connect_params.network_params.port = "8883"; /* 服务器端口号 */
|
||||
init_params.connect_params.user_name = "xxxxxxx"; /* 用户名 */
|
||||
init_params.connect_params.password = "xxxxxxx"; /* 密码 */
|
||||
init_params.connect_params.client_id = "xxxxxxx"; /* 客户端id */
|
||||
```
|
||||
|
||||
### 打开salof
|
||||
[salof](https://github.com/jiejieTop/salof) 全称是:`Synchronous Asynchronous Log Output Framework`(同步异步日志输出框架)
|
||||
### mbedtls
|
||||
|
||||
默认打开mbedtls。
|
||||
|
||||
[salof](https://github.com/jiejieTop/salof) 全称是:`Synchronous Asynchronous Log Output Framework`(同步异步日志输出框架),它是一个同步异步日志输出框架,在空闲时候输出对应的日志信息,并且该库与mqttclient无缝衔接。
|
||||
|
||||
**配置对应的日志输出级别:**
|
||||
|
||||
它是一个异步日志输出库,在空闲时候输出对应的日志信息,并且该库与mqttclient无缝衔接,如果不需要则将 `LOG_IS_SALOF` 定义为0即可。
|
||||
```c
|
||||
#define LOG_IS_SALOF 0
|
||||
```
|
||||
在`mqttclient/common/log/config.h`配置文件中打开对应的日志输出级别:
|
||||
```c
|
||||
#define BASE_LEVEL (0)
|
||||
#define ASSERT_LEVEL (BASE_LEVEL + 1) /* 日志输出级别:断言级别(非常高优先级) */
|
||||
@@ -39,17 +94,75 @@ sudo apt-get install cmake
|
||||
#define INFO_LEVEL (WARN_LEVEL + 1) /* 日志输出级别:信息级别(低优先级) */
|
||||
#define DEBUG_LEVEL (INFO_LEVEL + 1) /* 日志输出级别:调试级别(更低优先级) */
|
||||
|
||||
#define SALOF_OS USE_LINUX /* 选择对应的平台:Linux/FreeRTOS/TencentOS */
|
||||
#define LOG_LEVEL WARN_LEVEL /* 日志输出级别 */
|
||||
```
|
||||
|
||||
**日志其他选项:**
|
||||
|
||||
- 终端带颜色
|
||||
- 时间戳
|
||||
- 标签
|
||||
|
||||
### mqttclient的配置
|
||||
配置文件是:`mqttclient/mqtt_config.h`,在这里可以根据自身需求配置对应的信息。
|
||||
如是否选择`mbedtls`加密层:
|
||||
|
||||
配置mqtt等待应答列表的最大值,对于qos1 qos2服务质量有要求的可以将其设置大一点,当然也必须资源跟得上,它主要是保证qos1 qos2的mqtt报文能准确到达服务器。
|
||||
|
||||
```c
|
||||
#define MQTT_NETWORK_TYPE_TLS MQTT_YES
|
||||
#define MQTT_ACK_HANDLER_NUM_MAX 64
|
||||
```
|
||||
|
||||
选择MQTT协议的版本,默认为4,表示使用MQTT 3.1.1版本,而3则表示为MQTT 3.1版本。
|
||||
|
||||
```c
|
||||
#define MQTT_VERSION 4 // 4 is mqtt 3.1.1
|
||||
```
|
||||
|
||||
设置默认的保活时间,它主要是保证MQTT客户端与服务器的保持活性连接,单位为 秒 ,比如MQTT客户端与服务器100S没有发送数据了,有没有接收到数据,此时MQTT客户端会发送一个ping包,确认一下这个会话是否存在,如果收到服务器的应答,那么说明这个会话还是存在的,可以随时收发数据,而如果不存在了,就清除会话。
|
||||
|
||||
```c
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 100 // unit: second
|
||||
```
|
||||
|
||||
默认的命令超时,它主要是用于socket读写超时,在MQTT初始化时可以指定:
|
||||
|
||||
```
|
||||
#define MQTT_DEFAULT_CMD_TIMEOUT 4000
|
||||
```
|
||||
|
||||
默认主题的长度,主题是支持通配符的,如果主题太长则会被截断:
|
||||
|
||||
```c
|
||||
#define MQTT_TOPIC_LEN_MAX 64
|
||||
```
|
||||
|
||||
默认的算法数据缓冲区的大小,如果要发送大量数据则修改大一些,在MQTT初始化时可以指定:
|
||||
|
||||
```c
|
||||
#define MQTT_DEFAULT_BUF_SIZE 1024
|
||||
```
|
||||
|
||||
线程相关的配置,如线程栈,线程优先级,线程时间片等:
|
||||
在linux环境下可以是不需要理会这些参数的,而在RTOS平台则需要配置,如果不使用mbedtls,线程栈2048字节已足够,而使用mbedtls加密后,需要配置4096字节以上。
|
||||
```c
|
||||
#define MQTT_THREAD_STACK_SIZE 2048 // 线程栈
|
||||
#define MQTT_THREAD_PRIO 5 // 线程优先级
|
||||
#define MQTT_THREAD_TICK 50 // 线程时间片
|
||||
```
|
||||
|
||||
默认的重连时间间隔,当发生掉线时,会以这个时间间隔尝试重连:
|
||||
```c
|
||||
#define MQTT_RECONNECT_DEFAULT_DURATION 1000
|
||||
```
|
||||
|
||||
其他不需要怎么配置的东西:
|
||||
```c
|
||||
#define MQTT_MAX_PACKET_ID (0xFFFF - 1) // mqtt报文id
|
||||
#define MQTT_MAX_CMD_TIMEOUT 20000 //最大的命令超时参数
|
||||
#define MQTT_MIN_CMD_TIMEOUT 1000 //最小的命令超时参数
|
||||
```
|
||||
|
||||
> ps:以上参数基本不需要怎么配置的,直接用即可~
|
||||
|
||||
### 编译 & 运行
|
||||
```bash
|
||||
./build.sh
|
||||
@@ -60,6 +173,7 @@ sudo apt-get install cmake
|
||||
- 整体采用分层式设计,代码实现采用异步设计方式,降低耦合。
|
||||
- 消息的处理使用回调的方式处理:用户指定`[订阅的主题]`与指定`[消息的处理函数]`
|
||||
- 不对外产生依赖
|
||||
|
||||
## API
|
||||
`mqttclient`拥有非常简洁的`api`接口
|
||||
```c
|
||||
@@ -79,10 +193,10 @@ int mqtt_yield(mqtt_client_t* c, int timeout_ms);
|
||||
```c
|
||||
typedef struct mqtt_client {
|
||||
unsigned short packet_id;
|
||||
unsigned char *read_buf;
|
||||
unsigned char *write_buf;
|
||||
unsigned char ping_outstanding;
|
||||
unsigned char ack_handler_number;
|
||||
unsigned char *read_buf;
|
||||
unsigned char *write_buf;
|
||||
unsigned int cmd_timeout;
|
||||
unsigned int read_buf_size;
|
||||
unsigned int write_buf_size;
|
||||
@@ -97,10 +211,12 @@ typedef struct mqtt_client {
|
||||
network_t *network;
|
||||
platform_thread_t *thread;
|
||||
platform_timer_t reconnect_timer;
|
||||
platform_timer_t ping_timer;
|
||||
platform_timer_t last_sent;
|
||||
platform_timer_t last_received;
|
||||
connect_params_t *connect_params;
|
||||
} mqtt_client_t;
|
||||
```
|
||||
|
||||
该结构主要维护以下内容:
|
||||
1. 读写数据缓冲区`read_buf、write_buf`
|
||||
2. 命令超时时间`cmd_timeout`(主要是读写阻塞时间、等待响应的时间、重连等待时间)
|
||||
@@ -108,82 +224,158 @@ typedef struct mqtt_client {
|
||||
4. 维护消息处理列表`msg_handler_list`,这是`mqtt`协议必须实现的内容,所有来自服务器的`publish`报文都会被处理(前提是订阅了对应的消息)
|
||||
5. 维护一个网卡接口`network`
|
||||
6. 维护一个内部线程`thread`,所有来自服务器的mqtt包都会在这里被处理!
|
||||
7. 两个定时器,分别是掉线重连定时器与保活定时器`reconnect_timer、ping_timer`
|
||||
7. 两个定时器,分别是掉线重连定时器与保活定时器`reconnect_timer、last_sent、last_received`
|
||||
8. 一些连接的参数`connect_params`
|
||||
|
||||
## 初始化
|
||||
|
||||
## mqttclient实现
|
||||
|
||||
以下是整个框架的实现方式,方便大家更容易理解mqttclient的代码与设计思想,让大家能够修改源码与使用,还可以提交pr或者issues,开源的世界期待各位大神的参与,感谢!
|
||||
|
||||
除此之外以下代码的`记录机制`与其`超时处理机制`是非常好的编程思想,大家有兴趣一定要看源代码!
|
||||
|
||||
### 初始化
|
||||
|
||||
```c
|
||||
int mqtt_init(mqtt_client_t* c, client_init_params_t* init)
|
||||
```
|
||||
|
||||
主要是配置`mqtt_client_t`结构的相关信息,如果没有指定初始化参数,则系统会提供默认的参数。
|
||||
但连接部分的参数则必须指定:
|
||||
|
||||
```c
|
||||
init_params.connect_params.network_params.addr = "[你的mqtt服务器IP地址或者是域名]";
|
||||
init_params.connect_params.network_params.port = 1883; //端口号
|
||||
init_params.connect_params.user_name = "jiejietop";
|
||||
init_params.connect_params.password = "123456";
|
||||
init_params.connect_params.client_id = "clientid";
|
||||
|
||||
mqtt_init(&client, &init_params);
|
||||
```
|
||||
|
||||
## 连接服务器
|
||||
### 连接服务器
|
||||
|
||||
```c
|
||||
int mqtt_connect(mqtt_client_t* c);
|
||||
```
|
||||
连接服务器则是使用非异步的方式设计,因为必须等待连接上服务器才能进行下一步操作。
|
||||
过程如下
|
||||
|
||||
参数只有 `mqtt_client_t` 类型的指针,字符串类型的`主题`(支持通配符"#" "+"),主题的`服务质量`,以及收到报文的`处理函数`,如不指定则有默认处理函数。连接服务器则是使用非异步的方式设计,因为必须等待连接上服务器才能进行下一步操作。
|
||||
|
||||
过程如下:
|
||||
1. 调用底层的连接函数连接上服务器:
|
||||
|
||||
```c
|
||||
c->network->connect(c->network);
|
||||
```
|
||||
|
||||
2. 序列化`mqtt`的`CONNECT`报文并且发送
|
||||
|
||||
```c
|
||||
MQTTSerialize_connect(c->write_buf, c->write_buf_size, &connect_data)
|
||||
mqtt_send_packet(c, len, &connect_timer)
|
||||
```
|
||||
|
||||
3. 等待来自服务器的`CONNACK`报文
|
||||
|
||||
```c
|
||||
mqtt_wait_packet(c, CONNACK, &connect_timer)
|
||||
```
|
||||
4. 连接成功后创建一个内部线程`mqtt_yield_thread`
|
||||
|
||||
4. 连接成功后创建一个内部线程`mqtt_yield_thread`,并在合适的时候启动它:
|
||||
|
||||
```c
|
||||
platform_thread_init("mqtt_yield_thread", mqtt_yield_thread, c, MQTT_THREAD_STACK_SIZE, MQTT_THREAD_PRIO, MQTT_THREAD_TICK)
|
||||
|
||||
if (NULL != c->thread) {
|
||||
mqtt_set_client_state(c, CLIENT_STATE_CONNECTED);
|
||||
platform_thread_startup(c->thread);
|
||||
platform_thread_start(c->thread); /* start run mqtt thread */
|
||||
}
|
||||
```
|
||||
## 订阅报文
|
||||
|
||||
5. 而对于重连来说则不会重新创建线程,直接改变客户端状态为连接状态即可:
|
||||
|
||||
```c
|
||||
mqtt_set_client_state(c, CLIENT_STATE_CONNECTED);
|
||||
```
|
||||
|
||||
### 订阅报文
|
||||
|
||||
```c
|
||||
int mqtt_subscribe(mqtt_client_t* c, const char* topic_filter, mqtt_qos_t qos, message_handler_t handler)
|
||||
```
|
||||
|
||||
订阅报文使用异步设计来实现的:
|
||||
过程如下:
|
||||
1. 序列化订阅报文并且发送给服务器
|
||||
|
||||
```c
|
||||
MQTTSerialize_subscribe(c->write_buf, c->write_buf_size, 0, mqtt_get_next_packet_id(c), 1, &topic, (int*)&qos)
|
||||
mqtt_send_packet(c, len, &timer)
|
||||
```
|
||||
|
||||
2. 创建对应的消息处理节点,这个消息节点在收到服务器的`SUBACK`订阅应答报文后会挂载到消息处理列表`msg_handler_list`上
|
||||
|
||||
```c
|
||||
mqtt_msg_handler_create(topic_filter, qos, handler)
|
||||
```
|
||||
3. 在发送了报文给服务器那就要等待服务器的响应了,记录这个等待`SUBACK`
|
||||
|
||||
3. 在发送了报文给服务器那就要等待服务器的响应了,先记录这个等待`SUBACK`
|
||||
|
||||
```c
|
||||
mqtt_ack_list_record(c, SUBACK, mqtt_get_next_packet_id(c), len, msg_handler)
|
||||
```
|
||||
## 取消订阅
|
||||
|
||||
### 取消订阅
|
||||
与订阅报文的逻辑基本差不多的~
|
||||
|
||||
## 发布报文
|
||||
1. 序列化订阅报文并且发送给服务器
|
||||
|
||||
```c
|
||||
MQTTSerialize_unsubscribe(c->write_buf, c->write_buf_size, 0, packet_id, 1, &topic)
|
||||
mqtt_send_packet(c, len, &timer)
|
||||
```
|
||||
|
||||
2. 创建对应的消息处理节点,这个消息节点在收到服务器的`UNSUBACK`取消订阅应答报文后将消息处理列表`msg_handler_list`上的已经订阅的主题消息节点销毁
|
||||
|
||||
```c
|
||||
mqtt_msg_handler_create((const char*)topic_filter, QOS0, NULL)
|
||||
```
|
||||
|
||||
3. 在发送了报文给服务器那就要等待服务器的响应了,先记录这个等待`UNSUBACK`
|
||||
|
||||
```c
|
||||
mqtt_ack_list_record(c, UNSUBACK, packet_id, len, msg_handler)
|
||||
```
|
||||
|
||||
### 发布报文
|
||||
|
||||
```c
|
||||
int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg)
|
||||
```
|
||||
|
||||
参数只有 `mqtt_client_t` 类型的指针,字符串类型的`主题`(支持通配符),要发布的消息(包括`服务质量`、`消息主体`)。
|
||||
|
||||
```c
|
||||
mqtt_message_t msg;
|
||||
|
||||
msg.qos = 2;
|
||||
msg.payload = (void *) buf;
|
||||
|
||||
mqtt_publish(&client, "testtopic1", &msg);
|
||||
```
|
||||
|
||||
核心思想都差不多,过程如下:
|
||||
1. 先序列化发布报文,然后发送到服务器
|
||||
|
||||
```c
|
||||
MQTTSerialize_publish(c->write_buf, c->write_buf_size, 0, msg->qos, msg->retained, msg->id,
|
||||
topic, (unsigned char*)msg->payload, msg->payloadlen);
|
||||
mqtt_send_packet(c, len, &timer)
|
||||
```
|
||||
|
||||
2. 对于QOS0的逻辑,不做任何处理,对于QOS1和QOS2的报文则需要记录下来,在没收到服务器应答的时候进行重发
|
||||
|
||||
```c
|
||||
if (QOS1 == msg->qos) {
|
||||
rc = mqtt_ack_list_record(c, PUBACK, mqtt_get_next_packet_id(c), len, NULL);
|
||||
@@ -191,17 +383,29 @@ mqtt_send_packet(c, len, &timer)
|
||||
rc = mqtt_ack_list_record(c, PUBREC, mqtt_get_next_packet_id(c), len, NULL);
|
||||
}
|
||||
```
|
||||
## 内部线程
|
||||
|
||||
3. 还有非常重要的一点,重发报文的MQTT报文头部需要设置DUP标志位,这是MQTT协议的标准,因此,在重发的时候作者直接操作了报文的DUP标志位,因为修改DUP标志位的函数我没有从MQTT库中找到,所以我封装了一个函数,这与LwIP中的交叉存取思想是一个道理,它假设我知道MQTT报文的所有操作,所以我可以操作它,这样子可以提高很多效率:
|
||||
|
||||
```c
|
||||
mqtt_set_publish_dup(c,1); /* may resend this data, set the udp flag in advance */
|
||||
```
|
||||
|
||||
### 内部线程
|
||||
|
||||
```c
|
||||
static void mqtt_yield_thread(void *arg)
|
||||
```
|
||||
|
||||
主要是对`mqtt_yield`函数的返回值做处理,比如在`disconnect`的时候销毁这个线程。
|
||||
|
||||
## 核心的处理函数`mqtt_yield`
|
||||
### 核心的处理函数
|
||||
|
||||
1. 数据包的处理`mqtt_packet_handle`
|
||||
|
||||
```c
|
||||
static int mqtt_packet_handle(mqtt_client_t* c, platform_timer_t* timer)
|
||||
```
|
||||
|
||||
对不同的包使用不一样的处理:
|
||||
|
||||
```c
|
||||
@@ -242,118 +446,184 @@ static int mqtt_packet_handle(mqtt_client_t* c, platform_timer_t* timer)
|
||||
goto exit;
|
||||
}
|
||||
```
|
||||
|
||||
并且做保活的处理:
|
||||
|
||||
```c
|
||||
mqtt_keep_alive(c)
|
||||
```
|
||||
|
||||
当发生超时后
|
||||
|
||||
```c
|
||||
if (platform_timer_is_expired(&c->last_sent) || platform_timer_is_expired(&c->last_received))
|
||||
```
|
||||
|
||||
序列号一个心跳包并且发送给服务器
|
||||
|
||||
```c
|
||||
MQTTSerialize_pingreq(c->write_buf, c->write_buf_size);
|
||||
mqtt_send_packet(c, len, &timer);
|
||||
```
|
||||
|
||||
当再次发生超时后,表示与服务器的连接已断开,需要重连的操作,设置客户端状态为断开连接
|
||||
|
||||
```c
|
||||
mqtt_set_client_state(c, CLIENT_STATE_DISCONNECTED);
|
||||
```
|
||||
|
||||
2. `ack`链表的扫描,当收到服务器的报文时,对ack列表进行扫描操作
|
||||
|
||||
```c
|
||||
mqtt_ack_list_scan(c);
|
||||
```
|
||||
|
||||
当超时后就销毁ack链表节点:
|
||||
|
||||
```c
|
||||
mqtt_ack_handler_destroy(ack_handler);
|
||||
```
|
||||
|
||||
当然下面这几种报文则需要重发操作:(`PUBACK 、PUBREC、 PUBREL 、PUBCOMP`,保证QOS1 QOS2的服务质量)
|
||||
|
||||
```c
|
||||
if ((ack_handler->type == PUBACK) || (ack_handler->type == PUBREC) || (ack_handler->type == PUBREL) || (ack_handler->type == PUBCOMP))
|
||||
mqtt_ack_handler_resend(c, ack_handler);
|
||||
```
|
||||
|
||||
3. 保持活性的时间过去了,可能掉线了,需要重连操作
|
||||
|
||||
```c
|
||||
mqtt_try_reconnect(c);
|
||||
```
|
||||
|
||||
重连成功后尝试重新订阅报文,保证恢复原始状态~
|
||||
|
||||
```c
|
||||
mqtt_try_resubscribe(c)
|
||||
```
|
||||
## `发布应答`与`发布完成`报文的处理
|
||||
|
||||
### 发布应答与发布完成报文的处理
|
||||
|
||||
```c
|
||||
static int mqtt_puback_and_pubcomp_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
```
|
||||
|
||||
1. 反序列化报文
|
||||
|
||||
```c
|
||||
MQTTDeserialize_ack(&packet_type, &dup, &packet_id, c->read_buf, c->read_buf_size)
|
||||
```
|
||||
|
||||
2. 取消对应的ack记录
|
||||
|
||||
```c
|
||||
mqtt_ack_list_unrecord(c, packet_type, packet_id, NULL);
|
||||
```
|
||||
## `订阅应答`报文的处理
|
||||
|
||||
### 订阅应答报文的处理
|
||||
|
||||
```c
|
||||
static int mqtt_suback_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
```
|
||||
|
||||
1. 反序列化报文
|
||||
|
||||
```c
|
||||
MQTTDeserialize_suback(&packet_id, 1, &count, (int*)&granted_qos, c->read_buf, c->read_buf_size)
|
||||
```
|
||||
|
||||
2. 取消对应的ack记录
|
||||
|
||||
```c
|
||||
mqtt_ack_list_unrecord(c, packet_type, packet_id, NULL);
|
||||
```
|
||||
|
||||
3. 安装对应的订阅消息处理函数,如果是已存在的则不会安装
|
||||
|
||||
```c
|
||||
mqtt_msg_handlers_install(c, msg_handler);
|
||||
```
|
||||
## `取消订阅应答`报文的处理
|
||||
|
||||
### 取消订阅应答报文的处理
|
||||
|
||||
```c
|
||||
static int mqtt_unsuback_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
```
|
||||
|
||||
1. 反序列化报文
|
||||
|
||||
```c
|
||||
MQTTDeserialize_unsuback(&packet_id, c->read_buf, c->read_buf_size)
|
||||
```
|
||||
2. 取消对应的ack记录
|
||||
|
||||
2. 取消对应的ack记录,并且获取到已经订阅的消息处理节点
|
||||
|
||||
```c
|
||||
mqtt_ack_list_unrecord(c, UNSUBACK, packet_id, &msg_handler)
|
||||
```
|
||||
|
||||
3. 销毁对应的订阅消息处理函数
|
||||
|
||||
```c
|
||||
mqtt_msg_handler_destroy(msg_handler);
|
||||
mqtt_msg_handler_destory(msg_handler);
|
||||
```
|
||||
## 来自服务器的`发布`报文的处理
|
||||
|
||||
### 来自服务器的发布报文的处理
|
||||
|
||||
```c
|
||||
static int mqtt_publish_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
```
|
||||
|
||||
1. 反序列化报文
|
||||
|
||||
```c
|
||||
MQTTDeserialize_publish(&msg.dup, &qos, &msg.retained, &msg.id, &topic_name,
|
||||
(unsigned char**)&msg.payload, (int*)&msg.payloadlen, c->read_buf, c->read_buf_size)
|
||||
```
|
||||
|
||||
2. 对于QOS0、QOS1的报文,直接去处理消息
|
||||
|
||||
```c
|
||||
mqtt_deliver_message(c, &topic_name, &msg);
|
||||
```
|
||||
|
||||
3. 对于QOS1的报文,还需要发送一个`PUBACK`应答报文给服务器
|
||||
|
||||
```c
|
||||
MQTTSerialize_ack(c->write_buf, c->write_buf_size, PUBACK, 0, msg.id);
|
||||
```
|
||||
|
||||
4. 而对于QOS2的报文则需要发送`PUBREC`报文给服务器,除此之外还需要记录`PUBREL`到ack链表上,等待服务器的发布释放报文,最后再去处理这个消息
|
||||
|
||||
```c
|
||||
MQTTSerialize_ack(c->write_buf, c->write_buf_size, PUBREC, 0, msg.id);
|
||||
mqtt_ack_list_record(c, PUBREL, msg.id + 1, len, NULL)
|
||||
mqtt_deliver_message(c, &topic_name, &msg);
|
||||
```
|
||||
|
||||
> 说明:一旦注册到ack列表上的报文,当具有重复的报文是不会重新被注册的,它会通过`mqtt_ack_list_node_is_exist`函数判断这个节点是否存在,主要是依赖等待响应的消息类型与msgid。
|
||||
|
||||
## `发布收到`与`发布释放`报文的处理
|
||||
### 发布收到与发布释放报文的处理
|
||||
|
||||
```c
|
||||
static int mqtt_pubrec_and_pubrel_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
```
|
||||
|
||||
1. 反序列化报文
|
||||
|
||||
```c
|
||||
MQTTDeserialize_ack(&packet_type, &dup, &packet_id, c->read_buf, c->read_buf_size)
|
||||
```
|
||||
|
||||
2. 产生一个对应的应答报文
|
||||
|
||||
```c
|
||||
mqtt_publish_ack_packet(c, packet_id, packet_type);
|
||||
```
|
||||
|
||||
3. 取消对应的ack记录
|
||||
|
||||
```c
|
||||
mqtt_ack_list_unrecord(c, UNSUBACK, packet_id, &msg_handler)
|
||||
```
|
||||
|
||||
## 在后台测试
|
||||
nohup ./mqtt-client > log.out 2>&1 &
|
||||
|
@@ -2,17 +2,18 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-15 00:42:16
|
||||
* @LastEditTime : 2020-01-10 01:01:26
|
||||
* @LastEditTime: 2020-03-21 16:49:07
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _ERROR_H_
|
||||
#define _ERROR_H_
|
||||
|
||||
typedef enum mqtt_error {
|
||||
MQTT_SSL_CERT_ERROR = -0x001C, /* cetr parse failed */
|
||||
MQTT_SOCKET_FAILED = -0x001B, /* socket fd failed */
|
||||
MQTT_SOCKET_UNKNOWN_HOST = -0x001A, /* socket unknown host ip or domain */
|
||||
MQTT_SET_PUBLISH_DUP_FAILED = -0x0019, /* mqtt publish packet set udp bit failed */
|
||||
MQTT_CLOSE_SESSION_ERROR = -0x0018, /* mqtt close session error */
|
||||
MQTT_CLEAN_SESSION_ERROR = -0x0018, /* mqtt clean session error */
|
||||
MQTT_ACK_NODE_IS_EXIST = -0x0017, /* mqtt ack list is exist ack node */
|
||||
MQTT_ACK_HANDLER_NUM_TOO_MUCH = -0x0016, /* mqtt ack handler number is too much */
|
||||
MQTT_RESUBSCRIBE_ERROR = -0x0015, /* mqtt resubscribe error */
|
||||
|
@@ -2,17 +2,15 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-27 03:25:58
|
||||
* @LastEditTime : 2020-02-20 01:16:25
|
||||
* @LastEditTime: 2020-02-25 05:42:32
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _LOG_H_
|
||||
#define _LOG_H_
|
||||
|
||||
#define LOG_IS_SALOF 1
|
||||
#include "mqtt_defconfig.h"
|
||||
|
||||
#define LOG_LEVEL DEBUG_LEVEL //WARN_LEVEL DEBUG_LEVEL INFO_LEVEL
|
||||
|
||||
#if LOG_IS_SALOF
|
||||
#ifdef LOG_IS_SALOF
|
||||
#include "salof.h"
|
||||
|
||||
#define LOG_D(fmt, ...) LOG_DEBUG(fmt, ##__VA_ARGS__)
|
||||
|
@@ -2,11 +2,12 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-26 19:11:34
|
||||
* @LastEditTime : 2019-12-28 01:51:38
|
||||
* @LastEditTime: 2020-03-02 01:32:23
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#include "config.h"
|
||||
#include "salof_defconfig.h"
|
||||
|
||||
#ifdef USE_LOG
|
||||
|
||||
void *salof_alloc(unsigned int size)
|
||||
{
|
||||
@@ -119,3 +120,4 @@ char *salof_get_task_name(void)
|
||||
return k_curr_task->name;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -1,6 +1,8 @@
|
||||
#include "fifo.h"
|
||||
#include <string.h>
|
||||
|
||||
#ifdef USE_LOG
|
||||
|
||||
static unsigned int _flbs(unsigned int x) /* find last bit set*/
|
||||
{
|
||||
unsigned int r = 32;
|
||||
@@ -131,3 +133,4 @@ unsigned int fifo_write_able(fifo_t fifo)
|
||||
return (fifo->size - fifo_read_able(fifo));
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -2,13 +2,15 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-25 23:54:38
|
||||
* @LastEditTime : 2019-12-28 01:04:08
|
||||
* @LastEditTime: 2020-02-25 08:10:01
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _FIFO_H_
|
||||
#define _FIFO_H_
|
||||
|
||||
#include "config.h"
|
||||
#include "salof_defconfig.h"
|
||||
|
||||
#ifdef USE_LOG
|
||||
|
||||
#define FIFO_READ 0
|
||||
#define FIFO_WRITE 1
|
||||
@@ -33,5 +35,7 @@ unsigned int fifo_read(fifo_t fifo, void *buff, unsigned int len, unsigned int t
|
||||
unsigned int fifo_read_able(fifo_t fifo);
|
||||
unsigned int fifo_write_able(fifo_t fifo);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // !_FIFO_H_
|
||||
|
||||
|
@@ -2,11 +2,13 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-25 23:54:19
|
||||
* @LastEditTime : 2019-12-28 01:53:41
|
||||
* @LastEditTime: 2020-02-25 08:12:02
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#include "format.h"
|
||||
#include "salof_defconfig.h"
|
||||
|
||||
#ifdef USE_LOG
|
||||
|
||||
static int _get_atoi(const char **str)
|
||||
{
|
||||
@@ -280,9 +282,6 @@ int format_nstr(char *buf, unsigned int size, const char *fmt, va_list ap)
|
||||
return n;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
@@ -2,13 +2,12 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-25 23:54:38
|
||||
* @LastEditTime : 2019-12-26 20:10:28
|
||||
* @LastEditTime: 2020-02-25 07:18:09
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _FORMAT_H_
|
||||
#define _FORMAT_H_
|
||||
|
||||
#include "config.h"
|
||||
#include <stdarg.h>
|
||||
|
||||
#define FORMAT_BUF_LEN 12
|
||||
|
@@ -9,6 +9,8 @@
|
||||
|
||||
#include "salof.h"
|
||||
|
||||
#ifdef USE_LOG
|
||||
|
||||
#ifndef SALOF_BUFF_SIZE
|
||||
#define SALOF_BUFF_SIZE (1024U)
|
||||
#endif
|
||||
@@ -104,5 +106,5 @@ void salof_task(void *parm)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#ifndef _SALOF_H_
|
||||
#define _SALOF_H_
|
||||
|
||||
#include "salof_defconfig.h"
|
||||
#include "format.h"
|
||||
#include "fifo.h"
|
||||
#include <stdio.h>
|
||||
@@ -18,21 +19,19 @@ void salof(const char *fmt, ...);
|
||||
#define FC_DARK 36
|
||||
#define FC_WHITE 37
|
||||
|
||||
#if USE_LOG
|
||||
#ifdef USE_LOG
|
||||
|
||||
#if USE_SALOF
|
||||
#define PRINT_LOG salof
|
||||
#define PRINT_LOG salof
|
||||
#else
|
||||
|
||||
#if ((!USE_SALOF)&&(!PRINT_LOG))
|
||||
#define PRINT_LOG printf
|
||||
#define PRINT_LOG printf
|
||||
#endif
|
||||
|
||||
#ifndef PRINT_LOG
|
||||
#error "If the USE_LOG macro definition is turned on, you must define PRINT_LOG as the LOG output, such as #definePRINT_LOG printf"
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if LOG_TS || LOG_TAR
|
||||
|
||||
#endif
|
||||
|
||||
@@ -71,7 +70,7 @@ void salof(const char *fmt, ...);
|
||||
#define DEBUG_LEVEL (INFO_LEVEL + 1)
|
||||
|
||||
#ifndef LOG_LEVEL
|
||||
#define LOG_LEVEL WARN_LEVEL
|
||||
#define LOG_LEVEL DEBUG_LEVEL
|
||||
#endif
|
||||
|
||||
#if LOG_LEVEL < DEBUG_LEVEL
|
||||
|
14
components/connectivity/mqttclient/common/log/salof_config.h
Normal file
14
components/connectivity/mqttclient/common/log/salof_config.h
Normal file
@@ -0,0 +1,14 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2020-02-25 06:01:24
|
||||
* @LastEditTime: 2020-02-25 09:28:09
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
|
||||
#ifndef _SALOF_CONFIG_H_
|
||||
#define _SALOF_CONFIG_H_
|
||||
|
||||
#include "mqtt_config.h"
|
||||
|
||||
#endif /* _SALOF_CONFIG_H_ */
|
144
components/connectivity/mqttclient/common/log/salof_defconfig.h
Normal file
144
components/connectivity/mqttclient/common/log/salof_defconfig.h
Normal file
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-25 23:56:34
|
||||
* @LastEditTime: 2020-03-02 01:32:32
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _SALOF_DEFCONFIG_H_
|
||||
#define _SALOF_DEFCONFIG_H_
|
||||
|
||||
#include "salof_config.h"
|
||||
|
||||
#ifdef USE_LOG
|
||||
|
||||
#define USE_RTT 1
|
||||
#define USE_FREERTOS 2
|
||||
#define USE_TENCENTOS 3
|
||||
#define USE_LINUX 4
|
||||
|
||||
#define BASE_LEVEL (0)
|
||||
#define ASSERT_LEVEL (BASE_LEVEL + 1)
|
||||
#define ERR_LEVEL (ASSERT_LEVEL + 1)
|
||||
#define WARN_LEVEL (ERR_LEVEL + 1)
|
||||
#define INFO_LEVEL (WARN_LEVEL + 1)
|
||||
#define DEBUG_LEVEL (INFO_LEVEL + 1)
|
||||
|
||||
#ifndef USE_SALOF
|
||||
#define USE_SALOF (1U)
|
||||
#endif
|
||||
|
||||
#ifndef USE_IDLE_HOOK
|
||||
#define USE_IDLE_HOOK (0U)
|
||||
#endif
|
||||
|
||||
#ifndef LOG_COLOR
|
||||
#define LOG_COLOR (1U)
|
||||
#endif
|
||||
|
||||
#ifndef LOG_TS
|
||||
#define LOG_TS (1U)
|
||||
#endif
|
||||
|
||||
#ifndef LOG_TAR
|
||||
#define LOG_TAR (0U)
|
||||
#endif
|
||||
|
||||
#ifndef LOG_LEVEL
|
||||
#define LOG_LEVEL DEBUG_LEVEL //WARN_LEVEL DEBUG_LEVEL
|
||||
#endif
|
||||
|
||||
#if USE_SALOF
|
||||
|
||||
#ifndef SALOF_BUFF_SIZE
|
||||
#define SALOF_BUFF_SIZE (512U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_FIFO_SIZE
|
||||
#define SALOF_FIFO_SIZE (1024*4U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_TASK_STACK_SIZE
|
||||
#define SALOF_TASK_STACK_SIZE (2048U)
|
||||
#endif
|
||||
|
||||
#ifndef SALOF_TASK_TICK
|
||||
#define SALOF_TASK_TICK (20U)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
#if !defined(SALOF_OS)
|
||||
#error "SALOF_OS isn't defined in 'salof_config.h'"
|
||||
#endif
|
||||
|
||||
#if (SALOF_OS == USE_FREERTOS)
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
#include "semphr.h"
|
||||
#define salof_mutex SemaphoreHandle_t
|
||||
#define salof_tcb TaskHandle_t
|
||||
#define salof_sem salof_mutex
|
||||
#if USE_IDLE_HOOK
|
||||
#define salof_handler vApplicationIdleHook
|
||||
#endif
|
||||
#define SALOF_TASK_PRIO (0U)
|
||||
|
||||
#elif (SALOF_OS == USE_TENCENTOS)
|
||||
#include "tos_k.h"
|
||||
#define salof_mutex k_mutex_t*
|
||||
#define salof_sem k_sem_t*
|
||||
#define salof_tcb k_task_t*
|
||||
#define SALOF_TASK_PRIO (TOS_CFG_TASK_PRIO_MAX - 2u)
|
||||
#undef USE_IDLE_HOOK
|
||||
|
||||
#elif (SALOF_OS == USE_RTT)
|
||||
#include <rtconfig.h>
|
||||
#include <rtthread.h>
|
||||
#include <rthw.h>
|
||||
#include <stdio.h>
|
||||
#define salof_mutex rt_mutex_t
|
||||
#define salof_sem rt_sem_t
|
||||
#define salof_tcb rt_thread_t
|
||||
#define SALOF_TASK_PRIO (RT_THREAD_PRIORITY_MAX - 1)
|
||||
|
||||
#elif (SALOF_OS == USE_LINUX)
|
||||
#include "pthread.h"
|
||||
#include "memory.h"
|
||||
#include <semaphore.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#define salof_mutex pthread_mutex_t*
|
||||
#define salof_sem sem_t*
|
||||
#define salof_tcb pthread_t*
|
||||
#define SALOF_TASK_PRIO (0U)
|
||||
#undef USE_IDLE_HOOK
|
||||
|
||||
#else
|
||||
#error "not supported OS type"
|
||||
#endif
|
||||
|
||||
void *salof_alloc(unsigned int size);
|
||||
void salof_free(void *mem);
|
||||
salof_tcb salof_task_create(const char *name,
|
||||
void (*task_entry)(void *param),
|
||||
void * const param,
|
||||
unsigned int stack_size,
|
||||
unsigned int priority,
|
||||
unsigned int tick);
|
||||
salof_mutex salof_mutex_create(void);
|
||||
void salof_mutex_delete(salof_mutex mutex);
|
||||
int salof_mutex_pend(salof_mutex mutex, unsigned int timeout);
|
||||
int salof_mutex_post(salof_mutex mutex);
|
||||
salof_sem salof_sem_create(void);
|
||||
void salof_sem_delete(salof_sem sem);
|
||||
int salof_sem_pend(salof_sem sem, unsigned int timeout);
|
||||
int salof_sem_post(salof_sem sem);
|
||||
unsigned int salof_get_tick(void);
|
||||
char *salof_get_task_name(void);
|
||||
extern int send_buff(char *buf, int len);
|
||||
|
||||
#endif
|
||||
|
||||
#endif // !_SALOF_DEFCONFIG_H_
|
||||
|
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2020-02-25 03:36:09
|
||||
* @LastEditTime: 2020-02-25 07:16:43
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
|
||||
#ifndef _DEFCONFIG_H_
|
||||
#define _DEFCONFIG_H_
|
||||
|
||||
#include "mqtt_config.h"
|
||||
|
||||
#ifndef MQTT_MAX_PACKET_ID
|
||||
#define MQTT_MAX_PACKET_ID (0xFFFF - 1)
|
||||
#endif // !MQTT_MAX_PACKET_ID
|
||||
|
||||
#ifndef MQTT_TOPIC_LEN_MAX
|
||||
#define MQTT_TOPIC_LEN_MAX 64
|
||||
#endif // !MQTT_TOPIC_LEN_MAX
|
||||
|
||||
#ifndef MQTT_ACK_HANDLER_NUM_MAX
|
||||
#define MQTT_ACK_HANDLER_NUM_MAX 64
|
||||
#endif // !MQTT_ACK_HANDLER_NUM_MAX
|
||||
|
||||
#ifndef MQTT_DEFAULT_BUF_SIZE
|
||||
#define MQTT_DEFAULT_BUF_SIZE 1024
|
||||
#endif // !MQTT_DEFAULT_BUF_SIZE
|
||||
|
||||
#ifndef MQTT_DEFAULT_CMD_TIMEOUT
|
||||
#define MQTT_DEFAULT_CMD_TIMEOUT 4000
|
||||
#endif // !MQTT_DEFAULT_CMD_TIMEOUT
|
||||
|
||||
#ifndef MQTT_MAX_CMD_TIMEOUT
|
||||
#define MQTT_MAX_CMD_TIMEOUT 20000
|
||||
#endif // !MQTT_MAX_CMD_TIMEOUT
|
||||
|
||||
#ifndef MQTT_MIN_CMD_TIMEOUT
|
||||
#define MQTT_MIN_CMD_TIMEOUT 1000
|
||||
#endif // !MQTT_MIN_CMD_TIMEOUT
|
||||
|
||||
#ifndef MQTT_KEEP_ALIVE_INTERVAL
|
||||
#define MQTT_KEEP_ALIVE_INTERVAL 100 // unit: second
|
||||
#endif // !MQTT_KEEP_ALIVE_INTERVAL
|
||||
|
||||
#ifndef MQTT_VERSION
|
||||
#define MQTT_VERSION 4 // 4 is mqtt 3.1.1
|
||||
#endif // !MQTT_VERSION
|
||||
|
||||
#ifndef MQTT_RECONNECT_DEFAULT_DURATION
|
||||
#define MQTT_RECONNECT_DEFAULT_DURATION 1000
|
||||
#endif // !MQTT_RECONNECT_DEFAULT_DURATION
|
||||
|
||||
#ifndef MQTT_THREAD_STACK_SIZE
|
||||
#define MQTT_THREAD_STACK_SIZE 4096
|
||||
#endif // !MQTT_THREAD_STACK_SIZE
|
||||
|
||||
#ifndef MQTT_THREAD_PRIO
|
||||
#define MQTT_THREAD_PRIO 5
|
||||
#endif // !MQTT_THREAD_PRIO
|
||||
|
||||
#ifndef MQTT_THREAD_TICK
|
||||
#define MQTT_THREAD_TICK 50
|
||||
#endif // !MQTT_THREAD_TICK
|
||||
|
||||
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
|
||||
#ifndef MQTT_TLS_HANDSHAKE_TIMEOUT
|
||||
#define MQTT_TLS_HANDSHAKE_TIMEOUT (5 * 1000)
|
||||
#endif // !MQTT_TLS_HANDSHAKE_TIMEOUT
|
||||
|
||||
#include "mbedtls/ssl.h"
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/net_sockets.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/debug.h"
|
||||
#endif /* MQTT_NETWORK_TYPE_TLS */
|
||||
|
||||
#endif /* _DEFCONFIG_H_ */
|
@@ -2,7 +2,7 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-09 21:31:25
|
||||
* @LastEditTime : 2020-02-16 02:50:32
|
||||
* @LastEditTime: 2020-03-15 01:33:01
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#include "mqttclient.h"
|
||||
@@ -28,22 +28,35 @@ static void mqtt_set_client_state(mqtt_client_t* c, client_state_t state)
|
||||
platform_mutex_unlock(&c->global_lock);
|
||||
}
|
||||
|
||||
static int mqtt_is_connected(mqtt_client_t* c)
|
||||
{
|
||||
client_state_t state;
|
||||
|
||||
state = mqtt_get_client_state(c);
|
||||
if (CLIENT_STATE_CLEAN_SESSION == state) {
|
||||
RETURN_ERROR(MQTT_CLEAN_SESSION_ERROR);
|
||||
} else if (CLIENT_STATE_CONNECTED != state) {
|
||||
RETURN_ERROR(MQTT_NOT_CONNECT_ERROR);
|
||||
}
|
||||
RETURN_ERROR(MQTT_SUCCESS_ERROR);
|
||||
}
|
||||
|
||||
static int mqtt_set_publish_dup(mqtt_client_t* c, unsigned char dup)
|
||||
{
|
||||
unsigned char *read_data = c->write_buf;
|
||||
unsigned char *write_data = c->write_buf;
|
||||
MQTTHeader header = {0};
|
||||
MQTTHeader header = {0};
|
||||
|
||||
if (NULL == c->write_buf)
|
||||
RETURN_ERROR(MQTT_SET_PUBLISH_DUP_FAILED);
|
||||
|
||||
header.byte = readChar(&read_data); /* read header */
|
||||
header.byte = readChar(&read_data); /* read header */
|
||||
|
||||
if (header.bits.type != PUBLISH)
|
||||
RETURN_ERROR(MQTT_SET_PUBLISH_DUP_FAILED);
|
||||
|
||||
header.bits.dup = dup;
|
||||
writeChar(&write_data, header.byte); /* write header */
|
||||
header.bits.dup = dup;
|
||||
writeChar(&write_data, header.byte); /* write header */
|
||||
|
||||
RETURN_ERROR(MQTT_SUCCESS_ERROR);
|
||||
}
|
||||
@@ -98,10 +111,10 @@ static int mqtt_decode_packet(mqtt_client_t* c, int* value, int timeout)
|
||||
rc = MQTTPACKET_READ_ERROR; /* bad data */
|
||||
goto exit;
|
||||
}
|
||||
rc = c->network->read(c->network, &i, 1, timeout);
|
||||
rc = c->network->read(c->network, &i, 1, timeout); /* read network data */
|
||||
if (rc != 1)
|
||||
goto exit;
|
||||
*value += (i & 127) * multiplier;
|
||||
*value += (i & 127) * multiplier; /* decode data length according to mqtt protocol */
|
||||
multiplier *= 128;
|
||||
} while ((i & 128) != 0);
|
||||
exit:
|
||||
@@ -128,7 +141,7 @@ static void mqtt_packet_drain(mqtt_client_t* c, platform_timer_t *timer, int pac
|
||||
bytes2read = packet_len - total_bytes_read;
|
||||
}
|
||||
}
|
||||
} while ((total_bytes_read < packet_len) && (0 != read_len));
|
||||
} while ((total_bytes_read < packet_len) && (0 != read_len)); /* read and discard all corrupted data */
|
||||
}
|
||||
|
||||
static int mqtt_read_packet(mqtt_client_t* c, int* packet_type, platform_timer_t* timer)
|
||||
@@ -141,6 +154,9 @@ static int mqtt_read_packet(mqtt_client_t* c, int* packet_type, platform_timer_t
|
||||
if (NULL == packet_type)
|
||||
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
|
||||
|
||||
platform_timer_init(timer);
|
||||
platform_timer_cutdown(timer, c->cmd_timeout);
|
||||
|
||||
/* 1. read the header byte. This has the packet type in it */
|
||||
rc = c->network->read(c->network, c->read_buf, len, platform_timer_remain(timer));
|
||||
if (rc != len)
|
||||
@@ -153,8 +169,11 @@ static int mqtt_read_packet(mqtt_client_t* c, int* packet_type, platform_timer_t
|
||||
len += MQTTPacket_encode(c->read_buf + len, remain_len);
|
||||
|
||||
if ((len + remain_len) > c->read_buf_size) {
|
||||
|
||||
/* mqtt buffer is too short, read and discard all corrupted data */
|
||||
mqtt_packet_drain(c, timer, remain_len);
|
||||
RETURN_ERROR(MQTT_BUFFER_TOO_SHORT_ERROR);
|
||||
|
||||
RETURN_ERROR(MQTT_BUFFER_TOO_SHORT_ERROR);
|
||||
}
|
||||
|
||||
/* 3. read the rest of the buffer using a callback to supply the rest of the data */
|
||||
@@ -174,9 +193,13 @@ static int mqtt_send_packet(mqtt_client_t* c, int length, platform_timer_t* time
|
||||
int len = 0;
|
||||
int sent = 0;
|
||||
|
||||
platform_timer_init(timer);
|
||||
platform_timer_cutdown(timer, c->cmd_timeout);
|
||||
|
||||
/* send mqtt packet in a blocking manner or exit when it timer is expired */
|
||||
while ((sent < length) && (!platform_timer_is_expired(timer))) {
|
||||
len = c->network->write(c->network, &c->write_buf[sent], length, platform_timer_remain(timer));
|
||||
if (len < 0) // there was an error writing the data
|
||||
if (len <= 0) // there was an error writing the data
|
||||
break;
|
||||
sent += len;
|
||||
}
|
||||
@@ -215,8 +238,11 @@ static char mqtt_topic_is_matched(char* topic_filter, MQTTString* topic_name)
|
||||
{
|
||||
if (*curn == '/' && *curf != '/')
|
||||
break;
|
||||
if (*curf != '+' && *curf != '#' && *curf != *curn)
|
||||
|
||||
/* support wildcards for MQTT topics, such as '#' '+' */
|
||||
if (*curf != '+' && *curf != '#' && *curf != *curn)
|
||||
break;
|
||||
|
||||
if (*curf == '+') {
|
||||
char* nextpos = curn + 1;
|
||||
while (nextpos < curn_end && *nextpos != '/')
|
||||
@@ -236,7 +262,7 @@ static void mqtt_new_message_data(message_data_t* md, MQTTString* topic_name, mq
|
||||
int len;
|
||||
len = (topic_name->lenstring.len < MQTT_TOPIC_LEN_MAX - 1) ? topic_name->lenstring.len : MQTT_TOPIC_LEN_MAX - 1;
|
||||
memcpy(md->topic_name, topic_name->lenstring.data, len);
|
||||
md->topic_name[len] = '\0';
|
||||
md->topic_name[len] = '\0'; /* the topic name is too long and will be truncated */
|
||||
md->message = message;
|
||||
}
|
||||
|
||||
@@ -245,9 +271,11 @@ static message_handlers_t *mqtt_get_msg_handler(mqtt_client_t* c, MQTTString* to
|
||||
list_t *curr, *next;
|
||||
message_handlers_t *msg_handler;
|
||||
|
||||
/* traverse the msg_handler_list to find the matching message handler */
|
||||
LIST_FOR_EACH_SAFE(curr, next, &c->msg_handler_list) {
|
||||
msg_handler = LIST_ENTRY(curr, message_handlers_t, list);
|
||||
|
||||
/* judge topic is equal or match, support wildcard, such as '#' '+' */
|
||||
if ((NULL != msg_handler->topic_filter) && ((MQTTPacket_equals(topic_name, (char*)msg_handler->topic_filter)) ||
|
||||
(mqtt_topic_is_matched((char*)msg_handler->topic_filter, topic_name)))) {
|
||||
return msg_handler;
|
||||
@@ -261,12 +289,13 @@ static int mqtt_deliver_message(mqtt_client_t* c, MQTTString* topic_name, mqtt_m
|
||||
int rc = MQTT_FAILED_ERROR;
|
||||
message_handlers_t *msg_handler;
|
||||
|
||||
/* get mqtt message handler */
|
||||
msg_handler = mqtt_get_msg_handler(c, topic_name);
|
||||
|
||||
if (NULL != msg_handler) {
|
||||
message_data_t md;
|
||||
mqtt_new_message_data(&md, topic_name, message);
|
||||
msg_handler->handler(c, &md);
|
||||
mqtt_new_message_data(&md, topic_name, message); /* make a message data */
|
||||
msg_handler->handler(c, &md); /* deliver the message */
|
||||
rc = MQTT_SUCCESS_ERROR;
|
||||
} else
|
||||
goto exit;
|
||||
@@ -288,14 +317,14 @@ static ack_handlers_t *mqtt_ack_handler_create(mqtt_client_t* c, int type, unsig
|
||||
|
||||
list_init(&ack_handler->list);
|
||||
platform_timer_init(&ack_handler->timer);
|
||||
platform_timer_cutdown(&ack_handler->timer, c->cmd_timeout);
|
||||
platform_timer_cutdown(&ack_handler->timer, c->cmd_timeout); /* No response within timeout will be destroyed or resent */
|
||||
|
||||
ack_handler->type = type;
|
||||
ack_handler->packet_id = packet_id;
|
||||
ack_handler->payload_len = payload_len;
|
||||
ack_handler->payload = (unsigned char *)ack_handler + sizeof(ack_handlers_t);
|
||||
ack_handler->handler = handler;
|
||||
memcpy(ack_handler->payload, c->write_buf, payload_len);
|
||||
memcpy(ack_handler->payload, c->write_buf, payload_len); /* save the data in ack handler*/
|
||||
|
||||
return ack_handler;
|
||||
}
|
||||
@@ -304,7 +333,7 @@ static void mqtt_ack_handler_destroy(ack_handlers_t* ack_handler)
|
||||
{
|
||||
if (NULL != &ack_handler->list) {
|
||||
list_del(&ack_handler->list);
|
||||
platform_memory_free(ack_handler);
|
||||
platform_memory_free(ack_handler); /* delete ack handler from the list, and free memory */
|
||||
}
|
||||
}
|
||||
|
||||
@@ -313,13 +342,13 @@ static void mqtt_ack_handler_resend(mqtt_client_t* c, ack_handlers_t* ack_handle
|
||||
platform_timer_t timer;
|
||||
platform_timer_init(&timer);
|
||||
platform_timer_cutdown(&timer, c->cmd_timeout);
|
||||
platform_timer_cutdown(&ack_handler->timer, c->cmd_timeout);
|
||||
platform_timer_cutdown(&ack_handler->timer, c->cmd_timeout); /* timeout, recutdown */
|
||||
|
||||
platform_mutex_lock(&c->write_lock);
|
||||
memcpy(c->write_buf, ack_handler->payload, ack_handler->payload_len);
|
||||
memcpy(c->write_buf, ack_handler->payload, ack_handler->payload_len); /* copy data to write buf form ack handler */
|
||||
|
||||
mqtt_send_packet(c, ack_handler->payload_len, &timer);
|
||||
LOG_E("%s:%d %s()... resend %d package, packet_id is %d ", __FILE__, __LINE__, __FUNCTION__, ack_handler->type, ack_handler->packet_id);
|
||||
mqtt_send_packet(c, ack_handler->payload_len, &timer); /* resend data */
|
||||
LOG_W("%s:%d %s()... resend %d package, packet_id is %d ", __FILE__, __LINE__, __FUNCTION__, ack_handler->type, ack_handler->packet_id);
|
||||
platform_mutex_unlock(&c->write_lock);
|
||||
}
|
||||
|
||||
@@ -334,7 +363,9 @@ static int mqtt_ack_list_node_is_exist(mqtt_client_t* c, int type, unsigned shor
|
||||
LIST_FOR_EACH_SAFE(curr, next, &c->ack_handler_list) {
|
||||
ack_handler = LIST_ENTRY(curr, ack_handlers_t, list);
|
||||
|
||||
if ((packet_id == ack_handler->packet_id) && (type == ack_handler->type))
|
||||
/* For mqtt packets of qos1 and qos2, you can use the packet id and type as the unique
|
||||
identifier to determine whether the node already exists and avoid repeated addition. */
|
||||
if ((packet_id == ack_handler->packet_id) && (type == ack_handler->type))
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -346,9 +377,11 @@ static int mqtt_ack_list_record(mqtt_client_t* c, int type, unsigned short packe
|
||||
int rc = MQTT_SUCCESS_ERROR;
|
||||
ack_handlers_t *ack_handler = NULL;
|
||||
|
||||
/* Determine if the node already exists */
|
||||
if (mqtt_ack_list_node_is_exist(c, type, packet_id))
|
||||
RETURN_ERROR(MQTT_ACK_NODE_IS_EXIST);
|
||||
|
||||
/* create a ack handler node */
|
||||
ack_handler = mqtt_ack_handler_create(c, type, packet_id, payload_len, handler);
|
||||
if (NULL == ack_handler)
|
||||
RETURN_ERROR(MQTT_MEM_NOT_ENOUGH_ERROR);
|
||||
@@ -377,6 +410,7 @@ static int mqtt_ack_list_unrecord(mqtt_client_t* c, int type, unsigned short pac
|
||||
if (handler)
|
||||
*handler = ack_handler->handler;
|
||||
|
||||
/* destroy a ack handler node */
|
||||
mqtt_ack_handler_destroy(ack_handler);
|
||||
mqtt_subtract_ack_handler_num(c);
|
||||
}
|
||||
@@ -395,13 +429,13 @@ static message_handlers_t *mqtt_msg_handler_create(const char* topic_filter, mqt
|
||||
list_init(&msg_handler->list);
|
||||
|
||||
msg_handler->qos = qos;
|
||||
msg_handler->handler = handler;
|
||||
msg_handler->handler = handler; /* register callback handler */
|
||||
msg_handler->topic_filter = topic_filter;
|
||||
|
||||
return msg_handler;
|
||||
}
|
||||
|
||||
static void mqtt_msg_handler_destroy(message_handlers_t *msg_handler)
|
||||
static void mqtt_msg_handler_destory(message_handlers_t *msg_handler)
|
||||
{
|
||||
if (NULL != &msg_handler->list) {
|
||||
list_del(&msg_handler->list);
|
||||
@@ -423,6 +457,7 @@ static int mqtt_msg_handler_is_exist(mqtt_client_t* c, message_handlers_t *handl
|
||||
LIST_FOR_EACH_SAFE(curr, next, &c->msg_handler_list) {
|
||||
msg_handler = LIST_ENTRY(curr, message_handlers_t, list);
|
||||
|
||||
/* determine whether a node already exists by mqtt topic, but wildcards are not supported */
|
||||
if ((NULL != msg_handler->topic_filter) && (mqtt_is_topic_equals(msg_handler->topic_filter, handler->topic_filter))) {
|
||||
LOG_W("%s:%d %s()...msg_handler->topic_filter: %s, handler->topic_filter: %s", __FILE__, __LINE__, __FUNCTION__, msg_handler->topic_filter, handler->topic_filter);
|
||||
return 1;
|
||||
@@ -435,10 +470,11 @@ static int mqtt_msg_handler_is_exist(mqtt_client_t* c, message_handlers_t *handl
|
||||
static int mqtt_msg_handlers_install(mqtt_client_t* c, message_handlers_t *handler)
|
||||
{
|
||||
if (mqtt_msg_handler_is_exist(c, handler)) {
|
||||
mqtt_msg_handler_destroy(handler);
|
||||
mqtt_msg_handler_destory(handler);
|
||||
RETURN_ERROR(MQTT_SUCCESS_ERROR);
|
||||
}
|
||||
|
||||
/* install to msg_handler_list*/
|
||||
list_add_tail(&handler->list, &c->msg_handler_list);
|
||||
|
||||
RETURN_ERROR(MQTT_SUCCESS_ERROR);
|
||||
@@ -451,6 +487,7 @@ static void mqtt_clean_session(mqtt_client_t* c)
|
||||
ack_handlers_t *ack_handler;
|
||||
message_handlers_t *msg_handler;
|
||||
|
||||
/* release all ack_handler_list memory */
|
||||
if (!(list_is_empty(&c->ack_handler_list))) {
|
||||
LIST_FOR_EACH_SAFE(curr, next, &c->ack_handler_list) {
|
||||
ack_handler = LIST_ENTRY(curr, ack_handlers_t, list);
|
||||
@@ -459,6 +496,7 @@ static void mqtt_clean_session(mqtt_client_t* c)
|
||||
list_del_init(&c->ack_handler_list);
|
||||
}
|
||||
|
||||
/* release all msg_handler_list memory */
|
||||
if (!(list_is_empty(&c->msg_handler_list))) {
|
||||
LIST_FOR_EACH_SAFE(curr, next, &c->msg_handler_list) {
|
||||
msg_handler = LIST_ENTRY(curr, message_handlers_t, list);
|
||||
@@ -467,6 +505,8 @@ static void mqtt_clean_session(mqtt_client_t* c)
|
||||
}
|
||||
list_del_init(&c->msg_handler_list);
|
||||
}
|
||||
|
||||
mqtt_set_client_state(c, CLIENT_STATE_INVALID);
|
||||
}
|
||||
|
||||
static void mqtt_ack_list_scan(mqtt_client_t* c)
|
||||
@@ -484,6 +524,8 @@ static void mqtt_ack_list_scan(mqtt_client_t* c)
|
||||
continue;
|
||||
|
||||
if ((ack_handler->type == PUBACK) || (ack_handler->type == PUBREC) || (ack_handler->type == PUBREL) || (ack_handler->type == PUBCOMP)) {
|
||||
|
||||
/* timeout has occurred. for qos1 and qos2 packets, you need to resend them. */
|
||||
mqtt_ack_handler_resend(c, ack_handler);
|
||||
continue;
|
||||
}
|
||||
@@ -505,6 +547,7 @@ static int mqtt_try_resubscribe(mqtt_client_t* c)
|
||||
LIST_FOR_EACH_SAFE(curr, next, &c->msg_handler_list) {
|
||||
msg_handler = LIST_ENTRY(curr, message_handlers_t, list);
|
||||
|
||||
/* resubscribe topic */
|
||||
if ((rc = mqtt_subscribe(c, msg_handler->topic_filter, msg_handler->qos, msg_handler->handler)) == MQTT_ACK_HANDLER_NUM_TOO_MUCH)
|
||||
LOG_W("%s:%d %s()... mqtt ack handler num too much ...", __FILE__, __LINE__, __FUNCTION__);
|
||||
|
||||
@@ -518,10 +561,10 @@ static int mqtt_try_do_reconnect(mqtt_client_t* c)
|
||||
int rc = MQTT_CONNECT_FAILED_ERROR;
|
||||
|
||||
if (CLIENT_STATE_CONNECTED != mqtt_get_client_state(c))
|
||||
rc = mqtt_connect(c);
|
||||
rc = mqtt_connect(c); /* reconnect */
|
||||
|
||||
if (MQTT_SUCCESS_ERROR == rc) {
|
||||
rc = mqtt_try_resubscribe(c);
|
||||
rc = mqtt_try_resubscribe(c); /* resubscribe */
|
||||
}
|
||||
|
||||
LOG_I("%s:%d %s()... mqtt try connect result is %#x", __FILE__, __LINE__, __FUNCTION__, rc);
|
||||
@@ -556,15 +599,15 @@ static int mqtt_publish_ack_packet(mqtt_client_t *c, unsigned short packet_id, i
|
||||
platform_mutex_lock(&c->write_lock);
|
||||
|
||||
switch (packet_type) {
|
||||
case PUBREC:
|
||||
len = MQTTSerialize_ack(c->write_buf, c->write_buf_size, PUBREL, 0, packet_id);
|
||||
rc = mqtt_ack_list_record(c, PUBCOMP, packet_id, len, NULL);
|
||||
case PUBREC:
|
||||
len = MQTTSerialize_ack(c->write_buf, c->write_buf_size, PUBREL, 0, packet_id); /* make a PUBREL ack packet */
|
||||
rc = mqtt_ack_list_record(c, PUBCOMP, packet_id, len, NULL); /* record ack, expect to receive PUBCOMP*/
|
||||
if (MQTT_SUCCESS_ERROR != rc)
|
||||
goto exit;
|
||||
break;
|
||||
|
||||
case PUBREL:
|
||||
len = MQTTSerialize_ack(c->write_buf, c->write_buf_size, PUBCOMP, 0, packet_id);
|
||||
len = MQTTSerialize_ack(c->write_buf, c->write_buf_size, PUBCOMP, 0, packet_id); /* make a PUBCOMP ack packet */
|
||||
break;
|
||||
|
||||
default:
|
||||
@@ -591,11 +634,15 @@ static int mqtt_puback_and_pubcomp_packet_handle(mqtt_client_t *c, platform_time
|
||||
unsigned short packet_id;
|
||||
unsigned char dup, packet_type;
|
||||
|
||||
rc = mqtt_is_connected(c);
|
||||
if (MQTT_SUCCESS_ERROR != rc)
|
||||
RETURN_ERROR(rc);
|
||||
|
||||
if (MQTTDeserialize_ack(&packet_type, &dup, &packet_id, c->read_buf, c->read_buf_size) != 1)
|
||||
rc = MQTT_PUBREC_PACKET_ERROR;
|
||||
|
||||
(void) dup;
|
||||
rc = mqtt_ack_list_unrecord(c, packet_type, packet_id, NULL);
|
||||
rc = mqtt_ack_list_unrecord(c, packet_type, packet_id, NULL); /* unrecord ack handler */
|
||||
|
||||
RETURN_ERROR(rc);
|
||||
}
|
||||
@@ -608,7 +655,12 @@ static int mqtt_suback_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
unsigned short packet_id;
|
||||
int is_nack = 0;
|
||||
message_handlers_t *msg_handler = NULL;
|
||||
|
||||
|
||||
rc = mqtt_is_connected(c);
|
||||
if (MQTT_SUCCESS_ERROR != rc)
|
||||
RETURN_ERROR(rc);
|
||||
|
||||
/* deserialize subscribe ack packet */
|
||||
if (MQTTDeserialize_suback(&packet_id, 1, &count, (int*)&granted_qos, c->read_buf, c->read_buf_size) != 1)
|
||||
RETURN_ERROR(MQTT_SUBSCRIBE_ACK_PACKET_ERROR);
|
||||
|
||||
@@ -620,7 +672,7 @@ static int mqtt_suback_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
RETURN_ERROR(MQTT_MEM_NOT_ENOUGH_ERROR);
|
||||
|
||||
if (is_nack) {
|
||||
mqtt_msg_handler_destroy(msg_handler);
|
||||
mqtt_msg_handler_destory(msg_handler); /* subscribe topic failed, destory message handler */
|
||||
RETURN_ERROR(MQTT_SUBSCRIBE_NOT_ACK_ERROR);
|
||||
}
|
||||
|
||||
@@ -634,16 +686,20 @@ static int mqtt_unsuback_packet_handle(mqtt_client_t *c, platform_timer_t *timer
|
||||
int rc = MQTT_FAILED_ERROR;
|
||||
message_handlers_t *msg_handler;
|
||||
unsigned short packet_id = 0;
|
||||
|
||||
rc = mqtt_is_connected(c);
|
||||
if (MQTT_SUCCESS_ERROR != rc)
|
||||
RETURN_ERROR(rc);
|
||||
|
||||
if (MQTTDeserialize_unsuback(&packet_id, c->read_buf, c->read_buf_size) != 1)
|
||||
RETURN_ERROR(MQTT_UNSUBSCRIBE_ACK_PACKET_ERROR);
|
||||
|
||||
rc = mqtt_ack_list_unrecord(c, UNSUBACK, packet_id, &msg_handler);
|
||||
rc = mqtt_ack_list_unrecord(c, UNSUBACK, packet_id, &msg_handler); /* unrecord ack handler, and get message handler */
|
||||
|
||||
if (!msg_handler)
|
||||
RETURN_ERROR(MQTT_MEM_NOT_ENOUGH_ERROR);
|
||||
|
||||
mqtt_msg_handler_destroy(msg_handler);
|
||||
mqtt_msg_handler_destory(msg_handler); /* destory message handler */
|
||||
|
||||
RETURN_ERROR(rc);
|
||||
}
|
||||
@@ -655,6 +711,10 @@ static int mqtt_publish_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
mqtt_message_t msg;
|
||||
int qos;
|
||||
msg.payloadlen = 0;
|
||||
|
||||
rc = mqtt_is_connected(c);
|
||||
if (MQTT_SUCCESS_ERROR != rc)
|
||||
RETURN_ERROR(rc);
|
||||
|
||||
if (MQTTDeserialize_publish(&msg.dup, &qos, &msg.retained, &msg.id, &topic_name,
|
||||
(unsigned char**)&msg.payload, (int*)&msg.payloadlen, c->read_buf, c->read_buf_size) != 1)
|
||||
@@ -662,6 +722,7 @@ static int mqtt_publish_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
|
||||
msg.qos = (mqtt_qos_t)qos;
|
||||
|
||||
/* for qos1 and qos2, you need to send a ack packet */
|
||||
if (msg.qos != QOS0) {
|
||||
platform_mutex_lock(&c->write_lock);
|
||||
|
||||
@@ -684,6 +745,7 @@ static int mqtt_publish_packet_handle(mqtt_client_t *c, platform_timer_t *timer)
|
||||
if (msg.qos != QOS2)
|
||||
mqtt_deliver_message(c, &topic_name, &msg);
|
||||
else {
|
||||
/* record the received of a qos2 message and only processes it when the qos2 message is received for the first time */
|
||||
if ((rc = mqtt_ack_list_record(c, PUBREL, msg.id, len, NULL)) != MQTT_ACK_NODE_IS_EXIST)
|
||||
mqtt_deliver_message(c, &topic_name, &msg);
|
||||
}
|
||||
@@ -697,12 +759,16 @@ static int mqtt_pubrec_and_pubrel_packet_handle(mqtt_client_t *c, platform_timer
|
||||
int rc = MQTT_FAILED_ERROR;
|
||||
unsigned short packet_id;
|
||||
unsigned char dup, packet_type;
|
||||
|
||||
rc = mqtt_is_connected(c);
|
||||
if (MQTT_SUCCESS_ERROR != rc)
|
||||
RETURN_ERROR(rc);
|
||||
|
||||
if (MQTTDeserialize_ack(&packet_type, &dup, &packet_id, c->read_buf, c->read_buf_size) != 1)
|
||||
RETURN_ERROR(MQTT_PUBREC_PACKET_ERROR);
|
||||
|
||||
(void) dup;
|
||||
rc = mqtt_publish_ack_packet(c, packet_id, packet_type);
|
||||
rc = mqtt_publish_ack_packet(c, packet_id, packet_type); /* make a ack packet and send it */
|
||||
rc = mqtt_ack_list_unrecord(c, packet_type, packet_id, NULL);
|
||||
|
||||
RETURN_ERROR(rc);
|
||||
@@ -710,21 +776,17 @@ static int mqtt_pubrec_and_pubrel_packet_handle(mqtt_client_t *c, platform_timer
|
||||
|
||||
static int mqtt_packet_handle(mqtt_client_t* c, platform_timer_t* timer)
|
||||
{
|
||||
|
||||
int rc = MQTT_SUCCESS_ERROR;
|
||||
int packet_type = 0;
|
||||
|
||||
rc = mqtt_read_packet(c, &packet_type, timer);
|
||||
|
||||
platform_timer_init(timer);
|
||||
platform_timer_cutdown(timer, c->cmd_timeout);
|
||||
|
||||
switch (packet_type) {
|
||||
case 0: /* timed out reading packet */
|
||||
break;
|
||||
|
||||
case CONNACK:
|
||||
break;
|
||||
case CONNACK: /* has been processed */
|
||||
goto exit;
|
||||
|
||||
case PUBACK:
|
||||
case PUBCOMP:
|
||||
@@ -749,19 +811,15 @@ static int mqtt_packet_handle(mqtt_client_t* c, platform_timer_t* timer)
|
||||
break;
|
||||
|
||||
case PINGRESP:
|
||||
c->ping_outstanding = 0;
|
||||
c->ping_outstanding = 0; /* keep alive ping success */
|
||||
break;
|
||||
|
||||
default:
|
||||
goto exit;
|
||||
}
|
||||
|
||||
if (mqtt_keep_alive(c) != MQTT_SUCCESS_ERROR) {
|
||||
mqtt_set_client_state(c, CLIENT_STATE_DISCONNECTED);
|
||||
rc = MQTT_NOT_CONNECT_ERROR;
|
||||
}
|
||||
|
||||
|
||||
rc = mqtt_keep_alive(c);
|
||||
|
||||
exit:
|
||||
if (rc == MQTT_SUCCESS_ERROR)
|
||||
rc = packet_type;
|
||||
@@ -785,11 +843,19 @@ static int mqtt_wait_packet(mqtt_client_t* c, int packet_type, platform_timer_t*
|
||||
static void mqtt_yield_thread(void *arg)
|
||||
{
|
||||
int rc;
|
||||
client_state_t state;
|
||||
mqtt_client_t *c = (mqtt_client_t *)arg;
|
||||
|
||||
state = mqtt_get_client_state(c);
|
||||
if (CLIENT_STATE_CONNECTED != state) {
|
||||
LOG_W("%s:%d %s()..., mqtt is not connected to the server...", __FILE__, __LINE__, __FUNCTION__);
|
||||
platform_thread_stop(c->thread); /* mqtt is not connected to the server, stop thread */
|
||||
}
|
||||
|
||||
while (1) {
|
||||
rc = mqtt_yield(c, c->cmd_timeout);
|
||||
if (MQTT_CLOSE_SESSION_ERROR == rc) {
|
||||
LOG_E("%s:%d %s()..., mqtt close session....", __FILE__, __LINE__, __FUNCTION__);
|
||||
if (MQTT_CLEAN_SESSION_ERROR == rc) {
|
||||
LOG_E("%s:%d %s()..., mqtt clean session....", __FILE__, __LINE__, __FUNCTION__);
|
||||
c->network->disconnect(c->network);
|
||||
mqtt_clean_session(c);
|
||||
goto exit;
|
||||
@@ -836,8 +902,11 @@ static int mqtt_connect_with_results(mqtt_client_t* c)
|
||||
|
||||
platform_mutex_lock(&c->write_lock);
|
||||
|
||||
/* serialize connect packet */
|
||||
if ((len = MQTTSerialize_connect(c->write_buf, c->write_buf_size, &connect_data)) <= 0)
|
||||
goto exit;
|
||||
|
||||
/* send connect packet */
|
||||
if ((rc = mqtt_send_packet(c, len, &connect_timer)) != MQTT_SUCCESS_ERROR)
|
||||
goto exit;
|
||||
|
||||
@@ -851,12 +920,24 @@ static int mqtt_connect_with_results(mqtt_client_t* c)
|
||||
|
||||
exit:
|
||||
if (rc == MQTT_SUCCESS_ERROR) {
|
||||
if(NULL ==c->thread)
|
||||
if(NULL == c->thread) {
|
||||
|
||||
/* connect success, and need init mqtt thread */
|
||||
c->thread= platform_thread_init("mqtt_yield_thread", mqtt_yield_thread, c, MQTT_THREAD_STACK_SIZE, MQTT_THREAD_PRIO, MQTT_THREAD_TICK);
|
||||
c->ping_outstanding = 0;
|
||||
mqtt_set_client_state(c, CLIENT_STATE_CONNECTED);
|
||||
|
||||
if (NULL != c->thread) {
|
||||
mqtt_set_client_state(c, CLIENT_STATE_CONNECTED);
|
||||
platform_thread_startup(c->thread);
|
||||
platform_thread_start(c->thread); /* start run mqtt thread */
|
||||
}
|
||||
} else {
|
||||
mqtt_set_client_state(c, CLIENT_STATE_CONNECTED); /* reconnect, mqtt thread is already exists */
|
||||
}
|
||||
|
||||
c->ping_outstanding = 0; /* reset ping outstanding */
|
||||
|
||||
} else {
|
||||
mqtt_set_client_state(c, CLIENT_STATE_INITIALIZED);
|
||||
mqtt_set_client_state(c, CLIENT_STATE_INITIALIZED); /* connect failed */
|
||||
}
|
||||
|
||||
platform_mutex_unlock(&c->write_lock);
|
||||
@@ -869,15 +950,18 @@ exit:
|
||||
int mqtt_keep_alive(mqtt_client_t* c)
|
||||
{
|
||||
int rc = MQTT_SUCCESS_ERROR;
|
||||
|
||||
rc = mqtt_is_connected(c);
|
||||
if (MQTT_SUCCESS_ERROR != rc)
|
||||
RETURN_ERROR(rc);
|
||||
|
||||
if (platform_timer_is_expired(&c->last_sent) || platform_timer_is_expired(&c->last_received)) {
|
||||
if (c->ping_outstanding) {
|
||||
LOG_W("%s:%d %s()... ping outstanding", __FILE__, __LINE__, __FUNCTION__);
|
||||
rc = MQTT_FAILED_ERROR; /* PINGRESP not received in keepalive interval */
|
||||
mqtt_set_client_state(c, CLIENT_STATE_DISCONNECTED);
|
||||
rc = MQTT_NOT_CONNECT_ERROR; /* PINGRESP not received in keepalive interval */
|
||||
} else {
|
||||
platform_timer_t timer;
|
||||
platform_timer_init(&timer);
|
||||
platform_timer_cutdown(&timer, c->cmd_timeout);
|
||||
int len = MQTTSerialize_pingreq(c->write_buf, c->write_buf_size);
|
||||
if (len > 0 && (rc = mqtt_send_packet(c, len, &timer)) == MQTT_SUCCESS_ERROR) // send the ping packet
|
||||
c->ping_outstanding++;
|
||||
@@ -903,14 +987,15 @@ int mqtt_init(mqtt_client_t* c, client_init_params_t* init)
|
||||
}
|
||||
memset(c->network, 0, sizeof(network_t));
|
||||
|
||||
if ((MQTT_MIN_PAYLOAD_SIZE <= init->read_buf_size) || (MQTT_MAX_PAYLOAD_SIZE >= init->read_buf_size))
|
||||
if ((MQTT_MIN_PAYLOAD_SIZE >= init->read_buf_size) || (MQTT_MAX_PAYLOAD_SIZE <= init->read_buf_size))
|
||||
init->read_buf_size = MQTT_DEFAULT_BUF_SIZE;
|
||||
if ((MQTT_MIN_PAYLOAD_SIZE <= init->write_buf_size) || (MQTT_MAX_PAYLOAD_SIZE >= init->read_buf_size))
|
||||
if ((MQTT_MIN_PAYLOAD_SIZE >= init->write_buf_size) || (MQTT_MAX_PAYLOAD_SIZE <= init->read_buf_size))
|
||||
init->write_buf_size = MQTT_DEFAULT_BUF_SIZE;
|
||||
|
||||
c->read_buf = (unsigned char*) platform_memory_alloc(init->read_buf_size);
|
||||
c->write_buf = (unsigned char*) platform_memory_alloc(init->write_buf_size);
|
||||
if ((NULL == c->read_buf) || (NULL == c->write_buf)){
|
||||
|
||||
if ((NULL == c->read_buf) || (NULL == c->write_buf)) {
|
||||
LOG_E("%s:%d %s()... malloc buf failed...", __FILE__, __LINE__, __FUNCTION__);
|
||||
RETURN_ERROR(MQTT_MEM_NOT_ENOUGH_ERROR);
|
||||
}
|
||||
@@ -930,8 +1015,10 @@ int mqtt_init(mqtt_client_t* c, client_init_params_t* init)
|
||||
|
||||
if (0 == init->connect_params.keep_alive_interval)
|
||||
init->connect_params.keep_alive_interval = MQTT_KEEP_ALIVE_INTERVAL;
|
||||
|
||||
if (0 == init->connect_params.mqtt_version)
|
||||
init->connect_params.mqtt_version = MQTT_VERSION;
|
||||
|
||||
if (0 == init->reconnect_try_duration)
|
||||
init->reconnect_try_duration = MQTT_RECONNECT_DEFAULT_DURATION;
|
||||
|
||||
@@ -968,8 +1055,9 @@ int mqtt_release(mqtt_client_t* c)
|
||||
if (NULL == c)
|
||||
RETURN_ERROR(MQTT_NULL_VALUE_ERROR);
|
||||
|
||||
while (CLIENT_STATE_INVALID != mqtt_get_client_state(c)); /* wait for the clean session to complete */
|
||||
|
||||
if (NULL != c->network) {
|
||||
network_release(c->network);
|
||||
platform_memory_free(c->network);
|
||||
c->network = NULL;
|
||||
}
|
||||
@@ -984,8 +1072,6 @@ int mqtt_release(mqtt_client_t* c)
|
||||
c->read_buf = NULL;
|
||||
}
|
||||
|
||||
mqtt_clean_session(c);
|
||||
|
||||
memset(c, 0, sizeof(mqtt_client_t));
|
||||
|
||||
RETURN_ERROR(MQTT_SUCCESS_ERROR);
|
||||
@@ -993,6 +1079,7 @@ int mqtt_release(mqtt_client_t* c)
|
||||
|
||||
int mqtt_connect(mqtt_client_t* c)
|
||||
{
|
||||
/* connect server in blocking mode and wait for connection result */
|
||||
return mqtt_connect_with_results(c);
|
||||
}
|
||||
|
||||
@@ -1007,13 +1094,14 @@ int mqtt_disconnect(mqtt_client_t* c)
|
||||
|
||||
platform_mutex_lock(&c->write_lock);
|
||||
|
||||
len = MQTTSerialize_disconnect(c->write_buf, c->write_buf_size);
|
||||
/* serialize disconnect packet and send it */
|
||||
len = MQTTSerialize_disconnect(c->write_buf, c->write_buf_size);
|
||||
if (len > 0)
|
||||
rc = mqtt_send_packet(c, len, &timer);
|
||||
|
||||
platform_mutex_unlock(&c->write_lock);
|
||||
|
||||
mqtt_set_client_state(c, CLIENT_STATE_INVALID);
|
||||
mqtt_set_client_state(c, CLIENT_STATE_CLEAN_SESSION);
|
||||
|
||||
RETURN_ERROR(rc);
|
||||
}
|
||||
@@ -1031,22 +1119,22 @@ int mqtt_subscribe(mqtt_client_t* c, const char* topic_filter, mqtt_qos_t qos, m
|
||||
if (CLIENT_STATE_CONNECTED != mqtt_get_client_state(c))
|
||||
RETURN_ERROR(MQTT_NOT_CONNECT_ERROR);
|
||||
|
||||
platform_timer_init(&timer);
|
||||
platform_timer_cutdown(&timer, c->cmd_timeout);
|
||||
|
||||
platform_mutex_lock(&c->write_lock);
|
||||
|
||||
packet_id = mqtt_get_next_packet_id(c);
|
||||
|
||||
/* serialize subscribe packet and send it */
|
||||
len = MQTTSerialize_subscribe(c->write_buf, c->write_buf_size, 0, packet_id, 1, &topic, (int*)&qos);
|
||||
if (len <= 0)
|
||||
goto exit;
|
||||
|
||||
if ((rc = mqtt_send_packet(c, len, &timer)) != MQTT_SUCCESS_ERROR)
|
||||
goto exit;
|
||||
|
||||
if (NULL == handler)
|
||||
handler = default_msg_handler;
|
||||
handler = default_msg_handler; /* if handler is not specified, the default handler is used */
|
||||
|
||||
/* create a message and record it */
|
||||
msg_handler = mqtt_msg_handler_create(topic_filter, qos, handler);
|
||||
if (NULL == msg_handler)
|
||||
RETURN_ERROR(MQTT_MEM_NOT_ENOUGH_ERROR);
|
||||
@@ -1073,18 +1161,17 @@ int mqtt_unsubscribe(mqtt_client_t* c, const char* topic_filter)
|
||||
if (CLIENT_STATE_CONNECTED != mqtt_get_client_state(c))
|
||||
RETURN_ERROR(MQTT_NOT_CONNECT_ERROR);
|
||||
|
||||
platform_timer_init(&timer);
|
||||
platform_timer_cutdown(&timer, c->cmd_timeout);
|
||||
|
||||
platform_mutex_lock(&c->write_lock);
|
||||
|
||||
packet_id = mqtt_get_next_packet_id(c);
|
||||
|
||||
|
||||
/* serialize unsubscribe packet and send it */
|
||||
if ((len = MQTTSerialize_unsubscribe(c->write_buf, c->write_buf_size, 0, packet_id, 1, &topic)) <= 0)
|
||||
goto exit;
|
||||
if ((rc = mqtt_send_packet(c, len, &timer)) != MQTT_SUCCESS_ERROR)
|
||||
goto exit;
|
||||
|
||||
/* create a message and record it */
|
||||
msg_handler = mqtt_msg_handler_create((const char*)topic_filter, QOS0, NULL);
|
||||
if (NULL == msg_handler)
|
||||
RETURN_ERROR(MQTT_MEM_NOT_ENOUGH_ERROR);
|
||||
@@ -1114,19 +1201,17 @@ int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg
|
||||
if ((NULL != msg->payload) && (0 == msg->payloadlen))
|
||||
msg->payloadlen = strlen((char*)msg->payload);
|
||||
|
||||
platform_timer_init(&timer);
|
||||
platform_timer_cutdown(&timer, c->cmd_timeout);
|
||||
|
||||
platform_mutex_lock(&c->write_lock);
|
||||
|
||||
if (msg->qos == QOS1 || msg->qos == QOS2) {
|
||||
if (mqtt_ack_handler_is_maximum(c)) {
|
||||
rc = MQTT_ACK_HANDLER_NUM_TOO_MUCH;
|
||||
rc = MQTT_ACK_HANDLER_NUM_TOO_MUCH; /* the recorded ack handler has reached the maximum */
|
||||
goto exit;
|
||||
}
|
||||
msg->id = mqtt_get_next_packet_id(c);
|
||||
}
|
||||
|
||||
/* serialize publish packet and send it */
|
||||
len = MQTTSerialize_publish(c->write_buf, c->write_buf_size, 0, msg->qos, msg->retained, msg->id,
|
||||
topic, (unsigned char*)msg->payload, msg->payloadlen);
|
||||
if (len <= 0)
|
||||
@@ -1136,12 +1221,15 @@ int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg
|
||||
goto exit;
|
||||
|
||||
if (QOS0 != msg->qos) {
|
||||
mqtt_set_publish_dup(c,1);
|
||||
mqtt_set_publish_dup(c,1); /* may resend this data, set the udp flag in advance */
|
||||
|
||||
if (QOS1 == msg->qos) {
|
||||
rc = mqtt_ack_list_record(c, PUBACK, msg->id, len, NULL);
|
||||
/* expect to receive PUBACK, otherwise data will be resent */
|
||||
rc = mqtt_ack_list_record(c, PUBACK, msg->id, len, NULL);
|
||||
|
||||
} else if (QOS2 == msg->qos) {
|
||||
rc = mqtt_ack_list_record(c, PUBREC, msg->id, len, NULL);
|
||||
/* expect to receive PUBREC, otherwise data will be resent */
|
||||
rc = mqtt_ack_list_record(c, PUBREC, msg->id, len, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1169,26 +1257,33 @@ int mqtt_yield(mqtt_client_t* c, int timeout_ms)
|
||||
|
||||
while (!platform_timer_is_expired(&timer)) {
|
||||
state = mqtt_get_client_state(c);
|
||||
if (CLIENT_STATE_INVALID == state) {
|
||||
RETURN_ERROR(MQTT_CLOSE_SESSION_ERROR);
|
||||
if (CLIENT_STATE_CLEAN_SESSION == state) {
|
||||
RETURN_ERROR(MQTT_CLEAN_SESSION_ERROR);
|
||||
} else if (CLIENT_STATE_CONNECTED != state) {
|
||||
/* mqtt not connect, need reconnect */
|
||||
rc = mqtt_try_reconnect(c);
|
||||
|
||||
if (MQTT_RECONNECT_TIMEOUT_ERROR == rc)
|
||||
RETURN_ERROR(rc);
|
||||
continue;
|
||||
}
|
||||
|
||||
/* mqtt connected, handle mqtt packet */
|
||||
rc = mqtt_packet_handle(c, &timer);
|
||||
|
||||
if (rc >= 0) {
|
||||
/* scan ack list, destroy ack handler that have timed out or resend them */
|
||||
mqtt_ack_list_scan(c);
|
||||
|
||||
} else if (MQTT_NOT_CONNECT_ERROR == rc) {
|
||||
LOG_E("%s:%d %s()... mqtt not connect", __FILE__, __LINE__, __FUNCTION__);
|
||||
|
||||
/* reconnect timer cutdown */
|
||||
platform_timer_cutdown(&c->reconnect_timer, c->reconnect_try_duration);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
RETURN_ERROR(rc);
|
||||
}
|
||||
|
@@ -2,7 +2,7 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-09 21:31:25
|
||||
* @LastEditTime : 2020-01-11 20:11:47
|
||||
* @LastEditTime: 2020-03-15 01:12:28
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _MQTTCLIENT_H_
|
||||
@@ -17,7 +17,7 @@
|
||||
#include "platform_memory.h"
|
||||
#include "platform_mutex.h"
|
||||
#include "platform_thread.h"
|
||||
#include "mqtt_config.h"
|
||||
#include "mqtt_defconfig.h"
|
||||
#include "network.h"
|
||||
#include "random.h"
|
||||
#include "error.h"
|
||||
@@ -35,6 +35,7 @@ typedef enum client_state {
|
||||
CLIENT_STATE_INITIALIZED = 0,
|
||||
CLIENT_STATE_CONNECTED = 1,
|
||||
CLIENT_STATE_DISCONNECTED = 2,
|
||||
CLIENT_STATE_CLEAN_SESSION = 3
|
||||
}client_state_t;
|
||||
|
||||
typedef struct mqtt_connack_data {
|
||||
@@ -93,10 +94,10 @@ typedef struct connect_params {
|
||||
|
||||
typedef struct mqtt_client {
|
||||
unsigned short packet_id;
|
||||
unsigned char *read_buf;
|
||||
unsigned char *write_buf;
|
||||
unsigned char ping_outstanding;
|
||||
unsigned char ack_handler_number;
|
||||
unsigned char *read_buf;
|
||||
unsigned char *write_buf;
|
||||
unsigned int cmd_timeout;
|
||||
unsigned int read_buf_size;
|
||||
unsigned int write_buf_size;
|
||||
|
@@ -2,19 +2,19 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-09 21:30:54
|
||||
* @LastEditTime : 2020-01-14 03:38:15
|
||||
* @LastEditTime: 2020-02-25 03:49:43
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#include "platform_timer.h"
|
||||
#include "platform_nettype_tcp.h"
|
||||
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
#include "platform_nettype_tls.h"
|
||||
#endif
|
||||
|
||||
int network_read(network_t *n, unsigned char *buf, int len, int timeout)
|
||||
{
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
return platform_nettype_tls_read(n, buf, len, timeout);
|
||||
#else
|
||||
return platform_nettype_tcp_read(n, buf, len, timeout);
|
||||
@@ -23,7 +23,7 @@ int network_read(network_t *n, unsigned char *buf, int len, int timeout)
|
||||
|
||||
int network_write(network_t *n, unsigned char *buf, int len, int timeout)
|
||||
{
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
return platform_nettype_tls_write(n, buf, len, timeout);
|
||||
#else
|
||||
return platform_nettype_tcp_write(n, buf, len, timeout);
|
||||
@@ -32,7 +32,7 @@ int network_write(network_t *n, unsigned char *buf, int len, int timeout)
|
||||
|
||||
int network_connect(network_t *n)
|
||||
{
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
return platform_nettype_tls_connect(n);
|
||||
#else
|
||||
return platform_nettype_tcp_connect(n);
|
||||
@@ -41,7 +41,7 @@ int network_connect(network_t *n)
|
||||
|
||||
void network_disconnect(network_t *n)
|
||||
{
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
platform_nettype_tls_disconnect(n);
|
||||
#else
|
||||
platform_nettype_tcp_disconnect(n);
|
||||
@@ -61,7 +61,7 @@ int network_init(network_t* n, network_params_t* network_params)
|
||||
n->network_params.addr = network_params->addr;
|
||||
n->network_params.port = network_params->port;
|
||||
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
n->network_params.network_ssl_params.ca_crt = network_params->network_ssl_params.ca_crt;
|
||||
n->network_params.network_ssl_params.ca_crt_len = strlen(n->network_params.network_ssl_params.ca_crt);
|
||||
#if defined(MBEDTLS_FS_IO)
|
||||
|
@@ -2,15 +2,15 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-09 21:31:02
|
||||
* @LastEditTime : 2020-01-14 03:38:32
|
||||
* @LastEditTime: 2020-02-25 03:49:11
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _NETWORK_H_
|
||||
#define _NETWORK_H_
|
||||
|
||||
#include "mqtt_config.h"
|
||||
#include "mqtt_defconfig.h"
|
||||
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
typedef struct network_ssl_params {
|
||||
const char *ca_crt;
|
||||
size_t ca_crt_len;
|
||||
@@ -31,7 +31,7 @@ typedef struct network_ssl_params {
|
||||
typedef struct network_params {
|
||||
char *addr;
|
||||
char *port;
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
network_ssl_params_t network_ssl_params;
|
||||
void *nettype_tls_params;
|
||||
#endif /* MQTT_NETWORK_TYPE_TLS */
|
||||
|
@@ -2,7 +2,7 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-14 22:02:07
|
||||
* @LastEditTime : 2019-12-20 20:43:38
|
||||
* @LastEditTime: 2020-02-19 23:53:50
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#include "tos_k.h"
|
||||
|
@@ -2,7 +2,7 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-14 22:06:35
|
||||
* @LastEditTime : 2019-12-20 20:57:32
|
||||
* @LastEditTime: 2020-02-19 23:54:02
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _PLATFORM_MEMORY_H_
|
||||
|
@@ -92,7 +92,7 @@ int platform_net_socket_write_timeout(int fd, unsigned char *buf, int len, int t
|
||||
tv.tv_usec = 100;
|
||||
}
|
||||
|
||||
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
|
||||
platform_net_socket_setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (char *)&tv,sizeof(struct timeval));
|
||||
|
||||
return write(fd, buf, len);
|
||||
}
|
||||
|
@@ -2,21 +2,21 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2019-12-15 13:39:00
|
||||
* @LastEditTime : 2020-01-12 00:22:22
|
||||
* @LastEditTime: 2020-02-19 01:02:51
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#ifndef _PLATFORM_NET_SOCKET_H_
|
||||
#define _PLATFORM_NET_SOCKET_H_
|
||||
|
||||
#include "network.h"
|
||||
#include "error.h"
|
||||
|
||||
#include "lwip/opt.h"
|
||||
#include "lwip/sys.h"
|
||||
#include "lwip/api.h"
|
||||
#include <lwip/sockets.h>
|
||||
#include "lwip/netdb.h"
|
||||
|
||||
#include "network.h"
|
||||
#include "error.h"
|
||||
|
||||
#define PLATFORM_NET_PROTO_TCP 0 /**< The TCP transport protocol */
|
||||
#define PLATFORM_NET_PROTO_UDP 1 /**< The UDP transport protocol */
|
||||
|
||||
|
@@ -2,7 +2,7 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2020-01-11 19:45:35
|
||||
* @LastEditTime : 2020-01-16 00:17:56
|
||||
* @LastEditTime: 2020-03-05 23:52:30
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
#include "platform_nettype_tls.h"
|
||||
@@ -11,7 +11,7 @@
|
||||
#include "platform_timer.h"
|
||||
#include "random.h"
|
||||
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
|
||||
#include "mbedtls/platform.h"
|
||||
#include "mbedtls/ssl.h"
|
||||
@@ -20,6 +20,8 @@
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "mbedtls/error.h"
|
||||
#include "mbedtls/debug.h"
|
||||
#include "mbedtls\x509_crt.h"
|
||||
#include "mbedtls\pk.h"
|
||||
|
||||
#if !defined(MBEDTLS_FS_IO)
|
||||
static const int ciphersuites[] = { MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA, MBEDTLS_TLS_PSK_WITH_AES_256_CBC_SHA, 0 };
|
||||
@@ -28,7 +30,8 @@ static const int ciphersuites[] = { MBEDTLS_TLS_PSK_WITH_AES_128_CBC_SHA, MBEDTL
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
static int server_certificate_verify(void *hostname, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
|
||||
{
|
||||
LOG_I("%s:%d %s()... server_certificate_verify failed returned 0x%04x\n", __FILE__, __LINE__, __FUNCTION__, *flags);
|
||||
if (0 != *flags)
|
||||
LOG_E("%s:%d %s()... server_certificate_verify failed returned 0x%04x\n", __FILE__, __LINE__, __FUNCTION__, *flags);
|
||||
return *flags;
|
||||
}
|
||||
#endif
|
||||
@@ -110,12 +113,12 @@ static int platform_nettype_tls_init(network_t* n, nettype_tls_params_t* nettype
|
||||
if (n->network_params.network_ssl_params.cert_file != NULL && n->network_params.network_ssl_params.key_file != NULL) {
|
||||
if ((rc = mbedtls_x509_crt_parse_file(&(nettype_tls_params->client_cert), n->network_params.network_ssl_params.cert_file)) != 0) {
|
||||
LOG_E("%s:%d %s()... load client cert file failed returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
|
||||
return PLATFORM_ERR_SSL_CERT;
|
||||
return MQTT_SSL_CERT_ERROR;
|
||||
}
|
||||
|
||||
if ((rc = mbedtls_pk_parse_keyfile(&(nettype_tls_params->private_key), n->network_params.network_ssl_params.key_file, "")) != 0) {
|
||||
LOG_E("%s:%d %s()... load client key file failed returned 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
|
||||
return PLATFORM_ERR_SSL_CERT;
|
||||
return MQTT_SSL_CERT_ERROR;
|
||||
}
|
||||
} else {
|
||||
LOG_I("%s:%d %s()... cert_file/key_file is empty! | cert_file = %s | key_file = %s", __FILE__, __LINE__, __FUNCTION__,
|
||||
@@ -129,12 +132,13 @@ static int platform_nettype_tls_init(network_t* n, nettype_tls_params_t* nettype
|
||||
n->network_params.network_ssl_params.psk_length, (const unsigned char *) psk_id, strlen( psk_id ));
|
||||
|
||||
mbedtls_ssl_conf_ciphersuites(&(nettype_tls_params->ssl_conf), ciphersuites);
|
||||
|
||||
if (0 != rc) {
|
||||
LOG_E("%s:%d %s()... mbedtls_ssl_conf_psk fail: 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
|
||||
return rc;
|
||||
}
|
||||
}
|
||||
|
||||
if (0 != rc) {
|
||||
LOG_E("%s:%d %s()... mbedtls_ssl_conf_psk fail: 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
|
||||
return rc;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
@@ -277,7 +281,7 @@ int platform_nettype_tls_read(network_t *n, unsigned char *buf, int len, int tim
|
||||
if (rc > 0) {
|
||||
read_len += rc;
|
||||
} else if ((rc == 0) || ((rc != MBEDTLS_ERR_SSL_WANT_WRITE) && (rc != MBEDTLS_ERR_SSL_WANT_READ) && (rc != MBEDTLS_ERR_SSL_TIMEOUT))) {
|
||||
LOG_E("%s:%d %s()... mbedtls_ssl_read failed: 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
|
||||
// LOG_E("%s:%d %s()... mbedtls_ssl_read failed: 0x%04x", __FILE__, __LINE__, __FUNCTION__, (rc < 0 )? -rc : rc);
|
||||
break;
|
||||
}
|
||||
} while((!platform_timer_is_expired(&timer)) && (read_len < len));
|
||||
|
@@ -2,7 +2,7 @@
|
||||
* @Author: jiejie
|
||||
* @Github: https://github.com/jiejieTop
|
||||
* @Date: 2020-01-11 19:45:44
|
||||
* @LastEditTime : 2020-01-13 07:26:26
|
||||
* @LastEditTime: 2020-02-25 03:51:37
|
||||
* @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
|
||||
*/
|
||||
|
||||
@@ -10,12 +10,12 @@
|
||||
#define _PLATFORM_NETTYPE_TLS_H_
|
||||
|
||||
#include "platform_net_socket.h"
|
||||
#include "mqtt_config.h"
|
||||
#include "mqtt_defconfig.h"
|
||||
#include "network.h"
|
||||
#include "error.h"
|
||||
#include "log.h"
|
||||
|
||||
#if MQTT_NETWORK_TYPE_TLS
|
||||
#ifdef MQTT_NETWORK_TYPE_TLS
|
||||
|
||||
typedef struct nettype_tls_params {
|
||||
mbedtls_net_context socket_fd; /**< mbed TLS network context. */
|
||||
|
@@ -38,6 +38,23 @@ platform_thread_t *platform_thread_init( const char *name,
|
||||
return thread;
|
||||
}
|
||||
|
||||
void platform_thread_startup(platform_thread_t* thread)
|
||||
{
|
||||
(void)thread;
|
||||
}
|
||||
|
||||
|
||||
void platform_thread_stop(platform_thread_t* thread)
|
||||
{
|
||||
tos_task_suspend(&(thread->thread));
|
||||
}
|
||||
|
||||
void platform_thread_start(platform_thread_t* thread)
|
||||
{
|
||||
tos_task_resume(&(thread->thread));
|
||||
}
|
||||
|
||||
|
||||
void platform_thread_destroy(platform_thread_t* thread)
|
||||
{
|
||||
if (NULL != thread)
|
||||
|
@@ -20,6 +20,9 @@ platform_thread_t *platform_thread_init( const char *name,
|
||||
unsigned int stack_size,
|
||||
unsigned int priority,
|
||||
unsigned int tick);
|
||||
void platform_thread_startup(platform_thread_t* thread);
|
||||
void platform_thread_stop(platform_thread_t* thread);
|
||||
void platform_thread_start(platform_thread_t* thread);
|
||||
void platform_thread_destroy(platform_thread_t* thread);
|
||||
|
||||
#endif
|
||||
|
@@ -63,7 +63,9 @@ void application_entry(void *arg)
|
||||
LOG_D("mqtt connect error is %#x", error);
|
||||
|
||||
mqtt_subscribe(&client, "tos-topic", QOS0, tos_topic_handler);
|
||||
|
||||
|
||||
LOG_D("mqtt subscribe error is %#x", error);
|
||||
|
||||
memset(&msg, 0, sizeof(msg));
|
||||
|
||||
for (;;) {
|
||||
|
@@ -7,36 +7,37 @@
|
||||
*/
|
||||
static const char *test_ca_crt = {
|
||||
"-----BEGIN CERTIFICATE-----\r\n"
|
||||
"MIIFlzCCBH+gAwIBAgIQBf0c/dicZUkWCJnzWyxOuDANBgkqhkiG9w0BAQsFADBy\r\n"
|
||||
"MIIFrTCCBJWgAwIBAgIQDApSGhCHMtFicAaXgcO0vjANBgkqhkiG9w0BAQsFADBy\r\n"
|
||||
"MQswCQYDVQQGEwJDTjElMCMGA1UEChMcVHJ1c3RBc2lhIFRlY2hub2xvZ2llcywg\r\n"
|
||||
"SW5jLjEdMBsGA1UECxMURG9tYWluIFZhbGlkYXRlZCBTU0wxHTAbBgNVBAMTFFRy\r\n"
|
||||
"dXN0QXNpYSBUTFMgUlNBIENBMB4XDTE5MDMyOTAwMDAwMFoXDTIwMDMyODEyMDAw\r\n"
|
||||
"dXN0QXNpYSBUTFMgUlNBIENBMB4XDTIwMDEyOTAwMDAwMFoXDTIxMDQyODEyMDAw\r\n"
|
||||
"MFowGzEZMBcGA1UEAxMQd3d3LmppZWppZTAxLnRvcDCCASIwDQYJKoZIhvcNAQEB\r\n"
|
||||
"BQADggEPADCCAQoCggEBAKV5ot0wC7L2sa2ABzLDaYYmxbX61FpHYscY8R1R8nUQ\r\n"
|
||||
"pRfbj8eCNcg6DZf85OX/sSmtqRnk2Z+Nu9ML5KO5sSvVCr/eL0bW8jqlQ39FuyoR\r\n"
|
||||
"vsLkUP8iXIkHyEulPagYK8yFNCf0eR0c/SQO0U+UI4JrVOtwFHV/y8IbZ9pEboBH\r\n"
|
||||
"2II2OYC8azavFoUQXVn+niQiYJb9KGN6Jz4mCgVeGXfoKjW5qtQuCtj0rdpDx+UX\r\n"
|
||||
"JWP5ktLCCfV5Ke+Vb0Ovahr8kASremR/XSh+K8pP+94uxrMp79wc4YVEiMbKEb8j\r\n"
|
||||
"PVnOJwF7d7bjOU2M0baq5ouxE1xl8c2xIc8zABIrUuMCAwEAAaOCAn4wggJ6MB8G\r\n"
|
||||
"A1UdIwQYMBaAFH/TmfOgRw4xAFZWIo63zJ7dygGKMB0GA1UdDgQWBBS4EllqeVPA\r\n"
|
||||
"jt5ZiROEesnBWP5vDTApBgNVHREEIjAgghB3d3cuamllamllMDEudG9wggxqaWVq\r\n"
|
||||
"BQADggEPADCCAQoCggEBAKIDMo45w085LaCMG1LWY5b8V94zDqdt+weVhKolgsLZ\r\n"
|
||||
"htAQTDrafBx1sNJtOpa8ADeQkFbWOTEy3tgViOBvBr+8Qhl6vYsESJrg7DXeCVRm\r\n"
|
||||
"04pk+cFrdWYRE70AUz8RXRuaWLv1Fu2L+qlymrnZB/WBJFnETINh6yzqY8FNETUV\r\n"
|
||||
"EL08eE0LoXt/4b7iAJYEFRYTyBKjLpkr04e92SQIuL/l42j92lYNOjYfYOlpWZUC\r\n"
|
||||
"cp4WHXEDd1YXdTMmXpfsU3VlYS3RTusOFsgXpLFET9xGRvtDSu5qw6rPGf/y/PhT\r\n"
|
||||
"1LFB/xlL2E6Rpo/6VWuQ8A5rA+H3D1I/fIBB97orMYUCAwEAAaOCApQwggKQMB8G\r\n"
|
||||
"A1UdIwQYMBaAFH/TmfOgRw4xAFZWIo63zJ7dygGKMB0GA1UdDgQWBBQyzyOTN1l5\r\n"
|
||||
"Rg1Ih1tQ0TPYMqfw+jApBgNVHREEIjAgghB3d3cuamllamllMDEudG9wggxqaWVq\r\n"
|
||||
"aWUwMS50b3AwDgYDVR0PAQH/BAQDAgWgMB0GA1UdJQQWMBQGCCsGAQUFBwMBBggr\r\n"
|
||||
"BgEFBQcDAjBMBgNVHSAERTBDMDcGCWCGSAGG/WwBAjAqMCgGCCsGAQUFBwIBFhxo\r\n"
|
||||
"dHRwczovL3d3dy5kaWdpY2VydC5jb20vQ1BTMAgGBmeBDAECATB9BggrBgEFBQcB\r\n"
|
||||
"AQRxMG8wIQYIKwYBBQUHMAGGFWh0dHA6Ly9vY3NwLmRjb2NzcC5jbjBKBggrBgEF\r\n"
|
||||
"BQcwAoY+aHR0cDovL2NhY2VydHMuZGlnaXRhbGNlcnR2YWxpZGF0aW9uLmNvbS9U\r\n"
|
||||
"cnVzdEFzaWFUTFNSU0FDQS5jcnQwCQYDVR0TBAIwADCCAQQGCisGAQQB1nkCBAIE\r\n"
|
||||
"gfUEgfIA8AB2ALvZ37wfinG1k5Qjl6qSe0c4V5UKq1LoGpCWZDaOHtGFAAABacpL\r\n"
|
||||
"x9kAAAQDAEcwRQIgbNEvEkLAgPDhqZGrXqV3rmByWiQ5CgTUnRo8vRKYH7ICIQDS\r\n"
|
||||
"V+bNpM7cOoPADyZsmRDxdu+kUfkhD3csdkALmMyobAB2AId1v+dZfPiMQ5lfvfNu\r\n"
|
||||
"/1aNR1Y2/0q1YMG06v9eoIMPAAABacpLyO0AAAQDAEcwRQIgP69W3D/iVIaVFVc/\r\n"
|
||||
"+I98YB083woBDWfquBpZq2dXA5ECIQDtP465d8Q0UuNK1gRYCZOe1Daexdndkt2g\r\n"
|
||||
"8zServG3HjANBgkqhkiG9w0BAQsFAAOCAQEADgP0Q4vUgYAR6e98x/tP12Rpsqkt\r\n"
|
||||
"l0Mbpr12lqSDmH2QfmoWRIRmyRrbpR2fkPKikrib3ezTmSJI70saELY8ceLZiaQu\r\n"
|
||||
"Vq6zT/8xXDUcWJbeRgUVo8J3b20oxJLLsf8QmmEMdycKsUJv0iybQq9CCqay8Tq3\r\n"
|
||||
"mGJ2bc6zAqWUjcugLSvh/c6QPp9d8UGyVre/UJFbGVpk8swkhXFTypUVcm+TNqzW\r\n"
|
||||
"A3kKC3k5Sk+l1CTNHgQTO7+T+/9anzLuyf/FNTO3WLav/J/FobMbXycqbwYemisk\r\n"
|
||||
"kS9xodt6dEDc5wf8nWlUOHzU/n0WCjr4Cgl1gRdTRFwy+cLEhLMZOu4vjQ==\r\n"
|
||||
"dHRwczovL3d3dy5kaWdpY2VydC5jb20vQ1BTMAgGBmeBDAECATCBkgYIKwYBBQUH\r\n"
|
||||
"AQEEgYUwgYIwNAYIKwYBBQUHMAGGKGh0dHA6Ly9zdGF0dXNlLmRpZ2l0YWxjZXJ0\r\n"
|
||||
"dmFsaWRhdGlvbi5jb20wSgYIKwYBBQUHMAKGPmh0dHA6Ly9jYWNlcnRzLmRpZ2l0\r\n"
|
||||
"YWxjZXJ0dmFsaWRhdGlvbi5jb20vVHJ1c3RBc2lhVExTUlNBQ0EuY3J0MAkGA1Ud\r\n"
|
||||
"EwQCMAAwggEEBgorBgEEAdZ5AgQCBIH1BIHyAPAAdQB9PvL4j/+IVWgkwsDKnlKJ\r\n"
|
||||
"eSvFDngJfy5ql2iZfiLw1wAAAW/wKSTgAAAEAwBGMEQCIGFq0FdvZfXf4lV20Am1\r\n"
|
||||
"HRP6F7wxzkesK0r1566sNqvxAiBp5W3iTLWEgeJa/PfH5hX/d+K5CIyXScLa4qqa\r\n"
|
||||
"MCLHwwB3AFzcQ5L+5qtFRLFemtRW5hA3+9X6R9yhc5SyXub2xw7KAAABb/ApJJIA\r\n"
|
||||
"AAQDAEgwRgIhANEsjShyRf0GGpwJ6ZTQKBHo933rlSpaNIvor7cG8RBQAiEAkeDf\r\n"
|
||||
"7+n+zyEGZUMOYI0E0R2chjPBJGvtw1yD12sxekowDQYJKoZIhvcNAQELBQADggEB\r\n"
|
||||
"AIzaZ5X1So+xVe2JWkMfmJA8IQhdp9WghCDLRORcIggcY9BtYxFSBdusxIa1bhdt\r\n"
|
||||
"rCY6RoepCwTrhV9PotwMgZtSOu8szHHRlqX8zNUhIh628yzPSTDDZ4xgeJvlGAkT\r\n"
|
||||
"Zlv0XrezkDLRZkKN9R6KX1ccaPNbn9PD6SMtpHPbE7UEZYfrV3wAJhFzsyhp2JF4\r\n"
|
||||
"KLLNPaeDgDM3Lu6tUm/bznDEyxi7/ZoR+7fSQAMF5Jo1ysKUAOC00I9Ne+7eSbTh\r\n"
|
||||
"flV//8NBN+Z2ShCV0uFedf6ugDUMOuOUCtp0c7N+sM1IVE5MOhLRDAGRIUyi7/43\r\n"
|
||||
"dJ0Okust1fXo4UTDGJtyp30=\r\n"
|
||||
"-----END CERTIFICATE-----\r\n"
|
||||
"-----BEGIN CERTIFICATE-----\r\n"
|
||||
"MIIErjCCA5agAwIBAgIQBYAmfwbylVM0jhwYWl7uLjANBgkqhkiG9w0BAQsFADBh\r\n"
|
||||
|
Reference in New Issue
Block a user