TinyPPP
PPP networking over any Arduino Stream
Loading...
Searching...
No Matches
TinyPPP_Generic.h
Go to the documentation of this file.
1#pragma once
16#include "IPStack.h"
17#include "PPPEngine.h"
18#include "PPPSockets.h"
19
20namespace tinyppp {
21
26class TinyPPP_Generic : public PPPEngine, public IPStack {
27 public:
29 using PacketCallback = void (*)(const uint8_t *data, size_t len, void *ref);
31 using StateCallback = void (*)(bool up, void *ref);
32
34 bool begin(Stream &io) {
35 IPStack::current() = this;
36 start(io);
37 return true;
38 }
39
41 void end() { stop(); }
42
44 void loop() { poll(); }
45
56
60 while (!connected()) {
61 if (millis() - start >= timeoutMs) return false;
62 loop();
63 delay(1);
64 }
65 return true;
66 }
67
69 bool connected() const { return isUp(); }
73 IPAddress gatewayIP() const { return toIPAddress(peerIP()); }
75 IPAddress subnetMask() const { return IPAddress(255, 255, 255, 255); }
77 IPAddress dnsIP(int n = 0) const {
78 return toIPAddress(n == 0 ? dns1() : dns2());
79 }
80
83 void onReceive(PacketCallback cb, void *ref = nullptr) {
85 _rxRef = ref;
86 }
88 void onStateChange(StateCallback cb, void *ref = nullptr) {
90 _stateRef = ref;
91 }
92
94 bool send(const uint8_t *data, size_t len) { return sendIPv4(data, len); }
95
99
100 protected:
102 void *_rxRef = nullptr;
104 void *_stateRef = nullptr;
105
106 void onIPv4Packet(const uint8_t *data, size_t len) override {
107 // with a raw callback, unknown connections are not answered with resets
108 if (ipInput(data, len, _rxCallback == nullptr)) return;
109 if (_rxCallback) _rxCallback(data, len, _rxRef);
110 }
111
112 void onLinkUp() override {
114 }
115
116 void onLinkDown() override {
117 ipLinkDown();
119 }
120
121 // IPStack link layer
122 bool ipBegin() override { return beginIPv4(); }
123 void ipWrite(const uint8_t *data, size_t len) override { writeIPv4(data, len); }
124 bool ipEnd() override { return endIPv4(); }
125 void ipPoll() override { process(); }
126 bool ipIsUp() const override { return isUp(); }
127 ip4_t ipLocal() const override { return PPPEngine::localIP(); }
128 ip4_t ipDns(int n) const override { return n == 0 ? dns1() : dns2(); }
129 uint16_t ipMtu() const override {
130 uint16_t m = mtu();
131 return m > TINYPPP_MRU ? TINYPPP_MRU : m;
132 }
133};
134
136using TinyPPP = TinyPPP_Generic;
137
138} // namespace tinyppp
#define TINYPPP_MRU
Definition PPPEngine.h:21
Minimal IPv4 stack (ICMP echo, UDP, TCP, DNS resolver) for platforms without lwIP.
Definition IPStack.h:195
bool ipInput(const uint8_t *data, size_t len, bool reply=true)
Definition IPStack.h:542
void ipLinkDown()
Called when the link goes down: all connections are reset.
Definition IPStack.h:571
static IPStack *& current()
The stack used by sockets that were created without an explicit stack.
Definition IPStack.h:215
void poll()
Processes the link (input and timers): called by all socket methods.
Definition IPStack.h:221
Portable PPP engine: LCP, PAP client and IPCP over an Arduino Stream.
Definition PPPEngine.h:134
void writeIPv4(const uint8_t *data, size_t len)
Adds data to the packet started with beginIPv4()
Definition PPPEngine.h:266
bool sendIPv4(const uint8_t *data, size_t len)
Sends a complete IPv4 packet.
Definition PPPEngine.h:253
void start(Stream &io)
Starts the PPP negotiation on the indicated stream.
Definition PPPEngine.h:165
void process()
Reads available input and runs the timers: call frequently.
Definition PPPEngine.h:196
void setOfferedDNS(ip4_t dns1, ip4_t dns2=0)
DNS servers to offer the peer if it asks for them (server role)
Definition PPPEngine.h:143
void setLocalIP(ip4_t ip)
Optional static address configuration. 0 = negotiate with the peer.
Definition PPPEngine.h:139
ip4_t localIP() const
Our IP address (0 if not negotiated yet)
Definition PPPEngine.h:286
void setLogger(Print *log)
Debug output (nullptr to disable)
Definition PPPEngine.h:162
ip4_t dns2() const
Second DNS server received from the peer.
Definition PPPEngine.h:292
ip4_t dns1() const
First DNS server received from the peer.
Definition PPPEngine.h:290
void setPeerIP(ip4_t ip)
Address to assign to the peer if it requests one (server role)
Definition PPPEngine.h:141
void stop()
Closes the link gracefully (sends LCP Terminate-Request)
Definition PPPEngine.h:180
ip4_t peerIP() const
IP address of the peer.
Definition PPPEngine.h:288
bool beginIPv4()
Streaming API for chained buffers (e.g. lwIP pbuf)
Definition PPPEngine.h:260
bool endIPv4()
Sends the packet started with beginIPv4()
Definition PPPEngine.h:268
bool isUp() const
true when IPCP is open and IPv4 packets can be exchanged
Definition PPPEngine.h:280
uint16_t mtu() const
MTU we may use for sending (peer MRU)
Definition PPPEngine.h:294
Byte ring buffer with a fixed capacity of N bytes.
Definition IPStack.h:76
PPP network interface for platforms without lwIP: the PPP engine combined with the TinyPPP IPStack (T...
Definition TinyPPP_Generic.h:26
ip4_t ipLocal() const override
Definition TinyPPP_Generic.h:127
bool begin(Stream &io)
Starts the PPP negotiation over the indicated (already opened) stream.
Definition TinyPPP_Generic.h:34
IPAddress subnetMask() const
Always 255.255.255.255 (point-to-point link)
Definition TinyPPP_Generic.h:75
void * _stateRef
Definition TinyPPP_Generic.h:104
void onLinkUp() override
Called when IPCP is opened: addresses are available.
Definition TinyPPP_Generic.h:112
ip4_t ipDns(int n) const override
Definition TinyPPP_Generic.h:128
void onReceive(PacketCallback cb, void *ref=nullptr)
Definition TinyPPP_Generic.h:83
PacketCallback _rxCallback
Definition TinyPPP_Generic.h:101
bool send(const uint8_t *data, size_t len)
Sends a raw IPv4 packet.
Definition TinyPPP_Generic.h:94
void * _rxRef
Definition TinyPPP_Generic.h:102
bool waitConnected(uint32_t timeoutMs=30000)
Waits (while processing the link) until IPCP is up.
Definition TinyPPP_Generic.h:58
uint16_t ipMtu() const override
Definition TinyPPP_Generic.h:129
void ipPoll() override
Processes link input: received packets are passed to ipInput()
Definition TinyPPP_Generic.h:125
IPAddress localIP() const
Our IP address.
Definition TinyPPP_Generic.h:71
StateCallback _stateCallback
Definition TinyPPP_Generic.h:103
bool ipBegin() override
Starts an outgoing IPv4 packet.
Definition TinyPPP_Generic.h:122
bool connected() const
true when the link is up and has an IP address
Definition TinyPPP_Generic.h:69
void end()
Terminates the link.
Definition TinyPPP_Generic.h:41
bool config(IPAddress local, IPAddress peer=IPAddress(0, 0, 0, 0), IPAddress dns1=IPAddress(0, 0, 0, 0), IPAddress dns2=IPAddress(0, 0, 0, 0))
Definition TinyPPP_Generic.h:48
void onIPv4Packet(const uint8_t *data, size_t len) override
Called for each received IPv4 packet.
Definition TinyPPP_Generic.h:106
void ipWrite(const uint8_t *data, size_t len) override
Adds data to the outgoing packet.
Definition TinyPPP_Generic.h:123
void setLogger(Print &out)
Debug output, e.g. setLogger(Serial1)
Definition TinyPPP_Generic.h:98
void onStateChange(StateCallback cb, void *ref=nullptr)
Callback for link up/down; ref is passed to the callback.
Definition TinyPPP_Generic.h:88
IPAddress dnsIP(int n=0) const
The DNS server n (0 or 1) received from the peer.
Definition TinyPPP_Generic.h:77
void onLinkDown() override
Called when the IP link goes down.
Definition TinyPPP_Generic.h:116
IPAddress gatewayIP() const
The IP address of the peer.
Definition TinyPPP_Generic.h:73
bool ipEnd() override
Sends the packet.
Definition TinyPPP_Generic.h:124
bool ipIsUp() const override
Definition TinyPPP_Generic.h:126
void loop()
Processes input and timers: call regularly.
Definition TinyPPP_Generic.h:44
TinyPPP internals: PPP engine, HDLC framing and the IP stack.
Definition HDLC.h:11
IPAddress toIPAddress(ip4_t ip)
Converts an engine address to an Arduino IPAddress.
Definition PPPEngine.h:1146
ip4_t fromIPAddress(const IPAddress &ip)
Converts an Arduino IPAddress to an engine address (0 if not set)
Definition PPPEngine.h:1152
TinyPPP_ESP32 TinyPPP
The PPP network interface of this platform.
Definition TinyPPP_ESP32.h:304