2016-05-17 23:26:57 -03:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2014-02-23 20:59:24 -04:00
|
|
|
#include <stdarg.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <stdio.h>
|
2014-02-23 20:59:24 -04:00
|
|
|
#include <stdlib.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <sys/stat.h>
|
2015-01-19 13:30:18 -04:00
|
|
|
#include <time.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <unistd.h>
|
2014-02-23 20:59:24 -04:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2014-02-23 20:59:24 -04:00
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include "Heat_Pwm.h"
|
2016-10-29 05:23:31 -03:00
|
|
|
#include "ToneAlarm_Disco.h"
|
2014-02-23 20:59:24 -04:00
|
|
|
#include "Util.h"
|
2015-09-25 11:07:59 -03:00
|
|
|
|
2014-02-23 20:59:24 -04:00
|
|
|
using namespace Linux;
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
extern const AP_HAL::HAL& hal;
|
2014-11-15 01:51:21 -04:00
|
|
|
|
2017-09-08 02:39:35 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
|
2016-10-29 05:23:31 -03:00
|
|
|
ToneAlarm_Disco Util::_toneAlarm;
|
2016-01-10 08:17:33 -04:00
|
|
|
#else
|
2015-10-20 18:13:25 -03:00
|
|
|
ToneAlarm Util::_toneAlarm;
|
2016-01-10 08:17:33 -04:00
|
|
|
#endif
|
2015-09-25 11:07:59 -03:00
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
void Util::init(int argc, char * const *argv) {
|
2015-09-25 11:07:59 -03:00
|
|
|
saved_argc = argc;
|
|
|
|
saved_argv = argv;
|
|
|
|
|
|
|
|
#ifdef HAL_UTILS_HEAT
|
|
|
|
#if HAL_UTILS_HEAT == HAL_LINUX_HEAT_PWM
|
2015-11-27 06:36:00 -04:00
|
|
|
_heat = new Linux::HeatPwm(HAL_LINUX_HEAT_PWM_NUM,
|
2016-06-15 05:01:14 -03:00
|
|
|
HAL_LINUX_HEAT_KP,
|
|
|
|
HAL_LINUX_HEAT_KI,
|
|
|
|
HAL_LINUX_HEAT_PERIOD_NS);
|
2015-09-25 11:07:59 -03:00
|
|
|
#else
|
|
|
|
#error Unrecognized Heat
|
|
|
|
#endif // #if
|
|
|
|
#else
|
2015-10-20 18:13:25 -03:00
|
|
|
_heat = new Linux::Heat();
|
2015-09-25 11:07:59 -03:00
|
|
|
#endif // #ifdef
|
|
|
|
}
|
|
|
|
|
2016-06-15 05:01:14 -03:00
|
|
|
// set current IMU temperatue in degrees C
|
2015-10-20 18:13:25 -03:00
|
|
|
void Util::set_imu_temp(float current)
|
2015-09-25 11:07:59 -03:00
|
|
|
{
|
|
|
|
_heat->set_imu_temp(current);
|
2016-06-15 05:01:14 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
// set target IMU temperatue in degrees C
|
|
|
|
void Util::set_imu_target_temp(int8_t *target)
|
|
|
|
{
|
|
|
|
_heat->set_imu_target_temp(target);
|
2015-09-25 11:07:59 -03:00
|
|
|
}
|
|
|
|
|
2014-02-23 20:59:24 -04:00
|
|
|
/**
|
|
|
|
return commandline arguments, if available
|
|
|
|
*/
|
2015-10-20 18:13:25 -03:00
|
|
|
void Util::commandline_arguments(uint8_t &argc, char * const *&argv)
|
2014-02-23 20:59:24 -04:00
|
|
|
{
|
|
|
|
argc = saved_argc;
|
|
|
|
argv = saved_argv;
|
|
|
|
}
|
|
|
|
|
2018-02-16 18:25:36 -04:00
|
|
|
void Util::set_hw_rtc(uint64_t time_utc_usec)
|
2015-01-19 13:30:18 -04:00
|
|
|
{
|
2015-01-22 00:58:20 -04:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE != HAL_BOARD_SUBTYPE_LINUX_NONE
|
2018-02-16 18:25:36 -04:00
|
|
|
// call superclass method to set time. We've guarded this so we
|
|
|
|
// don't reset the HW clock time on people's laptops.
|
|
|
|
AP_HAL::Util::set_hw_rtc(time_utc_usec);
|
|
|
|
#endif
|
2015-01-19 13:30:18 -04:00
|
|
|
}
|
|
|
|
|
2015-10-20 18:13:25 -03:00
|
|
|
bool Util::is_chardev_node(const char *path)
|
2015-06-27 18:18:09 -03:00
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
|
2016-06-03 11:40:45 -03:00
|
|
|
if (!path || lstat(path, &st) < 0) {
|
2015-06-27 18:18:09 -03:00
|
|
|
return false;
|
2016-06-03 11:40:45 -03:00
|
|
|
}
|
2015-06-27 18:18:09 -03:00
|
|
|
|
|
|
|
return S_ISCHR(st.st_mode);
|
|
|
|
}
|
|
|
|
|
2015-11-05 01:06:15 -04:00
|
|
|
/*
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-11-16 17:15:59 -04:00
|
|
|
#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<ARRAY_SIZE(paths); i++) {
|
|
|
|
int fd = open(paths[i], O_RDONLY);
|
|
|
|
if (fd == -1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ssize_t ret = read(fd, buf, len);
|
|
|
|
close(fd);
|
|
|
|
if (ret <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
len = ret;
|
|
|
|
char *p = strchr(cbuf, '\n');
|
|
|
|
if (p) {
|
|
|
|
*p = 0;
|
|
|
|
}
|
|
|
|
len = strnlen(cbuf, len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// fallback to hostname
|
|
|
|
if (gethostname(cbuf, len) != 0) {
|
|
|
|
// use a default name so this always succeeds. Without it we can't
|
|
|
|
// implement some features (such as UAVCAN)
|
|
|
|
strncpy(cbuf, HAL_LINUX_DEFAULT_SYSTEM_ID, len);
|
|
|
|
}
|
|
|
|
len = strnlen(cbuf, len);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
as get_system_id_unformatted will already be ascii, we use the same
|
|
|
|
ID here
|
|
|
|
*/
|
|
|
|
bool Util::get_system_id(char buf[40])
|
|
|
|
{
|
|
|
|
uint8_t len = 40;
|
|
|
|
return get_system_id_unformatted((uint8_t *)buf, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-10-01 18:19:15 -03:00
|
|
|
int Util::write_file(const char *path, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
|
2016-06-03 11:40:45 -03:00
|
|
|
int fd = open(path, O_WRONLY | O_CLOEXEC);
|
2015-10-01 18:19:15 -03:00
|
|
|
if (fd == -1) {
|
|
|
|
return -errno;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
2016-06-03 11:40:45 -03:00
|
|
|
int ret = vdprintf(fd, fmt, args);
|
2015-10-01 18:19:15 -03:00
|
|
|
int errno_bkp = errno;
|
2016-06-03 11:40:45 -03:00
|
|
|
close(fd);
|
2015-10-01 18:19:15 -03:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
|
|
|
if (ret < 1) {
|
|
|
|
return -errno_bkp;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int Util::read_file(const char *path, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
|
2016-06-03 11:40:45 -03:00
|
|
|
FILE *file = fopen(path, "re");
|
|
|
|
if (!file) {
|
2015-10-01 18:19:15 -03:00
|
|
|
return -errno;
|
2016-06-03 11:40:45 -03:00
|
|
|
}
|
2015-10-01 18:19:15 -03:00
|
|
|
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
|
2016-06-03 11:40:45 -03:00
|
|
|
int ret = vfscanf(file, fmt, args);
|
2015-10-01 18:19:15 -03:00
|
|
|
int errno_bkp = errno;
|
2016-06-03 11:40:45 -03:00
|
|
|
fclose(file);
|
2015-10-01 18:19:15 -03:00
|
|
|
|
|
|
|
va_end(args);
|
|
|
|
|
2016-06-03 11:40:45 -03:00
|
|
|
if (ret < 1) {
|
2015-10-01 18:19:15 -03:00
|
|
|
return -errno_bkp;
|
2016-06-03 11:40:45 -03:00
|
|
|
}
|
2015-10-01 18:19:15 -03:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-22 09:49:12 -04:00
|
|
|
const char *Linux::Util::_hw_names[UTIL_NUM_HARDWARES] = {
|
|
|
|
[UTIL_HARDWARE_RPI1] = "BCM2708",
|
|
|
|
[UTIL_HARDWARE_RPI2] = "BCM2709",
|
2020-01-10 09:53:38 -04:00
|
|
|
[UTIL_HARDWARE_RPI4] = "BCM2711",
|
2015-12-22 09:49:12 -04:00
|
|
|
[UTIL_HARDWARE_BEBOP] = "Mykonos3 board",
|
|
|
|
[UTIL_HARDWARE_BEBOP2] = "Milos board",
|
2016-06-06 19:16:25 -03:00
|
|
|
[UTIL_HARDWARE_DISCO] = "Evinrude board",
|
2015-12-22 09:49:12 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MAX_SIZE_LINE 50
|
|
|
|
int Util::get_hw_arm32()
|
|
|
|
{
|
2016-06-03 11:40:45 -03:00
|
|
|
char buffer[MAX_SIZE_LINE] = { 0 };
|
|
|
|
FILE *f = fopen("/proc/cpuinfo", "r");
|
2016-10-30 02:24:21 -03:00
|
|
|
if (f == nullptr) {
|
2016-06-03 11:40:45 -03:00
|
|
|
return -errno;
|
2015-12-22 09:49:12 -04:00
|
|
|
}
|
|
|
|
|
2016-10-30 02:24:21 -03:00
|
|
|
while (fgets(buffer, MAX_SIZE_LINE, f) != nullptr) {
|
|
|
|
if (strstr(buffer, "Hardware") == nullptr) {
|
2016-06-03 11:40:45 -03:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (uint8_t i = 0; i < UTIL_NUM_HARDWARES; i++) {
|
2016-10-30 02:24:21 -03:00
|
|
|
if (strstr(buffer, _hw_names[i]) == nullptr) {
|
2016-06-03 11:40:45 -03:00
|
|
|
continue;
|
2015-12-22 09:49:12 -04:00
|
|
|
}
|
2016-06-03 11:40:45 -03:00
|
|
|
fclose(f);
|
|
|
|
return i;
|
2015-12-22 09:49:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(f);
|
2016-06-03 11:40:45 -03:00
|
|
|
return -ENOENT;
|
2015-12-22 09:49:12 -04:00
|
|
|
}
|
2019-03-23 11:36:17 -03:00
|
|
|
|
|
|
|
#ifdef ENABLE_HEAP
|
|
|
|
void *Util::allocate_heap_memory(size_t size)
|
|
|
|
{
|
|
|
|
struct heap *new_heap = (struct heap*)malloc(sizeof(struct heap));
|
|
|
|
if (new_heap != nullptr) {
|
|
|
|
new_heap->max_heap_size = size;
|
|
|
|
new_heap->current_heap_usage = 0;
|
|
|
|
}
|
|
|
|
return (void *)new_heap;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Util::heap_realloc(void *h, void *ptr, size_t new_size)
|
|
|
|
{
|
|
|
|
if (h == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct heap *heapp = (struct heap*)h;
|
|
|
|
|
|
|
|
// extract appropriate headers
|
|
|
|
size_t 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;
|
|
|
|
}
|
|
|
|
|
2019-08-26 21:25:03 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-03-23 11:36:17 -03:00
|
|
|
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
|
2021-06-24 13:49:47 -03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
}
|