-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathetherport.h
104 lines (92 loc) · 2.59 KB
/
etherport.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/* OpenSprinkler Unified (AVR/RPI/BBB/LINUX) Firmware
* Copyright (C) 2015 by Ray Wang ([email protected])
*
* Linux Ethernet functions header file
* This file is based on Richard Zimmerman's sprinklers_pi program
* Copyright (c) 2013 Richard Zimmerman
*
* This file is part of the OpenSprinkler library
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see
* <http://www.gnu.org/licenses/>.
*/
#ifndef _ETHERPORT_H_
#define _ETHERPORT_H_
#if defined(ARDUINO)
#else // headers for RPI/BBB/Linux
#include <stdio.h>
#include <inttypes.h>
#include <ctype.h>
#include <netdb.h>
#include <openssl/ssl.h>
#include <string>
#ifdef __APPLE__
#undef MSG_NOSIGNAL
#define MSG_NOSIGNAL SO_NOSIGPIPE
#endif
#define TMPBUF 1024*8
class EthernetServer;
class EthernetClient {
public:
EthernetClient();
EthernetClient(int sock);
~EthernetClient();
virtual int connect(const char *server, uint16_t port);
virtual bool connected();
virtual void stop();
virtual int read(uint8_t *buf, size_t size);
virtual int timedRead();
virtual size_t readBytesUntil(char terminator, char *buffer, size_t length);
virtual size_t write(const uint8_t *buf, size_t size);
virtual operator bool();
int GetSocket() {
return m_sock;
}
virtual void flush();
virtual bool available();
virtual void setTimeout(int msec);
protected:
uint8_t *tmpbuf = NULL;
int tmpbufsize = 0;
int tmpbufidx = 0;
int m_sock = 0;
bool m_connected;
friend class EthernetServer;
};
class EthernetClientSsl : public EthernetClient {
public:
EthernetClientSsl();
EthernetClientSsl(int sock);
~EthernetClientSsl();
virtual int connect(const char *server, uint16_t port);
virtual bool connected();
virtual void stop();
virtual int read(uint8_t *buf, size_t size);
virtual size_t write(const uint8_t *buf, size_t size);
virtual operator bool();
protected:
SSL* ssl;
};
class EthernetServer {
public:
EthernetServer(uint16_t port);
~EthernetServer();
virtual bool begin();
virtual EthernetClient available();
private:
uint16_t m_port;
int m_sock;
};
#endif
#endif /* _ETHERPORT_H_ */