put serial/tcp bridge in a class

This commit is contained in:
luc lebosse 2016-09-08 15:15:56 +02:00
parent 36e0ab5f03
commit af17fff2e0
5 changed files with 154 additions and 63 deletions

88
esp3d/bridge.cpp Normal file
View File

@ -0,0 +1,88 @@
/*
bridge.cpp - esp3d bridge serial/tcp class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "bridge.h"
#include "command.h"
#include "webinterface.h"
#ifdef TCP_IP_DATA_FEATURE
WiFiServer * data_server;
WiFiClient serverClients[MAX_SRV_CLIENTS];
#endif
bool BRIDGE::processFromSerial2TCP(){
uint8_t i;
//check UART for data
if(Serial.available()) {
size_t len = Serial.available();
uint8_t sbuf[len];
Serial.readBytes(sbuf, len);
#ifdef TCP_IP_DATA_FEATURE
//push UART data to all connected tcp clients
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write(sbuf, len);
delay(1);
}
}
#endif
//process data if any
COMMAND::read_buffer_serial(sbuf, len);
return true;
}
else return false;
}
#ifdef TCP_IP_DATA_FEATURE
void BRIDGE::processFromTCP2Serial(){
uint8_t i,data;
//check if there are any new clients
if (data_server->hasClient()) {
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
//find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected()) {
if(serverClients[i]) {
serverClients[i].stop();
}
serverClients[i] = data_server->available();
continue;
}
}
//no free/disconnected spot so reject
WiFiClient serverClient = data_server->available();
serverClient.stop();
}
//check clients for data
//to avoid any pollution if Uploading file to SDCard
if ((web_interface->blockserial) == false){
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
if(serverClients[i].available()) {
//get data from the tcp client and push it to the UART
while(serverClients[i].available()) {
data = serverClients[i].read();
Serial.write(data);
COMMAND::read_buffer_tcp(data);
}
}
}
}
}
}
#endif

38
esp3d/bridge.h Normal file
View File

@ -0,0 +1,38 @@
/*
Copyright (c) 2014 Luc Lebosse. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "config.h"
#ifndef BRIDGE_H
#define BRIDGE_H
#ifdef TCP_IP_DATA_FEATURE
extern WiFiServer * data_server;
#endif
class BRIDGE
{
public:
static bool processFromSerial2TCP();
#ifdef TCP_IP_DATA_FEATURE
static void processFromTCP2Serial();
#endif
};
#endif

View File

@ -1,5 +1,5 @@
/*
CONFIG.H - esp8266 configuration class
config.h - ESP3D configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
@ -28,6 +28,9 @@
//FIRMWARE_TARGET: the targeted FW, can be REPETIER (Original Repetier)/ REPETIER4DV (Repetier for Davinci) / MARLIN (Marlin)/ SMOOTHIEWARE (Smoothieware)
#define FIRMWARE_TARGET SMOOTHIEWARE
//number of clients allowed to use data port at once
#define MAX_SRV_CLIENTS 1
//comment to disable
//MDNS_FEATURE: this feature allow type the name defined
//in web browser by default: http:\\esp8266.local and connect
@ -85,7 +88,9 @@
//#define DEBUG_ESP3D
//#define DEBUG_OUTPUT_SPIFFS
//#define DEBUG_OUTPUT_SD
#define DEBUG_OUTPUT_SERIAL
//#define DEBUG_OUTPUT_SERIAL
#include <FS.h>
#ifdef DEBUG_ESP3D
#ifdef DEBUG_OUTPUT_SPIFFS
@ -115,6 +120,11 @@
#define FSINFO FSInfo
#endif
#ifndef TCP_IP_DATA_FEATURE
#undef MAX_SRV_CLIENTS
#define MAX_SRV_CLIENTS 0
#endif
#ifndef CONFIG_h
#define CONFIG_h

View File

@ -1,12 +1,12 @@
/*
This file is part of ESP8266 Firmware for 3D printer.
This file is part of ESP3D Firmware for 3D printer.
ESP8266 Firmware for 3D printer is free software: you can redistribute it and/or modify
ESP3D Firmware for 3D printer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ESP8266 Firmware for 3D printer is distributed in the hope that it will be useful,
ESP3D Firmware for 3D printer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
@ -30,6 +30,7 @@
#include <EEPROM.h>
#include "config.h"
#include "wifi.h"
#include "bridge.h"
#include "webinterface.h"
#include "command.h"
#include <ESP8266WiFi.h>
@ -49,16 +50,15 @@ DNSServer dnsServer;
#ifdef NETBIOS_FEATURE
#include <ESP8266NetBIOS.h>
#endif
#define MAX_SRV_CLIENTS 1
WiFiServer * data_server;
WiFiClient serverClients[MAX_SRV_CLIENTS];
void setup()
{
bool breset_config=false;
long baud_rate=0;
web_interface = NULL;
#ifdef TCP_IP_DATA_FEATURE
data_server = NULL;
#endif
// init:
#ifdef DEBUG_ESP3D
Serial.begin(DEFAULT_BAUD_RATE);
@ -197,60 +197,10 @@ void loop()
#endif
//web requests
web_interface->WebServer.handleClient();
//TODO use a method to handle serial also in class and call it instead of this one
uint8_t i,data;
#ifdef TCP_IP_DATA_FEATURE
//check if there are any new clients
if (data_server->hasClient()) {
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
//find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected()) {
if(serverClients[i]) {
serverClients[i].stop();
}
serverClients[i] = data_server->available();
continue;
}
}
//no free/disconnected spot so reject
WiFiClient serverClient = data_server->available();
serverClient.stop();
}
//check clients for data
//to avoid any pollution if Uploading file to SDCard
if ((web_interface->blockserial) == false){
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
if(serverClients[i].available()) {
//get data from the tcp client and push it to the UART
while(serverClients[i].available()) {
data = serverClients[i].read();
Serial.write(data);
COMMAND::read_buffer_tcp(data);
}
}
}
}
}
BRIDGE::processFromTCP2Serial();
#endif
//check UART for data
if(Serial.available()) {
size_t len = Serial.available();
uint8_t sbuf[len];
Serial.readBytes(sbuf, len);
#ifdef TCP_IP_DATA_FEATURE
//push UART data to all connected tcp clients
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i] && serverClients[i].connected()) {
serverClients[i].write(sbuf, len);
delay(1);
}
}
#endif
//process data if any
COMMAND::read_buffer_serial(sbuf, len);
}
BRIDGE::processFromSerial2TCP();
if (web_interface->restartmodule) {
CONFIG::esp_restart();
}

View File

@ -1,5 +1,5 @@
/*
webinterface.cpp - esp8266 configuration class
webinterface.cpp - ESP3D configuration class
Copyright (c) 2014 Luc Lebosse. All rights reserved.
@ -30,6 +30,7 @@
#include "LinkedList.h"
#include "storestrings.h"
#include "command.h"
#include "bridge.h"
#ifdef SSDP_FEATURE
#include <ESP8266SSDP.h>
@ -3088,6 +3089,7 @@ void handle_not_found()
if (page_not_found )
{
LOG("Page not found it \n")
if (SPIFFS.exists("/404.tpl")) {
STORESTRINGS_CLASS KeysList ;
STORESTRINGS_CLASS ValuesList ;
@ -3536,7 +3538,10 @@ String WEBINTERFACE_CLASS::getContentType(String filename)
return "image/png";
} else if(filename.endsWith(".gif")) {
return "image/gif";
} else if(filename.endsWith(".jpg")) {
} else if(filename.endsWith(".jpeg")) {
return "image/jpeg";
}
else if(filename.endsWith(".jpg")) {
return "image/jpeg";
} else if(filename.endsWith(".ico")) {
return "image/x-icon";