diff --git a/include/gpu.h b/include/gpu.h index 56b04ba..04ec036 100644 --- a/include/gpu.h +++ b/include/gpu.h @@ -1,7 +1,6 @@ #ifndef LINUXSMARTIE_GPU_H #define LINUXSMARTIE_GPU_H -#include #include struct gpu_sensors_t { diff --git a/include/lcd.h b/include/lcd.h index bb2479b..15eef14 100644 --- a/include/lcd.h +++ b/include/lcd.h @@ -4,7 +4,6 @@ #define LCD_LINES 4 #define LCD_COLS 40 -#include #include struct lcd_t { diff --git a/include/log.h b/include/log.h index b0eaf4e..3bb332c 100644 --- a/include/log.h +++ b/include/log.h @@ -12,8 +12,6 @@ #ifdef APPLOG_ENABLED -#include - 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 */ diff --git a/include/utils.h b/include/utils.h index 6632864..34e1a57 100644 --- a/include/utils.h +++ b/include/utils.h @@ -1,10 +1,10 @@ #ifndef LINUXSMARTIE_UTILS_H #define LINUXSMARTIE_UTILS_H -#include +#include #include -#if __STDC_VERSION__ >= 202311L +#if __STDC_VERSION__ >= 202000L #define C_STD "C23" #elif __STDC_VERSION__ >= 201710L #define C_STD "C17" diff --git a/src/aio.c b/src/aio.c index bb8c9bd..c63b91a 100644 --- a/src/aio.c +++ b/src/aio.c @@ -5,11 +5,11 @@ #include #include -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"); } \ No newline at end of file diff --git a/src/cpu.c b/src/cpu.c index 9bfe7cd..83d4356 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -3,7 +3,6 @@ #include "utils.h" #include -#include #include #include @@ -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); } diff --git a/src/gpu.c b/src/gpu.c index 6aa5e29..503c332 100644 --- a/src/gpu.c +++ b/src/gpu.c @@ -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); diff --git a/src/lcd.c b/src/lcd.c index ecdd63d..c806fdc 100644 --- a/src/lcd.c +++ b/src/lcd.c @@ -1,6 +1,7 @@ #include "lcd.h" #include "log.h" #include +#include #include #include #include @@ -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); diff --git a/src/main.c b/src/main.c index 05ab541..f55beb5 100644 --- a/src/main.c +++ b/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; } \ No newline at end of file diff --git a/src/sys.c b/src/sys.c index acd2db4..31d2028 100644 --- a/src/sys.c +++ b/src/sys.c @@ -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]); diff --git a/src/utils.c b/src/utils.c index 2ad4272..94e1632 100644 --- a/src/utils.c +++ b/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; } \ No newline at end of file