1 /*
2 	This code implements basic I/O hardware via the Raspberry Pi's GPIO port, using sysfs.
3 */
4 #pragma once
5 
6 #include "DomoticzHardware.h"
7 #include "../main/RFXtrx.h"
8 
9 struct gpio_info
10 {
11 	int		pin_number;		// GPIO Pin number
12 	int		read_value_fd;	// Fast read fd
13 	int		edge_fd;		// Edge detect fd
14 	int8_t	request_update;	// Request update
15 	int8_t	db_state;		// Database Value
16 	int8_t	value;			// GPIO pin Value
17 	int8_t	direction;		// GPIO IN or OUT
18 	int8_t	active_low;		// GPIO ActiveLow
19 	int8_t	edge;			// GPIO int Edge
20 	int8_t	id1;			// Device id1
21 	int8_t	id2;			// Device id2
22 	int8_t	id3;			// Device id3
23 	int8_t	id4;			// Device id4
24 	int8_t	id_valid;		// Device valid
25 };
26 
27 class CSysfsGpio : public CDomoticzHardwareBase
28 {
29 
30 public:
31 
32 	CSysfsGpio(const int ID, const int ManualDevices, const int Debounce);
33 	~CSysfsGpio();
34 
35 	bool WriteToHardware(const char *pdata, const unsigned char length) override;
36 	static std::vector<int> GetGpioIds();
37 	static std::vector<std::string> GetGpioNames();
38 	static void RequestDbUpdate(int pin);
39 private:
40 	bool StartHardware() override;
41 	bool StopHardware() override;
42 	void FindGpioExports();
43 	void Do_Work();
44 	void EdgeDetectThread();
45 	void Init();
46 	void PollGpioInputs(bool PollOnce);
47 	void CreateDomoticzDevices();
48 	void UpdateDomoticzInputs(bool forceUpdate);
49 	void UpdateDomoticzDatabase();
50 	void UpdateGpioOutputs();
51 	void UpdateDeviceID(int pin);
52 	std::vector<std::string> GetGpioDeviceId();
53 	int GpioRead(int pin, const char* param);
54 	int GpioReadFd(int fd);
55 	int GpioWrite(int pin, int value);
56 	int GpioOpenRw(int gpio_pin);
57 	int GetReadResult(int bytecount, char* value_str);
58 	int GpioGetState(int index);
59 	void GpioSaveState(int index, int value);
60 
61 	static std::vector<gpio_info> m_saved_state;
62 	static int m_sysfs_hwdid;
63 	static int m_sysfs_req_update;
64 	bool m_polling_enabled;
65 	bool m_interrupts_enabled;
66 	int m_debounce_msec;
67 	int m_auto_configure_devices;
68 	int m_maxfd;
69 	fd_set m_rfds;
70 	tRBUF m_Packet;
71 
72 	std::shared_ptr<std::thread> m_thread;
73 	std::shared_ptr<std::thread> m_edge_thread;
74 	std::mutex m_state_mutex;
75 };
76