ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
netservices.cpp
Go to the documentation of this file.
1 /*
2  netservices.cpp - network services functions class
3 
4  Copyright (c) 2014 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 
23 #include "netconfig.h"
24 #include "netservices.h"
25 #include "../../core/settings_esp3d.h"
26 #include "../../core/esp3doutput.h"
27 #if defined( ARDUINO_ARCH_ESP8266)
28 #ifdef MDNS_FEATURE
29 #include <ESP8266mDNS.h>
30 #endif //MDNS_FEATURE
31 #ifdef SSDP_FEATURE
32 #include <ESP8266SSDP.h>
33 #endif //SSDP_FEATURE
34 #endif //ARDUINO_ARCH_ESP8266
35 #if defined( ARDUINO_ARCH_ESP32)
36 #ifdef MDNS_FEATURE
37 #include <ESPmDNS.h>
38 #endif //MDNS_FEATURE
39 #ifdef SSDP_FEATURE
40 #include <ESP32SSDP.h>
41 #endif //SSDP_FEATURE
42 #endif //ARDUINO_ARCH_ESP32
43 #ifdef OTA_FEATURE
44 #include <ArduinoOTA.h>
45 #endif //OTA_FEATURE
46 #if defined(FILESYSTEM_FEATURE)
47 #include "../filesystem/esp_filesystem.h"
48 #endif //FILESYSTEM_FEATURE
49 #ifdef TELNET_FEATURE
50 #include "../telnet/telnet_server.h"
51 #endif //TELNET_FEATURE
52 #ifdef FTP_FEATURE
53 #include "../ftp/FtpServer.h"
54 #endif //FP_FEATURE
55 #ifdef HTTP_FEATURE
56 #include "../http/http_server.h"
57 #endif //HTTP_FEATURE
58 #if defined(HTTP_FEATURE) || defined(WS_DATA_FEATURE)
59 #include "../websocket/websocket_server.h"
60 #endif //HTTP_FEATURE || WS_DATA_FEATURE
61 #ifdef CAPTIVE_PORTAL_FEATURE
62 #include <DNSServer.h>
63 const byte DNS_PORT = 53;
64 DNSServer dnsServer;
65 #endif //CAPTIVE_PORTAL_FEATURE
66 #ifdef TIMESTAMP_FEATURE
67 #include "../time/time_server.h"
68 #endif //TIMESTAMP_FEATURE
69 #ifdef NOTIFICATION_FEATURE
70 #include "../notifications/notifications_service.h"
71 #endif //NOTIFICATION_FEATURE
72 #ifdef CAMERA_DEVICE
73 #include "../camera/camera.h"
74 #endif //CAMERA_DEVICE
75 bool NetServices::_started = false;
76 bool NetServices::_restart = false;
78 {
79 }
81 {
82  end();
83 }
84 
86 {
87  bool res = true;
88  _started = false;
89  String hostname = Settings_ESP3D::read_string(ESP_HOSTNAME);
91  end();
92 #ifdef TIMESTAMP_FEATURE
93  if (WiFi.getMode() != WIFI_AP) {
94  if(!timeserver.begin()) {
96  output.printERROR("Failed contact time servers!");
97  }
98  } else {
99  String tmp = "Time set :";
100  tmp+=timeserver.current_time();
101  output.printMSG(tmp.c_str());
102  }
103  }
104 #endif //TIMESTAMP_FEATURE
105 #if defined(MDNS_FEATURE) && defined(ARDUINO_ARCH_ESP8266)
106  if(WiFi.getMode() != WIFI_AP) {
107  String lhostname =hostname;
108  lhostname.toLowerCase();
109  if (!MDNS.begin(hostname.c_str())) {
110  output.printERROR("mDNS failed to start");
111  _started =false;
112  } else {
113  String stmp = "mDNS started with '" + lhostname + ".local'";
114  output.printMSG(stmp.c_str());
115  }
116  }
117 #endif //MDNS_FEATURE && ARDUINO_ARCH_ESP8266
118 
119 #ifdef OTA_FEATURE
120  if(WiFi.getMode() != WIFI_AP) {
121  ArduinoOTA.onStart([]() {
123  String type = "Start OTA updating ";
124  if (ArduinoOTA.getCommand() == U_FLASH) {
125  type += "sketch";
126  } else { // U_SPIFFS or any FS
127  // NOTE: if updating FS this would be the place to unmount FS using FS.end()
128  type += "filesystem";
129 #if defined(FILESYSTEM_FEATURE)
131 #endif //FILESYSTEM_FEATURE
132 
133  }
134  output.printMSG(type.c_str());
135  });
136  ArduinoOTA.onEnd([]() {
138  output.printMSG("End OTA");
139  });
140  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
141  String prg = "OTA Progress ";
143  prg += String(progress / (total / 100)) + "%";
144  output.printMSG(prg.c_str());
145  });
146  ArduinoOTA.onError([](ota_error_t error) {
147  String stmp = "OTA Error: " + String(error);
149  output.printERROR(stmp.c_str());
150  if (error == OTA_AUTH_ERROR) {
151  output.printERROR("Auth Failed");
152  } else if (error == OTA_BEGIN_ERROR) {
153  output.printERROR("Begin Failed");
154  } else if (error == OTA_CONNECT_ERROR) {
155  output.printERROR("Connect Failed");
156  } else if (error == OTA_RECEIVE_ERROR) {
157  output.printERROR("Receive Failed");
158  } else if (error == OTA_END_ERROR) {
159  output.printERROR("End Failed");
160  }
161  });
162  output.printMSG("OTA service started");
163  ArduinoOTA.begin();
164  }
165 #endif
166 
167 #if defined(MDNS_FEATURE) && defined(ARDUINO_ARCH_ESP32)
168  if(WiFi.getMode() != WIFI_AP) {
169  String lhostname =hostname;
170  lhostname.toLowerCase();
171  if (!MDNS.begin(hostname.c_str())) {
172  output.printERROR("mDNS failed to start");
173  _started =false;
174  } else {
175  String stmp = "mDNS started with '" + lhostname + ".local'";
176  output.printMSG(stmp.c_str());
177  }
178  }
179 #endif //MDNS_FEATURE && ARDUINO_ARCH_ESP8266
180 
181 #ifdef CAPTIVE_PORTAL_FEATURE
182  if(WiFi.getMode() == WIFI_AP) {
183  // if DNSServer is started with "*" for domain name, it will reply with
184  // provided IP to all DNS request
185  if (dnsServer.start(DNS_PORT, "*", WiFi.softAPIP())) {
186  output.printMSG("Captive Portal started");
187  } else {
188  output.printERROR("Failed start Captive Portal");
189  }
190  }
191 #endif //CAPTIVE_PORTAL_FEATURE
192 
193 #ifdef HTTP_FEATURE
194  if (!HTTP_Server::begin()) {
195  res= false;
196  output.printERROR("HTTP server failed");
197  } else {
198  if(HTTP_Server::started()) {
199  String stmp = "HTTP server started port " + String(HTTP_Server::port());
200  output.printMSG(stmp.c_str());
201  }
202  }
203 #endif //HTTP_FEATURE
204 #ifdef TELNET_FEATURE
205  if (!telnet_server.begin()) {
206  res= false;
207  output.printERROR("Telnet server failed");
208  } else {
209  if(telnet_server.started()) {
210  String stmp = "Telnet server started port " + String(telnet_server.port());
211  output.printMSG(stmp.c_str());
212  }
213  }
214 #endif //TELNET_FEATURE
215 #ifdef FTP_FEATURE
216  if (!ftp_server.begin()) {
217  res= false;
218  output.printERROR("Ftp server failed");
219  } else {
220  if(ftp_server.started()) {
221  String stmp = "Ftp server started ports: " + String(ftp_server.ctrlport()) + ","+ String(ftp_server.dataactiveport()) + ","+ String(ftp_server.datapassiveport());
222  output.printMSG(stmp.c_str());
223  }
224  }
225 #endif //FTP_FEATURE
226 #ifdef WS_DATA_FEATURE
228  output.printMSG("Failed start Terminal Web Socket");
229  } else {
231  String stmp = "Websocket server started port " + String(websocket_data_server.port());
232  output.printMSG(stmp.c_str());
233  }
234  }
235 #endif //WS_DATA_FEATURE
236 #if defined(HTTP_FEATURE)
238  output.printMSG("Failed start Terminal Web Socket");
239  }
240 #endif //HTTP_FEATURE
241 #ifdef MDNS_FEATURE
242  if(WiFi.getMode() != WIFI_AP) {
243  // Add service to MDNS-SD
244  MDNS.addService("http", "tcp", 80);
245  }
246 #endif //MDNS_FEATURE
247 #ifdef SSDP_FEATURE
248  //SSDP service presentation
249  if(WiFi.getMode() != WIFI_AP && HTTP_Server::started()) {
250  //Add specific for SSDP
251  String stmp = String(Hal::getChipID());
252  SSDP.setSchemaURL ("description.xml");
253  SSDP.setHTTPPort (HTTP_Server::port());
254  SSDP.setName (hostname.c_str());
255  SSDP.setURL ("/");
256  SSDP.setDeviceType ("upnp:rootdevice");
257  // SSDP.setSerialNumber (stmp.c_str());
258  //Any customization could be here
259  // SSDP.setModelName (ESP_MODEL_NAME);
260  // SSDP.setModelURL (ESP_MODEL_URL);
261  // SSDP.setModelNumber (ESP_MODEL_NUMBER);
262  // SSDP.setManufacturer (ESP_MANUFACTURER_NAME);
263  // SSDP.setManufacturerURL (ESP_MANUFACTURER_URL);
264  SSDP.begin();
265  stmp = "SSDP started with '" + hostname + "'";
266  output.printMSG(stmp.c_str());
267  }
268 #endif //SSDP_FEATURE
269 #ifdef NOTIFICATION_FEATURE
272 #endif //NOTIFICATION_FEATURE
273 #ifdef CAMERA_DEVICE
274  if (!esp3d_camera.begin()) {
275  output.printMSG("Failed start camera streaming server");
276  }
277 #endif //CAMERA_DEVICE
278  if (!res) {
279  end();
280  }
281  _started = res;
282  return _started;
283 }
285 {
286  _restart = false;
287  if(!_started) {
288  return;
289  }
290  _started = false;
291 #ifdef CAMERA_DEVICE
292  esp3d_camera.end();
293 #endif //CAMERA_DEVICE
294 #ifdef NOTIFICATION_FEATURE
296 #endif //NOTIFICATION_FEATURE
297 #ifdef CAPTIVE_PORTAL_FEATURE
298  if(WiFi.getMode() == WIFI_AP) {
299  dnsServer.stop();
300  }
301 #endif //CAPTIVE_PORTAL_FEATURE
302 #ifdef SSDP_FEATURE
303 #if defined(ARDUINO_ARCH_ESP32)
304  SSDP.end();
305 #endif //ARDUINO_ARCH_ESP32
306 #endif //SSDP_FEATURE
307 #ifdef MDNS_FEATURE
308 #if defined(ARDUINO_ARCH_ESP32)
309  if(WiFi.getMode() != WIFI_AP) {
310  mdns_service_remove("_http", "_tcp");
311  MDNS.end();
312  }
313 #endif // ARDUINO_ARCH_ESP32
314 #endif //MDNS_FEATURE
315 
316 #ifdef OTA_FEATURE
317 #if defined(ARDUINO_ARCH_ESP32)
318  if(WiFi.getMode() != WIFI_AP) {
319  ArduinoOTA.end();
320  }
321 #endif // ARDUINO_ARCH_ESP32
322 #endif //OTA_FEATURE
323 #if defined(HTTP_FEATURE)
325 #endif //HTTP_FEATURE
326 #ifdef HTTP_FEATURE
328 #endif //HTTP_FEATURE
329 #ifdef WS_DATA_FEATURE
331 #endif //WS_DATA_FEATURE
332 #ifdef TELNET_FEATURE
333  telnet_server.end();
334 #endif //TELNET_FEATURE
335 #ifdef FTP_FEATURE
336  ftp_server.end();
337 #endif //FTP_FEATURE
338 }
339 
341 {
342  if (_started) {
343 #ifdef MDNS_FEATURE
344 #if defined(ARDUINO_ARCH_ESP8266)
345  MDNS.update();
346 #endif //ARDUINO_ARCH_ESP8266
347 #endif //MDNS_FEATURE
348 #ifdef OTA_FEATURE
349  ArduinoOTA.handle();
350 #endif //OTA_FEATURE
351 #ifdef CAPTIVE_PORTAL_FEATURE
352  if (WiFi.getMode()== WIFI_AP ) {
353  dnsServer.processNextRequest();
354  }
355 #endif //CAPTIVE_PORTAL_FEATURE
356 #ifdef HTTP_FEATURE
358 #endif //HTTP_FEATURE
359 #ifdef WS_DATA_FEATURE
361 #endif //WS_DATA_FEATURE
362 #if defined(HTTP_FEATURE)
364 #endif //HTTP_FEATURE
365 #ifdef TELNET_FEATURE
367 #endif //TELNET_FEATURE
368 #ifdef FTP_FEATURE
369  ftp_server.handle();
370 #endif //FTP_FEATURE
371 #ifdef NOTIFICATION_FEATURE
373 #endif //NOTIFICATION_FEATURE
374  }
375  if (_restart) {
376  begin();
377  }
378 }
379 
NotificationsService::sendAutoNotification
bool sendAutoNotification(const char *msg)
Definition: notifications_service.cpp:98
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
FtpServer::handle
void handle()
Definition: FtpServer.cpp:219
NetServices::NetServices
NetServices()
Definition: netservices.cpp:77
NotificationsService::begin
bool begin()
Definition: notifications_service.cpp:389
notificationsservice
NotificationsService notificationsservice
Definition: notifications_service.cpp:67
FtpServer::datapassiveport
uint16_t datapassiveport()
Definition: FtpServer.h:86
FtpServer::started
bool started()
Definition: FtpServer.cpp:170
Camera::begin
bool begin(bool forceinit=false)
Telnet_Server::begin
bool begin(uint16_t port=0, bool debug=false)
Definition: telnet_server.cpp:93
NotificationsService::end
void end()
Definition: notifications_service.cpp:428
Telnet_Server::end
void end()
Definition: telnet_server.cpp:127
NotificationsService::handle
void handle()
Definition: notifications_service.cpp:442
Telnet_Server::port
uint16_t port()
Definition: telnet_server.h:69
HTTP_Server::begin
static bool begin()
Definition: http_server.cpp:155
NetServices::~NetServices
~NetServices()
Definition: netservices.cpp:80
websocket_terminal_server
WebSocket_Server websocket_terminal_server
Definition: websocket_server.cpp:33
netservices.h
websocket_data_server
WebSocket_Server websocket_data_server
Definition: websocket_server.cpp:35
WebSocket_Server::started
bool started()
Definition: websocket_server.h:74
DNS_PORT
const byte DNS_PORT
Definition: netservices.cpp:63
timeserver
TimeServer timeserver
Camera::end
void end()
HTTP_Server::end
static void end()
Definition: http_server.cpp:186
FtpServer::begin
bool begin()
Definition: FtpServer.cpp:175
Telnet_Server::started
bool started()
Definition: telnet_server.cpp:154
netconfig.h
WebSocket_Server::end
void end()
Definition: websocket_server.cpp:176
dnsServer
DNSServer dnsServer
Definition: netservices.cpp:64
FtpServer::dataactiveport
uint16_t dataactiveport()
Definition: FtpServer.h:90
telnet_server
Telnet_Server telnet_server
Definition: telnet_server.cpp:31
FtpServer::ctrlport
uint16_t ctrlport()
Definition: FtpServer.h:82
Hal::getChipID
static uint16_t getChipID()
Definition: hal.cpp:238
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
TimeServer::begin
bool begin()
TimeServer::is_internet_time
bool is_internet_time(bool readfromsettings=false)
ESP3DOutput::printERROR
size_t printERROR(const char *s, int code_error=200)
Definition: esp3doutput.cpp:247
ESP_WEBSOCKET_PORT
#define ESP_WEBSOCKET_PORT
Definition: settings_esp3d.h:64
ESP_FileSystem::end
static void end()
Settings_ESP3D::read_uint32
static uint32_t read_uint32(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:919
Telnet_Server::handle
void handle()
Definition: telnet_server.cpp:159
HTTP_Server::port
static uint16_t port()
Definition: http_server.h:57
NOTIFICATION_ESP_ONLINE
#define NOTIFICATION_ESP_ONLINE
Definition: configuration.h:261
HTTP_Server::started
static bool started()
Definition: http_server.h:53
WebSocket_Server::handle
void handle()
Definition: websocket_server.cpp:295
FtpServer::end
void end()
Definition: FtpServer.cpp:143
ESP_ALL_CLIENTS
#define ESP_ALL_CLIENTS
Definition: esp3doutput.h:30
WebSocket_Server::port
uint16_t port()
Definition: websocket_server.h:59
esp3d_camera
Camera esp3d_camera
HTTP_Server::handle
static void handle()
Definition: http_server.cpp:200
ftp_server
FtpServer ftp_server
Definition: FtpServer.cpp:106
NetServices::end
static void end()
Definition: netservices.cpp:284
ESP3DOutput
Definition: esp3doutput.h:48
TimeServer::current_time
const char * current_time(time_t t=0)
WebSocket_Server::begin
bool begin(uint16_t port=0, bool debug=false)
Definition: websocket_server.cpp:140
NetServices::handle
static void handle()
Definition: netservices.cpp:340