Skip to content

Commit

Permalink
Added HW reset
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Ger committed Mar 18, 2018
1 parent 1c4e331 commit c083d58
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 26 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ If you are not using automesh, you can now reload the page and change the "Soft

If you like, you can mark the "lock" checkbox and click "Lock". Now the config cannot be changed anymore without first unlocking it with the uplink WiFi network's password (define one even if the network is open).

If you did a mistake and you lost any contact with ESP you can still use the serial console to recover it ("reset facory", see below).
If you want to enter non-ASCII or special characters in the web interface you have to use HTTP-style hex encoding like "My%20AccessPoint". This will result in a string "My AccessPoint". With this hex encoding you can enter any byte value you like, except for 0 (for C-internal reasons).

If you did a mistake and you lost any contact with the ESP you can still use the serial console to recover it ("reset facory", see below).

# Command Line Interface
Advanced configuration has to be done via the command line on the console interface. This console is available either via the serial port at 115200 baud or via tcp port 7777 (e.g. "telnet 192.168.4.1 7777" from a connected STA).
Expand All @@ -56,7 +58,7 @@ Use the following commands for an initial setup:
- save
- reset

If you want to enter non-ASCII or special characters on the command line you can use quoting: either use C-style quotes with backslash like this "My\ AccessPoint" or use HTTP-style hex encoding like "My%20AccessPoint". Both methods will result in a string "My AccessPoint". With the hex encoding you can enter any byte value you like, except for 0 (for C-internal reasons).
Again, if you want to enter non-ASCII or special characters you can use HTTP-style hex encoding (e.g. "My%20AccessPoint") or, only on the CLI, as shortcut C-style quotes with backslash (e.g. "My\ AccessPoint"). Both methods will result in a string "My AccessPoint".

The command line understands a lot more commands:

Expand Down Expand Up @@ -144,6 +146,9 @@ In default config GPIO2 is configured to drive a status LED (connected to GND) w

With "set status_led GPIOno" the GPIO pin can be changed (any value > 16, e.g. "set status_led 255" will disable the status LED completely). When configured to GPIO1, it works with the buildin blue LED on the ESP-01 boards. However, as GPIO1 ist also the UART-TX-pin this means, that the serial console is not working. Configuration is then limited to network access.

# HW reset
If you pull low GPIO 0 for more than 2 seconds, the repeater will do a factory reset and restarts with default config.

# Port Mapping
In order to allow clients from the external network to connect to server port on the internal network, ports have to be mapped. An external port is mapped to an internal port of a specific internal IP address. Use the "portmap add" command for that. Port mappings can be listed with the "show" command and are saved with the current config.

Expand Down
2 changes: 2 additions & 0 deletions driver/new_uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,8 @@ int UART_Send(uint8 uart_no, char *buffer, int len)
ch = *(buffer+index);
uart_tx_one_char(uart_no, ch);
}

return index;
}

/*---------------------------------------------------------------------------*
Expand Down
Binary file modified firmware/0x00000.bin
Binary file not shown.
Binary file modified firmware/0x10000.bin
Binary file not shown.
4 changes: 2 additions & 2 deletions firmware/sha1sums
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
fd905b6e4a6a3a3805502d267c654a56725217b8 0x00000.bin
20d51455fc4c50825310f63ba7249e86745b0482 0x10000.bin
2afacd52bae0132ccd8bd34829c90b37632befec 0x00000.bin
ff3bc6339dfb81c3c188816ef021c1981ac5cca6 0x10000.bin
4 changes: 2 additions & 2 deletions user/acl.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ uint8_t allow;
switch (proto) {
case IP_PROTO_UDP:
if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr)+sizeof(struct udp_hdr))
return;
return ACL_DENY;
udp_h = (struct udp_hdr *)&packet[sizeof(struct eth_hdr)+sizeof(struct ip_hdr)];
src_port = ntohs(udp_h->src);
dest_port = ntohs(udp_h->dest);
break;

case IP_PROTO_TCP:
if (p->len < sizeof(struct eth_hdr)+sizeof(struct ip_hdr)+sizeof(struct tcp_hdr))
return;
return ACL_DENY;
tcp_h = (struct tcp_hdr *)&packet[sizeof(struct eth_hdr)+sizeof(struct ip_hdr)];
src_port = ntohs(tcp_h->src);
dest_port = ntohs(tcp_h->dest);
Expand Down
12 changes: 6 additions & 6 deletions user/config_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* time at least. When you want to change some data in flash, you have to
* erase the whole sector, and then write it back with the new data.
*--------------------------------------------------------------------------*/
void config_load_default(sysconfig_p config)
void ICACHE_FLASH_ATTR config_load_default(sysconfig_p config)
{
uint8_t mac[6];

Expand Down Expand Up @@ -101,7 +101,7 @@ uint8_t mac[6];
#endif
}

int config_load(sysconfig_p config)
int ICACHE_FLASH_ATTR config_load(sysconfig_p config)
{
if (config == NULL) return -1;
uint16_t base_address = FLASH_BLOCK_NO;
Expand Down Expand Up @@ -136,7 +136,7 @@ int config_load(sysconfig_p config)
return 0;
}

void config_save(sysconfig_p config)
void ICACHE_FLASH_ATTR config_save(sysconfig_p config)
{
uint16_t base_address = FLASH_BLOCK_NO;
config->no_routes = ip_route_max;
Expand All @@ -151,20 +151,20 @@ void config_save(sysconfig_p config)
spi_flash_write(base_address * SPI_FLASH_SEC_SIZE, (uint32 *)config, sizeof(sysconfig_t));
}

void blob_save(uint8_t blob_no, uint32_t *data, uint16_t len)
void ICACHE_FLASH_ATTR blob_save(uint8_t blob_no, uint32_t *data, uint16_t len)
{
uint16_t base_address = FLASH_BLOCK_NO + 1 + blob_no;
spi_flash_erase_sector(base_address);
spi_flash_write(base_address * SPI_FLASH_SEC_SIZE, data, len);
}

void blob_load(uint8_t blob_no, uint32_t *data, uint16_t len)
void ICACHE_FLASH_ATTR blob_load(uint8_t blob_no, uint32_t *data, uint16_t len)
{
uint16_t base_address = FLASH_BLOCK_NO + 1 + blob_no;
spi_flash_read(base_address * SPI_FLASH_SEC_SIZE, data, len);
}

void blob_zero(uint8_t blob_no, uint16_t len)
void ICACHE_FLASH_ATTR blob_zero(uint8_t blob_no, uint16_t len)
{
int i;
uint8_t z[len];
Expand Down
1 change: 1 addition & 0 deletions user/config_flash.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ typedef struct
} sysconfig_t, *sysconfig_p;

int config_load(sysconfig_p config);
void config_load_default(sysconfig_p config);
void config_save(sysconfig_p config);

void blob_save(uint8_t blob_no, uint32_t *data, uint16_t len);
Expand Down
7 changes: 6 additions & 1 deletion user/user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,19 @@
//
// Size of the console buffers
//
#define MAX_CON_SEND_SIZE 1024
#define MAX_CON_SEND_SIZE 1200
#define MAX_CON_CMD_SIZE 80

//
// Define this if you have a status LED connected to a GPIO pin
//
#define STATUS_LED_GIPO 2

//
// Define this if you want to have a HW factory reset when this GPIO is pulled low
//
#define FACTORY_RESET_PIN 0

//
// Define this to support the "scan" command for AP search
//
Expand Down
46 changes: 33 additions & 13 deletions user/user_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ err_t ICACHE_FLASH_ATTR my_input_ap (struct pbuf *p, struct netif *inp) {
if (put_packet_to_ringbuf(p) != 0) {
#ifdef DROP_PACKET_IF_NOT_RECORDED
pbuf_free(p);
return;
return ERR_OK;
#endif
}
if (!monitoring_send_ongoing)
Expand All @@ -447,7 +447,7 @@ err_t ICACHE_FLASH_ATTR my_input_ap (struct pbuf *p, struct netif *inp) {
// If not allowed, drop packet
if (!(acl_check&ACL_ALLOW)) {
pbuf_free(p);
return;
return ERR_OK;
};
#endif

Expand All @@ -457,15 +457,15 @@ err_t ICACHE_FLASH_ATTR my_input_ap (struct pbuf *p, struct netif *inp) {
token_bucket_us -= p->tot_len;
} else {
pbuf_free(p);
return;
return ERR_OK;
}
}
#endif

Bytes_in += p->tot_len;
Packets_in++;

orig_input_ap (p, inp);
return orig_input_ap (p, inp);
}

err_t ICACHE_FLASH_ATTR my_output_ap (struct netif *outp, struct pbuf *p) {
Expand All @@ -487,7 +487,7 @@ err_t ICACHE_FLASH_ATTR my_output_ap (struct netif *outp, struct pbuf *p) {
if (put_packet_to_ringbuf(p) != 0) {
#ifdef DROP_PACKET_IF_NOT_RECORDED
pbuf_free(p);
return;
return ERR_OK;
#endif
}
if (!monitoring_send_ongoing)
Expand All @@ -508,7 +508,7 @@ err_t ICACHE_FLASH_ATTR my_output_ap (struct netif *outp, struct pbuf *p) {
// If not allowed, drop packet
if (!(acl_check&ACL_ALLOW)) {
pbuf_free(p);
return;
return ERR_OK;
};
#endif

Expand All @@ -518,38 +518,37 @@ err_t ICACHE_FLASH_ATTR my_output_ap (struct netif *outp, struct pbuf *p) {
token_bucket_ds -= p->tot_len;
} else {
pbuf_free(p);
return;
return ERR_OK;
}
}
#endif

Bytes_out += p->tot_len;
Packets_out++;

orig_output_ap (outp, p);
return orig_output_ap (outp, p);
}


err_t ICACHE_FLASH_ATTR my_input_sta (struct pbuf *p, struct netif *inp) {

ap_watchdog_cnt = config.ap_watchdog;
#ifdef ACLS
if (!acl_is_empty(2) && !(acl_check_packet(2, p) & ACL_ALLOW)) {
pbuf_free(p);
return;
return ERR_OK;
};
#endif
orig_input_sta (p, inp);
return orig_input_sta (p, inp);
}

err_t ICACHE_FLASH_ATTR my_output_sta (struct netif *outp, struct pbuf *p) {
#ifdef ACLS
if (!acl_is_empty(3) && !(acl_check_packet(3, p) & ACL_ALLOW)) {
pbuf_free(p);
return;
return ERR_OK;
};
#endif
orig_output_sta (outp, p);
return orig_output_sta (outp, p);
}

static void ICACHE_FLASH_ATTR patch_netif(ip_addr_t netif_ip, netif_input_fn ifn, netif_input_fn *orig_ifn, netif_linkoutput_fn ofn, netif_linkoutput_fn *orig_ofn, bool nat)
Expand Down Expand Up @@ -2428,6 +2427,23 @@ uint32_t Bps;
}
}

#ifdef FACTORY_RESET_PIN
static count_pin;
bool pin_in = easygpio_inputGet(FACTORY_RESET_PIN);
if (!pin_in) {
count_pin++;
if (count_pin > 4) {
os_printf("Factory reset pressed\r\n");
config_load_default(&config);
config_save(&config);
blob_zero(0, sizeof(struct portmap_table) * IP_PORTMAP_MAX);
system_restart();
}
} else {
count_pin = 0;
}
#endif

if (config.status_led <= 16)
easygpio_outputSet (config.status_led, toggle && connected);

Expand Down Expand Up @@ -3026,6 +3042,10 @@ struct ip_info info;
easygpio_outputSet (config.status_led, 0);
}

#ifdef FACTORY_RESET_PIN
easygpio_pinMode(FACTORY_RESET_PIN, EASYGPIO_PULLUP, EASYGPIO_INPUT);
#endif

#ifdef MQTT_CLIENT
#ifdef USER_GPIO_IN
easygpio_pinMode(USER_GPIO_IN, EASYGPIO_PULLUP, EASYGPIO_INPUT);
Expand Down

0 comments on commit c083d58

Please sign in to comment.