urg_serial_utils_linux.c
Go to the documentation of this file.
00001 
00010 #include "urg_c/urg_serial_utils.h"
00011 #include <dirent.h>
00012 #include <sys/stat.h>
00013 #include <string.h>
00014 #include <stdio.h>
00015 
00016 
00017 enum {
00018     MAX_PORTS = 16,
00019     DEVICE_NAME_SIZE = 255,
00020 };
00021 
00022 
00023 static char found_ports[MAX_PORTS][DEVICE_NAME_SIZE];
00024 static int found_ports_size = 0;
00025 
00026 static const char *search_dir_names[] = {
00027     "/dev",
00028     "/dev/usb",
00029 };
00030 static const char *search_base_names[] = {
00031     "ttyACM",
00032     "ttyUSB",
00033     "tty.usbmodem",
00034 };
00035 
00036 
00037 static void check_base_name(const char* dir_name, const char *file_name)
00038 {
00039     int n = sizeof(search_base_names) / sizeof(search_base_names[0]);
00040     int i;
00041 
00042     for (i = 0; i < n; ++i) {
00043         const char *base_name = search_base_names[i];
00044         if (!strncmp(base_name, file_name, strlen(base_name))) {
00045             snprintf(found_ports[found_ports_size], DEVICE_NAME_SIZE,
00046                      "%s/%s", dir_name, file_name);
00047             ++found_ports_size;
00048         }
00049     }
00050 }
00051 
00052 
00053 int urg_serial_find_port(void)
00054 {
00055     int n = sizeof(search_dir_names) / sizeof(search_dir_names[0]);
00056     int i;
00057 
00058     found_ports_size = 0;
00059     for (i = 0; i < n; ++i) {
00060         struct dirent* dir;
00061         const char *dir_name = search_dir_names[i];
00062         DIR *dp = opendir(dir_name);
00063         if (!dp) {
00064             continue;
00065         }
00066 
00067         while ((dir = readdir(dp))) {
00068             check_base_name(dir_name, dir->d_name);
00069         }
00070     }
00071     return found_ports_size;
00072 }
00073 
00074 
00075 const char *urg_serial_port_name(int index)
00076 {
00077     if ((index < 0) || (index >= found_ports_size)) {
00078         return "";
00079     } else {
00080         return found_ports[index];
00081     }
00082 }
00083 
00084 
00085 int urg_serial_is_urg_port(int index)
00086 {
00087     // Linux の場合、ポートが URG かどうかは断定できない
00088     // !!! 余力があれば、dmesg などの出力から判定するようにしてもよい
00089     (void)index;
00090     return 0;
00091 }


urg_c
Author(s): Satofumi Kamimura , Katsumi Kimoto, Adrian Boeing
autogenerated on Thu Jun 6 2019 19:06:57