current
src
urg_serial_utils_linux.c
Go to the documentation of this file.
1
10
#include "
urg_c/urg_serial_utils.h
"
11
#include <dirent.h>
12
#include <sys/stat.h>
13
#include <string.h>
14
#include <stdio.h>
15
16
17
enum
{
18
MAX_PORTS
= 16,
19
DEVICE_NAME_SIZE
= 255,
20
};
21
22
23
static
char
found_ports
[
MAX_PORTS
][
DEVICE_NAME_SIZE
];
24
static
int
found_ports_size
= 0;
25
26
static
const
char
*
search_dir_names
[] = {
27
"/dev"
,
28
"/dev/usb"
,
29
};
30
static
const
char
*
search_base_names
[] = {
31
"ttyACM"
,
32
"ttyUSB"
,
33
"tty.usbmodem"
,
34
};
35
36
37
static
void
check_base_name
(
const
char
* dir_name,
const
char
*file_name)
38
{
39
int
n =
sizeof
(
search_base_names
) /
sizeof
(
search_base_names
[0]);
40
int
i;
41
42
for
(i = 0; i < n; ++i) {
43
const
char
*base_name =
search_base_names
[i];
44
if
(!strncmp(base_name, file_name, strlen(base_name))) {
45
snprintf(
found_ports
[
found_ports_size
],
DEVICE_NAME_SIZE
,
46
"%s/%s"
, dir_name, file_name);
47
++
found_ports_size
;
48
}
49
}
50
}
51
52
53
int
urg_serial_find_port
(
void
)
54
{
55
int
n =
sizeof
(
search_dir_names
) /
sizeof
(
search_dir_names
[0]);
56
int
i;
57
58
found_ports_size
= 0;
59
for
(i = 0; i < n; ++i) {
60
struct
dirent* dir;
61
const
char
*dir_name =
search_dir_names
[i];
62
DIR *dp = opendir(dir_name);
63
if
(!dp) {
64
continue
;
65
}
66
67
while
((dir = readdir(dp))) {
68
check_base_name
(dir_name, dir->d_name);
69
}
70
}
71
return
found_ports_size
;
72
}
73
74
75
const
char
*
urg_serial_port_name
(
int
index)
76
{
77
if
((index < 0) || (index >=
found_ports_size
)) {
78
return
""
;
79
}
else
{
80
return
found_ports
[index];
81
}
82
}
83
84
85
int
urg_serial_is_urg_port
(
int
index)
86
{
87
// Linux の場合、ポートが URG かどうかは断定できない
88
// !!! 余力があれば、dmesg などの出力から判定するようにしてもよい
89
(void)index;
90
return
0;
91
}
urg_serial_utils.h
シリアル用の補助関数
urg_serial_is_urg_port
int urg_serial_is_urg_port(int index)
ポートが URG かどうか
Definition:
urg_serial_utils_linux.c:85
MAX_PORTS
@ MAX_PORTS
Definition:
urg_serial_utils_linux.c:18
found_ports_size
static int found_ports_size
Definition:
urg_serial_utils_linux.c:24
search_base_names
static const char * search_base_names[]
Definition:
urg_serial_utils_linux.c:30
search_dir_names
static const char * search_dir_names[]
Definition:
urg_serial_utils_linux.c:26
urg_serial_find_port
int urg_serial_find_port(void)
シリアルポートを検索する
Definition:
urg_serial_utils_linux.c:53
urg_serial_port_name
const char * urg_serial_port_name(int index)
検索したシリアルポート名を返す
Definition:
urg_serial_utils_linux.c:75
check_base_name
static void check_base_name(const char *dir_name, const char *file_name)
Definition:
urg_serial_utils_linux.c:37
DEVICE_NAME_SIZE
@ DEVICE_NAME_SIZE
Definition:
urg_serial_utils_linux.c:19
found_ports
static char found_ports[MAX_PORTS][DEVICE_NAME_SIZE]
Definition:
urg_serial_utils_linux.c:23
urg_c
Author(s): Satofumi Kamimura
, Katsumi Kimoto, Adrian Boeing
autogenerated on Wed Mar 2 2022 01:08:11