Instrument Neutral Distributed Interface INDI 2.2.5
hotplugmanager.h
Go to the documentation of this file.
1/*
2 Hot Plug Manager Class Header File
3
4 Copyright (C) 2026 Jasem Mutlaq ([email protected])
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2.1 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#pragma once
22
24#include "inditimer.h"
25#include <memory>
26#include <vector>
27#include <mutex>
28#include <map>
29#include <atomic> // For std::atomic_bool
30#include <chrono> // For std::chrono::steady_clock::time_point
31
32#ifdef HAVE_UDEV
33#include <libudev.h> // For udev hotplugging
34#endif
35
36namespace INDI
37{
38
40{
41 private:
42 HotPlugManager(); // Private constructor for singleton
43 ~HotPlugManager(); // Private destructor
44
45 public:
46 // Delete copy constructor and assignment operator to prevent copying
47 HotPlugManager(const HotPlugManager &) = delete;
49
55
59 static const char* getDeviceName()
60 {
61 return "HotPlugManager";
62 }
63
68 void registerHandler(std::shared_ptr<HotPlugCapableDevice> handler);
69
74 void unregisterHandler(std::shared_ptr<HotPlugCapableDevice> handler);
75
81 void start(uint32_t intervalMs, bool oneShot = false);
82
86 void stop();
87
93 void setNonUdevPollingDuration(int seconds);
94
100 void setInitialPollingDuration(int seconds);
101
102 private:
106 void checkHotPlugEvents();
107 bool initUdev();
108 void deinitUdev();
109
114 void handleUdevEvent(int fd);
115
119 static void udevCallbackWrapper(int fd, void* userdata);
120
121 std::vector<std::shared_ptr<HotPlugCapableDevice>> registeredHandlers;
122 INDI::Timer hotPlugTimer;
123 std::recursive_mutex handlersMutex; // Mutex to protect access to registeredHandlers
124
125#ifdef HAVE_UDEV
126 udev* udevContext;
127 udev_monitor* udevMonitor;
128 int udevCallbackId; // Callback ID for event loop
129#endif
130 std::atomic_int pollingCount;
131 std::atomic_bool oneShotMode;
132 std::chrono::steady_clock::time_point nonUdevPollingStartTime;
133 std::atomic_int nonUdevPollingDurationSeconds; // Configurable max duration for non-udev polling (-1 = default 60s, 0 = unlimited)
134 std::atomic_int initialPollingDurationSeconds; // Configurable max duration for initial polling with udev (-1 = default 5s)
135 std::atomic_bool udevEventReceived;
136 INDI::Timer mainThreadDebounceTimer;
137};
138
139} // namespace INDI
static HotPlugManager & getInstance()
Get the singleton instance of HotPlugManager.
void registerHandler(std::shared_ptr< HotPlugCapableDevice > handler)
Register a HotPlugCapableDevice handler with the manager.
HotPlugManager & operator=(const HotPlugManager &)=delete
static const char * getDeviceName()
Static name used for LOGGING purposes.
void start(uint32_t intervalMs, bool oneShot=false)
Starts the hot-plugging timer.
void setInitialPollingDuration(int seconds)
Set the maximum initial polling duration for systems with udev.
void setNonUdevPollingDuration(int seconds)
Set the maximum polling duration for non-udev systems (macOS, Windows).
void unregisterHandler(std::shared_ptr< HotPlugCapableDevice > handler)
Unregister a HotPlugCapableDevice handler from the manager.
HotPlugManager(const HotPlugManager &)=delete
void stop()
Stops the hot-plugging timer.
The Timer class provides repetitive and single-shot timers.
Definition inditimer.h:41
int fd
Namespace to encapsulate INDI client, drivers, and mediator classes.