log.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef LOG_H_
  2. #define LOG_H_
  3. #include "util.h"
  4. #include "datetime.h"
  5. /* Log message types. */
  6. enum log_type_flags {
  7. /* Types */
  8. LOG_ERROR,
  9. LOG_INFO,
  10. LOG_SENSOR_DATA,
  11. LOG_TYPE_MASK = 0x7F,
  12. LOG_FLAGS_MASK = 0x80,
  13. /* Overflow flag. */
  14. LOG_OVERFLOW = 0x80,
  15. };
  16. enum log_error {
  17. LOG_ERR_SENSOR, /* Sensor short circuit. */
  18. };
  19. enum log_info {
  20. LOG_INFO_DEBUG, /* Generic debug message. */
  21. LOG_INFO_CONTSTATCHG, /* Controller status change */
  22. LOG_INFO_WATERINGCHG, /* The "watering" state changed. */
  23. };
  24. /* Construct a 'sensor_data' field. */
  25. #define LOG_SENSOR_DATA(sensor_nr, value) \
  26. (((sensor_nr) << 10) | ((value) & 0x3FF))
  27. /* Log message item. */
  28. struct log_item {
  29. /* Log message type and flags. */
  30. uint8_t type_flags;
  31. /* Timestamp of the event occurrence. */
  32. timestamp_t time;
  33. union {
  34. /* LOG_ERROR and LOG_INFO */
  35. struct {
  36. /* Error/info code. */
  37. uint8_t code;
  38. /* Error/info payload. */
  39. uint8_t data;
  40. } _packed;
  41. /* LOG_SENSOR_DATA */
  42. struct {
  43. /* Sensor number and value combined.
  44. * See LOG_SENSOR_DATA() macro for
  45. * creating this field. */
  46. uint16_t sensor_data;
  47. } _packed;
  48. } _packed;
  49. } _packed;
  50. void log_init(struct log_item *item, uint8_t type);
  51. void log_append(const struct log_item *item);
  52. bool log_pop(struct log_item *item);
  53. static inline void log_debug(uint8_t data)
  54. {
  55. struct log_item item;
  56. log_init(&item, LOG_INFO);
  57. item.code = LOG_INFO_DEBUG;
  58. item.data = data;
  59. log_append(&item);
  60. }
  61. #endif /* LOG_H_ */