diff --git a/.vscode/settings.json b/.vscode/settings.json index 1dba064..48fe63b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -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": [ diff --git a/CMakeLists.txt b/CMakeLists.txt index 992027f..2188cc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/src/mportlink-core.c b/src/mpl-core.c similarity index 90% rename from src/mportlink-core.c rename to src/mpl-core.c index bff39bc..3f3e8c2 100644 --- a/src/mportlink-core.c +++ b/src/mpl-core.c @@ -1,40 +1,20 @@ /* - * mportlink-core.c + * mpl-core.c * * by Alexander Zhirov (alexander@zhirov.kz) * Telegram @alexanderzhirov */ -#include #include #include -#include -#include -#include -#include -#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 diff --git a/src/mportlink-core.h b/src/mpl-core.h similarity index 54% rename from src/mportlink-core.h rename to src/mpl-core.h index e6f8956..8872ac0 100644 --- a/src/mportlink-core.h +++ b/src/mpl-core.h @@ -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 - -#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 diff --git a/src/mportlink-lib.c b/src/mpl-lib.c similarity index 64% rename from src/mportlink-lib.c rename to src/mpl-lib.c index 2c588b8..a7f4fbf 100644 --- a/src/mportlink-lib.c +++ b/src/mpl-lib.c @@ -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" diff --git a/src/mpl-lib.h b/src/mpl-lib.h new file mode 100644 index 0000000..69fb474 --- /dev/null +++ b/src/mpl-lib.h @@ -0,0 +1,38 @@ +/* + * mpl-lib.h + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + +#ifndef MPL_LIB_H_ +#define MPL_LIB_H_ + +#include +#include +#include +#include +#include +#include + +#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 diff --git a/src/mpl-udev.c b/src/mpl-udev.c new file mode 100644 index 0000000..266502b --- /dev/null +++ b/src/mpl-udev.c @@ -0,0 +1,94 @@ +/* + * mpl-udev.c + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + +#include "mpl-lib.h" +#include "mpl-udev.h" +#include + +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); +} diff --git a/src/mpl-udev.h b/src/mpl-udev.h new file mode 100644 index 0000000..b1ef2e7 --- /dev/null +++ b/src/mpl-udev.h @@ -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); diff --git a/src/mpl.c b/src/mpl.c new file mode 100644 index 0000000..4e24828 --- /dev/null +++ b/src/mpl.c @@ -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; +} diff --git a/src/mportlink.h b/src/mpl.h similarity index 62% rename from src/mportlink.h rename to src/mpl.h index cfcdfff..6eccea7 100644 --- a/src/mportlink.h +++ b/src/mpl.h @@ -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 diff --git a/src/mportlink-lib.h b/src/mportlink-lib.h deleted file mode 100644 index a893af3..0000000 --- a/src/mportlink-lib.h +++ /dev/null @@ -1,13 +0,0 @@ -/* - * mportlink-lib.h - * - * by Alexander Zhirov (alexander@zhirov.kz) - * Telegram @alexanderzhirov - */ - -#ifndef MPL_LIB_H_ -#define MPL_LIB_H_ - - - -#endif diff --git a/src/mportlink.c b/src/mportlink.c deleted file mode 100644 index da9d0bd..0000000 --- a/src/mportlink.c +++ /dev/null @@ -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; -}