add qcloud explorer ota sample
This commit is contained in:
@@ -493,8 +493,8 @@
|
||||
<tvExp>0</tvExp>
|
||||
<tvExpOptDlg>0</tvExpOptDlg>
|
||||
<bDave2>0</bDave2>
|
||||
<PathWithFileName>..\..\..\..\examples\qcloud_iot_explorer_sdk_data_template\qcloud_iot_hub_sdk_explorer.c</PathWithFileName>
|
||||
<FilenameWithoutPath>qcloud_iot_hub_sdk_explorer.c</FilenameWithoutPath>
|
||||
<PathWithFileName>..\..\..\..\examples\qcloud_iot_explorer_sdk_data_template\entry.c</PathWithFileName>
|
||||
<FilenameWithoutPath>entry.c</FilenameWithoutPath>
|
||||
<RteFlg>0</RteFlg>
|
||||
<bShared>0</bShared>
|
||||
</File>
|
||||
|
@@ -16,7 +16,7 @@
|
||||
<TargetCommonOption>
|
||||
<Device>STM32L431RCTx</Device>
|
||||
<Vendor>STMicroelectronics</Vendor>
|
||||
<PackID>Keil.STM32L4xx_DFP.2.2.0</PackID>
|
||||
<PackID>Keil.STM32L4xx_DFP.2.0.0</PackID>
|
||||
<PackURL>http://www.keil.com/pack</PackURL>
|
||||
<Cpu>IRAM(0x20000000-0x2000FFFF) IROM(0x8000000-0x803FFFF) CLOCK(8000000) FPU2 CPUTYPE("Cortex-M4")</Cpu>
|
||||
<FlashUtilSpec></FlashUtilSpec>
|
||||
@@ -468,9 +468,9 @@
|
||||
<FilePath>..\..\..\..\examples\qcloud_iot_explorer_sdk_data_template\events_config.c</FilePath>
|
||||
</File>
|
||||
<File>
|
||||
<FileName>qcloud_iot_hub_sdk_explorer.c</FileName>
|
||||
<FileName>entry.c</FileName>
|
||||
<FileType>1</FileType>
|
||||
<FilePath>..\..\..\..\examples\qcloud_iot_explorer_sdk_data_template\qcloud_iot_hub_sdk_explorer.c</FilePath>
|
||||
<FilePath>..\..\..\..\examples\qcloud_iot_explorer_sdk_data_template\entry.c</FilePath>
|
||||
</File>
|
||||
</Files>
|
||||
</Group>
|
||||
|
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,404 @@
|
||||
;********************** COPYRIGHT(c) 2017 STMicroelectronics ******************
|
||||
;* File Name : startup_stm32l431xx.s
|
||||
;* Author : MCD Application Team
|
||||
;* Description : STM32L431xx Ultra Low Power devices vector table for MDK-ARM toolchain.
|
||||
;* This module performs:
|
||||
;* - Set the initial SP
|
||||
;* - Set the initial PC == Reset_Handler
|
||||
;* - Set the vector table entries with the exceptions ISR address
|
||||
;* - Branches to __main in the C library (which eventually
|
||||
;* calls main()).
|
||||
;* After Reset the Cortex-M4 processor is in Thread mode,
|
||||
;* priority is Privileged, and the Stack is set to Main.
|
||||
;* <<< Use Configuration Wizard in Context Menu >>>
|
||||
;*******************************************************************************
|
||||
;*
|
||||
;* Redistribution and use in source and binary forms, with or without modification,
|
||||
;* are permitted provided that the following conditions are met:
|
||||
;* 1. Redistributions of source code must retain the above copyright notice,
|
||||
;* this list of conditions and the following disclaimer.
|
||||
;* 2. Redistributions in binary form must reproduce the above copyright notice,
|
||||
;* this list of conditions and the following disclaimer in the documentation
|
||||
;* and/or other materials provided with the distribution.
|
||||
;* 3. Neither the name of STMicroelectronics nor the names of its contributors
|
||||
;* may be used to endorse or promote products derived from this software
|
||||
;* without specific prior written permission.
|
||||
;*
|
||||
;* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
;* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
;* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
;* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
||||
;* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
;* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
||||
;* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
||||
;* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
||||
;* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
;* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
;*
|
||||
;*******************************************************************************
|
||||
;
|
||||
; Amount of memory (in bytes) allocated for Stack
|
||||
; Tailor this value to your application needs
|
||||
; <h> Stack Configuration
|
||||
; <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
|
||||
; </h>
|
||||
|
||||
Stack_Size EQU 0x100
|
||||
|
||||
AREA STACK, NOINIT, READWRITE, ALIGN=3
|
||||
Stack_Mem SPACE Stack_Size
|
||||
__initial_sp
|
||||
|
||||
|
||||
; <h> Heap Configuration
|
||||
; <o> Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
|
||||
; </h>
|
||||
|
||||
Heap_Size EQU 0x100
|
||||
|
||||
AREA HEAP, NOINIT, READWRITE, ALIGN=3
|
||||
__heap_base
|
||||
Heap_Mem SPACE Heap_Size
|
||||
__heap_limit
|
||||
|
||||
PRESERVE8
|
||||
THUMB
|
||||
|
||||
|
||||
; Vector Table Mapped to Address 0 at Reset
|
||||
AREA RESET, DATA, READONLY
|
||||
EXPORT __Vectors
|
||||
EXPORT __Vectors_End
|
||||
EXPORT __Vectors_Size
|
||||
|
||||
__Vectors DCD __initial_sp ; Top of Stack
|
||||
DCD Reset_Handler ; Reset Handler
|
||||
DCD NMI_Handler ; NMI Handler
|
||||
DCD HardFault_Handler ; Hard Fault Handler
|
||||
DCD MemManage_Handler ; MPU Fault Handler
|
||||
DCD BusFault_Handler ; Bus Fault Handler
|
||||
DCD UsageFault_Handler ; Usage Fault Handler
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD SVC_Handler ; SVCall Handler
|
||||
DCD DebugMon_Handler ; Debug Monitor Handler
|
||||
DCD 0 ; Reserved
|
||||
DCD PendSV_Handler ; PendSV Handler
|
||||
DCD SysTick_Handler ; SysTick Handler
|
||||
|
||||
; External Interrupts
|
||||
DCD WWDG_IRQHandler ; Window WatchDog
|
||||
DCD PVD_PVM_IRQHandler ; PVD/PVM1/PVM2/PVM3/PVM4 through EXTI Line detection
|
||||
DCD TAMP_STAMP_IRQHandler ; Tamper and TimeStamps through the EXTI line
|
||||
DCD RTC_WKUP_IRQHandler ; RTC Wakeup through the EXTI line
|
||||
DCD FLASH_IRQHandler ; FLASH
|
||||
DCD RCC_IRQHandler ; RCC
|
||||
DCD EXTI0_IRQHandler ; EXTI Line0
|
||||
DCD EXTI1_IRQHandler ; EXTI Line1
|
||||
DCD EXTI2_IRQHandler ; EXTI Line2
|
||||
DCD EXTI3_IRQHandler ; EXTI Line3
|
||||
DCD EXTI4_IRQHandler ; EXTI Line4
|
||||
DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1
|
||||
DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2
|
||||
DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3
|
||||
DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4
|
||||
DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5
|
||||
DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6
|
||||
DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7
|
||||
DCD ADC1_IRQHandler ; ADC1
|
||||
DCD CAN1_TX_IRQHandler ; CAN1 TX
|
||||
DCD CAN1_RX0_IRQHandler ; CAN1 RX0
|
||||
DCD CAN1_RX1_IRQHandler ; CAN1 RX1
|
||||
DCD CAN1_SCE_IRQHandler ; CAN1 SCE
|
||||
DCD EXTI9_5_IRQHandler ; External Line[9:5]s
|
||||
DCD TIM1_BRK_TIM15_IRQHandler ; TIM1 Break and TIM15
|
||||
DCD TIM1_UP_TIM16_IRQHandler ; TIM1 Update and TIM16
|
||||
DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation
|
||||
DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare
|
||||
DCD TIM2_IRQHandler ; TIM2
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD I2C1_EV_IRQHandler ; I2C1 Event
|
||||
DCD I2C1_ER_IRQHandler ; I2C1 Error
|
||||
DCD I2C2_EV_IRQHandler ; I2C2 Event
|
||||
DCD I2C2_ER_IRQHandler ; I2C2 Error
|
||||
DCD SPI1_IRQHandler ; SPI1
|
||||
DCD SPI2_IRQHandler ; SPI2
|
||||
DCD USART1_IRQHandler ; USART1
|
||||
DCD USART2_IRQHandler ; USART2
|
||||
DCD USART3_IRQHandler ; USART3
|
||||
DCD EXTI15_10_IRQHandler ; External Line[15:10]
|
||||
DCD RTC_Alarm_IRQHandler ; RTC Alarm (A and B) through EXTI Line
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD SDMMC1_IRQHandler ; SDMMC1
|
||||
DCD 0 ; Reserved
|
||||
DCD SPI3_IRQHandler ; SPI3
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD TIM6_DAC_IRQHandler ; TIM6 and DAC1&2 underrun errors
|
||||
DCD TIM7_IRQHandler ; TIM7
|
||||
DCD DMA2_Channel1_IRQHandler ; DMA2 Channel 1
|
||||
DCD DMA2_Channel2_IRQHandler ; DMA2 Channel 2
|
||||
DCD DMA2_Channel3_IRQHandler ; DMA2 Channel 3
|
||||
DCD DMA2_Channel4_IRQHandler ; DMA2 Channel 4
|
||||
DCD DMA2_Channel5_IRQHandler ; DMA2 Channel 5
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD COMP_IRQHandler ; COMP Interrupt
|
||||
DCD LPTIM1_IRQHandler ; LP TIM1 interrupt
|
||||
DCD LPTIM2_IRQHandler ; LP TIM2 interrupt
|
||||
DCD 0 ; Reserved
|
||||
DCD DMA2_Channel6_IRQHandler ; DMA2 Channel 6
|
||||
DCD DMA2_Channel7_IRQHandler ; DMA2 Channel 7
|
||||
DCD LPUART1_IRQHandler ; LP UART1 interrupt
|
||||
DCD QUADSPI_IRQHandler ; Quad SPI global interrupt
|
||||
DCD I2C3_EV_IRQHandler ; I2C3 event
|
||||
DCD I2C3_ER_IRQHandler ; I2C3 error
|
||||
DCD SAI1_IRQHandler ; Serial Audio Interface 1 global interrupt
|
||||
DCD 0 ; Reserved
|
||||
DCD SWPMI1_IRQHandler ; Serial Wire Interface 1 global interrupt
|
||||
DCD TSC_IRQHandler ; Touch Sense Controller global interrupt
|
||||
DCD 0 ; Reserved
|
||||
DCD 0 ; Reserved
|
||||
DCD RNG_IRQHandler ; RNG global interrupt
|
||||
DCD FPU_IRQHandler ; FPU
|
||||
DCD CRS_IRQHandler ; CRS interrupt
|
||||
|
||||
__Vectors_End
|
||||
|
||||
__Vectors_Size EQU __Vectors_End - __Vectors
|
||||
|
||||
AREA |.text|, CODE, READONLY
|
||||
|
||||
; Reset handler
|
||||
Reset_Handler PROC
|
||||
EXPORT Reset_Handler [WEAK]
|
||||
IMPORT SystemInit
|
||||
IMPORT __main
|
||||
|
||||
LDR R0, =SystemInit
|
||||
BLX R0
|
||||
LDR R0, =__main
|
||||
BX R0
|
||||
ENDP
|
||||
|
||||
; Dummy Exception Handlers (infinite loops which can be modified)
|
||||
|
||||
NMI_Handler PROC
|
||||
EXPORT NMI_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
HardFault_Handler\
|
||||
PROC
|
||||
EXPORT HardFault_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
MemManage_Handler\
|
||||
PROC
|
||||
EXPORT MemManage_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
BusFault_Handler\
|
||||
PROC
|
||||
EXPORT BusFault_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
UsageFault_Handler\
|
||||
PROC
|
||||
EXPORT UsageFault_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
SVC_Handler PROC
|
||||
EXPORT SVC_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
DebugMon_Handler\
|
||||
PROC
|
||||
EXPORT DebugMon_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
PendSV_Handler PROC
|
||||
EXPORT PendSV_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
SysTick_Handler PROC
|
||||
EXPORT SysTick_Handler [WEAK]
|
||||
B .
|
||||
ENDP
|
||||
|
||||
Default_Handler PROC
|
||||
|
||||
EXPORT WWDG_IRQHandler [WEAK]
|
||||
EXPORT PVD_PVM_IRQHandler [WEAK]
|
||||
EXPORT TAMP_STAMP_IRQHandler [WEAK]
|
||||
EXPORT RTC_WKUP_IRQHandler [WEAK]
|
||||
EXPORT FLASH_IRQHandler [WEAK]
|
||||
EXPORT RCC_IRQHandler [WEAK]
|
||||
EXPORT EXTI0_IRQHandler [WEAK]
|
||||
EXPORT EXTI1_IRQHandler [WEAK]
|
||||
EXPORT EXTI2_IRQHandler [WEAK]
|
||||
EXPORT EXTI3_IRQHandler [WEAK]
|
||||
EXPORT EXTI4_IRQHandler [WEAK]
|
||||
EXPORT DMA1_Channel1_IRQHandler [WEAK]
|
||||
EXPORT DMA1_Channel2_IRQHandler [WEAK]
|
||||
EXPORT DMA1_Channel3_IRQHandler [WEAK]
|
||||
EXPORT DMA1_Channel4_IRQHandler [WEAK]
|
||||
EXPORT DMA1_Channel5_IRQHandler [WEAK]
|
||||
EXPORT DMA1_Channel6_IRQHandler [WEAK]
|
||||
EXPORT DMA1_Channel7_IRQHandler [WEAK]
|
||||
EXPORT ADC1_IRQHandler [WEAK]
|
||||
EXPORT CAN1_TX_IRQHandler [WEAK]
|
||||
EXPORT CAN1_RX0_IRQHandler [WEAK]
|
||||
EXPORT CAN1_RX1_IRQHandler [WEAK]
|
||||
EXPORT CAN1_SCE_IRQHandler [WEAK]
|
||||
EXPORT EXTI9_5_IRQHandler [WEAK]
|
||||
EXPORT TIM1_BRK_TIM15_IRQHandler [WEAK]
|
||||
EXPORT TIM1_UP_TIM16_IRQHandler [WEAK]
|
||||
EXPORT TIM1_TRG_COM_IRQHandler [WEAK]
|
||||
EXPORT TIM1_CC_IRQHandler [WEAK]
|
||||
EXPORT TIM2_IRQHandler [WEAK]
|
||||
EXPORT I2C1_EV_IRQHandler [WEAK]
|
||||
EXPORT I2C1_ER_IRQHandler [WEAK]
|
||||
EXPORT I2C2_EV_IRQHandler [WEAK]
|
||||
EXPORT I2C2_ER_IRQHandler [WEAK]
|
||||
EXPORT SPI1_IRQHandler [WEAK]
|
||||
EXPORT SPI2_IRQHandler [WEAK]
|
||||
EXPORT USART1_IRQHandler [WEAK]
|
||||
EXPORT USART2_IRQHandler [WEAK]
|
||||
EXPORT USART3_IRQHandler [WEAK]
|
||||
EXPORT EXTI15_10_IRQHandler [WEAK]
|
||||
EXPORT RTC_Alarm_IRQHandler [WEAK]
|
||||
EXPORT SDMMC1_IRQHandler [WEAK]
|
||||
EXPORT SPI3_IRQHandler [WEAK]
|
||||
EXPORT TIM6_DAC_IRQHandler [WEAK]
|
||||
EXPORT TIM7_IRQHandler [WEAK]
|
||||
EXPORT DMA2_Channel1_IRQHandler [WEAK]
|
||||
EXPORT DMA2_Channel2_IRQHandler [WEAK]
|
||||
EXPORT DMA2_Channel3_IRQHandler [WEAK]
|
||||
EXPORT DMA2_Channel4_IRQHandler [WEAK]
|
||||
EXPORT DMA2_Channel5_IRQHandler [WEAK]
|
||||
EXPORT COMP_IRQHandler [WEAK]
|
||||
EXPORT LPTIM1_IRQHandler [WEAK]
|
||||
EXPORT LPTIM2_IRQHandler [WEAK]
|
||||
EXPORT DMA2_Channel6_IRQHandler [WEAK]
|
||||
EXPORT DMA2_Channel7_IRQHandler [WEAK]
|
||||
EXPORT LPUART1_IRQHandler [WEAK]
|
||||
EXPORT QUADSPI_IRQHandler [WEAK]
|
||||
EXPORT I2C3_EV_IRQHandler [WEAK]
|
||||
EXPORT I2C3_ER_IRQHandler [WEAK]
|
||||
EXPORT SAI1_IRQHandler [WEAK]
|
||||
EXPORT SWPMI1_IRQHandler [WEAK]
|
||||
EXPORT TSC_IRQHandler [WEAK]
|
||||
EXPORT RNG_IRQHandler [WEAK]
|
||||
EXPORT FPU_IRQHandler [WEAK]
|
||||
EXPORT CRS_IRQHandler [WEAK]
|
||||
|
||||
WWDG_IRQHandler
|
||||
PVD_PVM_IRQHandler
|
||||
TAMP_STAMP_IRQHandler
|
||||
RTC_WKUP_IRQHandler
|
||||
FLASH_IRQHandler
|
||||
RCC_IRQHandler
|
||||
EXTI0_IRQHandler
|
||||
EXTI1_IRQHandler
|
||||
EXTI2_IRQHandler
|
||||
EXTI3_IRQHandler
|
||||
EXTI4_IRQHandler
|
||||
DMA1_Channel1_IRQHandler
|
||||
DMA1_Channel2_IRQHandler
|
||||
DMA1_Channel3_IRQHandler
|
||||
DMA1_Channel4_IRQHandler
|
||||
DMA1_Channel5_IRQHandler
|
||||
DMA1_Channel6_IRQHandler
|
||||
DMA1_Channel7_IRQHandler
|
||||
ADC1_IRQHandler
|
||||
CAN1_TX_IRQHandler
|
||||
CAN1_RX0_IRQHandler
|
||||
CAN1_RX1_IRQHandler
|
||||
CAN1_SCE_IRQHandler
|
||||
EXTI9_5_IRQHandler
|
||||
TIM1_BRK_TIM15_IRQHandler
|
||||
TIM1_UP_TIM16_IRQHandler
|
||||
TIM1_TRG_COM_IRQHandler
|
||||
TIM1_CC_IRQHandler
|
||||
TIM2_IRQHandler
|
||||
I2C1_EV_IRQHandler
|
||||
I2C1_ER_IRQHandler
|
||||
I2C2_EV_IRQHandler
|
||||
I2C2_ER_IRQHandler
|
||||
SPI1_IRQHandler
|
||||
SPI2_IRQHandler
|
||||
USART1_IRQHandler
|
||||
USART2_IRQHandler
|
||||
USART3_IRQHandler
|
||||
EXTI15_10_IRQHandler
|
||||
RTC_Alarm_IRQHandler
|
||||
SDMMC1_IRQHandler
|
||||
SPI3_IRQHandler
|
||||
TIM6_DAC_IRQHandler
|
||||
TIM7_IRQHandler
|
||||
DMA2_Channel1_IRQHandler
|
||||
DMA2_Channel2_IRQHandler
|
||||
DMA2_Channel3_IRQHandler
|
||||
DMA2_Channel4_IRQHandler
|
||||
DMA2_Channel5_IRQHandler
|
||||
COMP_IRQHandler
|
||||
LPTIM1_IRQHandler
|
||||
LPTIM2_IRQHandler
|
||||
DMA2_Channel6_IRQHandler
|
||||
DMA2_Channel7_IRQHandler
|
||||
LPUART1_IRQHandler
|
||||
QUADSPI_IRQHandler
|
||||
I2C3_EV_IRQHandler
|
||||
I2C3_ER_IRQHandler
|
||||
SAI1_IRQHandler
|
||||
SWPMI1_IRQHandler
|
||||
TSC_IRQHandler
|
||||
RNG_IRQHandler
|
||||
FPU_IRQHandler
|
||||
CRS_IRQHandler
|
||||
|
||||
B .
|
||||
|
||||
ENDP
|
||||
|
||||
ALIGN
|
||||
|
||||
;*******************************************************************************
|
||||
; User Stack and Heap initialization
|
||||
;*******************************************************************************
|
||||
IF :DEF:__MICROLIB
|
||||
|
||||
EXPORT __initial_sp
|
||||
EXPORT __heap_base
|
||||
EXPORT __heap_limit
|
||||
|
||||
ELSE
|
||||
|
||||
IMPORT __use_two_region_memory
|
||||
EXPORT __user_initial_stackheap
|
||||
|
||||
__user_initial_stackheap
|
||||
|
||||
LDR R0, = Heap_Mem
|
||||
LDR R1, =(Stack_Mem + Stack_Size)
|
||||
LDR R2, = (Heap_Mem + Heap_Size)
|
||||
LDR R3, = Stack_Mem
|
||||
BX LR
|
||||
|
||||
ALIGN
|
||||
|
||||
ENDIF
|
||||
|
||||
END
|
||||
|
||||
;************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE*****
|
@@ -11,7 +11,7 @@
|
||||
/* #undef LOG_UPLOAD */
|
||||
/* #undef IOT_DEBUG */
|
||||
/* #undef DEBUG_DEV_INFO_USED */
|
||||
#define AT_TCP_ENABLED
|
||||
/* #undef AT_TCP_ENABLED */
|
||||
#define AT_UART_RECV_IRQ
|
||||
/* #undef AT_OS_USED */
|
||||
/* #undef AT_DEBUG */
|
||||
|
@@ -119,7 +119,7 @@ typedef void (*OnSubEventHandler)(void *pClient, MQTTEventType event_type, void
|
||||
*/
|
||||
typedef struct {
|
||||
QoS qos; // MQTT QoS level
|
||||
OnMessageHandler on_message_handler; // callback when message arrived
|
||||
OnMessageHandler on_message_handler; // callback when message arrived
|
||||
OnSubEventHandler on_sub_event_handler; // callback when event happened
|
||||
void *user_data; // user context for callback
|
||||
} SubscribeParams;
|
||||
@@ -160,7 +160,7 @@ typedef struct {
|
||||
/* device info */
|
||||
char *product_id; // product ID
|
||||
char *device_name; // device name
|
||||
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
char *cert_file; // cert file path
|
||||
char *key_file; // key file path
|
||||
@@ -219,7 +219,7 @@ int IOT_MQTT_Yield(void *pClient, uint32_t timeout_ms);
|
||||
/**
|
||||
* @brief Publish MQTT message
|
||||
*
|
||||
* @param pClient handle to MQTT client
|
||||
* @param pClient handle to MQTT client
|
||||
* @param topicName MQTT topic name
|
||||
* @param pParams publish parameters
|
||||
*
|
||||
@@ -230,7 +230,7 @@ int IOT_MQTT_Publish(void *pClient, char *topicName, PublishParams *pParams);
|
||||
/**
|
||||
* @brief Subscribe MQTT topic
|
||||
*
|
||||
* @param pClient handle to MQTT client
|
||||
* @param pClient handle to MQTT client
|
||||
* @param topicFilter MQTT topic filter
|
||||
* @param pParams subscribe parameters
|
||||
*
|
||||
@@ -241,7 +241,7 @@ int IOT_MQTT_Subscribe(void *pClient, char *topicFilter, SubscribeParams *pParam
|
||||
/**
|
||||
* @brief Unsubscribe MQTT topic
|
||||
*
|
||||
* @param pClient handle to MQTT client
|
||||
* @param pClient handle to MQTT client
|
||||
* @param topicFilter MQTT topic filter
|
||||
*
|
||||
* @return packet id (>=0) when success, or err code (<0) for failure
|
||||
@@ -251,7 +251,7 @@ int IOT_MQTT_Unsubscribe(void *pClient, char *topicFilter);
|
||||
/**
|
||||
* @brief Check if MQTT is connected
|
||||
*
|
||||
* @param pClient handle to MQTT client
|
||||
* @param pClient handle to MQTT client
|
||||
* @return true= connected, false = unconnected
|
||||
*/
|
||||
bool IOT_MQTT_IsConnected(void *pClient);
|
||||
|
@@ -20,9 +20,9 @@
|
||||
* Below variables are dependant on user situation (network status/device memory/application context)
|
||||
* Adjust the default value to meet your requirement
|
||||
*/
|
||||
|
||||
|
||||
/* default MQTT/CoAP timeout value when connect/pub/sub (unit: ms) */
|
||||
#define QCLOUD_IOT_MQTT_COMMAND_TIMEOUT (5 * 1000)
|
||||
#define QCLOUD_IOT_MQTT_COMMAND_TIMEOUT (15 * 1000)
|
||||
|
||||
/* default MQTT keep alive interval (unit: ms) */
|
||||
#define QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL (240 * 1000)
|
||||
@@ -57,7 +57,7 @@
|
||||
#define MAX_LOG_MSG_LEN (1023)
|
||||
#endif
|
||||
|
||||
/*
|
||||
/*
|
||||
* Log upload related params, which will affect the size of device memory/disk consumption
|
||||
* the default value can be changed for different user situation
|
||||
*/
|
||||
|
@@ -56,12 +56,16 @@ typedef struct {
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_ssl_context ssl;
|
||||
mbedtls_ssl_config ssl_conf;
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt ca_cert;
|
||||
mbedtls_x509_crt client_cert;
|
||||
#endif
|
||||
mbedtls_pk_context private_key;
|
||||
|
||||
mbedtls_timing_delay_context timer;
|
||||
#if 0
|
||||
mbedtls_ssl_cookie_ctx cookie_ctx;
|
||||
#endif
|
||||
} DTLSDataParams;
|
||||
|
||||
/**
|
||||
@@ -70,14 +74,18 @@ typedef struct {
|
||||
static void _free_mebeddtls(DTLSDataParams *pParams)
|
||||
{
|
||||
mbedtls_net_free(&(pParams->socket_fd));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_free(&(pParams->client_cert));
|
||||
mbedtls_x509_crt_free(&(pParams->ca_cert));
|
||||
mbedtls_pk_free(&(pParams->private_key));
|
||||
#endif
|
||||
mbedtls_ssl_free(&(pParams->ssl));
|
||||
mbedtls_ssl_config_free(&(pParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_free(&(pParams->ctr_drbg));
|
||||
mbedtls_entropy_free(&(pParams->entropy));
|
||||
#if 0
|
||||
mbedtls_ssl_cookie_free(&(pParams->cookie_ctx));
|
||||
#endif
|
||||
|
||||
HAL_Free(pParams);
|
||||
}
|
||||
@@ -100,9 +108,11 @@ static int _mbedtls_client_init(DTLSDataParams *pDataParams, DTLSConnectParams *
|
||||
mbedtls_net_init( &(pDataParams->socket_fd) );
|
||||
mbedtls_ssl_init( &(pDataParams->ssl) );
|
||||
mbedtls_ssl_config_init( &(pDataParams->ssl_conf) );
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_init( &(pDataParams->ca_cert) );
|
||||
mbedtls_x509_crt_init(&(pDataParams->client_cert));
|
||||
mbedtls_pk_init(&(pDataParams->private_key));
|
||||
#endif
|
||||
mbedtls_ctr_drbg_init( &(pDataParams->ctr_drbg) );
|
||||
mbedtls_entropy_init( &(pDataParams->entropy) );
|
||||
|
||||
@@ -114,26 +124,32 @@ static int _mbedtls_client_init(DTLSDataParams *pDataParams, DTLSConnectParams *
|
||||
|
||||
mbedtls_ssl_conf_authmode(&pDataParams->ssl_conf, MBEDTLS_SSL_VERIFY_NONE );
|
||||
|
||||
if (pConnectParams->ca_crt != NULL) {
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (pConnectParams->ca_crt != NULL)
|
||||
{
|
||||
if ((ret = mbedtls_x509_crt_parse(&(pDataParams->ca_cert), (const unsigned char *)pConnectParams->ca_crt,
|
||||
(pConnectParams->ca_crt_len + 1)))) {
|
||||
Log_e("parse ca crt failed returned -0x%04x", -ret);
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
if (pConnectParams->cert_file != NULL && pConnectParams->key_file != NULL) {
|
||||
if ((ret = mbedtls_x509_crt_parse_file(&(pDataParams->client_cert), pConnectParams->cert_file)) != 0) {
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if ((ret = mbedtls_x509_crt_parse_file(&(pDataParams->client_cert), pConnectParams->cert_file)) != 0) {
|
||||
Log_e("load client cert file failed returned -0x%x", ret);
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((ret = mbedtls_pk_parse_keyfile(&(pDataParams->private_key), pConnectParams->key_file, "")) != 0) {
|
||||
Log_e("load client key file failed returned -0x%x", ret);
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (0 == ret) {
|
||||
mbedtls_ssl_conf_ca_chain(&(pDataParams->ssl_conf), &(pDataParams->ca_cert), NULL);
|
||||
if ((ret = mbedtls_ssl_conf_own_cert(&(pDataParams->ssl_conf), &(pDataParams->client_cert), &(pDataParams->private_key))) != 0) {
|
||||
@@ -141,6 +157,7 @@ static int _mbedtls_client_init(DTLSDataParams *pDataParams, DTLSConnectParams *
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
Log_d("cert_file/key_file is empty!|cert_file=%s|key_file=%s", pConnectParams->cert_file, pConnectParams->key_file);
|
||||
}
|
||||
@@ -188,10 +205,12 @@ int _mbedtls_udp_connect(mbedtls_net_context *socket_fd, const char *host, int p
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
if ((ret = mbedtls_net_set_block(socket_fd)) != 0) {
|
||||
Log_e("set block faliled returned -0x%04x", -ret);
|
||||
return QCLOUD_ERR_TCP_CONNECT;
|
||||
}
|
||||
#endif
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
@@ -223,12 +242,14 @@ uintptr_t HAL_DTLS_Connect(DTLSConnectParams *pConnectParams, const char *host,
|
||||
mbedtls_ssl_conf_rng(&pDataParams->ssl_conf, mbedtls_ctr_drbg_random, &pDataParams->ctr_drbg);
|
||||
mbedtls_ssl_conf_dbg(&pDataParams->ssl_conf, _dtls_debug, NULL);
|
||||
|
||||
#if 0
|
||||
if ((ret = mbedtls_ssl_cookie_setup(&pDataParams->cookie_ctx, mbedtls_ctr_drbg_random, &pDataParams->ctr_drbg)) != 0) {
|
||||
Log_e("mbedtls_ssl_cookie_setup result 0x%04x", ret);
|
||||
goto error;
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_dtls_cookies(&pDataParams->ssl_conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check, &pDataParams->cookie_ctx);
|
||||
#endif
|
||||
|
||||
#ifndef AUTH_MODE_CERT
|
||||
mbedtls_ssl_conf_ciphersuites(&(pDataParams->ssl_conf), ciphersuites);
|
||||
@@ -255,23 +276,27 @@ uintptr_t HAL_DTLS_Connect(DTLSConnectParams *pConnectParams, const char *host,
|
||||
mbedtls_timing_get_delay);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if ((ret = mbedtls_ssl_set_hostname(&(pDataParams->ssl), host)) != 0) {
|
||||
Log_e("mbedtls_ssl_set_hostname failed returned -0x%x", -ret);
|
||||
goto error;
|
||||
}
|
||||
Log_e("mbedtls_ssl_set_hostname failed returned -0x%x", -ret);
|
||||
goto error;
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_set_bio(&(pDataParams->ssl), (void *)&pDataParams->socket_fd, mbedtls_net_send, mbedtls_net_recv,
|
||||
mbedtls_net_recv_timeout);
|
||||
|
||||
while ((ret = mbedtls_ssl_handshake(&(pDataParams->ssl))) != 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
Log_e("mbedtls_ssl_handshake failed returned -0x%x", -ret);
|
||||
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
|
||||
Log_e("Unable to verify the server's certificate");
|
||||
}
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
while ((ret = mbedtls_ssl_handshake(&(pDataParams->ssl))) != 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
Log_e("mbedtls_ssl_handshake failed returned -0x%x", -ret);
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
|
||||
Log_e("Unable to verify the server's certificate");
|
||||
}
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
if ((ret = mbedtls_ssl_get_verify_result(&(pDataParams->ssl))) != 0) {
|
||||
Log_e("mbedtls_ssl_get_verify_result failed returned -0x%x", -ret);
|
||||
@@ -299,14 +324,18 @@ void HAL_DTLS_Disconnect(uintptr_t handle)
|
||||
} while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
|
||||
|
||||
mbedtls_net_free(&(pParams->socket_fd));
|
||||
#ifdef MBEDTLS_X509_CRT_PARSE_C
|
||||
mbedtls_x509_crt_free(&(pParams->client_cert));
|
||||
mbedtls_x509_crt_free(&(pParams->ca_cert));
|
||||
mbedtls_pk_free(&(pParams->private_key));
|
||||
#endif
|
||||
mbedtls_ssl_free(&(pParams->ssl));
|
||||
mbedtls_ssl_config_free(&(pParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_free(&(pParams->ctr_drbg));
|
||||
mbedtls_entropy_free(&(pParams->entropy));
|
||||
#if 0
|
||||
mbedtls_ssl_cookie_free(&(pParams->cookie_ctx));
|
||||
#endif
|
||||
|
||||
HAL_Free((void *)handle);
|
||||
}
|
||||
|
@@ -50,8 +50,10 @@ typedef struct {
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_ssl_context ssl;
|
||||
mbedtls_ssl_config ssl_conf;
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt ca_cert;
|
||||
mbedtls_x509_crt client_cert;
|
||||
#endif
|
||||
mbedtls_pk_context private_key;
|
||||
} TLSDataParams;
|
||||
|
||||
@@ -61,9 +63,11 @@ typedef struct {
|
||||
static void _free_mebedtls(TLSDataParams *pParams)
|
||||
{
|
||||
mbedtls_net_free(&(pParams->socket_fd));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_free(&(pParams->client_cert));
|
||||
mbedtls_x509_crt_free(&(pParams->ca_cert));
|
||||
mbedtls_pk_free(&(pParams->private_key));
|
||||
#endif
|
||||
mbedtls_ssl_free(&(pParams->ssl));
|
||||
mbedtls_ssl_config_free(&(pParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_free(&(pParams->ctr_drbg));
|
||||
@@ -91,9 +95,11 @@ static int _mbedtls_client_init(TLSDataParams *pDataParams, TLSConnectParams *pC
|
||||
mbedtls_ssl_init(&(pDataParams->ssl));
|
||||
mbedtls_ssl_config_init(&(pDataParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_init(&(pDataParams->ctr_drbg));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_init(&(pDataParams->ca_cert));
|
||||
mbedtls_x509_crt_init(&(pDataParams->client_cert));
|
||||
mbedtls_pk_init(&(pDataParams->private_key));
|
||||
#endif
|
||||
|
||||
mbedtls_entropy_init(&(pDataParams->entropy));
|
||||
// custom parameter is NULL for now
|
||||
@@ -103,20 +109,25 @@ static int _mbedtls_client_init(TLSDataParams *pDataParams, TLSConnectParams *pC
|
||||
return QCLOUD_ERR_SSL_INIT;
|
||||
}
|
||||
|
||||
if (pConnectParams->ca_crt != NULL) {
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (pConnectParams->ca_crt != NULL)
|
||||
{
|
||||
if ((ret = mbedtls_x509_crt_parse(&(pDataParams->ca_cert), (const unsigned char *)pConnectParams->ca_crt,
|
||||
(pConnectParams->ca_crt_len + 1)))) {
|
||||
Log_e("parse ca crt failed returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
if (pConnectParams->cert_file != NULL && pConnectParams->key_file != NULL) {
|
||||
if ((ret = mbedtls_x509_crt_parse_file(&(pDataParams->client_cert), pConnectParams->cert_file)) != 0) {
|
||||
Log_e("load client cert file failed returned 0x%x", ret < 0 ? -ret : ret);
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if ((ret = mbedtls_x509_crt_parse_file(&(pDataParams->client_cert), pConnectParams->cert_file)) != 0) {
|
||||
Log_e("load client cert file failed returned 0x%x", ret<0?-ret:ret);
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((ret = mbedtls_pk_parse_keyfile(&(pDataParams->private_key), pConnectParams->key_file, "")) != 0) {
|
||||
Log_e("load client key file failed returned 0x%x", ret < 0 ? -ret : ret);
|
||||
@@ -171,10 +182,12 @@ int _mbedtls_tcp_connect(mbedtls_net_context *socket_fd, const char *host, int p
|
||||
|
||||
}
|
||||
|
||||
#if 0
|
||||
if ((ret = mbedtls_net_set_block(socket_fd)) != 0) {
|
||||
Log_e("set block faliled returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
return QCLOUD_ERR_TCP_CONNECT;
|
||||
}
|
||||
#endif
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
@@ -186,10 +199,11 @@ int _mbedtls_tcp_connect(mbedtls_net_context *socket_fd, const char *host, int p
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
int _qcloud_server_certificate_verify(void *hostname, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
|
||||
{
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
int _qcloud_server_certificate_verify(void *hostname, mbedtls_x509_crt *crt, int depth, uint32_t *flags) {
|
||||
return *flags;
|
||||
}
|
||||
#endif
|
||||
|
||||
uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, int port)
|
||||
{
|
||||
@@ -210,18 +224,22 @@ uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, in
|
||||
goto error;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_ssl_conf_verify(&(pDataParams->ssl_conf), _qcloud_server_certificate_verify, (void *)host);
|
||||
|
||||
mbedtls_ssl_conf_authmode(&(pDataParams->ssl_conf), MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_conf_rng(&(pDataParams->ssl_conf), mbedtls_ctr_drbg_random, &(pDataParams->ctr_drbg));
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_ssl_conf_ca_chain(&(pDataParams->ssl_conf), &(pDataParams->ca_cert), NULL);
|
||||
if ((ret = mbedtls_ssl_conf_own_cert(&(pDataParams->ssl_conf),
|
||||
&(pDataParams->client_cert), &(pDataParams->private_key))) != 0) {
|
||||
Log_e("mbedtls_ssl_conf_own_cert failed returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
goto error;
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_conf_read_timeout(&(pDataParams->ssl_conf), pConnectParams->timeout_ms);
|
||||
if ((ret = mbedtls_ssl_setup(&(pDataParams->ssl), &(pDataParams->ssl_conf))) != 0) {
|
||||
@@ -236,11 +254,13 @@ uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, in
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
// Set the hostname to check against the received server certificate and sni
|
||||
if ((ret = mbedtls_ssl_set_hostname(&(pDataParams->ssl), host)) != 0) {
|
||||
Log_e("mbedtls_ssl_set_hostname failed returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
goto error;
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_set_bio(&(pDataParams->ssl), &(pDataParams->socket_fd), mbedtls_net_send, mbedtls_net_recv,
|
||||
mbedtls_net_recv_timeout);
|
||||
@@ -253,10 +273,13 @@ uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, in
|
||||
|
||||
while ((ret = mbedtls_ssl_handshake(&(pDataParams->ssl))) != 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
Log_e("mbedtls_ssl_handshake failed returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
Log_e("mbedtls_ssl_handshake failed returned 0x%04x", ret<0?-ret:ret);
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
|
||||
Log_e("Unable to verify the server's certificate");
|
||||
}
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -290,9 +313,11 @@ void HAL_TLS_Disconnect(uintptr_t handle)
|
||||
} while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
|
||||
|
||||
mbedtls_net_free(&(pParams->socket_fd));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_free(&(pParams->client_cert));
|
||||
mbedtls_x509_crt_free(&(pParams->ca_cert));
|
||||
mbedtls_pk_free(&(pParams->private_key));
|
||||
#endif
|
||||
mbedtls_ssl_free(&(pParams->ssl));
|
||||
mbedtls_ssl_config_free(&(pParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_free(&(pParams->ctr_drbg));
|
||||
|
@@ -20,7 +20,7 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <sys/types.h>
|
||||
// #include <sys/types.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "qcloud_iot_export_ota.h"
|
||||
|
@@ -38,8 +38,6 @@ static UART_HandleTypeDef *pAtUart = &huart1;
|
||||
extern void AT_Uart_Init(void);
|
||||
extern void at_client_uart_rx_isr_cb(uint8_t *pdata, uint8_t len);
|
||||
|
||||
|
||||
#include "board.h"
|
||||
/**
|
||||
* @brief This function handles AT UART global interrupt,push recv char to ringbuff.
|
||||
*/
|
||||
@@ -51,7 +49,6 @@ void HAL_AT_UART_IRQHandler(void)
|
||||
ch = (uint8_t) READ_REG(pAtUart->Instance->RDR)&0xFF;
|
||||
/*this callback for at_client*/
|
||||
at_client_uart_rx_isr_cb(&ch, 1);
|
||||
HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
|
||||
}
|
||||
__HAL_UART_CLEAR_PEFLAG(pAtUart);
|
||||
}
|
||||
|
@@ -55,12 +55,16 @@ typedef struct {
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_ssl_context ssl;
|
||||
mbedtls_ssl_config ssl_conf;
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt ca_cert;
|
||||
mbedtls_x509_crt client_cert;
|
||||
#endif
|
||||
mbedtls_pk_context private_key;
|
||||
|
||||
mbedtls_timing_delay_context timer;
|
||||
#if 0
|
||||
mbedtls_ssl_cookie_ctx cookie_ctx;
|
||||
#endif
|
||||
} DTLSDataParams;
|
||||
|
||||
/**
|
||||
@@ -69,14 +73,18 @@ typedef struct {
|
||||
static void _free_mebeddtls(DTLSDataParams *pParams)
|
||||
{
|
||||
mbedtls_net_free(&(pParams->socket_fd));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_free(&(pParams->client_cert));
|
||||
mbedtls_x509_crt_free(&(pParams->ca_cert));
|
||||
mbedtls_pk_free(&(pParams->private_key));
|
||||
#endif
|
||||
mbedtls_ssl_free(&(pParams->ssl));
|
||||
mbedtls_ssl_config_free(&(pParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_free(&(pParams->ctr_drbg));
|
||||
mbedtls_entropy_free(&(pParams->entropy));
|
||||
#if 0
|
||||
mbedtls_ssl_cookie_free(&(pParams->cookie_ctx));
|
||||
#endif
|
||||
|
||||
HAL_Free(pParams);
|
||||
}
|
||||
@@ -97,11 +105,13 @@ static int _mbedtls_client_init(DTLSDataParams *pDataParams, DTLSConnectParams *
|
||||
mbedtls_net_init(&(pDataParams->socket_fd));
|
||||
mbedtls_ssl_init(&(pDataParams->ssl));
|
||||
mbedtls_ssl_config_init(&(pDataParams->ssl_conf));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_init(&(pDataParams->ca_cert));
|
||||
mbedtls_x509_crt_init(&(pDataParams->client_cert));
|
||||
mbedtls_pk_init(&(pDataParams->private_key));
|
||||
mbedtls_ctr_drbg_init(&(pDataParams->ctr_drbg));
|
||||
mbedtls_entropy_init(&(pDataParams->entropy));
|
||||
#endif
|
||||
mbedtls_ctr_drbg_init( &(pDataParams->ctr_drbg) );
|
||||
mbedtls_entropy_init( &(pDataParams->entropy) );
|
||||
|
||||
if ((ret = mbedtls_ctr_drbg_seed(&pDataParams->ctr_drbg, mbedtls_entropy_func, &pDataParams->entropy, NULL, 0)) !=
|
||||
0) {
|
||||
@@ -111,6 +121,7 @@ static int _mbedtls_client_init(DTLSDataParams *pDataParams, DTLSConnectParams *
|
||||
|
||||
mbedtls_ssl_conf_authmode(&pDataParams->ssl_conf, MBEDTLS_SSL_VERIFY_NONE);
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (pConnectParams->ca_crt != NULL) {
|
||||
if ((ret = mbedtls_x509_crt_parse(&(pDataParams->ca_cert), (const unsigned char *)pConnectParams->ca_crt,
|
||||
(pConnectParams->ca_crt_len + 1)))) {
|
||||
@@ -118,19 +129,23 @@ static int _mbedtls_client_init(DTLSDataParams *pDataParams, DTLSConnectParams *
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
if (pConnectParams->cert_file != NULL && pConnectParams->key_file != NULL) {
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if ((ret = mbedtls_x509_crt_parse_file(&(pDataParams->client_cert), pConnectParams->cert_file)) != 0) {
|
||||
Log_e("load client cert file failed returned -0x%x", ret);
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((ret = mbedtls_pk_parse_keyfile(&(pDataParams->private_key), pConnectParams->key_file, "")) != 0) {
|
||||
Log_e("load client key file failed returned -0x%x", ret);
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (0 == ret) {
|
||||
mbedtls_ssl_conf_ca_chain(&(pDataParams->ssl_conf), &(pDataParams->ca_cert), NULL);
|
||||
if ((ret = mbedtls_ssl_conf_own_cert(&(pDataParams->ssl_conf), &(pDataParams->client_cert),
|
||||
@@ -139,6 +154,7 @@ static int _mbedtls_client_init(DTLSDataParams *pDataParams, DTLSConnectParams *
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
Log_d("cert_file/key_file is empty!|cert_file=%s|key_file=%s", pConnectParams->cert_file,
|
||||
pConnectParams->key_file);
|
||||
@@ -187,10 +203,12 @@ int _mbedtls_udp_connect(mbedtls_net_context *socket_fd, const char *host, int p
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
if ((ret = mbedtls_net_set_block(socket_fd)) != 0) {
|
||||
Log_e("set block faliled returned -0x%04x", -ret);
|
||||
return QCLOUD_ERR_TCP_CONNECT;
|
||||
}
|
||||
#endif
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
@@ -222,6 +240,7 @@ uintptr_t HAL_DTLS_Connect(DTLSConnectParams *pConnectParams, const char *host,
|
||||
mbedtls_ssl_conf_rng(&pDataParams->ssl_conf, mbedtls_ctr_drbg_random, &pDataParams->ctr_drbg);
|
||||
mbedtls_ssl_conf_dbg(&pDataParams->ssl_conf, _dtls_debug, NULL);
|
||||
|
||||
#if 0
|
||||
if ((ret = mbedtls_ssl_cookie_setup(&pDataParams->cookie_ctx, mbedtls_ctr_drbg_random, &pDataParams->ctr_drbg)) !=
|
||||
0) {
|
||||
Log_e("mbedtls_ssl_cookie_setup result 0x%04x", ret);
|
||||
@@ -229,7 +248,7 @@ uintptr_t HAL_DTLS_Connect(DTLSConnectParams *pConnectParams, const char *host,
|
||||
}
|
||||
|
||||
mbedtls_ssl_conf_dtls_cookies(&pDataParams->ssl_conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
|
||||
&pDataParams->cookie_ctx);
|
||||
#endif
|
||||
|
||||
#ifndef AUTH_MODE_CERT
|
||||
mbedtls_ssl_conf_ciphersuites(&(pDataParams->ssl_conf), ciphersuites);
|
||||
@@ -256,10 +275,12 @@ uintptr_t HAL_DTLS_Connect(DTLSConnectParams *pConnectParams, const char *host,
|
||||
mbedtls_timing_get_delay);
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if ((ret = mbedtls_ssl_set_hostname(&(pDataParams->ssl), host)) != 0) {
|
||||
Log_e("mbedtls_ssl_set_hostname failed returned -0x%x", -ret);
|
||||
goto error;
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_set_bio(&(pDataParams->ssl), (void *)&pDataParams->socket_fd, mbedtls_net_send, mbedtls_net_recv,
|
||||
mbedtls_net_recv_timeout);
|
||||
@@ -267,9 +288,11 @@ uintptr_t HAL_DTLS_Connect(DTLSConnectParams *pConnectParams, const char *host,
|
||||
while ((ret = mbedtls_ssl_handshake(&(pDataParams->ssl))) != 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
Log_e("mbedtls_ssl_handshake failed returned -0x%x", -ret);
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
|
||||
Log_e("Unable to verify the server's certificate");
|
||||
}
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -300,14 +323,18 @@ void HAL_DTLS_Disconnect(uintptr_t handle)
|
||||
} while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
|
||||
|
||||
mbedtls_net_free(&(pParams->socket_fd));
|
||||
#ifdef MBEDTLS_X509_CRT_PARSE_C
|
||||
mbedtls_x509_crt_free(&(pParams->client_cert));
|
||||
mbedtls_x509_crt_free(&(pParams->ca_cert));
|
||||
mbedtls_pk_free(&(pParams->private_key));
|
||||
#endif
|
||||
mbedtls_ssl_free(&(pParams->ssl));
|
||||
mbedtls_ssl_config_free(&(pParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_free(&(pParams->ctr_drbg));
|
||||
mbedtls_entropy_free(&(pParams->entropy));
|
||||
#if 0
|
||||
mbedtls_ssl_cookie_free(&(pParams->cookie_ctx));
|
||||
#endif
|
||||
|
||||
HAL_Free((void *)handle);
|
||||
}
|
||||
|
@@ -48,8 +48,10 @@ typedef struct {
|
||||
mbedtls_ctr_drbg_context ctr_drbg;
|
||||
mbedtls_ssl_context ssl;
|
||||
mbedtls_ssl_config ssl_conf;
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt ca_cert;
|
||||
mbedtls_x509_crt client_cert;
|
||||
#endif
|
||||
mbedtls_pk_context private_key;
|
||||
} TLSDataParams;
|
||||
|
||||
@@ -59,9 +61,11 @@ typedef struct {
|
||||
static void _free_mebedtls(TLSDataParams *pParams)
|
||||
{
|
||||
mbedtls_net_free(&(pParams->socket_fd));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_free(&(pParams->client_cert));
|
||||
mbedtls_x509_crt_free(&(pParams->ca_cert));
|
||||
mbedtls_pk_free(&(pParams->private_key));
|
||||
#endif
|
||||
mbedtls_ssl_free(&(pParams->ssl));
|
||||
mbedtls_ssl_config_free(&(pParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_free(&(pParams->ctr_drbg));
|
||||
@@ -88,9 +92,11 @@ static int _mbedtls_client_init(TLSDataParams *pDataParams, TLSConnectParams *pC
|
||||
mbedtls_ssl_init(&(pDataParams->ssl));
|
||||
mbedtls_ssl_config_init(&(pDataParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_init(&(pDataParams->ctr_drbg));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_init(&(pDataParams->ca_cert));
|
||||
mbedtls_x509_crt_init(&(pDataParams->client_cert));
|
||||
mbedtls_pk_init(&(pDataParams->private_key));
|
||||
#endif
|
||||
|
||||
mbedtls_entropy_init(&(pDataParams->entropy));
|
||||
// custom parameter is NULL for now
|
||||
@@ -100,6 +106,7 @@ static int _mbedtls_client_init(TLSDataParams *pDataParams, TLSConnectParams *pC
|
||||
return QCLOUD_ERR_SSL_INIT;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (pConnectParams->ca_crt != NULL) {
|
||||
if ((ret = mbedtls_x509_crt_parse(&(pDataParams->ca_cert), (const unsigned char *)pConnectParams->ca_crt,
|
||||
(pConnectParams->ca_crt_len + 1)))) {
|
||||
@@ -107,13 +114,16 @@ static int _mbedtls_client_init(TLSDataParams *pDataParams, TLSConnectParams *pC
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
if (pConnectParams->cert_file != NULL && pConnectParams->key_file != NULL) {
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if ((ret = mbedtls_x509_crt_parse_file(&(pDataParams->client_cert), pConnectParams->cert_file)) != 0) {
|
||||
Log_e("load client cert file failed returned 0x%x", ret < 0 ? -ret : ret);
|
||||
return QCLOUD_ERR_SSL_CERT;
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((ret = mbedtls_pk_parse_keyfile(&(pDataParams->private_key), pConnectParams->key_file, "")) != 0) {
|
||||
Log_e("load client key file failed returned 0x%x", ret < 0 ? -ret : ret);
|
||||
@@ -167,10 +177,12 @@ int _mbedtls_tcp_connect(mbedtls_net_context *socket_fd, const char *host, int p
|
||||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
if ((ret = mbedtls_net_set_block(socket_fd)) != 0) {
|
||||
Log_e("set block faliled returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
return QCLOUD_ERR_TCP_CONNECT;
|
||||
}
|
||||
#endif
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
@@ -182,10 +194,12 @@ int _mbedtls_tcp_connect(mbedtls_net_context *socket_fd, const char *host, int p
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
int _qcloud_server_certificate_verify(void *hostname, mbedtls_x509_crt *crt, int depth, uint32_t *flags)
|
||||
{
|
||||
return *flags;
|
||||
}
|
||||
#endif
|
||||
|
||||
uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, int port)
|
||||
{
|
||||
@@ -204,18 +218,22 @@ uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, in
|
||||
goto error;
|
||||
}
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_ssl_conf_verify(&(pDataParams->ssl_conf), _qcloud_server_certificate_verify, (void *)host);
|
||||
|
||||
mbedtls_ssl_conf_authmode(&(pDataParams->ssl_conf), MBEDTLS_SSL_VERIFY_REQUIRED);
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_conf_rng(&(pDataParams->ssl_conf), mbedtls_ctr_drbg_random, &(pDataParams->ctr_drbg));
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_ssl_conf_ca_chain(&(pDataParams->ssl_conf), &(pDataParams->ca_cert), NULL);
|
||||
if ((ret = mbedtls_ssl_conf_own_cert(&(pDataParams->ssl_conf), &(pDataParams->client_cert),
|
||||
&(pDataParams->private_key))) != 0) {
|
||||
Log_e("mbedtls_ssl_conf_own_cert failed returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
goto error;
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_conf_read_timeout(&(pDataParams->ssl_conf), pConnectParams->timeout_ms);
|
||||
if ((ret = mbedtls_ssl_setup(&(pDataParams->ssl), &(pDataParams->ssl_conf))) != 0) {
|
||||
@@ -230,11 +248,13 @@ uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, in
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
// Set the hostname to check against the received server certificate and sni
|
||||
if ((ret = mbedtls_ssl_set_hostname(&(pDataParams->ssl), host)) != 0) {
|
||||
Log_e("mbedtls_ssl_set_hostname failed returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
goto error;
|
||||
}
|
||||
#endif
|
||||
|
||||
mbedtls_ssl_set_bio(&(pDataParams->ssl), &(pDataParams->socket_fd), mbedtls_net_send, mbedtls_net_recv,
|
||||
mbedtls_net_recv_timeout);
|
||||
@@ -248,9 +268,11 @@ uintptr_t HAL_TLS_Connect(TLSConnectParams *pConnectParams, const char *host, in
|
||||
while ((ret = mbedtls_ssl_handshake(&(pDataParams->ssl))) != 0) {
|
||||
if (ret != MBEDTLS_ERR_SSL_WANT_READ && ret != MBEDTLS_ERR_SSL_WANT_WRITE) {
|
||||
Log_e("mbedtls_ssl_handshake failed returned 0x%04x", ret < 0 ? -ret : ret);
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
if (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED) {
|
||||
Log_e("Unable to verify the server's certificate");
|
||||
}
|
||||
#endif
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
@@ -284,9 +306,11 @@ void HAL_TLS_Disconnect(uintptr_t handle)
|
||||
} while (ret == MBEDTLS_ERR_SSL_WANT_READ || ret == MBEDTLS_ERR_SSL_WANT_WRITE);
|
||||
|
||||
mbedtls_net_free(&(pParams->socket_fd));
|
||||
#if defined(MBEDTLS_X509_CRT_PARSE_C)
|
||||
mbedtls_x509_crt_free(&(pParams->client_cert));
|
||||
mbedtls_x509_crt_free(&(pParams->ca_cert));
|
||||
mbedtls_pk_free(&(pParams->private_key));
|
||||
#endif
|
||||
mbedtls_ssl_free(&(pParams->ssl));
|
||||
mbedtls_ssl_config_free(&(pParams->ssl_conf));
|
||||
mbedtls_ctr_drbg_free(&(pParams->ctr_drbg));
|
||||
|
@@ -15,21 +15,19 @@
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
#include "qcloud_iot_import.h"
|
||||
#include "qcloud_iot_export.h"
|
||||
#include "qcloud_iot_import.h"
|
||||
|
||||
#ifdef AT_TCP_ENABLED
|
||||
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "utils_ringbuff.h"
|
||||
#include "at_client.h"
|
||||
|
||||
|
||||
#include "stm32l4xx_hal.h"
|
||||
#include "utils_ringbuff.h"
|
||||
|
||||
#define HAL_AT_UART_IRQHandler USART1_IRQHandler
|
||||
extern UART_HandleTypeDef huart1;
|
||||
|
@@ -437,7 +437,7 @@ int data_template_light_thread(void) {
|
||||
return rc;
|
||||
}
|
||||
|
||||
void *client = IOT_Template_Construct(&init_params);
|
||||
void *client = IOT_Template_Construct(&init_params, NULL);
|
||||
if (client != NULL) {
|
||||
Log_i("Cloud Device Construct Success");
|
||||
} else {
|
||||
|
@@ -39,7 +39,7 @@ void application_entry(void *arg)
|
||||
data_template_light_thread();
|
||||
|
||||
while (1) {
|
||||
printf("This is a tencent cloud sdk data template demo!\r\n");
|
||||
printf("This is a qcloud explorer sdk data template demo!\r\n");
|
||||
tos_task_delay(1000);
|
||||
}
|
||||
}
|
46
examples/qcloud_iot_explorer_sdk_ota/entry.c
Normal file
46
examples/qcloud_iot_explorer_sdk_ota/entry.c
Normal file
@@ -0,0 +1,46 @@
|
||||
#include "tos_k.h"
|
||||
|
||||
/* 用户根据自己的底层通信链路来配置此宏
|
||||
* 如果是基于以太网lwip的链路,这里应该定义 USE_LWIP
|
||||
* 如果是基于模组的通信链路,这里应该定义相应的模组宏,如使用ESP8266则定义 USE_ESP8266
|
||||
* */
|
||||
#define USE_ESP8266
|
||||
|
||||
#ifdef USE_LWIP
|
||||
#include "lwip/api.h"
|
||||
#include "lwip/sockets.h"
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sys.h"
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
#include "esp8266.h"
|
||||
#endif
|
||||
|
||||
void application_entry(void *arg)
|
||||
{
|
||||
#ifdef USE_LWIP
|
||||
dns_init();
|
||||
MX_LWIP_Init();
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
extern int esp8266_sal_init(hal_uart_port_t uart_port);
|
||||
extern int esp8266_join_ap(const char *ssid, const char *pwd);
|
||||
esp8266_sal_init(HAL_UART_PORT_0);
|
||||
esp8266_join_ap("SheldonDai", "srnr6x9xbhmb0");
|
||||
#endif
|
||||
|
||||
#ifdef USE_NB_BC35
|
||||
extern int bc35_28_95_sal_init(hal_uart_port_t uart_port);
|
||||
bc35_28_95_sal_init(HAL_UART_PORT_0);
|
||||
#endif
|
||||
|
||||
ota_thread();
|
||||
|
||||
while (1) {
|
||||
printf("This is a qcloud explorer sdk ota demo!\r\n");
|
||||
tos_task_delay(1000);
|
||||
}
|
||||
}
|
||||
|
522
examples/qcloud_iot_explorer_sdk_ota/ota_mqtt_sample.c
Normal file
522
examples/qcloud_iot_explorer_sdk_ota/ota_mqtt_sample.c
Normal file
@@ -0,0 +1,522 @@
|
||||
/*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
|
||||
|
||||
* Licensed under the MIT License (the "License"); you may not use this file except in
|
||||
* compliance with the License. You may obtain a copy of the License at
|
||||
* http://opensource.org/licenses/MIT
|
||||
|
||||
* 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.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "qcloud_iot_export.h"
|
||||
#include "qcloud_iot_import.h"
|
||||
#include "lite-utils.h"
|
||||
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
static char sg_cert_file[PATH_MAX + 1]; // full path of device cert file
|
||||
static char sg_key_file[PATH_MAX + 1]; // full path of device key file
|
||||
#endif
|
||||
|
||||
|
||||
static DeviceInfo sg_devInfo;
|
||||
|
||||
|
||||
#define OTA_BUF_LEN (5000)
|
||||
|
||||
static bool sg_pub_ack = false;
|
||||
static int sg_packet_id = 0;
|
||||
|
||||
static void event_handler(void *pclient, void *handle_context, MQTTEventMsg *msg)
|
||||
{
|
||||
uintptr_t packet_id = (uintptr_t)msg->msg;
|
||||
|
||||
switch (msg->event_type) {
|
||||
case MQTT_EVENT_UNDEF:
|
||||
Log_i("undefined event occur.");
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_DISCONNECT:
|
||||
Log_i("MQTT disconnect.");
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_RECONNECT:
|
||||
Log_i("MQTT reconnect.");
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBCRIBE_SUCCESS:
|
||||
Log_i("subscribe success, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBCRIBE_TIMEOUT:
|
||||
Log_i("subscribe wait ack timeout, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBCRIBE_NACK:
|
||||
Log_i("subscribe nack, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_PUBLISH_SUCCESS:
|
||||
Log_i("publish success, packet-id=%u", (unsigned int)packet_id);
|
||||
if (sg_packet_id == packet_id)
|
||||
sg_pub_ack = true;
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_PUBLISH_TIMEOUT:
|
||||
Log_i("publish timeout, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_PUBLISH_NACK:
|
||||
Log_i("publish nack, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
default:
|
||||
Log_i("Should NOT arrive here.");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* demo of firmware info management in device side for resuming download from break point */
|
||||
#define VERSION_FILE_PATH "./local_fw_info.json"
|
||||
#define KEY_VER "version"
|
||||
#define KEY_MD5 "md5"
|
||||
#define KEY_SIZE "downloadSize"
|
||||
#define KEY_STATE "state"
|
||||
#define KEY_PREVER "running_version"
|
||||
static char * get_local_fw_version(char **ver, char **md5, char **size)
|
||||
{
|
||||
#define INFO_FILE_MAX_LEN 256
|
||||
|
||||
FILE *fp;
|
||||
int len;
|
||||
int rlen;
|
||||
char *preVer;
|
||||
char *reportVer = NULL;
|
||||
|
||||
fp = fopen(VERSION_FILE_PATH, "r");
|
||||
if (NULL == fp) {
|
||||
Log_e("open file %s failed", VERSION_FILE_PATH);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
fseek(fp, 0L, SEEK_END);
|
||||
len = ftell(fp);
|
||||
if (len > INFO_FILE_MAX_LEN) {
|
||||
Log_e("%s is too big, pls check", VERSION_FILE_PATH);
|
||||
goto exit;
|
||||
}
|
||||
|
||||
char *JsonDoc = (char *)HAL_Malloc(len + 10);
|
||||
if (NULL == JsonDoc) {
|
||||
Log_e("malloc buffer for json file read fail");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
rewind(fp);
|
||||
rlen = fread(JsonDoc, 1, len, fp);
|
||||
|
||||
if (len != rlen) {
|
||||
Log_e("read data len (%d) less than needed (%d), %s", rlen, len, JsonDoc);
|
||||
}
|
||||
|
||||
*ver = LITE_json_value_of(KEY_VER, JsonDoc);
|
||||
*md5 = LITE_json_value_of(KEY_MD5, JsonDoc);
|
||||
*size = LITE_json_value_of(KEY_SIZE, JsonDoc);
|
||||
preVer = LITE_json_value_of(KEY_PREVER, JsonDoc);
|
||||
|
||||
if ((NULL != *ver) && (NULL != preVer) && (0 == strcmp(*ver, preVer))) {
|
||||
reportVer = *ver;
|
||||
HAL_Free(preVer);
|
||||
} else {
|
||||
reportVer = preVer;
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
if (NULL != fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
return reportVer;
|
||||
#undef INFO_FILE_MAX_LEN
|
||||
}
|
||||
|
||||
/* update local firmware info for resuming download from break point */
|
||||
static int update_local_fw_info(const char *version, const char *preVer, const char *md5, uint32_t downloadedSize)
|
||||
{
|
||||
#define INFO_FILE_MAX_LEN 256
|
||||
|
||||
FILE *fp;
|
||||
int wlen;
|
||||
int ret = QCLOUD_RET_SUCCESS;
|
||||
char dataBuff[INFO_FILE_MAX_LEN];
|
||||
|
||||
memset(dataBuff, 0, INFO_FILE_MAX_LEN);
|
||||
HAL_Snprintf(dataBuff, INFO_FILE_MAX_LEN, "{\"%s\":\"%s\", \"%s\":\"%s\",\"%s\":%d,\"%s\":\"%s\"}", \
|
||||
KEY_VER, version, KEY_MD5, md5, KEY_SIZE, downloadedSize, \
|
||||
KEY_PREVER, (NULL == preVer) ? "1.0.0" : preVer);
|
||||
|
||||
fp = fopen(VERSION_FILE_PATH, "w");
|
||||
if (NULL == fp) {
|
||||
Log_e("open file %s failed", VERSION_FILE_PATH);
|
||||
ret = QCLOUD_ERR_FAILURE;
|
||||
goto exit;
|
||||
}
|
||||
|
||||
wlen = fwrite(dataBuff, 1, strlen(dataBuff), fp);
|
||||
if (wlen != strlen(dataBuff)) {
|
||||
Log_e("save version to file err");
|
||||
ret = QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
if (NULL != fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
return ret;
|
||||
#undef INFO_FILE_MAX_LEN
|
||||
}
|
||||
|
||||
/* get local firmware offset for resuming download from break point */
|
||||
static int getFwOffset(void *h_ota, char *local_ver, char *local_md5, char *local_size, uint32_t *offset, uint32_t *size_file)
|
||||
{
|
||||
char version[128], md5sum[33];
|
||||
uint32_t local_len;
|
||||
int Ret;
|
||||
|
||||
Ret = IOT_OTA_Ioctl(h_ota, IOT_OTAG_VERSION, version, 128);
|
||||
Ret |= IOT_OTA_Ioctl(h_ota, IOT_OTAG_MD5SUM, md5sum, 33);
|
||||
Ret |= IOT_OTA_Ioctl(h_ota, IOT_OTAG_FILE_SIZE, size_file, 4);
|
||||
|
||||
local_len = (NULL == local_size) ? 0 : atoi(local_size);
|
||||
|
||||
|
||||
if ((NULL == local_ver) || (NULL == local_md5) || (NULL == local_size)) {
|
||||
*offset = 0;
|
||||
} else if ((0 != strcmp(local_ver, version)) || (0 != strcmp(local_md5, md5sum)) || (local_len > *size_file)) {
|
||||
*offset = 0;
|
||||
} else {
|
||||
*offset = local_len;
|
||||
}
|
||||
|
||||
return Ret;
|
||||
}
|
||||
|
||||
/* calculate left MD5 for resuming download from break point */
|
||||
static int cal_exist_fw_md5(void *h_ota, FILE *fp, size_t size)
|
||||
{
|
||||
#define BUFF_LEN 1024
|
||||
|
||||
char buff[BUFF_LEN];
|
||||
size_t rlen;
|
||||
int Ret = QCLOUD_RET_SUCCESS;
|
||||
|
||||
while ((size > 0) && (!feof(fp))) {
|
||||
rlen = (size > BUFF_LEN) ? BUFF_LEN : size;
|
||||
if (rlen != fread(buff, 1, rlen, fp)) {
|
||||
Log_e("read data len not expected");
|
||||
Ret = QCLOUD_ERR_FAILURE;
|
||||
break;
|
||||
}
|
||||
IOT_OTA_UpdateClientMd5(h_ota, buff, rlen);
|
||||
size -= rlen;
|
||||
}
|
||||
return Ret;
|
||||
#undef BUFF_LEN
|
||||
}
|
||||
|
||||
static int _setup_connect_init_params(MQTTInitParams* initParams)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = HAL_GetDevInfo((void *)&sg_devInfo);
|
||||
if (QCLOUD_RET_SUCCESS != ret) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
initParams->device_name = sg_devInfo.device_name;
|
||||
initParams->product_id = sg_devInfo.product_id;
|
||||
|
||||
#ifdef AUTH_MODE_CERT
|
||||
char certs_dir[PATH_MAX + 1] = "certs";
|
||||
char current_path[PATH_MAX + 1];
|
||||
char *cwd = getcwd(current_path, sizeof(current_path));
|
||||
if (cwd == NULL) {
|
||||
Log_e("getcwd return NULL");
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
sprintf(sg_cert_file, "%s/%s/%s", current_path, certs_dir, sg_devInfo.dev_cert_file_name);
|
||||
sprintf(sg_key_file, "%s/%s/%s", current_path, certs_dir, sg_devInfo.dev_key_file_name);
|
||||
|
||||
initParams->cert_file = sg_cert_file;
|
||||
initParams->key_file = sg_key_file;
|
||||
#else
|
||||
initParams->device_secret = sg_devInfo.device_secret;
|
||||
#endif
|
||||
|
||||
|
||||
initParams->command_timeout = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
||||
initParams->keep_alive_interval_ms = QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL;
|
||||
initParams->auto_connect_enable = 1;
|
||||
initParams->event_handle.h_fp = event_handler;
|
||||
|
||||
return QCLOUD_RET_SUCCESS;
|
||||
}
|
||||
|
||||
int ota_thread(int argc, char **argv)
|
||||
{
|
||||
IOT_Log_Set_Level(eLOG_DEBUG);
|
||||
int rc;
|
||||
uint32_t firmware_valid;
|
||||
static char version[128], md5sum[33];
|
||||
uint32_t len, size_downloaded, size_file;
|
||||
|
||||
int ota_over = 0;
|
||||
bool upgrade_fetch_success = true;
|
||||
static char buf_ota[OTA_BUF_LEN];
|
||||
FILE *fp = NULL;
|
||||
uint32_t offset = 0;
|
||||
|
||||
|
||||
MQTTInitParams init_params = DEFAULT_MQTTINIT_PARAMS;
|
||||
rc = _setup_connect_init_params(&init_params);
|
||||
if (rc != QCLOUD_RET_SUCCESS) {
|
||||
Log_e("init params err,rc=%d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void *client = IOT_MQTT_Construct(&init_params);
|
||||
if (client != NULL) {
|
||||
Log_i("Cloud Device Construct Success");
|
||||
} else {
|
||||
Log_e("Cloud Device Construct Failed");
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
|
||||
void *h_ota = IOT_OTA_Init(sg_devInfo.product_id, sg_devInfo.device_name, client);
|
||||
if (NULL == h_ota) {
|
||||
Log_e("initialize OTA failed");
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
|
||||
IOT_MQTT_Yield(client, 1000); //make sure subscribe success
|
||||
|
||||
char *local_ver = NULL, *local_md5 = NULL, *local_size = NULL, *reportVer = NULL;
|
||||
reportVer = get_local_fw_version(&local_ver, &local_md5, &local_size);
|
||||
Log_d("local_ver:%s local_md5:%s, local_size:%s", local_ver, local_md5, local_size);
|
||||
|
||||
/* Must report version first */
|
||||
if (0 > IOT_OTA_ReportVersion(h_ota, (NULL == reportVer) ? "1.0.0" : reportVer)) {
|
||||
Log_e("report OTA version failed");
|
||||
goto exit;
|
||||
}
|
||||
|
||||
do {
|
||||
Log_i("wait for ota upgrade command...");
|
||||
|
||||
IOT_MQTT_Yield(client, 200);
|
||||
|
||||
if (IOT_OTA_IsFetching(h_ota)) {
|
||||
|
||||
/*check pre-download finished or not*/
|
||||
/*if version & MD5 is the same,then pre-download not finished,get breakpoint continue download.otherwise the version is new*/
|
||||
rc = getFwOffset(h_ota, local_ver, local_md5, local_size, &offset, &size_file);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
Log_e("get fw offset err,rc:%d", rc);
|
||||
upgrade_fetch_success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
//finish download but not report state
|
||||
if(offset == size_file) {
|
||||
Log_d("download success last time without report!");
|
||||
upgrade_fetch_success = true;
|
||||
|
||||
/* get fw information */
|
||||
IOT_OTA_Ioctl(h_ota, IOT_OTAG_MD5SUM, md5sum, 33);
|
||||
IOT_OTA_Ioctl(h_ota, IOT_OTAG_VERSION, version, 128);
|
||||
size_downloaded = size_file;
|
||||
break;
|
||||
}
|
||||
|
||||
/*start http connect*/
|
||||
rc = IOT_OTA_StartDownload(h_ota, offset, size_file);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
Log_e("OTA download start err,rc:%d", rc);
|
||||
upgrade_fetch_success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
/*cal file md5*/
|
||||
//Log_d("Get offset:%d(%x)", offset, offset);
|
||||
if (offset > 0) {
|
||||
if (NULL == (fp = fopen("ota.bin", "ab+"))) {
|
||||
Log_e("open file failed");
|
||||
upgrade_fetch_success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
if (QCLOUD_RET_SUCCESS != cal_exist_fw_md5(h_ota, fp, offset)) {
|
||||
Log_e("cal exist fw md5 failed");
|
||||
upgrade_fetch_success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
/*set offset*/
|
||||
fseek(fp, offset, SEEK_SET);
|
||||
} else {
|
||||
if (NULL == (fp = fopen("ota.bin", "wb+"))) {
|
||||
Log_e("open file failed");
|
||||
upgrade_fetch_success = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
do {
|
||||
len = IOT_OTA_FetchYield(h_ota, buf_ota, OTA_BUF_LEN, 1);
|
||||
if (len > 0) {
|
||||
if (1 != fwrite(buf_ota, len, 1, fp)) {
|
||||
Log_e("write data to file failed");
|
||||
upgrade_fetch_success = false;
|
||||
break;
|
||||
}
|
||||
} else if (len < 0) {
|
||||
Log_e("download fail rc=%d", len);
|
||||
upgrade_fetch_success = false;
|
||||
break;
|
||||
}
|
||||
fflush(fp);
|
||||
|
||||
/* get OTA information and update local info */
|
||||
IOT_OTA_Ioctl(h_ota, IOT_OTAG_FETCHED_SIZE, &size_downloaded, 4);
|
||||
IOT_OTA_Ioctl(h_ota, IOT_OTAG_FILE_SIZE, &size_file, 4);
|
||||
IOT_OTA_Ioctl(h_ota, IOT_OTAG_MD5SUM, md5sum, 33);
|
||||
IOT_OTA_Ioctl(h_ota, IOT_OTAG_VERSION, version, 128);
|
||||
rc = update_local_fw_info(version, reportVer, md5sum, size_downloaded);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
Log_e("update local fw info err,rc:%d", rc);
|
||||
}
|
||||
|
||||
IOT_MQTT_Yield(client, 100);
|
||||
|
||||
} while (!IOT_OTA_IsFetchFinish(h_ota));
|
||||
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
|
||||
/* Must check MD5 match or not */
|
||||
if (upgrade_fetch_success) {
|
||||
IOT_OTA_Ioctl(h_ota, IOT_OTAG_CHECK_FIRMWARE, &firmware_valid, 4);
|
||||
if (0 == firmware_valid) {
|
||||
Log_e("The firmware is invalid");
|
||||
upgrade_fetch_success = false;
|
||||
rc = update_local_fw_info(NULL, reportVer, NULL, 0);
|
||||
if (QCLOUD_RET_SUCCESS != rc) {
|
||||
Log_e("update local fw info err,rc:%d", rc);
|
||||
}
|
||||
} else {
|
||||
Log_i("The firmware is valid");
|
||||
upgrade_fetch_success = true;
|
||||
}
|
||||
}
|
||||
|
||||
ota_over = 1;
|
||||
}
|
||||
|
||||
HAL_SleepMs(2000);
|
||||
} while (!ota_over);
|
||||
|
||||
|
||||
if (upgrade_fetch_success) {
|
||||
/* begin execute OTA files, should report upgrade begin */
|
||||
sg_packet_id = IOT_OTA_ReportUpgradeBegin(h_ota);
|
||||
if (0 > sg_packet_id) {
|
||||
Log_e("report OTA begin failed error:%d", sg_packet_id);
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
while (!sg_pub_ack) {
|
||||
HAL_SleepMs(1000);
|
||||
IOT_MQTT_Yield(client, 200);
|
||||
}
|
||||
sg_pub_ack = false;
|
||||
|
||||
//* add your own upgrade logic here*//
|
||||
//* fw_upgrade.....
|
||||
|
||||
if (QCLOUD_RET_SUCCESS == rc) {
|
||||
/* if upgrade success */
|
||||
/* after execute OTA files, should report upgrade result */
|
||||
sg_packet_id = IOT_OTA_ReportUpgradeSuccess(h_ota, NULL);
|
||||
if (0 > sg_packet_id) {
|
||||
Log_e("report OTA result failed error:%d", sg_packet_id);
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
while (!sg_pub_ack) {
|
||||
HAL_SleepMs(1000);
|
||||
IOT_MQTT_Yield(client, 200);
|
||||
}
|
||||
rc = update_local_fw_info(version, version, md5sum, size_downloaded); // just for example, add your own logic
|
||||
sg_pub_ack = false;
|
||||
|
||||
} else {
|
||||
/* if upgrade fail */
|
||||
sg_packet_id = IOT_OTA_ReportUpgradeFail(h_ota, NULL);
|
||||
if (0 > sg_packet_id) {
|
||||
Log_e("report OTA result failed error:%d", sg_packet_id);
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
while (!sg_pub_ack) {
|
||||
HAL_SleepMs(1000);
|
||||
IOT_MQTT_Yield(client, 200);
|
||||
}
|
||||
rc = update_local_fw_info(NULL, reportVer, NULL, 0); // just for example, add your own logic
|
||||
sg_pub_ack = false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
exit:
|
||||
|
||||
if (NULL != fp) {
|
||||
fclose(fp);
|
||||
fp = NULL;
|
||||
}
|
||||
|
||||
if (NULL != local_ver) {
|
||||
reportVer = (reportVer == local_ver) ? NULL : reportVer;
|
||||
HAL_Free(local_ver);
|
||||
local_ver = NULL;
|
||||
}
|
||||
|
||||
if (NULL != local_md5) {
|
||||
HAL_Free(local_md5);
|
||||
local_md5 = NULL;
|
||||
}
|
||||
|
||||
if (NULL != local_size) {
|
||||
HAL_Free(local_size);
|
||||
local_size = NULL;
|
||||
}
|
||||
|
||||
if (NULL != reportVer) {
|
||||
HAL_Free(reportVer);
|
||||
reportVer = NULL;
|
||||
}
|
||||
|
||||
IOT_OTA_Destroy(h_ota);
|
||||
|
||||
IOT_MQTT_Destroy(&client);
|
||||
|
||||
return 0;
|
||||
}
|
Reference in New Issue
Block a user