RSeries astromech firmware
WifiMarcduinoReceiver.h
Go to the documentation of this file.
1 #ifndef WifiMarcduinoReceiver_h
2 #define WifiMarcduinoReceiver_h
3 
4 #include "ReelTwo.h"
5 #include "wifi/WifiAccess.h"
6 #include <WiFiClient.h>
7 
8 #ifndef JAWALITE_IDLE_TIMEOUT
9 #define JAWALITE_IDLE_TIMEOUT 5000
10 #endif
11 
37 template<unsigned maxClients = 1, uint16_t BUFFER_SIZE = 64>
38 class WifiMarcduinoReceiverBase : public WiFiServer, public WifiAccess::Notify, public AnimatedEvent
39 {
40 public:
41  WiFiClient fClients[maxClients];
42  uint32_t fClientLastMsg[maxClients] = {};
43 
50  WifiMarcduinoReceiverBase(WifiAccess &wifiAccess, uint16_t port = 2000) :
51  WiFiServer(port)
52  {
53  wifiAccess.addNotify(this);
54  }
55 
56  void setCommandHandler(void (*commandHandler)(const char* cmd))
57  {
58  fCommandHandler = commandHandler;
59  }
60 
61  void setEnabled(bool enabled)
62  {
63  fEnabled = enabled;
64  }
65 
66  bool enabled()
67  {
68  return fEnabled;
69  }
70 
71  virtual void wifiConnected(WifiAccess& access) override
72  {
73  DEBUG_PRINTLN("WifiMarcduinoReceiverBase.wifiConnected");
74  if (!fStarted && enabled())
75  {
76  begin();
77  fStarted = true;
78  }
79  }
80 
81  virtual void wifiDisconnected(WifiAccess& access) override
82  {
83  DEBUG_PRINTLN("WifiMarcduinoReceiverBase.wifiDisconnected");
84  for (unsigned i = 0; i < maxClients; i++)
85  {
86  if (fClients[i])
87  {
88  fClients[i].stop();
89  fPos[i] = 0;
90  }
91  }
92  }
93 
97  virtual void animate() override
98  {
99  unsigned i;
100  if (!enabled())
101  return;
102  //check if there are any new clients
103  if (hasClient())
104  {
105  ::printf("HAS CLIENT\n");
106  for (i = 0; i < maxClients; i++)
107  {
108  //find free/disconnected spot
109  if (!fClients[i] || !fClients[i].connected())
110  {
111  if (fClients[i])
112  fClients[i].stop();
113  fClients[i] = available();
114  if (!fClients[i])
115  DEBUG_PRINTLN("available broken");
116  DEBUG_PRINT("New client: ");
117  DEBUG_PRINT(i); DEBUG_PRINT(' ');
118  DEBUG_PRINTLN(fClients[i].remoteIP());
119  fPos[i] = 0;
120  fClientLastMsg[i] = millis();
121  break;
122  }
123  }
124  if (i >= maxClients)
125  {
126  //no free/disconnected spot so reject
127  available().stop();
128  DEBUG_PRINTLN("CLIENT REJECT");
129  }
130  }
131  //check clients for data
132  for (i = 0; i < maxClients; i++)
133  {
134  if (fClients[i] && fClients[i].connected())
135  {
136  if (fClients[i].available())
137  {
138  //get data from the telnet client and push it to the UART
139  while (fClients[i].available())
140  {
141  char ch = fClients[i].read();
142  if (ch == 0x0D)
143  {
144  fBuffer[i][fPos[i]] = '\0';
145  fPos[i] = 0;
146  if (fCommandHandler != nullptr && fBuffer[i][0] != '\0')
147  {
148  fCommandHandler(fBuffer[i]);
149  }
150  fClientLastMsg[i] = millis();
151  }
152  else if (fPos[i] < BUFFER_SIZE-1)
153  {
154  fBuffer[i][fPos[i]++] = ch;
155  }
156  fClients[i].write(ch);
157  }
158  }
159  else if (fClientLastMsg[i] + JAWALITE_IDLE_TIMEOUT < millis())
160  {
161  DEBUG_PRINTLN("CLIENT TIMEOUT");
162  fClients[i].stop();
163  fPos[i] = 0;
164  }
165  }
166  else
167  {
168  if (fClients[i])
169  {
170  DEBUG_PRINTLN("CLIENT DISCONNECTED");
171  fClients[i].stop();
172  fPos[i] = 0;
173  }
174  }
175  }
176  }
177 
178 private:
179  bool fStarted = false;
180  bool fEnabled = true;
181  void (*fCommandHandler)(const char* cmd) = nullptr;
182  unsigned fPos[maxClients] = {};
183  char fBuffer[maxClients][BUFFER_SIZE];
184 };
185 
203 #endif
WifiMarcduinoReceiverBase::WifiMarcduinoReceiverBase
WifiMarcduinoReceiverBase(WifiAccess &wifiAccess, uint16_t port=2000)
Constructor.
Definition: WifiMarcduinoReceiver.h:50
WifiMarcduinoReceiverBase::setCommandHandler
void setCommandHandler(void(*commandHandler)(const char *cmd))
Definition: WifiMarcduinoReceiver.h:56
WifiAccess
Definition: WifiAccess.h:25
DEBUG_PRINT
#define DEBUG_PRINT(s)
Definition: ReelTwo.h:189
ReelTwo.h
AnimatedEvent
Base class for all animated devices. AnimatedEvent::animate() is called for each device once through ...
Definition: AnimatedEvent.h:18
WifiMarcduinoReceiverBase::fClients
WiFiClient fClients[maxClients]
Definition: WifiMarcduinoReceiver.h:41
WifiMarcduinoReceiverBase::animate
virtual void animate() override
Dispatch any received i2c event to CommandEvent.
Definition: WifiMarcduinoReceiver.h:97
WifiAccess::Notify
Definition: WifiAccess.h:166
DEBUG_PRINTLN
#define DEBUG_PRINTLN(s)
Definition: ReelTwo.h:188
WifiMarcduinoReceiverBase::wifiDisconnected
virtual void wifiDisconnected(WifiAccess &access) override
Definition: WifiMarcduinoReceiver.h:81
WifiMarcduinoReceiverBase
Base template of Marcduino receiver over WiFi.
Definition: WifiMarcduinoReceiver.h:38
WifiAccess::addNotify
void addNotify(WifiAccess::Notify *client)
Definition: WifiAccess.h:173
WifiAccess.h
WifiMarcduinoReceiver
WifiMarcduinoReceiverBase WifiMarcduinoReceiver
Definition: WifiMarcduinoReceiver.h:202
JAWALITE_IDLE_TIMEOUT
#define JAWALITE_IDLE_TIMEOUT
Definition: WifiMarcduinoReceiver.h:9
WifiMarcduinoReceiverBase::wifiConnected
virtual void wifiConnected(WifiAccess &access) override
Definition: WifiMarcduinoReceiver.h:71
WifiMarcduinoReceiverBase::setEnabled
void setEnabled(bool enabled)
Definition: WifiMarcduinoReceiver.h:61
WifiMarcduinoReceiverBase::enabled
bool enabled()
Definition: WifiMarcduinoReceiver.h:66
WifiMarcduinoReceiverBase::fClientLastMsg
uint32_t fClientLastMsg[maxClients]
Definition: WifiMarcduinoReceiver.h:42