-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinosSetRadioPorts
executable file
·229 lines (165 loc) · 5.87 KB
/
MinosSetRadioPorts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env perl
# Script to update Minos Radio Comport configuration based on identified USB->tty Devices
# by Stewart Wilkinson G0LGS
# Created: 20 Feb 2021
# Note: Minos is sensitive to changes to the config files and
# will reject the file if the case of values are changed
# therefore the radio definitions must match exactly
# Requires: libconfig-inifiles-perl, perl-doc
use warnings;
use strict;
use Config::IniFiles;
use File::Basename;
use File::stat;
use Sys::Syslog qw(:DEFAULT setlogsock);
use Getopt::Long qw(VersionMessage :config no_auto_abbrev );
use Pod::Usage qw(pod2usage);
use Data::Dumper;
use POSIX qw/strftime/;
# Define Radios expected in Minos and port alias created by udev
my %Radios = ();
$Radios{"IC-9700"}= { 'port' => '/dev/ic9700a'};
$Radios{"IC-7300"}= { 'port' => '/dev/ic7300'};
#$Radios{"IC-7100"}= { 'port' => '/dev/ic7100'};
#$Radios{"FT-817"}= { 'port' => '/dev/ft817'};
# You should not need to edit below #
# --------------------------------- #
=head1 NAME
MinosSetRadioPorts
=cut
our($VERSION)="0.5";
our($VERDATE)="06/04/2024";
=head1 VERSION
V0.5 06/04/2024
=cut
my($DEBUG)=0;
my($VERBOSE)=0;
my($QUIET)=0;
my($ExitVal)=0;
my($Updated)=0;
my($CfgPath);
my(@comment);
push(@comment,"; Updated @" .strftime("%d/%m/%Y %H:%M", localtime) ." by MinosSetRadioPorts (created by Stewart G0LGS)");
# Get command options
if( ! GetOptions (
'DEBUG+' => \$DEBUG,
'V|VERBOSE+' => \$VERBOSE,
'ver|version' => sub { VersionMessage( "-exitval" => -1 ); },
'Q|QUIET' => sub { $QUIET=1; $VERBOSE=0; },
'c|cfg=s' => \$CfgPath,
'h|help|?' => sub { pod2usage( -verbose => 99, -sections => "NAME|VERSION|SYNOPSIS|DESCRIPTION|COPYRIGHT" ); },
'man' => sub { pod2usage( -verbose => 2, -noperldoc => 1 ); },
) ) {
pod2usage( { -message => "Wrong or Missing Paramaters!\n", -verbose => 0} );
exit 1;
}
setlogsock("unix");
openlog(basename($0), "pid", "local3");
if( !defined($CfgPath) ) {
pod2usage( { -message => "-cfg Paramater is Required.\n", -verbose => 0} );
syslog("err", "-cfg Paramater is Required.");
exit 1;
}
# Remove any Trailing /
$CfgPath =~ s/\/$//;
my($IniPath) = $CfgPath. '/Radio/AvailRadio.ini';
# Check File exists
if( ! -e -f $IniPath ){
printf STDERR "ERROR: %s was not found\n", $IniPath unless($QUIET);
syslog("err", "%s was not found", $IniPath);
exit 1;
}
# Get File Owner
my $gid = stat($IniPath)->gid;
my $uid = stat($IniPath)->uid;
# Get File Perms
my $mode = stat($IniPath)->mode;
# Read the Ini File
my $MinosIni = Config::IniFiles->new(-file => $IniPath );
# Report any Error with reading the file
unless( $MinosIni ){
printf STDERR "Errors were found in %s:\n%s\n", $IniPath, join("\n", @Config::IniFiles::errors);
syslog("err", "Errors were found in %s: %s", $IniPath, join("", @Config::IniFiles::errors) );
exit 1;
}
local $Data::Dumper::Terse=1;
print STDOUT Data::Dumper->Dump( [ $MinosIni ], [ qw{ *MinosIni } ] ) if($DEBUG);
# Loop through the list (hash) of Radios
foreach my $radio (keys %Radios) {
if( defined $Radios{$radio}{'port'} ){
my $RadioPort = $Radios{$radio}{'port'};
printf "Checking %s link as %s\n", $radio, $RadioPort if($VERBOSE);
# Does the defined /dev/name symlink exist ?
if ( -e $RadioPort ) {
printf "Found %s\n", $RadioPort if($VERBOSE);
# read the link
my $ComPort = readlink $RadioPort;
if( $ComPort ) {
if ($MinosIni->exists($radio, 'comport')) {
if( $MinosIni->setval($radio, 'comport', $ComPort ) ){
$Updated++;
printf "Updated port for %s OK\n", $radio if($VERBOSE);
$MinosIni->SetParameterComment($radio, 'comport', @comment);
syslog("info", "Updated port for %s applied", $RadioPort );
}else{
printf "WARNING: Failed to update port for %s\n", $radio if($VERBOSE);
syslog("warning", "Failed to update port for %s", $radio );
}
} else {
printf "WARNING: No Minos Radio defined for %s\n", $radio if($VERBOSE);
syslog("warning", "No Minos Radio defined for %s", $radio );
}
}else{
printf "WARNING: %s is not a valid link\n", $RadioPort if($VERBOSE);
syslog("warning", "%s is not a valid link", $RadioPort );
}
}else{
printf "WARNING: %s not found or not valid.\n", $RadioPort unless($QUIET);
syslog("warning", "%s not found or not valid", $RadioPort ) if($VERBOSE);
}
}else{
printf STDERR "ERROR: No port alias defined for %s\n", $radio unless($QUIET);
syslog("err", "No port alias defined for %s", $radio );
$ExitVal++;
}
}
# Write Updates to the file
if ( $Updated ) {
$MinosIni->RewriteConfig();
# If 'root' Ensure we retain Owner / Perms
if( $< == 0 ){
chown $uid, $gid, $IniPath;
chmod $mode, $IniPath;
}
}
closelog();
exit $ExitVal;
__END__
=head1 SYNOPSIS
MinosSetRadioPorts [options]
MinosSetRadioPorts -man | -help
options:
-Q | -QUIET
-V | -VERBOSE
-cfg <Path_to_minos_configuration_folder>
=head1 DESCRIPTION
B<MinosSetRadioPorts> Updates Minos Radio configuration file (AvailRadio.ini) to have the correct
ttyUSB interface values each time radios with USB to CAT interfaces are connected to the Computer. It
relies on a correctly configured udev rule to call this program each time a matching USB device is
detected.
The program will be run by the kernel so in effect has 'root' access and should therefore have access
to any files/folders, it should ensure that the original permissions are retained on modification
of the configuration file.
=head1 OPTIONS
=over 4
=item B<-V> | B<-VERBOSE>
The program does will only write fatal Error messages to the stdout/stderr, unless Verbose is set.
=item B<-Q> | B<-QUIET>
Be Quiet - turns off Verbose option
=item B<-c> | B<-cfg> <path>
The path to the Minos Configuration Folder - this is a required parameter, the script will exit
with an error if not specified or if the folder cannot be access.
=back
=head1 COPYRIGHT
MinosSetRadioPorts is B<Copyright> (c) 2021-2024 by Stewart Wilkinson (G0LGS) ([email protected])
=cut