refine thread func file.

This commit is contained in:
pandx 2024-10-24 20:54:29 +08:00
parent 82d74b6f08
commit 2c29f2ec19

View File

@ -30,7 +30,7 @@ static long long connect_time = 0; // ms
Dial dial; Dial dial;
void StartCgiServer() { void StartCgiServer() {
zlog_info(zbt,"start deal cgi"); zlog_info(zbt, "start deal cgi");
while (1) { while (1) {
TcpCgi::startCgiServer(); TcpCgi::startCgiServer();
@ -56,7 +56,10 @@ void HeartRep() {
jsHeart["IP"] = GlobalConfig::IpAddr_G; jsHeart["IP"] = GlobalConfig::IpAddr_G;
std::string strJson = fw.write(jsHeart); std::string strJson = fw.write(jsHeart);
int iRet = data_publish(strJson.c_str(), GlobalConfig::Topic_G.mPubHeart.c_str()); int iRet = data_publish(strJson.c_str(), GlobalConfig::Topic_G.mPubHeart.c_str());
zlog_info(zct,"heart = %s,iRet = %d", strJson.c_str(), iRet); if (iRet != 0) {
zlog_error(zct, "fail to send heart mqtt msg");
}
zlog_info(zct, "heart = %s,iRet = %d", strJson.c_str(), iRet);
if (iRet != 0) { if (iRet != 0) {
gpio_set(GlobalConfig::GPIO_G.errorLed, 1); gpio_set(GlobalConfig::GPIO_G.errorLed, 1);
sleep(1); sleep(1);
@ -128,7 +131,7 @@ open4G:
string signal = GetOneContent(csq, 13, ","); string signal = GetOneContent(csq, 13, ",");
GlobalConfig::NetSignal = atoi(signal.c_str()); GlobalConfig::NetSignal = atoi(signal.c_str());
GlobalConfig::NetType = GetOneContent(csq, 2, ","); GlobalConfig::NetType = GetOneContent(csq, 2, ",");
zlog_info(zct,"NetStatus = %s,NetSignal = %d", GlobalConfig::NetStatus.c_str(), GlobalConfig::NetSignal); zlog_info(zct, "NetStatus = %s,NetSignal = %d", GlobalConfig::NetStatus.c_str(), GlobalConfig::NetSignal);
} }
memset(szbuffer, 0x00, sizeof(szbuffer)); memset(szbuffer, 0x00, sizeof(szbuffer));
offSize = 0; offSize = 0;
@ -136,16 +139,17 @@ open4G:
} }
usleep(10000); usleep(10000);
} else if (ret > 0) { } else if (ret > 0) {
zlog_info(zct,"ret = %d,buff = %s", ret, buff); zlog_info(zct, "ret = %d,buff = %s", ret, buff);
memcpy(szbuffer + offSize, buff, ret); memcpy(szbuffer + offSize, buff, ret);
offSize = offSize + ret; offSize = offSize + ret;
zlog_info(zct,"szbuffer = %s", szbuffer); zlog_info(zct, "szbuffer = %s", szbuffer);
continue; continue;
} }
sleep(10); sleep(10);
} }
#endif #endif
} }
void Dial5G() { void Dial5G() {
sleep(2); sleep(2);
dial5G: dial5G:
@ -158,13 +162,13 @@ dial5G:
} }
void UartStart() { void UartStart() {
zlog_info(zbt,"zigAckrep = %d,zigAckreset = %d,zigReset = %d,errorLed = %d,power = %d", GlobalConfig::GPIO_G.zigAckrep, GlobalConfig::GPIO_G.zigAckreset, GlobalConfig::GPIO_G.zigReset, GlobalConfig::GPIO_G.errorLed, GlobalConfig::GPIO_G.power); zlog_info(zbt, "zigAckrep = %d,zigAckreset = %d,zigReset = %d,errorLed = %d,power = %d", GlobalConfig::GPIO_G.zigAckrep, GlobalConfig::GPIO_G.zigAckreset, GlobalConfig::GPIO_G.zigReset, GlobalConfig::GPIO_G.errorLed, GlobalConfig::GPIO_G.power);
InitGpio(GlobalConfig::GPIO_G.zigAckrep, 0); // ACK InitGpio(GlobalConfig::GPIO_G.zigAckrep, 0); // ACK
InitGpio(GlobalConfig::GPIO_G.zigAckreset, 1); // ACK reset InitGpio(GlobalConfig::GPIO_G.zigAckreset, 1); // ACK reset
InitGpio(GlobalConfig::GPIO_G.zigReset, 1); // Zigbee reset InitGpio(GlobalConfig::GPIO_G.zigReset, 1); // Zigbee reset
gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1);
gpio_set(GlobalConfig::GPIO_G.zigReset, 1); gpio_set(GlobalConfig::GPIO_G.zigReset, 1);
zlog_info(zbt,"GPIO Init"); zlog_info(zbt, "GPIO Init");
#ifdef IMX6UL_GATEWAY #ifdef IMX6UL_GATEWAY
InitGpio(GlobalConfig::GPIO_G.errorLed, 1); //指示灯 InitGpio(GlobalConfig::GPIO_G.errorLed, 1); //指示灯
@ -190,6 +194,7 @@ void UartStart() {
uart_inst::instance().UartRecv(uart_inst::instance().fd, 1, buffer); uart_inst::instance().UartRecv(uart_inst::instance().fd, 1, buffer);
sleep(1); sleep(1);
} }
void InitModule() { void InitModule() {
#ifdef NR5G_MODULE #ifdef NR5G_MODULE
#ifdef G2UL_GATEWAY #ifdef G2UL_GATEWAY
@ -201,12 +206,13 @@ void InitModule() {
InitGpio(GlobalConfig::GPIO_G.commRest, 1); InitGpio(GlobalConfig::GPIO_G.commRest, 1);
gpio_set(GlobalConfig::GPIO_G.commRest, 0); //高电平复位 gpio_set(GlobalConfig::GPIO_G.commRest, 0); //高电平复位
#endif #endif // G2UL_GATEWAY
#ifdef IMX6UL_GATEWAY #ifdef IMX6UL_GATEWAY
InitGpio(GlobalConfig::GPIO_G.commRest, 1); InitGpio(GlobalConfig::GPIO_G.commRest, 1);
gpio_set(GlobalConfig::GPIO_G.commRest, 1); // gpio_set(GlobalConfig::GPIO_G.commRest, 1); //
#endif #endif // IMX6UL_GATEWAY
#endif #endif // NR5G_MODULE
#ifdef Q4G_MODULE #ifdef Q4G_MODULE
#ifdef G2UL_GATEWAY #ifdef G2UL_GATEWAY
InitGpio(GlobalConfig::GPIO_G.commPower, 1); // 4G,5G模组供电 InitGpio(GlobalConfig::GPIO_G.commPower, 1); // 4G,5G模组供电
@ -218,10 +224,10 @@ void InitModule() {
std::string strAPN = ReadStrByOpt(SERVERCONFIG, "Server", "APN"); std::string strAPN = ReadStrByOpt(SERVERCONFIG, "Server", "APN");
sprintf(szquectel, "/opt/quectel-CM/quectel-CM -s %s &", strAPN.c_str()); sprintf(szquectel, "/opt/quectel-CM/quectel-CM -s %s &", strAPN.c_str());
system(szquectel); system(szquectel);
#endif #endif // G2UL_GATEWAY
#endif #endif // Q4G_MODULE
#ifdef WIFI_MODULE #ifdef WIFI_MODULE
zlog_info(zbt,"Init WiFi!"); zlog_info(zbt, "Init WiFi!");
#ifdef IMX6UL_GATEWAY #ifdef IMX6UL_GATEWAY
InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位0复位1取消复位 InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位0复位1取消复位
@ -231,7 +237,7 @@ void InitModule() {
wpa.ReconnectWiFi(); wpa.ReconnectWiFi();
system("/etc/init.d/wpa_restart"); system("/etc/init.d/wpa_restart");
system("udhcpc -i wlan2 &"); system("udhcpc -i wlan2 &");
#endif #endif // IMX6UL_GATEWAY
#ifdef G2UL_GATEWAY #ifdef G2UL_GATEWAY
InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位1复位0取消复位 InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位1复位0取消复位
gpio_set(GlobalConfig::GPIO_G.wifiReset, 0); gpio_set(GlobalConfig::GPIO_G.wifiReset, 0);
@ -242,9 +248,10 @@ void InitModule() {
wpa.ReconnectWiFi(); wpa.ReconnectWiFi();
system("/usr/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf &"); system("/usr/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf &");
system("udhcpc -b -i wlan0 &"); system("udhcpc -b -i wlan0 &");
#endif #endif // G2UL_GATEWAY
#endif #endif // WIFI_MODULE
} }
void TestUart() { void TestUart() {
uart_inst::instance().InitTestUart(B115200); uart_inst::instance().InitTestUart(B115200);
uart_inst::instance().ReadTestUart(); uart_inst::instance().ReadTestUart();
@ -253,6 +260,7 @@ void TestUart() {
void UartStartWave() { uart_inst::instance().DealWaveThread(); } void UartStartWave() { uart_inst::instance().DealWaveThread(); }
void StartUdpSys() { udp_sys::instance().StartConnectSysUdp(); } void StartUdpSys() { udp_sys::instance().StartConnectSysUdp(); }
void WatchDog() { void WatchDog() {
int fd = OpenWatchDog(); int fd = OpenWatchDog();
while (1) { while (1) {
@ -261,27 +269,37 @@ void WatchDog() {
} }
close(fd); close(fd);
} }
//static const char *LOCAL_WILL_TOPIC = "up/uart/will";
void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) {} void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) {}
void my_connect_callback(struct mosquitto *mosq, void *obj, int result) { void my_connect_callback(struct mosquitto *mosq, void *obj, int result) {
struct userdata *ud; struct userdata *ud;
ud = (struct userdata *)obj; ud = (struct userdata *)obj;
if (result) {
if (!result) { zlog_error(zct, "%s", mosquitto_connack_string(result));
return;
}
int ret = 0;
for (int i = 0; i < ud->topic_count; i++) { for (int i = 0; i < ud->topic_count; i++) {
zlog_info(zct,"mosquitto_subscribe ud->topics[%d]:%s", i, ud->topics[i]); zlog_info(zct, "mosquitto_subscribe ud->topics[%d]:%s", i, ud->topics[i]);
int iret = mosquitto_subscribe(mosq, NULL, ud->topics[i], ud->topic_qos); ret = mosquitto_subscribe(mosq, NULL, ud->topics[i], ud->topic_qos);
zlog_info(zct,"mosquitto_subscribe ret:%d", iret); if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to subscribe, ret: [%s], topic: %s", mosquitto_strerror(ret), ud->topics[i]);
return;
}
} }
int iret = mosquitto_subscribe(mosq, NULL, GlobalConfig::Topic_G.mSubData.c_str(), 1); ret = mosquitto_subscribe(mosq, NULL, GlobalConfig::Topic_G.mSubData.c_str(), 1);
zlog_info(zct,"mosquitto_subscribe's return value: %d", iret); if (ret != MOSQ_ERR_SUCCESS) {
zlog_error(zct, "fail to subscribe, ret: [%s], topic: %s", mosquitto_strerror(ret), GlobalConfig::Topic_G.mSubData.c_str());
return;
}
zlog_info(zct, "mosquitto_subscribe's return value: %d", ret);
char gwTime[32] = {0}; char gwTime[32] = {0};
GetTimeNet(gwTime, 0); GetTimeNet(gwTime, 0);
connect_time = strtoll(gwTime, NULL, 10); connect_time = strtoll(gwTime, NULL, 10);
zlog_info(zct,"connect_time:%lld", connect_time); zlog_info(zct, "connect_time:%lld", connect_time);
long long difftime_ms = connect_time - connect_lost_time; long long difftime_ms = connect_time - connect_lost_time;
if (difftime_ms > 20 * 1000) { // 超过20秒判定为连接断开 if (difftime_ms > 20 * 1000) { // 超过20秒判定为连接断开
char reply_string[256] = {0}; char reply_string[256] = {0};
@ -301,47 +319,29 @@ void my_connect_callback(struct mosquitto *mosq, void *obj, int result) {
std::string instr = std::string(reply_string); std::string instr = std::string(reply_string);
std::string topic = "equipment/state/" + GlobalConfig::MacAddr_G; std::string topic = "equipment/state/" + GlobalConfig::MacAddr_G;
int ret = data_publish(instr.c_str(), topic.c_str()); ret = data_publish(instr.c_str(), topic.c_str());
if (ret != MOSQ_ERR_SUCCESS) { if (ret != MOSQ_ERR_SUCCESS) {
zlog_info(zct,"Publish failed:%d, %s", ret, instr.c_str()); zlog_error(zct, "Publish failed:%d, %s", ret, instr.c_str());
disconnect(); disconnect();
} }
} }
GlobalConfig::LinkStatus_G = 1; GlobalConfig::LinkStatus_G = 1;
zlog_info(zct,"Connect to server success."); zlog_info(zct, "Connect to server success.");
char buf[256] = {0}; char buf[256] = {0};
sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str()); sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str());
std::string str = std::string(buf); std::string str = std::string(buf);
std::string runinfo = "本地服务器连接成功"; std::string runinfo = "本地服务器连接成功";
} else {
if (result && !ud->quiet) {
fprintf(stderr, "%s\n", mosquitto_connack_string(result));
if (result == 1) {
// Connection Refused: unacceptable protocol version
} else if (result == 2) {
// Connection Refused: identifier rejected
} else if (result == 3) {
// Connection Refused: server unavailable
} else if (result == 4) {
// Connection Refused: bad user name or password
// exit(0);
} else if (result == 5) {
// Connection Refused: not authorized
}
}
}
} }
void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result) { void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result) {
disconnect(); disconnect();
zlog_info(zct,"The MQTT connection lost\n"); zlog_info(zct, "The MQTT connection lost\n");
char gwTime[32] = {0}; char gwTime[32] = {0};
GetTimeNet(gwTime, 0); GetTimeNet(gwTime, 0);
uptime = std::string(gwTime); uptime = std::string(gwTime);
connect_lost_time = strtoll(uptime.c_str(), NULL, 10); connect_lost_time = strtoll(uptime.c_str(), NULL, 10);
zlog_info(zct,"connect_lost_time:%lld\n", connect_lost_time); zlog_info(zct, "connect_lost_time:%lld\n", connect_lost_time);
GlobalConfig::LinkStatus_G = 0; GlobalConfig::LinkStatus_G = 0;
GlobalConfig::LinkCount = GlobalConfig::LinkCount + 1; GlobalConfig::LinkCount = GlobalConfig::LinkCount + 1;
@ -371,7 +371,7 @@ void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result) {
memset(buf, 0, sizeof(buf)); memset(buf, 0, sizeof(buf));
sprintf(buf, "wifi RSSI:%s,state:%s,MQTT reconnect :%d\n", RSSI.c_str(), state.c_str(), iRet); sprintf(buf, "wifi RSSI:%s,state:%s,MQTT reconnect :%d\n", RSSI.c_str(), state.c_str(), iRet);
zlog_info(zct,"%s\n", buf); zlog_info(zct, "%s\n", buf);
#else #else
@ -395,13 +395,13 @@ void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquit
if (ud->verbose) { if (ud->verbose) {
if (message->payloadlen) { if (message->payloadlen) {
std::string strtopic(message->topic); std::string strtopic(message->topic);
zlog_info(zct,"strtopic : %s \n", strtopic.c_str()); zlog_info(zct, "strtopic : %s \n", strtopic.c_str());
LocalServer::HandleFromServer((const char *)message->payload, message->payloadlen, message->topic); LocalServer::HandleFromServer((const char *)message->payload, message->payloadlen, message->topic);
if (ud->eol) { if (ud->eol) {
} }
} else { } else {
if (ud->eol) { if (ud->eol) {
zlog_info(zct,"%s (null)\n", message->topic); zlog_info(zct, "%s (null)\n", message->topic);
} }
} }
fflush(stdout); fflush(stdout);
@ -420,9 +420,9 @@ void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_c
struct userdata *ud; struct userdata *ud;
assert(obj); assert(obj);
ud = (struct userdata *)obj; ud = (struct userdata *)obj;
if (!ud->quiet) zlog_info(zct,"Subscribed (mid: %d): %d", mid, granted_qos[0]); zlog_info(zct, "Subscribed (mid: %d): %d", mid, granted_qos[0]);
for (i = 1; i < qos_count; i++) { for (i = 1; i < qos_count; i++) {
if (!ud->quiet) zlog_info(zct,", %d", granted_qos[i]); zlog_info(zct, ", %d", granted_qos[i]);
} }
} }
@ -437,7 +437,7 @@ void my_log_callback(struct mosquitto *mosq, void *obj, int level, const char *s
} }
void StartMqttClient() { void StartMqttClient() {
zlog_info(zct,"start mqtt \n"); zlog_info(zct, "start mqtt \n");
std::string runinfo = "MQTT通信模块启动"; std::string runinfo = "MQTT通信模块启动";
while (1) { while (1) {
@ -487,9 +487,10 @@ void RecvUpdateFile() {
bzero(buffer, 1024); bzero(buffer, 1024);
fp = fopen("/tmp/upgrade.tar.gz", "w"); fp = fopen("/tmp/upgrade.tar.gz", "w");
if (NULL == fp) { if (NULL == fp) {
zlog_info(zct,"File:\t Can Not Open To Write"); zlog_error(zbt, "File:/tmp/upgrade.tar.gz Can Not Open");
exit(1); return;
} }
while ((len = socket.read_some(boost::asio::buffer(buffer), ec))) { while ((len = socket.read_some(boost::asio::buffer(buffer), ec))) {
write_len = fwrite(buffer, sizeof(char), len, fp); write_len = fwrite(buffer, sizeof(char), len, fp);
if (write_len < len) { if (write_len < len) {
@ -498,13 +499,14 @@ void RecvUpdateFile() {
} }
bzero(buffer, 1024); bzero(buffer, 1024);
} }
zlog_info(zct, "Receive File From Server Finished!"); zlog_info(zbt, "Receive File From Server Finished!");
fclose(fp); fclose(fp);
Json::Value jsData; Json::Value jsData;
Json::FastWriter fw; Json::FastWriter fw;
jsData["cmd"] = "03"; jsData["cmd"] = "03";
jsData["updatefilename"] = "updatefile"; jsData["updatefilename"] = "updatefile";
std::string str = fw.write(jsData); std::string str = fw.write(jsData);
zlog_warn(zbt, "==============upgrade start===============");
system("/etc/init.d/sysupgrade.sh"); system("/etc/init.d/sysupgrade.sh");
} }
} }