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 "indidevapi.h"
24 
25 #include <functional>
26 #include <string>
27 
28 namespace INDI
29 {
30 class DefaultDevice;
31 }
32 
33 /**
34  * @namespace Connection
35  * @brief Combines all INDI Connection Plugins. Each INDI connection plugin is responsible of managing communications
36  * with a specific physical or logical medium (e.g. serial or ethernet).
37  */
38 namespace Connection
39 {
40 /**
41  * @brief The Interface class is the base class for all INDI connection plugins.
42  *
43  * Each plugin implements the connection details specific to a particular medium (e.g. serial). After the connection to the medium is successful, a <i>handshake</i> is
44  * initialted to make sure the device is online and responding to commands. The child class employing the plugin must register
45  * the handshake to perform the actual low-level communication with the device.
46  *
47  * @see Connection::Serial
48  * @see Connection::TCP
49  * @see INDI::Telescope utilizes both serial and tcp plugins to communicate with mounts.
50  */
51 class Interface
52 {
53     public:
54         /**
55          * \struct Type
56          * \brief Holds the connection type
57          */
58         typedef enum
59         {
60             CONNECTION_NONE   = 1 << 0, /** Do not use any connection plugin */
61             CONNECTION_SERIAL = 1 << 1, /** For regular serial and bluetooth connections */
62             CONNECTION_TCP    = 1 << 2,  /** For Wired and WiFI connections */
63             CONNECTION_USB    = 1 << 3,  /** For USB-based connections */
64             CONNECTION_CUSTOM = 1 << 15, /** Custom connection */
65         } Type;
66 
67         /**
68          * @brief Connect Connect to device via the implemented communication medium. Do not perform any handshakes.
69          * @return True if successful, false otherwise.
70          */
71         virtual bool Connect() = 0;
72 
73         /**
74          * @brief Disconnect Disconnect from device.
75          * @return True if successful, false otherwise.
76          */
77         virtual bool Disconnect() = 0;
78 
79         /**
80          * @brief Activated Function called by the framework when the plugin is activated (i.e. selected by the user). It is usually used to define properties
81          * pertaining to the specific plugin functionalities.
82          */
83         virtual void Activated() = 0;
84 
85         /**
86          * @brief Deactivated Function called by the framework when the plugin is deactivated. It is usually used to delete properties by were defined
87          * previously since the plugin is no longer active.
88          */
89         virtual void Deactivated() = 0;
90 
91         /**
92          * @return Plugin name
93          */
94         virtual std::string name() = 0;
95 
96         /**
97          * @return Plugin friendly label presented to the client/user.
98          */
99         virtual std::string label() = 0;
100 
101         /**
102          * @brief type Return connection type
103          * @return connection type
104          */
type()105         virtual Type type()
106         {
107             return m_Type;
108         }
109 
110         virtual bool ISNewSwitch(const char *dev, const char *name, ISState *states, char *names[], int n);
111         virtual bool ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n);
112         virtual bool ISNewText(const char *dev, const char *name, char *texts[], char *names[], int n);
113         virtual bool ISNewBLOB(const char *dev, const char *name, int sizes[], int blobsizes[], char *blobs[],
114                                char *formats[], char *names[], int n);
115         virtual bool saveConfigItems(FILE *fp);
116 
117         /**
118          * @brief registerHandshake Register a handshake function to be called once the intial connection to the device is established.
119          * @param callback Handshake function callback
120          * @see INDI::Telescope
121          */
122         void registerHandshake(std::function<bool()> callback);
123 
124     protected:
125         Interface(INDI::DefaultDevice *dev, Type type = CONNECTION_NONE);
126         virtual ~Interface();
127 
128         const char *getDeviceName();
129         std::function<bool()> Handshake;
130         INDI::DefaultDevice *m_Device {  nullptr };
131         Type m_Type { CONNECTION_NONE };
132 };
133 }
134