TS-E+ базовая библиотека.
libTSE+
 Указатель Классы Функции Переменные Определения типов
net.h
1 /*
2  * net.h
3  *
4  * Created on: Apr 12, 2015
5  * Author: nick
6  */
7 
8 #ifndef NET_H_
9 #define NET_H_
10 
11 #include <stdint.h>
12 #include <stddef.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <avr/pgmspace.h>
16 
17 #define MTU 1500 //<! размер Maximum Transfer Unit
18 
22 uint32_t htonl(const uint32_t);
23 
27 uint16_t htons(const uint16_t);
28 
32 typedef union EthernetAddress_ {
33  uint8_t a8[6];
34  uint16_t a16[3];
35  union EthernetAddress_ &operator = (const union EthernetAddress_ other) {
36  for(size_t i=0;i<sizeof(a8);i++)
37  this->a8[i] = other.a8[i];
38  return *this;
39  }
40  union EthernetAddress_ &operator = (const uint8_t a[]) {
41  for(int i=0;i<6;i++)
42  this->a8[i] = a[i];
43  return *this;
44  }
45  /*
46  * Получить MAC из строки формата xx:xx:xx:xx:xx:xx
47  */
48  union EthernetAddress_ &operator << (const char *str) {
49  sscanf_P(str,PSTR("%02X:%02X:%02X:%02X:%02X:%02X"),&a8[0],&a8[1],&a8[2],&a8[3],&a8[4],&a8[5]);
50  return *this;
51  }
52  const bool operator == (const union EthernetAddress_ other) {
53  return this->a16[0] == other.a16[0] && this->a16[1] == other.a16[1] && this->a16[2] == other.a16[2];
54  }
55  const bool operator != (const union EthernetAddress_ other) {
56  return this->a16[0] != other.a16[0] || this->a16[1] != other.a16[1] || this->a16[2] != other.a16[2];
57  }
59 
60 /*
61  * IPv4 адрес
62  */
63 typedef union IP4Address_ {
64  uint8_t a8[4];
65  uint32_t a32;
66  union IP4Address_ &operator = (const union IP4Address_ &other) {
67  this->a32 = other.a32;
68  return *this;
69  }
70  union IP4Address_ &operator = (const uint8_t a[]) {
71  for(int i=0;i<4;i++)
72  this->a8[i] = a[i];
73  return *this;
74  }
78  union IP4Address_ &operator << (const char *astr) {
79  sscanf_P(astr,PSTR("%hhu.%hhu.%hhu.%hhu"),&a8[0],&a8[1],&a8[2],&a8[3]);
80  return *this;
81  }
82  const bool operator == (const union IP4Address_ &v2) {
83  return this->a32 == v2.a32;
84  }
85  const bool operator != (const union IP4Address_ &v2) {
86  return this->a32 != v2.a32;
87  }
88 
89 
90 } IP4Address;
91 
92 
96 typedef struct _Inet4Address {
97  IP4Address addr;
98  uint16_t port;
99  _Inet4Address(): port(0) {}
100  _Inet4Address(IP4Address addr, uint16_t port) {
101  this->addr = addr;
102  this->port = port;
103  }
104  struct _Inet4Address &operator = (const struct _Inet4Address addr) {
105  this->addr = addr.addr;
106  port = addr.port;
107  return *this;
108  }
112  uint16_t getPort() { return htons(port); }
113 } Inet4Address;
114 
118 #define PROTO_ICMP 1
119 #define PROTO_TCP 6
120 #define PROTO_UDP 17
121 
125 typedef enum {
126  closed,
127  syn,
128  established,
129  error,
130  closeWait1,
131  closeWait2
132 } SocketState;
133 
137 typedef struct {
138  size_t connections;
139  uint32_t packetsIn;
140  uint32_t bytesIn;
141  uint32_t packetsOut;
142  uint32_t bytesOut;
143  uint32_t packetsDropped;
145 
146 
147 extern NetworkStatistic network_statistic;
148 
149 class SocketListener;
150 class SocketServer;
151 
155 class Socket {
156 protected:
157  uint8_t *_RX; //<! входящий буфер
158  size_t _RXSize;
159  void *_attachment;
160  virtual size_t _send(const void *, size_t,
161  void *(copier)(void *, const void *, size_t)) = 0;
162 public:
164 
167  size_t send(const void *buffer,size_t size) {
168  return _send(buffer,size,&memcpy);
169  }
173  size_t send_P(const void *buffer,size_t size) {
174  return _send(buffer,size,&memcpy_P);
175  }
179  virtual bool flush() = 0;
180  Socket &operator << (const char *str) {
181  if(str && str[0])
182  send(str,strlen(str));
183  return *this;
184  }
185  virtual Socket &operator << (const char) = 0;
189  virtual size_t recv() = 0;
193  virtual void close() = 0;
194  virtual ~Socket() { }
198  void *attachment() { return _attachment; }
202  void attach(void *data) { _attachment = data; }
206  size_t RXSize() { return _RXSize; }
210  uint8_t *RX() { return _RX; }
215  uint8_t *RX(size_t offset) {
216  if(offset > _RXSize) return NULL;
217  return &_RX[offset];
218  }
219 };
220 
221 extern const EthernetAddress BROADCAST_ETHERNET_ADDRESS;
222 extern const IP4Address BROADCAST_IP4_ADDRESS;
223 
227 public:
228  NetworkAdapter() {}
229  virtual size_t write(const void *,size_t) = 0;
230  virtual size_t read(void *,size_t) = 0;
234  virtual bool isLinkUp() = 0;
239  virtual bool establishConnection() = 0;
240  virtual ~NetworkAdapter() {}
241 };
242 
243 
248 private:
249  uint8_t _protocol;
250  bool _registred;
251 protected:
252  uint16_t _port;
253 public:
259  SocketListener(const uint8_t protocol ,const uint16_t port) : _protocol(protocol), _registred(false),_port(port) { }
264  void setPort(uint16_t port) {
265  if(_registred) return;
266  _port = port;
267  }
271  const uint8_t protocol() { return _protocol; }
275  const uint16_t port() { return _port; }
279  virtual uint32_t timeout() { return 1000; }
284  virtual void handle(Socket *socket) = 0;
289  virtual void release(Socket *socket) = 0;
293  virtual void onRegister() {_registred = true; }
297  virtual void onIdle() {}
298  virtual ~SocketListener() { }
299 };
300 
301 extern class NetworkAdapter *NetworkAdapter;
302 extern class SocketServer *SocketServer;
303 
307 class SocketServer {
308 protected:
309  IP4Address _address, _netmask, _gw, _ns;
310 public:
311  SocketServer() {}
318  virtual void configure(const bool dhcp, const IP4Address address,const IP4Address mask) = 0;
323  virtual void registerSocketListener(SocketListener *server) = 0;
327  virtual void removeSocketListener(SocketListener *server) = 0;
331  virtual void serve() = 0;
332  virtual ~SocketServer() { }
333 };
334 
335 
336 
337 
338 
339 #endif /* NET_H_ */
Definition: net.h:137
size_t send(const void *buffer, size_t size)
Definition: net.h:167
const uint16_t port()
Definition: net.h:275
void attach(void *data)
Definition: net.h:202
uint32_t packetsOut
пакетов отправлено
Definition: net.h:141
Definition: net.h:63
const uint8_t protocol()
Definition: net.h:271
Definition: net.h:226
virtual bool establishConnection()=0
virtual void configure(const bool dhcp, const IP4Address address, const IP4Address mask)=0
virtual bool flush()=0
union IP4Address_ & operator<<(const char *astr)
Definition: net.h:78
virtual void removeSocketListener(SocketListener *server)=0
uint32_t packetsDropped
пакетов проигнорировано
Definition: net.h:143
uint8_t * RX()
Definition: net.h:210
uint32_t bytesIn
байт принято
Definition: net.h:140
Inet4Address dest
Адрес соединения
Definition: net.h:163
size_t connections
Количество соединений
Definition: net.h:138
virtual void registerSocketListener(SocketListener *server)=0
virtual void release(Socket *socket)=0
uint8_t * RX(size_t offset)
Definition: net.h:215
virtual void onRegister()
Definition: net.h:293
uint16_t getPort()
Definition: net.h:112
size_t _RXSize
размер входящего буфера
Definition: net.h:158
void * _attachment
пользовательские данные
Definition: net.h:159
virtual uint32_t timeout()
Definition: net.h:279
Definition: net.h:32
size_t send_P(const void *buffer, size_t size)
Definition: net.h:173
Definition: net.h:247
Definition: net.h:155
void * attachment()
Definition: net.h:198
virtual void close()=0
void setPort(uint16_t port)
Definition: net.h:264
Definition: net.h:307
Definition: net.h:96
virtual void serve()=0
SocketListener(const uint8_t protocol, const uint16_t port)
Definition: net.h:259
virtual void onIdle()
Definition: net.h:297
virtual void handle(Socket *socket)=0
virtual size_t recv()=0
virtual bool isLinkUp()=0
uint32_t packetsIn
пакетов принято
Definition: net.h:139
uint32_t bytesOut
байт отправлено
Definition: net.h:142
size_t RXSize()
Definition: net.h:206