intrepidcs / icsscand

User-mode SocketCAN daemon for Intrepid devices
BSD 2-Clause "Simplified" License
10 stars 6 forks source link

Allow the device scan interval to be specified #12

Closed j-rge closed 10 months ago

j-rge commented 1 year ago

icsscand polls once a second for devices. During the search, each interface on the machine is placed into promiscuous mode. This act can have undesirable side effects and in addition, pollutes the kernel log every time the search takes place, e.g.:

[284098.101395] device lo entered promiscuous mode
[284098.167519] device lo left promiscuous mode
[284098.209400] device wlp38s0 entered promiscuous mode
[284098.275514] device wlp38s0 left promiscuous mode
[284098.317401] device sfpp-0 entered promiscuous mode
[284098.383552] device sfpp-0 left promiscuous mode
[284098.417402] device sfpp-1 entered promiscuous mode
[284098.483500] device sfpp-1 left promiscuous mode
[284098.529398] device qsfp28-1 entered promiscuous mode
[284098.611811] device qsfp28-1 left promiscuous mode
[284098.645835] device qsfp28-0 entered promiscuous mode
[284098.715736] device qsfp28-0 left promiscuous mode
[284098.769422] device docker0 entered promiscuous mode
[284098.843682] device docker0 left promiscuous mode
[284099.898728] device company entered promiscuous mode
[284099.971562] device company left promiscuous mode
[284100.137367] device lo entered promiscuous mode
[284100.211430] device lo left promiscuous mode
[284100.265373] device wlp38s0 entered promiscuous mode
[284100.335497] device wlp38s0 left promiscuous mode
[284100.365371] device sfpp-0 entered promiscuous mode
[284100.431420] device sfpp-0 left promiscuous mode
[284100.461369] device sfpp-1 entered promiscuous mode
[284100.527419] device sfpp-1 left promiscuous mode
[284100.573371] device qsfp28-1 entered promiscuous mode
[284100.639480] device qsfp28-1 left promiscuous mode
[284100.669618] device qsfp28-0 entered promiscuous mode
[284100.735487] device qsfp28-0 left promiscuous mode
[284100.773392] device docker0 entered promiscuous mode
[284100.847423] device docker0 left promiscuous mode
. . .

The following diff will allow the user to specify (--scan-interval-ms) how often the scanning takes place, and if an interval of 0 is specified, the scanning will only occur once. If not specified, the scanning will behave as it currently does.

diff --git a/src/main.cpp b/src/main.cpp
index 5f5de68..a5135b6 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -45,6 +45,8 @@
 #define GET_RX_BOX(DEVICE_INDEX)       (reinterpret_cast<uint8_t*>(sharedMemory) + (RX_BOX_SIZE * DEVICE_INDEX))
 #define GET_TX_BOX(INDEX)              (reinterpret_cast<uint8_t*>(sharedMemory) + (sharedMemSize / 2) + (INDEX * TX_BOX_SIZE))

+#define DEFAULT_SCAN_INTERVAL_MS 1000
+
 bool runningAsDaemon = false;
 int driver = 0; // /dev/intrepid_netdevice
 int driverMajor = 0;
@@ -54,6 +56,7 @@ int maxInterfaces = 0; // From driver
 int sharedMemSize = 0; // From driver
 void* sharedMemory = nullptr;
 std::string serialFilter;
+int scan_interval_ms = DEFAULT_SCAN_INTERVAL_MS;

 std::atomic<bool> stopRunning(false);

@@ -219,10 +222,11 @@ void usage(std::string executableName) {
        std::cerr << "Copyright 2019-2023 Intrepid Control Systems, Inc.\n\n";
        std::cerr << "Usage: " << executableName << " [option]\n\n";
        std::cerr << "Options:\n";
-       std::cerr << "\t-d,     --daemon\t\tRun as a daemon in the background\n";
-       std::cerr << "\t-h, -?, --help, --usage\t\tShow this help page\n";
-       std::cerr << "\t        --devices\t\tList supported devices\n";
-       std::cerr << "\t        --filter <serial>\tOnly connect to devices with serial\n\t\t\t\t\tnumbers starting with this filter\n";
+       std::cerr << "\t-d,     --daemon\t\t\tRun as a daemon in the background\n";
+       std::cerr << "\t-h, -?, --help, --usage\t\t\tShow this help page\n";
+       std::cerr << "\t        --devices\t\t\tList supported devices\n";
+       std::cerr << "\t        --filter <serial>\t\tOnly connect to devices with serial\n\t\t\t\t\t\tnumbers starting with this filter\n";
+       std::cerr << "\t        --scan-interval-ms <interval>\tDevice scan interval in ms\n\t\t\t\t\t\tIf 0, only a single scan is performed\n";
 }

 void terminateSignal(int signal) {
@@ -378,7 +382,10 @@ void searchForDevices() {
 void deviceSearchThread() {
        while(!stopRunning) {
                searchForDevices();
-               std::this_thread::sleep_for(std::chrono::milliseconds(1000));
+               if (scan_interval_ms == 0) {
+                       break;
+               }
+               std::this_thread::sleep_for(std::chrono::milliseconds(scan_interval_ms));
        }
 }

@@ -399,6 +406,9 @@ int main(int argc, char** argv) {
                } else if(arg == "--filter" && i + 1 <= argc) {
                        serialFilter = argv[++i];
                        transform(serialFilter.begin(), serialFilter.end(), serialFilter.begin(), ::toupper);
+               } else if(arg == "--scan-interval-ms" && i + 1 <= argc) {
+                       scan_interval_ms = std::atoi(argv[i + 1]);
+                       i++;
                } else {
                        usage(argv[0]);
                        return EX_USAGE;
j-rge commented 1 year ago

See pull request #13

j-rge commented 10 months ago

PR #13 merged