nrf_fprintf.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_FPRINTF_H__
  41. #define NRF_FPRINTF_H__
  42. #include <stdbool.h>
  43. #include <stddef.h>
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. typedef void (* nrf_fprintf_fwrite)(void const * p_user_ctx, char const * p_str, size_t length);
  48. /**
  49. * @brief fprintf context
  50. */
  51. typedef struct nrf_fprintf_ctx
  52. {
  53. char * const p_io_buffer; ///< Pointer to IO buffer.
  54. size_t const io_buffer_size; ///< IO buffer size.
  55. size_t io_buffer_cnt; ///< IO buffer usage.
  56. bool auto_flush; ///< Auto flush configurator.
  57. void const * const p_user_ctx; ///< Pointer to user data to be passed to the fwrite funciton.
  58. nrf_fprintf_fwrite fwrite; ///< Pointer to function sending data stream.
  59. } nrf_fprintf_ctx_t;
  60. /**
  61. * @brief Macro for defining nrf_fprintf instance.
  62. *
  63. * @param name Instance name.
  64. * @param _p_user_ctx Pointer to user data.
  65. * @param _p_io_buffer Pointer to IO buffer
  66. * @param _io_buffer_size Size of IO buffer.
  67. * @param _auto_flush Indicator if IO buffer shall be automatically flush.
  68. * @param _fwrite Pointer to function sending data stream.
  69. * */
  70. #define NRF_FPRINTF_DEF(name, _p_user_ctx, _p_io_buffer, _io_buffer_size, _auto_flush, _fwrite) \
  71. static nrf_fprintf_ctx_t name = \
  72. { \
  73. .p_io_buffer = _p_io_buffer, \
  74. .io_buffer_size = _io_buffer_size, \
  75. .io_buffer_cnt = 0, \
  76. .auto_flush = _auto_flush, \
  77. .p_user_ctx = _p_user_ctx, \
  78. .fwrite = _fwrite \
  79. }
  80. /**
  81. * @brief fprintf like function which send formated data stream to output specified by user
  82. * @ref nrf_fprintf_ctx_t
  83. *
  84. * @param p_ctx fprintf context.
  85. * @param p_fmt Format string.
  86. * @param ... List of parameters to print.
  87. * */
  88. void nrf_fprintf(nrf_fprintf_ctx_t * const p_ctx,
  89. char const * p_fmt,
  90. ...);
  91. /**
  92. * @brief function flushing data stored in io_buffer @ref nrf_fprintf_ctx_t
  93. *
  94. * @param p_ctx fprintf context
  95. */
  96. void nrf_fprintf_buffer_flush(nrf_fprintf_ctx_t * const p_ctx);
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif /* NRF_FPRINTF_H__ */