Explorar o código

add app entry parameter

xuqiang hai 3 meses
pai
achega
f21df0b718
Modificáronse 5 ficheiros con 88 adicións e 7 borrados
  1. 11 2
      include/defs.h
  2. 1 1
      scripts/run.sh
  3. 4 1
      src/broadcast.cpp
  4. 69 2
      src/main.cpp
  5. 3 1
      src/mainwindow.cpp

+ 11 - 2
include/defs.h

@@ -6,8 +6,6 @@
 #define NETPARSE_FIFO_PATH      "/tmp/netparsefifo"
 #define CALCULATE_FIFO_PATH     "/tmp/calculatefifo"
 
-#define BROADCAST_PORT 60999
-
 // cpu temperature
 #define BIG_CORE0   "/sys/class/thermal/thermal_zone1"  // bigcore0
 #define BIG_CORE1   "/sys/class/thermal/thermal_zone2"  // bigcore1
@@ -23,4 +21,15 @@
 // global settings file
 #define GLOBAL_SETTINGS_FILE "storage/conf/settings.ini"
 
+// app parameter
+enum {
+    OPT_BROADCAST = 1000
+};
+
+typedef struct
+{
+    char *iface = NULL;
+    int broadcast_port = -1;
+}app_param_st;
+
 #endif

+ 1 - 1
scripts/run.sh

@@ -7,4 +7,4 @@ export QT_QPA_PLATFORMTHEME=qt5ct
 
 # run
 export LD_LIBRARY_PATH=./lib:$LD_LIBRARY_PATH
-./bin/monitor
+./bin/monitor -i eth0 --broadcast 37000

+ 4 - 1
src/broadcast.cpp

@@ -1,6 +1,9 @@
 #include "broadcast.h"
 #include "utils.h"
 #include <QJsonDocument>
+#include "defs.h"
+
+extern app_param_st g_appParam;
 
 Broadcast::Broadcast(const int port, QObject *parent)
     : QObject{parent}
@@ -36,7 +39,7 @@ bool Broadcast::init(const int port)
 
     char *ip = NULL;
     char *mask = NULL;
-    bool result = Utils::ipv4("eth0", &ip, &mask);
+    bool result = Utils::ipv4(g_appParam.iface, &ip, &mask);
     if(!result) {
         close(sockfd);
         exit(EXIT_FAILURE);

+ 69 - 2
src/main.cpp

@@ -5,10 +5,29 @@
 #include "appcontext.h"
 #include "version.h"
 #include "utils.h"
+#include "netparseprocess.h"
+#include "calprocess.h"
+#include "defs.h"
+
+#include <getopt.h>
 
 #define SCREEN_WIDTH    1280
 #define SCREEN_HEIGHT   720
 
+app_param_st g_appParam;
+
+void print_help(const char *prog)
+{
+    std::cout <<
+        "Usage: " << prog << " [OPTIONS]\n\n"
+        "Options:\n"
+        "  -i, --iface <name>       Network interface name (required)\n"
+        "      --broadcast <port>   Broadcast port number (1-65535)\n"
+        "  -h, --help               Show this help message\n\n"
+        "Examples:\n"
+        "  " << prog << " -i eth0 --broadcast 8888\n";
+}
+
 int main(int argc, char *argv[])
 {
 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
@@ -16,6 +35,51 @@ int main(int argc, char *argv[])
 #endif
     QCoreApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
     QApplication app(argc, argv);
+    /* parse argv */
+    static struct option long_options[] = {
+        {"iface",     required_argument, 0, 'i'},
+        {"broadcast", required_argument, 0, OPT_BROADCAST},
+        {"help",      no_argument,       0, 'h'},
+        {0, 0, 0, 0}
+    };
+    int opt;
+    while ((opt = getopt_long(argc, argv, "i:h", long_options, nullptr)) != -1) {
+        switch (opt) {
+        case 'i':
+            g_appParam.iface = optarg;
+            break;
+
+        case OPT_BROADCAST:
+            g_appParam.broadcast_port = std::atoi(optarg);
+            if (g_appParam.broadcast_port <= 0 || g_appParam.broadcast_port > 65535) {
+                std::cerr << "error: invalid broadcast port: " << optarg << "\n";
+                return EXIT_FAILURE;
+            }
+            break;
+
+        case 'h':
+            print_help(argv[0]);
+            return EXIT_SUCCESS;
+
+        default:
+            std::cerr << "unknown option";
+            std::cerr << "try '" << argv[0] << " --help' for more information.\n";
+            return EXIT_FAILURE;
+        }
+    }
+
+    /* 必选参数检查 */
+    if (!g_appParam.iface) {
+        std::cerr << "Error: --iface is required\n";
+        std::cerr << "Try '" << argv[0] << " --help' for more information.\n";
+        return EXIT_FAILURE;
+    }
+
+    if (g_appParam.broadcast_port == -1) {
+        std::cerr << "Error: --broadcast is required\n";
+        std::cerr << "Try '" << argv[0] << " --help' for more information.\n";
+        return EXIT_FAILURE;
+    }
 
 #ifdef NDEBUG
     Logger::setup(Logger::INFO, "storage/logs/log.txt");
@@ -24,10 +88,12 @@ int main(int argc, char *argv[])
 #endif
 
     CRITICAL_LOG(spdlog::level::info, "app start...");
+    CRITICAL_LOG(spdlog::level::info, "iface:          {}", g_appParam.iface);
+    CRITICAL_LOG(spdlog::level::info, "broadcast port: {}", g_appParam.broadcast_port);
 
     char *ip = NULL;
     char *mask = NULL;
-    bool result = Utils::ipv4("eth0", &ip, &mask);
+    bool result = Utils::ipv4(g_appParam.iface, &ip, &mask);
 
     CRITICAL_LOG(spdlog::level::info, "PROJECT_NAME:         {}", PROJECT_NAME);
     CRITICAL_LOG(spdlog::level::info, "PROJECT_VERSION:      {}", PROJECT_VERSION);
@@ -53,11 +119,12 @@ int main(int argc, char *argv[])
     int frequency = QString(argv[1]).toInt();
 #endif
 
-    AppContext &appCtx = AppContext::instance();
     MainWindow w;
     w.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
     w.showFullScreen();
 
+    AppContext &appCtx = AppContext::instance();
+
     int ret = app.exec();
     return ret;
 }

+ 3 - 1
src/mainwindow.cpp

@@ -12,11 +12,13 @@
 #include <time.h>
 #include "defs.h"
 
+extern app_param_st g_appParam;
+
 MainWindow::MainWindow(QWidget *parent)
     : QMainWindow(parent)
     , m_pStackedWidget(nullptr)
     , m_pErrStrLabel(nullptr)
-    , m_broadcast(Broadcast(BROADCAST_PORT))
+    , m_broadcast(Broadcast(g_appParam.broadcast_port))
     , m_packageCount("0")
 {
     setWindowFlags(Qt::FramelessWindowHint | Qt::Window);