RocketLogger 2.1.2
util.c
Go to the documentation of this file.
1
32#include <ctype.h>
33#include <errno.h>
34#include <signal.h>
35#include <stdarg.h>
36#include <stdbool.h>
37#include <stdint.h>
38#include <stdio.h>
39#include <string.h>
40
41#include <sys/statvfs.h>
42#include <time.h>
43
44#include "log.h"
45#include "rl.h"
46
47#include "util.h"
48
49bool is_current(int index) {
50 if (index == RL_CONFIG_CHANNEL_I1H || index == RL_CONFIG_CHANNEL_I1L ||
51 index == RL_CONFIG_CHANNEL_I2H || index == RL_CONFIG_CHANNEL_I2L) {
52 return true;
53 }
54 return false;
55}
56
57bool is_low_current(int index) {
58 if (index == RL_CONFIG_CHANNEL_I1L || index == RL_CONFIG_CHANNEL_I2L) {
59 return true;
60 }
61 return false;
62}
63
64bool is_voltage(int index) {
65 if (index == RL_CONFIG_CHANNEL_V1 || index == RL_CONFIG_CHANNEL_V2 ||
66 index == RL_CONFIG_CHANNEL_V3 || index == RL_CONFIG_CHANNEL_V4) {
67 return true;
68 }
69 return false;
70}
71
72int count_channels(bool const channels[RL_CHANNEL_COUNT]) {
73 int count = 0;
74 for (int i = 0; i < RL_CHANNEL_COUNT; i++) {
75 if (channels[i]) {
76 count++;
77 }
78 }
79 return count;
80}
81
82int div_ceil(int n, int d) {
83 if (n % d == d || n % d == 0) {
84 return n / d;
85 } else {
86 return n / d + 1;
87 }
88}
89
90void create_time_stamp(rl_timestamp_t *const timestamp_realtime,
91 rl_timestamp_t *const timestamp_monotonic) {
92
93 struct timespec spec_real;
94 struct timespec spec_monotonic;
95
96 // get time stamp of real-time and monotonic clock
97 int ret1 = clock_gettime(CLOCK_REALTIME, &spec_real);
98 int ret2 = clock_gettime(CLOCK_MONOTONIC_RAW, &spec_monotonic);
99
100 if (ret1 < 0 || ret2 < 0) {
101 rl_log(RL_LOG_ERROR, "failed to get time; %d message: %s", errno,
102 strerror(errno));
103 }
104
105 // convert to own time stamp
106 timestamp_realtime->sec = (int64_t)spec_real.tv_sec;
107 timestamp_realtime->nsec = (int64_t)spec_real.tv_nsec;
108 timestamp_monotonic->sec = (int64_t)spec_monotonic.tv_sec;
109 timestamp_monotonic->nsec = (int64_t)spec_monotonic.tv_nsec;
110}
111
112void get_mac_addr(uint8_t mac_address[MAC_ADDRESS_LENGTH]) {
113 FILE *fp = fopen(MAC_ADDRESS_FILE, "r");
114
115 unsigned int temp;
116 fscanf(fp, "%x", &temp);
117 mac_address[0] = (uint8_t)temp;
118 for (int i = 1; i < MAC_ADDRESS_LENGTH; i++) {
119 fscanf(fp, ":%x", &temp);
120 mac_address[i] = (uint8_t)temp;
121 }
122 fclose(fp);
123}
124
125int64_t fs_space_free(char const *const path) {
126 struct statvfs stat;
127 int ret = statvfs(path, &stat);
128 if (ret < 0) {
130 "failed getting free file system size; %d message: %s", errno,
131 strerror(errno));
132 }
133
134 return (uint64_t)stat.f_bavail * (uint64_t)stat.f_bsize;
135}
136
137int64_t fs_space_total(char const *const path) {
138 struct statvfs stat;
139 int ret = statvfs(path, &stat);
140 if (ret < 0) {
142 "failed getting total file system size; %d message: %s", errno,
143 strerror(errno));
144 }
145
146 return ((uint64_t)stat.f_blocks * (uint64_t)stat.f_frsize);
147}
148
149bool is_empty_string(char const *str) {
150 while (*str) {
151 if (isgraph(*str)) {
152 return false;
153 }
154 }
155 return true;
156}
157
158bool is_printable_string(char const *str) {
159 while (*str) {
160 if (isprint(*str) || isspace(*str)) {
161 str++;
162 continue;
163 }
164 return false;
165 }
166 return true;
167}
168
169void print_json_bool(bool const *const data, const int length) {
170 printf("[");
171 for (int i = 0; i < length; i++) {
172 if (i > 0) {
173 printf(", ");
174 }
175 printf("%d", data[i] ? 1 : 0);
176 }
177 printf("]");
178}
179
180void print_json_int64(int64_t const *const data, const int length) {
181 printf("[");
182 for (int i = 0; i < length; i++) {
183 if (i > 0) {
184 printf(", ");
185 }
186 printf("%lld", data[i]);
187 }
188 printf("]");
189}
190
191int snprintfcat(char *const buffer, size_t length, char const *format, ...) {
192 va_list args;
193 va_start(args, format);
194 char *const buffer_next = buffer + strlen(buffer);
195 size_t length_next = length - strlen(buffer);
196 int res = vsnprintf(buffer_next, length_next, format, args);
197 va_end(args);
198 return res;
199}
rl_calibration_t data
The actual calibration data.
Definition: calibration.h:9
int rl_log(rl_log_level_t log_level, char const *const format,...)
Definition: log.c:82
@ RL_LOG_ERROR
Error.
Definition: log.h:49
@ RL_LOG_WARNING
Warning.
Definition: log.h:50
#define RL_CONFIG_CHANNEL_I1H
Definition: rl.h:88
#define RL_CONFIG_CHANNEL_I2L
Definition: rl.h:89
#define RL_CONFIG_CHANNEL_I2H
Definition: rl.h:90
#define RL_CONFIG_CHANNEL_V4
Definition: rl.h:86
#define RL_CONFIG_CHANNEL_V3
Definition: rl.h:85
#define RL_CONFIG_CHANNEL_V2
Definition: rl.h:84
#define RL_CONFIG_CHANNEL_I1L
Definition: rl.h:87
#define RL_CHANNEL_COUNT
Number of RocketLogger analog channels.
Definition: rl.h:56
#define RL_CONFIG_CHANNEL_V1
Configuration channel indexes.
Definition: rl.h:83
int64_t nsec
Nanoseconds.
Definition: util.h:53
int64_t sec
Seconds in UNIX time (UTC)
Definition: util.h:51
bool is_current(int index)
Definition: util.c:49
int64_t fs_space_free(char const *const path)
Definition: util.c:125
int count_channels(bool const channels[RL_CHANNEL_COUNT])
Definition: util.c:72
bool is_printable_string(char const *str)
Definition: util.c:158
int div_ceil(int n, int d)
Definition: util.c:82
int64_t fs_space_total(char const *const path)
Definition: util.c:137
void create_time_stamp(rl_timestamp_t *const timestamp_realtime, rl_timestamp_t *const timestamp_monotonic)
Definition: util.c:90
void get_mac_addr(uint8_t mac_address[MAC_ADDRESS_LENGTH])
Definition: util.c:112
bool is_low_current(int index)
Definition: util.c:57
void print_json_bool(bool const *const data, const int length)
Definition: util.c:169
bool is_empty_string(char const *str)
Definition: util.c:149
bool is_voltage(int index)
Definition: util.c:64
int snprintfcat(char *const buffer, size_t length, char const *format,...)
Definition: util.c:191
void print_json_int64(int64_t const *const data, const int length)
Definition: util.c:180
#define MAC_ADDRESS_LENGTH
MAC address length in bytes.
Definition: util.h:41
#define MAC_ADDRESS_FILE
File to read MAC address.
Definition: util.h:44