@dotpi/tools 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/DOC.md +66 -0
  2. package/LICENSE +28 -0
  3. package/NOTES.md +81 -0
  4. package/README.md +341 -0
  5. package/dotpi-tools.js +35 -0
  6. package/dotpi_root/bin/dotpi +66 -0
  7. package/dotpi_root/bin/dotpi_firstrun +193 -0
  8. package/dotpi_root/bin/dotpi_prepare_sd_card +616 -0
  9. package/dotpi_root/bin/dotpi_prepare_system +244 -0
  10. package/dotpi_root/etc/dotpi_environment_default.bash +69 -0
  11. package/dotpi_root/etc/dotpi_tarignore +3 -0
  12. package/dotpi_root/share/dotpi-install/update_to_v1.0.0_manager +39 -0
  13. package/dotpi_root/share/dotpi-install/update_to_v1.0.0_pi +39 -0
  14. package/dotpi_root/share/dotpi_audio_bluetooth/dotpi_audio_bluetooth.service +23 -0
  15. package/dotpi_root/share/dotpi_audio_bluetooth/dotpi_audio_bluetooth_command +134 -0
  16. package/dotpi_root/share/dotpi_audio_bluetooth/dotpi_audio_pipewire_relink +132 -0
  17. package/dotpi_root/share/dotpi_avahi_monitor/dotpi_avahi_monitor.service +22 -0
  18. package/dotpi_root/share/dotpi_avahi_monitor/dotpi_avahi_monitor_command +14 -0
  19. package/dotpi_root/share/dotpi_environment.bash +5 -0
  20. package/dotpi_root/share/dotpi_functions/dotpi_functions_audio_bluetooth.bash +346 -0
  21. package/dotpi_root/share/dotpi_functions/dotpi_functions_audio_device.bash +504 -0
  22. package/dotpi_root/share/dotpi_functions/dotpi_functions_configuration.bash +435 -0
  23. package/dotpi_root/share/dotpi_functions/dotpi_functions_connection.bash +199 -0
  24. package/dotpi_root/share/dotpi_functions/dotpi_functions_dotpi.bash +36 -0
  25. package/dotpi_root/share/dotpi_functions/dotpi_functions_service.bash +111 -0
  26. package/dotpi_root/share/dotpi_functions/dotpi_functions_system.bash +175 -0
  27. package/dotpi_root/share/dotpi_functions/dotpi_functions_utils.bash +154 -0
  28. package/dotpi_root/share/dotpi_functions.bash +14 -0
  29. package/dotpi_root/share/dotpi_init.bash +18 -0
  30. package/dotpi_root/share/jackd/dotpi_jackd_bluetooth.bash +18 -0
  31. package/dotpi_root/share/jackd/dotpi_jackd_command +26 -0
  32. package/dotpi_root/share/jackd/dotpi_jackd_headphones.bash +21 -0
  33. package/dotpi_root/share/jackd/dotpi_jackd_hifiberry.bash +17 -0
  34. package/dotpi_root/share/jackd/jackd.service +22 -0
  35. package/dotpi_root/share/limits/zzz_dotpi_audio.conf +11 -0
  36. package/dotpi_root/share/wireplumber/bluetooth.lua.d/8zzz-dotpi-wireplumber-config.lua +4 -0
  37. package/examples/example-development/configuration/dotpi_project.bash +40 -0
  38. package/examples/example-headphones/configuration/dotpi_project.bash +18 -0
  39. package/examples/example-hifiberry/configuration/dotpi_instance_012.bash +4 -0
  40. package/examples/example-hifiberry/configuration/dotpi_project.bash +18 -0
  41. package/examples/example-network/configuration/dotpi_project.bash +21 -0
  42. package/package.json +35 -0
  43. package/src/configure-host.js +187 -0
  44. package/src/constants.js +30 -0
  45. package/src/create-project.js +438 -0
  46. package/src/install-rpi.js +173 -0
  47. package/src/templates/README.md +16 -0
  48. package/src/templates/ap.nmconnection +25 -0
  49. package/src/templates/dotpi_project.bash +31 -0
  50. package/src/templates/dotpi_secrets.bash +4 -0
  51. package/src/templates/infrastructure.nmconnection +29 -0
  52. package/src/templates/ssh_config +6 -0
  53. package/src/utils.js +175 -0
  54. package/test/constants.spec.js +37 -0
  55. package/test/create-project.test.js +57 -0
  56. package/test/install-rpi.test.js +8 -0
  57. package/test/utils.spec.js +48 -0
@@ -0,0 +1,244 @@
1
+ #!/bin/bash
2
+
3
+ set +e
4
+
5
+ error_exit() {
6
+ error_code="${1:-255}"
7
+ error_context="${2:-dotpi_prepare_system}"
8
+ exit_code="${3:-255}"
9
+ exit_message="${4:-System is NOT READY. Please flash SD card again.}"
10
+
11
+ dotpi echo_error "dotpi_prepare_system: ${error_context}: error ${error_code}"
12
+ dotpi echo_error "${exit_message}"
13
+
14
+ exit "$exit_code"
15
+ }
16
+
17
+ ############### bootstrap
18
+ error_exit_code_init=1
19
+
20
+ # This script MUST be executed by `pi` user
21
+ FIRSTUSER="$(getent passwd 1000 | cut -d: -f1)"
22
+ if [[ $(whoami) != "${FIRSTUSER}" ]] ; then
23
+ error_exit 255 "This script MUST be executed by pi user (or first user)" $error_exit_code_init
24
+ fi
25
+
26
+ DOTPI_ROOT="${DOTPI_ROOT:-/opt/dotpi}"
27
+ source "${DOTPI_ROOT}/share/dotpi_init.bash" || {
28
+ error_exit $? "source dotpi_init.bash" $error_exit_code_init
29
+ }
30
+
31
+ # set wifi country code
32
+ #
33
+ # This is also done earlier by `dotpi_firstrun` but doing this
34
+ # only in one place fail for some unknown reason
35
+ dotpi echo_info "raspi-config nonint do_wifi_country ${dotpi_wifi_country_code}"
36
+ sudo raspi-config nonint do_wifi_country "${dotpi_wifi_country_code}" || {
37
+ error_exit $? "set wifi country code" $error_exit_code_init
38
+ }
39
+
40
+ # unblock wifi in any case
41
+ sudo nmcli radio wifi on
42
+ sudo rfkill unblock wifi
43
+
44
+ ############### system packages via apt
45
+ error_exit_code_package=2
46
+
47
+ # in case dpkg was interrupted
48
+ sudo dpkg --configure -a || {
49
+ error_exit $? dpkg $error_exit_code_package
50
+ }
51
+
52
+ dotpi echo_info "Uninstall apt packages: ${dotpi_apt_uninstall_default[@]} ${dotpi_apt_uninstall[@]}"
53
+ sudo dotpi apt_get purge "${dotpi_apt_uninstall_default[@]}" "${dotpi_apt_uninstall[@]}" || {
54
+ error_exit $? 'apt_get purge' $error_exit_code_package
55
+ }
56
+
57
+ sudo dotpi apt_get clean || {
58
+ error_exit $? 'apt_get clean' $error_exit_code_package
59
+ }
60
+ sudo dotpi apt_get autoremove -y || {
61
+ error_exit $? 'apt_get autoremove' $error_exit_code_package
62
+ }
63
+
64
+ dotpi echo_info "Update and dist-upgrade"
65
+ sudo dotpi apt_get update || {
66
+ error_exit $? 'apt_get update' $error_exit_code_package
67
+ }
68
+ sudo dotpi apt_get dist-upgrade || {
69
+ error_exit $? 'apt_get dist-upgrade' $error_exit_code_package
70
+ }
71
+
72
+ dotpi echo_info "Install apt packages: ${dotpi_apt_install_default[@]} ${dotpi_apt_install[@]}"
73
+ sudo dotpi apt_get install "${dotpi_apt_install_default[@]}" "${dotpi_apt_install[@]}" || {
74
+ error_exit $? 'apt_get install' $error_exit_code_package
75
+ }
76
+
77
+
78
+ ############### node.js
79
+ error_exit_code_node=3
80
+
81
+ dotpi echo_info "Install Node.js"
82
+
83
+ word_size="$(dotpi file_word_size "$(which file)" )"
84
+ if [[ $word_size == "32" ]] ; then
85
+ node_arch=armv7l
86
+ else
87
+ node_arch=arm64
88
+ fi
89
+
90
+ # install n for sudo usage
91
+ N_PREFIX="${DOTPI_ROOT}/share/n"
92
+ sudo rm -rf -- "$N_PREFIX" || {
93
+ error_exit $? "rm ${N_PREFIX}" $error_exit_code_node
94
+ }
95
+
96
+
97
+ sudo mkdir -p -- "$N_PREFIX" || {
98
+ error_exit $? "mkdir ${N_PREFIX}" $error_exit_code_node
99
+ }
100
+
101
+ while ! curl -L https://bit.ly/n-install -o /tmp/n-install ; do
102
+ dotpi echo_warning "Error while retriving n-install"
103
+ sleep 1
104
+ echo "trying again"
105
+ done
106
+
107
+ while ! sudo N_PREFIX="${DOTPI_ROOT}/share/n" bash /tmp/n-install -y -a "$node_arch" "$dotpi_node_version" ; do
108
+ dotpi echo_warning "Error with n-install"
109
+ sleep 1
110
+ echo "trying again"
111
+ sudo rm -rf -- "$N_PREFIX"
112
+ sudo mkdir -p -- "$N_PREFIX"
113
+ done
114
+
115
+ rm /tmp/n-install
116
+ sudo bash -c "cat >> '/root/.bashrc' << EOF
117
+
118
+ # dotpi: force the usage of -a for correct architecture
119
+ # (n relies on uname, which tells about the kernel, not the executables)
120
+
121
+ alias n='n -a ${node_arch}'
122
+
123
+ EOF
124
+ " || {
125
+ error_exit $? "n alias in /root/.bashrc" $error_exit_code_node
126
+ }
127
+
128
+ source="${N_PREFIX}/bin"
129
+ destination="/usr/bin"
130
+ executables=(
131
+ corepack
132
+ n
133
+ node
134
+ npm
135
+ npx
136
+ )
137
+ for executable in "${executables[@]}" ; do
138
+ sudo ln -s -f "${source}/${executable}" "${destination}/${executable}" || {
139
+ error_exit $? "symlink to ${destination}/${executable}" $error_exit_code_node
140
+ }
141
+ done
142
+
143
+ ############## services
144
+ error_exit_code_services=4
145
+
146
+ # allow to run user services without login
147
+ sudo loginctl enable-linger "$FIRSTUSER" || {
148
+ error_exit $? "loginctl enable-linger" $error_exit_code_services
149
+ }
150
+
151
+ ############# avahi (multicast DNS, bonjour)
152
+
153
+ # restart when hostname is not resolved by mDNS
154
+ sudo dotpi service_install "${DOTPI_ROOT}/share/dotpi_avahi_monitor/dotpi_avahi_monitor.service" || {
155
+ error_exit $? "install dotpi_avahi_monitor.service" $error_exit_code_services
156
+ }
157
+
158
+ ############## pipewire (and jackd compatibility)
159
+ error_exit_code_pipewire=6
160
+
161
+ # over-ride calls to jackd
162
+ # copy with prefix 0- to ensure it is loaded before default aarch64-linux-gnu.conf
163
+ for f in /usr/share/doc/pipewire/examples/ld.so.conf.d/pipewire-jack-*.conf ; do
164
+ source_basename="$(basename "$f")"
165
+ destination_basename="0-${source_basename}"
166
+ sudo cp "$f" "/etc/ld.so.conf.d/${destination_basename}" || {
167
+ error_exit $? "install ${f}" $error_exit_code_pipewire
168
+ }
169
+ done
170
+ sudo ldconfig || {
171
+ error_exit $? "ldconfig" $error_exit_code_pipewire
172
+ }
173
+
174
+ systemctl --user enable --now wireplumber.service || {
175
+ dotpi echo_warning "error while enabling wireplumber.service now: ${?}"
176
+ dotpi echo_warning "wireplumber.service will be available after a reboot"
177
+ # do not exit
178
+ }
179
+
180
+ ############## dotpi-manager
181
+ error_exit_code_dotpi_manager=7
182
+
183
+ if [[ "$dotpi_manager_install" == "yes" ]]; then (
184
+ destination="${DOTPI_ROOT}/share/dotpi-manager"
185
+ sudo mkdir -p -- "$destination" || {
186
+ error_exit $? "dotpi-manager: mkdir ${destination}" $error_exit_code_dotpi_manager
187
+ }
188
+ cd -- "$destination" || {
189
+ error_exit $? "dotpi-manager: cd ${destination}" $error_exit_code_dotpi_manager
190
+ }
191
+
192
+ destination_runtime="${destination}/runtime"
193
+ sudo rm -rf -- "$destination_runtime" || {
194
+ error_exit $? "dotpi-manager: rm ${destination_runtime}" $error_exit_code_dotpi_manager
195
+ }
196
+ sudo git clone --depth=1 https://github.com/ircam-ismm/dotpi-manager.git "$destination_runtime" || {
197
+ error_exit $? "dotpi-manager: git clone" $error_exit_code_dotpi_manager
198
+ }
199
+ # bootstrap-install
200
+ sudo bash -c "\
201
+ source '${DOTPI_ROOT}/share/dotpi_init.bash' && \
202
+ source runtime/dotpi/dotpi-manager.bash && \
203
+ dotpi_manager_update \
204
+ " || {
205
+ error_exit $? "dotpi_manager_update" $error_exit_code_dotpi_manager
206
+ }
207
+ ) fi
208
+
209
+ ############## dotpi-led
210
+ error_exit_code_dotpi_led=8
211
+
212
+ if [[ "$dotpi_led_install" == "yes" ]]; then (
213
+ destination="${DOTPI_ROOT}/share/dotpi-led"
214
+ sudo mkdir -p -- "$destination" || {
215
+ error_exit $? "dotpi-led: mkdir ${destination}" $error_exit_code_dotpi_led
216
+ }
217
+ cd -- "$destination" || {
218
+ error_exit $? "dotpi-led: cd ${destination}" $error_exit_code_dotpi_led
219
+ }
220
+
221
+ destination_runtime="${destination}/runtime"
222
+ sudo rm -rf -- "$destination_runtime" || {
223
+ error_exit $? "dotpi-led: rm ${destination_runtime}" $error_exit_code_dotpi_led
224
+ }
225
+ sudo git clone --depth=1 https://github.com/ircam-ismm/dotpi-led.git "$destination_runtime" || {
226
+ error_exit $? "dotpi-led: git clone" $error_exit_code_dotpi_led
227
+ }
228
+ # bootstrap-install
229
+ sudo bash -c "\
230
+ source '${DOTPI_ROOT}/share/dotpi_init.bash' && \
231
+ source runtime/dotpi/dotpi-led.bash && \
232
+ dotpi_led_update \
233
+ " || {
234
+ error_exit $? "dotpi-led: dotpi_led_update" $error_exit_code_dotpi_led
235
+ }
236
+ ) fi
237
+
238
+ ############## finalisation
239
+
240
+ dotpi echo_info "System prepared."
241
+ dotpi echo_info "Rebooting..."
242
+
243
+ sudo reboot
244
+
@@ -0,0 +1,69 @@
1
+ #!/bin/bash
2
+
3
+ # variables are used in dotpi_environment.bash, not in this file
4
+ # shellcheck disable=SC2034
5
+
6
+ # force 32bit executable
7
+ # (and kernel for Raspberry Pi v3, not v4)
8
+ # 32 for 32bit system, 64 for 64bit system
9
+ # only applies if the system is in 32 bits
10
+ dotpi_word_size='32'
11
+
12
+ # dot *not* change this
13
+ dotpi_hostname_prefix='dotpi'
14
+
15
+ dotpi_user='pi'
16
+
17
+ # no default password: set `dotpi_password` in <project>/dotpi_secrets.bash
18
+ dotpi_password=
19
+
20
+ # no default key
21
+ dotpi_ssh_authorized_keys=()
22
+
23
+ dotpi_ssh_allow_password_authentication='yes'
24
+
25
+ dotpi_timezone='Europe/Paris'
26
+ dotpi_wifi_country_code='FR'
27
+ dotpi_keymap='fr'
28
+
29
+ dotpi_audio_device='default'
30
+
31
+ # run /opt/dotpi/bin/dotpi_prepare_system after the first reboot
32
+ dotpi_prepare_system_automatic='yes'
33
+
34
+ # install dotpi-manager service
35
+ dotpi_manager_install='yes'
36
+
37
+ # install dotpi-led service
38
+ dotpi_led_install='no'
39
+ dotpi_led_strip_configuration='default'
40
+
41
+ # system default
42
+ dotpi_apt_uninstall_default=()
43
+
44
+ # user-defined, use +=(package1 package2) to add packages to list
45
+ dotpi_apt_uninstall=()
46
+
47
+ # dotpi requirements
48
+ dotpi_apt_install_default=(
49
+ 'systemd-container' # machinectl
50
+
51
+ 'ca-certificates'
52
+ 'curl'
53
+ 'g++'
54
+ 'gcc'
55
+ 'git'
56
+ 'gnupg'
57
+ 'make'
58
+ 'cmake'
59
+ 'scons'
60
+
61
+ 'pipewire'
62
+ 'pipewire-audio'
63
+ 'pipewire-jack'
64
+ )
65
+
66
+ # user-defined, use +=(package1 package2) to add packages to list
67
+ dotpi_apt_install=()
68
+
69
+ dotpi_node_version='lts'
@@ -0,0 +1,3 @@
1
+ .DS_Store
2
+ .Spotlight-V100
3
+ .editorbackup
@@ -0,0 +1,39 @@
1
+ #!/bin/bash
2
+
3
+ ####### Bootstrap
4
+
5
+ dotpi_echo_error() {
6
+ printf 'ERROR: %s\n' "$1" >&2
7
+ }
8
+
9
+ local_path="$( (cd -- "$(dirname -- "$0")" && pwd) 2> /dev/null || {
10
+ dotpi_echo_error "unable to access local path $(dirname -- "$0")"
11
+ } )"
12
+ if [ -z "$local_path" ] ; then
13
+ exit 1
14
+ fi
15
+
16
+ # script in "share/dotpi-install"
17
+ DOTPI_ROOT="${local_path}/../.."
18
+ source "${DOTPI_ROOT}/share/dotpi_init.bash"
19
+
20
+ cd "${DOTPI_ROOT}" || {
21
+ dotpi_echo_error "unable to change directory to ${DOTPI_ROOT}"
22
+ exit 1
23
+ }
24
+
25
+ dotpi_echo_info "Update dotpi_root from ${DOTPI_ROOT}"
26
+
27
+ for y in {1..7} ; do
28
+ for x in {1..6} ; do
29
+ d="dotpi-dev-0${y}${x}.local"
30
+ ssh -o ConnectTimeout=1 "${d}" "dotpi echo_info '${d}: connected'" && (
31
+
32
+ dotpi_echo_info "${d}: rsync"
33
+ rsync --rsync-path="sudo rsync" --archive --archive --hard-links --acls --one-file-system --relative './' "${d}:/opt/dotpi"
34
+
35
+ dotpi_echo_info "${d}: update"
36
+ ssh "${d}" "/opt/dotpi/share/dotpi-install/update_to_v1.0.0_pi"
37
+ )&
38
+ done
39
+ done
@@ -0,0 +1,39 @@
1
+ #!/bin/bash
2
+
3
+ _dotpi_command="$(basename -- "$0")"
4
+ log_file="${DOTPI_ROOT}/var/log/${_dotpi_command}_$(date +"%Y%m%d-%H%M%S").log"
5
+ exec &> >(sudo dotpi log "$log_file")
6
+
7
+ dotpi echo_info "Log of dotpi-manager update: ${log_file}"
8
+
9
+ # This script MUST be executed by `pi` user
10
+ FIRSTUSER="$(getent passwd 1000 | cut -d: -f1)"
11
+ if [[ $(whoami) != "${FIRSTUSER}" ]] ; then
12
+ dotpi echo_error "$(hostname): This script MUST be executed by pi user (or first user)"
13
+ exit 1
14
+ fi
15
+
16
+ dotpi echo_info update definitions
17
+ source "${DOTPI_ROOT}/share/dotpi_init.bash"
18
+
19
+ service='dotpi-manager.service'
20
+ dotpi echo_info "$(hostname): dameon-reload"
21
+ sudo systemctl daemon-reload
22
+ dotpi echo_info "$(hostname): remove old system service ${service}"
23
+ sudo dotpi service_uninstall "$service"
24
+
25
+ dotpi echo_info "$(hostname): enable user services"
26
+
27
+ dotpi echo_info "$(hostname): enable linger for ${FIRSTUSER}"
28
+ sudo loginctl enable-linger "$FIRSTUSER"
29
+
30
+ service='/opt/dotpi/share/dotpi-manager/dotpi-manager.service'
31
+ dotpi echo_info "$(hostname): new user service ${service}"
32
+ sudo dotpi service_install --user "$service"
33
+
34
+ sudo dotpi service_install "${DOTPI_ROOT}/share/dotpi_avahi_monitor/dotpi_avahi_monitor.service"
35
+
36
+ dotpi echo_info "$(hostname): ${_dotpi_command}: finished"
37
+
38
+ # dotpi echo_info "$(hostname): reboot"
39
+ # sudo reboot
@@ -0,0 +1,23 @@
1
+ [Unit]
2
+ Description=dotpi bluetooth audio daemon
3
+ Requires=bluetooth.target
4
+ After=bluetooth.target pipewire.service
5
+
6
+ # TODO: stop on system stop
7
+
8
+ [Service]
9
+ Type=exec
10
+ User=pi
11
+
12
+ ExecStart=/opt/dotpi/share/dotpi_audio_bluetooth/dotpi_audio_bluetooth_command
13
+
14
+ Restart=always
15
+
16
+ # wait before restarting
17
+ RestartSec=5s
18
+
19
+ # Do not stop to try restarting service
20
+ StartLimitBurst=0
21
+
22
+ [Install]
23
+ WantedBy=multi-user.target
@@ -0,0 +1,134 @@
1
+ #!/bin/bash
2
+
3
+ start_restart_reboot() (
4
+
5
+ (( trial_count = 0 ))
6
+ while ! dotpi audio_bluetooth_destination_start ; do
7
+ (( ++trial_count ))
8
+ dotpi echo_info "Try to start audio bluetooth: attempt ${trial_count}."
9
+
10
+ hci_up="$(hciconfig hci0 | grep 'UP' | wc -l)"
11
+ if (( $? != 0 )) || (( hci_up == 0 )) ; then
12
+ dotpi echo_error "Bluetooth device is down."
13
+ dotpi echo_warning "Try to reset bluetooth device."
14
+ sudo hciconfig hci0 reset || {
15
+ dotpi echo_error "Failed to reset bluetooth device."
16
+ dotpi echo_warning "Rebooting system."
17
+ sudo reboot
18
+ }
19
+ sleep 20
20
+ fi
21
+
22
+ if (( trial_count >=9 )) ; then
23
+ dotpi echo_error "Failed to start audio bluetooth destination."
24
+ dotpi echo_warning "Rebooting system."
25
+ sudo reboot
26
+ fi
27
+
28
+ if (( trial_count == 6 )) ; then
29
+
30
+ sudo hciconfig hci0 reset || {
31
+ dotpi echo_error "Failed to reset bluetooth device."
32
+ dotpi echo_warning "Rebooting system."
33
+ sudo reboot
34
+ }
35
+
36
+ sleep 20
37
+ fi
38
+
39
+ if (( trial_count == 3 || trial_count == 6)) ; then
40
+ sleep 10
41
+ dotpi echo_warning "Stopping audio bluetooth destination."
42
+ dotpi audio_bluetooth_destination_stop
43
+ fi
44
+
45
+ sleep 10
46
+ dotpi echo_warning "Starting audio bluetooth destination again."
47
+ done
48
+
49
+ local_file="$(dotpi_readlink_follow "$0")"
50
+ local_path="$(dirname -- "$local_file")"
51
+ FIRSTUSER="$(getent passwd 1000 | cut -d: -f1)"
52
+ # execute in a proper user session for pipewire to work
53
+ sudo machinectl shell "${FIRSTUSER}@" /bin/bash -c "(
54
+ (( trial_count = 0 ))
55
+ while (( trial_count < 10 )) ; do
56
+ '${local_path}/dotpi_audio_pipewire_relink' && exit 0
57
+ sleep 3
58
+ (( ++trial_count ))
59
+ done
60
+ sudo reboot
61
+ )"
62
+
63
+ )
64
+
65
+ source /opt/dotpi/share/dotpi_init.bash
66
+
67
+ dotpi echo_info "############ Start command"
68
+ clean_up() {
69
+ if [[ -n ${btctl_process_PID} ]] ; then
70
+ dotpi echo_info "########### clean_up"
71
+ dotpi echo_info "Ending bluetoothctl process ${btctl_process_PID}"
72
+ kill "${btctl_process_PID}"
73
+ dotpi echo_info "Stopping audio bluetooth destination"
74
+ sudo dotpi audio_bluetooth_destination_stop
75
+ fi
76
+ }
77
+ trap clean_up HUP TERM INT
78
+
79
+ destination_mac="${dotpi_audio_bluetooth_mac}"
80
+ destination_descriptor="${dotpi_audio_bluetooth_mac//:/_}"
81
+
82
+ # dotpi echo_info "Stopping audio bluetooth destination"
83
+ # sudo dotpi audio_bluetooth_destination_stop
84
+
85
+ dotpi echo_info "Starting bluetoothctl monitor process"
86
+ coproc btctl_process { bluetoothctl; }
87
+
88
+ dotpi echo_info "Starting audio bluetooth destination"
89
+
90
+ start_restart_reboot
91
+
92
+ status='init'
93
+ dotpi echo_warning "status: $status"
94
+ while IFS= read -r output ; do
95
+ echo "$output" >&2
96
+
97
+ # destination disconnected
98
+
99
+ if [[ "$output" =~ [DEL].*Endpoint.*/org/bluez/.*dev_"${destination_descriptor}" ]] ; then
100
+ # connect again
101
+ dotpi echo_error "Audio bluetooth destination (${destination_mac}) disconnected."
102
+ dotpi echo_info "Connecting again"
103
+
104
+ sleep 2
105
+
106
+ start_restart_reboot
107
+
108
+ fi
109
+
110
+ # if [[ "$status" == "init" ]] \
111
+ # && [[ "$output" =~ [DEL].*Endpoint.*/org/bluez/.*dev_"${destination_descriptor}" ]] ; then
112
+ # status='deleted'
113
+ # dotpi echo_warning "status: $status"
114
+ # fi
115
+
116
+ # if [[ "$status" == "deleted" ]] \
117
+ # && [[ "$output" =~ [CHG].*Device.*"${destination_mac}".*Connected:.*no ]] ; then
118
+
119
+ # # connect again
120
+ # dotpi echo_error "Audio bluetooth destination (${destination_mac}) disconnected."
121
+ # dotpi echo_info "Connecting again"
122
+
123
+ # status='init'
124
+ # dotpi echo_warning "status: $status"
125
+
126
+ # dotpi audio_bluetooth_destination_start
127
+ # fi
128
+
129
+ done <&${btctl_process[0]}
130
+
131
+ dotpi echo_info "############ Start ending"
132
+
133
+ dotpi echo_info "Stopping audio bluetooth destination"
134
+ dotpi audio_bluetooth_destination_stop
@@ -0,0 +1,132 @@
1
+ #!/usr/bin/node
2
+
3
+ // @TODO: use a proper pipewire lua script that automatically runs
4
+ // whenever the default audio output is restored
5
+
6
+ const util = require('node:util');
7
+ const exec = util.promisify(require('node:child_process').exec);
8
+
9
+ let pipewireData = null;
10
+ async function pipewireRelink() {
11
+
12
+ try {
13
+ const { stdout, stderr } = await exec('pw-dump');
14
+ // console.log('pw-dump stdout:', stdout);
15
+ if(stderr) {
16
+ console.warn('pw-dump stderr:', stderr);
17
+ }
18
+
19
+ pipewireData = JSON.parse(stdout);
20
+
21
+ } catch (error) {
22
+ console.error('Error with pw-dump:', error.message, error);
23
+ process.exit(1);
24
+ }
25
+ const playbackNodes = pipewireData.filter( (e) => {
26
+ return e.type === "PipeWire:Interface:Node"
27
+ && e.info
28
+ && e.info.props
29
+ && typeof e.info.props['client.id'] !== 'undefined'
30
+ && e.info.props['media.type'] === 'Audio'
31
+ });
32
+
33
+ if (playbackNodes.length === 0) {
34
+ console.warn('pipewireRelink: no playback node');
35
+ // no error
36
+ process.exit(0);
37
+ }
38
+ // console.log('playbackNodes:', playbackNodes);
39
+
40
+ // @TODO: associate ports
41
+ // out_0 -> FL
42
+ // out_1 -> FR
43
+
44
+ // const playbackPorts = [];
45
+ // playbackNodes.forEach( (n) => {
46
+ // const ports = pipewireData.filter( (e) => {
47
+ // return e.type === "PipeWire:Interface:Port"
48
+ // && e.info
49
+ // && e.info.props
50
+ // && e.info.props['node.id'] === n.id
51
+ // });
52
+
53
+ // playbackPorts.push(...ports);
54
+ // });
55
+
56
+
57
+ // if (playbackPorts.length === 0) {
58
+ // console.error('pipewireRelink: no playback port');
59
+ // return;
60
+ // }
61
+ // console.log('playbackPorts:', playbackPorts);
62
+
63
+ const outputNodes = pipewireData.filter( (e) => {
64
+ return e.type === "PipeWire:Interface:Node"
65
+ && e.info
66
+ // && e.info['max-input-ports'] > 1
67
+ && e.info.props
68
+ && e.info.props['media.class'] === 'Audio/Sink';
69
+ });
70
+
71
+ if (outputNodes.length === 0) {
72
+ console.warn('pipewireRelink: no output node');
73
+ // no error
74
+ process.exit(0);
75
+ }
76
+ else if (outputNodes.length > 1) {
77
+ console.warn('pipewireRelink: multiple output nodes');
78
+ }
79
+ // console.log('outputNodes:', outputNodes);
80
+
81
+ // const outputPorts = [];
82
+ // outputNodes.forEach( (n) => {
83
+ // const ports = pipewireData.filter( (e) => {
84
+ // return e.type === "PipeWire:Interface:Port"
85
+ // && e.info
86
+ // && e.info.props
87
+ // && e.info.props['node.id'] === n.id
88
+ // && typeof e.info.props['port.name'] === 'string'
89
+ // && e.info.props['port.name'].startsWith('playback')
90
+ // });
91
+
92
+ // outputPorts.push(...ports);
93
+ // });
94
+
95
+
96
+ // if (outputPorts.length === 0) {
97
+ // console.error('pipewireRelink: no playback port');
98
+ // return;
99
+ // }
100
+ // console.log('outputPorts:', outputPorts);
101
+
102
+
103
+ for(let p = 0; p < playbackNodes.length; ++p) {
104
+ for(let o = 0; o < outputNodes.length; ++o) {
105
+ playbackIndex = playbackNodes[p].id;
106
+ outputIndex = outputNodes[o].id;
107
+ const commandString = `pw-link ${playbackIndex} ${outputIndex}`;
108
+ try {
109
+ console.log(commandString);
110
+ const {stdout, stderr} = await exec(commandString);
111
+ // console.log('pw-link stdout:', stdout);
112
+ if(stderr) {
113
+ console.warn('pw-link stderr:', stderr);
114
+ }
115
+ } catch(error) {
116
+ // error when linking nodes instead of ports: ignore
117
+ // console.error( `Error with ${commandString}:`, error.message, error);
118
+ continue;
119
+ }
120
+ }
121
+ }
122
+
123
+ process.exit(0);
124
+
125
+ }
126
+
127
+ pipewireRelink();
128
+
129
+
130
+
131
+
132
+