Add conditional for udev presence in Linux.

This commit is contained in:
2019-10-20 16:34:38 +01:00
parent 5ad1aad8f5
commit beff90faa8
3 changed files with 61 additions and 43 deletions

View File

@@ -5,13 +5,14 @@ set(CMAKE_C_STANDARD 90)
set(MAIN_SOURCES main.c list_devices.c device.c scsi.c hex2bin.c usb.c ieee1394.c pcmcia.c ata.c sdhci.c) set(MAIN_SOURCES main.c list_devices.c device.c scsi.c hex2bin.c usb.c ieee1394.c pcmcia.c ata.c sdhci.c)
if("${CMAKE_SYSTEM}" MATCHES "Linux") if ("${CMAKE_SYSTEM}" MATCHES "Linux")
set(PLATFORM_SOURCES linux/list_devices.c linux/linux.h linux/device.c linux/scsi.c linux/usb.c linux/ieee1394.c linux/pcmcia.c linux/ata.c linux/sdhci.c) set(PLATFORM_SOURCES linux/list_devices.c linux/linux.h linux/device.c linux/scsi.c linux/usb.c linux/ieee1394.c linux/pcmcia.c linux/ata.c linux/sdhci.c)
endif() find_library(HAS_UDEV NAMES udev)
endif ()
add_executable(dicremote ${MAIN_SOURCES} ${PLATFORM_SOURCES}) add_executable(dicremote ${MAIN_SOURCES} ${PLATFORM_SOURCES})
# TODO: Properly check udev exists if (HAS_UDEV)
if("${CMAKE_SYSTEM}" MATCHES "Linux")
target_link_libraries(dicremote udev) target_link_libraries(dicremote udev)
endif() add_definitions(-DHAS_UDEV)
endif ()

View File

@@ -19,12 +19,15 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <libudev.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#ifdef HAS_UDEV
#include <libudev.h>
#endif
int LinuxOpenDevice(const char* device_path) int LinuxOpenDevice(const char* device_path)
{ {
int fd; int fd;
@@ -38,6 +41,9 @@ int LinuxOpenDevice(const char* device_path)
int32_t LinuxGetDeviceType(const char* device_path) int32_t LinuxGetDeviceType(const char* device_path)
{ {
#ifndef HAS_UDEV
return DICMOTE_DEVICE_TYPE_UNKNOWN;
#else
struct udev* udev; struct udev* udev;
struct udev_device* udev_device; struct udev_device* udev_device;
const char* tmp_string; const char* tmp_string;
@@ -120,26 +126,26 @@ int32_t LinuxGetDeviceType(const char* device_path)
} }
int32_t LinuxGetSdhciRegisters(const char* device_path, int32_t LinuxGetSdhciRegisters(const char* device_path,
char** csd, char** csd,
char** cid, char** cid,
char** ocr, char** ocr,
char** scr, char** scr,
uint32_t* csd_len, uint32_t* csd_len,
uint32_t* cid_len, uint32_t* cid_len,
uint32_t* ocr_len, uint32_t* ocr_len,
uint32_t* scr_len) uint32_t* scr_len)
{ {
char* tmp_string; char* tmp_string;
char* sysfs_path_csd; char* sysfs_path_csd;
char* sysfs_path_cid; char* sysfs_path_cid;
char* sysfs_path_scr; char* sysfs_path_scr;
char* sysfs_path_ocr; char* sysfs_path_ocr;
size_t len; size_t len;
FILE* file; FILE* file;
*csd = NULL; *csd = NULL;
*cid = NULL; *cid = NULL;
*ocr = NULL; *ocr = NULL;
*scr = NULL; *scr = NULL;
*csd_len = 0; *csd_len = 0;
*cid_len = 0; *cid_len = 0;
*ocr_len = 0; *ocr_len = 0;
@@ -148,12 +154,12 @@ int32_t LinuxGetSdhciRegisters(const char* device_path,
if(strncmp(device_path, "/dev/mmcblk", 11) != 0) return 0; if(strncmp(device_path, "/dev/mmcblk", 11) != 0) return 0;
len = strlen(device_path) + 19; len = strlen(device_path) + 19;
sysfs_path_csd = malloc(len); sysfs_path_csd = malloc(len);
sysfs_path_cid = malloc(len); sysfs_path_cid = malloc(len);
sysfs_path_scr = malloc(len); sysfs_path_scr = malloc(len);
sysfs_path_ocr = malloc(len); sysfs_path_ocr = malloc(len);
tmp_string = malloc(1024); tmp_string = malloc(1024);
if(!sysfs_path_csd || !sysfs_path_cid || !sysfs_path_scr || !sysfs_path_ocr || !tmp_string) if(!sysfs_path_csd || !sysfs_path_cid || !sysfs_path_scr || !sysfs_path_ocr || !tmp_string)
{ {
@@ -191,7 +197,7 @@ int32_t LinuxGetSdhciRegisters(const char* device_path,
if(*csd_len <= 0) if(*csd_len <= 0)
{ {
*csd_len = 0; *csd_len = 0;
*csd = NULL; *csd = NULL;
} }
} }
@@ -213,7 +219,7 @@ int32_t LinuxGetSdhciRegisters(const char* device_path,
if(*cid_len <= 0) if(*cid_len <= 0)
{ {
*cid_len = 0; *cid_len = 0;
*cid = NULL; *cid = NULL;
} }
} }
@@ -235,7 +241,7 @@ int32_t LinuxGetSdhciRegisters(const char* device_path,
if(*scr_len <= 0) if(*scr_len <= 0)
{ {
*scr_len = 0; *scr_len = 0;
*scr = NULL; *scr = NULL;
} }
} }
@@ -257,7 +263,7 @@ int32_t LinuxGetSdhciRegisters(const char* device_path,
if(*ocr_len <= 0) if(*ocr_len <= 0)
{ {
*ocr_len = 0; *ocr_len = 0;
*ocr = NULL; *ocr = NULL;
} }
} }
@@ -272,4 +278,5 @@ int32_t LinuxGetSdhciRegisters(const char* device_path,
free(tmp_string); free(tmp_string);
return csd_len != 0 || cid_len != 0 || scr_len != 0 || ocr_len != 0; return csd_len != 0 || cid_len != 0 || scr_len != 0 || ocr_len != 0;
#endif
} }

View File

@@ -20,7 +20,6 @@
#include <ctype.h> #include <ctype.h>
#include <dirent.h> #include <dirent.h>
#include <libudev.h>
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
@@ -28,23 +27,29 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#ifdef HAS_UDEV
#include <libudev.h>
#endif
DeviceInfoList* LinuxListDevices() DeviceInfoList* LinuxListDevices()
{ {
DIR* dir; DIR* dir;
struct dirent* dirent; struct dirent* dirent;
int i;
DeviceInfoList *list_start = NULL, *list_current = NULL, *list_next = NULL;
const char* tmp_string;
FILE* file;
char* line_str;
size_t n, ret;
char* chrptr;
#ifdef HAS_UDEV
int has_udev;
struct udev* udev; struct udev* udev;
int has_udev = false, i;
DeviceInfoList * list_start = NULL, *list_current = NULL, *list_next = NULL;
struct udev_device* udev_device; struct udev_device* udev_device;
const char* tmp_string;
FILE* file;
char* line_str;
size_t n, ret;
char* chrptr;
udev = udev_new();
udev = udev_new();
has_udev = udev != 0; has_udev = udev != 0;
#endif
dir = opendir(PATH_SYS_DEVBLOCK); dir = opendir(PATH_SYS_DEVBLOCK);
if(!dir) return NULL; if(!dir) return NULL;
@@ -66,8 +71,9 @@ DeviceInfoList* LinuxListDevices()
{ {
closedir(dir); closedir(dir);
#ifdef HAS_UDEV
if(has_udev) udev_unref(udev); if(has_udev) udev_unref(udev);
#endif
return list_start; return list_start;
} }
@@ -77,6 +83,7 @@ DeviceInfoList* LinuxListDevices()
snprintf(list_next->this.path, 1024, "/dev/%s", dirent->d_name); snprintf(list_next->this.path, 1024, "/dev/%s", dirent->d_name);
#ifdef HAS_UDEV
if(has_udev) if(has_udev)
{ {
udev_device = udev_device_new_from_subsystem_sysname(udev, "block", dirent->d_name); udev_device = udev_device_new_from_subsystem_sysname(udev, "block", dirent->d_name);
@@ -127,6 +134,7 @@ DeviceInfoList* LinuxListDevices()
} }
} }
} }
#endif
tmp_string = malloc(1024); tmp_string = malloc(1024);
memset((void*)tmp_string, 0, 1024); memset((void*)tmp_string, 0, 1024);
@@ -299,7 +307,9 @@ DeviceInfoList* LinuxListDevices()
closedir(dir); closedir(dir);
#ifdef HAS_UDEV
if(has_udev) udev_unref(udev); if(has_udev) udev_unref(udev);
#endif
return list_start; return list_start;
} }