This commit is contained in:
Alexander Zhirov 2023-12-25 17:06:59 +03:00
parent cd5f9f7a12
commit ee679a128b
10 changed files with 226 additions and 122 deletions

View File

@ -34,7 +34,10 @@
"mpl-args.h": "c", "mpl-args.h": "c",
"*.in": "cpp", "*.in": "cpp",
"signal.h": "c", "signal.h": "c",
"stdio.h": "c" "stdio.h": "c",
"getopt.h": "c",
"mpl-common.h": "c",
"cerrno": "c"
}, },
"cmake.configureOnOpen": false "cmake.configureOnOpen": false
} }

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.18.4) cmake_minimum_required(VERSION 3.18.4)
project(mportlink) 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_MAJOR 0)
set(VERSION_MINOR 1) set(VERSION_MINOR 1)

111
src/mpl-common.c Normal file
View File

@ -0,0 +1,111 @@
/*
* mpl-common.c
*
* by Alexander Zhirov (alexander@zhirov.kz)
* Telegram @alexanderzhirov
*/
#include "mpl-common.h"
#include <sys/stat.h>
#include <sys/file.h>
#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);
}

31
src/mpl-common.h Normal file
View File

@ -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

View File

@ -5,108 +5,83 @@
* Telegram @alexanderzhirov * Telegram @alexanderzhirov
*/ */
#include "mpl-lib.h"
#include "mpl-core.h" #include "mpl-core.h"
#include <sys/stat.h> #include <regex.h>
#include <sys/file.h> #include <dirent.h>
#include <unistd.h> #include <fcntl.h>
#define LOCKFILE "/run/lock/mportlink.lock" int device_path(const char *dirname, int num)
#define DEV_PATH "/dev/"
#define DONGLE_PATH (DEV_PATH "dongle/")
static int mpl_create_dongle_dir()
{ {
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(&regex, pattern, REG_EXTENDED);
if (ret != 0)
{ {
if (!S_ISDIR(st.st_mode)) printf("Failed to compile regular expression\n");
{
MPLOG(LOG_WARNING, "%s exists and is not a directory", DONGLE_PATH);
return 1; return 1;
} }
}
else if (mkdir(DONGLE_PATH, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1) dir = opendir(dirname);
if (dir == NULL)
{ {
MPLOG(LOG_ERR, "unable to create a directory %s", DONGLE_PATH); printf("Failed to open directory\n");
regfree(&regex);
return 1; return 1;
} }
while ((ent = readdir(dir)) != NULL)
{
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
{
continue;
}
ret = regexec(&regex, 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(&regex);
closedir(dir);
return 0; 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);
}

View File

@ -8,24 +8,6 @@
#ifndef MPL_CORE_H_ #ifndef MPL_CORE_H_
#define MPL_CORE_H_ #define MPL_CORE_H_
#include "mpl-lib.h" int device_path(const char *dirname, int num);
#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 #endif

View File

@ -14,8 +14,11 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <syslog.h> #include <syslog.h>
#include <unistd.h>
#define MPL_NAME "mportlink" #define MPL_NAME "mportlink"
#define MPL_DEV_PATH "/dev/"
#define MPL_DONGLE_PATH (MPL_DEV_PATH "dongle/")
#define MPLOG(PRIORITY, fmt, ...) do { \ #define MPLOG(PRIORITY, fmt, ...) do { \
char buf[256]; \ char buf[256]; \

View File

@ -6,6 +6,7 @@
*/ */
#include "mpl-udev.h" #include "mpl-udev.h"
#include "mpl-core.h"
#include <unistd.h> #include <unistd.h>
#include <sys/wait.h> #include <sys/wait.h>
@ -80,12 +81,13 @@ int mpl_udev_check_devices(MPL_UDEV *udev)
continue; continue;
const char *sysattr_value = udev_device_get_sysattr_value(dev, "bInterfaceNumber"); 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); // int num = atoi(sysattr_value);
const char *devsyspath = udev_device_get_syspath(dev); const char *devsyspath = udev_device_get_syspath(dev);
printf("syspath: %s\n", devsyspath); printf("syspath: %s\n", devsyspath);
// device_path(syspaths, num); // device_path(syspaths, num);
udev_device_unref(dev); udev_device_unref(dev);
@ -184,6 +186,5 @@ void mpl_udev_loop(MPL_UDEV *udev, MPL_CANCELLABLE *cancellable)
if (cancellable->exit) if (cancellable->exit)
return; return;
sleep(5); device_path(path_to_device, port_number);
printf("exit");
} }

View File

@ -5,9 +5,8 @@
* Telegram @alexanderzhirov * Telegram @alexanderzhirov
*/ */
#include <getopt.h>
#include "mpl.h" #include "mpl.h"
#include <getopt.h>
static MPL *mpl; static MPL *mpl;

View File

@ -12,7 +12,6 @@
#define VERSION "unrelease" #define VERSION "unrelease"
#endif #endif
#include "mpl-lib.h" #include "mpl-common.h"
#include "mpl-core.h"
#endif #endif