c23ify
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#ifndef LINUXSMARTIE_GPU_H
|
||||
#define LINUXSMARTIE_GPU_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct gpu_sensors_t {
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#define LCD_LINES 4
|
||||
#define LCD_COLS 40
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
struct lcd_t {
|
||||
|
||||
@@ -12,8 +12,6 @@
|
||||
|
||||
#ifdef APPLOG_ENABLED
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
typedef enum {
|
||||
LOG_INF,
|
||||
LOG_WRN,
|
||||
@@ -38,4 +36,4 @@ void log_write(log_level_t level, const char *func, const char *fmt, ...)
|
||||
#define applog(...) ((void)0)
|
||||
|
||||
#endif /* APPLOG_ENABLED */
|
||||
#endif /* LOG_H */
|
||||
#endif /* LINUXSMARTIE_LOG_H */
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
#ifndef LINUXSMARTIE_UTILS_H
|
||||
#define LINUXSMARTIE_UTILS_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <time.h>
|
||||
|
||||
#if __STDC_VERSION__ >= 202311L
|
||||
#if __STDC_VERSION__ >= 202000L
|
||||
#define C_STD "C23"
|
||||
#elif __STDC_VERSION__ >= 201710L
|
||||
#define C_STD "C17"
|
||||
|
||||
28
src/aio.c
28
src/aio.c
@@ -5,11 +5,11 @@
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
static const uint16_t usb_timeout = 250;
|
||||
static const uint8_t buf_size = 32; // needs 32 bc of overflow issue on these aios?
|
||||
static constexpr uint16_t usb_timeout = 250;
|
||||
static constexpr uint8_t buf_size = 32; // needs 32 bc of overflow issue on these aios?
|
||||
|
||||
static const uint16_t max_fan_speed = 1200;
|
||||
static const uint8_t max_curve_points = 7;
|
||||
static constexpr uint16_t max_fan_speed = 1200;
|
||||
static constexpr uint8_t max_curve_points = 7;
|
||||
|
||||
// posts cmd to the aio, puts n bytes of response inside resp buffer
|
||||
#define POST_AND_GET_RESP(cmd, n) uint8_t req = cmd; uint8_t resp[buf_size]; aio_post(dev, &req, 1, resp, n)
|
||||
@@ -23,7 +23,7 @@ static void aio_usb_claim(libusb_device_handle* dev) {
|
||||
applog(LOG_ERR, "failed to claim AIO device: %s", libusb_error_name(ret));
|
||||
return;
|
||||
}
|
||||
ret = libusb_control_transfer(dev,(LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),0x02,0x02,0, NULL, 0, usb_timeout);
|
||||
ret = libusb_control_transfer(dev,(LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE),0x02,0x02,0, nullptr, 0, usb_timeout);
|
||||
if (ret < 0)
|
||||
applog(LOG_ERR, "AIO clear-to-send failed: %s", libusb_error_name(ret));
|
||||
}
|
||||
@@ -33,8 +33,8 @@ static void aio_usb_release(libusb_device_handle* dev) {
|
||||
}
|
||||
|
||||
static void aio_post(libusb_device_handle *dev, uint8_t *req, const int req_len, uint8_t *resp, const int resp_len) {
|
||||
libusb_bulk_transfer(dev, 0x01, req, req_len, NULL, usb_timeout);
|
||||
libusb_bulk_transfer(dev, 0x81, resp, resp_len, NULL, usb_timeout);
|
||||
libusb_bulk_transfer(dev, 0x01, req, req_len, nullptr, usb_timeout);
|
||||
libusb_bulk_transfer(dev, 0x81, resp, resp_len, nullptr, usb_timeout);
|
||||
}
|
||||
|
||||
static float aio_get_water_temp(libusb_device_handle* dev) {
|
||||
@@ -73,7 +73,7 @@ static void aio_set_color(libusb_device_handle* dev, const enum aio_color_mode_t
|
||||
if (colors == NULL) // NOLINT no its not
|
||||
return;
|
||||
|
||||
const uint8_t modes[] = {0x52, 0x55, 0x55, 0x58};
|
||||
constexpr uint8_t modes[] = {0x52, 0x55, 0x55, 0x58};
|
||||
uint8_t req[buf_size], resp[buf_size];
|
||||
|
||||
req[0] = 0x56;
|
||||
@@ -89,8 +89,8 @@ static void aio_set_color(libusb_device_handle* dev, const enum aio_color_mode_t
|
||||
|
||||
// send speed, skip if fixed color
|
||||
if (mode != AIO_COLOR_FIXED) {
|
||||
const uint8_t speed_shift[] = {0x46, 0x28, 0x0F};
|
||||
const uint8_t speed_pulse[] = {0x50, 0x37, 0x1E};
|
||||
constexpr uint8_t speed_shift[] = {0x46, 0x28, 0x0F};
|
||||
constexpr uint8_t speed_pulse[] = {0x50, 0x37, 0x1E};
|
||||
req[0] = 0x53;
|
||||
req[1] = mode == AIO_COLOR_SHIFT ? speed_shift[speed] : speed_pulse[speed];
|
||||
aio_post(dev, req, 2, resp, buf_size);
|
||||
@@ -130,8 +130,8 @@ static void aio_set_fan_curve(libusb_device_handle* dev, const struct curve_poin
|
||||
}
|
||||
|
||||
void aio_detect(struct aio_t *aio, const float t_quiet, const float t_balanced) {
|
||||
libusb_init(NULL);
|
||||
aio->handle = libusb_open_device_with_vid_pid(NULL, AIO_USB_VID, AIO_USB_PID);
|
||||
libusb_init(nullptr);
|
||||
aio->handle = libusb_open_device_with_vid_pid(nullptr, AIO_USB_VID, AIO_USB_PID);
|
||||
if (aio->handle == NULL) {
|
||||
applog(LOG_ERR, "Can't open AIO USB device %x:%x", AIO_USB_VID, AIO_USB_PID);
|
||||
return;
|
||||
@@ -154,7 +154,7 @@ void aio_detect(struct aio_t *aio, const float t_quiet, const float t_balanced)
|
||||
aio_usb_claim(aio->handle);
|
||||
applog(LOG_INF, "found Corsair AIO device at %04x:%04x, fw ver: %s", AIO_USB_VID, AIO_USB_PID, aio_get_firmware_version(aio->handle, (char[16]){0}));
|
||||
aio_set_pump_mode(aio->handle, QUIET); // needed for init ig?
|
||||
aio_set_color(aio->handle, AIO_COLOR_FIXED, 0, NULL, 0); // so it doenst complain the func is unused
|
||||
aio_set_color(aio->handle, AIO_COLOR_FIXED, 0, nullptr, 0); // so it doenst complain the func is unused
|
||||
aio_set_fan_curve(aio->handle, fan_curve, sizeof(fan_curve) / sizeof(fan_curve[0]));
|
||||
aio_usb_release(aio->handle);
|
||||
}
|
||||
@@ -182,6 +182,6 @@ void aio_update(struct aio_t *aio) {
|
||||
void aio_close(const struct aio_t *aio) {
|
||||
libusb_attach_kernel_driver(aio->handle, 0); // reattach so liquidctl still works
|
||||
libusb_close(aio->handle);
|
||||
libusb_exit(NULL);
|
||||
libusb_exit(nullptr);
|
||||
applog(LOG_INF, "AIO device closed");
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
#include "utils.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -19,7 +18,7 @@ void cpu_detect(struct cpu_sensors_t *t, const char *pci_dev) {
|
||||
return;
|
||||
}
|
||||
|
||||
t->module = read_file(t->hwmon, "/in1_input", NULL, 0) ? zenpower : k10temp; // if we can read zenpower file then zenpower. idk if this is reliable, we're trying to avoid just reading the name
|
||||
t->module = read_file(t->hwmon, "/in1_input", nullptr, 0) ? zenpower : k10temp; // if we can read zenpower file then zenpower. idk if this is reliable, we're trying to avoid just reading the name
|
||||
applog(LOG_INF, "Detected %d cores, module enum #%d in path %s", t->num_cores, t->module, t->hwmon);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
#include "log.h"
|
||||
#include "utils.h"
|
||||
|
||||
const char rel_metrics_path[] = "/../../gpu_metrics"; // gpu_metrics path relative to hwmon path
|
||||
const uint16_t max_metrics_length = 512; // length of the metrics buffer
|
||||
constexpr char rel_metrics_path[] = "/../../gpu_metrics"; // gpu_metrics path relative to hwmon path
|
||||
constexpr uint16_t max_metrics_length = 512; // length of the metrics buffer
|
||||
|
||||
void gpu_detect(struct gpu_sensors_t *t, const char *pci) {
|
||||
snprintf(t->hwmon, sizeof(t->hwmon), "/sys/bus/pci/devices/%s/hwmon", pci);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "lcd.h"
|
||||
#include "log.h"
|
||||
#include <fcntl.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
@@ -37,7 +38,7 @@ void lcd_write(struct lcd_t *lcd, const uint8_t *cmdbuf, const uint16_t len) {
|
||||
}
|
||||
|
||||
void lcd_clear(struct lcd_t *lcd) {
|
||||
const uint8_t cmdbuf[] = {254, 88};
|
||||
constexpr uint8_t cmdbuf[] = {254, 88};
|
||||
lcd_write(lcd, cmdbuf, sizeof(cmdbuf));
|
||||
}
|
||||
|
||||
@@ -98,7 +99,7 @@ inline void lcd_clear_buf(struct lcd_t *lcd) {
|
||||
|
||||
/* flushes writebuffer to lcd, hyperoptmized, flushes buffer and commands in a single write operation */
|
||||
inline void lcd_flush_buf(struct lcd_t *lcd) {
|
||||
const uint8_t set_disp[] = {254, 71, 1, 1}; // set cursor to char 1, line 1
|
||||
constexpr uint8_t set_disp[] = {254, 71, 1, 1}; // set cursor to char 1, line 1
|
||||
memcpy(&lcd->writebuf.text[0], set_disp, 4);
|
||||
memcpy(&lcd->writebuf.text[2 * LCD_COLS + 4], set_disp, 4);
|
||||
lcd->writebuf.text[2 * LCD_COLS + 7] = 3; // overwrite set cursor c1 l1 to c1 l3
|
||||
@@ -136,7 +137,7 @@ int lcd_init(struct lcd_t *lcd) {
|
||||
}
|
||||
|
||||
lcd_clear_buf(lcd);
|
||||
const uint8_t cmdbuf[] = {254, 75, 254, 84};
|
||||
constexpr uint8_t cmdbuf[] = {254, 75, 254, 84};
|
||||
lcd_write(lcd, cmdbuf, sizeof(cmdbuf));
|
||||
|
||||
lcd_clear(lcd);
|
||||
|
||||
28
src/main.c
28
src/main.c
@@ -15,11 +15,11 @@
|
||||
#include "sys.h"
|
||||
#include "utils.h"
|
||||
|
||||
static const uint8_t lcd_contrast = 200;
|
||||
static const uint16_t refresh_interval_ms = 1000;
|
||||
static const char netif[] = "enp4s0f0";
|
||||
static const char cpu_pcidev[] = "0000:00:18.3";
|
||||
static const char gpu_pcidev[] = "0000:0a:00.0";
|
||||
static constexpr uint8_t lcd_contrast = 200;
|
||||
static constexpr uint16_t refresh_interval_ms = 1000;
|
||||
static constexpr char netif[] = "enp4s0f0";
|
||||
static constexpr char cpu_pcidev[] = "0000:00:18.3";
|
||||
static constexpr char gpu_pcidev[] = "0000:0a:00.0";
|
||||
|
||||
struct control_t {
|
||||
atomic_bool g_running;
|
||||
@@ -78,7 +78,7 @@ static void *sensors_thread(void *arg) {
|
||||
pthread_mutex_unlock(&control.sensors_mtx);
|
||||
|
||||
timespec_add_ms(&next, refresh_interval_ms);
|
||||
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next, NULL);
|
||||
clock_nanosleep(CLOCK_REALTIME, TIMER_ABSTIME, &next, nullptr);
|
||||
}
|
||||
|
||||
aio_close(&sensors->aio);
|
||||
@@ -121,7 +121,7 @@ static void *display_thread(void *arg) {
|
||||
|
||||
{
|
||||
// prepare some tricky sensors
|
||||
const char modes[] = "QBP";
|
||||
constexpr char modes[] = "QBP";
|
||||
const uint16_t avg_fan_speed = (sensors->aio.fan_speed[0] + sensors->aio.fan_speed[1]) / 2;
|
||||
|
||||
char gpudelta[5];
|
||||
@@ -194,13 +194,13 @@ int main(void) {
|
||||
sigaddset(&sigset, SIGINT);
|
||||
sigaddset(&sigset, SIGTERM);
|
||||
sigaddset(&sigset, SIGQUIT);
|
||||
pthread_sigmask(SIG_BLOCK, &sigset, NULL);
|
||||
pthread_sigmask(SIG_BLOCK, &sigset, nullptr);
|
||||
struct signalfd_siginfo info;
|
||||
const int sfd = signalfd(-1, &sigset, SFD_CLOEXEC);
|
||||
|
||||
/* control vars setup */
|
||||
pthread_mutex_init(&control.sensors_mtx, NULL);
|
||||
pthread_cond_init(&control.sensors_ready, NULL);
|
||||
pthread_mutex_init(&control.sensors_mtx, nullptr);
|
||||
pthread_cond_init(&control.sensors_ready, nullptr);
|
||||
control.g_running = true;
|
||||
|
||||
/* thread setup */
|
||||
@@ -208,8 +208,8 @@ int main(void) {
|
||||
pthread_t display_pthread;
|
||||
|
||||
/* actually start the threads */
|
||||
pthread_create(&sensors_pthread, NULL, sensors_thread, sensors);
|
||||
pthread_create(&display_pthread, NULL, display_thread, sensors);
|
||||
pthread_create(&sensors_pthread, nullptr, sensors_thread, sensors);
|
||||
pthread_create(&display_pthread, nullptr, display_thread, sensors);
|
||||
|
||||
/* halts here until signal to stop */
|
||||
const ssize_t ret = read(sfd, &info, sizeof(info));
|
||||
@@ -219,8 +219,8 @@ int main(void) {
|
||||
applog(LOG_WRN, "got stop signal, shutting down...");
|
||||
close(sfd);
|
||||
control.g_running = false;
|
||||
pthread_join(sensors_pthread, NULL);
|
||||
pthread_join(display_pthread, NULL);
|
||||
pthread_join(sensors_pthread, nullptr);
|
||||
pthread_join(display_pthread, nullptr);
|
||||
free(sensors);
|
||||
return 0;
|
||||
}
|
||||
@@ -9,9 +9,9 @@
|
||||
#include "utils.h"
|
||||
|
||||
void sys_get_time(struct time_date_t *td) {
|
||||
const char months[][4] = {"sty", "lut", "mar", "kwi", "maj", "cze", "lip", "sie", "wrz", "paz", "lis", "gru"};
|
||||
const char wdays[][4] = {"Nie", "Pon", "Wto", "Sro", "Czw", "Pia", "Sob"};
|
||||
const time_t t = time(NULL);
|
||||
constexpr char months[][4] = {"sty", "lut", "mar", "kwi", "maj", "cze", "lip", "sie", "wrz", "paz", "lis", "gru"};
|
||||
constexpr char wdays[][4] = {"Nie", "Pon", "Wto", "Sro", "Czw", "Pia", "Sob"};
|
||||
const time_t t = time(nullptr);
|
||||
const struct tm *tm = localtime(&t);
|
||||
snprintf(td->time, sizeof(td->time), "%02d:%02d:%02d", tm->tm_hour, tm->tm_min, tm->tm_sec);
|
||||
snprintf(td->date, sizeof(td->date), "%s, %02d %s", wdays[tm->tm_wday], tm->tm_mday, months[tm->tm_mon]);
|
||||
|
||||
14
src/utils.c
14
src/utils.c
@@ -55,7 +55,7 @@ inline char *pretty_num(const double num, char *buf, const size_t bufsz) {
|
||||
}
|
||||
|
||||
inline char *pretty_bytes(const uint64_t bytes, int8_t start_suffix, char *buf, const size_t bufsz) {
|
||||
const char suffix_list[] = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'};
|
||||
constexpr char suffix_list[] = {'K', 'M', 'G', 'T', 'P', 'E', 'Z'};
|
||||
start_suffix *= -1;
|
||||
double bytes_d = (double)bytes;
|
||||
while (bytes_d >= 1024.0) {
|
||||
@@ -71,7 +71,7 @@ int get_hwmon_dir(char *path, const size_t bufsz) {
|
||||
glob_t g;
|
||||
strcat(path, "/hwmon*");
|
||||
|
||||
if (glob(path, 0, NULL, &g) == 0) {
|
||||
if (glob(path, 0, nullptr, &g) == 0) {
|
||||
strlcpy(path, g.gl_pathv[0], bufsz); // same size as hwmon length, careful
|
||||
globfree(&g);
|
||||
return 0;
|
||||
@@ -103,25 +103,25 @@ inline size_t read_file(const char *dir, const char *file, char *buffer, const s
|
||||
|
||||
inline void fast_read_num_u64(const char *dir, const char *file, char *buf, uint64_t *result, const uint32_t divider) {
|
||||
read_file(dir, file, buf, 32);
|
||||
*result = strtoull(buf, NULL, 10) / divider;
|
||||
*result = strtoull(buf, nullptr, 10) / divider;
|
||||
}
|
||||
|
||||
inline void fast_read_num_u16(const char *dir, const char *file, char *buf, uint16_t *result, const uint32_t divider) {
|
||||
read_file(dir, file, buf, 32);
|
||||
*result = strtol(buf, NULL, 10) / divider;
|
||||
*result = strtol(buf, nullptr, 10) / divider;
|
||||
}
|
||||
|
||||
inline void fast_read_num_u8(const char *dir, const char *file, char *buf, uint8_t *result, const uint32_t divider) {
|
||||
read_file(dir, file, buf, 32);
|
||||
*result = strtol(buf, NULL, 10) / divider;
|
||||
*result = strtol(buf, nullptr, 10) / divider;
|
||||
}
|
||||
|
||||
inline void fast_read_num_i16(const char *dir, const char *file, char *buf, int16_t *result, const uint32_t divider) {
|
||||
read_file(dir, file, buf, 32);
|
||||
*result = (int16_t)(strtol(buf, NULL, 10) / divider);
|
||||
*result = (int16_t)(strtol(buf, nullptr, 10) / divider);
|
||||
}
|
||||
|
||||
inline void fast_read_num_f(const char *dir, const char *file, char *buf, float *result, const uint32_t divider) {
|
||||
read_file(dir, file, buf, 32);
|
||||
*result = (float)strtoull(buf, NULL, 10) / (float)divider;
|
||||
*result = (float)strtoull(buf, nullptr, 10) / (float)divider;
|
||||
}
|
||||
Reference in New Issue
Block a user