diff --git a/.vscode/settings.json b/.vscode/settings.json index cbe4c24..40c4294 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -34,7 +34,10 @@ "mpl-args.h": "c", "*.in": "cpp", "signal.h": "c", - "stdio.h": "c" + "stdio.h": "c", + "getopt.h": "c", + "mpl-common.h": "c", + "cerrno": "c" }, "cmake.configureOnOpen": false } \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 8b5b116..ac66859 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.18.4) project(mportlink) -# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Os") +set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Os") set(VERSION_MAJOR 0) set(VERSION_MINOR 1) diff --git a/src/mpl-common.c b/src/mpl-common.c new file mode 100644 index 0000000..355c8a3 --- /dev/null +++ b/src/mpl-common.c @@ -0,0 +1,111 @@ +/* + * mpl-common.c + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + +#include "mpl-common.h" + +#include +#include + +#define LOCKFILE "/run/lock/mportlink.lock" + +static int mpl_create_dongle_dir() +{ + struct stat st; + + if (stat(MPL_DONGLE_PATH, &st) == 0) + { + if (!S_ISDIR(st.st_mode)) + { + MPLOG(LOG_WARNING, "%s exists and is not a directory", MPL_DONGLE_PATH); + return 1; + } + } + else if (mkdir(MPL_DONGLE_PATH, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) + { + MPLOG(LOG_ERR, "unable to create a directory %s", MPL_DONGLE_PATH); + return 1; + } + + return 0; +} + +void static mpl_free(MPL *mpl) +{ + if (mpl) + free(mpl); +} + +MPL *mpl_init(MPL_PARAMETERS *parameters) +{ + MPL *mpl = (MPL *)calloc(1, sizeof(MPL)); + + if (!mpl) + { + MPLOG(LOG_ERR, "error getting the MPL structure"); + exit(1); + } + + mpl->parameters.check_device = parameters->check_device; + mpl->cancellable.signal = 0; + mpl->cancellable.exit = 0; + + mpl->fd = open(LOCKFILE, O_CREAT | O_RDWR, 0644); + if (mpl->fd < 0) + { + MPLOG(LOG_ERR, "unable to create a lockfile"); + exit(1); + } + + int rc = flock(mpl->fd, LOCK_EX | LOCK_NB); + if (rc || errno == EWOULDBLOCK) + { + MPLOG(LOG_ERR, "only one instance of %s is allowed", MPL_NAME); + exit(1); + } + + if (mpl_create_dongle_dir()) + { + MPLOG(LOG_ERR, "%s will be stopped", MPL_NAME); + exit(1); + } + + if (mpl_udev_init(&mpl->udev)) + { + MPLOG(LOG_ERR, "%s will be stopped", MPL_NAME); + mpl_free(mpl); + exit(1); + } + + MPLOG(LOG_NOTICE, "starting the %s daemon", MPL_NAME); + + return mpl; +} + +void mpl_stop(MPL *mpl) +{ + mpl_udev_free(&mpl->udev); + + if (mpl->cancellable.exit) + { + flock(mpl->fd, LOCK_UN); + MPLOG(LOG_NOTICE, "stopping the %s daemon", MPL_NAME); + } + else + MPLOG(LOG_NOTICE, "successful completion of the process %d", getpid()); + + mpl_free(mpl); + + closelog(); +} + +void mpl_loop(MPL *mpl) +{ + if (mpl->parameters.check_device) + mpl_udev_check_devices(&mpl->udev); + + mpl_udev_loop(&mpl->udev, &mpl->cancellable); +} diff --git a/src/mpl-common.h b/src/mpl-common.h new file mode 100644 index 0000000..fd85c45 --- /dev/null +++ b/src/mpl-common.h @@ -0,0 +1,31 @@ +/* + * mpl-common.h + * + * by Alexander Zhirov (alexander@zhirov.kz) + * Telegram @alexanderzhirov + */ + +#ifndef MPL_COMMON_H_ +#define MPL_COMMON_H_ + +#include "mpl-lib.h" +#include "mpl-udev.h" + +typedef struct +{ + int check_device; +} MPL_PARAMETERS; + +typedef struct +{ + int fd; + MPL_PARAMETERS parameters; + MPL_CANCELLABLE cancellable; + MPL_UDEV udev; +} MPL; + +MPL *mpl_init(MPL_PARAMETERS *parameters); +void mpl_stop(MPL *mpl); +void mpl_loop(MPL *mpl); + +#endif diff --git a/src/mpl-core.c b/src/mpl-core.c index d60fc6a..8f93523 100644 --- a/src/mpl-core.c +++ b/src/mpl-core.c @@ -5,108 +5,83 @@ * Telegram @alexanderzhirov */ +#include "mpl-lib.h" #include "mpl-core.h" -#include -#include -#include +#include +#include +#include -#define LOCKFILE "/run/lock/mportlink.lock" -#define DEV_PATH "/dev/" -#define DONGLE_PATH (DEV_PATH "dongle/") - -static int mpl_create_dongle_dir() +int device_path(const char *dirname, int num) { - struct stat st; + DIR *dir; + struct dirent *ent; + regex_t regex; + const char *pattern = "^ttyUSB[0-9]*$"; - if (stat(DONGLE_PATH, &st) == 0) + int ret = regcomp(®ex, pattern, REG_EXTENDED); + if (ret != 0) { - if (!S_ISDIR(st.st_mode)) - { - MPLOG(LOG_WARNING, "%s exists and is not a directory", DONGLE_PATH); - return 1; - } - } - else if (mkdir(DONGLE_PATH, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) - { - MPLOG(LOG_ERR, "unable to create a directory %s", DONGLE_PATH); + printf("Failed to compile regular expression\n"); return 1; } + dir = opendir(dirname); + if (dir == NULL) + { + printf("Failed to open directory\n"); + regfree(®ex); + return 1; + } + + while ((ent = readdir(dir)) != NULL) + { + if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) + { + continue; + } + + ret = regexec(®ex, ent->d_name, 0, NULL, 0); + + if (ret == 0) + { + printf("Found a file matching the pattern: %s\n", ent->d_name); + + char *path = (char *)calloc(strlen(MPL_DEV_PATH) + strlen(ent->d_name) + 1, sizeof(char)); + strcpy(path, MPL_DEV_PATH); + strcat(path, ent->d_name); + + int fd = open(path, O_RDWR); + if (fd < 0) + { + printf("Не удалось открыть USB устройство.\n"); + free(path); + continue; + } + + char *tty = ttyname(fd); + close(fd); + + if (tty != NULL) + { + printf("Найдено устройство tty: %s\n", tty); + + // if (!num) + // print_imei(tty); + } + else + { + printf("Устройство tty не найдено\n"); + } + } + else if (ret != REG_NOMATCH) + { + printf("Failed to apply regular expression\n"); + } + } + + regfree(®ex); + closedir(dir); + return 0; } - -void static mpl_free(MPL *mpl) -{ - if (mpl) - free(mpl); -} - -MPL *mpl_init(MPL_PARAMETERS *parameters) -{ - MPL *mpl = (MPL *)calloc(1, sizeof(MPL)); - - if (!mpl) - { - MPLOG(LOG_ERR, "error getting the MPL structure"); - exit(1); - } - - mpl->parameters.check_device = parameters->check_device; - mpl->cancellable.signal = 0; - mpl->cancellable.exit = 0; - - mpl->fd = open(LOCKFILE, O_CREAT | O_RDWR, 0644); - if (mpl->fd < 0) - { - MPLOG(LOG_ERR, "unable to create a lockfile"); - exit(1); - } - - int rc = flock(mpl->fd, LOCK_EX | LOCK_NB); - if (rc || errno == EWOULDBLOCK) - { - MPLOG(LOG_ERR, "only one instance of %s is allowed", MPL_NAME); - exit(1); - } - - if (mpl_create_dongle_dir()) - { - MPLOG(LOG_ERR, "%s will be stopped", MPL_NAME); - exit(1); - } - - if (mpl_udev_init(&mpl->udev)) - { - MPLOG(LOG_ERR, "%s will be stopped", MPL_NAME); - mpl_free(mpl); - exit(1); - } - - MPLOG(LOG_NOTICE, "starting the %s daemon", MPL_NAME); - - return mpl; -} - -void mpl_stop(MPL *mpl) -{ - mpl_udev_free(&mpl->udev); - - flock(mpl->fd, LOCK_UN); - - mpl_free(mpl); - - if (mpl->cancellable.exit) - MPLOG(LOG_NOTICE, "stopping the %s daemon", MPL_NAME); - else - MPLOG(LOG_NOTICE, "successful completion of the process %d", getpid()); - closelog(); -} - -void mpl_loop(MPL *mpl) -{ - if (mpl->parameters.check_device) - mpl_udev_check_devices(&mpl->udev); - - mpl_udev_loop(&mpl->udev, &mpl->cancellable); -} diff --git a/src/mpl-core.h b/src/mpl-core.h index 2047178..76bc1b1 100644 --- a/src/mpl-core.h +++ b/src/mpl-core.h @@ -8,24 +8,6 @@ #ifndef MPL_CORE_H_ #define MPL_CORE_H_ -#include "mpl-lib.h" -#include "mpl-udev.h" - -typedef struct -{ - int check_device; -} MPL_PARAMETERS; - -typedef struct -{ - int fd; - MPL_PARAMETERS parameters; - MPL_CANCELLABLE cancellable; - MPL_UDEV udev; -} MPL; - -MPL *mpl_init(MPL_PARAMETERS *parameters); -void mpl_stop(MPL *mpl); -void mpl_loop(MPL *mpl); +int device_path(const char *dirname, int num); #endif diff --git a/src/mpl-lib.h b/src/mpl-lib.h index 4facc06..ca818f0 100644 --- a/src/mpl-lib.h +++ b/src/mpl-lib.h @@ -14,8 +14,11 @@ #include #include #include +#include #define MPL_NAME "mportlink" +#define MPL_DEV_PATH "/dev/" +#define MPL_DONGLE_PATH (MPL_DEV_PATH "dongle/") #define MPLOG(PRIORITY, fmt, ...) do { \ char buf[256]; \ diff --git a/src/mpl-udev.c b/src/mpl-udev.c index 27ccaf2..acfee46 100644 --- a/src/mpl-udev.c +++ b/src/mpl-udev.c @@ -6,6 +6,7 @@ */ #include "mpl-udev.h" +#include "mpl-core.h" #include #include @@ -80,12 +81,13 @@ int mpl_udev_check_devices(MPL_UDEV *udev) continue; const char *sysattr_value = udev_device_get_sysattr_value(dev, "bInterfaceNumber"); - printf("Номер порта: %s\n", sysattr_value); + printf("Number of port: %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); @@ -184,6 +186,5 @@ void mpl_udev_loop(MPL_UDEV *udev, MPL_CANCELLABLE *cancellable) if (cancellable->exit) return; - sleep(5); - printf("exit"); + device_path(path_to_device, port_number); } diff --git a/src/mpl.c b/src/mpl.c index 74e3a36..11c66b7 100644 --- a/src/mpl.c +++ b/src/mpl.c @@ -5,9 +5,8 @@ * Telegram @alexanderzhirov */ -#include - #include "mpl.h" +#include static MPL *mpl; diff --git a/src/mpl.h b/src/mpl.h index e5975dc..ab0284e 100644 --- a/src/mpl.h +++ b/src/mpl.h @@ -12,7 +12,6 @@ #define VERSION "unrelease" #endif -#include "mpl-lib.h" -#include "mpl-core.h" +#include "mpl-common.h" #endif