APM:Libraries
Util.cpp
Go to the documentation of this file.
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <stdarg.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/stat.h>
7 #include <time.h>
8 #include <unistd.h>
9 
10 #include <AP_HAL/AP_HAL.h>
11 
12 #include "Heat_Pwm.h"
13 #include "ToneAlarm_Disco.h"
14 #include "Util.h"
15 
16 using namespace Linux;
17 
18 extern const AP_HAL::HAL& hal;
19 
20 static int state;
21 #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_DISCO
23 #else
25 #endif
26 
27 void Util::init(int argc, char * const *argv) {
28  saved_argc = argc;
29  saved_argv = argv;
30 
31 #ifdef HAL_UTILS_HEAT
32 #if HAL_UTILS_HEAT == HAL_LINUX_HEAT_PWM
33  _heat = new Linux::HeatPwm(HAL_LINUX_HEAT_PWM_NUM,
34  HAL_LINUX_HEAT_KP,
35  HAL_LINUX_HEAT_KI,
36  HAL_LINUX_HEAT_PERIOD_NS);
37 #else
38  #error Unrecognized Heat
39 #endif // #if
40 #else
41  _heat = new Linux::Heat();
42 #endif // #ifdef
43 }
44 
45 // set current IMU temperatue in degrees C
46 void Util::set_imu_temp(float current)
47 {
48  _heat->set_imu_temp(current);
49 }
50 
51 // set target IMU temperatue in degrees C
52 void Util::set_imu_target_temp(int8_t *target)
53 {
54  _heat->set_imu_target_temp(target);
55 }
56 
60 void Util::commandline_arguments(uint8_t &argc, char * const *&argv)
61 {
62  argc = saved_argc;
63  argv = saved_argv;
64 }
65 
67 {
68  return _toneAlarm.init();
69 }
70 
71 void Util::toneAlarm_set_tune(uint8_t tone)
72 {
73  _toneAlarm.set_tune(tone);
74 }
75 
77  if(state == 0) {
79  } else if (state == 1) {
81  }
82  if (state == 2) {
83  state = state + _toneAlarm.play();
84  } else if (state == 3) {
85  state = 1;
86  }
87 
88  if (_toneAlarm.is_tune_comp()) {
89  state = 0;
90  }
91 
92 }
93 
94 void Util::set_system_clock(uint64_t time_utc_usec)
95 {
96 #if CONFIG_HAL_BOARD_SUBTYPE != HAL_BOARD_SUBTYPE_LINUX_NONE
97  timespec ts;
98  ts.tv_sec = time_utc_usec/1000000ULL;
99  ts.tv_nsec = (time_utc_usec % 1000000ULL) * 1000ULL;
100  clock_settime(CLOCK_REALTIME, &ts);
101 #endif
102 }
103 
104 bool Util::is_chardev_node(const char *path)
105 {
106  struct stat st;
107 
108  if (!path || lstat(path, &st) < 0) {
109  return false;
110  }
111 
112  return S_ISCHR(st.st_mode);
113 }
114 
115 /*
116  always report 256k of free memory. Using mallinfo() isn't useful as
117  it only reported the current heap, which auto-expands. What we're
118  trying to do here is ensure that code which checks for free memory
119  before allocating objects does allow the allocation
120  */
122 {
123  return 256*1024;
124 }
125 
126 int Util::write_file(const char *path, const char *fmt, ...)
127 {
128  errno = 0;
129 
130  int fd = open(path, O_WRONLY | O_CLOEXEC);
131  if (fd == -1) {
132  return -errno;
133  }
134 
135  va_list args;
136  va_start(args, fmt);
137 
138  int ret = vdprintf(fd, fmt, args);
139  int errno_bkp = errno;
140  close(fd);
141 
142  va_end(args);
143 
144  if (ret < 1) {
145  return -errno_bkp;
146  }
147 
148  return ret;
149 }
150 
151 int Util::read_file(const char *path, const char *fmt, ...)
152 {
153  errno = 0;
154 
155  FILE *file = fopen(path, "re");
156  if (!file) {
157  return -errno;
158  }
159 
160  va_list args;
161  va_start(args, fmt);
162 
163  int ret = vfscanf(file, fmt, args);
164  int errno_bkp = errno;
165  fclose(file);
166 
167  va_end(args);
168 
169  if (ret < 1) {
170  return -errno_bkp;
171  }
172 
173  return ret;
174 }
175 
177  [UTIL_HARDWARE_RPI1] = "BCM2708",
178  [UTIL_HARDWARE_RPI2] = "BCM2709",
179  [UTIL_HARDWARE_BEBOP] = "Mykonos3 board",
180  [UTIL_HARDWARE_BEBOP2] = "Milos board",
181  [UTIL_HARDWARE_DISCO] = "Evinrude board",
182 };
183 
184 #define MAX_SIZE_LINE 50
186 {
187  char buffer[MAX_SIZE_LINE] = { 0 };
188  FILE *f = fopen("/proc/cpuinfo", "r");
189  if (f == nullptr) {
190  return -errno;
191  }
192 
193  while (fgets(buffer, MAX_SIZE_LINE, f) != nullptr) {
194  if (strstr(buffer, "Hardware") == nullptr) {
195  continue;
196  }
197  for (uint8_t i = 0; i < UTIL_NUM_HARDWARES; i++) {
198  if (strstr(buffer, _hw_names[i]) == nullptr) {
199  continue;
200  }
201  fclose(f);
202  return i;
203  }
204  }
205 
206  fclose(f);
207  return -ENOENT;
208 }
void _toneAlarm_timer_tick()
Definition: Util.cpp:76
Heat * _heat
Definition: Util.h:110
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
Definition: posix.c:885
static const char * _hw_names[UTIL_NUM_HARDWARES]
Definition: Util.h:115
void set_imu_temp(float current) override
Definition: Util.cpp:46
int int read_file(const char *path, const char *fmt,...) FMT_SCANF(3
Definition: Util.cpp:151
virtual void set_imu_temp(float current)
Definition: Heat.h:21
char *const * saved_argv
Definition: Util.h:111
bool is_tune_comp()
Definition: ToneAlarm.cpp:72
#define MAX_SIZE_LINE
Definition: Util.cpp:184
static ToneAlarm_Disco _toneAlarm
Definition: Util.h:105
#define f(i)
const char * fmt
Definition: Printf.cpp:14
static const AP_HAL::HAL & hal
Definition: I2CDevice.cpp:61
void set_tune(uint8_t tone)
Definition: ToneAlarm.cpp:67
int close(int fileno)
POSIX Close a file with fileno handel.
Definition: posix.c:675
static int state
Definition: Util.cpp:20
void init(int argc, char *const *argv)
Definition: Util.cpp:27
int saved_argc
Definition: Util.h:109
void commandline_arguments(uint8_t &argc, char *const *&argv)
Definition: Util.cpp:60
void toneAlarm_set_tune(uint8_t tune)
Definition: Util.cpp:71
uint32_t available_memory(void) override
Definition: Util.cpp:121
int stat(const char *name, struct stat *buf)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
Definition: posix.c:1319
FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
Definition: posix.c:772
int write_file(const char *path, const char *fmt,...) FMT_PRINTF(3
Definition: Util.cpp:126
int errno
Note: fdevopen assigns stdin,stdout,stderr.
Definition: posix.c:118
void set_imu_target_temp(int8_t *target) override
Definition: Util.cpp:52
virtual void set_imu_target_temp(int8_t *target)
Definition: Heat.h:22
void set_system_clock(uint64_t time_utc_usec)
Definition: Util.cpp:94
bool toneAlarm_init()
Definition: Util.cpp:66
char * fgets(char *str, int size, FILE *stream)
get a string from stdin See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:398
bool is_chardev_node(const char *path)
Definition: Util.cpp:104
int get_hw_arm32()
Definition: Util.cpp:185