Instrument Neutral Distributed Interface INDI
1.9.5
|
Go to the documentation of this file.
32 #include <arpa/inet.h>
33 #include <netinet/in.h>
34 #include <sys/socket.h>
51 m_ConfigHost = defaultHostname;
56 IUFillText(&AddressT[0],
"ADDRESS",
"Address", defaultHostname);
57 IUFillText(&AddressT[1],
"PORT",
"Port", defaultPort);
61 int connectionTypeIndex = 0;
63 m_ConfigConnectionType = connectionTypeIndex;
69 int autoSearchIndex = 1;
84 bool TCP::ISNewText(
const char *dev,
const char *
name,
char *texts[],
char *names[],
int n)
128 LOG_INFO(
"LAN search is enabled. When connecting, the driver shall attempt to "
129 "communicate with all devices on the local network until a connection is "
132 LOG_INFO(
"Auto search is disabled.");
147 struct sockaddr_in serv_addr;
148 struct hostent *hp =
nullptr;
158 LOGF_INFO(
"Connecting to %s@%s ...", hostname.c_str(),
port.c_str());
160 LOGF_DEBUG(
"Connecting to %s@%s ...", hostname.c_str(),
port.c_str());
164 hp = gethostbyname(hostname.c_str());
168 LOG_ERROR(
"Failed to lookup IP Address or hostname.");
172 memset(&serv_addr, 0,
sizeof(serv_addr));
173 serv_addr.sin_family = AF_INET;
174 serv_addr.sin_addr.s_addr = ((
struct in_addr *)(hp->h_addr_list[0]))->s_addr;
175 serv_addr.sin_port = htons(atoi(
port.c_str()));
180 socketType = SOCK_STREAM;
184 socketType = SOCK_DGRAM;
187 if ((
m_SockFD = socket(AF_INET, socketType, 0)) < 0)
194 setsockopt(
m_SockFD, SOL_SOCKET, SO_RCVTIMEO, &ts,
sizeof(
struct timeval));
195 setsockopt(
m_SockFD, SOL_SOCKET, SO_SNDTIMEO, &ts,
sizeof(
struct timeval));
198 if (::connect(
m_SockFD, (
struct sockaddr *)&serv_addr,
sizeof(serv_addr)) < 0)
201 LOGF_ERROR(
"Failed to connect to %s@%s: %s.", hostname.c_str(),
port.c_str(), strerror(
errno));
218 LOG_ERROR(
"Error! Server address is missing or invalid.");
223 std::string hostname =
AddressT[0].text;
229 std::regex ipv4(
"^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$");
230 const auto isIPv4 = regex_match(hostname, ipv4);
239 size_t found = hostname.find_last_of(
".");
241 if (found != std::string::npos)
244 const auto sourceSubnet = hostname.substr(0, found);
245 std::deque<std::string> subnets;
249 for (
auto &oneInterfaceAddress : addrs_ipv4)
252 if (oneInterfaceAddress.rfind(
"127", 0) == 0)
255 size_t found = oneInterfaceAddress.find_last_of(
".");
256 if (found != std::string::npos)
259 const auto targetSubnet = oneInterfaceAddress.substr(0, found);
261 if (targetSubnet == sourceSubnet)
262 subnets.push_front(targetSubnet);
264 subnets.push_back(targetSubnet);
269 for (
auto &oneSubnet : subnets)
271 LOGF_INFO(
"Searching %s subnet, this operation will take a few minutes to complete. Stand by...", oneSubnet.c_str());
275 for (
int i = 1; i < 255; i++)
277 const auto newAddress = oneSubnet +
"." + std::to_string(i);
278 if (newAddress == hostname)
284 LOGF_DEBUG(
"Connection to %s@%s is successful, attempting handshake...", hostname.c_str(),
port.c_str());
288 hostname = newAddress;
303 LOGF_DEBUG(
"Connection to %s@%s is successful, attempting handshake...", hostname.c_str(),
port.c_str());
395 snprintf(portStr, 8,
"%d", addressPort);
std::function< bool()> Handshake
ITextVectorProperty AddressTP
Properties.
bool establishConnection(const std::string &hostname, const std::string &port, int timeout=-1)
establishConnection Create a socket connection to the host and port. If successful,...
#define LOGF_ERROR(fmt,...)
virtual bool Disconnect() override
Disconnect Disconnect from device.
const char * DEVICE_ADDRESS
Device hostname and port. It is part of Connection::TCPInterface to manage connections to devices ove...
void IDSetText(const ITextVectorProperty *t, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Tell client to update an existing text vector property.
bool isSimulation() const
void defineProperty(INumberVectorProperty *property)
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.
ISwitchVectorProperty LANSearchSP
bool isInitializationComplete() const
isInitializationComplete Check if driver initialization is complete.
const char * getDeviceName() const
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.
int IUUpdateText(ITextVectorProperty *tvp, char *texts[], char *names[], int n)
Update all text members in a text vector property.
virtual bool saveConfigItems(FILE *fp) override
void IUResetSwitch(ISwitchVectorProperty *svp)
Reset all switches in a switch vector property to OFF.
const char * getDeviceName()
virtual bool Connect() override
Connect Connect to device via the implemented communication medium. Do not perform any handshakes.
#define LOGF_DEBUG(fmt,...)
int IUGetConfigText(const char *dev, const char *property, const char *member, char *value, int len)
IUGetConfigText Opens configuration file and reads single text property.
void IUSaveConfigText(FILE *fp, const ITextVectorProperty *tvp)
Add a text vector property value to the configuration file.
void setConnectionType(int type)
TODO should be renamed to setDefaultConnectionType.
virtual Type type()
type Return connection type
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.
INDI::DefaultDevice * m_Device
const char * CONNECTION_TAB
CONNECTION_TAB Where all device connection settings (serial, usb, ethernet) are defined and controlle...
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
int IUUpdateSwitch(ISwitchVectorProperty *svp, ISState *states, char *names[], int n)
Update all switches in a switch vector property.
TCP(INDI::DefaultDevice *dev)
virtual void Activated() override
Activated Function called by the framework when the plugin is activated (i.e. selected by the user)....
#define LOGF_INFO(fmt,...)
static constexpr uint8_t SOCKET_TIMEOUT
const char * DEVICE_LAN_SEARCH
Toggle device LAN search. If the initial handshake with the specified hostname and port number fails,...
#define LOG_ERROR(txt)
Shorter logging macros. In order to use these macros, the function (or method) "getDeviceName()" must...
ISwitchVectorProperty TcpUdpSP
Combines all INDI Connection Plugins. Each INDI connection plugin is responsible of managing communic...
void IUSaveText(IText *tp, const char *newtext)
Function to reliably save new text in a IText.
virtual uint32_t port() const
void setDefaultHost(const char *addressHost)
virtual bool saveConfig(bool silent=false, const char *property=nullptr)
Save the current properties in a configuration file.
void setDefaultPort(uint32_t addressPort)
virtual std::string name() override
virtual void Deactivated() override
Deactivated Function called by the framework when the plugin is deactivated. It is usually used to de...
void setLANSearchEnabled(bool enabled)
std::vector< std::string > getInterfaceAddressesV4()
const char * DEVICE_AUTO_SEARCH
Toggle device auto search. If enabled and on connection failure with the default port,...
int IUGetConfigOnSwitchIndex(const char *dev, const char *property, int *index)
IUGetConfigOnSwitchIndex Opens configuration file and reads single switch property to find ON switch ...
int IUFindOnSwitchIndex(const ISwitchVectorProperty *sp)
Returns the index of first ON switch it finds in the vector switch property.
int m_ConfigConnectionType
void IUSaveConfigSwitch(FILE *fp, const ISwitchVectorProperty *svp)
Add a switch vector property value to the configuration file.
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
virtual bool deleteProperty(const char *propertyName)
Delete a property and unregister it. It will also be deleted from all clients.
void void void void void IDSetSwitch(const ISwitchVectorProperty *s, const char *msg,...) ATTRIBUTE_FORMAT_PRINTF(2
Tell client to update an existing switch vector property.
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.
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override