Util.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #pragma once
  2. #include <AP_Common/AP_Common.h>
  3. #include <AP_HAL/AP_HAL.h>
  4. #include "Heat.h"
  5. #include "Perf.h"
  6. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
  7. #include "ToneAlarm_Disco.h"
  8. #endif
  9. #include "ToneAlarm.h"
  10. #include "Semaphores.h"
  11. namespace Linux {
  12. enum hw_type {
  13. UTIL_HARDWARE_RPI1 = 0,
  14. UTIL_HARDWARE_RPI2,
  15. UTIL_HARDWARE_RPI4,
  16. UTIL_HARDWARE_BEBOP,
  17. UTIL_HARDWARE_BEBOP2,
  18. UTIL_HARDWARE_DISCO,
  19. UTIL_NUM_HARDWARES,
  20. };
  21. class Util : public AP_HAL::Util {
  22. public:
  23. static Util *from(AP_HAL::Util *util) {
  24. return static_cast<Util*>(util);
  25. }
  26. void init(int argc, char *const *argv);
  27. bool run_debug_shell(AP_HAL::BetterStream *stream) override { return false; }
  28. /**
  29. return commandline arguments, if available
  30. */
  31. void commandline_arguments(uint8_t &argc, char * const *&argv) override;
  32. /*
  33. set system clock in UTC microseconds
  34. */
  35. void set_hw_rtc(uint64_t time_utc_usec) override;
  36. const char *get_custom_log_directory() const override final { return custom_log_directory; }
  37. const char *get_custom_terrain_directory() const override final { return custom_terrain_directory; }
  38. const char *get_custom_storage_directory() const override final { return custom_storage_directory; }
  39. void set_custom_log_directory(const char *_custom_log_directory) { custom_log_directory = _custom_log_directory; }
  40. void set_custom_terrain_directory(const char *_custom_terrain_directory) { custom_terrain_directory = _custom_terrain_directory; }
  41. void set_custom_storage_directory(const char *_custom_storage_directory) {
  42. custom_storage_directory = _custom_storage_directory;
  43. }
  44. bool is_chardev_node(const char *path);
  45. void set_imu_temp(float current) override;
  46. void set_imu_target_temp(int8_t *target) override;
  47. uint32_t available_memory(void) override;
  48. bool get_system_id(char buf[40]) override;
  49. bool get_system_id_unformatted(uint8_t buf[], uint8_t &len) override;
  50. #ifdef ENABLE_HEAP
  51. // heap functions, note that a heap once alloc'd cannot be dealloc'd
  52. virtual void *allocate_heap_memory(size_t size) override;
  53. virtual void *heap_realloc(void *h, void *ptr, size_t new_size) override;
  54. #endif // ENABLE_HEAP
  55. /*
  56. * Write a string as specified by @fmt to the file in @path. Note this
  57. * should not be used on hot path since it will open, write and close the
  58. * file for each call.
  59. */
  60. int write_file(const char *path, const char *fmt, ...) FMT_PRINTF(3, 4);
  61. /*
  62. * Read a string as specified by @fmt from the file in @path. Note this
  63. * should not be used on hot path since it will open, read and close the
  64. * file for each call.
  65. */
  66. int read_file(const char *path, const char *fmt, ...) FMT_SCANF(3, 4);
  67. perf_counter_t perf_alloc(enum perf_counter_type t, const char *name) override
  68. {
  69. return Perf::get_singleton()->add(t, name);
  70. }
  71. void perf_begin(perf_counter_t perf) override
  72. {
  73. return Perf::get_singleton()->begin(perf);
  74. }
  75. void perf_end(perf_counter_t perf) override
  76. {
  77. return Perf::get_singleton()->end(perf);
  78. }
  79. void perf_count(perf_counter_t perf) override
  80. {
  81. return Perf::get_singleton()->count(perf);
  82. }
  83. int get_hw_arm32();
  84. bool toneAlarm_init() override { return _toneAlarm.init(); }
  85. void toneAlarm_set_buzzer_tone(float frequency, float volume, uint32_t duration_ms) override {
  86. _toneAlarm.set_buzzer_tone(frequency, volume, duration_ms);
  87. }
  88. private:
  89. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
  90. static ToneAlarm_Disco _toneAlarm;
  91. #else
  92. static ToneAlarm _toneAlarm;
  93. #endif
  94. int saved_argc;
  95. Heat *_heat;
  96. char *const *saved_argv;
  97. const char *custom_log_directory = nullptr;
  98. const char *custom_terrain_directory = nullptr;
  99. const char *custom_storage_directory = nullptr;
  100. static const char *_hw_names[UTIL_NUM_HARDWARES];
  101. #ifdef ENABLE_HEAP
  102. struct heap_allocation_header {
  103. size_t allocation_size; // size of allocated block, not including this header
  104. };
  105. struct heap {
  106. size_t max_heap_size;
  107. size_t current_heap_usage;
  108. };
  109. #endif // ENABLE_HEAP
  110. };
  111. }