ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
netconfig.cpp
Go to the documentation of this file.
1 /*
2  netconfig.cpp - network functions class
3 
4  Copyright (c) 2018 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 
21 #include "../../include/esp3d_config.h"
22 #if defined (WIFI_FEATURE) || defined (ETH_FEATURE) || defined (BLUETOOTH_FEATURE)
23 #ifdef ARDUINO_ARCH_ESP32
24 #define WIFI_EVENT_STAMODE_CONNECTED SYSTEM_EVENT_STA_CONNECTED
25 #define WIFI_EVENT_STAMODE_DISCONNECTED SYSTEM_EVENT_STA_DISCONNECTED
26 #define WIFI_EVENT_STAMODE_GOT_IP SYSTEM_EVENT_STA_GOT_IP
27 #define WIFI_EVENT_SOFTAPMODE_STACONNECTED SYSTEM_EVENT_AP_STACONNECTED
28 #endif //ARDUINO_ARCH_ESP32
29 #ifdef ARDUINO_ARCH_ESP8266
30 #endif //ARDUINO_ARCH_ESP8266
31 #include "netconfig.h"
32 #if defined (WIFI_FEATURE)
33 #include "../wifi/wificonfig.h"
34 #endif //WIFI_FEATURE
35 #if defined (ETH_FEATURE)
36 #include "../ethernet/ethconfig.h"
37 #endif //ETH_FEATURE
38 #if defined (BLUETOOTH_FEATURE)
39 #include "../bluetooth/BT_service.h"
40 #endif //BLUETOOTH_FEATURE
41 #include "netservices.h"
42 #include "../../core/esp3doutput.h"
43 #include "../../core/settings_esp3d.h"
44 
45 String NetConfig::_hostname = "";
46 bool NetConfig::_needReconnect2AP = false;
47 bool NetConfig::_events_registered = false;
48 bool NetConfig::_started = false;
49 uint8_t NetConfig::_mode = ESP_RADIO_OFF;
51 {
52 }
53 
55 {
56  //end();
57 }
58 
59 //just simple helper to convert mac address to string
60 char * NetConfig::mac2str (uint8_t mac [8])
61 {
62  static char macstr [18];
63  if (0 > sprintf (macstr, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]) ) {
64  strcpy (macstr, "00:00:00:00:00:00");
65  }
66  return macstr;
67 }
68 
69 
73 uint32_t NetConfig::IP_int_from_string(const char * s)
74 {
75  uint32_t ip_int = 0;
76  IPAddress ipaddr;
77  if (ipaddr.fromString(s)) {
78  ip_int = ipaddr;
79  }
80  return ip_int;
81 }
82 
86 String NetConfig::IP_string_from_int(uint32_t ip_int)
87 {
88  IPAddress ipaddr(ip_int);
89  return ipaddr.toString();
90 }
91 
96 bool NetConfig::isHostnameValid (const char * hostname)
97 {
98  //limited size
99  char c;
100  if (strlen (hostname) > MAX_HOSTNAME_LENGTH || strlen (hostname) < MIN_HOSTNAME_LENGTH) {
101  return false;
102  }
103  //only letter and digit
104  for (uint i = 0; i < strlen (hostname); i++) {
105  c = hostname[i];
106  if (! (isdigit (c) || isalpha (c) || c == '-') ) {
107  return false;
108  }
109  if (c == ' ') {
110  return false;
111  }
112  }
113  return true;
114 }
115 
120 {
121  static String currentIP = "";
122 #if defined( WIFI_FEATURE)
123  if (WiFi.getMode() == WIFI_STA) {
124  currentIP = WiFi.localIP().toString();
125  } else if (WiFi.getMode() == WIFI_AP) {
126  currentIP = WiFi.softAPIP().toString();
127  }
128 #endif //WIFI_FEATURE
129 #if defined (ETH_FEATURE)
130  if (EthConfig::started()) {
131  currentIP = ETH.localIP().toString();
132  }
133 #endif //ETH_FEATURE
134  if (currentIP.length() == 0) {
135  currentIP = "0.0.0.0";
136  }
137  return currentIP;
138 }
139 
144 bool NetConfig::isValidIP(const char * string)
145 {
146  IPAddress ip;
147  return ip.fromString(string);
148 }
149 
150 
151 //wifi event
152 void NetConfig::onWiFiEvent(WiFiEvent_t event)
153 {
155  switch (event) {
156  case WIFI_EVENT_STAMODE_CONNECTED:
157  _needReconnect2AP = false;
158  break;
159  case WIFI_EVENT_STAMODE_DISCONNECTED: {
160  if(_started) {
161  output.printMSG ("Disconnected");
162  ESP3DGlobalOutput::SetStatus("Disconnected");
163  _needReconnect2AP = true;
164  }
165  }
166  break;
167  case WIFI_EVENT_STAMODE_GOT_IP: {
168  output.printMSG ("Connected");
169  ESP3DGlobalOutput::SetStatus("Connected");
171  output.printMSG (WiFi.localIP().toString().c_str());
172  }
173  break;
174  case WIFI_EVENT_SOFTAPMODE_STACONNECTED: {
175  output.printMSG ("New client");
176  ESP3DGlobalOutput::SetStatus("New client");
177  }
178  break;
179 #ifdef ARDUINO_ARCH_ESP32
180  case SYSTEM_EVENT_STA_LOST_IP:
181  if(_started) {
182  _needReconnect2AP = true;
183  }
184  break;
185 #ifdef ETH_FEATURE
186  case SYSTEM_EVENT_ETH_CONNECTED: {
187  output.printMSG ("Cable connected");
188  ESP3DGlobalOutput::SetStatus("Cable connected");
189  }
190  break;
191  case SYSTEM_EVENT_ETH_DISCONNECTED: {
192  output.printMSG ("Cable disconnected");
193  ESP3DGlobalOutput::SetStatus("Cable disconnected");
194  }
195  break;
196  case SYSTEM_EVENT_ETH_GOT_IP:
197  output.printMSG (ETH.localIP().toString().c_str());
199  break;
200 #endif //ETH_FEATURE
201 #endif //ARDUINO_ARCH_ESP32
202  default:
203  break;
204  }
205 }
206 
208 {
209  return _mode;
210 }
211 
216 {
217  bool res = false;
218  //clear everything
219  end();
220  int8_t espMode =Settings_ESP3D::read_byte(ESP_RADIO_MODE);
222  if (espMode != NO_NETWORK) {
223  output.printMSG("Starting Network");
224  }
225  //setup events
226  if(!_events_registered) {
227 #ifdef ARDUINO_ARCH_ESP8266
228 #pragma GCC diagnostic push
229 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
230  WiFi.onEvent(NetConfig::onWiFiEvent, WIFI_EVENT_ANY);
231 #pragma GCC diagnostic pop
232 #endif
233 #ifdef ARDUINO_ARCH_ESP32
234  WiFi.onEvent(NetConfig::onWiFiEvent);
235 
236 #endif
237  _events_registered = true;
238  }
239  //Get hostname
241  _mode = espMode;
242  if (espMode == NO_NETWORK) {
244  return true;
245  }
246 #if defined (WIFI_FEATURE)
247  if ((espMode == ESP_WIFI_AP) || (espMode == ESP_WIFI_STA)) {
248  res = WiFiConfig::begin();
249  //in case STA failed and fallback to AP mode
250  if (WiFi.getMode() == WIFI_AP) {
251  _mode = ESP_WIFI_AP;
252  }
253  }
254 #endif //WIFI_FEATURE
255 #if defined (ETH_FEATURE)
256  //if ((espMode == ESP_ETH_STA) || (espMode == ESP_ETH_SRV)) {
257  if ((espMode == ESP_ETH_STA)) {
258  res = EthConfig::begin();
259  }
260 #endif //ETH_FEATURE
261 #if defined (BLUETOOTH_FEATURE)
262  if ((espMode == ESP_BT)) {
263  res = bt_service.begin();
264  }
265 #endif //BLUETOOTH_FEATURE
266  //if network is up, let's start services
267  if (res) {
268  _started = true;
269  bool start_services = false;
270 #if defined (ETH_FEATURE)
271  if (EthConfig::started()) {
272  start_services = true;
273  }
274 #endif //ETH_FEATURE
275 #if defined (WIFI_FEATURE)
276  if (WiFiConfig::started()) {
277  start_services = true;
278  }
279 #endif //WIFI_FEATURE
280  if (start_services) {
281  log_esp3d("Starting service");
282  res = NetServices::begin();
283  }
284  }
285  //work around as every services seems reset the AP name
286 #ifdef ARDUINO_ARCH_ESP32
287 #if defined (WIFI_FEATURE)
288  if (WiFi.getMode() == WIFI_AP) {
289  WiFi.softAPsetHostname(_hostname.c_str());
290  }
291 #endif //WIFI_FEATURE
292 #endif //ARDUINO_ARCH_ESP32
294  if (res) {
295  log_esp3d("Network config started");
296  } else {
297  end();
298  log_esp3d("Network config failed");
299  }
301  return res;
302 }
303 
309 {
312  _mode = ESP_RADIO_OFF;
313 #if defined (WIFI_FEATURE)
314  WiFiConfig::end();
315  _needReconnect2AP=false;
316 #else
317  WiFi.mode(WIFI_OFF);
318 #endif //WIFI_FEATURE
319 
320 #if defined (ETH_FEATURE)
321  EthConfig::end();
322 #endif //ETH_FEATURE
324 #if defined (BLUETOOTH_FEATURE)
325  bt_service.end();
326 #endif //BLUETOOTH_FEATURE
327  output.printMSG("Network Off");
328  _started = false;
329 }
330 
331 const char* NetConfig::hostname(bool fromsettings)
332 {
333  if (fromsettings) {
335  return _hostname.c_str();
336  }
337 #if defined (WIFI_FEATURE)
338  if(WiFi.getMode()!= WIFI_OFF) {
339  _hostname = WiFiConfig::hostname();
340  return _hostname.c_str();
341  }
342 #endif //WIFI_FEATURE
343 #if defined (ETH_FEATURE)
344  if(EthConfig::started()) {
345  return ETH.getHostname();
346  }
347 #endif //ETH_FEATURE
348 
349 #if defined (BLUETOOTH_FEATURE)
350  if(bt_service.started()) {
351  return bt_service.hostname();
352  }
353 #endif //BLUETOOTH_FEATURE
354  return _hostname.c_str();
355 }
356 
362 {
363  if (_started) {
364 #if defined (WIFI_FEATURE)
365  if(_needReconnect2AP) {
366 
367  if(WiFi.getMode()!= WIFI_OFF) {
368  begin();
369  }
370  }
372 #endif //WIFI_FEATURE
373 #if defined (ETH_FEATURE)
375 #endif //ETH_FEATURE
376 #if defined (BLUETOOTH_FEATURE)
377  bt_service.handle();
378 #endif //BLUETOOTH_FEATURE
380  //Debug
382  }
383 }
384 
385 bool NetConfig::isIPModeDHCP (uint8_t mode)
386 {
387  bool started = false;
388 #ifdef ARDUINO_ARCH_ESP32
389  tcpip_adapter_dhcp_status_t dhcp_status;
390  tcpip_adapter_dhcpc_get_status ((mode == ESP_WIFI_STA)?TCPIP_ADAPTER_IF_STA:(mode == ESP_WIFI_AP)?TCPIP_ADAPTER_IF_AP:TCPIP_ADAPTER_IF_ETH, &dhcp_status);
391  started = (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED);
392 #endif //ARDUINO_ARCH_ESP32
393 #ifdef ARDUINO_ARCH_ESP8266
394  (void)mode;
395  started = (wifi_station_dhcpc_status() == DHCP_STARTED);
396 #endif //ARDUINO_ARCH_ESP8266
397  return started;
398 }
399 
400 bool NetConfig::isDHCPServer (uint8_t mode)
401 {
402  bool itis = false;
403 #ifdef ARDUINO_ARCH_ESP32
404  tcpip_adapter_dhcp_status_t dhcp_status;
405  tcpip_adapter_dhcps_get_status ((mode == ESP_WIFI_STA)?TCPIP_ADAPTER_IF_STA:(mode == ESP_WIFI_AP)?TCPIP_ADAPTER_IF_AP:TCPIP_ADAPTER_IF_ETH, &dhcp_status);
406  itis = (dhcp_status == TCPIP_ADAPTER_DHCP_STARTED);
407 #endif //ARDUINO_ARCH_ESP32
408 #ifdef ARDUINO_ARCH_ESP8266
409  (void)mode;
410  itis = (wifi_softap_dhcps_status() == DHCP_STARTED);
411 #endif //ARDUINO_ARCH_ESP8266
412  return itis;
413 }
414 
415 #endif // WIFI_FEATURE || ETH_FEATURE
416 
ESP3DGlobalOutput::display_IP
static void display_IP(bool force=false)
Definition: esp3doutput.cpp:445
NetServices::begin
static bool begin()
Definition: netservices.cpp:85
Settings_ESP3D::read_string
static const char * read_string(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:794
EthConfig::handle
static void handle()
WiFiConfig::begin
static bool begin()
Definition: wificonfig.cpp:278
ESP_WIFI_STA
#define ESP_WIFI_STA
Definition: netconfig.h:41
BTService::end
void end()
NetConfig::isValidIP
static bool isValidIP(const char *string)
Definition: netconfig.cpp:144
netservices.h
MAX_HOSTNAME_LENGTH
#define MAX_HOSTNAME_LENGTH
Definition: netconfig.h:32
NetConfig::IP_string_from_int
static String IP_string_from_int(uint32_t ip_int)
Definition: netconfig.cpp:86
NetConfig::isIPModeDHCP
static bool isIPModeDHCP(uint8_t mode)
Definition: netconfig.cpp:385
WiFiConfig::end
static void end()
Definition: wificonfig.cpp:301
MIN_HOSTNAME_LENGTH
#define MIN_HOSTNAME_LENGTH
Definition: netconfig.h:33
NetConfig::end
static void end()
Definition: netconfig.cpp:308
NetConfig::~NetConfig
~NetConfig()
Definition: netconfig.cpp:54
NetConfig::hostname
static const char * hostname(bool fromsettings=false)
Definition: netconfig.cpp:331
DEBUG_ESP3D_NETWORK_INIT
#define DEBUG_ESP3D_NETWORK_INIT
Definition: debug_esp3d.h:33
NetConfig::handle
static void handle()
Definition: netconfig.cpp:361
BTService::begin
bool begin()
EthConfig::started
static bool started()
netconfig.h
NetConfig::getMode
static uint8_t getMode()
Definition: netconfig.cpp:207
EthConfig::end
static void end()
NetConfig::begin
static bool begin()
Definition: netconfig.cpp:215
DEBUG_ESP3D_NETWORK_HANDLE
#define DEBUG_ESP3D_NETWORK_HANDLE
Definition: debug_esp3d.h:34
DEBUG_ESP3D_NETWORK_END
#define DEBUG_ESP3D_NETWORK_END
Definition: debug_esp3d.h:35
ESP3DOutput::printMSG
size_t printMSG(const char *s, bool withNL=true)
Definition: esp3doutput.cpp:190
ESP_HOSTNAME
#define ESP_HOSTNAME
Definition: settings_esp3d.h:55
NetConfig::isHostnameValid
static bool isHostnameValid(const char *hostname)
Definition: netconfig.cpp:96
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
ESP_BT
#define ESP_BT
Definition: netconfig.h:43
NO_NETWORK
#define NO_NETWORK
Definition: settings_esp3d.h:37
ESP_RADIO_OFF
#define ESP_RADIO_OFF
Definition: netconfig.h:40
ESP_ETH_STA
#define ESP_ETH_STA
Definition: netconfig.h:44
BTService::handle
void handle()
BTService::started
bool started()
NetConfig::started
static bool started()
Definition: netconfig.h:74
Settings_ESP3D::read_byte
static uint8_t read_byte(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:715
ESP_ALL_CLIENTS
#define ESP_ALL_CLIENTS
Definition: esp3doutput.h:30
bt_service
BTService bt_service
ESP_WIFI_AP
#define ESP_WIFI_AP
Definition: netconfig.h:42
BTService::hostname
const char * hostname()
EthConfig::begin
static bool begin()
NetConfig::NetConfig
NetConfig()
Definition: netconfig.cpp:50
WiFiConfig::hostname
static const char * hostname()
Definition: wificonfig.cpp:63
NetConfig::mac2str
static char * mac2str(uint8_t mac[8])
Definition: netconfig.cpp:60
WiFiConfig::started
static bool started()
Definition: wificonfig.cpp:270
NetConfig::IP_int_from_string
static uint32_t IP_int_from_string(const char *s)
Definition: netconfig.cpp:73
NetServices::end
static void end()
Definition: netservices.cpp:284
NetConfig::localIP
static String localIP()
Definition: netconfig.cpp:119
WiFiConfig::handle
static void handle()
Definition: wificonfig.cpp:319
ESP3DOutput
Definition: esp3doutput.h:48
NetConfig::isDHCPServer
static bool isDHCPServer(uint8_t mode)
Definition: netconfig.cpp:400
ESP_RADIO_MODE
#define ESP_RADIO_MODE
Definition: settings_esp3d.h:39
ESP3DGlobalOutput::SetStatus
static void SetStatus(const char *status)
Definition: esp3doutput.cpp:428
NetServices::handle
static void handle()
Definition: netservices.cpp:340