testPlatform.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef RYANJSON_TEST_PLATFORM_H
  2. #define RYANJSON_TEST_PLATFORM_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stddef.h>
  7. #include <stdarg.h>
  8. #include <stdint.h>
  9. typedef void (*testPlatformThreadEntry_t)(void *arg);
  10. typedef int32_t (*testPlatformLogV_t)(const char *fmt, va_list args);
  11. typedef uint64_t (*testPlatformGetUptimeMs_t)(void);
  12. typedef void (*testPlatformSleepMs_t)(uint32_t ms);
  13. /* stackDepthWords 使用 FreeRTOS 栈单位(StackType_t 个数),不是字节。 */
  14. typedef int32_t (*testPlatformRunThreadWithStackSize_t)(testPlatformThreadEntry_t entry, void *arg, size_t stackDepthWords);
  15. typedef struct
  16. {
  17. testPlatformLogV_t logV;
  18. testPlatformGetUptimeMs_t getUptimeMs;
  19. testPlatformSleepMs_t sleepMs;
  20. testPlatformRunThreadWithStackSize_t runThreadWithStackSize;
  21. } testPlatformOps_t;
  22. // 平台函数表由 runner/main.c 提供默认实现;RTOS 可在启动时覆盖。
  23. extern testPlatformOps_t gTestPlatformOps;
  24. static inline void setTestPlatformOps(const testPlatformOps_t *ops)
  25. {
  26. if (NULL != ops) { gTestPlatformOps = *ops; }
  27. }
  28. static inline testPlatformOps_t *getTestPlatformOps(void)
  29. {
  30. return &gTestPlatformOps;
  31. }
  32. static inline int32_t testLog(const char *fmt, ...)
  33. {
  34. int32_t ret = 0;
  35. va_list args;
  36. testPlatformLogV_t logFunc = gTestPlatformOps.logV;
  37. if (NULL == logFunc || NULL == fmt) { return -1; }
  38. va_start(args, fmt);
  39. ret = logFunc(fmt, args);
  40. va_end(args);
  41. return ret;
  42. }
  43. static inline uint64_t testPlatformGetUptimeMs(void)
  44. {
  45. testPlatformGetUptimeMs_t getUptimeMsFunc = gTestPlatformOps.getUptimeMs;
  46. if (NULL == getUptimeMsFunc) { return 0U; }
  47. return getUptimeMsFunc();
  48. }
  49. static inline void testPlatformSleepMs(uint32_t ms)
  50. {
  51. testPlatformSleepMs_t sleepMsFunc = gTestPlatformOps.sleepMs;
  52. if (NULL == sleepMsFunc) { return; }
  53. sleepMsFunc(ms);
  54. }
  55. static inline int32_t testPlatformRunThreadWithStackSize(testPlatformThreadEntry_t entry, void *arg, size_t stackDepthWords)
  56. {
  57. testPlatformRunThreadWithStackSize_t runThreadFunc = gTestPlatformOps.runThreadWithStackSize;
  58. if (NULL == runThreadFunc || NULL == entry) { return -1; }
  59. return runThreadFunc(entry, arg, stackDepthWords);
  60. }
  61. #ifdef __cplusplus
  62. }
  63. #endif
  64. #endif