#include #include #include #include #include #include #include #include #include #include "Heat_Pwm.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; i