util.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  19. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  20. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  22. * SOFTWARE.
  23. */
  24. #ifndef TRUSTY_UTIL_H_
  25. #define TRUSTY_UTIL_H_
  26. #include "trusty/sysdeps.h"
  27. /* Returns the basename of |str|. This is defined as the last path
  28. * component, assuming the normal POSIX separator '/'. If there are no
  29. * separators, returns |str|.
  30. */
  31. const char* trusty_basename(const char* str);
  32. bool is_power_of_2(unsigned long n);
  33. #define MIN(x,y) ((x) < (y) ? (x) : (y))
  34. #define TRUSTY_STRINGIFY(x) #x
  35. #define TRUSTY_TO_STRING(x) TRUSTY_STRINGIFY(x)
  36. #define TRUSTY_STR "trusty driver:"
  37. #define TRUSTY_OS_STR "trusty os:"
  38. #define TRUSTY_TRCID 0
  39. #define TIPC_ENABLE_DEBUG
  40. /*
  41. * Aborts the program if @expr is false.
  42. *
  43. * This has no effect unless TIPC_ENABLE_DEBUG is defined.
  44. */
  45. #ifdef TIPC_ENABLE_DEBUG
  46. #define trusty_assert(expr) \
  47. do { \
  48. if (!(expr)) { \
  49. trusty_fatal(OSI_LOGPAR_S, "%s assert fail: " #expr "\n", TRUSTY_STR); \
  50. } \
  51. } while (0)
  52. #else
  53. #define trusty_assert(expr)
  54. #endif
  55. /*
  56. * Prints message and calls trusty_abort.
  57. */
  58. #define trusty_fatal(partype, fmt, ...) \
  59. do { \
  60. OSI_LOGXE(OSI_LOGPAR_SSS, TRUSTY_TRCID, "%s %s%s", \
  61. TRUSTY_STR, \
  62. trusty_basename(__FILE__), \
  63. ":"TRUSTY_TO_STRING(__LINE__) " FATAL "); \
  64. OSI_LOGXE(partype, TRUSTY_TRCID, fmt, ##__VA_ARGS__); \
  65. trusty_abort(); \
  66. } while (0)
  67. /*
  68. * Prints error message.
  69. */
  70. #define trusty_error(partype, fmt, ...) \
  71. do { \
  72. OSI_LOGXE(OSI_LOGPAR_SSS, TRUSTY_TRCID, "%s %s%s", \
  73. TRUSTY_STR, \
  74. trusty_basename(__FILE__), \
  75. ":"TRUSTY_TO_STRING(__LINE__) " ERROR "); \
  76. OSI_LOGXE(partype, TRUSTY_TRCID, fmt, ##__VA_ARGS__); \
  77. } while (0)
  78. /*
  79. * Prints info message.
  80. */
  81. #define trusty_info(partype, fmt, ...) \
  82. do { \
  83. OSI_LOGXI(OSI_LOGPAR_SSS, TRUSTY_TRCID, "%s %s%s", \
  84. TRUSTY_STR, \
  85. trusty_basename(__FILE__), \
  86. ":"TRUSTY_TO_STRING(__LINE__) " INFO "); \
  87. OSI_LOGXI(partype, TRUSTY_TRCID, fmt, ##__VA_ARGS__); \
  88. } while (0)
  89. #define trusty_os_info(partype, fmt, ...) \
  90. do { \
  91. OSI_LOGXI(partype, TRUSTY_TRCID, fmt, ##__VA_ARGS__); \
  92. } while (0)
  93. /*
  94. * Prints debug message.
  95. *
  96. * This has no effect unless TIPC_ENABLE_DEBUG and OSI_LOG_LEVEL_DEBUG is defined.
  97. */
  98. #ifdef TIPC_ENABLE_DEBUG
  99. #define trusty_debug(partype, fmt, ...) \
  100. do { \
  101. OSI_LOGXD(OSI_LOGPAR_SSS, TRUSTY_TRCID, "%s %s%s", \
  102. TRUSTY_STR, \
  103. trusty_basename(__FILE__), \
  104. ":"TRUSTY_TO_STRING(__LINE__) " DEBUG "); \
  105. OSI_LOGXD(partype, TRUSTY_TRCID, fmt, ##__VA_ARGS__); \
  106. } while (0)
  107. #else
  108. #define trusty_debug(partype, fmt, ...)
  109. #endif
  110. #endif /* TRUSTY_UTIL_H_ */