39 char configPort[256] = {0};
57 int autoSearchIndex = 0;
95 bool Serial::ISNewText(
const char *dev,
const char *name,
char *texts[],
char *names[],
int n)
108 return !strcmp(PortT[0].text, onePort.c_str());
112 LOGF_DEBUG(
"Auto search is disabled because %s is not a system port.",
PortT[0].text);
147 LOG_INFO(
"Auto search is enabled. When connecting, the driver shall attempt to "
148 "communicate with all available system ports until a connection is "
151 LOG_INFO(
"Auto search is disabled.");
199 LOGF_WARN(
"Communication with %s @ %d failed. Starting Auto Search...",
PortT[0].text,
202 std::this_thread::sleep_for(std::chrono::milliseconds(500 + (rand() % 1000)));
206 std::vector<std::string> systemPorts;
216 std::random_device rd;
217 std::minstd_rand g(rd());
218 std::shuffle(systemPorts.begin(), systemPorts.end(), g);
220 std::vector<std::string> doubleSearch = systemPorts;
223 systemPorts.push_back(
PortT[0].text);
226 systemPorts.insert(systemPorts.end(), doubleSearch.begin(), doubleSearch.end());
228 for (
const auto &
port : systemPorts)
237 bool saveConfig =
false;
267 std::this_thread::sleep_for(std::chrono::milliseconds(500 + (rand() % 1000)));
276 LOG_DEBUG(
"Connection successful, attempting handshake...");
307 LOGF_WARN(
"Port %s is already used by another driver or process.",
port);
312 LOGF_ERROR(
"Failed to connect to port (%s). Error: %s",
port, errorMsg);
389 #if defined(__APPLE__)
390 static const char *filter_names[] = {
"cu.",
nullptr };
392 static const char *filter_names[] = {
"ttyUSB",
"ttyACM",
nullptr };
396 for (filter = filter_names; *filter; ++filter)
398 if (strstr(entry->d_name, *filter) !=
nullptr)
408 static const char *filter_names[] = {
"usb-",
nullptr };
411 for (filter = filter_names; *filter; ++filter)
413 if (strstr(entry->d_name, *filter) !=
nullptr)
423 static const char *filter_names[] = {
"rfcomm",
nullptr };
426 for (filter = filter_names; *filter; ++filter)
428 if (strstr(entry->d_name, *filter) !=
nullptr)
438 std::vector<std::string> m_Ports;
441 auto searchPath = [&](std::string prefix, uint8_t searchType)
443 struct dirent **namelist;
444 std::vector<std::string> detectedDevices;
456 if (detectedDevices.size() < 10)
458 std::string s(namelist[devCount]->d_name);
459 s.erase(s.find_last_not_of(
" \n\r\t") + 1);
460 detectedDevices.push_back(prefix + s);
464 LOGF_DEBUG(
"Ignoring devices over %d : %s", detectedDevices.size(),
465 namelist[devCount]->d_name);
467 free(namelist[devCount]);
472 return detectedDevices;
477 const std::vector<std::string> serialDevices = searchPath(
"/dev/",
SERIAL_DEV);
478 const std::vector<std::string> usbIDDevices = searchPath(
"/dev/serial/by-id/",
USB_ID_DEV);
479 const std::vector<std::string> btDevices = searchPath(
"/dev/",
BLUETOOTH_DEV);
480 m_Ports.insert(m_Ports.end(), btDevices.begin(), btDevices.end());
485 if (usbIDDevices.size() >= serialDevices.size())
486 m_Ports.insert(m_Ports.end(), usbIDDevices.begin(), usbIDDevices.end());
488 m_Ports.insert(m_Ports.end(), serialDevices.begin(), serialDevices.end());
490 const std::vector<std::string> serialDevices = searchPath(
"/dev/",
SERIAL_DEV);
491 m_Ports.insert(m_Ports.end(), serialDevices.begin(), serialDevices.end());
494 const int pCount = m_Ports.size();
499 LOG_WARN(
"No candidate ports found on the system.");
505 LOGF_INFO(
"Scan complete. Found %d port(s).", pCount);
525 for (
const auto &onePort : m_Ports)
528 std::string
name = onePort.substr(onePort.find_last_of(
"/\\") + 1);
533 std::regex re(
"usb-(.[^-]+)");
535 if (std::regex_search(onePort, match, re))
539 std::regex target(
"FTDI_|UART_|USB_|Bridge_Controller_|to_");
540 label = std::regex_replace(
label, target,
"");
542 if (
label.length() <= 2)
543 label = match.str(1);
The Interface class is the base class for all INDI connection plugins.
const char * getDeviceName()
std::function< bool()> Handshake
INDI::DefaultDevice * m_Device
ISwitchVectorProperty RefreshSP
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
virtual std::string name() override
BaudRate
Supported baud rates.
ITextVectorProperty PortTP
std::vector< std::string > m_SystemPorts
virtual void Activated() override
Activated Function called by the framework when the plugin is activated (i.e. selected by the user)....
bool Refresh(bool silent=false)
ISwitchVectorProperty BaudRateSP
void setDefaultBaudRate(BaudRate newRate)
setDefaultBaudRate Set default baud rate. The default baud rate is 9600 unless otherwise changed by t...
virtual void Deactivated() override
Deactivated Function called by the framework when the plugin is deactivated. It is usually used to de...
ISwitchVectorProperty SystemPortSP
ISwitchVectorProperty AutoSearchSP
Serial(INDI::DefaultDevice *dev)
virtual bool Disconnect() override
Disconnect Disconnect from device.
virtual bool saveConfigItems(FILE *fp) override
void setDefaultPort(const char *port)
setDefaultPort Set default port. Call this function in initProperties() of your driver if you want to...
virtual bool Connect() override
Connect Connect to device via the implemented communication medium. Do not perform any handshakes.
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
virtual bool processHandshake()
virtual std::string label() override
virtual const char * port()
const char * getDeviceName() const
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
bool isInitializationComplete() const
isInitializationComplete Check if driver initialization is complete.
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void defineProperty(INumberVectorProperty *property)
bool isSimulation() const
int tty_connect(const char *device, int bit_rate, int word_size, int parity, int stop_bits, int *fd)
Establishes a tty connection to a terminal device.
int tty_disconnect(int fd)
Closes a tty connection and flushes the bus.
void tty_error_msg(int err_code, char *err_msg, int err_msg_len)
Retrieve the tty error message.
Implementations for common driver routines.
void IUSaveConfigSwitch(FILE *fp, const ISwitchVectorProperty *svp)
Add a switch vector property value to the configuration file.
int IUFindOnSwitchIndex(const ISwitchVectorProperty *svp)
Returns the index of first ON switch it finds in the vector switch property.
void IUResetSwitch(ISwitchVectorProperty *svp)
Reset all switches in a switch vector property to OFF.
void IUFillTextVector(ITextVectorProperty *tvp, IText *tp, int ntp, const char *dev, const char *name, const char *label, const char *group, IPerm p, double timeout, IPState s)
Assign attributes for a text vector property. The vector's auxiliary elements will be set to NULL.
ISwitch * IUFindOnSwitch(const ISwitchVectorProperty *svp)
Returns the first ON switch it finds in the vector switch property.
void IUSaveText(IText *tp, const char *newtext)
Function to reliably save new text in a IText.
void IUFillSwitch(ISwitch *sp, const char *name, const char *label, ISState s)
Assign attributes for a switch property. The switch's auxiliary elements will be set to NULL.
void IUFillText(IText *tp, const char *name, const char *label, const char *initialText)
Assign attributes for a text property. The text's auxiliary elements will be set to NULL.
void IUSaveConfigText(FILE *fp, const ITextVectorProperty *tvp)
Add a text vector property value to the configuration file.
void IUFillSwitchVector(ISwitchVectorProperty *svp, ISwitch *sp, int nsp, const char *dev, const char *name, const char *label, const char *group, IPerm p, ISRule r, double timeout, IPState s)
Assign attributes for a switch vector property. The vector's auxiliary elements will be set to NULL.
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
void IDSetSwitch(const ISwitchVectorProperty *svp, const char *fmt,...)
int IUUpdateText(ITextVectorProperty *tvp, char *texts[], char *names[], int n)
Update all text members in a text vector property.
int IUGetConfigText(const char *dev, const char *property, const char *member, char *value, int len)
IUGetConfigText Opens configuration file and reads single text property.
int IUGetConfigOnSwitchIndex(const char *dev, const char *property, int *index)
IUGetConfigOnSwitchIndex Opens configuration file and reads single switch property to find ON switch ...
void IDSetText(const ITextVectorProperty *tvp, const char *fmt,...)
#define LOGF_INFO(fmt,...)
#define LOGF_WARN(fmt,...)
#define LOGF_DEBUG(fmt,...)
#define LOGF_ERROR(fmt,...)
Combines all INDI Connection Plugins. Each INDI connection plugin is responsible of managing communic...
const char * CONNECTION_TAB
int usb_dev_file_select(const dirent *entry)
int bluetooth_dev_file_select(const dirent *entry)
int serial_dev_file_select(const dirent *entry)
const char * DEVICE_AUTO_SEARCH
Toggle device auto search. If enabled and on connection failure with the default port,...
const char * DEVICE_PORT
Device serial (or bluetooth) connection port. The default value on Linux is /dev/ttyUSB0 while on Mac...
const char * DEVICE_BAUD_RATE
Set device baud rate.