diff --git a/Makefile b/Makefile index 258857e..be82a23 100644 --- a/Makefile +++ b/Makefile @@ -56,7 +56,7 @@ TARGET = httpd # which modules (subdirectories) of the project to include in compiling MODULES = user sbmp/library -EXTRA_INCDIR = include libesphttpd/include sbmp/library +EXTRA_INCDIR = include libesphttpd/include sbmp/library user/ # libraries used in this project, mainly provided by the SDK LIBS = c gcc hal phy pp net80211 wpa main lwip crypto diff --git a/libesphttpd/Makefile b/libesphttpd/Makefile index 078c2b0..cc2bc3b 100644 --- a/libesphttpd/Makefile +++ b/libesphttpd/Makefile @@ -48,7 +48,8 @@ LIB = libesphttpd.a MODULES = espfs core util EXTRA_INCDIR = ./include \ . \ - lib/heatshrink/ + lib/heatshrink/ \ + ../user # compiler flags using during compilation of source files diff --git a/libesphttpd/include/logging.h b/libesphttpd/include/logging.h index e430a28..3bdcce4 100644 --- a/libesphttpd/include/logging.h +++ b/libesphttpd/include/logging.h @@ -1,19 +1,51 @@ #ifndef HTTPD_LOGGING_H #define HTTPD_LOGGING_H - -#ifndef httpd_printf -#define httpd_printf(fmt, ...) os_printf(fmt, ##__VA_ARGS__) -#endif +#include +#include "uptime.h" // logging functions #define LOG_EOL "\n" -#define dbg(fmt, ...) httpd_printf(fmt LOG_EOL, ##__VA_ARGS__); -#define banner(fmt, ...) httpd_printf("\x1b[32;1m"fmt"\x1b[0m"LOG_EOL, ##__VA_ARGS__); -#define info(fmt, ...) httpd_printf("\x1b[32m"fmt"\x1b[0m"LOG_EOL, ##__VA_ARGS__); -#define error(fmt, ...) httpd_printf("\x1b[31;1m"fmt"\x1b[0m"LOG_EOL, ##__VA_ARGS__); -#define warn(fmt, ...) httpd_printf("\x1b[33;1m"fmt"\x1b[0m"LOG_EOL, ##__VA_ARGS__); + + +#define dbg(fmt, ...) \ + do { \ + uptime_print(); \ + printf(" [ ] "fmt LOG_EOL, ##__VA_ARGS__); \ + } while(0) + + +#define banner(fmt, ...) \ + do { \ + printf("\x1b[32;1m"); \ + uptime_print(); \ + printf(" [i] "fmt"\x1b[0m"LOG_EOL, ##__VA_ARGS__); \ + } while(0) + + +#define info(fmt, ...) \ + do { \ + printf("\x1b[32m"); \ + uptime_print(); \ + printf(" [i] "fmt"\x1b[0m"LOG_EOL, ##__VA_ARGS__); \ + } while(0) + + +#define error(fmt, ...) \ + do { \ + printf("\x1b[31;1m"); \ + uptime_print(); \ + printf(" [E] "fmt"\x1b[0m"LOG_EOL, ##__VA_ARGS__); \ + } while(0) + + +#define warn(fmt, ...) \ + do { \ + printf("\x1b[33;1m"); \ + uptime_print(); \ + printf(" [W] "fmt"\x1b[0m"LOG_EOL, ##__VA_ARGS__); \ + } while(0) #endif // HTTPD_LOGGING_H diff --git a/libesphttpd/include/platform.h b/libesphttpd/include/platform.h index 5e107e0..3a0f23d 100644 --- a/libesphttpd/include/platform.h +++ b/libesphttpd/include/platform.h @@ -25,7 +25,7 @@ typedef RtosConnType* ConnTypePtr; #define strlen(a) os_strlen(a) #define memcmp(a, b, c) os_memcmp(a, b, c) typedef struct espconn* ConnTypePtr; -#define httpd_printf(format, ...) os_printf(format, ##__VA_ARGS__) +//#define httpd_printf(format, ...) os_printf(format, ##__VA_ARGS__) #endif diff --git a/libesphttpd/util/cgiflash.c b/libesphttpd/util/cgiflash.c index dec6e25..8a63bf6 100644 --- a/libesphttpd/util/cgiflash.c +++ b/libesphttpd/util/cgiflash.c @@ -55,7 +55,7 @@ int ICACHE_FLASH_ATTR cgiGetFirmwareNext(HttpdConnData *connData) { httpdEndHeaders(connData); char *next = id == 1 ? "user1.bin" : "user2.bin"; httpdSend(connData, next, -1); - httpd_printf("Next firmware: %s (got %d)\n", next, id); + dbg("Next firmware: %s (got %d)", next, id); return HTTPD_CGI_DONE; } @@ -70,7 +70,7 @@ int ICACHE_FLASH_ATTR cgiReadFlash(HttpdConnData *connData) { } if (*pos==0) { - httpd_printf("Start flash download.\n"); + info("Start flash download."); httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "application/bin"); httpdEndHeaders(connData); diff --git a/user/sampling.c b/user/sampling.c index b77c93a..af30a7d 100644 --- a/user/sampling.c +++ b/user/sampling.c @@ -70,6 +70,7 @@ static void FLASH_FN request_data_sesn_listener(SBMP_Endpoint *ep, SBMP_Datagram switch (dg->type) { case DG_BULK_OFFER:// Data ready notification info("--- Data offered for bulk transfer ---"); + setReadoutTmeoTimer(1000); // data is ready to be read pp = pp_start(dg->payload, dg->length); @@ -79,13 +80,14 @@ static void FLASH_FN request_data_sesn_listener(SBMP_Endpoint *ep, SBMP_Datagram dbg("Total bytes: %d", readState->total); - retry_TO(100, sbmp_bulk_request(ep, readState->pos, CHUNK_LEN, dg->session)); + // we choose to request the data immediately - setReadoutTmeoTimer(500); + retry_TO(100, sbmp_bulk_request(ep, readState->pos, CHUNK_LEN, dg->session)); break; case DG_BULK_DATA: // data received info("--- Received a chunk, length %d ---", dg->length); + setReadoutTmeoTimer(1000); // Process the received data pp = pp_start(dg->payload, dg->length); @@ -110,7 +112,6 @@ static void FLASH_FN request_data_sesn_listener(SBMP_Endpoint *ep, SBMP_Datagram } else { // read next part retry_TO(100, sbmp_bulk_request(ep, readState->pos, CHUNK_LEN, dg->session)); - setReadoutTmeoTimer(500); } break; diff --git a/user/sbmp_config.h b/user/sbmp_config.h index d63a69c..ed00d91 100644 --- a/user/sbmp_config.h +++ b/user/sbmp_config.h @@ -55,13 +55,25 @@ /** * @brief Enable detailed logging (only for debugging, disable for better performance). */ -#define SBMP_DEBUG 0 +#define SBMP_DEBUG 1 // here are the actual logging functions -#define sbmp_error(fmt, ...) (SBMP_DEBUG||SBMP_LOGGING ? os_printf("\x1b[31;1m[SBMP][E] "fmt"\x1b[0m\n", ##__VA_ARGS__) : 0) -#define sbmp_warn(fmt, ...) (SBMP_DEBUG||SBMP_LOGGING ? os_printf("\x1b[33;1m[SBMP][E] "fmt"\x1b[0m\n", ##__VA_ARGS__) : 0) -#define sbmp_info(fmt, ...) (SBMP_DEBUG||SBMP_LOGGING ? os_printf("\x1b[32m[SBMP][i] "fmt"\x1b[0m\n", ##__VA_ARGS__) : 0) -#define sbmp_dbg(fmt, ...) (SBMP_DEBUG ? os_printf("[SBMP][ ] "fmt"\n", ##__VA_ARGS__) : 0) + +#if SBMP_DEBUG||SBMP_LOGGING +# define sbmp_error(fmt, ...) error("[SBMP] "fmt, ##__VA_ARGS__) +# define sbmp_warn(fmt, ...) warn("[SBMP] "fmt, ##__VA_ARGS__) +# define sbmp_info(fmt, ...) info("[SBMP] "fmt, ##__VA_ARGS__) +#else +# define sbmp_error(fmt, ...) +# define sbmp_warn(fmt, ...) +# define sbmp_info(fmt, ...) +#endif + +#if SBMP_DEBUG +# define sbmp_dbg(fmt, ...) dbg("[SBMP] "fmt, ##__VA_ARGS__) +#else +# define sbmp_dbg(fmt, ...) +#endif /* ---------- Fix formatting -------------- */ diff --git a/user/uptime.c b/user/uptime.c index 000c0aa..d145863 100644 --- a/user/uptime.c +++ b/user/uptime.c @@ -36,3 +36,24 @@ void uptime_str(char *buf) sprintf(buf, "%02u:%02u:%02u", hours, mins, secs); } } + +void uptime_print(void) +{ + u32 a = uptime; + u32 days = a / 86400; + a -= days * 86400; + + u32 hours = a / 3600; + a -= hours * 3600; + + u32 mins = a / 60; + a -= mins * 60; + + u32 secs = a; + + if (days > 0) { + printf("%ud %02u:%02u:%02u", days, hours, mins, secs); + } else { + printf("%02u:%02u:%02u", hours, mins, secs); + } +} diff --git a/user/uptime.h b/user/uptime.h index 729743e..fcc046c 100644 --- a/user/uptime.h +++ b/user/uptime.h @@ -10,4 +10,6 @@ void uptime_timer_init(void); /** Print uptime to a buffer. Should be at least 20 long. */ void uptime_str(char *buf); +void uptime_print(void); + #endif // UPTIME_H diff --git a/user/user_main.c b/user/user_main.c index e3f0061..753bd85 100644 --- a/user/user_main.c +++ b/user/user_main.c @@ -43,9 +43,7 @@ static void ICACHE_FLASH_ATTR prHeapTimerCb(void *arg) if (++cnt == 3) { cnt = 0; u32 heap = system_get_free_heap_size(); - char upt[20]; - uptime_str(upt); - os_printf("%s, heap: %u (~ %d)\n", upt, heap, (heap-last)); + dbg("Heap: %u (~ %d)\n", heap, (heap-last)); last = heap; }