tracing_backend.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) 2019 Intel corporation
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #ifndef _TRACE_BACKEND_H
  7. #define _TRACE_BACKEND_H
  8. #include <string.h>
  9. #include <sys/util.h>
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. /**
  14. * @brief Tracing backend
  15. * @defgroup Tracing_backend Tracing backend
  16. * @{
  17. * @}
  18. */
  19. struct tracing_backend;
  20. /**
  21. * @brief Tracing backend API.
  22. */
  23. struct tracing_backend_api {
  24. void (*init)(void);
  25. void (*output)(const struct tracing_backend *backend,
  26. uint8_t *data, uint32_t length);
  27. };
  28. /**
  29. * @brief Tracing backend structure.
  30. */
  31. struct tracing_backend {
  32. const char *name;
  33. const struct tracing_backend_api *api;
  34. };
  35. /**
  36. * @brief Create tracing_backend instance.
  37. *
  38. * @param _name Instance name.
  39. * @param _api Tracing backend API.
  40. */
  41. #define TRACING_BACKEND_DEFINE(_name, _api) \
  42. static const STRUCT_SECTION_ITERABLE(tracing_backend, _name) = { \
  43. .name = STRINGIFY(_name), \
  44. .api = &_api \
  45. }
  46. /**
  47. * @brief Initialize tracing backend.
  48. *
  49. * @param backend Pointer to tracing_backend instance.
  50. */
  51. static inline void tracing_backend_init(
  52. const struct tracing_backend *backend)
  53. {
  54. if (backend && backend->api && backend->api->init) {
  55. backend->api->init();
  56. }
  57. }
  58. /**
  59. * @brief Output tracing packet with tracing backend.
  60. *
  61. * @param backend Pointer to tracing_backend instance.
  62. * @param data Address of outputing buffer.
  63. * @param length Length of outputing buffer.
  64. */
  65. static inline void tracing_backend_output(
  66. const struct tracing_backend *backend,
  67. uint8_t *data, uint32_t length)
  68. {
  69. if (backend && backend->api) {
  70. backend->api->output(backend, data, length);
  71. }
  72. }
  73. /**
  74. * @brief Get tracing backend based on the name of
  75. * tracing backend in tracing backend section.
  76. *
  77. * @param name Name of wanted tracing backend.
  78. *
  79. * @return Pointer of the wanted backend or NULL.
  80. */
  81. static inline struct tracing_backend *tracing_backend_get(char *name)
  82. {
  83. STRUCT_SECTION_FOREACH(tracing_backend, backend) {
  84. if (strcmp(backend->name, name) == 0) {
  85. return backend;
  86. }
  87. }
  88. return NULL;
  89. }
  90. #ifdef __cplusplus
  91. }
  92. #endif
  93. #endif