#include #include #include #include #include #include #include #include #include #include "Heat_Pwm.h" #include "ToneAlarm_Disco.h" #include "Util.h" using namespace Linux; extern const AP_HAL::HAL& hal; #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO ToneAlarm_Disco Util::_toneAlarm; #else ToneAlarm Util::_toneAlarm; #endif void Util::init(int argc, char * const *argv) { saved_argc = argc; saved_argv = argv; #ifdef HAL_UTILS_HEAT #if HAL_UTILS_HEAT == HAL_LINUX_HEAT_PWM _heat = NEW_NOTHROW Linux::HeatPwm(HAL_LINUX_HEAT_PWM_NUM, HAL_LINUX_HEAT_KP, HAL_LINUX_HEAT_KI, HAL_LINUX_HEAT_PERIOD_NS); #else #error Unrecognized Heat #endif // #if #else _heat = NEW_NOTHROW Linux::Heat(); #endif // #ifdef } // set current IMU temperatue in degrees C void Util::set_imu_temp(float current) { _heat->set_imu_temp(current); } // set target IMU temperatue in degrees C void Util::set_imu_target_temp(int8_t *target) { _heat->set_imu_target_temp(target); } /** return commandline arguments, if available */ void Util::commandline_arguments(uint8_t &argc, char * const *&argv) { argc = saved_argc; argv = saved_argv; } uint64_t Util::get_hw_rtc() const { struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); const uint64_t seconds = ts.tv_sec; const uint64_t nanoseconds = ts.tv_nsec; return (seconds * 1000000ULL + nanoseconds/1000ULL); } void Util::set_hw_rtc(uint64_t time_utc_usec) { // don't reset the HW clock time on people's laptops. #if CONFIG_HAL_BOARD_SUBTYPE != HAL_BOARD_SUBTYPE_LINUX_NONE timespec ts; ts.tv_sec = time_utc_usec/1000000ULL; ts.tv_nsec = (time_utc_usec % 1000000ULL) * 1000ULL; clock_settime(CLOCK_REALTIME, &ts); #endif } bool Util::is_chardev_node(const char *path) { struct stat st; if (!path || lstat(path, &st) < 0) { return false; } return S_ISCHR(st.st_mode); } /* always report 256k of free memory. Using mallinfo() isn't useful as it only reported the current heap, which auto-expands. What we're trying to do here is ensure that code which checks for free memory before allocating objects does allow the allocation */ uint32_t Util::available_memory(void) { return 256*1024; } #ifndef HAL_LINUX_DEFAULT_SYSTEM_ID #define HAL_LINUX_DEFAULT_SYSTEM_ID "linux-unknown" #endif /* get a (hopefully unique) machine ID */ bool Util::get_system_id_unformatted(uint8_t buf[], uint8_t &len) { char *cbuf = (char *)buf; // try first to use machine-id file. Most systems will have this const char *paths[] = { "/etc/machine-id", "/var/lib/dbus/machine-id" }; for (uint8_t i=0; imax_heap_size = size; new_heap->current_heap_usage = 0; } return (void *)new_heap; } void *Util::heap_realloc(void *h, void *ptr, size_t old_size, size_t new_size) { if (h == nullptr) { return nullptr; } struct heap *heapp = (struct heap*)h; // extract appropriate headers. We use the old_size from the // header not from the caller. We use SITL to catch cases they // don't match (which would be a lua bug) old_size = 0; heap_allocation_header *old_header = nullptr; if (ptr != nullptr) { old_header = ((heap_allocation_header *)ptr) - 1; old_size = old_header->allocation_size; } if ((heapp->current_heap_usage + new_size - old_size) > heapp->max_heap_size) { // fail the allocation as we don't have the memory. Note that we don't simulate fragmentation return nullptr; } heapp->current_heap_usage -= old_size; if (new_size == 0) { free(old_header); return nullptr; } heap_allocation_header *new_header = (heap_allocation_header *)malloc(new_size + sizeof(heap_allocation_header)); if (new_header == nullptr) { // total failure to allocate, this is very surprising in SITL return nullptr; } heapp->current_heap_usage += new_size; new_header->allocation_size = new_size; void *new_mem = new_header + 1; if (ptr == nullptr) { return new_mem; } memcpy(new_mem, ptr, old_size > new_size ? new_size : old_size); free(old_header); return new_mem; } #endif // ENABLE_HEAP /** * This method will read random values with set size. */ bool Util::get_random_vals(uint8_t* data, size_t size) { int dev_random = open("/dev/urandom", O_RDONLY); if (dev_random < 0) { return false; } ssize_t result = read(dev_random, data, size); if (result < 0) { close(dev_random); return false; } close(dev_random); return true; } bool Util::parse_cpu_set(const char *str, cpu_set_t *cpu_set) const { unsigned long cpu1, cpu2; char *endptr, sep; CPU_ZERO(cpu_set); do { cpu1 = strtoul(str, &endptr, 10); if (str == endptr) { return false; } str = endptr + 1; sep = *endptr; if (sep == ',' || sep == '\0') { CPU_SET(cpu1, cpu_set); continue; } if (sep != '-') { return false; } cpu2 = strtoul(str, &endptr, 10); if (str == endptr) { return false; } str = endptr + 1; for (; cpu1 <= cpu2; cpu1++) { CPU_SET(cpu1, cpu_set); } } while (*endptr != '\0'); return true; }