add new qloud-c-sdk component
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
file(GLOB src_ota_mqtt ${CMAKE_CURRENT_SOURCE_DIR}/src/*.c)
|
||||
set(src_services ${src_services} ${src_ota_mqtt} PARENT_SCOPE)
|
||||
|
||||
file(GLOB src_ota_mqtt_sample ${CMAKE_CURRENT_SOURCE_DIR}/sample/ota_mqtt_sample.c)
|
||||
add_executable(ota_mqtt_sample ${src_ota_mqtt_sample})
|
||||
target_link_libraries(ota_mqtt_sample ${libsdk})
|
||||
|
||||
if( ${CONFIG_IOT_TEST} STREQUAL "ON")
|
||||
file(GLOB src_unit_test ${CMAKE_CURRENT_SOURCE_DIR}/test/*.cc)
|
||||
set(src_test ${src_test} ${src_unit_test} PARENT_SCOPE)
|
||||
endif()
|
@@ -0,0 +1,266 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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.
|
||||
*
|
||||
* @file broadcast_sample.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-07-18
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-07-18 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "qcloud_iot_common.h"
|
||||
|
||||
#include "utils_log.h"
|
||||
|
||||
/**
|
||||
* @brief MQTT event callback, @see MQTTEventHandleFun
|
||||
*
|
||||
* @param[in] client pointer to mqtt client
|
||||
* @param[in] handle_context context
|
||||
* @param[in] msg msg
|
||||
*/
|
||||
static void _mqtt_event_handler(void *client, void *handle_context, MQTTEventMsg *msg)
|
||||
{
|
||||
MQTTMessage *mqtt_message = (MQTTMessage *)msg->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_PUBLISH_RECEIVED:
|
||||
Log_i("topic message arrived but without any related handle: topic=%.*s, topic_msg=%.*s",
|
||||
mqtt_message->topic_len, STRING_PTR_PRINT_SANITY_CHECK(mqtt_message->topic_name),
|
||||
mqtt_message->payload_len, STRING_PTR_PRINT_SANITY_CHECK((char *)mqtt_message->payload));
|
||||
break;
|
||||
case MQTT_EVENT_SUBSCRIBE_SUCCESS:
|
||||
Log_i("subscribe success, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBE_TIMEOUT:
|
||||
Log_i("subscribe wait ack timeout, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_SUBSCRIBE_NACK:
|
||||
Log_i("subscribe nack, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_UNSUBSCRIBE_SUCCESS:
|
||||
Log_i("unsubscribe success, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_UNSUBSCRIBE_TIMEOUT:
|
||||
Log_i("unsubscribe timeout, packet-id=%u", (unsigned int)packet_id);
|
||||
break;
|
||||
|
||||
case MQTT_EVENT_UNSUBSCRIBE_NACK:
|
||||
Log_i("unsubscribe 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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Setup MQTT construct parameters.
|
||||
*
|
||||
* @param[in,out] initParams @see MQTTInitParams
|
||||
* @param[in] device_info @see DeviceInfo
|
||||
*/
|
||||
static void _setup_connect_init_params(MQTTInitParams *init_params, DeviceInfo *device_info)
|
||||
{
|
||||
init_params->device_info = device_info;
|
||||
init_params->command_timeout = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
||||
init_params->keep_alive_interval_ms = QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL;
|
||||
init_params->auto_connect_enable = 1;
|
||||
init_params->event_handle.h_fp = _mqtt_event_handler;
|
||||
init_params->event_handle.context = NULL;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// OTA function
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
int _firmware_download(void *client, char *buf, int buf_len, UtilsJsonValue url, UtilsJsonValue md5sum,
|
||||
uint32_t file_size, const char *version)
|
||||
{
|
||||
Log_i("downloading firmware:url=%.*s|md5sum=%.*s|file_size=%u", url.value_len, url.value, md5sum.value_len,
|
||||
md5sum.value, file_size);
|
||||
IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_DOWNLOADING, 0, version);
|
||||
IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_DOWNLOADING, 50, version);
|
||||
IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_DOWNLOADING, 100, version);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void _firmware_burn(void *client, char *buf, int buf_len, const char *version)
|
||||
{
|
||||
IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_UPGRADE_BEGIN, 0, version);
|
||||
IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_UPGRADE_SUCCESS, 0, version);
|
||||
IOT_OTA_ReportVersion(client, buf, buf_len, version);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// OTA callback
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
void _update_firmware_callback(UtilsJsonValue version, UtilsJsonValue url, UtilsJsonValue md5sum, uint32_t file_size,
|
||||
void *usr_data)
|
||||
{
|
||||
char firmware_version[64] = {0};
|
||||
char buf[256];
|
||||
int buf_len = sizeof(buf);
|
||||
|
||||
Log_i("recv firmware: version=%.*s|url=%.*s|md5sum=%.*s|file_size=%u", version.value_len, version.value,
|
||||
url.value_len, url.value, md5sum.value_len, md5sum.value, file_size);
|
||||
strncpy(firmware_version, version.value, version.value_len);
|
||||
|
||||
int rc = _firmware_download(usr_data, buf, buf_len, url, md5sum, file_size, firmware_version);
|
||||
if (rc) {
|
||||
IOT_OTA_ReportProgress(usr_data, buf, buf_len, IOT_OTA_REPORT_TYPE_UPGRADE_FAIL, 0, firmware_version);
|
||||
return;
|
||||
}
|
||||
_firmware_burn(usr_data, buf, buf_len, firmware_version);
|
||||
}
|
||||
|
||||
void _report_version_reply_callback(int result_code, void *usr_data)
|
||||
{
|
||||
Log_i("recv code=%d", result_code);
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
// Main
|
||||
// ----------------------------------------------------------------------------
|
||||
static int sg_main_exit = 0;
|
||||
|
||||
#ifdef __linux__
|
||||
|
||||
#include <signal.h>
|
||||
#include <pthread.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static void _main_exit(int sig)
|
||||
{
|
||||
Log_e("demo exit by signal:%d\n", sig);
|
||||
sg_main_exit = 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
#ifdef __linux__
|
||||
signal(SIGINT, _main_exit);
|
||||
#endif
|
||||
|
||||
int rc;
|
||||
|
||||
char buf[1024];
|
||||
|
||||
// init log level
|
||||
LogHandleFunc func = {0};
|
||||
func.log_malloc = HAL_Malloc;
|
||||
func.log_free = HAL_Free;
|
||||
func.log_get_current_time_str = HAL_Timer_Current;
|
||||
func.log_printf = HAL_Printf;
|
||||
utils_log_init(func, LOG_LEVEL_DEBUG, 2048);
|
||||
|
||||
DeviceInfo device_info;
|
||||
rc = HAL_GetDevInfo((void *)&device_info);
|
||||
if (rc) {
|
||||
Log_e("get device info failed: %d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
// init connection
|
||||
MQTTInitParams init_params = DEFAULT_MQTT_INIT_PARAMS;
|
||||
_setup_connect_init_params(&init_params, &device_info);
|
||||
|
||||
// create MQTT client and connect with server
|
||||
void *client = IOT_MQTT_Construct(&init_params);
|
||||
if (client) {
|
||||
Log_i("Cloud Device Construct Success");
|
||||
} else {
|
||||
Log_e("MQTT Construct failed!");
|
||||
return QCLOUD_ERR_FAILURE;
|
||||
}
|
||||
|
||||
IotOTAUpdateCallback ota_callback = {
|
||||
.update_firmware_callback = _update_firmware_callback,
|
||||
.report_version_reply_callback = _report_version_reply_callback,
|
||||
};
|
||||
rc = IOT_OTA_Init(client, ota_callback, client);
|
||||
if (rc) {
|
||||
Log_e("OTA init failed!, rc=%d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
rc = IOT_OTA_ReportVersion(client, buf, sizeof(buf), QCLOUD_IOT_DEVICE_SDK_VERSION);
|
||||
if (rc) {
|
||||
Log_e("OTA report version failed!, rc=%d", rc);
|
||||
return rc;
|
||||
}
|
||||
|
||||
do {
|
||||
rc = IOT_MQTT_Yield(client, QCLOUD_IOT_MQTT_YIELD_TIMEOUT);
|
||||
switch (rc) {
|
||||
case QCLOUD_RET_SUCCESS:
|
||||
break;
|
||||
case QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT:
|
||||
continue;
|
||||
case QCLOUD_RET_MQTT_RECONNECTED:
|
||||
break;
|
||||
default:
|
||||
Log_e("Exit loop caused of errCode:%d", rc);
|
||||
goto exit;
|
||||
}
|
||||
} while (!sg_main_exit);
|
||||
exit:
|
||||
IOT_OTA_Deinit(client);
|
||||
rc = IOT_MQTT_Destroy(&client);
|
||||
utils_log_deinit();
|
||||
return rc;
|
||||
}
|
@@ -0,0 +1,310 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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.
|
||||
*
|
||||
* @file ota_mqtt.c
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-10-18
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-10-18 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include "qcloud_iot_ota.h"
|
||||
|
||||
/**
|
||||
* @brief Context of OTA update topic, callback and user data.
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
IotOTAUpdateCallback callback;
|
||||
void * usr_data;
|
||||
} OTAUpdateContext;
|
||||
|
||||
/**
|
||||
* @brief OTA update type.
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
OTA_UPDATE_TYPE_REPORT_VERSION_RSP = 0,
|
||||
OTA_UPDATE_TYPE_UPDATE_FIRMWARE,
|
||||
} OTAUpdateType;
|
||||
|
||||
/**
|
||||
* @brief Parse payload and callback.
|
||||
*
|
||||
* @param[in] type @see OTAUpdateType
|
||||
* @param[in] message message from cloud
|
||||
* @param[in] callback callback for user
|
||||
* @param[in,out] usr_data user data used in callback
|
||||
*/
|
||||
static void _parse_update_payload_and_callback(OTAUpdateType type, const MQTTMessage *message,
|
||||
const IotOTAUpdateCallback *callback, void *usr_data)
|
||||
{
|
||||
int rc, result_code;
|
||||
uint32_t file_size = 0;
|
||||
UtilsJsonValue version, md5sum, url, value_file_size, value_result_code;
|
||||
|
||||
// callback
|
||||
switch (type) {
|
||||
case OTA_UPDATE_TYPE_REPORT_VERSION_RSP:
|
||||
if (!callback->report_version_reply_callback) {
|
||||
return;
|
||||
}
|
||||
|
||||
rc = utils_json_value_get("result_code", strlen("result_code"), message->payload_str, message->payload_len,
|
||||
&value_result_code);
|
||||
if (rc) {
|
||||
goto error;
|
||||
}
|
||||
rc = utils_json_value_data_get(value_result_code, UTILS_JSON_VALUE_TYPE_INT32, &result_code);
|
||||
if (rc) {
|
||||
goto error;
|
||||
}
|
||||
callback->report_version_reply_callback(result_code, usr_data);
|
||||
break;
|
||||
case OTA_UPDATE_TYPE_UPDATE_FIRMWARE:
|
||||
if (!callback->update_firmware_callback) {
|
||||
return;
|
||||
}
|
||||
|
||||
rc = utils_json_value_get("version", strlen("version"), message->payload_str, message->payload_len,
|
||||
&version);
|
||||
rc |= utils_json_value_get("url", strlen("url"), message->payload_str, message->payload_len, &url);
|
||||
rc |= utils_json_value_get("md5sum", strlen("md5sum"), message->payload_str, message->payload_len, &md5sum);
|
||||
rc |= utils_json_value_get("file_size", strlen("file_size"), message->payload_str, message->payload_len,
|
||||
&value_file_size);
|
||||
rc |= utils_json_value_data_get(value_file_size, UTILS_JSON_VALUE_TYPE_UINT32, &file_size);
|
||||
if (rc) {
|
||||
goto error;
|
||||
}
|
||||
callback->update_firmware_callback(version, url, md5sum, file_size, usr_data);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return;
|
||||
error:
|
||||
Log_e("invalid format of payload!");
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Mqtt message callback for property topic.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] message message from topic
|
||||
* @param[in,out] usr_data pointer to @see OTAUpdateContext
|
||||
*/
|
||||
static void _ota_mqtt_message_callback(void *client, const MQTTMessage *message, void *usr_data)
|
||||
{
|
||||
const char *ota_update_str[] = {
|
||||
"report_version_rsp", // OTA_UPDATE_TYPE_REPORT_VERSION_RSP
|
||||
"update_firmware", // OTA_UPDATE_TYPE_UPDATE_FIRMWARE
|
||||
};
|
||||
|
||||
int rc, i = 0;
|
||||
|
||||
OTAUpdateContext *ota_update_context = (OTAUpdateContext *)usr_data;
|
||||
UtilsJsonValue update_type;
|
||||
|
||||
Log_d("receive ota message:%.*s", message->payload_len, message->payload_str);
|
||||
|
||||
rc = utils_json_value_get("type", strlen("type"), message->payload_str, message->payload_len, &update_type);
|
||||
if (rc) {
|
||||
Log_e("invalid ota message!");
|
||||
return;
|
||||
}
|
||||
|
||||
for (i = OTA_UPDATE_TYPE_REPORT_VERSION_RSP; i <= OTA_UPDATE_TYPE_UPDATE_FIRMWARE; i++) {
|
||||
if (!strncmp(update_type.value, ota_update_str[i], update_type.value_len)) {
|
||||
Log_d("callback ota message!");
|
||||
_parse_update_payload_and_callback(i, message, &ota_update_context->callback, ota_update_context->usr_data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Check if topic already subscribed, if not then subscribe.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param callback @see IotOTAUpdateCallback
|
||||
* @param usr_data usr data in callback
|
||||
* @return @see IotReturnCode
|
||||
*/
|
||||
static int _ota_mqtt_update_topic_check_and_sub(void *client, IotOTAUpdateCallback callback, void *usr_data)
|
||||
{
|
||||
int rc = 0;
|
||||
char ota_result_topic[MAX_SIZE_OF_CLOUD_TOPIC];
|
||||
|
||||
HAL_Snprintf(ota_result_topic, MAX_SIZE_OF_CLOUD_TOPIC, "$ota/update/%s/%s",
|
||||
STRING_PTR_PRINT_SANITY_CHECK(IOT_MQTT_GetDeviceInfo(client)->product_id),
|
||||
STRING_PTR_PRINT_SANITY_CHECK(IOT_MQTT_GetDeviceInfo(client)->device_name));
|
||||
|
||||
OTAUpdateContext *ota_update_context = (OTAUpdateContext *)HAL_Malloc(sizeof(OTAUpdateContext));
|
||||
if (!ota_update_context) {
|
||||
return QCLOUD_ERR_MALLOC;
|
||||
}
|
||||
ota_update_context->callback = callback;
|
||||
ota_update_context->usr_data = usr_data;
|
||||
|
||||
SubscribeParams sub_params = DEFAULT_SUB_PARAMS;
|
||||
sub_params.on_message_handler = _ota_mqtt_message_callback;
|
||||
sub_params.qos = QOS1;
|
||||
sub_params.user_data = ota_update_context;
|
||||
sub_params.user_data_free = HAL_Free;
|
||||
|
||||
rc = IOT_MQTT_SubscribeSync(client, ota_result_topic, &sub_params);
|
||||
if (rc) {
|
||||
HAL_Free(ota_update_context);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Unsubscribe topic.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @return packet id (>=0) when success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
static int _ota_mqtt_update_topic_unsubscribe(void *client)
|
||||
{
|
||||
char ota_result_topic[MAX_SIZE_OF_CLOUD_TOPIC];
|
||||
HAL_Snprintf(ota_result_topic, MAX_SIZE_OF_CLOUD_TOPIC, "$ota/update/%s/%s",
|
||||
STRING_PTR_PRINT_SANITY_CHECK(IOT_MQTT_GetDeviceInfo(client)->product_id),
|
||||
STRING_PTR_PRINT_SANITY_CHECK(IOT_MQTT_GetDeviceInfo(client)->device_name));
|
||||
return IOT_MQTT_Unsubscribe(client, ota_result_topic);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Publish to OTA report topic.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] qos @see QoS
|
||||
* @param[in] payload payload of mqtt packet
|
||||
* @param[in] payload_len payload len
|
||||
* @return packet id (>=0) when success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
static int _ota_mqtt_publish(void *client, QoS qos, const char *payload, int payload_len)
|
||||
{
|
||||
char ota_report_topic[MAX_SIZE_OF_CLOUD_TOPIC];
|
||||
HAL_Snprintf(ota_report_topic, sizeof(ota_report_topic), "$ota/report/%s/%s",
|
||||
STRING_PTR_PRINT_SANITY_CHECK(IOT_MQTT_GetDeviceInfo(client)->product_id),
|
||||
STRING_PTR_PRINT_SANITY_CHECK(IOT_MQTT_GetDeviceInfo(client)->device_name));
|
||||
|
||||
PublishParams pub_params = DEFAULT_PUB_PARAMS;
|
||||
pub_params.qos = qos;
|
||||
pub_params.payload = (void *)payload;
|
||||
pub_params.payload_len = payload_len;
|
||||
return IOT_MQTT_Publish(client, ota_report_topic, &pub_params);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief OTA init, subscribe update topic.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[in] callback @see IotOTAUpdateCallback
|
||||
* @param[in] usr_data usr data used in callback
|
||||
* @return 0 for success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
int IOT_OTA_Init(void *client, IotOTAUpdateCallback callback, void *usr_data)
|
||||
{
|
||||
POINTER_SANITY_CHECK(client, QCLOUD_ERR_INVAL);
|
||||
return _ota_mqtt_update_topic_check_and_sub(client, callback, usr_data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief OTA deinit, unsubscribe update topic.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
*/
|
||||
void IOT_OTA_Deinit(void *client)
|
||||
{
|
||||
POINTER_SANITY_CHECK_RTN(client);
|
||||
_ota_mqtt_update_topic_unsubscribe(client);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Report upgrade progress.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[out] buf publish message buffer
|
||||
* @param[in] buf_len buffer len
|
||||
* @param[in] report_type @see IotOTAReportType
|
||||
* @param[in] progress progress using in IOT_OTA_REPORT_TYPE_DOWNLOADING
|
||||
* @param[in] version update firmware version
|
||||
* @return packet id (>=0) when success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
int IOT_OTA_ReportProgress(void *client, char *buf, int buf_len, IotOTAReportType report_type, int progress,
|
||||
const char *version)
|
||||
{
|
||||
POINTER_SANITY_CHECK(client, QCLOUD_ERR_INVAL);
|
||||
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
|
||||
NUMBERIC_SANITY_CHECK(buf_len, QCLOUD_ERR_INVAL);
|
||||
POINTER_SANITY_CHECK(version, QCLOUD_ERR_INVAL);
|
||||
|
||||
/**
|
||||
* @brief order @see IotOTAReportType
|
||||
*
|
||||
*/
|
||||
const char *ota_state[] = {"downloading", "burning", "done", "fail", "fail", "fail", "fail", "fail"};
|
||||
int result_code[] = {0, 0, 0, -1, -2, -3, -4, -5};
|
||||
const char *result_msg[] = {"", "", "", "timeout", "file not exit", "auth fail", "md5 not match", "upgrade fail"};
|
||||
|
||||
int len;
|
||||
switch (report_type) {
|
||||
case IOT_OTA_REPORT_TYPE_DOWNLOADING:
|
||||
len = HAL_Snprintf(buf, buf_len,
|
||||
"{\"type\":\"report_progress\",\"report\":{\"progress\":{\"state\":\"%s\",\"percent\":"
|
||||
"\"%d\",\"result_code\":\"%d\",\"result_msg\":\"\"},\"version\":\"%s\"}}",
|
||||
ota_state[report_type], progress, result_code[report_type], version);
|
||||
break;
|
||||
case IOT_OTA_REPORT_TYPE_UPGRADE_BEGIN:
|
||||
case IOT_OTA_REPORT_TYPE_UPGRADE_SUCCESS:
|
||||
case IOT_OTA_REPORT_TYPE_DOWNLOAD_TIMEOUT:
|
||||
case IOT_OTA_REPORT_TYPE_FILE_NOT_EXIST:
|
||||
case IOT_OTA_REPORT_TYPE_AUTH_FAIL:
|
||||
case IOT_OTA_REPORT_TYPE_MD5_NOT_MATCH:
|
||||
case IOT_OTA_REPORT_TYPE_UPGRADE_FAIL:
|
||||
len = HAL_Snprintf(buf, buf_len,
|
||||
"{\"type\":\"report_progress\",\"report\":{\"progress\":{\"state\":\"%s\",\"result_"
|
||||
"code\":\"%d\",\"result_msg\":\"%s\"},\"version\":\"%s\"}}",
|
||||
ota_state[report_type], result_code[report_type], result_msg[report_type], version);
|
||||
break;
|
||||
default:
|
||||
return QCLOUD_ERR_INVAL;
|
||||
}
|
||||
return _ota_mqtt_publish(client, QOS0, buf, len);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Report current firmware version.
|
||||
*
|
||||
* @param[in,out] client pointer to mqtt client
|
||||
* @param[out] buf publish message buffer
|
||||
* @param[in] buf_len buffer len
|
||||
* @param[in] version current firmware version
|
||||
* @return packet id (>=0) when success, or err code (<0) @see IotReturnCode
|
||||
*/
|
||||
int IOT_OTA_ReportVersion(void *client, char *buf, int buf_len, const char *version)
|
||||
{
|
||||
int len = HAL_Snprintf(buf, buf_len, "{\"type\":\"report_version\",\"report\":{\"version\":\"%s\"}}",
|
||||
STRING_PTR_PRINT_SANITY_CHECK(version));
|
||||
return _ota_mqtt_publish(client, QOS0, buf, len);
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
Language: Cpp
|
||||
BasedOnStyle: Google
|
||||
ColumnLimit: 120
|
||||
DerivePointerAlignment: true
|
||||
PointerAlignment: Left
|
||||
SortIncludes: true
|
||||
IncludeBlocks: Preserve
|
||||
IndentPPDirectives: AfterHash
|
@@ -0,0 +1,72 @@
|
||||
/**
|
||||
* @copyright
|
||||
*
|
||||
* Tencent is pleased to support the open source community by making IoT Hub available.
|
||||
* Copyright(C) 2018 - 2021 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.
|
||||
*
|
||||
* @file test_ota_mqtt.cc
|
||||
* @brief
|
||||
* @author fancyxu (fancyxu@tencent.com)
|
||||
* @version 1.0
|
||||
* @date 2021-10-19
|
||||
*
|
||||
* @par Change Log:
|
||||
* <table>
|
||||
* <tr><th>Date <th>Version <th>Author <th>Description
|
||||
* <tr><td>2021-10-19 <td>1.0 <td>fancyxu <td>first commit
|
||||
* </table>
|
||||
*/
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "mqtt_client_test.h"
|
||||
#include "qcloud_iot_common.h"
|
||||
|
||||
namespace mqtt_client_unittest {
|
||||
|
||||
void _update_firmware_callback(UtilsJsonValue version, UtilsJsonValue url, UtilsJsonValue md5sum, uint32_t file_size,
|
||||
void *usr_data) {
|
||||
Log_i("recv firmware: version=%.*s|url=%.*s|md5sum=%.*s|file_size=%u", version.value_len, version.value,
|
||||
url.value_len, url.value, md5sum.value_len, md5sum.value, file_size);
|
||||
}
|
||||
|
||||
void _report_version_reply_callback(int result_code, void *usr_data) { Log_i("recv code=%d", result_code); }
|
||||
|
||||
/**
|
||||
* @brief Test ota mqtt.
|
||||
*
|
||||
*/
|
||||
TEST_F(MqttClientTest, ota_mqtt) {
|
||||
char buf[256];
|
||||
int buf_len = sizeof(buf);
|
||||
const char *version = "1.0.0";
|
||||
IotOTAUpdateCallback ota_callback = {
|
||||
.update_firmware_callback = _update_firmware_callback,
|
||||
.report_version_reply_callback = _report_version_reply_callback,
|
||||
};
|
||||
ASSERT_EQ(IOT_OTA_Init(client, ota_callback, client), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportVersion(client, buf, buf_len, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_DOWNLOADING, 0, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_DOWNLOADING, 100, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_UPGRADE_BEGIN, 0, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_UPGRADE_SUCCESS, 0, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_DOWNLOAD_TIMEOUT, 0, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_FILE_NOT_EXIST, 0, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_AUTH_FAIL, 0, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_MD5_NOT_MATCH, 0, version), 0);
|
||||
ASSERT_GE(IOT_OTA_ReportProgress(client, buf, buf_len, IOT_OTA_REPORT_TYPE_UPGRADE_FAIL, 0, version), 0);
|
||||
IOT_OTA_Deinit(client);
|
||||
}
|
||||
|
||||
} // namespace mqtt_client_unittest
|
Reference in New Issue
Block a user