WLG/mqttclient/mqtt_client.cpp

274 lines
9.3 KiB
C++
Raw Normal View History

2024-10-23 09:22:06 +08:00
#include "mqtt_client.h"
2024-10-19 16:02:41 +08:00
#include <signal.h>
#include <stdio.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <memory.h>
#include <errno.h>
#include <json/json.h>
#include <sys/types.h>
2024-10-23 19:51:01 +08:00
#include <zlog.h>
2024-10-23 09:22:06 +08:00
#include "common/global.hpp"
#include "utility/secure.hpp"
2024-10-19 16:02:41 +08:00
2024-10-24 16:01:21 +08:00
extern zlog_category_t *zct;
2024-10-24 20:36:27 +08:00
extern zlog_category_t *zbt;
2024-10-24 16:01:21 +08:00
2024-10-19 16:02:41 +08:00
static char res_topic[100];
static char req_topic[100];
struct mosquitto *mosq = NULL;
static int mid_sent = 0;
struct userdata ud;
connect_callback my_connect_callback = NULL;
disconnect_callback my_disconnect_callback = NULL;
message_callback my_message_callback = NULL;
subscribe_callback my_subscribe_callback = NULL;
log_callback my_log_callback = NULL;
publish_callback my_publish_callback = NULL;
2024-10-22 19:04:25 +08:00
void register_collback(connect_callback connect_c, message_callback message_c, subscribe_callback subscribe_c, log_callback log_c, disconnect_callback disconnect_c, publish_callback publish_c) {
2024-10-19 16:02:41 +08:00
my_connect_callback = connect_c;
my_message_callback = message_c;
my_subscribe_callback = subscribe_c;
my_log_callback = log_c;
my_disconnect_callback = disconnect_c;
my_publish_callback = publish_c;
}
2024-10-22 19:04:25 +08:00
int data_publish(const char *str, const char *topic) {
2024-10-24 20:36:27 +08:00
int ret = -1;
2024-10-22 19:04:25 +08:00
if (mosq != NULL) {
2024-10-19 16:02:41 +08:00
std::string strTopic = std::string(topic);
2024-10-22 19:04:25 +08:00
if (strTopic.find("cmd") != std::string::npos || strTopic.find("configureInfo") != std::string::npos) {
2024-10-19 16:02:41 +08:00
std::string pData = (std::string)str;
char *base64_aes = new char[pData.length() * 2];
memset(base64_aes, 0, pData.length() * 2);
2024-10-24 16:01:21 +08:00
secure::instance().Base64Encode((unsigned char *)pData.c_str(), base64_aes, pData.length());
2024-10-24 20:36:27 +08:00
ret = mosquitto_publish(mosq, &mid_sent, topic, strlen(base64_aes), base64_aes, ud.topic_qos, false);
2024-10-23 19:51:01 +08:00
2024-10-22 19:04:25 +08:00
if (base64_aes != NULL) delete[] base64_aes;
2024-10-24 20:36:27 +08:00
if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to send mqtt msg, ret: [%s], topic: %s, str: %s", mosquitto_strerror(ret), topic, str);
}
2024-10-19 16:02:41 +08:00
} else {
2024-10-24 20:36:27 +08:00
ret = mosquitto_publish(mosq, &mid_sent, topic, strlen(str), str, ud.topic_qos, false);
if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to send mqtt msg, ret: [%s], topic: %s, str: %s", mosquitto_strerror(ret), topic, str);
}
2024-10-19 16:02:41 +08:00
}
}
2024-10-24 20:36:27 +08:00
if (ret != 0) {
2024-10-22 19:04:25 +08:00
disconnect();
2024-10-19 16:02:41 +08:00
}
2024-10-24 20:36:27 +08:00
return ret;
2024-10-19 16:02:41 +08:00
}
2024-10-22 19:04:25 +08:00
int data_publish_wave(WAVE_CONTAIN *str, const char *topic) {
2024-10-24 20:36:27 +08:00
int ret = -1;
2024-10-22 19:04:25 +08:00
if (mosq != NULL) {
2024-10-24 16:01:21 +08:00
zlog_info(zct, "data_publish:Unit : %s type:%d flag : %d channelid : %s total : %d count : %d number :%d floats:%f floate:%f\n", str->SensorEngineeringUnit, str->type, str->flag, str->channelId, str->total, str->count, str->number, str->waveData[0],
2024-10-24 20:36:27 +08:00
str->waveData[(str->number - 2)]);
ret = mosquitto_publish(mosq, &mid_sent, topic, sizeof(WAVE_CONTAIN), str, ud.topic_qos, false);
if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to send mqtt msg, ret: [%s], topic: %s, str: %s", mosquitto_strerror(ret), topic, str);
}
2024-10-19 16:02:41 +08:00
}
2024-10-24 20:36:27 +08:00
return ret;
2024-10-19 16:02:41 +08:00
}
2024-10-22 19:04:25 +08:00
int subscribe(const char *topic, int qos) {
2024-10-24 20:36:27 +08:00
int ret = mosquitto_subscribe(mosq, NULL, topic, qos);
if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to subscribe, ret: [%s], topic: %s", mosquitto_strerror(ret), topic);
}
return ret;
2024-10-19 16:02:41 +08:00
}
2024-10-22 19:04:25 +08:00
int unsubscribe(const char *topic) {
2024-10-24 20:36:27 +08:00
int ret = mosquitto_unsubscribe(mosq, NULL, topic);
if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to unsubscribe, ret: [%s], topic: %s", mosquitto_strerror(ret), topic);
}
return ret;
2024-10-19 16:02:41 +08:00
}
2024-10-22 19:04:25 +08:00
int disconnect() { return mosquitto_disconnect(mosq); }
2024-10-19 16:02:41 +08:00
2024-10-22 19:04:25 +08:00
int reconnect() { return mosquitto_reconnect(mosq); }
int start_client(const char *boardname, const char *gwid, const char *gwip, const char *gwver, const char *gwcode, std::string &salt) {
2024-10-19 16:02:41 +08:00
char *id = NULL;
char *id_prefix = NULL;
char *host;
int port = 51613;
char *bind_address = NULL;
int keepalive = 60;
bool clean_session = true;
int rc = 0;
char hostname[256];
int len;
char will_payload[100];
long will_payloadlen = 0;
int will_qos = 0;
bool will_retain = false;
char *will_topic = NULL;
char *psk = NULL;
char *psk_identity = NULL;
mqttsdata_t mqttsdata;
2024-10-22 19:04:25 +08:00
signal(SIGCHLD, SIG_IGN); // signal函数作用为一个信号注册一个信号处理函数。其中SIGCHLD在一个进程终止或者停止时将SIGCHLD信号发送给父进程。而信号处理函数是一个函数指针不过它使用的是预定义函数SIG_IGN(忽视信号)
2024-10-19 16:02:41 +08:00
signal(SIGPIPE, SIG_IGN);
if (my_connect_callback == NULL || my_message_callback == NULL || my_subscribe_callback == NULL || my_log_callback == NULL) {
2024-10-24 20:36:27 +08:00
zlog_error(zct, "please call the register_collback function first.");
2024-10-19 16:02:41 +08:00
return -1;
}
2024-10-24 20:36:27 +08:00
int qos = readIntValue("config", "qos", (char *)GlobalConfig::Config_G.c_str());
2024-10-19 16:02:41 +08:00
memset(&mqttsdata, 0, sizeof(mqttsdata_t));
memcpy(mqttsdata.gwid, gwid, strlen(gwid));
salt = std::string(mqttsdata.salt);
memset(&ud, 0, sizeof(struct userdata));
ud.eol = true;
char localip[100];
sprintf(localip, "%s", gwip);
host = localip;
mqttsdata.port = GlobalConfig::ServerPort;
port = GlobalConfig::ServerPort;
#ifdef WIFI_MODULE
mqttsdata.qos = 0;
#else
mqttsdata.qos = qos;
#endif
2024-10-24 20:36:27 +08:00
zlog_info(zct, "mqttsdata.qos = %d", mqttsdata.qos);
2024-10-19 16:02:41 +08:00
std::string username = ReadStrByOpt(SERVERCONFIG, "Server", "UserName");
std::string password = ReadStrByOpt(SERVERCONFIG, "Server", "Password");
2024-10-22 19:04:25 +08:00
if (username == "" || password == "") {
username = "chaos";
password = "HSD272*#xkd";
2024-10-19 16:02:41 +08:00
}
char userName[100];
2024-10-24 20:36:27 +08:00
memset(userName, 0, 100);
2024-10-19 16:02:41 +08:00
sprintf(userName, "%s", username.c_str());
char passWord[100];
2024-10-24 20:36:27 +08:00
memset(passWord, 0, 100);
2024-10-19 16:02:41 +08:00
sprintf(passWord, "%s", password.c_str());
2024-10-24 20:36:27 +08:00
zlog_info(zbt, "userName = %s , passWord = %s", userName, passWord);
2024-10-19 16:02:41 +08:00
ud.username = userName;
ud.password = passWord;
ud.gwid = mqttsdata.gwid;
ud.dataUrl = mqttsdata.dataUrl;
ud.fsUrl = mqttsdata.fsUrl;
will_qos = mqttsdata.qos;
ud.topic_qos = will_qos;
ud.topic_count++;
2024-10-22 19:04:25 +08:00
ud.topics = (char **)realloc(ud.topics, ud.topic_count * sizeof(char *));
2024-10-19 16:02:41 +08:00
if (ud.topics == NULL) {
2024-10-24 20:36:27 +08:00
zlog_error(zbt, "fail to realloc for ud.topics");
2024-10-19 16:02:41 +08:00
return 1;
}
memset(req_topic, 0, 100);
sprintf(req_topic, "dev/cidwcc");
ud.topics[ud.topic_count - 1] = req_topic;
ud.verbose = 1;
2024-10-23 19:51:01 +08:00
2024-10-19 16:02:41 +08:00
memset(res_topic, 0, 100);
sprintf(res_topic, "gw/will");
2024-10-24 20:36:27 +08:00
zlog_info(zct, "res_topic:%s", res_topic);
2024-10-19 16:02:41 +08:00
will_topic = res_topic;
ud.resp_topic = res_topic;
sprintf(will_payload, "{\"cmd\":\"15\",\"gwID\":\"%s\"}", mqttsdata.gwid);
will_payloadlen = strlen(will_payload);
2024-10-24 20:36:27 +08:00
zlog_info(zct, "will_payload:%s,will_payloadlen:%ld", will_payload, will_payloadlen);
hostname[0] = '\0';
gethostname(hostname, 256);
hostname[255] = '\0';
len = strlen("mosqsub/-") + 6 + strlen(hostname);
mosquitto_lib_init();
mosq = mosquitto_new(NULL, true, &ud);
2024-10-22 19:04:25 +08:00
if (!mosq) {
2024-10-24 20:36:27 +08:00
zlog_error(zct, "fail to mosquitto_new, errno:%d", errno);
free(ud.topics);
2024-10-19 16:02:41 +08:00
mosquitto_lib_cleanup();
return 1;
}
2024-10-24 20:36:27 +08:00
mosquitto_log_callback_set(mosq, my_log_callback);
2024-10-19 16:02:41 +08:00
if (!psk) {
psk = (char *)malloc(32 * sizeof(char));
if (!psk) {
2024-10-24 20:36:27 +08:00
free(ud.topics);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
2024-10-24 16:01:21 +08:00
zlog_error(zct, "Error: No free space 1");
2024-10-19 16:02:41 +08:00
return -1;
}
memset(psk, 0, 32);
2024-10-24 16:01:21 +08:00
snprintf(psk, 32, "%d", getpid());
2024-10-19 16:02:41 +08:00
psk_identity = (char *)malloc(32 * sizeof(char));
if (!psk_identity) {
2024-10-24 20:36:27 +08:00
free(ud.topics);
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
2024-10-24 16:01:21 +08:00
zlog_error(zct, "Error: No free space 2");
2024-10-19 16:02:41 +08:00
free(psk);
return -1;
}
strncpy(psk_identity, psk, 32);
}
2024-10-24 20:36:27 +08:00
if (mosquitto_will_set(mosq, will_topic, will_payloadlen, will_payload, will_qos, will_retain)) {
zlog_error(zct, "Error: Problem setting will.");
goto mosq_free;
2024-10-19 16:02:41 +08:00
}
if (ud.username && mosquitto_username_pw_set(mosq, ud.username, ud.password)) {
2024-10-24 20:36:27 +08:00
zlog_error(zct, "Error: Problem setting username and password.");
goto mosq_free;
2024-10-19 16:02:41 +08:00
}
mosquitto_connect_callback_set(mosq, my_connect_callback);
mosquitto_disconnect_callback_set(mosq, my_disconnect_callback);
mosquitto_message_callback_set(mosq, my_message_callback);
mosquitto_publish_callback_set(mosq, my_publish_callback);
2024-10-24 20:36:27 +08:00
mosquitto_subscribe_callback_set(mosq, my_subscribe_callback);
2024-10-19 16:02:41 +08:00
2024-10-24 20:36:27 +08:00
rc = mosquitto_connect_bind(mosq, host, port, keepalive, bind_address);
zlog_info(zct, "host = %s port = %d rc = %d", host, port, rc);
2024-10-22 19:04:25 +08:00
if (rc) {
2024-10-24 20:36:27 +08:00
zlog_error(zct, "Unable to connect (%d).", rc);
2024-10-19 16:02:41 +08:00
goto mosq_free;
}
2024-10-24 20:36:27 +08:00
2024-10-19 16:02:41 +08:00
rc = mosquitto_loop_forever(mosq, -1, 1);
2024-10-22 19:04:25 +08:00
if (rc) {
2024-10-23 19:51:01 +08:00
zlog_error(zct, "Error: %s", mosquitto_strerror(rc));
2024-10-19 16:02:41 +08:00
}
2024-10-24 20:36:27 +08:00
2024-10-19 16:02:41 +08:00
mosq_free:
mosquitto_destroy(mosq);
mosquitto_lib_cleanup();
2024-10-24 20:36:27 +08:00
if (ud.topics != NULL) {
free(ud.topics);
}
if (psk) {
free(psk);
}
if (psk_identity) {
free(psk_identity);
}
2024-10-19 16:02:41 +08:00
sleep(1);
2024-10-24 16:01:21 +08:00
return 0;
2024-10-19 16:02:41 +08:00
}