RSeries astromech firmware
WifiSerialBridge.h
Go to the documentation of this file.
1 #ifndef WifiSerialBridge_h
2 #define WifiSerialBridge_h
3 
4 #include "ReelTwo.h"
5 #include "core/SetupEvent.h"
6 #include "core/AnimatedEvent.h"
7 #include <WiFi.h>
8 #include <WiFiClient.h>
9 #include <WiFiAP.h>
10 
35 template<unsigned maxClients = 1>
36 class WifiSerialBridgeBase : public WiFiServer, public WifiAccess::Notify, public AnimatedEvent
37 {
38 public:
39  WiFiClient fClients[maxClients];
40 
47  WifiSerialBridgeBase(HardwareSerial& serial, WifiAccess &wifiAccess, uint16_t port = 2000) :
48  WiFiServer(port),
49  fSerial(serial)
50  {
51  wifiAccess.addNotify(this);
52  }
53 
54  void setEnabled(bool enabled)
55  {
56  fEnabled = enabled;
57  }
58 
59  bool enabled()
60  {
61  return fEnabled;
62  }
63 
64  virtual void wifiConnected(WifiAccess& access) override
65  {
66  DEBUG_PRINTLN("WifiSerialBridgeBase.wifiConnected");
67  if (!fStarted)
68  {
69  begin();
70  fStarted = true;
71  }
72  }
73 
74  virtual void wifiDisconnected(WifiAccess& access) override
75  {
76  DEBUG_PRINTLN("WifiSerialBridgeBase.wifiDisconnected");
77  for (unsigned i = 0; i < maxClients; i++)
78  {
79  if (fClients[i])
80  {
81  fClients[i].stop();
82  }
83  }
84  }
85 
89  virtual void animate() override
90  {
91  unsigned i;
92  if (!enabled())
93  return;
94  //check if there are any new clients
95  if (hasClient())
96  {
97  for (i = 0; i < maxClients; i++)
98  {
99  //find free/disconnected spot
100  if (!fClients[i] || !fClients[i].connected())
101  {
102  if (fClients[i])
103  fClients[i].stop();
104  fClients[i] = available();
105  if (!fClients[i])
106  DEBUG_PRINTLN("available broken");
107  DEBUG_PRINT("New client: ");
108  DEBUG_PRINT(i); DEBUG_PRINT(' ');
109  DEBUG_PRINTLN(fClients[i].remoteIP());
110  break;
111  }
112  }
113  if (i >= maxClients)
114  {
115  //no free/disconnected spot so reject
116  available().stop();
117  }
118  }
119  //check clients for data
120  for (i = 0; i < maxClients; i++)
121  {
122  if (fClients[i] && fClients[i].connected())
123  {
124  if (fClients[i].available())
125  {
126  //get data from the telnet client and push it to the UART
127  while (fClients[i].available())
128  {
129  char ch = fClients[i].read();
130  fClients[i].write(ch);
131  fSerial.write(ch);
132  }
133  }
134  }
135  else
136  {
137  if (fClients[i])
138  {
139  fClients[i].stop();
140  }
141  }
142  }
143  }
144 
145 private:
146  HardwareSerial& fSerial;
147  bool fStarted = false;
148  bool fEnabled = true;
149 };
150 
168 #endif
WifiSerialBridgeBase::wifiConnected
virtual void wifiConnected(WifiAccess &access) override
Definition: WifiSerialBridge.h:64
WifiAccess
Definition: WifiAccess.h:25
DEBUG_PRINT
#define DEBUG_PRINT(s)
Definition: ReelTwo.h:189
ReelTwo.h
SetupEvent.h
WifiSerialBridgeBase
Base template of automatic forwarder from i2c to CommandEvent.
Definition: WifiSerialBridge.h:36
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
WifiAccess::Notify
Definition: WifiAccess.h:166
AnimatedEvent.h
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
WifiSerialBridgeBase::enabled
bool enabled()
Definition: WifiSerialBridge.h:59
WifiSerialBridgeBase::animate
virtual void animate() override
Dispatch any received i2c event to CommandEvent.
Definition: WifiSerialBridge.h:89
WifiSerialBridgeBase::setEnabled
void setEnabled(bool enabled)
Definition: WifiSerialBridge.h:54
WifiAccess::addNotify
void addNotify(WifiAccess::Notify *client)
Definition: WifiAccess.h:173
WifiSerialBridgeBase::wifiDisconnected
virtual void wifiDisconnected(WifiAccess &access) override
Definition: WifiSerialBridge.h:74
WifiSerialBridgeBase::WifiSerialBridgeBase
WifiSerialBridgeBase(HardwareSerial &serial, WifiAccess &wifiAccess, uint16_t port=2000)
Constructor.
Definition: WifiSerialBridge.h:47
WifiSerialBridgeBase::fClients
WiFiClient fClients[maxClients]
Definition: WifiSerialBridge.h:39
WifiSerialBridge
WifiSerialBridgeBase WifiSerialBridge
Definition: WifiSerialBridge.h:167