diff --git a/arch/platform/cooja/Makefile.cooja b/arch/platform/cooja/Makefile.cooja index 3c6c616d63df983ed4cbe6738bbe45854b687571..a2aa6c270645bdd273c4401e781456f74f4d0c6e 100644 --- a/arch/platform/cooja/Makefile.cooja +++ b/arch/platform/cooja/Makefile.cooja @@ -13,6 +13,9 @@ endif COOJA_DIR = $(CONTIKI_NG_TOOLS_DIR)/cooja +# Use dbg-io for IO functions like printf() +MODULES += os/lib/dbg-io + ### Assuming simulator quickstart if no JNI library name set from Cooja ifndef LIBNAME QUICKSTART=1 diff --git a/arch/platform/cooja/Makefile.customrules-cooja b/arch/platform/cooja/Makefile.customrules-cooja index 60d6240f1c806df896e11e34b9a59b9b5221a400..5791b914f429f11dd9197a93507cb9b900685168 100644 --- a/arch/platform/cooja/Makefile.customrules-cooja +++ b/arch/platform/cooja/Makefile.customrules-cooja @@ -18,10 +18,8 @@ $(ARCHIVE): $(CONTIKI_OBJECTFILES) | $(OBJECTDIR) $(JNILIB): $(CONTIKI_APP_OBJ) $(MAIN_OBJ) $(PROJECT_OBJECTFILES) $(ARCHIVE) | $(OBJECTDIR) ifdef REDEF_PRINTF - @echo Redefining printf - -$(foreach OBJ,$^, $(OBJCOPY) --redefine-sym printf=log_printf $(OBJ); ) - -$(foreach OBJ,$^, $(OBJCOPY) --redefine-sym puts=log_puts $(OBJ); ) - -$(foreach OBJ,$^, $(OBJCOPY) --redefine-sym putchar=log_putchar $(OBJ); ) + @echo Redefining printf,sprintf,vsnprintf,etc. + -$(foreach OBJ,$^, $(OBJCOPY) --redefine-syms $(CONTIKI)/arch/platform/cooja/redefine.syms $(OBJ); ) endif ## REDEF_PRINTF $(LINK_COMMAND_1) $^ $(LINK_COMMAND_2) diff --git a/arch/platform/cooja/redefine.syms b/arch/platform/cooja/redefine.syms new file mode 100644 index 0000000000000000000000000000000000000000..30f261f4383b90bf2b460d4275c62c8fa8cce347 --- /dev/null +++ b/arch/platform/cooja/redefine.syms @@ -0,0 +1,7 @@ +# Symbols to redefine to ensure use of Contiki-NG implementations +printf log_printf +sprintf log_sprintf +snprintf log_snprintf +vsnprintf log_vsnprintf +puts log_puts +putchar log_putchar diff --git a/arch/platform/cooja/sys/cooja-log.c b/arch/platform/cooja/sys/cooja-log.c index c7ffdb9ac5b175daea944891fed08497528d21cf..9ec09b29cec10281c61169d3c9ee99d4fbf36b10 100644 --- a/arch/platform/cooja/sys/cooja-log.c +++ b/arch/platform/cooja/sys/cooja-log.c @@ -33,13 +33,10 @@ #include <string.h> #include "lib/simEnvChange.h" -#define IMPLEMENT_PRINTF 1 - #ifndef MAX_LOG_LENGTH #define MAX_LOG_LENGTH 8192 #endif /* MAX_LOG_LENGTH */ - const struct simInterface simlog_interface; /* Variables shared between COOJA and Contiki */ @@ -64,13 +61,14 @@ simlog_char(char c) void simlog(const char *message) { - if (simLoggedLength + strlen(message) > MAX_LOG_LENGTH) { + int message_len = strlen(message); + if(simLoggedLength + message_len > MAX_LOG_LENGTH) { /* Dropping message due to buffer overflow */ return; } - memcpy(simLoggedData + simLoggedLength, message, strlen(message)); - simLoggedLength += strlen(message); + memcpy(simLoggedData + simLoggedLength, message, message_len); + simLoggedLength += message_len; simLoggedFlag = 1; } /*-----------------------------------------------------------------------------------*/ @@ -98,9 +96,8 @@ log_set_putchar_with_slip(int with) log_putchar_with_slip = with; } /*-----------------------------------------------------------------------------------*/ -#if IMPLEMENT_PRINTF int -putchar(int c) +dbg_putchar(int c) { #define SLIP_END 0300 static char debug_frame = 0; @@ -124,41 +121,21 @@ putchar(int c) simlog_char(SLIP_END); debug_frame = 0; } - - return c; } else { simlog_char(c); - return c; } + return c; } /*-----------------------------------------------------------------------------------*/ -int -puts(const char* s) -{ - simlog(s); - simlog_char('\n'); - return 0; -} -/*-----------------------------------------------------------------------------------*/ -int -printf(const char *fmt, ...) +unsigned int +dbg_send_bytes(const unsigned char *s, unsigned int len) { - int res; - static char buf[MAX_LOG_LENGTH]; - va_list ap; - int i; - - va_start(ap, fmt); - res = vsnprintf(buf, MAX_LOG_LENGTH, fmt, ap); - va_end(ap); - - // simlog(buf); - for(i = 0; i < res; i++) { - putchar(buf[i]); + unsigned int i; + for(i = 0; i < len && s && *s != 0; i++) { + putchar(*s++); } - return res; + return i; } -#endif /* IMPLEMENT_PRINTF */ /*-----------------------------------------------------------------------------------*/ SIM_INTERFACE(simlog_interface, diff --git a/os/lib/dbg-io/snprintf.c b/os/lib/dbg-io/snprintf.c index 96f99e568abb2b20ef6b38e0c42f01e5254da826..e4ec35def23361c7a70da3e2e7a6492f862aa9f9 100644 --- a/os/lib/dbg-io/snprintf.c +++ b/os/lib/dbg-io/snprintf.c @@ -34,6 +34,8 @@ #include <stdio.h> #include <strformat.h> #include <string.h> +#undef snprintf +#undef vsnprintf /*---------------------------------------------------------------------------*/ struct fmt_buffer { char *pos; diff --git a/os/lib/dbg-io/sprintf.c b/os/lib/dbg-io/sprintf.c index 4ecf5b540a758f99da486dc8ae83ccde0219d3b2..cfdf98323a74bd1adcc87754357e50b92928cc7d 100644 --- a/os/lib/dbg-io/sprintf.c +++ b/os/lib/dbg-io/sprintf.c @@ -34,6 +34,7 @@ #include <stdio.h> #include <strformat.h> #include <string.h> +#undef sprintf /*---------------------------------------------------------------------------*/ static strformat_result buffer_str(void *user_data, const char *data, unsigned int len) diff --git a/os/lib/dbg-io/strformat.c b/os/lib/dbg-io/strformat.c index c8aace30d3badee6f15ec734e7286ba0bcb295fb..e6518da1fd5f08e19cca724ee9e4da4cdff0596d 100644 --- a/os/lib/dbg-io/strformat.c +++ b/os/lib/dbg-io/strformat.c @@ -31,6 +31,7 @@ /*---------------------------------------------------------------------------*/ #include "contiki.h" +#include <string.h> #include <strformat.h> /*---------------------------------------------------------------------------*/ #define HAVE_DOUBLE