ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
ethconfig.cpp
Go to the documentation of this file.
1 /*
2  ethconfig.cpp - ethernet 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 (ETH_FEATURE)
23 #ifdef ARDUINO_ARCH_ESP32
24 #include "esp_eth.h"
25 #include "dhcpserver/dhcpserver_options.h"
26 #endif //ARDUINO_ARCH_ESP32
27 #ifdef ARDUINO_ARCH_ESP8266
28 #endif //ARDUINO_ARCH_ESP8266
29 #include "../../core/esp3doutput.h"
30 #include "../../core/settings_esp3d.h"
31 #include "../network/netconfig.h"
32 #include "ethconfig.h"
33 bool EthConfig::_started = false;
34 const uint8_t DEFAULT_AP_MASK_VALUE[] = {255, 255, 255, 0};
36 {
37 }
38 
40 {
41  end();
42 }
43 
45 {
46  bool res = true;
51  IPAddress ip(IP), mask(MK), gateway(GW);
52  res = ETH.config(ip, gateway,mask);
53  }
54  return res;
55 }
56 /*bool EthConfig::StartSRV()
57 {
58  bool res = true;
59  //static IP
60  int32_t IP = Settings_ESP3D::read_IP(ESP_AP_IP_VALUE);
61  IPAddress ip(IP), mask(DEFAULT_AP_MASK_VALUE), gateway(IP);
62  if (!ETH.config(ip, gateway,mask)) {
63  res = false;
64  log_esp3d("Set static IP error");
65  }
66  //start DHCP server
67  if(res) {
68  dhcps_lease_t lease;
69  lease.enable = true;
70  lease.start_ip.addr = static_cast<uint32_t>(IP) + (1 << 24);
71  lease.end_ip.addr = static_cast<uint32_t>(IP) + (11 << 24);
72  tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_ETH);
73  tcpip_adapter_dhcps_option(
74  (tcpip_adapter_option_mode_t)TCPIP_ADAPTER_OP_SET,
75  (tcpip_adapter_option_id_t)REQUESTED_IP_ADDRESS,
76  (void*)&lease, sizeof(dhcps_lease_t)
77  );
78 
79  if (tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_ETH) != ESP_OK){
80  res = false;
81  log_esp3d("Start DHCP server failed");
82  }
83  }
84  return res;
85 }*/
86 
90 bool EthConfig::begin()
91 {
92  bool res = false;
94  end();
95  _started = ETH.begin();
96  if (_started) {
97  output.printMSG("Starting Ethernet");
98  res=true;
99  } else {
100  output.printERROR("Failed Starting Ethernet");
101  }
102  if (!ETH.setHostname(NetConfig::hostname(true))) {
103  res = false;
104  log_esp3d("Set Hostname error");
105  }
106 
107  int8_t espMode =Settings_ESP3D::read_byte(ESP_RADIO_MODE);
108  //DHCP is only for Client
109  if (espMode == ESP_ETH_STA) {
110  if(!StartSTA()) {
111  res = false;
112  output.printMSG ("Failed Starting Client");
113  } else {
114  output.printMSG ("Client started");
115  }
116 
117  } else {
118  //if(!StartSRV()){
119  // res = false;
120  // output.printMSG ("Failed Starting Server");
121  //} else {
122  // output.printMSG ("Server started");
123  //}
124  }
125 
126  //if ((Settings_ESP3D::read_byte(ESP_STA_IP_MODE) != DHCP_MODE) || (espMode == ESP_ETH_SRV)){
128  //as no event to display static IP
129  output.printMSG (ETH.localIP().toString().c_str());
130  }
131 
132  //Let wait cable is connected
133  uint32_t start = millis();
134  String stmp ="Checking connection";
135  output.printMSG (stmp.c_str());
136  while (!ETH.linkUp() && ((millis()-start) < 10000)) {
137  Hal::wait(1000);
138  stmp +=".";
139  output.printMSG (stmp.c_str());
140  ESP3DGlobalOutput::SetStatus(stmp.c_str());
141  }
142  if (!ETH.linkUp()) {
143  output.printMSG ("Cable disconnected");
144  ESP3DGlobalOutput::SetStatus("Cable disconnected");
145  }
146  return res;
147 }
148 
153 void EthConfig::end()
154 {
155  esp_eth_disable();
156  _started = false;
157 }
158 
159 bool EthConfig::started()
160 {
161  return _started;
162 }
167 void EthConfig::handle()
168 {
169 }
170 
171 
172 #endif // ETH_FEATURE
173 
DHCP_MODE
#define DHCP_MODE
Definition: netconfig.h:36
Hal::wait
static void wait(uint32_t milliseconds)
Definition: hal.cpp:226
EthConfig::handle
static void handle()
EthConfig::StartSTA
static bool StartSTA()
ESP_STA_IP_MODE
#define ESP_STA_IP_MODE
Definition: settings_esp3d.h:42
ethconfig.h
EthConfig::~EthConfig
~EthConfig()
NetConfig::hostname
static const char * hostname(bool fromsettings=false)
Definition: netconfig.cpp:331
EthConfig::started
static bool started()
DEFAULT_AP_MASK_VALUE
const uint8_t DEFAULT_AP_MASK_VALUE[]
Definition: wificonfig.cpp:33
ESP_STA_IP_VALUE
#define ESP_STA_IP_VALUE
Definition: settings_esp3d.h:43
EthConfig::end
static void end()
Settings_ESP3D::read_IP
static uint32_t read_IP(int pos, bool *haserror=NULL)
Definition: settings_esp3d.cpp:958
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
ESP_ETH_STA
#define ESP_ETH_STA
Definition: netconfig.h:44
ESP_STA_GATEWAY_VALUE
#define ESP_STA_GATEWAY_VALUE
Definition: settings_esp3d.h:45
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
ESP_STA_MASK_VALUE
#define ESP_STA_MASK_VALUE
Definition: settings_esp3d.h:44
EthConfig::begin
static bool begin()
ESP3DOutput
Definition: esp3doutput.h:48
ESP_RADIO_MODE
#define ESP_RADIO_MODE
Definition: settings_esp3d.h:39
ESP3DGlobalOutput::SetStatus
static void SetStatus(const char *status)
Definition: esp3doutput.cpp:428
EthConfig::EthConfig
EthConfig()