stdio.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (C) Siddharth Bharat Purohit 2017
  3. * This file is free software: you can redistribute it and/or modify it
  4. * under the terms of the GNU General Public License as published by the
  5. * Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This file is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along
  14. * with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. wrappers for stdio functions
  18. Relies on linker wrap options
  19. Note that not all functions that have been wrapped are implemented
  20. here. The others are wrapped to ensure the function is not used
  21. without an implementation. If we need them then we can implement as
  22. needed.
  23. */
  24. #include <string.h>
  25. #include <hal.h>
  26. #include <chprintf.h>
  27. #include <ctype.h>
  28. #include "hwdef/common/stdio.h"
  29. #include <AP_HAL/AP_HAL.h>
  30. extern const AP_HAL::HAL& hal;
  31. int __wrap_snprintf(char *str, size_t size, const char *fmt, ...)
  32. {
  33. va_list arg;
  34. int done;
  35. va_start (arg, fmt);
  36. done = hal.util->vsnprintf(str, size, fmt, arg);
  37. va_end (arg);
  38. return done;
  39. }
  40. int __wrap_vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
  41. {
  42. return hal.util->vsnprintf(str, size, fmt, ap);
  43. }
  44. int __wrap_vasprintf(char **strp, const char *fmt, va_list ap)
  45. {
  46. int len = vsnprintf(NULL, 0, fmt, ap);
  47. if (len <= 0) {
  48. return -1;
  49. }
  50. char *buf = (char*)calloc(len+1, 1);
  51. if (!buf) {
  52. return -1;
  53. }
  54. vsnprintf(buf, len+1, fmt, ap);
  55. (*strp) = buf;
  56. return len;
  57. }
  58. int __wrap_asprintf(char **strp, const char *fmt, ...)
  59. {
  60. va_list ap;
  61. va_start(ap, fmt);
  62. int ret = vasprintf(strp, fmt, ap);
  63. va_end(ap);
  64. return ret;
  65. }
  66. int __wrap_vprintf(const char *fmt, va_list arg)
  67. {
  68. #ifdef HAL_STDOUT_SERIAL
  69. return chvprintf ((BaseSequentialStream*)&HAL_STDOUT_SERIAL, fmt, arg);
  70. #else
  71. (void)arg;
  72. return strlen(fmt);
  73. #endif
  74. }
  75. // hook to allow for printf() on systems without HAL_STDOUT_SERIAL
  76. int (*vprintf_console_hook)(const char *fmt, va_list arg) = vprintf;
  77. int __wrap_printf(const char *fmt, ...)
  78. {
  79. #ifndef HAL_NO_PRINTF
  80. va_list arg;
  81. int done;
  82. va_start (arg, fmt);
  83. done = vprintf_console_hook(fmt, arg);
  84. va_end (arg);
  85. return done;
  86. #else
  87. (void)fmt;
  88. return 0;
  89. #endif
  90. }
  91. /*
  92. we assume stdout or stderr. For output to files use the AP_Fileystem
  93. posix_compat headers
  94. */
  95. int __wrap_fprintf(void *f, const char *fmt, ...)
  96. {
  97. #ifndef HAL_NO_PRINTF
  98. va_list arg;
  99. int done;
  100. va_start (arg, fmt);
  101. done = vprintf_console_hook(fmt, arg);
  102. va_end (arg);
  103. return done;
  104. #else
  105. (void)fmt;
  106. return 0;
  107. #endif
  108. }
  109. //just a stub for scanf
  110. int __wrap_scanf(const char *fmt, ...)
  111. {
  112. (void)fmt;
  113. return 0;
  114. }
  115. extern "C" {
  116. // alias fiprintf() to fprintf(). This saves flash space
  117. int __wrap_fiprintf(const char *fmt, ...) __attribute__((alias("__wrap_fprintf")));
  118. }