2018-01-05 02:19:51 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) Siddharth Bharat Purohit 2017
|
|
|
|
* This file is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This file is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
wrappers for stdio functions
|
|
|
|
|
|
|
|
Relies on linker wrap options
|
|
|
|
|
|
|
|
Note that not all functions that have been wrapped are implemented
|
|
|
|
here. The others are wrapped to ensure the function is not used
|
|
|
|
without an implementation. If we need them then we can implement as
|
|
|
|
needed.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <hal.h>
|
|
|
|
#include <chprintf.h>
|
2018-02-07 03:22:33 -04:00
|
|
|
#include <ctype.h>
|
2018-09-28 12:29:50 -03:00
|
|
|
#include "hwdef/common/stdio.h"
|
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2019-12-26 20:45:03 -04:00
|
|
|
#if HAL_USE_SERIAL_USB == TRUE
|
|
|
|
#include <AP_HAL_ChibiOS/hwdef/common/usbcfg.h>
|
2020-02-11 18:50:18 -04:00
|
|
|
#include "UARTDriver.h"
|
2019-12-26 20:45:03 -04:00
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2020-07-30 14:50:57 -03:00
|
|
|
#ifndef HAL_BOOTLOADER_BUILD
|
2018-09-28 12:29:50 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2020-07-30 14:50:57 -03:00
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
|
2018-09-18 17:04:35 -03:00
|
|
|
int __wrap_snprintf(char *str, size_t size, const char *fmt, ...)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
va_list arg;
|
|
|
|
int done;
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
va_start (arg, fmt);
|
2020-07-30 14:50:57 -03:00
|
|
|
#ifdef HAL_BOOTLOADER_BUILD
|
|
|
|
done = chvsnprintf(str, size, fmt, arg);
|
|
|
|
#else
|
2019-08-21 18:25:54 -03:00
|
|
|
done = hal.util->vsnprintf(str, size, fmt, arg);
|
2020-07-30 14:50:57 -03:00
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
va_end (arg);
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
return done;
|
|
|
|
}
|
|
|
|
|
2019-09-08 07:54:35 -03:00
|
|
|
int __wrap_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
|
|
|
|
{
|
2019-10-19 17:04:40 -03:00
|
|
|
#ifdef HAL_BOOTLOADER_BUILD
|
|
|
|
return chvsnprintf(str, size, fmt, ap);
|
|
|
|
#else
|
|
|
|
return hal.util->vsnprintf(str, size, fmt, ap);
|
|
|
|
#endif
|
2019-09-08 07:54:35 -03:00
|
|
|
}
|
|
|
|
|
2019-09-08 17:31:37 -03:00
|
|
|
int __wrap_vasprintf(char **strp, const char *fmt, va_list ap)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
int len = vsnprintf(NULL, 0, fmt, ap);
|
|
|
|
if (len <= 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2018-09-28 12:29:50 -03:00
|
|
|
char *buf = (char*)calloc(len+1, 1);
|
2018-01-05 02:19:51 -04:00
|
|
|
if (!buf) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
vsnprintf(buf, len+1, fmt, ap);
|
|
|
|
(*strp) = buf;
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-09-08 17:31:37 -03:00
|
|
|
int __wrap_asprintf(char **strp, const char *fmt, ...)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start(ap, fmt);
|
|
|
|
int ret = vasprintf(strp, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-09-08 17:31:37 -03:00
|
|
|
int __wrap_vprintf(const char *fmt, va_list arg)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-03-01 20:46:30 -04:00
|
|
|
#ifdef HAL_STDOUT_SERIAL
|
2019-12-26 20:45:03 -04:00
|
|
|
return chvprintf((BaseSequentialStream*)&HAL_STDOUT_SERIAL, fmt, arg);
|
2020-07-30 14:50:57 -03:00
|
|
|
#elif HAL_USE_SERIAL_USB == TRUE && !defined(HAL_BOOTLOADER_BUILD)
|
2020-02-11 18:50:18 -04:00
|
|
|
usb_initialise();
|
2019-12-26 20:45:03 -04:00
|
|
|
return chvprintf((BaseSequentialStream*)&SDU1, fmt, arg);
|
2018-03-01 20:46:30 -04:00
|
|
|
#else
|
|
|
|
(void)arg;
|
|
|
|
return strlen(fmt);
|
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2018-05-26 02:21:24 -03:00
|
|
|
// hook to allow for printf() on systems without HAL_STDOUT_SERIAL
|
|
|
|
int (*vprintf_console_hook)(const char *fmt, va_list arg) = vprintf;
|
|
|
|
|
2019-09-08 17:31:37 -03:00
|
|
|
int __wrap_printf(const char *fmt, ...)
|
2018-01-05 02:19:51 -04:00
|
|
|
{
|
2018-06-25 00:38:58 -03:00
|
|
|
#ifndef HAL_NO_PRINTF
|
2018-01-05 02:19:51 -04:00
|
|
|
va_list arg;
|
|
|
|
int done;
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
va_start (arg, fmt);
|
2018-05-26 02:21:24 -03:00
|
|
|
done = vprintf_console_hook(fmt, arg);
|
2018-01-05 02:19:51 -04:00
|
|
|
va_end (arg);
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2018-01-05 02:19:51 -04:00
|
|
|
return done;
|
2018-06-25 00:38:58 -03:00
|
|
|
#else
|
2018-06-25 01:10:06 -03:00
|
|
|
(void)fmt;
|
2018-06-25 00:38:58 -03:00
|
|
|
return 0;
|
|
|
|
#endif
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|
|
|
|
|
2019-09-08 17:31:37 -03:00
|
|
|
/*
|
|
|
|
we assume stdout or stderr. For output to files use the AP_Fileystem
|
|
|
|
posix_compat headers
|
|
|
|
*/
|
|
|
|
int __wrap_fprintf(void *f, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
#ifndef HAL_NO_PRINTF
|
|
|
|
va_list arg;
|
|
|
|
int done;
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2019-09-08 17:31:37 -03:00
|
|
|
va_start (arg, fmt);
|
|
|
|
done = vprintf_console_hook(fmt, arg);
|
|
|
|
va_end (arg);
|
2019-10-20 10:31:12 -03:00
|
|
|
|
2019-09-08 17:31:37 -03:00
|
|
|
return done;
|
|
|
|
#else
|
|
|
|
(void)fmt;
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
//just a stub for scanf
|
|
|
|
int __wrap_scanf(const char *fmt, ...)
|
2018-02-01 15:31:44 -04:00
|
|
|
{
|
2018-02-07 01:46:13 -04:00
|
|
|
(void)fmt;
|
2018-02-01 15:31:44 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2019-09-08 17:31:37 -03:00
|
|
|
|
|
|
|
extern "C" {
|
2020-09-20 20:46:31 -03:00
|
|
|
// empty function fiprintf(), saves flash space for unused code path
|
|
|
|
int __wrap_fiprintf(const char *fmt, ...);
|
|
|
|
int __wrap_fiprintf(const char *fmt, ...) { return -1; }
|
2018-01-05 02:19:51 -04:00
|
|
|
}
|