fassert.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: fassert.c
  15. * Date: 2021-04-07 09:53:07
  16. * LastEditTime: 2022-02-17 18:04:28
  17. * Description:  This file is for assertion implmentation.
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 huanghe 2021/4/5 init commit
  23. * 1.1 zhugengyu 2022/3/7 re-define assert macro
  24. */
  25. /***************************** Include Files *********************************/
  26. #include "ftypes.h"
  27. #include "fassert.h"
  28. /************************** Constant Definitions *****************************/
  29. /**************************** Type Definitions *******************************/
  30. typedef struct
  31. {
  32. u32 status; /* 当前断言状态 */
  33. FAssertCB cb; /* 断言回调函数 */
  34. } FAssertInfo; /* 断言实例类型 */
  35. /***************** Macros (Inline Functions) Definitions *********************/
  36. /************************** Function Prototypes ******************************/
  37. static void FAssertCallback(const char *file, s32 line, int ret);
  38. /************************** Variable Definitions *****************************/
  39. static FAssertInfo assert_info =
  40. {
  41. .status = FASSERT_NONE,
  42. .cb = FAssertCallback
  43. }; /* 断言实例 */
  44. /*****************************************************************************/
  45. /**
  46. * @name: FAssertSetStatus
  47. * @msg: 设置断言状态
  48. * @return {*}
  49. * @param {FAssertStatus} status, 断言状态
  50. */
  51. void FAssertSetStatus(FAssertStatus status)
  52. {
  53. assert_info.status = status;
  54. }
  55. /**
  56. * @name: FAssertGetStatus
  57. * @msg: 获取当前断言状态
  58. * @return {FAssertStatus} 当前断言状态
  59. */
  60. FAssertStatus FAssertGetStatus(void)
  61. {
  62. return assert_info.status;
  63. }
  64. /**
  65. * @name: FAssertCallback
  66. * @msg: 默认的断言回调函数
  67. * @return {*}
  68. * @param {char} *file, 断言发生的源文件
  69. * @param {s32} line, 断言发生的源文件行号
  70. * @param {int} ret, 保留给Non-block断言使用
  71. */
  72. static void FAssertCallback(const char *file, s32 line, int ret)
  73. {
  74. f_printk("Assert Error at %s : %ld \r\n", file, line);
  75. }
  76. /**
  77. * @name: FAssertSetCB
  78. * @msg: 设置断言回调函数
  79. * @return {*}
  80. * @param {FAssertCB} cb, 断言回调函数
  81. */
  82. void FAssertSetCB(FAssertCB cb)
  83. {
  84. if (NULL != cb)
  85. {
  86. assert_info.cb = cb;
  87. }
  88. }
  89. /**
  90. * @name: FAssert
  91. * @msg: 断言实现
  92. * @return {*}
  93. * @param {char} *file, 断言发生的源文件
  94. * @param {s32} line, 断言发生的源文件行号
  95. * @param {int} code, 断言发生的退出码,保留给Non-block断言使用
  96. */
  97. void FAssert(const char *file, s32 line, int code)
  98. {
  99. if (NULL != assert_info.cb)
  100. {
  101. /* 如果要实现Non-block断言,需要在回调中返回 */
  102. assert_info.cb(file, line, code);
  103. }
  104. while (TRUE)
  105. {
  106. ;
  107. }
  108. }