描述:
要在国内测试新加坡设备,因为mqtt是就近原则,所以在国内测试肯定是连接的是上海的站点,所以需要在初始化MQTT时候修改下参数。在//begin bailong //end
int mqtt_client(mqttClinetInfo * info)
{
int rc = 0;
void *pclient;
int stack_used = 0;
iotx_conn_info_pt pconn_info;
iotx_mqtt_param_t mqtt_params;
hal_os_thread_param_t task_parms = {0};
//begin bailong
int domain_type = 0;
domain_type = IOTX_CLOUD_REGION_SINGAPORE;
IOT_Ioctl(IOTX_IOCTL_SET_DOMAIN, (void *)&domain_type);
//end
/* Device AUTH */
if (0 != IOT_SetupConnInfo(info->product_key, info->device_name, info->device_secret, (void **)&pconn_info)) {
EXAMPLE_TRACE("AUTH request failed!");
return -1;
}
/* Initialize MQTT parameter */
memset(&mqtt_params, 0x0, sizeof(mqtt_params));
mqtt_params.port = pconn_info->port;
mqtt_params.host = pconn_info->host_name;
mqtt_params.client_id = pconn_info->client_id;
mqtt_params.username = pconn_info->username;
mqtt_params.password = pconn_info->password;
mqtt_params.pub_key = pconn_info->pub_key;
mqtt_params.request_timeout_ms = 2000;
//bejin jian 2020.3.16 put 0=>1
mqtt_params.clean_session = 1;
//end
mqtt_params.keepalive_interval_ms = 60000;
mqtt_params.read_buf_size = MQTT_MSGLEN;
mqtt_params.write_buf_size = MQTT_MSGLEN;
mqtt_params.handle_event.h_fp = event_handle;
mqtt_params.handle_event.pcontext = NULL;
/* Construct a MQTT client with specify parameter 使用指定参数构造MQTT客户机*/
pclient = IOT_MQTT_Construct(&mqtt_params);
if (NULL == pclient) {
EXAMPLE_TRACE("MQTT construct failed");
return -1;
}
pubclient = pclient;
EXAMPLE_TRACE("TEST CASE");
task_parms.stack_size = 6144;
task_parms.name = "thread_message_receive";
rc = HAL_ThreadCreate(&g_thread_receive, thread_message_receive, (void *)pclient, &task_parms, &stack_used);
if (rc != 0) {
goto do_exit;
}
//start publish thread
initPublishThread(pclient);
while (!gIsmqttThreadExit) {
HAL_SleepMs(1000);
}
gIsResponseThreadExit = 1;
gIsReceiveThreadExit = 1;
HAL_SleepMs(5000);
do_exit:
HAL_ThreadDelete(g_thread_response);
HAL_ThreadDelete(g_thread_receive);
IOT_MQTT_Destroy(&pclient);
return rc;
}