Instrument Neutral Distributed Interface INDI  1.9.5
connectiontcp.h
Go to the documentation of this file.
1 /*******************************************************************************
2  Copyright(c) 2017 Jasem Mutlaq. All rights reserved.
3 
4  Connection Plugin Interface
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 *******************************************************************************/
20 
21 #pragma once
22 
23 #include "connectioninterface.h"
24 
25 #include <stdint.h>
26 #include <cstdlib>
27 #include <string>
28 
29 namespace Connection
30 {
37 class TCP : public Interface
38 {
39  public:
40  enum ConnectionType
41  {
42  TYPE_TCP = 0,
43  TYPE_UDP
44  };
45 
47  virtual ~TCP() = default;
48 
49  virtual bool Connect() override;
50 
51  virtual bool Disconnect() override;
52 
53  virtual void Activated() override;
54 
55  virtual void Deactivated() override;
56 
57  virtual std::string name() override
58  {
59  return "CONNECTION_TCP";
60  }
61 
62  virtual std::string label() override
63  {
64  return "Network";
65  }
66 
67  virtual const char *host() const
68  {
69  return AddressT[0].text;
70  }
71  virtual uint32_t port() const
72  {
73  return atoi(AddressT[1].text);
74  }
76  {
77  return static_cast<ConnectionType>(IUFindOnSwitchIndex(&TcpUdpSP));
78  }
79 
80  virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override;
81  virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override;
82  virtual bool saveConfigItems(FILE *fp) override;
83 
84  int getPortFD() const
85  {
86  return PortFD;
87  }
88  void setDefaultHost(const char *addressHost);
89  void setDefaultPort(uint32_t addressPort);
90  void setConnectionType(int type);
91  void setLANSearchEnabled(bool enabled);
92 
93  protected:
102  bool establishConnection(const std::string &hostname, const std::string &port, int timeout=-1);
103 
107  // IP Address/Port
109  IText AddressT[2] {};
110  // Connection Type
113  // Auto search
114  ISwitch LANSearchS[2];
116 
117  // Variables
118  std::string m_ConfigHost;
119  std::string m_ConfigPort;
121  int m_SockFD {-1};
122  int PortFD = -1;
123  static constexpr uint8_t SOCKET_TIMEOUT {5};
124 };
125 }
connectioninterface.h
Connection::TCP::getPortFD
int getPortFD() const
Definition: connectiontcp.h:120
Connection::TCP::AddressTP
ITextVectorProperty AddressTP
Properties.
Definition: connectiontcp.h:144
Connection::TCP::establishConnection
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,...
Definition: connectiontcp.cpp:161
Connection::TCP::Disconnect
virtual bool Disconnect() override
Disconnect Disconnect from device.
Definition: connectiontcp.cpp:349
ISwitch
One switch descriptor.
Connection::TCP::TcpUdpS
ISwitch TcpUdpS[2]
Definition: connectiontcp.h:147
Connection::TCP::TYPE_TCP
@ TYPE_TCP
Definition: connectiontcp.h:96
Connection::TCP::m_ConfigPort
std::string m_ConfigPort
Definition: connectiontcp.h:155
Connection::TCP::LANSearchSP
ISwitchVectorProperty LANSearchSP
Definition: connectiontcp.h:151
Connection::TCP::PortFD
int PortFD
Definition: connectiontcp.h:158
Connection::TCP::saveConfigItems
virtual bool saveConfigItems(FILE *fp) override
Definition: connectiontcp.cpp:383
Connection::TCP::label
virtual std::string label() override
Definition: connectiontcp.h:98
Connection::TCP::Connect
virtual bool Connect() override
Connect Connect to device via the implemented communication medium. Do not perform any handshakes.
Definition: connectiontcp.cpp:229
_ITextVectorProperty
Text vector property descriptor.
Definition: indiapi.h:244
Connection::TCP::~TCP
virtual ~TCP()=default
IText
One text descriptor.
Connection::TCP::LANSearchS
ISwitch LANSearchS[2]
Definition: connectiontcp.h:150
Connection::TCP::setConnectionType
void setConnectionType(int type)
TODO should be renamed to setDefaultConnectionType.
Definition: connectiontcp.cpp:421
Connection::Interface::type
virtual Type type()
type Return connection type
Definition: connectioninterface.h:105
Connection::TCP::ISNewText
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
Definition: connectiontcp.cpp:100
Connection::TCP::TCP
TCP(INDI::DefaultDevice *dev)
Definition: connectiontcp.cpp:60
Connection::TCP::ConnectionType
ConnectionType
Definition: connectiontcp.h:76
Connection::TCP::Activated
virtual void Activated() override
Activated Function called by the framework when the plugin is activated (i.e. selected by the user)....
Definition: connectiontcp.cpp:363
Connection::TCP::SOCKET_TIMEOUT
static constexpr uint8_t SOCKET_TIMEOUT
Definition: connectiontcp.h:159
Connection::TCP::connectionType
ConnectionType connectionType() const
Definition: connectiontcp.h:111
Connection::TCP::TcpUdpSP
ISwitchVectorProperty TcpUdpSP
Definition: connectiontcp.h:148
Connection
Combines all INDI Connection Plugins. Each INDI connection plugin is responsible of managing communic...
Definition: arduino_st4.h:33
Connection::TCP::port
virtual uint32_t port() const
Definition: connectiontcp.h:107
Connection::TCP::m_SockFD
int m_SockFD
Definition: connectiontcp.h:157
Connection::TCP::TYPE_UDP
@ TYPE_UDP
Definition: connectiontcp.h:97
Connection::TCP::setDefaultHost
void setDefaultHost(const char *addressHost)
Definition: connectiontcp.cpp:395
Connection::TCP::setDefaultPort
void setDefaultPort(uint32_t addressPort)
Definition: connectiontcp.cpp:406
Connection::TCP::name
virtual std::string name() override
Definition: connectiontcp.h:93
Connection::TCP::m_ConfigHost
std::string m_ConfigHost
Definition: connectiontcp.h:154
Connection::TCP::host
virtual const char * host() const
Definition: connectiontcp.h:103
Connection::TCP::Deactivated
virtual void Deactivated() override
Deactivated Function called by the framework when the plugin is deactivated. It is usually used to de...
Definition: connectiontcp.cpp:373
Connection::TCP::setLANSearchEnabled
void setLANSearchEnabled(bool enabled)
Definition: connectiontcp.cpp:435
ISState
ISState
Switch state.
Definition: indiapi.h:148
IUFindOnSwitchIndex
int IUFindOnSwitchIndex(const ISwitchVectorProperty *sp)
Returns the index of first ON switch it finds in the vector switch property.
Definition: indicom.c:1424
Connection::TCP::m_ConfigConnectionType
int m_ConfigConnectionType
Definition: connectiontcp.h:156
INDI::DefaultDevice
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
Definition: defaultdevice.h:118
Connection::TCP::AddressT
IText AddressT[2]
Definition: connectiontcp.h:145
Connection::TCP::ISNewSwitch
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
Definition: connectiontcp.cpp:120
_ISwitchVectorProperty
Switch vector property descriptor.
Definition: indiapi.h:365