Instrument Neutral Distributed Interface INDI  2.0.2
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:
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
110  // Connection Type
113  // Auto search
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 }
The Interface class is the base class for all INDI connection plugins.
virtual Type type()
type Return connection type
The TCP class manages connection with devices over the network via TCP/IP. Upon successfull connectio...
Definition: connectiontcp.h:38
virtual ~TCP()=default
void setDefaultHost(const char *addressHost)
ISwitchVectorProperty TcpUdpSP
ISwitch LANSearchS[2]
virtual std::string label() override
Definition: connectiontcp.h:62
virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n) override
static constexpr uint8_t SOCKET_TIMEOUT
void setLANSearchEnabled(bool enabled)
virtual std::string name() override
Definition: connectiontcp.h:57
virtual bool Disconnect() override
Disconnect Disconnect from device.
void setConnectionType(int type)
TODO should be renamed to setDefaultConnectionType.
virtual void Activated() override
Activated Function called by the framework when the plugin is activated (i.e. selected by the user)....
void setDefaultPort(uint32_t addressPort)
int getPortFD() const
Definition: connectiontcp.h:84
virtual const char * host() const
Definition: connectiontcp.h:67
virtual bool Connect() override
Connect Connect to device via the implemented communication medium. Do not perform any handshakes.
ISwitch TcpUdpS[2]
std::string m_ConfigHost
std::string m_ConfigPort
virtual uint32_t port() const
Definition: connectiontcp.h:71
virtual bool saveConfigItems(FILE *fp) override
ConnectionType connectionType() const
Definition: connectiontcp.h:75
ISwitchVectorProperty LANSearchSP
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,...
virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n) override
ITextVectorProperty AddressTP
Properties.
virtual void Deactivated() override
Deactivated Function called by the framework when the plugin is deactivated. It is usually used to de...
TCP(INDI::DefaultDevice *dev)
Class to provide extended functionality for devices in addition to the functionality provided by INDI...
ISState
Switch state.
Definition: indiapi.h:150
int IUFindOnSwitchIndex(const ISwitchVectorProperty *svp)
Returns the index of first ON switch it finds in the vector switch property.
Definition: indidevapi.c:128
Combines all INDI Connection Plugins. Each INDI connection plugin is responsible of managing communic...
Definition: arduino_st4.h:34
One switch descriptor.
One text descriptor.
Switch vector property descriptor.
Definition: indiapi.h:367
Text vector property descriptor.
Definition: indiapi.h:246