Bluetooth TNC Helper scripts

Bluetooth TNC Helper scripts
This commit is contained in:
TC² 2025-01-22 16:51:20 -05:00
parent 751c92cd7e
commit 93805363fc
3 changed files with 183 additions and 0 deletions

View File

@ -82,6 +82,33 @@ coming soon. This software is experimental and could have bugs. Bug reports and
**[RAW_PACKET_DISPLAY]**
Display RAW packet data on the terminal - "True" to display or "False" to not display
### [OPTIONAL] Steps for Bluetooth TNC Radio Users
1. Run the following command to make the necessary scripts executable.
```sh
chmod +x bt_pair.exp rfcomm_bind.sh
```
2. Install "expect"
```sh
sudo apt install expect
```
3. Pairing radio:
The first time you connect your radio, it will need to be paired and trusted. The bt_pair.exp makes this process easy. It will search for currently known Bluetooth TNC capable radios ("VR-N76" "UV-PRO" "GA-5WB" "TH-D75" "TH-D74" "VR-N7500") If one is found, it will pair, trust, and save the info in a file for the binding script in the next step. This step only needs to be performed once when you're first setting things up, or wanting to use a different radio.
Run the follwing command to go through the pairing process:
```sh
./bt_pair.exp
```
4. Binding the radio to a serial port:
This will need to be run before you start the server (not every time; likely only after a reboot). If you're not sure, you can run the `rfcomm` command and if you see something listed, you probably don't need to run this script. If you get nothing from the `rfcomm` command then you need to run this script to bind your radio to the serial port. After running this command, you can continue on to the "Running the Server" section
Run the following command to bind your radio to the serial port
```sh
./rfcomm_bind.sh
```
### Running the Server

116
bt_pair.exp Normal file
View File

@ -0,0 +1,116 @@
#!/usr/bin/expect -f
# Timeout for expect commands
set timeout 30
# List of target device names
set device_names {"VR-N76" "UV-PRO" "GA-5WB" "TH-D75" "TH-D74" "VR-N7500"}
set group_one {"VR-N76" "UV-PRO" "GA-5WB" "VR-N7500"}
set group_two {"TH-D75" "TH-D74"}
set device ""
set device_group ""
# Function to scan for devices with bluetoothctl
proc scan_for_device {device_names group_one group_two} {
spawn bluetoothctl
expect "Agent registered"
send -- "scan on\r"
puts "Scanning for devices. This may take up to 30 seconds..."
set found_device ""
set found_group ""
set timeout 30
while {1} {
expect {
timeout { puts "No matching devices found."; send -- "scan off\r"; exit 1 }
eof { puts "Unexpected end of bluetoothctl"; exit 1 }
-re {Device ([A-F0-9:]{17}) (.+)} {
set mac $expect_out(1,string)
set name $expect_out(2,string)
foreach device_name $device_names {
if {[string match "*$device_name*" $name]} {
puts "Found device: $name with MAC $mac"
set found_device $mac
foreach group_device $group_one {
if {[string match "*$group_device*" $name]} {
set found_group "GROUP_ONE"
break
}
}
foreach group_device $group_two {
if {[string match "*$group_device*" $name]} {
set found_group "GROUP_TWO"
break
}
}
break
}
}
if {$found_device ne "" && $found_group ne ""} {
break
}
}
}
}
send -- "scan off\r"
expect "Discovery stopped"
return [list $found_device $found_group]
}
# Scan for target devices and set the MAC address
set scan_result [scan_for_device $device_names $group_one $group_two]
set device [lindex $scan_result 0]
set device_group [lindex $scan_result 1]
# Verify the captured MAC address and group
puts "Using device MAC: $device"
puts "Device group: $device_group"
# Validate the MAC address
if {[regexp {^([A-F0-9:]{17})$} $device]} {
puts "Using device MAC: $device"
} else {
puts "Error: Invalid MAC address detected: $device"
exit 1
}
# Start bluetoothctl
spawn bluetoothctl
expect "Agent registered"
# Pair the radio
send -- "pair $device\r"
if {$device_group eq "GROUP_TWO"} {
expect {
"Confirm passkey" {
puts "Passkey confirmation requested for GROUP_TWO."
send -- "yes\r"
exp_continue
}
}
}
expect {
"Pairing successful" { puts "Pairing successful." }
timeout { puts "Pairing failed."; exit 1 }
}
# Trust the radio
send -- "trust $device\r"
expect {
"trust succeeded" { puts "Device trusted." }
timeout { puts "Failed to trust the device."; exit 1 }
}
# Exit bluetoothctl
send -- "exit\r"
expect eof
# Save the MAC address and group info
set script_dir [file dirname [info script]]
set output_file [file join $script_dir "bt_device_info.txt"]
set file [open $output_file "w"]
puts $file "$device $device_group"
close $file
puts "MAC address and group information saved to $output_file."

40
rfcomm_bind.sh Normal file
View File

@ -0,0 +1,40 @@
#!/bin/bash
# Read the MAC address and group from bt_device_info.txt
info_file="bt_device_info.txt"
if [[ ! -f "$info_file" ]]; then
echo "Error: $info_file not found. Ensure the bt_pair.exp script has been run successfully."
exit 1
fi
# Extract the MAC address and group
read -r mac_address device_group < "$info_file"
if [[ -z "$mac_address" || -z "$device_group" ]]; then
echo "Error: Missing MAC address or group information in $info_file."
exit 1
fi
# Determine the rfcomm bind command based on the group
case "$device_group" in
GROUP_ONE)
group_id=1
;;
GROUP_TWO)
group_id=2
;;
*)
echo "Error: Unknown device group: $device_group"
exit 1
;;
esac
# Bind to serial port
sudo rfcomm bind /dev/rfcomm0 "$mac_address" "$group_id"
if [[ $? -eq 0 ]]; then
echo "Successfully bound /dev/rfcomm0 to $mac_address with group ID $group_id."
else
echo "Failed to bind rfcomm device."
fi