Util.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "Util.h"
  2. #include <sys/time.h>
  3. #ifdef WITH_SITL_TONEALARM
  4. HALSITL::ToneAlarm_SF HALSITL::Util::_toneAlarm;
  5. #endif
  6. uint64_t HALSITL::Util::get_hw_rtc() const
  7. {
  8. #ifndef CLOCK_REALTIME
  9. struct timeval ts;
  10. gettimeofday(&ts, nullptr);
  11. return ((long long)((ts.tv_sec * 1000000) + ts.tv_usec));
  12. #else
  13. struct timespec ts;
  14. clock_gettime(CLOCK_REALTIME, &ts);
  15. const uint64_t seconds = ts.tv_sec;
  16. const uint64_t nanoseconds = ts.tv_nsec;
  17. return (seconds * 1000000ULL + nanoseconds/1000ULL);
  18. #endif
  19. }
  20. /*
  21. get a (hopefully unique) machine ID
  22. */
  23. bool HALSITL::Util::get_system_id_unformatted(uint8_t buf[], uint8_t &len)
  24. {
  25. char *cbuf = (char *)buf;
  26. // try first to use machine-id file. Most systems will have this
  27. const char *paths[] = { "/etc/machine-id", "/var/lib/dbus/machine-id" };
  28. for (uint8_t i=0; i<ARRAY_SIZE(paths); i++) {
  29. int fd = open(paths[i], O_RDONLY);
  30. if (fd == -1) {
  31. continue;
  32. }
  33. ssize_t ret = read(fd, buf, len);
  34. close(fd);
  35. if (ret <= 0) {
  36. continue;
  37. }
  38. len = ret;
  39. char *p = strchr(cbuf, '\n');
  40. if (p) {
  41. *p = 0;
  42. }
  43. len = strnlen(cbuf, len);
  44. return true;
  45. }
  46. // fallback to hostname
  47. if (gethostname(cbuf, len) != 0) {
  48. // use a default name so this always succeeds. Without it we can't
  49. // implement some features (such as UAVCAN)
  50. strncpy(cbuf, "sitl-unknown", len);
  51. }
  52. len = strnlen(cbuf, len);
  53. return true;
  54. }
  55. /*
  56. as get_system_id_unformatted will already be ascii, we use the same
  57. ID here
  58. */
  59. bool HALSITL::Util::get_system_id(char buf[40])
  60. {
  61. uint8_t len = 40;
  62. return get_system_id_unformatted((uint8_t *)buf, len);
  63. }
  64. #ifdef ENABLE_HEAP
  65. void *HALSITL::Util::allocate_heap_memory(size_t size)
  66. {
  67. struct heap *new_heap = (struct heap*)malloc(sizeof(struct heap));
  68. if (new_heap != nullptr) {
  69. new_heap->scripting_max_heap_size = size;
  70. new_heap->current_heap_usage = 0;
  71. }
  72. return (void *)new_heap;
  73. }
  74. void *HALSITL::Util::heap_realloc(void *heap_ptr, void *ptr, size_t new_size)
  75. {
  76. if (heap_ptr == nullptr) {
  77. return nullptr;
  78. }
  79. struct heap *heapp = (struct heap*)heap_ptr;
  80. // extract appropriate headers
  81. size_t old_size = 0;
  82. heap_allocation_header *old_header = nullptr;
  83. if (ptr != nullptr) {
  84. old_header = ((heap_allocation_header *)ptr) - 1;
  85. old_size = old_header->allocation_size;
  86. }
  87. if ((heapp->current_heap_usage + new_size - old_size) > heapp->scripting_max_heap_size) {
  88. // fail the allocation as we don't have the memory. Note that we don't simulate fragmentation
  89. return nullptr;
  90. }
  91. heapp->current_heap_usage -= old_size;
  92. if (new_size == 0) {
  93. free(old_header);
  94. return nullptr;
  95. }
  96. heap_allocation_header *new_header = (heap_allocation_header *)malloc(new_size + sizeof(heap_allocation_header));
  97. if (new_header == nullptr) {
  98. // total failure to allocate, this is very surprising in SITL
  99. return nullptr;
  100. }
  101. heapp->current_heap_usage += new_size;
  102. new_header->allocation_size = new_size;
  103. void *new_mem = new_header + 1;
  104. if (ptr == nullptr) {
  105. return new_mem;
  106. }
  107. memcpy(new_mem, ptr, old_size > new_size ? new_size : old_size);
  108. free(old_header);
  109. return new_mem;
  110. }
  111. #endif // ENABLE_HEAP