diff --git a/src/drv_tuyaMCU.c b/src/drv_tuyaMCU.c index 32f6c64fc..4bbb96eeb 100644 --- a/src/drv_tuyaMCU.c +++ b/src/drv_tuyaMCU.c @@ -26,6 +26,13 @@ void TuyaMCU_RunFrame(); // from http_fns. should move to a utils file. extern unsigned char hexbyte( const char* hex ); +#if PLATFORM_BK7231T | PLATFORM_BK7231N + // from uart_bk.c + extern void bk_send_byte(UINT8 uport, UINT8 data); +#elif WINDOWS +#else +#endif + const char *TuyaMCU_GetCommandTypeLabel(int t) { if(t == TUYA_CMD_HEARTBEAT) @@ -196,6 +203,9 @@ void TuyaMCU_Bridge_InitUART(int baud) { #endif } + + + void TuyaMCU_Bridge_SendUARTByte(byte b) { #if PLATFORM_BK7231T | PLATFORM_BK7231N bk_send_byte(0, b); diff --git a/src/httpserver/new_http.c b/src/httpserver/new_http.c index d1221c302..9a94814ea 100644 --- a/src/httpserver/new_http.c +++ b/src/httpserver/new_http.c @@ -21,6 +21,9 @@ #include "../ota/ota.h" +// define the feature ADDLOGF_XXX will use +#define LOG_FEATURE LOG_FEATURE_HTTP + const char httpHeader[] = "HTTP/1.1 %d OK\nContent-type: %s" ; // HTTP header const char httpMimeTypeHTML[] = "text/html" ; // HTML MIME type const char httpMimeTypeText[] = "text/plain" ; // TEXT MIME type @@ -326,7 +329,7 @@ int postany(http_request_t *request, const char *str, int len){ currentlen = 0; } if (addlen > request->replymaxlen){ - printf("won't fit"); + ADDLOGF_ERROR("won't fit"); } else { memcpy( request->reply+request->replylen, str, addlen ); request->replylen += addlen; @@ -404,7 +407,7 @@ int HTTP_ProcessPacket(http_request_t *request) { } } if (request->method == -1){ - printf("unsupported method %7s", recvbuf); + ADDLOGF_ERROR("unsupported method %7s", recvbuf); return 0; } @@ -428,7 +431,7 @@ int HTTP_ProcessPacket(http_request_t *request) { p++; // past space } else { - printf("invalid request\n"); + ADDLOGF_ERROR("invalid request\n"); return 0; } @@ -442,7 +445,7 @@ int HTTP_ProcessPacket(http_request_t *request) { p++; // past \r p++; // past \n } else { - printf("invalid request\n"); + ADDLOGF_ERROR("invalid request\n"); return 0; } // i.e. not received diff --git a/src/littlefs/our_lfs.c b/src/littlefs/our_lfs.c index 75807fcac..d8e47067d 100644 --- a/src/littlefs/our_lfs.c +++ b/src/littlefs/our_lfs.c @@ -18,6 +18,8 @@ #ifdef BK_LITTLEFS +// define the feature ADDLOGF_XXX will use +#define LOG_FEATURE LOG_FEATURE_LFS // variables used by the filesystem int lfs_initialised = 0; @@ -85,25 +87,25 @@ void init_lfs(int create){ // this should only happen on the first boot if (err){ if (create) { - ADDLOG_INFO(LOG_FEATURE_LFS, "Formatting LFS"); + ADDLOGF_INFO("Formatting LFS"); err = lfs_format(&lfs, &cfg); if (err){ - ADDLOG_ERROR(LOG_FEATURE_LFS, "Format LFS failed %d", err); + ADDLOGF_ERROR("Format LFS failed %d", err); return; } - ADDLOG_INFO(LOG_FEATURE_LFS, "Formatted LFS"); + ADDLOGF_INFO("Formatted LFS"); err = lfs_mount(&lfs, &cfg); if (err){ - ADDLOG_ERROR(LOG_FEATURE_LFS, "Mount LFS failed %d", err); + ADDLOGF_ERROR("Mount LFS failed %d", err); return; } lfs_initialised = 1; } else { - ADDLOG_INFO(LOG_FEATURE_LFS, "LFS not present - not creating"); + ADDLOGF_INFO("LFS not present - not creating"); } } else { // mounted existing - ADDLOG_INFO(LOG_FEATURE_LFS, "Mounted existing LFS"); + ADDLOGF_INFO("Mounted existing LFS"); lfs_initialised = 1; } #ifdef LFS_BOOTCOUNT @@ -118,7 +120,7 @@ void init_lfs(int create){ // remember the storage is not updated until the file is closed successfully lfs_file_close(&lfs, &file); - ADDLOG_INFO(LOG_FEATURE_LFS, "boot count %d", boot_count); + ADDLOGF_INFO("boot count %d", boot_count); #endif } } diff --git a/src/logging/logging.c b/src/logging/logging.c index 72c0b0bc6..2f7f68ab5 100644 --- a/src/logging/logging.c +++ b/src/logging/logging.c @@ -27,19 +27,31 @@ unsigned int logfeatures = ( (1 << 12) | (1 << 13) | (1 << 14) | - (1 << 15) + (1 << 15) | + (1 << 16) | + (1 << 17) | + (1 << 18) | + (1 << 19) | + (1 << 20) | + (1 << 21) | + (1 << 22) | + (1 << 23) | + (1 << 24) ); static int log_delay = 0; +// must match header definitions in logging.h char *loglevelnames[] = { "NONE:", "Error:", "Warn:", "Info:", "Debug:", + "ExtraDebug:", "All:" }; +// must match header definitions in logging.h char *logfeaturenames[] = { "HTTP:",// = 0, "MQTT:",// = 1, @@ -52,9 +64,46 @@ char *logfeaturenames[] = { "API:", // = 8 "LFS:", // = 9 "CMD:", // = 10 + "NTP:", // = 11 }; +#ifdef WINDOWS +void addLog(char *fmt, ...){ + va_list argList; + va_start(argList, fmt); + vsprintf(tmp, fmt, argList); + va_end(argList); + printf(tmp); + printf("\r\n"); +} + +void addLogAdv(int level, int feature, char *fmt, ...){ + va_list argList; + char *t = tmp; + if (!((1< loglevel){ + return; + } + va_start(argList, fmt); + vsprintf(tmp, fmt, argList); + va_end(argList); + strcpy(t, loglevelnames[level]); + t += strlen(t); + if (feature < sizeof(logfeaturenames)/sizeof(*logfeaturenames)){ + strcpy(t, logfeaturenames[feature]); + t += strlen(t); + } + + printf(tmp); + printf("\r\n"); +} + +#else // from WINDOWS + + #ifdef DEBUG_USE_SIMPLE_LOGGER void addLog(char *fmt, ...){ @@ -562,4 +611,5 @@ int log_command(const void *context, const char *cmd, char *args){ } -#endif \ No newline at end of file +#endif // else from simple logger +#endif // else from windows \ No newline at end of file diff --git a/src/logging/logging.h b/src/logging/logging.h index 69b48a79a..02ba7ffb6 100644 --- a/src/logging/logging.h +++ b/src/logging/logging.h @@ -19,10 +19,18 @@ void addLogAdv(int level, int feature, char *fmt, ...); int log_command(const void *context, const char *cmd, char *args); -#define ADDLOG_ERROR(x, y, ...) addLogAdv(LOG_ERROR, x, y, ##__VA_ARGS__) -#define ADDLOG_WARN(x, y, ...) addLogAdv(LOG_WARN, x, y, ##__VA_ARGS__) -#define ADDLOG_INFO(x, y, ...) addLogAdv(LOG_INFO, x, y, ##__VA_ARGS__) -#define ADDLOG_DEBUG(x, y, ...) addLogAdv(LOG_DEBUG, x, y, ##__VA_ARGS__) +#define ADDLOG_ERROR(x, fmt, ...) addLogAdv(LOG_ERROR, x, fmt, ##__VA_ARGS__) +#define ADDLOG_WARN(x, fmt, ...) addLogAdv(LOG_WARN, x, fmt, ##__VA_ARGS__) +#define ADDLOG_INFO(x, fmt, ...) addLogAdv(LOG_INFO, x, fmt, ##__VA_ARGS__) +#define ADDLOG_DEBUG(x, fmt, ...) addLogAdv(LOG_DEBUG, x, fmt, ##__VA_ARGS__) +#define ADDLOG_EXTRADEBUG(x, fmt, ...) addLogAdv(LOG_EXTRADEBUG, x, fmt, ##__VA_ARGS__) + +#define ADDLOGF_ERROR(fmt, ...) addLogAdv(LOG_ERROR, LOG_FEATURE, fmt, ##__VA_ARGS__) +#define ADDLOGF_WARN(fmt, ...) addLogAdv(LOG_WARN, LOG_FEATURE, fmt, ##__VA_ARGS__) +#define ADDLOGF_INFO(fmt, ...) addLogAdv(LOG_INFO, LOG_FEATURE, fmt, ##__VA_ARGS__) +#define ADDLOGF_DEBUG(fmt, ...) addLogAdv(LOG_DEBUG, LOG_FEATURE, fmt, ##__VA_ARGS__) +#define ADDLOGF_EXTRADEBUG(fmt, ...) addLogAdv(LOG_EXTRADEBUG, LOG_FEATURE, fmt, ##__VA_ARGS__) + extern int loglevel; extern char *loglevelnames[]; @@ -42,8 +50,9 @@ typedef enum { LOG_WARN = 2, LOG_INFO = 3, LOG_DEBUG = 4, - LOG_ALL = 5, - LOG_MAX = 6 + LOG_EXTRADEBUG = 5, + LOG_ALL = 6, + LOG_MAX = 7 } log_levels; typedef enum { @@ -58,8 +67,9 @@ typedef enum { LOG_FEATURE_API = 8, LOG_FEATURE_LFS = 9, LOG_FEATURE_CMD = 10, + LOG_FEATURE_NTP = 11, // add in here - but also in names in logging.c - LOG_FEATURE_MAX = 11, + LOG_FEATURE_MAX = 12, } log_features; #endif \ No newline at end of file diff --git a/src/ntp_time.c b/src/ntp_time.c index 23bdc3e68..99655dd7a 100644 --- a/src/ntp_time.c +++ b/src/ntp_time.c @@ -21,8 +21,12 @@ #include "lwip/sockets.h" #include "logging/logging.h" +// please enable logging.c/h in windows!!! +#undef printf +#define printf(x, ...) ADDLOGF_ERROR(x, ##__VA_ARGS__) #endif +#define LOG_FEATURE LOG_FEATURE_NTP typedef struct { @@ -75,8 +79,8 @@ void NTP_Shutdown() { } void NTP_SendRequest(bool bBlocking) { byte *ptr; - int i, recv_len; - char buf[64]; + //int i, recv_len; + //char buf[64]; ntp_packet packet = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; adrLen = sizeof(g_address); diff --git a/src/user_main.c b/src/user_main.c index 5a1873ec3..1268a9539 100644 --- a/src/user_main.c +++ b/src/user_main.c @@ -76,6 +76,7 @@ int g_reset = 0; // save config in this number of seconds int g_savecfg = 0; +#define LOG_FEATURE LOG_FEATURE_MAIN // from wlan_ui.c void bk_reboot(void); @@ -109,7 +110,7 @@ int unw_recv(const int fd, void *buf, u32 nbytes) FD_SET( fd, &errfds ); ret = select( fd+1, &readfds, NULL, &errfds, NULL); - ADDLOG_DEBUG(LOG_FEATURE_MAIN, "select ret:%d, %d, %d\r\n", ret, FD_ISSET( fd, &readfds ), FD_ISSET( fd, &errfds )); + ADDLOGF_DEBUG("select ret:%d, %d, %d\r\n", ret, FD_ISSET( fd, &readfds ), FD_ISSET( fd, &errfds )); if(ret > 0 && FD_ISSET( fd, &readfds )) return recv(fd,buf,nbytes,0); @@ -150,7 +151,7 @@ void connect_to_wifi(const char *oob_ssid,const char *connect_key) network_cfg.dhcp_mode = DHCP_CLIENT; network_cfg.wifi_retry_interval = 100; - ADDLOG_INFO(LOG_FEATURE_MAIN, "ssid:%s key:%s\r\n", network_cfg.wifi_ssid, network_cfg.wifi_key); + ADDLOGF_INFO("ssid:%s key:%s\r\n", network_cfg.wifi_ssid, network_cfg.wifi_key); bk_wlan_start(&network_cfg); #endif @@ -168,7 +169,7 @@ static void app_led_timer_handler(void *data) NTP_OnEverySecond(); g_secondsElapsed ++; - ADDLOG_INFO(LOG_FEATURE_MAIN, "Timer is %i free mem %d\n", g_secondsElapsed, xPortGetFreeHeapSize()); + ADDLOGF_INFO("Timer is %i free mem %d\n", g_secondsElapsed, xPortGetFreeHeapSize()); // print network info if (!(g_secondsElapsed % 10)){ @@ -241,7 +242,7 @@ static int setup_wifi_open_access_point(void) os_strcpy((char *)wNetConfig.dns_server_ip_addr, APP_DRONE_DEF_NET_GW); - ADDLOG_INFO(LOG_FEATURE_MAIN, "no flash configuration, use default\r\n"); + ADDLOGF_INFO("no flash configuration, use default\r\n"); mac = (u8*)&ap_info.bssid.array; // this is MAC for Access Point, it's different than Client one // see wifi_get_mac_address source @@ -267,14 +268,14 @@ static int setup_wifi_open_access_point(void) wNetConfig.wifi_retry_interval = 100; if(1) { - ADDLOG_INFO(LOG_FEATURE_MAIN, "set ip info: %s,%s,%s\r\n", + ADDLOGF_INFO("set ip info: %s,%s,%s\r\n", wNetConfig.local_ip_addr, wNetConfig.net_mask, wNetConfig.dns_server_ip_addr); } if(1) { - ADDLOG_INFO(LOG_FEATURE_MAIN, "ssid:%s key:%s mode:%d\r\n", wNetConfig.wifi_ssid, wNetConfig.wifi_key, wNetConfig.wifi_mode); + ADDLOGF_INFO("ssid:%s key:%s mode:%d\r\n", wNetConfig.wifi_ssid, wNetConfig.wifi_key, wNetConfig.wifi_mode); } bk_wlan_start(&wNetConfig); @@ -289,7 +290,7 @@ static int setup_wifi_open_access_point(void) void wl_status( void *ctxt ){ rw_evt_type stat = *((rw_evt_type*)ctxt); - ADDLOG_INFO(LOG_FEATURE_MAIN, "wl_status %d\r\n", stat); + ADDLOGF_INFO("wl_status %d\r\n", stat); switch(stat){ case RW_EVT_STA_IDLE: @@ -347,8 +348,8 @@ void user_main(void) wifi_ssid = CFG_GetWiFiSSID(); wifi_pass = CFG_GetWiFiPass(); - ADDLOG_INFO(LOG_FEATURE_MAIN, "Using SSID [%s]\r\n",wifi_ssid); - ADDLOG_INFO(LOG_FEATURE_MAIN, "Using Pass [%s]\r\n",wifi_pass); + ADDLOGF_INFO("Using SSID [%s]\r\n",wifi_ssid); + ADDLOGF_INFO("Using Pass [%s]\r\n",wifi_pass); #if 0 // you can use this if you bricked your module by setting wrong access point data @@ -364,7 +365,7 @@ void user_main(void) bootFailures = boot_failures(); if (bootFailures > 3){ bForceOpenAP = 1; - ADDLOG_INFO(LOG_FEATURE_MAIN, "###### would force AP mode - boot failures %d", bootFailures); + ADDLOGF_INFO("###### force AP mode - boot failures %d", bootFailures); } if(*wifi_ssid == 0 || *wifi_pass == 0 || bForceOpenAP) { @@ -375,7 +376,7 @@ void user_main(void) connect_to_wifi(wifi_ssid,wifi_pass); // register function to get callbacks about wifi changes. bk_wlan_status_register_cb(wl_status); - ADDLOG_INFO(LOG_FEATURE_MAIN, "Registered for wifi changes\r\n"); + ADDLOGF_DEBUG("Registered for wifi changes\r\n"); } // NOT WORKING, I done it other way, see ethernetif.c @@ -383,14 +384,14 @@ void user_main(void) //demo_start_upd(); start_tcp_http(); - ADDLOG_INFO(LOG_FEATURE_MAIN, "Started http tcp server\r\n"); + ADDLOGF_DEBUG("Started http tcp server\r\n"); PIN_Init(); - ADDLOG_INFO(LOG_FEATURE_MAIN, "Initialised pins\r\n"); + ADDLOGF_DEBUG("Initialised pins\r\n"); PIN_SetGenericDoubleClickCallback(app_on_generic_dbl_click); - ADDLOG_INFO(LOG_FEATURE_MAIN, "Initialised other callbacks\r\n"); + ADDLOGF_DEBUG("Initialised other callbacks\r\n"); #ifdef BK_LITTLEFS @@ -420,7 +421,7 @@ void user_main(void) err = rtos_start_timer(&led_timer); ASSERT(kNoErr == err); - ADDLOG_INFO(LOG_FEATURE_MAIN, "started timer\r\n"); + ADDLOGF_DEBUG("started timer\r\n"); } #if PLATFORM_BK7231N