Added udev code

This commit is contained in:
Alexander Zhirov 2023-12-24 02:33:54 +03:00
parent 78e8b7e491
commit bb72f57776
12 changed files with 212 additions and 70 deletions

View File

@ -23,7 +23,14 @@
"mportlink-core.h": "c",
"mportlink.h": "c",
"mportlink-lib.h": "c",
"errno.h": "c"
"errno.h": "c",
"stdlib.h": "c",
"string.h": "c",
"libudev.h": "c",
"mpl-lib.h": "c",
"mpl-core.h": "c",
"mpl.h": "c",
"mpl-udev.h": "c"
},
"cmake.configureOnOpen": false,
"editor.rulers": [

View File

@ -5,7 +5,7 @@ file(GLOB_RECURSE SRC_FILES_LIST src/*.c)
find_package(PkgConfig REQUIRED)
pkg_search_module(UDEV REQUIRED udev)
pkg_search_module(UDEV REQUIRED libudev)
include_directories(${UDEV_INCLUDE_DIRS})
link_directories(${UDEV_LIBRARY_DIRS})

View File

@ -1,40 +1,20 @@
/*
* mportlink-core.c
* mpl-core.c
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#include <syslog.h>
#include <sys/stat.h>
#include <sys/file.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "mportlink-lib.h"
#include "mportlink-core.h"
#include "mpl-core.h"
#define LOCKFILE "/run/lock/mportlink.lock"
#define DEV_PATH "/dev/"
#define DONGLE_PATH (DEV_PATH "dongle/")
#define MPLOG(PRIORITY, fmt, ...) do { \
char buf[256]; \
snprintf(buf, sizeof(buf), fmt __VA_OPT__(,) __VA_ARGS__); \
PRIORITY < LOG_WARNING ? \
syslog(PRIORITY, "%s(): %s (%s)", __func__, buf, \
strerror(errno)) : \
syslog(PRIORITY, "%s(): %s", __func__, buf); \
} while(0)
static void signals_handler(int signum)
{
MPLOG(LOG_WARNING, "cancelling the main loop (%d)", signum);
}
void mpl_create_dongle_dir()
static void mpl_create_dongle_dir()
{
struct stat st;
@ -71,14 +51,10 @@ int mpl_init()
MPLOG(LOG_NOTICE, "Starting the %s daemon", MPL);
signal(SIGINT, signals_handler);
signal(SIGHUP, signals_handler);
signal(SIGTERM, signals_handler);
return lock_fd;
}
int mpl_stop(const int lock_fd)
void mpl_stop(const int lock_fd)
{
flock(lock_fd, LOCK_UN);
@ -86,7 +62,22 @@ int mpl_stop(const int lock_fd)
closelog();
}
MPL_UDEV *mpl_new()
{
MPL_UDEV *mpl_udev = (MPL_UDEV *)calloc(1, sizeof(MPL_UDEV));
if (mpl_udev == NULL) {
MPLOG(LOG_ERR, "Error getting the udev structure");
exit(EXIT_FAILURE);
}
return mpl_udev;
}
void mpl_loop(MPL_UDEV *mpl_udev)
{
}
// typedef struct

View File

@ -1,5 +1,5 @@
/*
* mportlink-core.h
* mpl-core.h
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
@ -8,11 +8,12 @@
#ifndef MPL_CORE_H_
#define MPL_CORE_H_
#include <stdlib.h>
#define MPL "mportlink"
#include "mpl-lib.h"
int mpl_init();
int mpl_stop(const int lock_fd);
void mpl_stop(const int lock_fd);
void mpl_loop(MPL_UDEV *mpl_udev);
MPL_UDEV *mpl_new();
#endif

View File

@ -1,8 +1,8 @@
/*
* mportlink-lib.c
* mpl-lib.c
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#include "mportlink-lib.h"
#include "mpl-lib.h"

38
src/mpl-lib.h Normal file
View File

@ -0,0 +1,38 @@
/*
* mpl-lib.h
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#ifndef MPL_LIB_H_
#define MPL_LIB_H_
#include <stdlib.h>
#include <signal.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
#define MPL "mportlink"
#define MPLOG(PRIORITY, fmt, ...) do { \
char buf[256]; \
snprintf(buf, sizeof(buf), fmt __VA_OPT__(,) __VA_ARGS__); \
PRIORITY < LOG_WARNING ? \
syslog(PRIORITY, "%s(): %s (%s)", __func__, buf, \
strerror(errno)) : \
syslog(PRIORITY, "%s(): %s", __func__, buf); \
} while(0)
typedef struct _MPL_UDEV MPL_UDEV;
struct _MPL_UDEV
{
struct udev *udev;
struct udev_monitor *monitor;
int udev_fd;
};
#endif

94
src/mpl-udev.c Normal file
View File

@ -0,0 +1,94 @@
/*
* mpl-udev.c
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#include "mpl-lib.h"
#include "mpl-udev.h"
#include <libudev.h>
void mpl_udev_init(MPL_UDEV *mpl_udev)
{
mpl_udev->udev = udev_new();
if (mpl_udev->udev == NULL) {
MPLOG(LOG_ERR, "Error creating udev context");
exit(EXIT_FAILURE);
}
mpl_udev->monitor = udev_monitor_new_from_netlink(mpl_udev->udev, "udev");
if (mpl_udev->monitor == NULL) {
MPLOG(LOG_ERR, "Error creating udev monitor");
udev_unref(mpl_udev->udev);
exit(EXIT_FAILURE);
}
udev_monitor_filter_add_match_subsystem_devtype(mpl_udev->monitor,
"usb", NULL);
udev_monitor_enable_receiving(mpl_udev->monitor);
mpl_udev->udev_fd = udev_monitor_get_fd(mpl_udev->monitor);
if (mpl_udev->udev_fd < 0) {
MPLOG(LOG_ERR, "Error creating udev monitor descriptor");
udev_unref(mpl_udev->udev);
exit(EXIT_FAILURE);
}
}
void mpl_udev_check_devices(MPL_UDEV *mpl_udev)
{
struct udev_device *dev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
enumerate = udev_enumerate_new(mpl_udev->udev);
if (enumerate == NULL) {
MPLOG(LOG_ERR, "Error creating udev enumerate");
exit(EXIT_FAILURE);
}
udev_enumerate_add_match_subsystem(enumerate, "usb");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
if (devices == NULL) {
MPLOG(LOG_ERR, "Error get udev devices");
exit(EXIT_FAILURE);
}
udev_list_entry_foreach(dev_list_entry, devices) {
const char *syspath = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(mpl_udev->udev, syspath);
const char *devtype = udev_device_get_devtype(dev);
if (!(strcmp(devtype, "usb_interface") == 0))
continue;
const char *driver = udev_device_get_driver(dev);
if (!(strcmp(driver, "option") == 0))
continue;
const char *sysattr_value = udev_device_get_sysattr_value(dev,
"bInterfaceNumber");
printf("Номер порта: %s\n", sysattr_value);
// int num = atoi(sysattr_value);
const char *devsyspath = udev_device_get_syspath(dev);
printf("syspath: %s\n", devsyspath);
// device_path(syspaths, num);
udev_device_unref(dev);
}
udev_enumerate_unref(enumerate);
}
mpl_udev_free(MPL_UDEV *mpl_udev)
{
udev_monitor_unref(mpl_udev->monitor);
udev_unref(mpl_udev->udev);
}

11
src/mpl-udev.h Normal file
View File

@ -0,0 +1,11 @@
/*
* mpl-udev.h
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#include "mpl-lib.h"
void mpl_udev_init(MPL_UDEV *mpl_udev);
void mpl_udev_check_devices(MPL_UDEV *mpl_udev);

29
src/mpl.c Normal file
View File

@ -0,0 +1,29 @@
/*
* mpl.c
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#include "mpl.h"
static void signals_handler(int signum)
{
MPLOG(LOG_WARNING, "cancelling the main loop (%d)", signum);
}
int main()
{
int lock_fd = mpl_init();
MPL_UDEV *mpl = mpl_new();
mpl_udev_init(mpl);
signal(SIGINT, signals_handler);
signal(SIGHUP, signals_handler);
signal(SIGTERM, signals_handler);
mpl_stop(lock_fd);
return EXIT_SUCCESS;
}

View File

@ -1,5 +1,5 @@
/*
* mportlink.h
* mpl.h
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
@ -8,7 +8,8 @@
#ifndef MPL_H_
#define MPL_H_
#include "mportlink-lib.h"
#include "mportlink-core.h"
#include "mpl-lib.h"
#include "mpl-core.h"
#include "mpl-udev.h"
#endif

View File

@ -1,13 +0,0 @@
/*
* mportlink-lib.h
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#ifndef MPL_LIB_H_
#define MPL_LIB_H_
#endif

View File

@ -1,17 +0,0 @@
/*
* mportlink.c
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#include "mportlink.h"
int main()
{
int lock_fd = mpl_init();
mpl_stop(lock_fd);
return EXIT_SUCCESS;
}