mirror of https://github.com/jeelabs/esp-link.git
Merge branch 'master' of https://github.com/jeelabs/esp-link
Added services page and moved some things aroundpull/72/head
commit
13bc8019c6
@ -0,0 +1,205 @@ |
||||
#include <esp8266.h> |
||||
#include "cgiwifi.h" |
||||
#include "cgi.h" |
||||
#include "config.h" |
||||
#include "syslog.h" |
||||
#include "sntp.h" |
||||
#include "cgimqtt.h" |
||||
|
||||
#ifdef CGISERVICES_DBG |
||||
#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) |
||||
#else |
||||
#define DBG(format, ...) do { } while(0) |
||||
#endif |
||||
|
||||
char* rst_codes[7] = { |
||||
"normal", "wdt reset", "exception", "soft wdt", "restart", "deep sleep", "external", |
||||
}; |
||||
|
||||
char* flash_maps[7] = { |
||||
"512KB:256/256", "256KB", "1MB:512/512", "2MB:512/512", "4MB:512/512", |
||||
"2MB:1024/1024", "4MB:1024/1024" |
||||
}; |
||||
|
||||
static ETSTimer reassTimer; |
||||
|
||||
// Cgi to update system info (name/description)
|
||||
int ICACHE_FLASH_ATTR cgiSystemSet(HttpdConnData *connData) { |
||||
if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||||
|
||||
int8_t n = getStringArg(connData, "name", flashConfig.hostname, sizeof(flashConfig.hostname)); |
||||
int8_t d = getStringArg(connData, "description", flashConfig.sys_descr, sizeof(flashConfig.sys_descr)); |
||||
|
||||
if (n < 0 || d < 0) return HTTPD_CGI_DONE; // getStringArg has produced an error response
|
||||
|
||||
if (n > 0) { |
||||
// schedule hostname change-over
|
||||
os_timer_disarm(&reassTimer); |
||||
os_timer_setfn(&reassTimer, configWifiIP, NULL); |
||||
os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it
|
||||
} |
||||
|
||||
if (configSave()) { |
||||
httpdStartResponse(connData, 204); |
||||
httpdEndHeaders(connData); |
||||
} |
||||
else { |
||||
httpdStartResponse(connData, 500); |
||||
httpdEndHeaders(connData); |
||||
httpdSend(connData, "Failed to save config", -1); |
||||
} |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
|
||||
// Cgi to return various System information
|
||||
int ICACHE_FLASH_ATTR cgiSystemInfo(HttpdConnData *connData) { |
||||
char buff[1024]; |
||||
|
||||
if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||||
|
||||
uint8 part_id = system_upgrade_userbin_check(); |
||||
uint32_t fid = spi_flash_get_id(); |
||||
struct rst_info *rst_info = system_get_rst_info(); |
||||
|
||||
os_sprintf(buff, |
||||
"{ " |
||||
"\"name\": \"%s\", " |
||||
"\"reset cause\": \"%d=%s\", " |
||||
"\"size\": \"%s\", " |
||||
"\"id\": \"0x%02lX 0x%04lX\", " |
||||
"\"partition\": \"%s\", " |
||||
"\"slip\": \"%s\", " |
||||
"\"mqtt\": \"%s/%s\", " |
||||
"\"baud\": \"%ld\", " |
||||
"\"description\": \"%s\"" |
||||
" }", |
||||
flashConfig.hostname, |
||||
rst_info->reason, |
||||
rst_codes[rst_info->reason], |
||||
flash_maps[system_get_flash_size_map()], |
||||
fid & 0xff, (fid & 0xff00) | ((fid >> 16) & 0xff), |
||||
part_id ? "user2.bin" : "user1.bin", |
||||
flashConfig.slip_enable ? "enabled" : "disabled", |
||||
flashConfig.mqtt_enable ? "enabled" : "disabled", |
||||
mqttState(), |
||||
flashConfig.baud_rate, |
||||
flashConfig.sys_descr |
||||
); |
||||
|
||||
jsonHeader(connData, 200); |
||||
httpdSend(connData, buff, -1); |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
|
||||
static void ICACHE_FLASH_ATTR cgiServicesSNTPInit() { |
||||
if (flashConfig.sntp_server[0]) { |
||||
DBG("SNTP timesource set to %s with offset %d\n", flashConfig.sntp_server, flashConfig.timezone_offset); |
||||
sntp_stop(); |
||||
sntp_setservername(0, flashConfig.sntp_server); |
||||
sntp_set_timezone(flashConfig.timezone_offset); |
||||
sntp_init(); |
||||
} |
||||
} |
||||
|
||||
int ICACHE_FLASH_ATTR cgiServicesInfo(HttpdConnData *connData) { |
||||
char buff[1024]; |
||||
|
||||
if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||||
|
||||
os_sprintf(buff,
|
||||
"{ "
|
||||
"\"syslog_enable\": \"%s\", " |
||||
"\"syslog_host\": \"%s\", " |
||||
"\"syslog_minheap\": %d, " |
||||
"\"syslog_filter\": %d, " |
||||
"\"syslog_showtick\": \"%s\", " |
||||
"\"syslog_showdate\": \"%s\", " |
||||
"\"timezone_offset\": %d, " |
||||
"\"sntp_server\": \"%s\", " |
||||
"\"mdns_enable\": \"%s\", " |
||||
"\"mdns_servername\": \"%s\"" |
||||
" }",
|
||||
flashConfig.syslog_enable ? "enabled" : "disabled", |
||||
flashConfig.syslog_host, |
||||
flashConfig.syslog_minheap, |
||||
flashConfig.syslog_filter, |
||||
flashConfig.syslog_showtick ? "enabled" : "disabled", |
||||
flashConfig.syslog_showdate ? "enabled" : "disabled", |
||||
flashConfig.timezone_offset, |
||||
flashConfig.sntp_server, |
||||
flashConfig.mdns_enable ? "enabled" : "disabled", |
||||
flashConfig.mdns_servername |
||||
); |
||||
|
||||
jsonHeader(connData, 200); |
||||
httpdSend(connData, buff, -1); |
||||
return HTTPD_CGI_DONE; |
||||
} |
||||
|
||||
int ICACHE_FLASH_ATTR cgiServicesSet(HttpdConnData *connData) { |
||||
if (connData->conn == NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up.
|
||||
|
||||
int8_t syslog = 0; |
||||
|
||||
syslog |= getBoolArg(connData, "syslog_enable", &flashConfig.syslog_enable); |
||||
if (syslog < 0) return HTTPD_CGI_DONE; |
||||
syslog |= getStringArg(connData, "syslog_host", flashConfig.syslog_host, sizeof(flashConfig.syslog_host)); |
||||
if (syslog < 0) return HTTPD_CGI_DONE; |
||||
syslog |= getUInt16Arg(connData, "syslog_minheap", &flashConfig.syslog_minheap); |
||||
if (syslog < 0) return HTTPD_CGI_DONE; |
||||
syslog |= getUInt8Arg(connData, "syslog_filter", &flashConfig.syslog_filter); |
||||
if (syslog < 0) return HTTPD_CGI_DONE; |
||||
syslog |= getBoolArg(connData, "syslog_showtick", &flashConfig.syslog_showtick); |
||||
if (syslog < 0) return HTTPD_CGI_DONE; |
||||
syslog |= getBoolArg(connData, "syslog_showdate", &flashConfig.syslog_showdate); |
||||
if (syslog < 0) return HTTPD_CGI_DONE; |
||||
|
||||
int8_t sntp = 0; |
||||
sntp |= getInt8Arg(connData, "timezone_offset", &flashConfig.timezone_offset); |
||||
if (sntp < 0) return HTTPD_CGI_DONE; |
||||
sntp |= getStringArg(connData, "sntp_server", flashConfig.sntp_server, sizeof(flashConfig.sntp_server)); |
||||
if (sntp < 0) return HTTPD_CGI_DONE; |
||||
|
||||
int8_t mdns = 0; |
||||
mdns |= getBoolArg(connData, "mdns_enable", &flashConfig.mdns_enable); |
||||
if (mdns < 0) return HTTPD_CGI_DONE; |
||||
mdns |= getStringArg(connData, "mdns_servername", flashConfig.mdns_servername, sizeof(flashConfig.mdns_servername)); |
||||
if (mdns < 0) return HTTPD_CGI_DONE; |
||||
|
||||
if (syslog > 0) { |
||||
if (!flashConfig.syslog_enable) { |
||||
flashConfig.syslog_host[0] = '\0'; |
||||
} |
||||
syslog_init(flashConfig.syslog_host); |
||||
} |
||||
|
||||
if (sntp > 0) { |
||||
cgiServicesSNTPInit(); |
||||
} |
||||
|
||||
if (mdns > 0) { |
||||
espconn_mdns_disable(); |
||||
if (flashConfig.mdns_enable) { |
||||
struct ip_info ipconfig; |
||||
wifi_get_ip_info(STATION_IF, &ipconfig); |
||||
mdns_started = false; |
||||
if (wifiState == wifiGotIP && ipconfig.ip.addr != 0) { |
||||
wifiStartMDNS(ipconfig.ip); |
||||
} |
||||
} |
||||
else { |
||||
mdns_started = true; |
||||
}
|
||||
} |
||||
|
||||
if (configSave()) { |
||||
httpdStartResponse(connData, 204); |
||||
httpdEndHeaders(connData); |
||||
} |
||||
else { |
||||
httpdStartResponse(connData, 500); |
||||
httpdEndHeaders(connData); |
||||
httpdSend(connData, "Failed to save config", -1); |
||||
} |
||||
return HTTPD_CGI_DONE; |
||||
} |
@ -0,0 +1,15 @@ |
||||
#ifndef CGISERVICES_H |
||||
#define CGISERVICES_H |
||||
|
||||
#include "httpd.h" |
||||
|
||||
int cgiSystemSet(HttpdConnData *connData); |
||||
int cgiSystemInfo(HttpdConnData *connData); |
||||
|
||||
int cgiServicesInfo(HttpdConnData *connData); |
||||
int cgiServicesSet(HttpdConnData *connData); |
||||
|
||||
extern char* rst_codes[7]; |
||||
extern char* flash_maps[7]; |
||||
|
||||
#endif // CGISERVICES_H
|
@ -0,0 +1,78 @@ |
||||
/*
|
||||
* task.c |
||||
* |
||||
* Copyright 2015 Susi's Strolch |
||||
* |
||||
* For license information see projects "License.txt" |
||||
* |
||||
* Not sure if it's save to use ICACHE_FLASH_ATTR, so we're running from RAM |
||||
*/ |
||||
|
||||
#undef USRTASK_DBG |
||||
|
||||
#include "esp8266.h" |
||||
#include <task.h> |
||||
|
||||
#define MAXUSRTASKS 8 |
||||
|
||||
#ifdef USRTASK_DBG |
||||
#define DBG_USRTASK(format, ...) os_printf(format, ## __VA_ARGS__) |
||||
#else |
||||
#define DBG_USRTASK(format, ...) do { } while(0) |
||||
#endif |
||||
|
||||
LOCAL os_event_t *_task_queue = NULL; // system_os_task queue
|
||||
LOCAL os_task_t *usr_task_queue = NULL; // user task queue
|
||||
|
||||
// it seems save to run the usr_event_handler from RAM, so no ICACHE_FLASH_ATTR here...
|
||||
|
||||
LOCAL void usr_event_handler(os_event_t *e) |
||||
{ |
||||
DBG_USRTASK("usr_event_handler: event %p (sig=%d, par=%p)\n", e, (int)e->sig, (void *)e->par); |
||||
if (usr_task_queue[e->sig] == NULL || e->sig < 0 || e->sig >= MAXUSRTASKS) { |
||||
os_printf("usr_event_handler: task %d %s\n", (int)e->sig, |
||||
usr_task_queue[e->sig] == NULL ? "not registered" : "out of range"); |
||||
return; |
||||
} |
||||
(usr_task_queue[e->sig])(e); |
||||
} |
||||
|
||||
LOCAL void init_usr_task() { |
||||
if (_task_queue == NULL) |
||||
_task_queue = (os_event_t *)os_zalloc(sizeof(os_event_t) * _task_queueLen); |
||||
|
||||
if (usr_task_queue == NULL) |
||||
usr_task_queue = (os_task_t *)os_zalloc(sizeof(os_task_t) * MAXUSRTASKS); |
||||
|
||||
system_os_task(usr_event_handler, _taskPrio, _task_queue, _task_queueLen); |
||||
} |
||||
|
||||
// public functions
|
||||
bool post_usr_task(uint8_t task, os_param_t par) |
||||
{ |
||||
return system_os_post(_taskPrio, task, par); |
||||
} |
||||
|
||||
uint8_t register_usr_task (os_task_t event) |
||||
{ |
||||
int task; |
||||
|
||||
DBG_USRTASK("register_usr_task: %p\n", event); |
||||
if (_task_queue == NULL || usr_task_queue == NULL) |
||||
init_usr_task(); |
||||
|
||||
for (task = 0; task < MAXUSRTASKS; task++) { |
||||
if (usr_task_queue[task] == event) |
||||
return task; // task already registered - bail out...
|
||||
} |
||||
|
||||
for (task = 0; task < MAXUSRTASKS; task++) { |
||||
if (usr_task_queue[task] == NULL) { |
||||
DBG_USRTASK("register_usr_task: assign task #%d\n", task); |
||||
usr_task_queue[task] = event; |
||||
break; |
||||
} |
||||
} |
||||
return task; |
||||
} |
||||
|
@ -0,0 +1,20 @@ |
||||
/*
|
||||
* task.h |
||||
* |
||||
* Copyright 2015 Susi's Strolch |
||||
* |
||||
* For license information see projects "License.txt" |
||||
* |
||||
* |
||||
*/ |
||||
|
||||
#ifndef USRTASK_H |
||||
#define USRTASK_H |
||||
|
||||
#define _taskPrio 1 |
||||
#define _task_queueLen 64 |
||||
|
||||
uint8_t register_usr_task (os_task_t event); |
||||
bool post_usr_task(uint8_t task, os_param_t par); |
||||
|
||||
#endif |
Before Width: | Height: | Size: 498 B After Width: | Height: | Size: 874 B |
@ -0,0 +1,111 @@ |
||||
<div id="main"> |
||||
<div class="header"> |
||||
<h1>Services</h1> |
||||
</div> |
||||
|
||||
<div class="content"> |
||||
<div class="pure-g"> |
||||
<div class="pure-u-1 pure-u-md-1-2"> |
||||
<div class="card"> |
||||
<h1> |
||||
Syslog |
||||
<div id="syslog-spinner" class="spinner spinner-small"></div> |
||||
</h1> |
||||
<form action="#" id="Syslog-form" class="pure-form" hidden> |
||||
<div class="form-horizontal"> |
||||
<label>Enable syslog</label> |
||||
<input type="checkbox" name="syslog_enable"/> |
||||
</div> |
||||
<br> |
||||
<legend>Syslog settings</legend> |
||||
<div class="pure-form-stacked"> |
||||
<label>Syslog Host</label> |
||||
<input type="text" name="syslog_host" /> |
||||
<div class="popup">Use server [hostname:port] as UDP Syslog server</div> |
||||
</div> |
||||
<div class="pure-form-stacked"> |
||||
<label>Min Heap</label> |
||||
<input type="text" name="syslog_minheap" /> |
||||
<label>Filter</label> |
||||
<select name="syslog_filter" href="#"> |
||||
<option value="0">EMERG</option> |
||||
<option value="1">ALERT</option> |
||||
<option value="2">CRIT</option> |
||||
<option value="3">ERR</option> |
||||
<option value="4">WARNING</option> |
||||
<option value="5">NOTICE</option> |
||||
<option value="6">INFO</option> |
||||
<option value="7">DEBUG</option> |
||||
</select> |
||||
</div> |
||||
<div class="form-horizontal"> |
||||
<label>Show ESP µC Ticker in log message</label> |
||||
<input type="checkbox" name="syslog_showtick" /> |
||||
</div> |
||||
<div class="form-horizontal"> |
||||
<label>Show date in log message</label> |
||||
<input type="checkbox" name="syslog_showdate" /> |
||||
<div class="popup">Synology does a log rotate if timestamp is in the past so disable to prevent this</div> |
||||
</div> |
||||
<button id="Syslog-button" type="submit" class="pure-button button-primary"> |
||||
Update syslog settings! |
||||
</button> |
||||
</form> |
||||
</div> |
||||
<div class="card"> |
||||
<h1> |
||||
mDNS |
||||
<div id="mdns-spinner" class="spinner spinner-small"></div> |
||||
</h1> |
||||
<form action="#" id="mDNS-form" class="pure-form" hidden> |
||||
<div class="form-horizontal"> |
||||
<input type="checkbox" name="mdns_enable"/> |
||||
<label>Enable mDNS</label> |
||||
</div> |
||||
<br> |
||||
<legend>mDNS server settings</legend> |
||||
<div class="pure-form-stacked"> |
||||
<label>Server Name</label> |
||||
<input type="text" name="mdns_servername"/> |
||||
</div> |
||||
<button id="mDNS-button" type="submit" class="pure-button button-primary"> |
||||
Update mDNS settings! |
||||
</button> |
||||
</form> |
||||
</div> |
||||
</div> |
||||
<div class="pure-u-1 pure-u-md-1-2"> |
||||
<div class="card"> |
||||
<h1> |
||||
SNTP |
||||
<div id="sntp-spinner" class="spinner spinner-small"></div> |
||||
</h1> |
||||
<form action="#" id="SNTP-form" class="pure-form" hidden> |
||||
<legend>SNTP server settings</legend> |
||||
<div class="pure-form-stacked"> |
||||
<label>Timezone Offset</label> |
||||
<input type="text" name="timezone_offset" /> |
||||
<label>SNTP Server</label> |
||||
<input type="text" name="sntp_server" /> |
||||
</div> |
||||
<button id="SNTP-button" type="submit" class="pure-button button-primary"> |
||||
Update SNTP settings! |
||||
</button> |
||||
</form> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
</div> |
||||
|
||||
<script src="services.js"></script> |
||||
<script type="text/javascript"> |
||||
onLoad(function() { |
||||
fetchServices(); |
||||
bnd($("#Syslog-form"), "submit", changeServices); |
||||
bnd($("#SNTP-form"), "submit", changeServices); |
||||
bnd($("#mDNS-form"), "submit", changeServices); |
||||
}); |
||||
</script> |
||||
</body></html> |
@ -0,0 +1,74 @@ |
||||
function changeServices(e) { |
||||
e.preventDefault(); |
||||
var url = "services/update?1=1"; |
||||
var i, inputs = document.querySelectorAll('#' + e.srcElement.id + ' input'); |
||||
for (i = 0; i < inputs.length; i++) { |
||||
if (inputs[i].type == "checkbox") { |
||||
var val = (inputs[i].checked) ? 1 : 0; |
||||
url += "&" + inputs[i].name + "=" + val; |
||||
} |
||||
else |
||||
url += "&" + inputs[i].name + "=" + inputs[i].value; |
||||
}; |
||||
|
||||
hideWarning(); |
||||
var n = e.srcElement.id.replace("-form", ""); |
||||
var cb = $("#" + n + "-button"); |
||||
addClass(cb, 'pure-button-disabled'); |
||||
ajaxSpin("POST", url, function (resp) { |
||||
showNotification(n + " updated"); |
||||
removeClass(cb, 'pure-button-disabled'); |
||||
}, function (s, st) { |
||||
showWarning("Error: " + st); |
||||
removeClass(cb, 'pure-button-disabled'); |
||||
window.setTimeout(fetchServices, 100); |
||||
}); |
||||
} |
||||
|
||||
function displayServices(data) { |
||||
Object.keys(data).forEach(function (v) { |
||||
el = $("#" + v); |
||||
if (el != null) { |
||||
if (el.nodeName === "INPUT") el.value = data[v]; |
||||
else el.innerHTML = data[v]; |
||||
return; |
||||
} |
||||
el = document.querySelector('input[name="' + v + '"]'); |
||||
if (el != null) { |
||||
if (el.type == "checkbox") { |
||||
el.checked = data[v] == "enabled"; |
||||
} else el.value = data[v]; |
||||
} |
||||
}); |
||||
|
||||
$("#syslog-spinner").setAttribute("hidden", ""); |
||||
$("#sntp-spinner").setAttribute("hidden", ""); |
||||
$("#mdns-spinner").setAttribute("hidden", ""); |
||||
|
||||
$("#Syslog-form").removeAttribute("hidden"); |
||||
$("#SNTP-form").removeAttribute("hidden"); |
||||
$("#mDNS-form").removeAttribute("hidden"); |
||||
|
||||
var i, inputs = $("input"); |
||||
for (i = 0; i < inputs.length; i++) { |
||||
if (inputs[i].type == "checkbox" && (inputs[i].name == "syslog_enable" || inputs[i].name == "mdns_enable")) { } |
||||
inputs[i].onclick = function () { setService(this.name, this.checked) }; |
||||
} |
||||
} |
||||
|
||||
function setService(name, v) { |
||||
ajaxSpin("POST", "/services/update?" + name + "=" + (v ? 1 : 0), function () { |
||||
var n = name.replace("_enable", ""); |
||||
n = (n == "syslog") ? "Syslog" : "mDNS"; |
||||
showNotification(n + " is now " + (v ? "enabled" : "disabled")); |
||||
}, function () { |
||||
showWarning("Enable/disable failed"); |
||||
window.setTimeout(fetchServices, 100); |
||||
}); |
||||
} |
||||
|
||||
function fetchServices() { |
||||
ajaxJson("GET", "/services/info", displayServices, function () { |
||||
window.setTimeout(fetchServices, 1000); |
||||
}); |
||||
} |
@ -0,0 +1,382 @@ |
||||
/*
|
||||
* syslog.c |
||||
* |
||||
* |
||||
* Copyright 2015 Susi's Strolch |
||||
* |
||||
* For license information see projects "License.txt" |
||||
* |
||||
*/ |
||||
|
||||
#include <esp8266.h> |
||||
#include "config.h" |
||||
#include "syslog.h" |
||||
#include "time.h" |
||||
#include "task.h" |
||||
|
||||
extern void * mem_trim(void *m, size_t s); // not well documented...
|
||||
|
||||
#ifdef SYSLOG_DBG |
||||
#define DBG(format, ...) do { os_printf(format, ## __VA_ARGS__); } while(0) |
||||
#else |
||||
#define DBG(format, ...) do { } while(0) |
||||
#endif |
||||
|
||||
#define WIFI_CHK_INTERVAL 1000 // ms to check Wifi statis
|
||||
static os_timer_t wifi_chk_timer; |
||||
|
||||
static struct espconn syslog_espconn; |
||||
static uint32_t syslog_msgid = 1; |
||||
static uint8_t syslog_task = 0; |
||||
|
||||
static syslog_host_t syslogHost; |
||||
static syslog_entry_t *syslogQueue = NULL; |
||||
|
||||
static enum syslog_state syslogState = SYSLOG_NONE; |
||||
|
||||
static void ICACHE_FLASH_ATTR syslog_add_entry(syslog_entry_t *entry); |
||||
static void ICACHE_FLASH_ATTR syslog_chk_wifi_stat(void); |
||||
static void ICACHE_FLASH_ATTR syslog_udp_sent_cb(void *arg); |
||||
#ifdef SYSLOG_UDP_RECV |
||||
static void ICACHE_FLASH_ATTR syslog_udp_recv_cb(void *arg, char *pusrdata, unsigned short length); |
||||
#endif |
||||
|
||||
#define syslog_send_udp() post_usr_task(syslog_task,0) |
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : syslog_chk_wifi_stat |
||||
* Description : check whether get ip addr or not |
||||
* Parameters : none |
||||
* Returns : none |
||||
*******************************************************************************/ |
||||
static void ICACHE_FLASH_ATTR syslog_chk_wifi_stat(void) |
||||
{ |
||||
struct ip_info ipconfig; |
||||
DBG("syslog_chk_wifi_stat: state: %d ", syslogState); |
||||
|
||||
//disarm timer first
|
||||
os_timer_disarm(&wifi_chk_timer); |
||||
|
||||
//try to get ip info of ESP8266 station
|
||||
wifi_get_ip_info(STATION_IF, &ipconfig); |
||||
int wifi_status = wifi_station_get_connect_status(); |
||||
if (wifi_status == STATION_GOT_IP && ipconfig.ip.addr != 0) |
||||
{ |
||||
if (syslogState == SYSLOG_WAIT) { // waiting for initialization
|
||||
DBG("connected, initializing UDP socket\n"); |
||||
syslog_init(flashConfig.syslog_host); |
||||
} |
||||
} else { |
||||
if ((wifi_status == STATION_WRONG_PASSWORD || |
||||
wifi_status == STATION_NO_AP_FOUND || |
||||
wifi_status == STATION_CONNECT_FAIL)) { |
||||
syslogState = SYSLOG_ERROR; |
||||
os_printf("*** connect failure!!!\n"); |
||||
} else { |
||||
DBG("re-arming timer...\n"); |
||||
os_timer_setfn(&wifi_chk_timer, (os_timer_func_t *)syslog_chk_wifi_stat, NULL); |
||||
os_timer_arm(&wifi_chk_timer, WIFI_CHK_INTERVAL, 0); |
||||
} |
||||
} |
||||
} |
||||
|
||||
static void ICACHE_FLASH_ATTR syslog_udp_send_event(os_event_t *events) { |
||||
if (syslogQueue == NULL) |
||||
syslogState = SYSLOG_READY; |
||||
else { |
||||
int res = 0; |
||||
syslog_espconn.proto.udp->remote_port = syslogHost.port; // ESP8266 udp remote port
|
||||
os_memcpy(&syslog_espconn.proto.udp->remote_ip, &syslogHost.addr.addr, 4); // ESP8266 udp remote IP
|
||||
res = espconn_send(&syslog_espconn, (uint8_t *)syslogQueue->datagram, syslogQueue->datagram_len); |
||||
if (res != 0) { |
||||
os_printf("syslog_udp_send: error %d\n", res); |
||||
} |
||||
} |
||||
} |
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : syslog_compose |
||||
* Description : compose a syslog_entry_t from va_args |
||||
* Parameters : va_args |
||||
* Returns : the malloced syslog_entry_t |
||||
******************************************************************************/ |
||||
static syslog_entry_t* ICACHE_FLASH_ATTR syslog_compose(uint8_t facility, uint8_t severity, const char *tag, const char *fmt, ...) |
||||
{ |
||||
syslog_entry_t *se = os_zalloc(sizeof (syslog_entry_t) + 1024); // allow up to 1k datagram
|
||||
char *p = se->datagram; |
||||
uint32_t tick = WDEV_NOW(); // 0 ... 4294.967295s
|
||||
|
||||
// The Priority value is calculated by first multiplying the Facility
|
||||
// number by 8 and then adding the numerical value of the Severity.
|
||||
p += os_sprintf(p, "<%d> ", facility * 8 + severity); |
||||
|
||||
// strftime doesn't work as expected - or adds 8k overhead.
|
||||
// so let's do poor man conversion - format is fixed anyway
|
||||
if (flashConfig.syslog_showdate == 0) |
||||
p += os_sprintf(p, "- "); |
||||
else { |
||||
time_t now = NULL; |
||||
struct tm *tp = NULL; |
||||
|
||||
// create timestamp: FULL-DATE "T" PARTIAL-TIME "Z": 'YYYY-mm-ddTHH:MM:SSZ '
|
||||
// as long as realtime_stamp is 0 we use tick div 10⁶ as date
|
||||
now = (realtime_stamp == 0) ? (tick / 1000000) : realtime_stamp; |
||||
tp = gmtime(&now); |
||||
|
||||
p += os_sprintf(p, "%4d-%02d-%02dT%02d:%02d:%02dZ ", |
||||
tp->tm_year + 1900, tp->tm_mon + 1, tp->tm_mday, |
||||
tp->tm_hour, tp->tm_min, tp->tm_sec); |
||||
} |
||||
|
||||
// add HOSTNAME APP-NAME PROCID MSGID
|
||||
if (flashConfig.syslog_showtick) |
||||
p += os_sprintf(p, "%s %s %lu.%06lu %lu ", flashConfig.hostname, tag, tick / 1000000, tick % 1000000, syslog_msgid++); |
||||
else |
||||
p += os_sprintf(p, "%s %s - %lu ", flashConfig.hostname, tag, syslog_msgid++); |
||||
|
||||
// append syslog message
|
||||
va_list arglist; |
||||
va_start(arglist, fmt); |
||||
p += ets_vsprintf(p, fmt, arglist ); |
||||
va_end(arglist); |
||||
|
||||
se->datagram_len = p - se->datagram; |
||||
se = mem_trim(se, sizeof(syslog_entry_t) + se->datagram_len + 1); |
||||
return se; |
||||
} |
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : syslog_add_entry |
||||
* Description : add a syslog_entry_t to the syslogQueue |
||||
* Parameters : entry: the syslog_entry_t |
||||
* Returns : none |
||||
******************************************************************************/ |
||||
static void ICACHE_FLASH_ATTR syslog_add_entry(syslog_entry_t *entry) |
||||
{ |
||||
syslog_entry_t *pse = syslogQueue; |
||||
|
||||
// append msg to syslog_queue
|
||||
if (pse == NULL) |
||||
syslogQueue = entry; |
||||
else { |
||||
while (pse->next != NULL) |
||||
pse = pse->next; |
||||
pse->next = entry; // append msg to syslog queue
|
||||
} |
||||
|
||||
// ensure we have sufficient heap for the rest of the system
|
||||
if (system_get_free_heap_size() < syslogHost.min_heap_size) { |
||||
if (syslogState != SYSLOG_HALTED) { |
||||
os_printf("syslog_add_entry: Warning: queue filled up, halted\n"); |
||||
entry->next = syslog_compose(SYSLOG_FAC_SYSLOG, SYSLOG_PRIO_CRIT, "-", "queue filled up, halted"); |
||||
if (syslogState == SYSLOG_READY) |
||||
syslog_send_udp(); |
||||
syslogState = SYSLOG_HALTED; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : syslog_sent_cb |
||||
* Description : udp sent successfully |
||||
* fetch next syslog package, free old message |
||||
* Parameters : arg -- Additional argument to pass to the callback function |
||||
* Returns : none |
||||
******************************************************************************/ |
||||
static void ICACHE_FLASH_ATTR syslog_udp_sent_cb(void *arg) |
||||
{ |
||||
struct espconn *pespconn = arg; |
||||
(void) pespconn; |
||||
|
||||
// datagram is delivered - free and advance queue
|
||||
syslog_entry_t *pse = syslogQueue; |
||||
syslogQueue = syslogQueue -> next; |
||||
os_free(pse); |
||||
|
||||
if (syslogQueue != NULL) |
||||
syslog_send_udp(); |
||||
else |
||||
syslogState = SYSLOG_READY; |
||||
} |
||||
|
||||
/*****************************************************************************
|
||||
* FunctionName : syslog_recv_cb |
||||
* Description : Processing the received udp packet |
||||
* Parameters : arg -- Additional argument to pass to the callback function |
||||
* pusrdata -- The received data (or NULL when the connection has been closed!) |
||||
* length -- The length of received data |
||||
* Returns : none |
||||
******************************************************************************/ |
||||
#ifdef SYSLOG_UDP_RECV |
||||
static void ICACHE_FLASH_ATTR syslog_udp_recv_cb(void *arg, char *pusrdata, unsigned short length) |
||||
{ |
||||
DBG("syslog_udp_recv_cb: %p, %p, %d\n", arg, pusrdata, length); |
||||
} |
||||
#endif |
||||
|
||||
/******************************************************************************
|
||||
* |
||||
******************************************************************************/ |
||||
static void ICACHE_FLASH_ATTR syslog_gethostbyname_cb(const char *name, ip_addr_t *ipaddr, void *arg) |
||||
{ |
||||
struct espconn *pespconn = (struct espconn *)arg; |
||||
(void) pespconn; |
||||
if (ipaddr != NULL) { |
||||
syslogHost.addr.addr = ipaddr->addr; |
||||
syslogState = SYSLOG_SENDING; |
||||
syslog_send_udp(); |
||||
} else { |
||||
syslogState = SYSLOG_ERROR; |
||||
DBG("syslog_gethostbyname_cb: state=SYSLOG_ERROR\n"); |
||||
} |
||||
} |
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : initSyslog |
||||
* Description : Initialize the syslog library |
||||
* Parameters : hostname -- the syslog server (host:port) |
||||
* host: IP-Addr | hostname |
||||
* Returns : none |
||||
*******************************************************************************/ |
||||
void ICACHE_FLASH_ATTR syslog_init(char *syslog_server) |
||||
{ |
||||
char host[32], *port = &host[0]; |
||||
|
||||
syslog_task = register_usr_task(syslog_udp_send_event); |
||||
syslogHost.min_heap_size = flashConfig.syslog_minheap; |
||||
syslogHost.port = 514; |
||||
syslogState = SYSLOG_WAIT; |
||||
|
||||
os_strncpy(host, syslog_server, 32); |
||||
while (*port && *port != ':') // find port delimiter
|
||||
port++; |
||||
if (*port) { |
||||
*port++ = '\0'; |
||||
syslogHost.port = atoi(port); |
||||
} |
||||
|
||||
wifi_set_broadcast_if(STATIONAP_MODE); // send UDP broadcast from both station and soft-AP interface
|
||||
syslog_espconn.type = ESPCONN_UDP; |
||||
syslog_espconn.proto.udp = (esp_udp *)os_zalloc(sizeof(esp_udp)); |
||||
syslog_espconn.proto.udp->local_port = espconn_port(); // set a available port
|
||||
#ifdef SYSLOG_UDP_RECV |
||||
espconn_regist_recvcb(&syslog_espconn, syslog_udp_recv_cb); // register a udp packet receiving callback
|
||||
#endif |
||||
espconn_regist_sentcb(&syslog_espconn, syslog_udp_sent_cb); // register a udp packet sent callback
|
||||
espconn_create(&syslog_espconn); // create udp
|
||||
|
||||
if (UTILS_StrToIP((const char *)host, (void*)&syslogHost.addr)) { |
||||
syslogState = SYSLOG_SENDING; |
||||
syslog_send_udp(); |
||||
} else { |
||||
static struct espconn espconn_ghbn; |
||||
espconn_gethostbyname(&espconn_ghbn, host, &syslogHost.addr, syslog_gethostbyname_cb); |
||||
// syslog_send_udp is called by syslog_gethostbyname_cb()
|
||||
} |
||||
#ifdef SYSLOG_UDP_RECV |
||||
DBG("syslog_init: host: %s, port: %d, lport: %d, recvcb: %p, sentcb: %p, state: %d\n", |
||||
host, syslogHost.port, syslog_espconn.proto.udp->local_port, |
||||
syslog_udp_recv_cb, syslog_udp_sent_cb, syslogState ); |
||||
#else |
||||
DBG("syslog_init: host: %s, port: %d, lport: %d, rsentcb: %p, state: %d\n", |
||||
host, syslogHost.port, syslog_espconn.proto.udp->local_port, |
||||
syslog_udp_sent_cb, syslogState ); |
||||
#endif |
||||
} |
||||
|
||||
/******************************************************************************
|
||||
* FunctionName : syslog |
||||
* Description : compose and queue a new syslog message |
||||
* Parameters : facility |
||||
* severity |
||||
* tag |
||||
* message |
||||
* ... |
||||
* |
||||
* SYSLOG-MSG = HEADER SP STRUCTURED-DATA [SP MSG] |
||||
|
||||
HEADER = PRI VERSION SP TIMESTAMP SP HOSTNAME |
||||
SP APP-NAME SP PROCID SP MSGID |
||||
PRI = "<" PRIVAL ">" |
||||
PRIVAL = 1*3DIGIT ; range 0 .. 191 |
||||
VERSION = NONZERO-DIGIT 0*2DIGIT |
||||
HOSTNAME = NILVALUE / 1*255PRINTUSASCII |
||||
|
||||
APP-NAME = NILVALUE / 1*48PRINTUSASCII |
||||
PROCID = NILVALUE / 1*128PRINTUSASCII |
||||
MSGID = NILVALUE / 1*32PRINTUSASCII |
||||
|
||||
TIMESTAMP = NILVALUE / FULL-DATE "T" FULL-TIME |
||||
FULL-DATE = DATE-FULLYEAR "-" DATE-MONTH "-" DATE-MDAY |
||||
DATE-FULLYEAR = 4DIGIT |
||||
DATE-MONTH = 2DIGIT ; 01-12 |
||||
DATE-MDAY = 2DIGIT ; 01-28, 01-29, 01-30, 01-31 based on |
||||
; month/year |
||||
FULL-TIME = PARTIAL-TIME TIME-OFFSET |
||||
PARTIAL-TIME = TIME-HOUR ":" TIME-MINUTE ":" TIME-SECOND |
||||
[TIME-SECFRAC] |
||||
TIME-HOUR = 2DIGIT ; 00-23 |
||||
TIME-MINUTE = 2DIGIT ; 00-59 |
||||
TIME-SECOND = 2DIGIT ; 00-59 |
||||
TIME-SECFRAC = "." 1*6DIGIT |
||||
TIME-OFFSET = "Z" / TIME-NUMOFFSET |
||||
TIME-NUMOFFSET = ("+" / "-") TIME-HOUR ":" TIME-MINUTE |
||||
|
||||
|
||||
STRUCTURED-DATA = NILVALUE / 1*SD-ELEMENT |
||||
SD-ELEMENT = "[" SD-ID *(SP SD-PARAM) "]" |
||||
SD-PARAM = PARAM-NAME "=" %d34 PARAM-VALUE %d34 |
||||
SD-ID = SD-NAME |
||||
PARAM-NAME = SD-NAME |
||||
PARAM-VALUE = UTF-8-STRING ; characters '"', '\' and |
||||
; ']' MUST be escaped. |
||||
SD-NAME = 1*32PRINTUSASCII |
||||
; except '=', SP, ']', %d34 (") |
||||
|
||||
MSG = MSG-ANY / MSG-UTF8 |
||||
MSG-ANY = *OCTET ; not starting with BOM |
||||
MSG-UTF8 = BOM UTF-8-STRING |
||||
BOM = %xEF.BB.BF |
||||
UTF-8-STRING = *OCTET ; UTF-8 string as specified |
||||
; in RFC 3629 |
||||
|
||||
OCTET = %d00-255 |
||||
SP = %d32 |
||||
PRINTUSASCII = %d33-126 |
||||
NONZERO-DIGIT = %d49-57 |
||||
DIGIT = %d48 / NONZERO-DIGIT |
||||
NILVALUE = "-" |
||||
* |
||||
* TIMESTAMP: realtime_clock == 0 ? timertick / 10⁶ : realtime_clock |
||||
* HOSTNAME hostname |
||||
* APPNAME: ems-esp-link |
||||
* PROCID: timertick |
||||
* MSGID: NILVALUE |
||||
* |
||||
* Returns : none |
||||
*******************************************************************************/ |
||||
void ICACHE_FLASH_ATTR syslog(uint8_t facility, uint8_t severity, const char *tag, const char *fmt, ...) |
||||
{ |
||||
DBG("syslog: state=%d ", syslogState); |
||||
if (syslogState == SYSLOG_ERROR || |
||||
syslogState == SYSLOG_HALTED) |
||||
return; |
||||
|
||||
// compose the syslog message
|
||||
void *arg = __builtin_apply_args(); |
||||
void *res = __builtin_apply((void*)syslog_compose, arg, 128); |
||||
syslog_entry_t *se = *(syslog_entry_t **)res; |
||||
|
||||
// and append it to the message queue
|
||||
syslog_add_entry(se); |
||||
|
||||
if (syslogState == SYSLOG_READY) { |
||||
syslogState = SYSLOG_SENDING; |
||||
syslog_send_udp(); |
||||
} |
||||
|
||||
if (syslogState == SYSLOG_NONE) { |
||||
syslogState = SYSLOG_WAIT; |
||||
syslog_chk_wifi_stat(); // fire the timer to check the Wifi connection status
|
||||
} |
||||
} |
@ -0,0 +1,110 @@ |
||||
/*
|
||||
* syslog.h |
||||
* |
||||
* |
||||
* Copyright 2015 Susi's Strolch |
||||
* |
||||
* For license information see projects "License.txt" |
||||
* |
||||
* part of syslog.c - client library |
||||
* |
||||
*/ |
||||
|
||||
|
||||
#ifndef _SYSLOG_H |
||||
#define _SYSLOG_H |
||||
|
||||
#ifdef __cplusplus |
||||
extern "C" { |
||||
#endif |
||||
|
||||
enum syslog_state { |
||||
SYSLOG_NONE, // not initialized
|
||||
SYSLOG_WAIT, // initialized, waiting for Wifi
|
||||
SYSLOG_READY, // Wifi established, ready to send
|
||||
SYSLOG_SENDING, // UDP package on the air
|
||||
SYSLOG_HALTED, // heap full, discard message
|
||||
SYSLOG_ERROR, |
||||
}; |
||||
|
||||
enum syslog_priority { |
||||
SYSLOG_PRIO_EMERG, /* system is unusable */ |
||||
SYSLOG_PRIO_ALERT, /* action must be taken immediately */ |
||||
SYSLOG_PRIO_CRIT, /* critical conditions */ |
||||
SYSLOG_PRIO_ERR, /* error conditions */ |
||||
SYSLOG_PRIO_WARNING, /* warning conditions */ |
||||
SYSLOG_PRIO_NOTICE, /* normal but significant condition */ |
||||
SYSLOG_PRIO_INFO, /* informational */ |
||||
SYSLOG_PRIO_DEBUG, /* debug-level messages */ |
||||
}; |
||||
|
||||
enum syslog_facility { |
||||
SYSLOG_FAC_KERN, /* kernel messages */ |
||||
SYSLOG_FAC_USER, /* random user-level messages */ |
||||
SYSLOG_FAC_MAIL, /* mail system */ |
||||
SYSLOG_FAC_DAEMON, /* system daemons */ |
||||
SYSLOG_FAC_AUTH, /* security/authorization messages */ |
||||
SYSLOG_FAC_SYSLOG, /* messages generated internally by syslogd */ |
||||
SYSLOG_FAC_LPR, /* line printer subsystem */ |
||||
SYSLOG_FAC_NEWS, /* network news subsystem */ |
||||
SYSLOG_FAC_UUCP, /* UUCP subsystem */ |
||||
SYSLOG_FAC_CRON, /* clock daemon */ |
||||
SYSLOG_FAC_AUTHPRIV,/* security/authorization messages (private) */ |
||||
SYSLOG_FAC_FTP, /* ftp daemon */ |
||||
SYSLOG_FAC_LOCAL0, /* reserved for local use */ |
||||
SYSLOG_FAC_LOCAL1, /* reserved for local use */ |
||||
SYSLOG_FAC_LOCAL2, /* reserved for local use */ |
||||
SYSLOG_FAC_LOCAL3, /* reserved for local use */ |
||||
SYSLOG_FAC_LOCAL4, /* reserved for local use */ |
||||
SYSLOG_FAC_LOCAL5, /* reserved for local use */ |
||||
SYSLOG_FAC_LOCAL6, /* reserved for local use */ |
||||
SYSLOG_FAC_LOCAL7, /* reserved for local use */ |
||||
}; |
||||
|
||||
#define MINIMUM_HEAP_SIZE 8192 |
||||
#define REG_READ(_r) (*(volatile uint32 *)(_r)) |
||||
#define WDEV_NOW() REG_READ(0x3ff20c00) |
||||
|
||||
extern uint32_t realtime_stamp; // 1sec NTP ticker
|
||||
|
||||
typedef struct syslog_host_t syslog_host_t; |
||||
struct syslog_host_t { |
||||
uint32_t min_heap_size; // minimum allowed heap size when buffering
|
||||
ip_addr_t addr; |
||||
uint16_t port; |
||||
}; |
||||
|
||||
// buffered syslog event - f.e. if network stack isn't up and running
|
||||
typedef struct syslog_entry_t syslog_entry_t; |
||||
struct syslog_entry_t { |
||||
syslog_entry_t *next; |
||||
uint16_t datagram_len; |
||||
char datagram[]; |
||||
}; |
||||
|
||||
syslog_host_t syslogserver; |
||||
|
||||
void ICACHE_FLASH_ATTR syslog_init(char *syslog_server); |
||||
void ICACHE_FLASH_ATTR syslog(uint8_t facility, uint8_t severity, const char tag[], const char message[], ...); |
||||
|
||||
// some convenience macros
|
||||
#ifdef SYSLOG |
||||
// extern char *esp_link_version; // in user_main.c
|
||||
#define LOG_DEBUG(format, ...) syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_DEBUG, "esp_link", format, ## __VA_ARGS__ ) |
||||
#define LOG_NOTICE(format, ...) syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_NOTICE, "esp_link", format, ## __VA_ARGS__ ) |
||||
#define LOG_INFO(format, ...) syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_INFO, "esp_link", format, ## __VA_ARGS__ ) |
||||
#define LOG_WARN(format, ...) syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_WARNING, "esp_link", format, ## __VA_ARGS__ ) |
||||
#define LOG_ERR(format, ...) syslog(SYSLOG_FAC_USER, SYSLOG_PRIO_ERR, "esp_link", format, ## __VA_ARGS__ ) |
||||
#else |
||||
#define LOG_DEBUG(format, ...) do { } while(0) |
||||
#define LOG_NOTICE(format, ...) do { } while(0) |
||||
#define LOG_WARN(format, ...) do { } while(0) |
||||
#define LOG_INFO(format, ...) do { } while(0) |
||||
#define LOG_ERR(format, ...) do { } while(0) |
||||
#endif |
||||
|
||||
#ifdef __cplusplus |
||||
} |
||||
#endif |
||||
|
||||
#endif /* _SYSLOG_H */ |
Loading…
Reference in new issue