Skip to content

Commit

Permalink
status_led: Add to ntrip and sockets
Browse files Browse the repository at this point in the history
  • Loading branch information
nebkat committed Oct 30, 2019
1 parent 322ef88 commit 823afb2
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 23 deletions.
12 changes: 6 additions & 6 deletions main/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ const config_item_t CONFIG_ITEMS[] = {
}, {
.key = KEY_CONFIG_NTRIP_SERVER_COLOR,
.type = CONFIG_ITEM_TYPE_COLOR,
.def.color.rgba = 0x00000000
.def.color.rgba = 0x00000055u
}, {
.key = KEY_CONFIG_NTRIP_SERVER_HOST,
.type = CONFIG_ITEM_TYPE_STRING,
Expand Down Expand Up @@ -90,7 +90,7 @@ const config_item_t CONFIG_ITEMS[] = {
}, {
.key = KEY_CONFIG_NTRIP_CLIENT_COLOR,
.type = CONFIG_ITEM_TYPE_COLOR,
.def.color.rgba = 0x00000000
.def.color.rgba = 0x00000055u
}, {
.key = KEY_CONFIG_NTRIP_CLIENT_HOST,
.type = CONFIG_ITEM_TYPE_STRING,
Expand Down Expand Up @@ -121,7 +121,7 @@ const config_item_t CONFIG_ITEMS[] = {
}, {
.key = KEY_CONFIG_NTRIP_CASTER_COLOR,
.type = CONFIG_ITEM_TYPE_COLOR,
.def.color.rgba = 0x00000000
.def.color.rgba = 0x00000055u
}, {
.key = KEY_CONFIG_NTRIP_CASTER_PORT,
.type = CONFIG_ITEM_TYPE_UINT16,
Expand Down Expand Up @@ -149,7 +149,7 @@ const config_item_t CONFIG_ITEMS[] = {
}, {
.key = KEY_CONFIG_SOCKET_SERVER_COLOR,
.type = CONFIG_ITEM_TYPE_COLOR,
.def.color.rgba = 0x00000000
.def.color.rgba = 0x00000055u
}, {
.key = KEY_CONFIG_SOCKET_SERVER_TCP_PORT,
.type = CONFIG_ITEM_TYPE_UINT16,
Expand All @@ -167,7 +167,7 @@ const config_item_t CONFIG_ITEMS[] = {
}, {
.key = KEY_CONFIG_SOCKET_CLIENT_COLOR,
.type = CONFIG_ITEM_TYPE_COLOR,
.def.color.rgba = 0x00000000
.def.color.rgba = 0x00000055u
}, {
.key = KEY_CONFIG_SOCKET_CLIENT_HOST,
.type = CONFIG_ITEM_TYPE_STRING,
Expand Down Expand Up @@ -245,7 +245,7 @@ const config_item_t CONFIG_ITEMS[] = {
}, {
.key = KEY_CONFIG_WIFI_AP_COLOR,
.type = CONFIG_ITEM_TYPE_COLOR,
.def.color.rgba = 0x00000000
.def.color.rgba = 0x00000055u
}, {
.key = KEY_CONFIG_WIFI_AP_SSID,
.type = CONFIG_ITEM_TYPE_STRING,
Expand Down
10 changes: 10 additions & 0 deletions main/ntrip_caster.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <wifi.h>
#include <mdns.h>
#include <tasks.h>
#include <status_led.h>
#include "ntrip.h"
#include "config.h"
#include "util.h"
Expand All @@ -34,6 +35,8 @@ static const char *TAG = "NTRIP_CASTER";

static int sock = -1;

static status_led_handle_t status_led = NULL;

typedef struct ntrip_caster_client_t {
int socket;
SLIST_ENTRY(ntrip_caster_client_t) next;
Expand All @@ -53,6 +56,8 @@ static void ntrip_caster_client_remove(ntrip_caster_client_t *caster_client) {

SLIST_REMOVE(&caster_clients_list, caster_client, ntrip_caster_client_t, next);
free(caster_client);

if (status_led != NULL && SLIST_EMPTY(&caster_clients_list)) status_led->flashing_mode = STATUS_LED_STATIC;
}

static void ntrip_caster_uart_handler(void* handler_args, esp_event_base_t base, int32_t id, void* event_data) {
Expand Down Expand Up @@ -102,6 +107,9 @@ static int ntrip_caster_socket_init() {
static void ntrip_caster_task(void *ctx) {
uart_register_handler(ntrip_caster_uart_handler);

config_color_t status_led_color = config_get_color(CONF_ITEM(KEY_CONFIG_NTRIP_CASTER_COLOR));
if (status_led_color.rgba != 0) status_led = status_led_add(status_led_color.rgba, STATUS_LED_STATIC, 500, 2000, 0);

while (true) {
wait_for_ip();

Expand Down Expand Up @@ -212,6 +220,8 @@ static void ntrip_caster_task(void *ctx) {
// Socket will now be dealt with by ntrip_caster_uart_handler, set to -1 so it doesn't get destroyed
sock_client = -1;

if (status_led != NULL) status_led->flashing_mode = STATUS_LED_FADE;

char *addr_str = sockaddrtostr((struct sockaddr *) &source_addr);
uart_nmea("$PESP,NTRIP,CST,CLIENT,CONNECTED,%s", addr_str);
}
Expand Down
11 changes: 6 additions & 5 deletions main/ntrip_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ static void ntrip_client_uart_handler(void* handler_args, esp_event_base_t base,
static void ntrip_client_task(void *ctx) {
uart_register_handler(ntrip_client_uart_handler);

status_led = status_led_add(0x00FF0055, STATUS_LED_FADE, 500, 5000, 0);
status_led->active = false;
config_color_t status_led_color = config_get_color(CONF_ITEM(KEY_CONFIG_NTRIP_CLIENT_COLOR));
if (status_led_color.rgba != 0) status_led = status_led_add(status_led_color.rgba, STATUS_LED_FADE, 500, 2000, 0);
if (status_led != NULL) status_led->active = false;

while (true) {
wait_for_ip();
Expand Down Expand Up @@ -87,16 +88,16 @@ static void ntrip_client_task(void *ctx) {
ERROR_ACTION(TAG, status == NULL || !ntrip_response_ok(status), free(status); goto _error, "Could not connect to mountpoint: %s", status == NULL ? "HTTP response malformed" : status);
free(status);

status_led->active = true;

ESP_LOGI(TAG, "Successfully connected to %s:%d/%s", host, port, mountpoint);
uart_nmea("$PESP,NTRIP,CLI,CONNECTED,%s:%d,%s", host, port, mountpoint);

if (status_led != NULL) status_led->active = true;

while ((len = read(sock, buffer, BUFFER_SIZE)) >= 0) {
uart_write(buffer, len);
}

status_led->active = false;
if (status_led != NULL) status_led->active = false;

ESP_LOGW(TAG, "Disconnected from %s:%d/%s", host, port, mountpoint);
uart_nmea("$PESP,NTRIP,CLI,DISCONNECTED,%s:%d,%s", host, port, mountpoint);
Expand Down
11 changes: 11 additions & 0 deletions main/ntrip_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <sys/socket.h>
#include <wifi.h>
#include <tasks.h>
#include <status_led.h>
#include "ntrip.h"
#include "config.h"
#include "util.h"
Expand All @@ -33,6 +34,8 @@ static const char *TAG = "NTRIP_SERVER";
static int sock = -1;
static int server_keep_alive;

static status_led_handle_t status_led = NULL;

static void ntrip_server_uart_handler(void* handler_args, esp_event_base_t base, int32_t id, void* event_data) {
if (sock == -1) return;

Expand All @@ -46,6 +49,10 @@ static void ntrip_server_uart_handler(void* handler_args, esp_event_base_t base,
static void ntrip_server_task(void *ctx) {
uart_register_handler(ntrip_server_uart_handler);

config_color_t status_led_color = config_get_color(CONF_ITEM(KEY_CONFIG_NTRIP_SERVER_COLOR));
if (status_led_color.rgba != 0) status_led = status_led_add(status_led_color.rgba, STATUS_LED_FADE, 500, 2000, 0);
if (status_led != NULL) status_led->active = false;

while (true) {
wait_for_ip();

Expand Down Expand Up @@ -84,6 +91,8 @@ static void ntrip_server_task(void *ctx) {
ESP_LOGI(TAG, "Successfully connected to %s:%d/%s", host, port, mountpoint);
uart_nmea("$PESP,NTRIP,SRV,CONNECTED,%s:%d,%s", host, port, mountpoint);

if (status_led != NULL) status_led->active = true;

// Keep alive
server_keep_alive = NTRIP_KEEP_ALIVE_THRESHOLD;
while (true) {
Expand All @@ -98,6 +107,8 @@ static void ntrip_server_task(void *ctx) {
vTaskDelay(pdMS_TO_TICKS(NTRIP_KEEP_ALIVE_THRESHOLD / 10));
}

if (status_led != NULL) status_led->active = false;

ESP_LOGW(TAG, "Disconnected from %s:%d/%s", host, port, mountpoint);
uart_nmea("$PESP,NTRIP,SRV,DISCONNECTED,%s:%d,%s", host, port, mountpoint);

Expand Down
12 changes: 7 additions & 5 deletions main/socket_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ static const char *TAG = "SOCKET_CLIENT";
#define BUFFER_SIZE 1024

static int sock = -1;

static status_led_handle_t status_led = NULL;

static void socket_client_uart_handler(void* handler_args, esp_event_base_t base, int32_t id, void* event_data) {
Expand All @@ -46,8 +47,9 @@ static void socket_client_uart_handler(void* handler_args, esp_event_base_t base
static void socket_client_task(void *ctx) {
uart_register_handler(socket_client_uart_handler);

status_led = status_led_add(0x00FF0055, STATUS_LED_FADE, 500, 5000, 0);
status_led->active = false;
config_color_t status_led_color = config_get_color(CONF_ITEM(KEY_CONFIG_SOCKET_CLIENT_COLOR));
if (status_led_color.rgba != 0) status_led = status_led_add(status_led_color.rgba, STATUS_LED_FADE, 500, 2000, 0);
if (status_led != NULL) status_led->active = false;

while (true) {
wait_for_ip();
Expand All @@ -67,11 +69,11 @@ static void socket_client_task(void *ctx) {
free(connect_message);
ERROR_ACTION(TAG, err < 0, goto _error, "Could not send connection message: %d %s", errno, strerror(errno));

status_led->active = true;

ESP_LOGI(TAG, "Successfully connected to %s:%d", host, port);
uart_nmea("$PESP,SOCK,CLI,CONNECTED,%s:%d", host, port);

if (status_led != NULL) status_led->active = true;

char *buffer = malloc(BUFFER_SIZE);

int len;
Expand All @@ -81,7 +83,7 @@ static void socket_client_task(void *ctx) {

free(buffer);

status_led->active = false;
if (status_led != NULL) status_led->active = false;

ESP_LOGW(TAG, "Disconnected from %s:%d: %d %s", host, port, errno, strerror(errno));
uart_nmea("$PESP,SOCK,CLI,DISCONNECTED,%s:%d", host, port);
Expand Down
18 changes: 14 additions & 4 deletions main/socket_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "lwip/err.h"
#include "lwip/sockets.h"
#include <wifi.h>
#include <status_led.h>

#include "socket_server.h"
#include "config.h"
Expand All @@ -44,6 +45,11 @@ static const char *TAG = "SOCKET_SERVER";
#define SOCKET_IP_PROTOCOL IPPROTO_IP6
#endif

int sock_tcp, sock_udp;
char *buffer;

static status_led_handle_t status_led = NULL;

typedef struct socket_client_t {
int socket;
struct sockaddr_in6 addr;
Expand All @@ -53,10 +59,6 @@ typedef struct socket_client_t {

SLIST_HEAD(socket_client_list_t, socket_client_t) socket_client_list;

int sock_tcp, sock_udp;

char *buffer;

static bool socket_address_equal(struct sockaddr_in6 *a, struct sockaddr_in6 *b) {
if (a->sin6_family != b->sin6_family) return false;

Expand Down Expand Up @@ -84,6 +86,8 @@ static socket_client_t * socket_client_add(int sock, struct sockaddr_in6 addr, i
ESP_LOGI(TAG, "Accepted %s client %s", SOCKTYPE_NAME(socktype), addr_str);
uart_nmea("$PESP,SOCK,SRV,%s,CONNECTED,%s", SOCKTYPE_NAME(socktype), addr_str);

if (status_led != NULL) status_led->flashing_mode = STATUS_LED_FADE;

return client;
}

Expand All @@ -96,6 +100,8 @@ static void socket_client_remove(socket_client_t *socket_client) {

SLIST_REMOVE(&socket_client_list, socket_client, socket_client_t, next);
free(socket_client);

if (status_led != NULL && SLIST_EMPTY(&socket_client_list)) status_led->flashing_mode = STATUS_LED_STATIC;
}

static void socket_server_uart_handler(void* handler_args, esp_event_base_t base, int32_t id, void* event_data) {
Expand Down Expand Up @@ -244,6 +250,10 @@ static void socket_clients_receive(fd_set *socket_set) {
static void socket_server_task(void *ctx) {
uart_register_handler(socket_server_uart_handler);

config_color_t status_led_color = config_get_color(CONF_ITEM(KEY_CONFIG_SOCKET_CLIENT_COLOR));
if (status_led_color.rgba != 0) status_led = status_led_add(status_led_color.rgba, STATUS_LED_FADE, 500, 2000, 0);
if (status_led != NULL) status_led->active = false;

while (true) {
wait_for_ip();

Expand Down
6 changes: 3 additions & 3 deletions main/wifi.c
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ static void handle_ap_sta_disconnected(void *arg, esp_event_base_t base, int32_t
wifi_sta_list_t ap_sta_list;
esp_wifi_ap_get_sta_list(&ap_sta_list);

if (status_led_ap != NULL) status_led_ap->flashing_mode = ap_sta_list.num > 0 ? STATUS_LED_FADE : STATUS_LED_STATIC;
if (status_led_ap != NULL && ap_sta_list.num == 0) status_led_ap->flashing_mode = STATUS_LED_STATIC;
}

static void handle_sta_got_ip(void *arg, esp_event_base_t base, int32_t event_id, void *event_data) {
Expand Down Expand Up @@ -332,7 +332,7 @@ void wifi_init() {
ap_password_len == 0 ? 'O' : 'P');

config_color_t ap_led_color = config_get_color(CONF_ITEM(KEY_CONFIG_WIFI_AP_COLOR));
if (ap_led_color.rgba != 0) status_led_ap = status_led_add(ap_led_color.rgba, STATUS_LED_STATIC, 250, 1000, 0);
if (ap_led_color.rgba != 0) status_led_ap = status_led_add(ap_led_color.rgba, STATUS_LED_STATIC, 500, 2000, 0);

ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &config_ap));
}
Expand All @@ -342,7 +342,7 @@ void wifi_init() {
uart_nmea("$PESP,WIFI,STA,CONNECTING,%s,%c", config_sta.sta.ssid, sta_password_len == 0 ? 'O' : 'P');

config_color_t sta_led_color = config_get_color(CONF_ITEM(KEY_CONFIG_WIFI_STA_COLOR));
if (sta_led_color.rgba != 0) status_led_sta = status_led_add(sta_led_color.rgba, STATUS_LED_STATIC, 250, 1000, 0);
if (sta_led_color.rgba != 0) status_led_sta = status_led_add(sta_led_color.rgba, STATUS_LED_STATIC, 500, 2000, 0);

ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &config_sta));

Expand Down

0 comments on commit 823afb2

Please sign in to comment.