Skip to content

Commit cc738dc

Browse files
authored
Merge pull request LiteOS#400 from l00438842/develop
1、增加bind_quit 2、psk_idid_len使用strlen计算 3、申请的内存进行初始化
2 parents 2871759 + 9e689ea commit cc738dc

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

components/connectivity/agent_tiny/mqtt_client/mqtt_client.c

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ typedef struct
6262
MQTTClient client;
6363
atiny_param_t atiny_params;
6464
char atiny_quit;
65+
char bind_quit;
6566
} handle_data_t;
6667

6768
static handle_data_t g_atiny_handle;
@@ -145,13 +146,13 @@ int atiny_param_dup(atiny_param_t *dest, atiny_param_t *src)
145146
dest->u.psk.psk_id = (unsigned char *)atiny_strdup((const char *)(src->u.psk.psk_id));
146147
if(NULL == dest->u.psk.psk_id)
147148
goto atiny_param_dup_failed;
148-
if(src->u.psk.psk_len < 0 || src->u.psk.psk_len > MQTT_PSK_MAX_LEN)
149+
dest->u.psk.psk_id_len = strlen((const char *)(src->u.psk.psk_id));
150+
if(NULL == src->u.psk.psk || src->u.psk.psk_len < 0 || src->u.psk.psk_len > MQTT_PSK_MAX_LEN)
149151
goto atiny_param_dup_failed;
150152
dest->u.psk.psk = (unsigned char *)atiny_malloc(src->u.psk.psk_len);
151153
if(NULL == dest->u.psk.psk)
152154
goto atiny_param_dup_failed;
153155
memcpy(dest->u.psk.psk, src->u.psk.psk, src->u.psk.psk_len);
154-
dest->u.psk.psk_id_len = src->u.psk.psk_id_len;
155156
dest->u.psk.psk_len = src->u.psk.psk_len;
156157
break;
157158
case CLOUD_SECURITY_TYPE_CA:
@@ -183,12 +184,18 @@ int atiny_init(atiny_param_t *atiny_params, void **phandle)
183184
return ATINY_ARG_INVALID;
184185
}
185186

187+
while(1 == g_atiny_handle.bind_quit)
188+
{
189+
(void)LOS_TaskDelay(10);
190+
}
191+
186192
memset((void *)&g_atiny_handle, 0, sizeof(handle_data_t));
187193

188194
if(0 != atiny_param_dup(&(g_atiny_handle.atiny_params), atiny_params))
189195
return ATINY_MALLOC_FAILED;
190196

191197
g_atiny_handle.atiny_quit = 0;
198+
g_atiny_handle.bind_quit = 0;
192199
*phandle = &g_atiny_handle;
193200

194201
return ATINY_OK;
@@ -197,27 +204,21 @@ int atiny_init(atiny_param_t *atiny_params, void **phandle)
197204
void atiny_deinit(void *phandle)
198205
{
199206
handle_data_t *handle;
200-
MQTTClient *client;
201-
Network *network;
202207

203208
if(NULL == phandle)
204209
{
205210
ATINY_LOG(LOG_FATAL, "Parameter null");
206211
return;
207212
}
208213
handle = (handle_data_t *)phandle;
209-
client = &(handle->client);
210-
network = client->ipstack;
211214
if(0 == handle->atiny_quit)
212215
{
213216
handle->atiny_quit = 1;
214-
atiny_param_member_free(&(handle->atiny_params));
215-
if(client->mutex) atiny_mutex_lock(client->mutex);
216-
device_info_member_free(&(handle->device_info));
217-
if(client->mutex) atiny_mutex_unlock(client->mutex);
218-
(void)MQTTDisconnect(client);
219-
MQTTClientDeInit(client);
220-
NetworkDisconnect(network);
217+
while(0 == handle->bind_quit)
218+
{
219+
(void)LOS_TaskDelay(10);
220+
}
221+
handle->bind_quit = 0;
221222
}
222223

223224
return;
@@ -618,6 +619,8 @@ int device_info_dup(atiny_device_info_t *dest, atiny_device_info_t *src)
618619
if(NULL == dest->will_options)
619620
goto device_info_dup_failed;
620621

622+
memset(dest->will_options, 0x0, sizeof(cloud_will_options_t));
623+
621624
dest->will_options->topic_name = atiny_strdup((const char *)(src->will_options->topic_name));
622625
if(NULL == dest->will_options->topic_name)
623626
goto device_info_dup_failed;
@@ -667,39 +670,38 @@ int atiny_isconnected(void *phandle)
667670
int atiny_bind(atiny_device_info_t *device_info, void *phandle)
668671
{
669672
Network n;
670-
handle_data_t *handle;
671-
MQTTClient *client;
673+
handle_data_t *handle = NULL;
674+
MQTTClient *client = NULL;
672675
atiny_param_t *atiny_params;
673676
atiny_device_info_t *device_info_t;
674677
int rc = -1, conn_failed_cnt = 0;
675678
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
676679

677-
if ((NULL == device_info) || (NULL == phandle))
680+
if(NULL == phandle)
678681
{
679682
ATINY_LOG(LOG_FATAL, "Parameter null");
680683
return ATINY_ARG_INVALID;
681684
}
682685

683-
if(NULL == device_info->client_id)
686+
handle = (handle_data_t *)phandle;
687+
client = &(handle->client);
688+
atiny_params = &(handle->atiny_params);
689+
690+
if ((NULL == device_info) || (NULL == device_info->client_id))
684691
{
685692
ATINY_LOG(LOG_FATAL, "Parameter null");
686-
return ATINY_ARG_INVALID;
693+
goto atiny_bind_quit;
687694
}
688695

689696
if(device_info->will_flag == MQTT_WILL_FLAG_TRUE && NULL == device_info->will_options)
690697
{
691698
ATINY_LOG(LOG_FATAL, "Parameter null");
692-
return ATINY_ARG_INVALID;
699+
goto atiny_bind_quit;
693700
}
694701

695-
handle = (handle_data_t *)phandle;
696-
client = &(handle->client);
697-
atiny_params = &(handle->atiny_params);
698-
699702
if(0 != device_info_dup(&(handle->device_info), device_info))
700703
{
701-
atiny_deinit(phandle);
702-
return ATINY_MALLOC_FAILED;
704+
goto atiny_bind_quit;
703705
}
704706
device_info_t = &(handle->device_info);
705707

@@ -719,7 +721,7 @@ int atiny_bind(atiny_device_info_t *device_info, void *phandle)
719721
break;
720722
case CLOUD_SECURITY_TYPE_CA:
721723
ATINY_LOG(LOG_INFO, "CLOUD_SECURITY_TYPE_CA unsupported now" );
722-
return ATINY_ARG_INVALID;
724+
goto atiny_bind_quit;
723725
default:
724726
ATINY_LOG(LOG_WARNING, "invalid security_typ : %d", atiny_params->security_type);
725727
break;
@@ -789,6 +791,13 @@ int atiny_bind(atiny_device_info_t *device_info, void *phandle)
789791
data.cleansession = MQTT_CLEAN_SESSION_FALSE;
790792
ATINY_LOG(LOG_ERR, "connect_again");
791793
}
794+
atiny_bind_quit:
795+
atiny_param_member_free(&(handle->atiny_params));
796+
if(client->mutex) atiny_mutex_lock(client->mutex);
797+
device_info_member_free(&(handle->device_info));
798+
if(client->mutex) atiny_mutex_unlock(client->mutex);
799+
MQTTClientDeInit(client);
800+
handle->bind_quit = 1;
792801
return ATINY_OK;
793802
}
794803

0 commit comments

Comments
 (0)