357 lines
11 KiB
C
357 lines
11 KiB
C
/*
|
|
* Tencent is pleased to support the open source community by making IoT Hub available.
|
|
* Copyright (C) 2018-2020 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.
|
|
*
|
|
*/
|
|
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "qcloud_iot_export.h"
|
|
#include "qcloud_iot_import.h"
|
|
#include "utils_getopt.h"
|
|
|
|
// MQTT event callback
|
|
static void _mqtt_event_handler(void *pclient, void *handle_context, MQTTEventMsg *msg)
|
|
{
|
|
MQTTMessage *mqtt_messge = (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_RECVEIVED:
|
|
Log_i("topic message arrived but without any related handle: topic=%.*s, topic_msg=%.*s",
|
|
mqtt_messge->topic_len, mqtt_messge->ptopic, mqtt_messge->payload_len, mqtt_messge->payload);
|
|
break;
|
|
case MQTT_EVENT_SUBCRIBE_SUCCESS:
|
|
Log_i("subscribe success, packet-id=%u", (unsigned int)packet_id);
|
|
break;
|
|
|
|
case MQTT_EVENT_SUBCRIBE_TIMEOUT:
|
|
Log_i("subscribe wait ack timeout, packet-id=%u", (unsigned int)packet_id);
|
|
break;
|
|
|
|
case MQTT_EVENT_SUBCRIBE_NACK:
|
|
Log_i("subscribe nack, packet-id=%u", (unsigned int)packet_id);
|
|
break;
|
|
|
|
case MQTT_EVENT_UNSUBCRIBE_SUCCESS:
|
|
Log_i("unsubscribe success, packet-id=%u", (unsigned int)packet_id);
|
|
break;
|
|
|
|
case MQTT_EVENT_UNSUBCRIBE_TIMEOUT:
|
|
Log_i("unsubscribe timeout, packet-id=%u", (unsigned int)packet_id);
|
|
break;
|
|
|
|
case MQTT_EVENT_UNSUBCRIBE_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;
|
|
}
|
|
}
|
|
|
|
// Setup MQTT construct parameters
|
|
static int _setup_connect_init_params(MQTTInitParams *initParams, DeviceInfo *device_info)
|
|
{
|
|
initParams->product_id = device_info->product_id;
|
|
initParams->device_name = device_info->device_name;
|
|
|
|
#ifdef AUTH_MODE_CERT
|
|
char certs_dir[16] = "certs";
|
|
char current_path[128];
|
|
char *cwd = getcwd(current_path, sizeof(current_path));
|
|
|
|
if (cwd == NULL) {
|
|
Log_e("getcwd return NULL");
|
|
return QCLOUD_ERR_FAILURE;
|
|
}
|
|
|
|
#ifdef WIN32
|
|
HAL_Snprintf(initParams->cert_file, FILE_PATH_MAX_LEN, "%s\\%s\\%s", current_path, certs_dir,
|
|
device_info->dev_cert_file_name);
|
|
HAL_Snprintf(initParams->key_file, FILE_PATH_MAX_LEN, "%s\\%s\\%s", current_path, certs_dir,
|
|
device_info->dev_key_file_name);
|
|
#else
|
|
HAL_Snprintf(initParams->cert_file, FILE_PATH_MAX_LEN, "%s/%s/%s", current_path, certs_dir,
|
|
device_info->dev_cert_file_name);
|
|
HAL_Snprintf(initParams->key_file, FILE_PATH_MAX_LEN, "%s/%s/%s", current_path, certs_dir,
|
|
device_info->dev_key_file_name);
|
|
#endif
|
|
|
|
#else
|
|
initParams->device_secret = device_info->device_secret;
|
|
#endif
|
|
|
|
initParams->command_timeout = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
|
|
initParams->keep_alive_interval_ms = QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL;
|
|
|
|
initParams->auto_connect_enable = 1;
|
|
initParams->event_handle.h_fp = _mqtt_event_handler;
|
|
initParams->event_handle.context = NULL;
|
|
|
|
return QCLOUD_RET_SUCCESS;
|
|
}
|
|
|
|
// publish MQTT msg
|
|
static int _publish_test_msg(void *client, char *topic_keyword, QoS qos)
|
|
{
|
|
char topic_name[128] = {0};
|
|
DeviceInfo *dev_info = IOT_MQTT_GetDeviceInfo(client);
|
|
|
|
int size = HAL_Snprintf(topic_name, sizeof(topic_name), "%s/%s/%s", dev_info->product_id, dev_info->device_name,
|
|
topic_keyword);
|
|
if (size < 0 || size > sizeof(topic_name) - 1) {
|
|
Log_e("topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(topic_name));
|
|
return QCLOUD_ERR_FAILURE;
|
|
}
|
|
|
|
static int test_count = 0;
|
|
PublishParams pub_params = DEFAULT_PUB_PARAMS;
|
|
pub_params.qos = qos;
|
|
|
|
char topic_content[128] = {0};
|
|
size = HAL_Snprintf(topic_content, sizeof(topic_content), "{\"action\": \"publish_test\", \"count\": \"%d\"}",
|
|
test_count++);
|
|
if (size < 0 || size > sizeof(topic_content) - 1) {
|
|
Log_e("payload content length not enough! content size:%d buf size:%d", size, (int)sizeof(topic_content));
|
|
return -3;
|
|
}
|
|
|
|
pub_params.payload = topic_content;
|
|
pub_params.payload_len = strlen(topic_content);
|
|
|
|
return IOT_MQTT_Publish(client, topic_name, &pub_params);
|
|
}
|
|
|
|
// callback when MQTT msg arrives
|
|
static void _on_message_callback(void *pClient, MQTTMessage *message, void *userData)
|
|
{
|
|
if (message == NULL) {
|
|
return;
|
|
}
|
|
|
|
Log_i("Receive Message With topicName:%.*s, payload:%.*s", (int)message->topic_len, message->ptopic,
|
|
(int)message->payload_len, (char *)message->payload);
|
|
}
|
|
|
|
// subscribe MQTT topic and wait for sub result
|
|
static int _subscribe_topic_wait_result(void *client, char *topic_keyword, QoS qos)
|
|
{
|
|
char topic_name[128] = {0};
|
|
DeviceInfo *dev_info = IOT_MQTT_GetDeviceInfo(client);
|
|
|
|
int size = HAL_Snprintf(topic_name, sizeof(topic_name), "%s/%s/%s", dev_info->product_id, dev_info->device_name,
|
|
topic_keyword);
|
|
if (size < 0 || size > sizeof(topic_name) - 1) {
|
|
Log_e("topic content length not enough! content size:%d buf size:%d", size, (int)sizeof(topic_name));
|
|
return QCLOUD_ERR_FAILURE;
|
|
}
|
|
|
|
SubscribeParams sub_params = DEFAULT_SUB_PARAMS;
|
|
sub_params.qos = qos;
|
|
sub_params.on_message_handler = _on_message_callback;
|
|
|
|
int rc = IOT_MQTT_Subscribe(client, topic_name, &sub_params);
|
|
if (rc < 0) {
|
|
Log_e("MQTT subscribe FAILED: %d", rc);
|
|
return rc;
|
|
}
|
|
|
|
int wait_cnt = 10;
|
|
while (!IOT_MQTT_IsSubReady(client, topic_name) && (wait_cnt > 0)) {
|
|
// wait for subscription result
|
|
rc = IOT_MQTT_Yield(client, 1000);
|
|
if (rc) {
|
|
Log_e("MQTT error: %d", rc);
|
|
return rc;
|
|
}
|
|
wait_cnt--;
|
|
}
|
|
|
|
if (wait_cnt > 0) {
|
|
return QCLOUD_RET_SUCCESS;
|
|
} else {
|
|
Log_e("wait for subscribe result timeout!");
|
|
return QCLOUD_ERR_FAILURE;
|
|
}
|
|
}
|
|
|
|
#ifdef LOG_UPLOAD
|
|
// init log upload module
|
|
static int _init_log_upload(MQTTInitParams *init_params)
|
|
{
|
|
LogUploadInitParams log_init_params;
|
|
memset(&log_init_params, 0, sizeof(LogUploadInitParams));
|
|
|
|
log_init_params.product_id = init_params->product_id;
|
|
log_init_params.device_name = init_params->device_name;
|
|
#ifdef AUTH_MODE_CERT
|
|
log_init_params.sign_key = init_params->cert_file;
|
|
#else
|
|
log_init_params.sign_key = init_params->device_secret;
|
|
#endif
|
|
|
|
#if defined(__linux__) || defined(WIN32)
|
|
log_init_params.read_func = HAL_Log_Read;
|
|
log_init_params.save_func = HAL_Log_Save;
|
|
log_init_params.del_func = HAL_Log_Del;
|
|
log_init_params.get_size_func = HAL_Log_Get_Size;
|
|
#endif
|
|
|
|
return IOT_Log_Init_Uploader(&log_init_params);
|
|
}
|
|
|
|
#endif
|
|
|
|
static bool sg_loop_test = false;
|
|
static int parse_arguments(int argc, char **argv)
|
|
{
|
|
int c;
|
|
while ((c = utils_getopt(argc, argv, "c:l")) != EOF) switch (c) {
|
|
case 'c':
|
|
if (HAL_SetDevInfoFile(utils_optarg))
|
|
return -1;
|
|
break;
|
|
|
|
case 'l':
|
|
sg_loop_test = true;
|
|
break;
|
|
|
|
default:
|
|
HAL_Printf(
|
|
"usage: %s [options]\n"
|
|
" [-c <config file for DeviceInfo>] \n"
|
|
" [-l ] loop test or not\n",
|
|
argv[0]);
|
|
return -1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int rc;
|
|
// init log level
|
|
IOT_Log_Set_Level(eLOG_DEBUG);
|
|
|
|
// parse arguments for device info file and loop test;
|
|
rc = parse_arguments(argc, argv);
|
|
if (rc != QCLOUD_RET_SUCCESS) {
|
|
Log_e("parse arguments error, rc = %d", rc);
|
|
return rc;
|
|
}
|
|
|
|
DeviceInfo device_info = {0};
|
|
rc = HAL_GetDevInfo((void *)&device_info);
|
|
if (QCLOUD_RET_SUCCESS != rc) {
|
|
Log_e("get device info failed: %d", rc);
|
|
return rc;
|
|
}
|
|
|
|
// init connection
|
|
MQTTInitParams init_params = DEFAULT_MQTTINIT_PARAMS;
|
|
rc = _setup_connect_init_params(&init_params, &device_info);
|
|
if (rc != QCLOUD_RET_SUCCESS) {
|
|
Log_e("init params error, rc = %d", rc);
|
|
return rc;
|
|
}
|
|
|
|
#ifdef LOG_UPLOAD
|
|
// _init_log_upload should be done after _setup_connect_init_params and before IOT_MQTT_Construct
|
|
rc = _init_log_upload(&init_params);
|
|
if (rc != QCLOUD_RET_SUCCESS)
|
|
Log_e("init log upload error, rc = %d", rc);
|
|
#endif
|
|
|
|
// create MQTT client and connect with server
|
|
void *client = IOT_MQTT_Construct(&init_params);
|
|
if (client != NULL) {
|
|
Log_i("Cloud Device Construct Success");
|
|
} else {
|
|
Log_e("MQTT Construct failed!");
|
|
IOT_Log_Upload(true);
|
|
return QCLOUD_ERR_FAILURE;
|
|
}
|
|
|
|
#ifdef SYSTEM_COMM
|
|
long time = 0;
|
|
// get system timestamp from server
|
|
rc = IOT_Get_SysTime(client, &time);
|
|
if (QCLOUD_RET_SUCCESS == rc) {
|
|
Log_i("system time is %ld", time);
|
|
} else {
|
|
Log_e("get system time failed!");
|
|
}
|
|
#endif
|
|
|
|
// subscribe normal topics and wait result
|
|
rc = _subscribe_topic_wait_result(client, "data", QOS0);
|
|
if (rc) {
|
|
Log_e("Client Subscribe Topic Failed: %d", rc);
|
|
IOT_Log_Upload(true);
|
|
return rc;
|
|
}
|
|
|
|
do {
|
|
rc = _publish_test_msg(client, "data", QOS1);
|
|
if (rc < 0) {
|
|
Log_e("client publish topic failed :%d.", rc);
|
|
}
|
|
|
|
rc = IOT_MQTT_Yield(client, 500);
|
|
if (rc == QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT) {
|
|
HAL_SleepMs(1000);
|
|
continue;
|
|
} else if (rc != QCLOUD_RET_SUCCESS && rc != QCLOUD_RET_MQTT_RECONNECTED) {
|
|
Log_e("exit with error: %d", rc);
|
|
break;
|
|
}
|
|
|
|
if (sg_loop_test)
|
|
HAL_SleepMs(1000);
|
|
|
|
} while (sg_loop_test);
|
|
|
|
rc = IOT_MQTT_Destroy(&client);
|
|
|
|
IOT_Log_Upload(true);
|
|
|
|
return rc;
|
|
}
|