Skip to content

Commit

Permalink
fix some potential bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
spikelin committed Mar 6, 2020
1 parent af362aa commit 60a1419
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
5 changes: 5 additions & 0 deletions sdk_src/protocol/mqtt/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@ int IOT_MQTT_Destroy(void **pClient)
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)(*pClient);

int rc = qcloud_iot_mqtt_disconnect(mqtt_client);
// disconnect network stack by force
if (rc != QCLOUD_RET_SUCCESS) {
mqtt_client->network_stack.disconnect(&(mqtt_client->network_stack));
set_client_conn_state(mqtt_client, NOTCONNECTED);
}

int i = 0;
for (i = 0; i < MAX_MESSAGE_HANDLERS; ++i) {
Expand Down
4 changes: 2 additions & 2 deletions sdk_src/services/shadow/shadow_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ void* IOT_Shadow_Construct(ShadowInitParams *pParams)
Qcloud_IoT_Shadow *shadow_client = NULL;
if ((shadow_client = (Qcloud_IoT_Shadow *)HAL_Malloc(sizeof(Qcloud_IoT_Shadow))) == NULL) {
Log_e("memory not enough to malloc ShadowClient");
return NULL;
}

MQTTInitParams mqtt_init_params;
Expand All @@ -155,8 +156,7 @@ void* IOT_Shadow_Construct(ShadowInitParams *pParams)

rc = qcloud_iot_shadow_init(shadow_client);
if (rc != QCLOUD_RET_SUCCESS) {
IOT_MQTT_Destroy(&(shadow_client->mqtt));
HAL_Free(shadow_client);
IOT_Shadow_Destroy(shadow_client);
goto End;
}

Expand Down
21 changes: 8 additions & 13 deletions sdk_src/services/shadow/shadow_client_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ static int _publish_operation_to_cloud(Qcloud_IoT_Shadow *pShadow, Method method

static int _add_request_to_list(Qcloud_IoT_Shadow *pShadow, const char *pClientToken, RequestParams *pParams);

static int _unsubscribe_operation_result_to_cloud(void *pClient);
static int _unsubscribe_operation_result_to_cloud(Qcloud_IoT_Shadow *pShadow);

static void _traverse_list(Qcloud_IoT_Shadow *pShadow, List *list, const char *pClientToken, const char *pType, TraverseHandle traverseHandle);

Expand Down Expand Up @@ -110,7 +110,7 @@ void qcloud_iot_shadow_reset(void *pClient)
list_destroy(shadow_client->inner_data.property_handle_list);
}

_unsubscribe_operation_result_to_cloud(shadow_client->mqtt);
_unsubscribe_operation_result_to_cloud(shadow_client);

if (shadow_client->inner_data.request_list) {
list_destroy(shadow_client->inner_data.request_list);
Expand Down Expand Up @@ -403,22 +403,17 @@ static int _set_shadow_json_type(char *pJsonDoc, size_t sizeOfBuffer, Method met
/**
* @brief unsubsribe topic: $shadow/operation/result/{ProductId}/{DeviceName}
*/
static int _unsubscribe_operation_result_to_cloud(void* pClient)
static int _unsubscribe_operation_result_to_cloud(Qcloud_IoT_Shadow *pShadow)
{
IOT_FUNC_ENTRY;
int rc = QCLOUD_RET_SUCCESS;

char operation_result_topic[MAX_SIZE_OF_CLOUD_TOPIC] = {0};
int size = HAL_Snprintf(operation_result_topic, MAX_SIZE_OF_CLOUD_TOPIC, "$shadow/operation/result/%s/%s", iot_device_info_get()->product_id, iot_device_info_get()->device_name);

if (size < 0 || size > MAX_SIZE_OF_CLOUD_TOPIC - 1) {
Log_e("buf size < topic length!");
IOT_FUNC_EXIT_RC(QCLOUD_ERR_FAILURE);
}

IOT_MQTT_Unsubscribe(pClient, operation_result_topic);
if (pShadow->inner_data.result_topic == NULL)
IOT_FUNC_EXIT_RC(rc);

rc = IOT_MQTT_Unsubscribe(pShadow->mqtt, pShadow->inner_data.result_topic);
if (rc < 0) {
Log_e("unsubscribe topic: %s failed: %d.", operation_result_topic, rc);
Log_e("unsubscribe topic: %s failed: %d.", pShadow->inner_data.result_topic, rc);
}

IOT_FUNC_EXIT_RC(rc);
Expand Down

0 comments on commit 60a1419

Please sign in to comment.