common.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #pragma once
  2. #define _GNU_SOURCE 1
  3. #include "compiler.h"
  4. /* Standard C headers */
  5. #include <inttypes.h>
  6. #include <stdarg.h>
  7. #include <stdbool.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. /* Arduino headers */
  12. #include <Arduino.h>
  13. #include <FreeRTOS.h>
  14. /* ESP-IDF headers */
  15. #include <sdkconfig.h>
  16. #include <esp_err.h>
  17. #include <esp_event.h>
  18. #include <esp_log.h>
  19. #include "xmalloc.h"
  20. #ifndef MODULE
  21. # define MODULE ""
  22. #endif
  23. #if DEBUG
  24. # define CMSG(...) printf(__VA_ARGS__)
  25. #else
  26. # define CMSG(...) ((void)0)
  27. #endif
  28. #define MSG(...) CMSG(MODULE ": " __VA_ARGS__)
  29. /*
  30. * Hack to deal with the lack of this information in FreeRTOS proper
  31. */
  32. #define EVENT_BITS (configUSE_16_BIT_TICKS ? 8 : 24)
  33. #define EVENT_ALL_BITS ((1U << EVENT_BITS)-1)
  34. /*
  35. * Common types for callbacks
  36. */
  37. typedef void *token_t;
  38. typedef int (*read_func_t)(token_t token, void *buf, size_t len);
  39. typedef int (*write_func_t)(token_t token, const void *buf, size_t len);
  40. /*
  41. * Sleep thread...
  42. */
  43. static inline void suspend(void)
  44. {
  45. vTaskSuspend(NULL);
  46. }
  47. /*
  48. * Kill thread; vTaskDelete(NULL) *should* never return...
  49. */
  50. static inline no_return exit_task(void)
  51. {
  52. vTaskDelete(NULL);
  53. while (1) {
  54. /* vTaskDelete() returned!? */
  55. vTaskSuspend(NULL);
  56. }
  57. }
  58. /*
  59. * Convert a NULL pointer string to ""
  60. */
  61. static inline const char *notempty(const char *str)
  62. {
  63. return str ? str : "";
  64. }
  65. static inline char *dupstr(const char *str)
  66. {
  67. return strdup(notempty(str));
  68. }
  69. /*
  70. * Reboot system
  71. */
  72. extern_c void reboot_now(void);
  73. extern_c int reboot_delayed(void);
  74. /*
  75. * Main MAC address from efuses
  76. */
  77. extern_c uint8_t efuse_default_mac[6];
  78. extern_c char serial_number[16];
  79. /*
  80. * Time sync status
  81. */
  82. extern_c volatile bool time_net_sync_status;
  83. struct timeval;
  84. extern_c void time_net_sync(const struct timeval *tv);
  85. extern_c void print_time(const char *msg, const struct timeval *tv);
  86. /*
  87. * Log output
  88. */
  89. extern_c volatile bool do_log_config_status;
  90. extern_c void __fmt_printf(2,3)
  91. logmsg(const char *module, const char *fmt, ...);