libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
IPhysicalInterface.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef IPHYSICALINTERFACE_H_
32 #define IPHYSICALINTERFACE_H_
33 
34 #include "../IEvents.h"
36 #include "../Managers/FileDescriptorManager.h"
37 #include "../IQueue.h"
38 
39 #include <list>
40 #include <thread>
41 #include <mutex>
42 #include <map>
43 #include <condition_variable>
44 #include <atomic>
45 
46 #include <dirent.h>
47 
48 namespace BaseLib {
49 
50 class SharedObjects;
51 
52 namespace Systems {
53 
54 class Packet;
55 
56 class IPhysicalInterface : public IEventsEx, public IQueue {
57  public:
58  struct GPIODirection {
59  enum Enum {
60  IN,
62  };
63  };
64 
65  struct GPIOEdge {
66  enum Enum {
69  BOTH
70  };
71  };
72 
73  //Event handling
75  public:
76  virtual bool onPacketReceived(std::string &senderID, std::shared_ptr<Packet> packet) = 0;
77  };
78  //End event handling
79 
81  IPhysicalInterface(BaseLib::SharedObjects *baseLib, int32_t familyId, std::shared_ptr<PhysicalInterfaceSettings> settings);
82 
83  virtual ~IPhysicalInterface();
84 
85  virtual void startListening();
86  virtual void stopListening();
87  virtual void enableUpdateMode();
88  virtual void disableUpdateMode();
89  virtual void sendPacket(std::shared_ptr<Packet> packet) = 0;
90  virtual bool lifetick();
91  virtual bool isOpen() { return _fileDescriptor && _fileDescriptor->descriptor != -1; }
92  virtual uint32_t responseDelay() { return _settings->responseDelay; }
93  virtual int64_t lastPacketSent() { return _lastPacketSent; }
94  virtual int64_t lastPacketReceived() { return _lastPacketReceived; }
95  virtual void setup(int32_t userID, int32_t groupID, bool setPermissions) {}
96  virtual std::string getType() { return _settings->type; }
97  virtual std::string getID() { return _settings->id; }
98  virtual std::string getSerialNumber() { return ""; }
99  virtual std::string getFirmwareVersion() { return ""; }
100  virtual bool isDefault() { return _settings->isDefault; }
101  virtual bool isNetworkDevice() { return _settings->device.empty() && !_settings->host.empty() && !_settings->port.empty(); }
102  virtual int32_t getAddress() { return _myAddress; }
103  virtual std::string getIpAddress() { return _ipAddress; }
104  virtual std::string getHostname() { return _hostname; }
105 
106  void setRawPacketEvent(std::function<void(int32_t familyId, const std::string &interfaceId, const BaseLib::PVariable &packet)> value) { _rawPacketEvent.swap(value); }
107  protected:
109  public:
110  QueueEntry(const std::shared_ptr<Packet> &packet) { this->packet = packet; };
111 
112  std::shared_ptr<Packet> packet;
113  };
114 
116  int32_t _familyId = -1;
117  std::shared_ptr<PhysicalInterfaceSettings> _settings;
118  std::thread _listenThread; //Used by derived classes
119  std::thread _callbackThread; //Used by derived classes
120  std::atomic_bool _stopCallbackThread {false}; //Used by derived classes
121  std::string _lockfile;
122  std::mutex _sendMutex;
123  std::atomic_bool _stopped; //Used by derived classes
124  std::shared_ptr<FileDescriptor> _fileDescriptor;
125  std::map<uint32_t, std::shared_ptr<FileDescriptor>> _gpioDescriptors;
126  std::atomic<int64_t> _lastPacketSent{-1};
127  std::atomic<int64_t> _lastPacketReceived{-1};
128  int64_t _maxPacketProcessingTime = 1000;
129  std::atomic_bool _updateMode{false};
130  std::atomic<int64_t> _lifetickTime{0};
131  std::atomic_bool _lifetickState{true};
132 
133  int32_t _myAddress = 0;
134  std::string _hostname;
135  std::string _ipAddress;
136 
137  std::function<void(int32_t familyId, const std::string &interfaceId, const BaseLib::PVariable &packet)> _rawPacketEvent;
138 
139  //Event handling
140  virtual void raisePacketReceived(std::shared_ptr<Packet> packet);
141  //End event handling
142  void processQueueEntry(int32_t index, std::shared_ptr<BaseLib::IQueueEntry> &entry) override;
143  virtual void setDevicePermission(int32_t userID, int32_t groupID);
144  virtual void openGPIO(uint32_t index, bool readOnly);
145  virtual void getGPIOPath(uint32_t index);
146  virtual void closeGPIO(uint32_t index);
147  virtual bool getGPIO(uint32_t index);
148  virtual void setGPIO(uint32_t index, bool value);
149  virtual void setGPIOPermission(uint32_t index, int32_t userID, int32_t groupID, bool readOnly);
150  virtual void exportGPIO(uint32_t index);
151  virtual bool setGPIODirection(uint32_t index, GPIODirection::Enum direction);
152  virtual bool setGPIOEdge(uint32_t index, GPIOEdge::Enum edge);
153  virtual bool gpioDefined(uint32_t);
154  virtual bool gpioOpen(uint32_t);
155 
156  virtual void saveSettingToDatabase(std::string setting, std::string &value);
157  virtual void saveSettingToDatabase(std::string setting, int32_t value);
158  virtual void saveSettingToDatabase(std::string setting, std::vector<char> &value);
159 };
160 
161 }
162 }
163 #endif
virtual void setGPIOPermission(uint32_t index, int32_t userID, int32_t groupID, bool readOnly)
Definition: IPhysicalInterface.cpp:267
virtual bool setGPIOEdge(uint32_t index, GPIOEdge::Enum edge)
Definition: IPhysicalInterface.cpp:386
void setRawPacketEvent(std::function< void(int32_t familyId, const std::string &interfaceId, const BaseLib::PVariable &packet)> value)
Definition: IPhysicalInterface.h:106
Enum
Definition: IPhysicalInterface.h:66
virtual int64_t lastPacketSent()
Definition: IPhysicalInterface.h:93
std::shared_ptr< FileDescriptor > _fileDescriptor
Definition: IPhysicalInterface.h:124
std::shared_ptr< PhysicalInterfaceSettings > _settings
Definition: IPhysicalInterface.h:117
virtual int32_t getAddress()
Definition: IPhysicalInterface.h:102
virtual void getGPIOPath(uint32_t index)
Definition: IPhysicalInterface.cpp:177
This is the base library main class.
Definition: BaseLib.h:95
Definition: IQueue.h:41
virtual bool isNetworkDevice()
Definition: IPhysicalInterface.h:101
QueueEntry(const std::shared_ptr< Packet > &packet)
Definition: IPhysicalInterface.h:110
virtual void exportGPIO(uint32_t index)
Definition: IPhysicalInterface.cpp:325
Definition: IPhysicalInterface.h:65
virtual void setup(int32_t userID, int32_t groupID, bool setPermissions)
Definition: IPhysicalInterface.h:95
virtual uint32_t responseDelay()
Definition: IPhysicalInterface.h:92
virtual bool setGPIODirection(uint32_t index, GPIODirection::Enum direction)
Definition: IPhysicalInterface.cpp:356
Definition: IPhysicalInterface.h:56
int32_t _familyId
Definition: IPhysicalInterface.h:116
virtual std::string getType()
Definition: IPhysicalInterface.h:96
Definition: BaseLib.cpp:34
virtual void setGPIO(uint32_t index, bool value)
Definition: IPhysicalInterface.cpp:250
virtual bool gpioDefined(uint32_t)
Definition: IPhysicalInterface.cpp:305
IPhysicalInterface(BaseLib::SharedObjects *baseLib, int32_t familyId)
Definition: IPhysicalInterface.cpp:40
virtual std::string getFirmwareVersion()
Definition: IPhysicalInterface.h:99
std::atomic_bool _updateMode
Definition: IPhysicalInterface.h:129
virtual ~IPhysicalInterface()
Definition: IPhysicalInterface.cpp:55
virtual void stopListening()
Definition: IPhysicalInterface.cpp:85
std::map< uint32_t, std::shared_ptr< FileDescriptor > > _gpioDescriptors
Definition: IPhysicalInterface.h:125
void processQueueEntry(int32_t index, std::shared_ptr< BaseLib::IQueueEntry > &entry) override
Definition: IPhysicalInterface.cpp:102
Definition: IPhysicalInterface.h:108
PVariable value
Definition: UiElements.h:217
virtual bool isDefault()
Definition: IPhysicalInterface.h:100
std::shared_ptr< Variable > PVariable
Definition: PhysicalInterfaceSettings.h:41
std::thread _listenThread
Definition: IPhysicalInterface.h:118
int64_t _maxPacketProcessingTime
Definition: IPhysicalInterface.h:128
virtual void startListening()
Definition: IPhysicalInterface.cpp:76
std::atomic< int64_t > _lastPacketSent
Definition: IPhysicalInterface.h:126
virtual std::string getSerialNumber()
Definition: IPhysicalInterface.h:98
std::atomic_bool _lifetickState
Definition: IPhysicalInterface.h:131
virtual void openGPIO(uint32_t index, bool readOnly)
Definition: IPhysicalInterface.cpp:166
virtual void closeGPIO(uint32_t index)
Definition: IPhysicalInterface.cpp:220
std::atomic< int64_t > _lifetickTime
Definition: IPhysicalInterface.h:130
This class implements a queue after the producer-consumer paradigm.
Definition: IQueue.h:52
std::string _lockfile
Definition: IPhysicalInterface.h:121
virtual int64_t lastPacketReceived()
Definition: IPhysicalInterface.h:94
virtual void sendPacket(std::shared_ptr< Packet > packet)=0
int32_t familyId
Definition: Licensing.h:380
Definition: IEvents.h:87
std::atomic< int64_t > _lastPacketReceived
Definition: IPhysicalInterface.h:127
virtual void setDevicePermission(int32_t userID, int32_t groupID)
Definition: IPhysicalInterface.cpp:145
std::function< void(int32_t familyId, const std::string &interfaceId, const BaseLib::PVariable &packet)> _rawPacketEvent
Definition: IPhysicalInterface.h:137
virtual void enableUpdateMode()
Definition: IPhysicalInterface.cpp:94
virtual void saveSettingToDatabase(std::string setting, std::string &value)
Definition: IPhysicalInterface.cpp:416
virtual std::string getID()
Definition: IPhysicalInterface.h:97
std::thread _callbackThread
Definition: IPhysicalInterface.h:119
Enum
Definition: IPhysicalInterface.h:59
std::string _ipAddress
Definition: IPhysicalInterface.h:135
std::mutex _sendMutex
Definition: IPhysicalInterface.h:122
virtual bool lifetick()
Definition: IPhysicalInterface.cpp:62
virtual bool isOpen()
Definition: IPhysicalInterface.h:91
virtual bool getGPIO(uint32_t index)
Definition: IPhysicalInterface.cpp:231
std::string _hostname
Definition: IPhysicalInterface.h:134
int32_t _myAddress
Definition: IPhysicalInterface.h:133
BaseLib::SharedObjects * _bl
Definition: IPhysicalInterface.h:115
std::atomic_bool _stopped
Definition: IPhysicalInterface.h:123
std::atomic_bool _stopCallbackThread
Definition: IPhysicalInterface.h:120
virtual std::string getIpAddress()
Definition: IPhysicalInterface.h:103
virtual bool gpioOpen(uint32_t)
Definition: IPhysicalInterface.cpp:315
Definition: IEvents.h:66
virtual std::string getHostname()
Definition: IPhysicalInterface.h:104
Definition: IPhysicalInterface.h:58
virtual void disableUpdateMode()
Definition: IPhysicalInterface.cpp:98
virtual void raisePacketReceived(std::shared_ptr< Packet > packet)
Definition: IPhysicalInterface.cpp:135