Running rigctld automatically when yo plug in your Digirig
Want to run rigctld whenever you plug in your digirig? Here it is, mind-dump fashion!
rigctld
?
If you’ve installed hamlib
on your computer, you have a program called rigctl
and its daemon counterpart, rigctld
. This is a little TCP server that listens for connections and “does stuff” with your connected radios. If you don’t know what this is, I wouldn’t recommend reading teh rest of this blog post until you’ve gotten up to speed on it!
Note! Pretty much all of the commands in this post will need to be run as root. We’re messing with the fundamental underpinnings of the operating system you’re using. If you’re not comfortable with that, do not continue.
As always, make sure you have backups, make sure you know what these commands do. I will try to explain as much as possible, but I do not know what you do not know.
Systemd Unit File
System Whosiwhatsit?
systemd
is the init system for most modern linux distributions. It is among the first programs that is launched by the kernel on boot, and is in charge of everything from loading the kernel modules on boot to managing services like networking and DNS. Ever needed to run a systemctl
command? Well, that’s interacting with systemd
.
A “unit file” is a “unit of work” that systemd
performs. It a single service which runs, like a web server, or, in our case, rigctld
!
The Process
First, you have to make a unit file. You need to know what device your digirig is before you continue with this.
You should make a user to use rigctld
. Not strictly necessary, but a good practice.
adduser --system --group --home /var/lib/rigctld
usermod -aG dialout rigctld
Then, you need a unit file named /etc/systemd/system/rigctld.service
:
[Unit]
Description=rigctld Ham Radio Controller
[Service]
ExecStart=/usr/bin/rigctld -m 1 -p /dev/{SERIAL-DEVICE} -P RTS
User=rigctld
Group=rigctld
[Install]
WantedBy=multi-user.target
We will edit this file a little later. For now, SERIAL-DEVICE
is probably something like ttyUSB0
. If you want to follow the next “step”, you’ll need to find this. The slowest and dirtiest way of doing that is to unplug your digirig, check your /dev
directory to see what’s there, then plug it back in and see what entry shows up.i There are better ways, like running udevadm monitor
and then unplugging, and looking for the appropriate text. . .but, eh. You can figure that out, I have faith. :)
Once you have that device file in hand and entered into the unit file, check that you can bring up the service:
systemctl daemon-reload
systemctl status -l rigctld.service
○ rigctld.service - rigctld Ham Radio Controller
Loaded: loaded (/etc/systemd/system/rigctld.service; disabled; vendor preset: enabled)
Active: inactive (dead)
systemctl start rigctld.service
systemctl status -l rigctld.service
● rigctld.service - rigctld Ham Radio Controller
Loaded: loaded (/etc/systemd/system/rigctld.service; disabled; vendor preset: enabled)
Active: active (running) since Wed 2024-02-21 17:34:11 EST; 3s ago
Main PID: 11426 (rigctld)
Tasks: 1 (limit: 76789)
Memory: 3.0M
CPU: 5ms
CGroup: /system.slice/rigctld.service
└─11426 /usr/bin/rigctld -m 1 -p /dev/digirig-serial -P RTS
udev
Rules
udev
is the subsystem on most modern Linux systems which handles phsyical devices. It is responsible for creating the device’s file representation in the Linux filesystem (that is, the listings in /dev
and /sys
), as well as spinning up appropriate drivers and the like.
Adding rules to udev
is a sort of super power that even some of the most hardened Linux wizards you know probably don’t use on any sort of regular basis. With udev
rules, you can do everything from customizing what permissions the devices have when they’re added to the filesystem to running scripts when specific devices connect (spoilers: this is what we’re going to do).
Serial Rules
Get the ID
You need to know which device is your digirig’s serial device, which you needed to test the service in the last section.
Once you have that, run this command:
udevadm info /dev/ttyUSB0 | grep ID_SERIAL_SHORT
That will give you something like this:
E: ID_SERIAL_SHORT=0a248736067eed118e01b4814fc49859
You need the hex number after the equals sign (=
), 0a248736067eed118e01b4814fc49859
in this case.
Make the rule file
Make a new file called /etc/udev/rules.d/71-rigctld.rules
that looks like this:
SUBSYSTEMS=="usb-serial", GOTO="digirig_usb_rules"
GOTO="digirig_usb_rules_end"
LABEL="digirig_usb_rules"
ACTION=="add", ENV{ID_SERIAL_SHORT}=="0a248736067eed118e01b4814fc49859", SYMLINK+="digirig-serial", MODE="660", GROUP="dialout"
LABEL="digirig_usb_rules_end"
What this does is say:
“Hey, while you’re bringing up a usb-serial
device, if that device has this serial number, make a new symbolic link to it in my /dev
directory that has the name digirig-serial
, this accessibility, and this group”.
This makes it so that you always have a known name to reference by rigctld
.
Audio Rules
Get your IDs
First, you need to get a vendor ID and a product ID for your digirig.
Put simply, type:
lsusb
and you will get a result that looks like this:
Bus 003 Device 039: ID 0d8c:013c C-Media Electronics, Inc. CM108 Audio Controller
HEY LISTEN The actual output of
lsusb
will be pretty big, probably, so don’t get scared! look for something that says something like “CM108 Audio COntroller and that’s probably right!”
In that string, you’ll see two hexadecimal numbers separated by a colon.
- The left one,
0d8c
in this case, is the vendor ID. - The right one,
013c
in this case, is the product ID.
Put those numbers in the relevant places in the next step.
Make the rule file
Make a new file called /etc/udev/rules.d/72-persistent-audio-name.rules
that looks like this:
SUBSYSTEM=="sound", GOTO="my_usb_audio_start"
GOTO="my_usb_audio_end"
LABEL="my_usb_audio_start"
ACTION=="add", ATTRS{idVendor}=="0d8c", ATTRS{idProduct}=="013c", ATTR{id}="digirig", RUN+="/usr/bin/systemctl start rigctld.service"
ACTION=="remove", ATTRS{idVendor}=="0d8c", ATTRS{idProduct}=="013c", RUN+="/usr/bin/systemctl stop rigctld.service"
LABEL="my_usb_audio_end"
Here, we’re saying two things:
- “Hey, when you’re bringing up a new
sound
device, if that device has a vendor ID that is0d8c
and a product id that is013c
, give it the hardware iddigirig
, and also run this command to bring up therigctld
service. - When I remove the device described above, run this command to stop the
rigctld
service.
This file will execute after the serial device rules, so the serial device will already be configured, and the symlink to the proper /dev
listing will already be present.
We’re also giving the audio device a specific name so that you can find it easily with something like direwolf
.
Bringing it all together
From here, just edit your /etc/systemd/system/rigctld.service
file to use the new and permanent device listing for your digirig:
[Unit]
Description=rigctld Ham Radio Controller
[Service]
ExecStart=/usr/bin/rigctld -m 1 -p /dev/digirig-serial -P RTS
User=rigctld
Group=rigctld
[Install]
WantedBy=multi-user.target
Now, when you run a systemctl daemon-reload
and udevadm trigger
, try plugging your digirig in and checking the status of rigctld
:
systemctl status -l rigctld.service