add qcloud explorer ota sample
This commit is contained in:
@@ -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;
|
||||
|
Reference in New Issue
Block a user