MQTT
IPStack/EthernetInterfaceIPStack.h@25:d13a6c558164, 2014-05-09 (annotated)
- Committer:
- sam_grove
- Date:
- Fri May 09 23:01:35 2014 +0000
- Revision:
- 25:d13a6c558164
Simplify example by moving IPStack into this library for each transport. Working on FP usage to not need the FP.cpp inclusion in main
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 25:d13a6c558164 | 1 | |
sam_grove | 25:d13a6c558164 | 2 | #ifndef ETHERNETINTERFACEIPSTACK_H |
sam_grove | 25:d13a6c558164 | 3 | #define ETHERNETINTERFACEIPSTACK_H |
sam_grove | 25:d13a6c558164 | 4 | |
sam_grove | 25:d13a6c558164 | 5 | #include "EthernetInterface.h" |
sam_grove | 25:d13a6c558164 | 6 | |
sam_grove | 25:d13a6c558164 | 7 | class IPStack |
sam_grove | 25:d13a6c558164 | 8 | { |
sam_grove | 25:d13a6c558164 | 9 | public: |
sam_grove | 25:d13a6c558164 | 10 | IPStack() |
sam_grove | 25:d13a6c558164 | 11 | { |
sam_grove | 25:d13a6c558164 | 12 | eth.init(); // Use DHCP |
sam_grove | 25:d13a6c558164 | 13 | eth.connect(); |
sam_grove | 25:d13a6c558164 | 14 | mysock.set_blocking(false, 1000); // 1 second Timeout |
sam_grove | 25:d13a6c558164 | 15 | } |
sam_grove | 25:d13a6c558164 | 16 | |
sam_grove | 25:d13a6c558164 | 17 | int connect(char* hostname, int port) |
sam_grove | 25:d13a6c558164 | 18 | { |
sam_grove | 25:d13a6c558164 | 19 | return mysock.connect(hostname, port); |
sam_grove | 25:d13a6c558164 | 20 | } |
sam_grove | 25:d13a6c558164 | 21 | |
sam_grove | 25:d13a6c558164 | 22 | int read(char* buffer, int len, int timeout) |
sam_grove | 25:d13a6c558164 | 23 | { |
sam_grove | 25:d13a6c558164 | 24 | mysock.set_blocking(false, timeout); |
sam_grove | 25:d13a6c558164 | 25 | return mysock.receive(buffer, len); |
sam_grove | 25:d13a6c558164 | 26 | } |
sam_grove | 25:d13a6c558164 | 27 | |
sam_grove | 25:d13a6c558164 | 28 | int write(char* buffer, int len, int timeout) |
sam_grove | 25:d13a6c558164 | 29 | { |
sam_grove | 25:d13a6c558164 | 30 | mysock.set_blocking(false, timeout); |
sam_grove | 25:d13a6c558164 | 31 | return mysock.send(buffer, len); |
sam_grove | 25:d13a6c558164 | 32 | } |
sam_grove | 25:d13a6c558164 | 33 | |
sam_grove | 25:d13a6c558164 | 34 | int disconnect() |
sam_grove | 25:d13a6c558164 | 35 | { |
sam_grove | 25:d13a6c558164 | 36 | return mysock.close(); |
sam_grove | 25:d13a6c558164 | 37 | } |
sam_grove | 25:d13a6c558164 | 38 | |
sam_grove | 25:d13a6c558164 | 39 | private: |
sam_grove | 25:d13a6c558164 | 40 | |
sam_grove | 25:d13a6c558164 | 41 | EthernetInterface eth; |
sam_grove | 25:d13a6c558164 | 42 | TCPSocketConnection mysock; |
sam_grove | 25:d13a6c558164 | 43 | |
sam_grove | 25:d13a6c558164 | 44 | }; |
sam_grove | 25:d13a6c558164 | 45 | |
sam_grove | 25:d13a6c558164 | 46 | |
sam_grove | 25:d13a6c558164 | 47 | class Countdown |
sam_grove | 25:d13a6c558164 | 48 | { |
sam_grove | 25:d13a6c558164 | 49 | public: |
sam_grove | 25:d13a6c558164 | 50 | Countdown() |
sam_grove | 25:d13a6c558164 | 51 | { |
sam_grove | 25:d13a6c558164 | 52 | t = Timer(); |
sam_grove | 25:d13a6c558164 | 53 | } |
sam_grove | 25:d13a6c558164 | 54 | |
sam_grove | 25:d13a6c558164 | 55 | Countdown(int ms) |
sam_grove | 25:d13a6c558164 | 56 | { |
sam_grove | 25:d13a6c558164 | 57 | t = Timer(); |
sam_grove | 25:d13a6c558164 | 58 | countdown_ms(ms); |
sam_grove | 25:d13a6c558164 | 59 | } |
sam_grove | 25:d13a6c558164 | 60 | |
sam_grove | 25:d13a6c558164 | 61 | |
sam_grove | 25:d13a6c558164 | 62 | bool expired() |
sam_grove | 25:d13a6c558164 | 63 | { |
sam_grove | 25:d13a6c558164 | 64 | return t.read_ms() >= interval_end_ms; |
sam_grove | 25:d13a6c558164 | 65 | } |
sam_grove | 25:d13a6c558164 | 66 | |
sam_grove | 25:d13a6c558164 | 67 | void countdown_ms(int ms) |
sam_grove | 25:d13a6c558164 | 68 | { |
sam_grove | 25:d13a6c558164 | 69 | t.stop(); |
sam_grove | 25:d13a6c558164 | 70 | interval_end_ms = ms; |
sam_grove | 25:d13a6c558164 | 71 | t.reset(); |
sam_grove | 25:d13a6c558164 | 72 | t.start(); |
sam_grove | 25:d13a6c558164 | 73 | } |
sam_grove | 25:d13a6c558164 | 74 | |
sam_grove | 25:d13a6c558164 | 75 | void countdown(int seconds) |
sam_grove | 25:d13a6c558164 | 76 | { |
sam_grove | 25:d13a6c558164 | 77 | countdown_ms(seconds * 1000); |
sam_grove | 25:d13a6c558164 | 78 | } |
sam_grove | 25:d13a6c558164 | 79 | |
sam_grove | 25:d13a6c558164 | 80 | int left_ms() |
sam_grove | 25:d13a6c558164 | 81 | { |
sam_grove | 25:d13a6c558164 | 82 | return interval_end_ms - t.read_ms(); |
sam_grove | 25:d13a6c558164 | 83 | } |
sam_grove | 25:d13a6c558164 | 84 | |
sam_grove | 25:d13a6c558164 | 85 | private: |
sam_grove | 25:d13a6c558164 | 86 | Timer t; |
sam_grove | 25:d13a6c558164 | 87 | int interval_end_ms; |
sam_grove | 25:d13a6c558164 | 88 | }; |
sam_grove | 25:d13a6c558164 | 89 | |
sam_grove | 25:d13a6c558164 | 90 | #endif |