From b1c92cd561ef5a74c74a8cf2917db37315f68f59 Mon Sep 17 00:00:00 2001 From: uvos Date: Wed, 3 May 2023 10:52:48 +0200 Subject: [PATCH] add service --- CMakeLists.txt | 14 +++++++++++++- ipmi.cpp | 39 +++++++++++++++++++++++++++++++++++++++ ipmi.h | 5 +++++ ipmifan.service | 10 ++++++++++ main.cpp | 41 +---------------------------------------- 5 files changed, 68 insertions(+), 41 deletions(-) create mode 100644 ipmifan.service diff --git a/CMakeLists.txt b/CMakeLists.txt index 5f17f3a..4ba30c9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,11 +4,15 @@ project(ipmifan LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) -find_package(Doxygen) +if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) + set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "..." FORCE) +endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) find_package(PkgConfig REQUIRED) pkg_search_module(IPMI_MONITORING REQUIRED libipmimonitoring) pkg_search_module(IPMI REQUIRED libfreeipmi) +pkg_search_module(SYSTEMD systemd) + add_executable(${PROJECT_NAME} main.cpp ipmi.cpp lm.cpp) target_link_libraries(${PROJECT_NAME} ${IPMI_LINK_LIBRARIES} ${IPMI_MONITORING_LINK_LIBRARIES} sensors) @@ -16,3 +20,11 @@ target_include_directories(${PROJECT_NAME} PRIVATE ${IPMI_INCLUDE_DIRS} ${IPMI_M target_compile_options(${PROJECT_NAME} PRIVATE "-Wall" "-O2" "-g" "-fno-strict-aliasing" "-Wfatal-errors" "-Wno-reorder") install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION bin) +if(SYSTEMD_FOUND) + pkg_get_variable(SYSTEMD_UNIT_DIR_PKG systemd systemd_system_unit_path) + message(STATUS ${SYSTEMD_UNIT_DIR_PKG}) + string(REPLACE ":" ";" SYSTEMD_UNIT_DIR_LIST ${SYSTEMD_UNIT_DIR_PKG}) + list(GET SYSTEMD_UNIT_DIR_LIST 0 SYSTEMD_UNIT_DIR) + + install(FILES ipmifan.service DESTINATION ${SYSTEMD_UNIT_DIR}) +endif(SYSTEMD_FOUND) diff --git a/ipmi.cpp b/ipmi.cpp index ed4a024..4795c77 100644 --- a/ipmi.cpp +++ b/ipmi.cpp @@ -2,6 +2,8 @@ #include #include +static constexpr size_t IPMI_RAW_MAX_ARGS = 65536*2; + static double ipmi_convert_sensor_reading(void *sensor_reading, int sensor_reading_type) { if(sensor_reading_type == IPMI_MONITORING_SENSOR_READING_TYPE_UNSIGNED_INTEGER8_BOOL) @@ -102,3 +104,40 @@ ipmi_monitoring_ctx_t init_ipmi_monitoring() return ctx; } + +ipmi_ctx_t ipmi_open_context() +{ + ipmi_ctx_t ctx = ipmi_ctx_create(); + if(!ctx) + { + std::cerr<<"Could not allocae raw context\n"; + return nullptr; + } + + ipmi_driver_type_t driver = IPMI_DEVICE_OPENIPMI; + int ret = ipmi_ctx_find_inband(ctx, &driver, false, 0, 0, nullptr, 0, 0); + if(ret < 0) + { + std::cerr<<"Could not create raw context "<(100), static_cast(speed*100)), static_cast(0)); + + std::cout<<"setting fan group "<(group)<<" to "<(converted_speed)<<")\n"; + + char command[] = {0x70, 0x66, 0x01, static_cast(group), converted_speed}; + char bytesrx[IPMI_RAW_MAX_ARGS] = {0}; + int rxlen = ipmi_cmd_raw(raw_ctx, 0, 0x30, command, sizeof(command), bytesrx, IPMI_RAW_MAX_ARGS); + if(rxlen < 0) + { + std::cerr<<"Raw write to ipmi failed with: "< #include #include +#include #include "sensor.h" @@ -11,3 +12,7 @@ bool ipmi_fill_sensor_ids(std::vector& sensors, ipmi_monitoring_ctx_t ct bool ipmi_update_sensors(std::vector& sensors, ipmi_monitoring_ctx_t ctx, struct ipmi_monitoring_ipmi_config* config); ipmi_monitoring_ctx_t init_ipmi_monitoring(); + +ipmi_ctx_t ipmi_open_context(); + +bool ipmi_set_fan_group(ipmi_ctx_t raw_ctx, uint8_t group, double speed); diff --git a/ipmifan.service b/ipmifan.service new file mode 100644 index 0000000..dd595db --- /dev/null +++ b/ipmifan.service @@ -0,0 +1,10 @@ +[Unit] +Description=Start impi fan control +After=lm_sensors.service systemd-modules-load.service + +[Service] +Type=simple +ExecStart=/usr/bin/ipmifan + +[Install] +WantedBy=multi-user.target diff --git a/main.cpp b/main.cpp index f23d03a..7e71117 100644 --- a/main.cpp +++ b/main.cpp @@ -15,8 +15,6 @@ #include "ipmi.h" #include "lm.h" -static constexpr size_t IPMI_RAW_MAX_ARGS = 65536*2; - sig_atomic_t running = true; void sig_handler(int sig) @@ -114,43 +112,6 @@ std::vector get_fan_zones(const std::vector& sensors) return out; } -ipmi_ctx_t ipmi_open() -{ - ipmi_ctx_t ctx = ipmi_ctx_create(); - if(!ctx) - { - std::cerr<<"Could not allocae raw context\n"; - return nullptr; - } - - ipmi_driver_type_t driver = IPMI_DEVICE_OPENIPMI; - int ret = ipmi_ctx_find_inband(ctx, &driver, false, 0, 0, nullptr, 0, 0); - if(ret < 0) - { - std::cerr<<"Could not create raw context "<(100), static_cast(speed*100)), static_cast(0)); - - std::cout<<"setting fan group "<(group)<<" to "<(converted_speed)<<")\n"; - - char command[] = {0x70, 0x66, 0x01, static_cast(group), converted_speed}; - char bytesrx[IPMI_RAW_MAX_ARGS] = {0}; - int rxlen = ipmi_cmd_raw(raw_ctx, 0, 0x30, command, sizeof(command), bytesrx, IPMI_RAW_MAX_ARGS); - if(rxlen < 0) - { - std::cerr<<"Raw write to ipmi failed with: "<