inital commit
This commit is contained in:
1
libfandevice/90-fandevice.rules
Normal file
1
libfandevice/90-fandevice.rules
Normal file
@ -0,0 +1 @@
|
||||
SUBSYSTEM=="usb", ATTRS{idVendor}=="fe17", GROUP="uucp"
|
44
libfandevice/CMakeLists.txt
Normal file
44
libfandevice/CMakeLists.txt
Normal file
@ -0,0 +1,44 @@
|
||||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(fandevice)
|
||||
|
||||
find_package(PkgConfig REQUIRED)
|
||||
find_package(Doxygen)
|
||||
pkg_check_modules(LIBUSB REQUIRED libusb-1.0)
|
||||
|
||||
set(SRC_FILES fandevice.c usbshm.c)
|
||||
|
||||
message("Platform " ${CMAKE_SYSTEM_NAME})
|
||||
if(WIN32)
|
||||
message("Building for Windows")
|
||||
file(GLOB_RECURSE ARGP_SRC ${PROJECT_SOURCE_DIR}/argp/*.*)
|
||||
set(SRC_FILES ${SRC_FILES} ${ARGP_SRC})
|
||||
include_directories(./argp)
|
||||
else()
|
||||
message("Building for UNIX")
|
||||
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||
set(CMAKE_INSTALL_PREFIX "/usr" CACHE PATH "..." FORCE)
|
||||
endif(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
|
||||
endif(WIN32)
|
||||
|
||||
add_library(${PROJECT_NAME} SHARED ${SRC_FILES})
|
||||
target_link_libraries(${PROJECT_NAME} ${LIBUSB_LIBRARIES})
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC ${LIBUSB_INCLUDE_DIRS})
|
||||
add_definitions("-std=c17 -Wall -O2 -fno-strict-aliasing")
|
||||
install(TARGETS ${PROJECT_NAME} DESTINATION lib)
|
||||
install(FILES ./fandevice.h DESTINATION include)
|
||||
|
||||
link_directories(${CMAKE_CURRENT_BINARY_DIR})
|
||||
add_executable(${PROJECT_NAME}_cli main.c)
|
||||
add_dependencies(${PROJECT_NAME}_cli ${PROJECT_NAME})
|
||||
target_link_libraries( ${PROJECT_NAME}_cli ${PROJECT_NAME})
|
||||
add_definitions("-std=c17 -Wall -O2 -fno-strict-aliasing")
|
||||
install(TARGETS ${PROJECT_NAME}_cli DESTINATION bin)
|
||||
|
||||
configure_file(pkgconfig/fandevice.pc.in pkgconfig/fandevice.pc @ONLY)
|
||||
install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pkgconfig/fandevice.pc DESTINATION lib/pkgconfig)
|
||||
|
||||
if(CMAKE_SYSTEM_NAME MATCHES "^Linux")
|
||||
set(UDEV_RULES_INSTALL_DIR /lib/udev/rules.d CACHE PATH "install directory for linux udev config")
|
||||
install(FILES 90-fandevice.rules DESTINATION ${UDEV_RULES_INSTALL_DIR})
|
||||
endif()
|
91
libfandevice/fandevice.c
Normal file
91
libfandevice/fandevice.c
Normal file
@ -0,0 +1,91 @@
|
||||
/* * Copyright (c) 2024 Carl Klemm <carl@uvos.xyz>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation and/or
|
||||
* other materials provided with the distribution.
|
||||
* * Neither the name of %ORGANIZATION% nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#define _POSIX_C_SOURCE 199309L
|
||||
|
||||
#include <stdint.h>
|
||||
#include "fandevice.h"
|
||||
#include "usbshm.h"
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
static void usleep(uint64_t microseconds)
|
||||
{
|
||||
struct timespec ts;
|
||||
ts.tv_sec = microseconds / 1000000;
|
||||
ts.tv_nsec = (microseconds % 1000000) * 1000;
|
||||
nanosleep(&ts, NULL);
|
||||
}
|
||||
|
||||
int fandevice_connect(struct fandevice* device, uint16_t serial)
|
||||
{
|
||||
int ret;
|
||||
device->priv = malloc(sizeof(*device->priv));
|
||||
if(!device->priv)
|
||||
return -1;
|
||||
|
||||
ret = usbshm_init(device->priv, NULL, device);
|
||||
if(ret)
|
||||
return -2;
|
||||
|
||||
unsigned char serialStr[5];
|
||||
snprintf((char*)serialStr, sizeof(serialStr), "%04hu", serial);
|
||||
|
||||
ret = usbshm_open(device->priv, 0xfe17, 0x28dc , serial == 0 ? NULL : serialStr);
|
||||
if(ret)
|
||||
return -3;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fandevice_heartbeat(struct fandevice* device)
|
||||
{
|
||||
int ret;
|
||||
while((ret = usbshm_writeControlTransfer(device->priv, COMMAND_HEARTBEAT, NULL, 0, 0, 0)) == USBSHM_ERROR_AGAIN)
|
||||
usleep(100000);
|
||||
return ret;
|
||||
}
|
||||
|
||||
int fandevice_set_channel(struct fandevice* device, channel_t channel, float speed)
|
||||
{
|
||||
if(channel == FAN_NONE || speed < 0 || speed > 1)
|
||||
return -1;
|
||||
|
||||
uint8_t value = speed*255;
|
||||
|
||||
int ret;
|
||||
while((ret = usbshm_writeControlTransfer(device->priv, COMMAND_SET_FAN, NULL, 0, value, (int)channel)) == USBSHM_ERROR_AGAIN)
|
||||
usleep(100000);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void fandevice_disconnect(struct fandevice* device)
|
||||
{
|
||||
usbshm_distroy(device->priv);
|
||||
free(device->priv);
|
||||
device->priv = NULL;
|
||||
}
|
98
libfandevice/fandevice.h
Normal file
98
libfandevice/fandevice.h
Normal file
@ -0,0 +1,98 @@
|
||||
/* * Copyright (c) 2024 Carl Klemm <carl@uvos.xyz>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation and/or
|
||||
* other materials provided with the distribution.
|
||||
* * Neither the name of %ORGANIZATION% nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
#include <semaphore.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
/**
|
||||
API to control a FanDevice.
|
||||
* @defgroup API User API
|
||||
* This API allows you to control a FanDevice.
|
||||
* @{
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
FAN_A = 0,
|
||||
FAN_B = 1,
|
||||
FAN_C = 2,
|
||||
FAN_D = 3,
|
||||
FAN_NONE = -1,
|
||||
} channel_t;
|
||||
|
||||
enum {
|
||||
COMMAND_HEARTBEAT = 1,
|
||||
COMMAND_SET_FAN = 2,
|
||||
};
|
||||
|
||||
struct fandevice {
|
||||
struct usbshm* priv;
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Attempts to connect to a FanDevice and initializes a fandevice struct
|
||||
* @param device pointer to a FanDevice struct to initialize
|
||||
* @param serial The serial number of the device to connect to, or 0 for any
|
||||
* @return 0 on success and < 0 on failure
|
||||
*/
|
||||
int fandevice_connect(struct fandevice* device, uint16_t serial);
|
||||
|
||||
/**
|
||||
* @brief Sends a heartbeat package to the fan device to avoid it ramping up the fans
|
||||
* @param device pointer to a fandevice struct
|
||||
* @return 0 on success and < 0 on failure
|
||||
*/
|
||||
int fandevice_heartbeat(struct fandevice* device);
|
||||
|
||||
/**
|
||||
* @brief Connects the given channel(s) to the common inputs disconnecting all others
|
||||
* @param device pointer to a fandevice struct
|
||||
* @param channel A channel set the speed for.
|
||||
* @param speed the [0,1] to set the fan to.
|
||||
* @return 0 on success and < 0 on failure
|
||||
*/
|
||||
int fandevice_set_channel(struct fandevice* device, channel_t channel, float speed);
|
||||
|
||||
/**
|
||||
* @brief Disconnects from the fandevice
|
||||
*/
|
||||
void fandevice_disconnect(struct fandevice* device);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
/**
|
||||
....
|
||||
* @}
|
||||
*/
|
212
libfandevice/main.c
Normal file
212
libfandevice/main.c
Normal file
@ -0,0 +1,212 @@
|
||||
/* * Copyright (c) 2023 Carl Klemm <carl@uvos.xyz>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation and/or
|
||||
* other materials provided with the distribution.
|
||||
* * Neither the name of %ORGANIZATION% nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include "fandevice.h"
|
||||
#include "options.h"
|
||||
|
||||
static channel_t char_to_channel(char ch)
|
||||
{
|
||||
switch(ch)
|
||||
{
|
||||
case 'a':
|
||||
case 'A':
|
||||
case '1':
|
||||
return FAN_A;
|
||||
case 'b':
|
||||
case 'B':
|
||||
case '2':
|
||||
return FAN_B;
|
||||
case 'c':
|
||||
case 'C':
|
||||
case '3':
|
||||
return FAN_C;
|
||||
case 'd':
|
||||
case 'D':
|
||||
case '4':
|
||||
return FAN_D;
|
||||
default:
|
||||
return FAN_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
void print_commands(void)
|
||||
{
|
||||
puts("Valid commands:");
|
||||
puts("hearbeat\t | set a heatbeat packet");
|
||||
puts("set [CHANNEL] [SPEED]\t | the the fan speed for a channel");
|
||||
}
|
||||
|
||||
static int process_commands(char** commands, size_t command_count, struct fandevice* device, char* name)
|
||||
{
|
||||
if(strcmp(commands[0], "help") == 0)
|
||||
{
|
||||
print_commands();
|
||||
}
|
||||
if(strcmp(commands[0], "heatbeat") == 0)
|
||||
{
|
||||
int ret = fandevice_heartbeat(device);
|
||||
if(ret < 0)
|
||||
printf("Error writeing to device");
|
||||
else
|
||||
printf("OK");
|
||||
|
||||
}
|
||||
else if(strcmp(commands[0], "set") == 0)
|
||||
{
|
||||
if(command_count < 3)
|
||||
{
|
||||
printf("Usage %s %s [CHANNEL] [SPEED]\n", name, commands[0]);
|
||||
return 2;
|
||||
}
|
||||
channel_t channel = char_to_channel(commands[1][0]);
|
||||
if(channel == FAN_NONE)
|
||||
{
|
||||
printf("%s is not a valid channel\n", commands[1]);
|
||||
return 2;
|
||||
}
|
||||
double speed = strtod(commands[2], NULL);
|
||||
if(speed > 1 || speed < 0)
|
||||
{
|
||||
puts("Speed is out of range [0,1]");
|
||||
return 2;
|
||||
}
|
||||
fandevice_set_channel(device, channel, speed);
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
INPUT_SUCESS = 0,
|
||||
INPUT_NO_INPUT,
|
||||
INPUT_TOO_LONG
|
||||
};
|
||||
|
||||
static int get_input(char *prompt, char *buffer, size_t length)
|
||||
{
|
||||
int ch, extra;
|
||||
|
||||
if(prompt != NULL)
|
||||
{
|
||||
fputs(prompt, stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
if(fgets (buffer, length, stdin) == NULL)
|
||||
return INPUT_NO_INPUT;
|
||||
|
||||
if(buffer[strlen(buffer)-1] != '\n')
|
||||
{
|
||||
extra = 0;
|
||||
while((ch = getchar()) != '\n' && ch != EOF)
|
||||
extra = 1;
|
||||
return (extra == 1) ? INPUT_TOO_LONG : INPUT_SUCESS;
|
||||
}
|
||||
|
||||
buffer[strlen(buffer)-1] = '\0';
|
||||
return INPUT_SUCESS;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
struct config config = {};
|
||||
argp_parse(&argp, argc, argv, 0, 0, &config);
|
||||
|
||||
if(config.list_commands)
|
||||
{
|
||||
print_commands();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if(config.command_count < 1 && !(config.interactive || config.pipe))
|
||||
{
|
||||
printf("A command is required\n");
|
||||
return 2;
|
||||
}
|
||||
|
||||
struct fandevice device;
|
||||
if(fandevice_connect(&device, config.serialSet ? config.serial : 0))
|
||||
{
|
||||
char serialStr[5];
|
||||
snprintf(serialStr, sizeof(serialStr), "%04hu", config.serial);
|
||||
printf("Can not connect to FanDevice%s%s\n",
|
||||
config.serialSet ? " with serial " : "", config.serialSet ? serialStr : "");
|
||||
return 1;
|
||||
}
|
||||
|
||||
int ret = 0;
|
||||
|
||||
if(config.interactive || config.pipe)
|
||||
{
|
||||
char buffer[256];
|
||||
while(true)
|
||||
{
|
||||
int ret = get_input(config.pipe ? NULL : "> ", buffer, sizeof(buffer));
|
||||
if(ret == INPUT_NO_INPUT)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else if(ret != INPUT_SUCESS)
|
||||
{
|
||||
puts("Invalid command");
|
||||
continue;
|
||||
}
|
||||
|
||||
char* commands[MAX_COMMANDS];
|
||||
char* command = strtok(buffer, " \t");
|
||||
size_t i = 0;
|
||||
while(command && i < MAX_COMMANDS)
|
||||
{
|
||||
commands[i] = command;
|
||||
command = strtok(NULL, " \t");
|
||||
++i;
|
||||
}
|
||||
|
||||
int cmdRet = process_commands(commands, i, &device, "");
|
||||
if(cmdRet == 0)
|
||||
puts("OK");
|
||||
else
|
||||
printf("ERR %i\n", cmdRet);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = process_commands(config.commands, config.command_count, &device, argv[0]);
|
||||
}
|
||||
|
||||
fandevice_disconnect(&device);
|
||||
return ret;
|
||||
}
|
109
libfandevice/options.h
Normal file
109
libfandevice/options.h
Normal file
@ -0,0 +1,109 @@
|
||||
/* * Copyright (c) 2023 Carl Klemm <carl@uvos.xyz>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation and/or
|
||||
* other materials provided with the distribution.
|
||||
* * Neither the name of %ORGANIZATION% nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include <argp.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define MAX_COMMANDS 8
|
||||
|
||||
const char *argp_program_version = "fandevice_cli";
|
||||
const char *argp_program_bug_address = "<carl@uvos.xyz>";
|
||||
static char doc[] = "Application to control eismultiplexer devices";
|
||||
static char args_doc[] = "COMMAND";
|
||||
|
||||
static struct argp_option options[] =
|
||||
{
|
||||
{"interactive", 'i', 0, 0, "run in interactive mode" },
|
||||
{"pipe", 'p', 0, 0, "run in pipe mode" },
|
||||
{"serial", 's', "[NUMBER]", 0, "serial number of device to connect to"},
|
||||
{"list", 'l', 0, 0, "list commands"},
|
||||
{0}
|
||||
};
|
||||
|
||||
struct command
|
||||
{
|
||||
char* command;
|
||||
};
|
||||
|
||||
struct config
|
||||
{
|
||||
bool interactive;
|
||||
bool pipe;
|
||||
uint16_t serial;
|
||||
bool serialSet;
|
||||
char* commands[MAX_COMMANDS];
|
||||
size_t command_count;
|
||||
bool list_commands;
|
||||
};
|
||||
|
||||
static error_t parse_opt (int key, char *arg, struct argp_state *state)
|
||||
{
|
||||
struct config *config = (struct config*)(state->input);
|
||||
switch (key)
|
||||
{
|
||||
case 'i':
|
||||
config->interactive = true;
|
||||
break;
|
||||
case 's':
|
||||
{
|
||||
int serial = atoi(arg);
|
||||
if(serial < 0 || serial > UINT16_MAX )
|
||||
{
|
||||
argp_usage(state);
|
||||
break;
|
||||
}
|
||||
config->serial = serial;
|
||||
config->serialSet = true;
|
||||
break;
|
||||
}
|
||||
case 'p':
|
||||
config->pipe = true;
|
||||
break;
|
||||
case 'l':
|
||||
config->list_commands = true;
|
||||
break;
|
||||
case ARGP_KEY_ARG:
|
||||
{
|
||||
if(config->command_count+1 > MAX_COMMANDS)
|
||||
{
|
||||
argp_usage(state);
|
||||
break;
|
||||
}
|
||||
config->commands[config->command_count] = arg;
|
||||
++config->command_count;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
return ARGP_ERR_UNKNOWN;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static struct argp argp = {options, parse_opt, args_doc, doc};
|
7
libfandevice/pkgconfig/fandevice.pc.in
Normal file
7
libfandevice/pkgconfig/fandevice.pc.in
Normal file
@ -0,0 +1,7 @@
|
||||
includedir=@CMAKE_INSTALL_PREFIX@/include
|
||||
|
||||
Name: libfandevice
|
||||
Description: C lib to control the fandevice
|
||||
Version: 1.0
|
||||
Libs: -L${libdir} -lfandevice
|
||||
Cflags: -I${includedir}
|
293
libfandevice/usbshm.c
Normal file
293
libfandevice/usbshm.c
Normal file
@ -0,0 +1,293 @@
|
||||
/* * Copyright (c) 2023 Carl Klemm <carl@uvos.xyz>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation and/or
|
||||
* other materials provided with the distribution.
|
||||
* * Neither the name of %ORGANIZATION% nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "usbshm.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <libusb-1.0/libusb.h>
|
||||
#include <errno.h>
|
||||
#include <unistd.h>
|
||||
|
||||
struct usbshm_priv
|
||||
{
|
||||
libusb_device_handle* handle;
|
||||
struct libusb_transfer* transfer;
|
||||
unsigned char* buffer;
|
||||
};
|
||||
|
||||
static unsigned objectCounter = 0;
|
||||
static struct timeval timeout = {1, 0};
|
||||
static pthread_mutex_t *libusbDataMutex;
|
||||
static pthread_t libusbThread;
|
||||
static atomic_bool threadStop;
|
||||
|
||||
static void usbshm_transferCallBack(struct libusb_transfer *transfer);
|
||||
|
||||
void usbshm_distroy(struct usbshm* instance)
|
||||
{
|
||||
while(!usbshm_ready(instance))
|
||||
sleep(1);
|
||||
free(instance->priv->transfer);
|
||||
libusb_close(instance->priv->handle);
|
||||
free(instance->priv->buffer);
|
||||
free(instance->priv);
|
||||
if(instance->serial)
|
||||
free(instance->serial);
|
||||
if(--objectCounter == 0)
|
||||
{
|
||||
threadStop = true;
|
||||
pthread_join(libusbThread, NULL);
|
||||
libusb_exit(NULL);
|
||||
pthread_mutex_destroy(libusbDataMutex);
|
||||
}
|
||||
}
|
||||
|
||||
static void* usbshm_libusbPoll(void* arg)
|
||||
{
|
||||
while(!threadStop)
|
||||
{
|
||||
libusb_handle_events_timeout_completed(NULL, &timeout, NULL);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
bool usbshm_isOpen(struct usbshm* instance)
|
||||
{
|
||||
return instance->priv->handle != NULL;
|
||||
}
|
||||
|
||||
int usbshm_init(struct usbshm* instance, void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data), void* user_data)
|
||||
{
|
||||
int ret=0;
|
||||
instance->priv = malloc(sizeof(*instance->priv));
|
||||
instance->priv->handle = NULL;
|
||||
instance->priv->transfer = NULL;
|
||||
instance->priv->buffer = NULL;
|
||||
instance->vendorID = 0;
|
||||
instance->productID = 0;
|
||||
instance->serial = NULL;
|
||||
instance->dataCallback = dataCallback;
|
||||
instance->user_data = user_data;
|
||||
if(objectCounter == 0)
|
||||
{
|
||||
ret = libusb_init(NULL) < 0 ? USBSHM_ERROR_ERR : 0;
|
||||
libusbDataMutex = malloc(sizeof(*libusbDataMutex));
|
||||
pthread_mutex_init(libusbDataMutex, NULL);
|
||||
pthread_create(&libusbThread, NULL, &usbshm_libusbPoll, NULL);
|
||||
}
|
||||
if(ret == 0)
|
||||
objectCounter++;
|
||||
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
||||
}
|
||||
|
||||
bool usbshm_ready(struct usbshm* instance)
|
||||
{
|
||||
return instance->priv->transfer == NULL;
|
||||
}
|
||||
|
||||
int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsigned char* serial)
|
||||
{
|
||||
instance->priv->handle = NULL;
|
||||
pthread_mutex_lock(libusbDataMutex);
|
||||
libusb_device** list;
|
||||
int count = libusb_get_device_list(NULL, &list);
|
||||
int errorCode = 0;
|
||||
if( count > 0)
|
||||
{
|
||||
struct libusb_device_descriptor desc = {0};
|
||||
for(int i = 0; i < count; ++i)
|
||||
{
|
||||
libusb_get_device_descriptor(list[i], &desc);
|
||||
if(desc.idVendor == vendorID && desc.idProduct == productID)
|
||||
{
|
||||
errorCode = libusb_open(list[i], &instance->priv->handle) < 0 ? USBSHM_ERROR_ERR : 0;
|
||||
if(serial)
|
||||
{
|
||||
size_t len = strlen((const char*)serial)+1;
|
||||
unsigned char* buffer = malloc(len);
|
||||
buffer[0] = '\0';
|
||||
libusb_get_string_descriptor_ascii(instance->priv->handle, desc.iSerialNumber, buffer, len);
|
||||
int cmp = strcmp((const char*)serial, (const char*)buffer);
|
||||
free(buffer);
|
||||
if(cmp != 0)
|
||||
{
|
||||
libusb_close(instance->priv->handle);
|
||||
instance->priv->handle = NULL;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Can not list devices\n");
|
||||
pthread_mutex_unlock(libusbDataMutex);
|
||||
return USBSHM_ERROR_ERR;
|
||||
}
|
||||
|
||||
if(usbshm_isOpen(instance))
|
||||
{
|
||||
instance->vendorID = vendorID;
|
||||
instance->productID = productID;
|
||||
if(serial)
|
||||
{
|
||||
instance->serial = calloc(strlen((char*)serial), 1);
|
||||
memcpy(instance->serial, serial, strlen((char*)serial));
|
||||
}
|
||||
libusb_set_auto_detach_kernel_driver(instance->priv->handle, true);
|
||||
}
|
||||
else
|
||||
{
|
||||
printf("Opening usb device failed\n");
|
||||
errorCode = USBSHM_ERROR_ERR;
|
||||
}
|
||||
|
||||
libusb_free_device_list(list, count);
|
||||
pthread_mutex_unlock(libusbDataMutex);
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
bool usbshm_usbshm_isOpen(struct usbshm* instance)
|
||||
{
|
||||
pthread_mutex_lock(libusbDataMutex);
|
||||
bool ret = instance->priv->handle != NULL;
|
||||
pthread_mutex_unlock(libusbDataMutex);
|
||||
return ret;
|
||||
}
|
||||
|
||||
void usbshm_reset(struct usbshm* instance)
|
||||
{
|
||||
pthread_mutex_lock(libusbDataMutex);
|
||||
libusb_reset_device(instance->priv->handle);
|
||||
pthread_mutex_unlock(libusbDataMutex);
|
||||
}
|
||||
|
||||
void usbshm_reopen(struct usbshm* instance)
|
||||
{
|
||||
usbshm_reset(instance);
|
||||
libusb_close(instance->priv->handle);
|
||||
usbshm_open(instance, instance->vendorID, instance->productID, instance->serial);
|
||||
}
|
||||
|
||||
int usbshm_writeControlTransfer(struct usbshm* instance, const uint8_t request,
|
||||
char* buffer, const uint8_t length,
|
||||
const uint16_t wValue, const uint16_t wIndex)
|
||||
{
|
||||
if(!usbshm_isOpen(instance))
|
||||
return USBSHM_ERROR_NOT_CONNECTED;
|
||||
if(length > 8)
|
||||
return USBSHM_ERROR_PARAM;
|
||||
if(instance->priv->transfer == NULL)
|
||||
{
|
||||
pthread_mutex_lock(libusbDataMutex);
|
||||
instance->priv->buffer = malloc(length+8);
|
||||
libusb_fill_control_setup(instance->priv->buffer,
|
||||
LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_OUT,
|
||||
request, wValue, wIndex, length );
|
||||
for(uint8_t i = 0; i < length; ++i)
|
||||
instance->priv->buffer[i+8] = buffer[i];
|
||||
instance->priv->transfer = libusb_alloc_transfer(0);
|
||||
libusb_fill_control_transfer(instance->priv->transfer, instance->priv->handle, instance->priv->buffer,
|
||||
&usbshm_transferCallBack, instance, 100);
|
||||
int ret = libusb_submit_transfer(instance->priv->transfer);
|
||||
if(ret < 0)
|
||||
{
|
||||
free(buffer);
|
||||
libusb_free_transfer(instance->priv->transfer);
|
||||
instance->priv->transfer = NULL;
|
||||
if(ret == LIBUSB_ERROR_NO_DEVICE)
|
||||
usbshm_reopen(instance);
|
||||
pthread_mutex_unlock(libusbDataMutex);
|
||||
}
|
||||
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
||||
}
|
||||
else return USBSHM_ERROR_AGAIN;
|
||||
}
|
||||
|
||||
int usbshm_readControlTransfer(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, const uint16_t length)
|
||||
{
|
||||
if(!usbshm_isOpen(instance))
|
||||
return USBSHM_ERROR_NOT_CONNECTED;
|
||||
if(length > 8)
|
||||
return USBSHM_ERROR_PARAM;
|
||||
if(instance->priv->transfer == NULL)
|
||||
{
|
||||
pthread_mutex_lock(libusbDataMutex);
|
||||
instance->priv->buffer = malloc(length+8);
|
||||
libusb_fill_control_setup(instance->priv->buffer, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
|
||||
request, wValue, wIndex, length);
|
||||
instance->priv->transfer = libusb_alloc_transfer(0);
|
||||
libusb_fill_control_transfer(instance->priv->transfer, instance->priv->handle, instance->priv->buffer,
|
||||
&usbshm_transferCallBack, instance, 100);
|
||||
int ret = libusb_submit_transfer(instance->priv->transfer);
|
||||
if(ret < 0)
|
||||
{
|
||||
free(instance->priv->buffer);
|
||||
libusb_free_transfer(instance->priv->transfer);
|
||||
instance->priv->transfer = NULL;
|
||||
if(ret == LIBUSB_ERROR_NO_DEVICE)
|
||||
usbshm_reopen(instance);
|
||||
pthread_mutex_unlock(libusbDataMutex);
|
||||
}
|
||||
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
||||
}
|
||||
else return USBSHM_ERROR_AGAIN;
|
||||
}
|
||||
|
||||
static void usbshm_transferCallBack(struct libusb_transfer *transfer)
|
||||
{
|
||||
struct usbshm* context = (struct usbshm*)transfer->user_data;
|
||||
if(transfer->status != LIBUSB_TRANSFER_COMPLETED)
|
||||
{
|
||||
printf("Usb transfer failed with %d\n", transfer->status);
|
||||
}
|
||||
else if(transfer->type == LIBUSB_TRANSFER_TYPE_CONTROL)
|
||||
{
|
||||
if(context->dataCallback && transfer->length-8 >= transfer->actual_length)
|
||||
context->dataCallback(transfer->buffer[1], transfer->buffer+8, transfer->actual_length, context->user_data);
|
||||
}
|
||||
free(context->priv->buffer);
|
||||
libusb_free_transfer(context->priv->transfer);
|
||||
context->priv->transfer = NULL;
|
||||
pthread_mutex_unlock(libusbDataMutex);
|
||||
}
|
||||
|
||||
int usbshm_readControlTransferSync(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, uint8_t* buffer, const uint16_t length)
|
||||
{
|
||||
pthread_mutex_lock(libusbDataMutex);
|
||||
int ret = libusb_control_transfer(instance->priv->handle, LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE | LIBUSB_ENDPOINT_IN,
|
||||
request, wValue, wIndex, buffer, length, 2000);
|
||||
pthread_mutex_unlock(libusbDataMutex);
|
||||
return ret < 0 ? USBSHM_ERROR_ERR : 0;
|
||||
}
|
74
libfandevice/usbshm.h
Normal file
74
libfandevice/usbshm.h
Normal file
@ -0,0 +1,74 @@
|
||||
/* * Copyright (c) 2023 Carl Klemm <carl@uvos.xyz>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice, this
|
||||
* list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation and/or
|
||||
* other materials provided with the distribution.
|
||||
* * Neither the name of %ORGANIZATION% nor the names of its contributors may be
|
||||
* used to endorse or promote products derived from this software without specific
|
||||
* prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <libusb-1.0/libusb.h>
|
||||
#include <pthread.h>
|
||||
#include <stdatomic.h>
|
||||
#include <stdint.h>
|
||||
#include <time.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
struct usbshm_priv;
|
||||
|
||||
enum {
|
||||
USBSHM_ERROR_AGAIN = -1,
|
||||
USBSHM_ERROR_PARAM = -2,
|
||||
USBSHM_ERROR_ERR = -3,
|
||||
USBSHM_ERROR_NOT_CONNECTED = -4,
|
||||
};
|
||||
|
||||
struct usbshm {
|
||||
struct usbshm_priv* priv;
|
||||
int vendorID;
|
||||
int productID;
|
||||
unsigned char* serial;
|
||||
void* user_data;
|
||||
void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data);
|
||||
};
|
||||
|
||||
void usbshm_distroy(struct usbshm* instance);
|
||||
|
||||
int usbshm_init(struct usbshm* instance, void (*dataCallback)(uint8_t request, unsigned char* data, size_t length, void* user_data), void* user_data);
|
||||
|
||||
bool usbshm_ready(struct usbshm* instance);
|
||||
|
||||
int usbshm_open(struct usbshm* instance, int vendorID, int productID, const unsigned char* serial);
|
||||
|
||||
bool usbshm_isOpen(struct usbshm* instance);
|
||||
|
||||
void usbshm_reset(struct usbshm* instance);
|
||||
|
||||
void usbshm_reopen(struct usbshm* instance);
|
||||
|
||||
int usbshm_writeControlTransfer(struct usbshm* instance, const uint8_t request,
|
||||
char* buffer, const uint8_t length,
|
||||
const uint16_t wValue, const uint16_t wIndex);
|
||||
|
||||
int usbshm_readControlTransfer(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, const uint16_t length);
|
||||
|
||||
int usbshm_readControlTransferSync(struct usbshm* instance, const uint8_t request, const uint16_t wValue, const uint16_t wIndex, uint8_t* buffer, const uint16_t length);
|
||||
|
Reference in New Issue
Block a user