Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variable TCP Telnet ports via web interface #205

Open
wants to merge 64 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
563957f
Update home.html
fuzzball03 Oct 27, 2016
143eb24
Update ui.js
fuzzball03 Oct 27, 2016
eb7fde7
Update cgipins.c
fuzzball03 Oct 27, 2016
5cb0350
Update config.h
fuzzball03 Oct 27, 2016
fb1bbd3
Update config.h
fuzzball03 Oct 27, 2016
93ca100
Update config.c
fuzzball03 Oct 27, 2016
02197a9
Create cgitelnet.h
fuzzball03 Oct 27, 2016
8919ff6
Update cgitelnet.h
fuzzball03 Oct 27, 2016
f315385
Create cgitelnet.c
fuzzball03 Oct 27, 2016
59edf88
Update serbridge.c
fuzzball03 Oct 27, 2016
6054496
Update serbridge.c
fuzzball03 Oct 27, 2016
a230f42
Update main.c
fuzzball03 Oct 27, 2016
4bddea7
Update main.c
fuzzball03 Oct 27, 2016
04bbccb
Update main.c
fuzzball03 Oct 27, 2016
936951d
Update main.c
fuzzball03 Oct 27, 2016
0009d8f
Update cgitelnet.c
fuzzball03 Oct 27, 2016
bead65f
Update cgitelnet.c
fuzzball03 Oct 27, 2016
c3a3b06
Update serbridge.c
fuzzball03 Oct 27, 2016
67a05de
Update cgitelnet.c
fuzzball03 Oct 27, 2016
1cf190a
Update cgitelnet.c
fuzzball03 Oct 27, 2016
6fb97bd
Update config.h
fuzzball03 Oct 27, 2016
a01db32
Update cgitelnet.c
fuzzball03 Oct 27, 2016
157569e
Cgitelnet changes/fixes
fuzzball03 Oct 28, 2016
fe95b4b
Cleanup
fuzzball03 Oct 28, 2016
ffd13c6
Ajax/Html changes
fuzzball03 Oct 28, 2016
3cb3804
Cgitelnet update
fuzzball03 Oct 28, 2016
a9f583e
Update cgitelnet.c
fuzzball03 Oct 28, 2016
7a93821
Update cgitelnet.c
fuzzball03 Oct 28, 2016
00d8b9f
Update cgitelnet.c
fuzzball03 Oct 28, 2016
973e4fb
Update .travis.yml
fuzzball03 Oct 28, 2016
b79a6d6
update for s3
fuzzball03 Oct 28, 2016
d887feb
Update .travis.yml
fuzzball03 Oct 28, 2016
e25d896
Update .travis.yml
fuzzball03 Oct 28, 2016
30658c5
Update .travis.yml
fuzzball03 Oct 28, 2016
8cd915f
Cgitelnet reform json output
fuzzball03 Oct 28, 2016
1da3ce7
Update cgitelnet.c
fuzzball03 Oct 28, 2016
6a730e7
Update cgitelnet.c
fuzzball03 Oct 28, 2016
7d60520
Update home.html
fuzzball03 Oct 28, 2016
338f983
Update readme to reflect changes
fuzzball03 Oct 31, 2016
3fdfe93
Various changes
fuzzball03 Oct 31, 2016
3f16121
Fix a few errors..
fuzzball03 Oct 31, 2016
2458e11
Serialbridge.c update
fuzzball03 Oct 31, 2016
27e6b5d
Fix compile errors
dannybackx Nov 1, 2016
36f15c9
Fix compile errors
dannybackx Nov 1, 2016
59a4707
Fix compile errors
dannybackx Nov 1, 2016
654f2ef
Fix compile errors
dannybackx Nov 1, 2016
4b3293e
Change comment
dannybackx Nov 1, 2016
547540d
Fix the build problem : don't call sprintf.
dannybackx Nov 1, 2016
589158e
Aha
dannybackx Nov 1, 2016
f3060e6
Change call to 0/1 instead of 1/2
dannybackx Nov 1, 2016
286e886
Makefile/Build file changes
fuzzball03 Nov 3, 2016
6596883
Formatting cleanup
fuzzball03 Nov 3, 2016
0b8368a
Modify all calls to port variables to match. Espconn1 = TelnetPort1
fuzzball03 Nov 3, 2016
b8404a1
Fix error for sucessful Travis build
fuzzball03 Nov 3, 2016
bda82de
Start making changes for Danny's security settings
fuzzball03 Nov 4, 2016
623f7b4
fix error
fuzzball03 Nov 4, 2016
5e346a3
Fix errors
fuzzball03 Nov 4, 2016
b5243b0
HTML Changes.
fuzzball03 Nov 4, 2016
6fd5938
Update .travis.yml
fuzzball03 Nov 16, 2016
25feca5
Update .travis.yml
fuzzball03 Nov 23, 2016
23a904f
5
fuzzball03 Nov 23, 2016
1df972b
9
fuzzball03 Nov 23, 2016
153f470
10
fuzzball03 Nov 23, 2016
9fcee90
12
fuzzball03 Nov 23, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion esp-link/cgipins.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

#include <esp8266.h>
#include "cgi.h"
#include "espfs.h"
Expand Down
79 changes: 79 additions & 0 deletions esp-link/cgitelnet.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#include <esp8266.h>
#include "cgi.h"
#include "config.h"
#include "serbridge.h"

// Cgi to return choice of Telnet ports
int ICACHE_FLASH_ATTR cgiTelnetGet(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted

char buff[1024];
int len;

len = os_sprintf(buff,
"{ \"telnet-port1\":%d, \"telnet-port2\":%d }",
flashConfig.telnet_port1, flashConfig.telnet_port2);

jsonHeader(connData, 200);
httpdSend(connData, buff, len);
return HTTPD_CGI_DONE;
}

// Cgi to change choice of Telnet ports
int ICACHE_FLASH_ATTR cgiTelnetSet(HttpdConnData *connData) {
if (connData->conn==NULL) {
return HTTPD_CGI_DONE; // Connection aborted
}

int8_t ok = 0;
uint8_t port1, port2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ports are 16 bits: 0..65535

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course! I got the thought of a short int = 8bit stuck in my head yesterday.
Once I realized that... unsigned 16bit should get me there.

ok |= getUInt16Arg(connData, "port1", &port1);
ok |= getUInt16Arg(connData, "port2", &port2);
if (ok < 0) return HTTPD_CGI_DONE;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't just return here, you have to send some HTTP response back. A 304 not modified or a 200 OK would be reasonable choices.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill modify it to include the responses.


char *coll;
if (ok > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I heaven't dealt with the ajax stuff in a while and forgotten everything (oops), but I think you're gonna get one port at a time when you fill out one input field and hit enter.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're right. Ill modify to have it check the flashconfig variables.

// check whether ports are different
if (port1 == port2) { coll = "Ports cannot be the same!"; goto collision; }

// we're good, set flashconfig
flashConfig.telnet_port1 = port1;
flashConfig.telnet_port2 = port2;
os_printf("Ports changed: port1=%d port2=%d\n",
port1, port2);

// apply the changes
//serbridgeReinit();
serbridgeInit(flashConfig.telnet_port1, flashConfig.telnet_port2);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please apply change after saving otherwise you might apply the changes, fail to save and return an error, but then things are inconsistent.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point


// save to flash
if (configSave()) {
httpdStartResponse(connData, 204);
httpdEndHeaders(connData);
} else {
httpdStartResponse(connData, 500);
httpdEndHeaders(connData);
httpdSend(connData, "Failed to save config", -1);
}
}
return HTTPD_CGI_DONE;

collision: {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put this inline since there is only one "goto" to here.

char buff[128];
os_sprintf(buff, "Ports assignment for %s collides with another assignment", coll);
errorResponse(connData, 400, buff);
return HTTPD_CGI_DONE;
}
}

int ICACHE_FLASH_ATTR cgiTelnet(HttpdConnData *connData) {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
if (connData->requestType == HTTPD_METHOD_GET) {
return cgiTelnetGet(connData);
} else if (connData->requestType == HTTPD_METHOD_POST) {
return cgiTelnetSet(connData);
} else {
jsonHeader(connData, 404);
return HTTPD_CGI_DONE;
}
}
9 changes: 9 additions & 0 deletions esp-link/cgitelnet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef CGITELNET_H
#define CGITELNET_H

#include "httpd.h"

int cgiTelnet(HttpdConnData *connData);
// int8_t telnet_port1, telnet_port2;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can delete this comment...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do.


#endif // CGITELNET_H
2 changes: 2 additions & 0 deletions esp-link/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ FlashConfig flashDefault = {
.data_bits = EIGHT_BITS,
.parity = NONE_BITS,
.stop_bits = ONE_STOP_BIT,
.telnet_port1 = 23,
.telnet_port2 = 2323,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something that seems to be missing is to take care of the upgrade path. When someone upgrades the telnet_portN fields in flash will be 0, so something needs to detect that when loading from flash and set the defaults explicitly.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ill take a look to see how that was done in some other code and proceed appropriately.

Copy link
Author

@fuzzball03 fuzzball03 Oct 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tve So I looked into this a bit - as the code stands now, if I erase the flash... Then flash my current build, the default ports of 23 & 2323 are set(as they are set in this file). How is it accomplishing this?

As far as I can tell, the configRestore() function is where checks for "0" values are checked.
Why not simply use a loop to check all the flashConfig variables for "0" or NULL and fill them with their flashDefault values if true? (Just trying to learn here - I'm sure you or someone else has a good reason here)

};

typedef union {
Expand Down
4 changes: 3 additions & 1 deletion esp-link/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ typedef struct {
char hostname[32]; // if using DHCP
uint32_t staticip, netmask, gateway; // using DHCP if staticip==0
uint8_t log_mode; // UART log debug mode
uint8_t swap_uart; // swap uart0 to gpio 13&15
uint8_t swap_uart; // swap uart0 to gpio 13&15
uint8_t tcp_enable, rssi_enable; // TCP client settings
char api_key[48]; // RSSI submission API key (Grovestreams for now)
uint8_t slip_enable, mqtt_enable, // SLIP protocol, MQTT client
Expand Down Expand Up @@ -41,6 +41,8 @@ typedef struct {
int8_t data_bits;
int8_t parity;
int8_t stop_bits;
uint16_t telnet_port1, // Telnet port settings
telnet_port2;
} FlashConfig;
extern FlashConfig flashConfig;

Expand Down
5 changes: 4 additions & 1 deletion esp-link/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "gpio.h"
#include "cgiservices.h"
#include "web-server.h"
#include "cgitelnet.h"

#ifdef SYSLOG
#include "syslog.h"
Expand Down Expand Up @@ -96,6 +97,8 @@ HttpdBuiltInUrl builtInUrls[] = {
{ "/services/info", cgiServicesInfo, NULL },
{ "/services/update", cgiServicesSet, NULL },
{ "/pins", cgiPins, NULL },
{ "/telnet/info", cgiTelnet, NULL},
{ "/telnet/update", cgiTelnet, NULL},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need one since you distinguish using GET and POST. Not sure why there are two for services, something for me to check.

Copy link
Author

@fuzzball03 fuzzball03 Oct 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, I picked up on this. However I wanted to attempt to avoid someone making an easy mistake of sending a PUT request via '/telnet' & accidently modifying it.
However, ill be happy to merge it to just one.
Also, ill have to modify the Ajax method for making inputs since it makes a call to /update

Services uses two because this & pins cgi use a single method to call back set or get. They determine which to use based on the header received(or GET or PUT)

#ifdef MQTT
{ "/mqtt", cgiMqtt, NULL },
#endif
Expand Down Expand Up @@ -179,7 +182,7 @@ user_init(void) {
WEB_Init();

// init the wifi-serial transparent bridge (port 23)
serbridgeInit(23, 2323);
serbridgeInit(flashConfig.telnet_port1, flashConfig.telnet_port1);
uart_add_recv_cb(&serbridgeUartCb);
#ifdef SHOW_HEAP_USE
os_timer_disarm(&prHeapTimer);
Expand Down
25 changes: 25 additions & 0 deletions html/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ <h1>Info</h1>
<a href="https://github.com/jeelabs/esp-link/blob/master/README.md">the online README</a>
for up-to-date help.</p>
</div>
<div class="card">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like the indent is off...

<h1>Telnet Serial-Bridge</h1>
<p style="margin-bottom:0;">There are two ports available for telnet to use by default: 23 &nbsp; 2323</p>
<div id="telnet-spinner" class="spinner spinner-small"></div>
<table id="telnet-table" class="pure-table pure-table-horizontal" hidden><tbody>
<tr><td>Current Telnet ports</td><td class="telnet-info"></td></tr>
<tr><td colspan=2 class="popup-target">Telnet port 1:<br>
<div class="click-to-edit telnet-port1">
<span class="edit-off" style="display:block; width:auto;"></span>
<textarea class="edit-on" rows=1 maxlength=5 hidden> </textarea>
<div class="popup">Click to edit!<br>Default port: 23</div>
</div>
</td></tr>
<tr><td colspan=2 class="popup-target">Telnet port 2:<br>
<div class="click-to-edit telnet-port2">
<span class="edit-off" style="display:block; width:auto;"></span>
<textarea class="edit-on" rows=1 maxlength=5 hidden> </textarea>
<div class="popup">Click to edit!<br>Default port: 2323</div>
</div>
</td></tr>
</tbody></table>
</div>
</div>
<!-- RIGHT COLUMN -->
<div class="pure-u-1 pure-u-md-1-2">
Expand Down Expand Up @@ -152,9 +174,12 @@ <h1>System details</h1>
onLoad(function() {
makeAjaxInput("system", "description");
makeAjaxInput("system", "name");
makeAjaxInput("telnet", "port1");
makeAjaxInput("telnet", "port2");
fetchPins();
getWifiInfo();
getSystemInfo();
getTelnetInfo();
bnd($("#pinform"), "submit", setPins);
});
</script>
Expand Down
16 changes: 16 additions & 0 deletions html/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,22 @@ function getWifiInfo() {
function(s, st) { window.setTimeout(getWifiInfo, 1000); });
}

//===== Telnet info

function showTelnetInfo(data) {
Object.keys(data).forEach(function(v) {
setEditToClick("telnet-"+v, data[v]);
});
$("#telnet-spinner").setAttribute("hidden", "");
$("#telnet-table").removeAttribute("hidden");
currAp = data.ssid;
}

function getTelnetInfo() {
ajaxJson('GET', "/telnet/info", showTelnetInfo,
function(s, st) { window.setTimeout(getTelnetInfo, 1000); });
}

//===== System info

function showSystemInfo(data) {
Expand Down
2 changes: 2 additions & 0 deletions serial/serbridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,8 @@ serbridgeInitPins()
}

// Start transparent serial bridge TCP server on specified port (typ. 23)
// Here is where we need to change the ports. But how do we do this from the Ajax call?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, old comment. Left it for me to find while working on this.

void ICACHE_FLASH_ATTR
serbridgeInit(int port1, int port2)
{
Expand Down