From 2c29f2ec19aff21105fa8bcdcf259176310fc07c Mon Sep 17 00:00:00 2001 From: pandx Date: Thu, 24 Oct 2024 20:54:29 +0800 Subject: [PATCH] refine thread func file. --- threadfunc/thread_func.cpp | 178 +++++++++++++++++++------------------ 1 file changed, 90 insertions(+), 88 deletions(-) diff --git a/threadfunc/thread_func.cpp b/threadfunc/thread_func.cpp index cec3e30..001de67 100644 --- a/threadfunc/thread_func.cpp +++ b/threadfunc/thread_func.cpp @@ -30,10 +30,10 @@ static long long connect_time = 0; // ms Dial dial; void StartCgiServer() { - zlog_info(zbt,"start deal cgi"); + zlog_info(zbt, "start deal cgi"); while (1) { - TcpCgi::startCgiServer(); + TcpCgi::startCgiServer(); sleep(10); } } @@ -56,7 +56,10 @@ void HeartRep() { jsHeart["IP"] = GlobalConfig::IpAddr_G; std::string strJson = fw.write(jsHeart); 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) { gpio_set(GlobalConfig::GPIO_G.errorLed, 1); sleep(1); @@ -128,7 +131,7 @@ open4G: string signal = GetOneContent(csq, 13, ","); GlobalConfig::NetSignal = atoi(signal.c_str()); 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)); offSize = 0; @@ -136,16 +139,17 @@ open4G: } usleep(10000); } 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); offSize = offSize + ret; - zlog_info(zct,"szbuffer = %s", szbuffer); + zlog_info(zct, "szbuffer = %s", szbuffer); continue; } sleep(10); } #endif } + void Dial5G() { sleep(2); dial5G: @@ -158,13 +162,13 @@ dial5G: } 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.zigAckreset, 1); // ACK reset InitGpio(GlobalConfig::GPIO_G.zigReset, 1); // Zigbee reset gpio_set(GlobalConfig::GPIO_G.zigAckreset, 1); gpio_set(GlobalConfig::GPIO_G.zigReset, 1); - zlog_info(zbt,"GPIO Init"); + zlog_info(zbt, "GPIO Init"); #ifdef IMX6UL_GATEWAY InitGpio(GlobalConfig::GPIO_G.errorLed, 1); //指示灯 @@ -190,6 +194,7 @@ void UartStart() { uart_inst::instance().UartRecv(uart_inst::instance().fd, 1, buffer); sleep(1); } + void InitModule() { #ifdef NR5G_MODULE #ifdef G2UL_GATEWAY @@ -201,12 +206,13 @@ void InitModule() { InitGpio(GlobalConfig::GPIO_G.commRest, 1); gpio_set(GlobalConfig::GPIO_G.commRest, 0); //高电平复位 -#endif +#endif // G2UL_GATEWAY #ifdef IMX6UL_GATEWAY InitGpio(GlobalConfig::GPIO_G.commRest, 1); gpio_set(GlobalConfig::GPIO_G.commRest, 1); // -#endif -#endif +#endif // IMX6UL_GATEWAY +#endif // NR5G_MODULE + #ifdef Q4G_MODULE #ifdef G2UL_GATEWAY InitGpio(GlobalConfig::GPIO_G.commPower, 1); // 4G,5G模组供电, @@ -218,10 +224,10 @@ void InitModule() { std::string strAPN = ReadStrByOpt(SERVERCONFIG, "Server", "APN"); sprintf(szquectel, "/opt/quectel-CM/quectel-CM -s %s &", strAPN.c_str()); system(szquectel); -#endif -#endif +#endif // G2UL_GATEWAY +#endif // Q4G_MODULE #ifdef WIFI_MODULE - zlog_info(zbt,"Init WiFi!"); + zlog_info(zbt, "Init WiFi!"); #ifdef IMX6UL_GATEWAY InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位,0复位,1取消复位 @@ -231,7 +237,7 @@ void InitModule() { wpa.ReconnectWiFi(); system("/etc/init.d/wpa_restart"); system("udhcpc -i wlan2 &"); -#endif +#endif // IMX6UL_GATEWAY #ifdef G2UL_GATEWAY InitGpio(GlobalConfig::GPIO_G.wifiReset, 1); // WiFi模组复位,1复位,0取消复位 gpio_set(GlobalConfig::GPIO_G.wifiReset, 0); @@ -242,9 +248,10 @@ void InitModule() { wpa.ReconnectWiFi(); system("/usr/sbin/wpa_supplicant -Dnl80211 -iwlan0 -c/etc/wpa_supplicant.conf &"); system("udhcpc -b -i wlan0 &"); -#endif -#endif +#endif // G2UL_GATEWAY +#endif // WIFI_MODULE } + void TestUart() { uart_inst::instance().InitTestUart(B115200); uart_inst::instance().ReadTestUart(); @@ -253,6 +260,7 @@ void TestUart() { void UartStartWave() { uart_inst::instance().DealWaveThread(); } void StartUdpSys() { udp_sys::instance().StartConnectSysUdp(); } + void WatchDog() { int fd = OpenWatchDog(); while (1) { @@ -261,87 +269,79 @@ void WatchDog() { } close(fd); } -//static const char *LOCAL_WILL_TOPIC = "up/uart/will"; + void my_publish_callback(struct mosquitto *mosq, void *obj, int mid) {} void my_connect_callback(struct mosquitto *mosq, void *obj, int result) { struct userdata *ud; ud = (struct userdata *)obj; + if (result) { + zlog_error(zct, "%s", mosquitto_connack_string(result)); + return; + } + int ret = 0; + for (int i = 0; i < ud->topic_count; i++) { + zlog_info(zct, "mosquitto_subscribe ud->topics[%d]:%s", i, ud->topics[i]); + ret = mosquitto_subscribe(mosq, NULL, ud->topics[i], ud->topic_qos); + if (ret != MOSQ_ERR_SUCCESS) { + zlog_error(zct, "fail to subscribe, ret: [%s], topic: %s", mosquitto_strerror(ret), ud->topics[i]); + return; + } + } - if (!result) { - for (int i = 0; i < ud->topic_count; 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); - zlog_info(zct,"mosquitto_subscribe ret:%d", iret); + ret = mosquitto_subscribe(mosq, NULL, GlobalConfig::Topic_G.mSubData.c_str(), 1); + 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}; + GetTimeNet(gwTime, 0); + connect_time = strtoll(gwTime, NULL, 10); + zlog_info(zct, "connect_time:%lld", connect_time); + long long difftime_ms = connect_time - connect_lost_time; + if (difftime_ms > 20 * 1000) { // 超过20秒,判定为连接断开 + char reply_string[256] = {0}; + std::string startStatus = "0"; + if (access(SYSTEMSTART, 0) >= 0) { + startStatus = GetFileContent(SYSTEMSTART, 1); } + sprintf(reply_string, "{\"dataNodeGatewayNo\":\"%s\",\"softVersion\":\"%s\",\"status\":\"%s\"}", GlobalConfig::MacAddr_G.c_str(), GlobalConfig::Version.c_str(), startStatus.c_str()); - int iret = mosquitto_subscribe(mosq, NULL, GlobalConfig::Topic_G.mSubData.c_str(), 1); - zlog_info(zct,"mosquitto_subscribe's return value: %d", iret); + Json::Value jsData; + Json::Value jsVal; + Json::FastWriter fw; + jsData["cmd"] = "15"; + jsData["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; - char gwTime[32] = {0}; - GetTimeNet(gwTime, 0); - connect_time = strtoll(gwTime, NULL, 10); - zlog_info(zct,"connect_time:%lld", connect_time); - long long difftime_ms = connect_time - connect_lost_time; - if (difftime_ms > 20 * 1000) { // 超过20秒,判定为连接断开 - char reply_string[256] = {0}; - std::string startStatus = "0"; - if (access(SYSTEMSTART, 0) >= 0) { - startStatus = GetFileContent(SYSTEMSTART, 1); - } - sprintf(reply_string, "{\"dataNodeGatewayNo\":\"%s\",\"softVersion\":\"%s\",\"status\":\"%s\"}", GlobalConfig::MacAddr_G.c_str(), GlobalConfig::Version.c_str(), startStatus.c_str()); + std::string strCmd15 = fw.write(jsData); - Json::Value jsData; - Json::Value jsVal; - Json::FastWriter fw; - jsData["cmd"] = "15"; - jsData["dataNodeGatewayNo"] = GlobalConfig::MacAddr_G; - - std::string strCmd15 = fw.write(jsData); - - std::string instr = std::string(reply_string); - std::string topic = "equipment/state/" + GlobalConfig::MacAddr_G; - int ret = data_publish(instr.c_str(), topic.c_str()); - if (ret != MOSQ_ERR_SUCCESS) { - zlog_info(zct,"Publish failed:%d, %s", ret, instr.c_str()); - disconnect(); - } - } - GlobalConfig::LinkStatus_G = 1; - zlog_info(zct,"Connect to server success."); - - char buf[256] = {0}; - sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str()); - std::string str = std::string(buf); - 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 - } + std::string instr = std::string(reply_string); + std::string topic = "equipment/state/" + GlobalConfig::MacAddr_G; + ret = data_publish(instr.c_str(), topic.c_str()); + if (ret != MOSQ_ERR_SUCCESS) { + zlog_error(zct, "Publish failed:%d, %s", ret, instr.c_str()); + disconnect(); } } + GlobalConfig::LinkStatus_G = 1; + zlog_info(zct, "Connect to server success."); + + char buf[256] = {0}; + sprintf(buf, "{\"dataNodeGatewayNo\":\"%s\",\"cmd\":\"12\",\"status\":\"REQ\"}", GlobalConfig::MacAddr_G.c_str()); + std::string str = std::string(buf); + std::string runinfo = "本地服务器连接成功"; } void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result) { disconnect(); - zlog_info(zct,"The MQTT connection lost\n"); + zlog_info(zct, "The MQTT connection lost\n"); char gwTime[32] = {0}; GetTimeNet(gwTime, 0); uptime = std::string(gwTime); 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::LinkCount = GlobalConfig::LinkCount + 1; @@ -371,7 +371,7 @@ void my_disconnect_callback(struct mosquitto *mosq, void *obj, int result) { memset(buf, 0, sizeof(buf)); 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 @@ -395,13 +395,13 @@ void my_message_callback(struct mosquitto *mosq, void *obj, const struct mosquit if (ud->verbose) { if (message->payloadlen) { 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); if (ud->eol) { } } else { if (ud->eol) { - zlog_info(zct,"%s (null)\n", message->topic); + zlog_info(zct, "%s (null)\n", message->topic); } } fflush(stdout); @@ -420,9 +420,9 @@ void my_subscribe_callback(struct mosquitto *mosq, void *obj, int mid, int qos_c struct userdata *ud; assert(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++) { - 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() { - zlog_info(zct,"start mqtt \n"); + zlog_info(zct, "start mqtt \n"); std::string runinfo = "MQTT通信模块启动"; while (1) { @@ -487,9 +487,10 @@ void RecvUpdateFile() { bzero(buffer, 1024); fp = fopen("/tmp/upgrade.tar.gz", "w"); if (NULL == fp) { - zlog_info(zct,"File:\t Can Not Open To Write"); - exit(1); + zlog_error(zbt, "File:/tmp/upgrade.tar.gz Can Not Open"); + return; } + while ((len = socket.read_some(boost::asio::buffer(buffer), ec))) { write_len = fwrite(buffer, sizeof(char), len, fp); if (write_len < len) { @@ -498,13 +499,14 @@ void RecvUpdateFile() { } bzero(buffer, 1024); } - zlog_info(zct, "Receive File From Server Finished!"); + zlog_info(zbt, "Receive File From Server Finished!"); fclose(fp); Json::Value jsData; Json::FastWriter fw; jsData["cmd"] = "03"; jsData["updatefilename"] = "updatefile"; std::string str = fw.write(jsData); - system("/etc/init.d/sysupgrade.sh"); + zlog_warn(zbt, "==============upgrade start==============="); + system("/etc/init.d/sysupgrade.sh"); } }