@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,22 @@
1
+ [Unit]
2
+ Description=avahi monitor
3
+ After=avahi-daemon.service
4
+
5
+ [Service]
6
+ Type=exec
7
+ LimitRTPRIO=infinity
8
+ LimitMEMLOCK=infinity
9
+ User=pi
10
+ Group=audio
11
+ ExecStart=/opt/dotpi/share/dotpi_avahi_monitor/dotpi_avahi_monitor_command
12
+ Restart=always
13
+
14
+ # wait before restarting
15
+ RestartSec=10s
16
+
17
+ # Do not stop to try restarting service
18
+ StartLimitBurst=0
19
+
20
+ [Install]
21
+ WantedBy=multi-user.target
22
+
@@ -0,0 +1,14 @@
1
+ #!/bin/bash
2
+
3
+ service='avahi-daemon.service'
4
+
5
+ while true ; do
6
+ if systemctl is-active "$service" > /dev/null 2>&1 ; then
7
+ ping -c 1 "$(hostname).local" > /dev/null 2>&1 || {
8
+ dotpi echo_info "$(hostname): restart avahi-daemon"
9
+ sudo systemctl restart "$service"
10
+ }
11
+ fi
12
+ sleep 60
13
+ done
14
+
@@ -0,0 +1,5 @@
1
+ #!/bin/bash
2
+
3
+ dotpi_source_if_available "${DOTPI_ROOT}/etc/dotpi_environment_default.bash"
4
+ dotpi_source_if_available "${DOTPI_ROOT}/etc/dotpi_environment_project.bash"
5
+ dotpi_source_if_available "${DOTPI_ROOT}/etc/dotpi_environment_instance.bash"
@@ -0,0 +1,346 @@
1
+ #!/bin/bash
2
+
3
+ _dotpi_audio_bluetooth_controller='hci0'
4
+
5
+ # bash v5 is available on Raspberry Pi, and provides named co-processes
6
+ if (( "${BASH_VERSINFO:-0}" < 5 )) ; then
7
+ return 0;
8
+ fi
9
+ # connect an audio bluetooth device, corresponding to dotpi_audio_bluetooth_mac,
10
+ # using bluetoothctl,
11
+ dotpi_audio_bluetooth_destination_connect() (
12
+ destination_mac="$dotpi_audio_bluetooth_mac"
13
+
14
+ dotpi_echo_info "Starting bluetoothctl process"
15
+ status='init'
16
+ coproc btctl_process { bluetoothctl; }
17
+
18
+ while IFS= read -r output ; do
19
+ echo "$output" >&2
20
+
21
+ if [[ "$status" == 'init' ]]; then
22
+ dotpi_echo_info "Registering agent"
23
+ echo "default-agent" >&${btctl_process[1]}
24
+ echo "agent on" >&${btctl_process[1]}
25
+
26
+ dotpi_echo_info "Starting scan"
27
+ echo "scan on" >&${btctl_process[1]}
28
+ status='starting scan'
29
+ fi
30
+
31
+ if [[ "$status" == "starting scan" ]] \
32
+ && [[ "$output" =~ "Discovery started" ]] ; then
33
+ dotpi_echo_info "Waiting for destination"
34
+ status='scanning'
35
+ fi
36
+
37
+ if [[ "$status" == "scanning" ]] \
38
+ && [[ "$output" =~ Device.*"$destination_mac" ]] ; then
39
+ dotpi_echo_info "Destination discovered"
40
+ status='destination discovered'
41
+ fi
42
+
43
+ if [[ "$status" == 'destination discovered' ]] ; then
44
+ dotpi_echo_info "Pairing destination"
45
+ echo "pair ${destination_mac}" >&${btctl_process[1]}
46
+ status='pairing'
47
+ fi
48
+
49
+ if [[ "$status" == "pairing" ]] \
50
+ && [[ "$output" =~ "Pairing successful" ]] ; then
51
+ dotpi_echo_info "Destination paired"
52
+ status='destination paired'
53
+ fi
54
+
55
+ if [[ "$status" == "pairing" ]] \
56
+ && [[ "$output" =~ "Failed to pair" ]] ; then
57
+ dotpi_echo_error "Paring failed"
58
+ dotpi_echo_warning "Turn destination on, in discovery mode"
59
+ sleep 1
60
+ echo "pair ${destination_mac}" >&${btctl_process[1]}
61
+ status='pairing'
62
+ fi
63
+
64
+ if [[ "$status" == "destination paired" ]] ; then
65
+ dotpi_echo_info "Connecting destination"
66
+ echo "connect ${destination_mac}" >&${btctl_process[1]}
67
+ status='connecting'
68
+ fi
69
+
70
+ if [[ "$status" == "connecting" ]] \
71
+ && [[ "$output" =~ "Connection successful" ]] ; then
72
+ dotpi_echo_info "Destination connected"
73
+ status='destination connected'
74
+ fi
75
+
76
+ if [[ "$status" == "destination connected" ]] ; then
77
+ dotpi_echo_info "Trusting destination"
78
+ echo "trust ${destination_mac}" >&${btctl_process[1]}
79
+ status='trusting'
80
+ fi
81
+
82
+ if [[ "$status" == "trusting" ]] \
83
+ && [[ "$output" =~ "trust succeeded" ]] ; then
84
+ dotpi_echo_info "Destination trusted"
85
+ status='destination trusted'
86
+ fi
87
+
88
+ if [[ "$status" == "destination trusted" ]] ; then
89
+ dotpi_echo_info "Ending scan"
90
+ echo "scan off" >&${btctl_process[1]}
91
+ status='ending scan'
92
+ fi
93
+
94
+ if [[ "$status" == "ending scan" ]] \
95
+ && [[ "$output" =~ "Discovery stopped" ]] ; then
96
+ dotpi_echo_info "Exiting"
97
+ status='exiting'
98
+ fi
99
+
100
+ if [[ "$status" == 'exiting' ]] ; then
101
+ echo "exit" >&${btctl_process[1]}
102
+ dotpi_echo_info "Done"
103
+ status='done'
104
+ break
105
+ fi
106
+
107
+ done <&${btctl_process[0]}
108
+ )
109
+
110
+ dotpi_audio_bluetooth_destination_disconnect() (
111
+ bluetoothctl -- remove "$dotpi_audio_bluetooth_mac"
112
+ )
113
+
114
+ _dotpi_audio_bluetooth_destination_start_ue() (
115
+ destination_mac="$dotpi_audio_bluetooth_mac"
116
+ destination_descriptor="${dotpi_audio_bluetooth_mac//:/_}"
117
+
118
+ trusted_mac="$(hciconfig "$_dotpi_audio_bluetooth_controller" \
119
+ | perl -ne 'if (m/BD Address:\s*([\w:]+).*$/i) { print "${1}\n"; }')"
120
+
121
+ coproc btctl_process { bluetoothctl; }
122
+
123
+ # test if already here
124
+ echo 'transport.list' >&${btctl_process[1]}
125
+
126
+ timeout_duration=30
127
+ kill_timeout=10
128
+ kill_signal='KILL'
129
+ timeout --kill-after="$kill_timeout" --signal="$kill_signal" "$timeout_duration" gatttool \
130
+ --adapter "$_dotpi_audio_bluetooth_controller" \
131
+ --device "${destination_mac}" \
132
+ --char-write-req \
133
+ --handle 0x0003 \
134
+ --value "${trusted_mac//:/}01" || {
135
+ timeout_status="$?"
136
+ case "$timeout_status" in
137
+ '124')
138
+ timeout_message="time out"
139
+ ;;
140
+
141
+ '125')
142
+ timeout_message="failed"
143
+ ;;
144
+ '126')
145
+ timeout_message="cannot be invoked"
146
+ ;;
147
+ '127')
148
+ timeout_message="not found"
149
+ ;;
150
+ '137')
151
+ timeout_message="killed"
152
+ ;;
153
+ *)
154
+ timeout_message="exit status: ${timeout_status}"
155
+ ;;
156
+ esac
157
+ dotpi_echo_error "Unable to start ${dotpi_audio_device}: ${timeout_message}"
158
+
159
+ kill ${btctl_process_PID}
160
+ return "$timeout_status"
161
+ }
162
+
163
+ while IFS= read -t "$timeout_duration" -r output ; do
164
+ echo "$output" >&2
165
+
166
+ # Connected and transport volume available
167
+ if [[ "$output" =~ [CHG].*Transport.*/org/bluez/.*dev_"${destination_descriptor}".*Volume ]] \
168
+ || [[ "$output" =~ ^Transport.*/org/bluez/.*dev_"${destination_descriptor}" ]] ; then
169
+ kill ${btctl_process_PID}
170
+ dotpi_echo_info "Destination started"
171
+ return 0
172
+ fi
173
+ done <&${btctl_process[0]}
174
+
175
+ dotpi_echo_error "Unable to start ${dotpi_audio_device}, despite no error"
176
+ return 99
177
+ )
178
+
179
+ # turn audio device on, and set volume
180
+ dotpi_audio_bluetooth_destination_start() (
181
+ model_normalised="$(dotpi_audio_device_model_normalise "$dotpi_audio_device")"
182
+
183
+ case "$model_normalised" in
184
+
185
+ 'ue boom 2'|'ue boom 3'|'ue megaboom 2'|'ue megaboom 3')
186
+ _dotpi_audio_bluetooth_destination_start_ue || {
187
+ status="$?"
188
+ dotpi_echo_error "Unable to start ${dotpi_audio_device}"
189
+ return "$status"
190
+ }
191
+ ;;
192
+
193
+ *)
194
+ dotpi_echo_error "Unable to start unknown model '${dotpi_audio_device}'"
195
+ return 1
196
+ ;;
197
+
198
+ esac
199
+
200
+ dotpi_audio_bluetooth_destination_volume_init || {
201
+ status="$?"
202
+ dotpi_echo_error "Unable to set volume for ${dotpi_audio_device}"
203
+ return "$status"
204
+ }
205
+ )
206
+
207
+
208
+ _dotpi_audio_bluetooth_destination_stop_ue() (
209
+ destination_mac="$dotpi_audio_bluetooth_mac"
210
+ destination_descriptor="${dotpi_audio_bluetooth_mac//:/_}"
211
+
212
+ trusted_mac="$(hciconfig "$_dotpi_audio_bluetooth_controller" \
213
+ | perl -ne 'if (m/BD Address:\s*([\w:]+).*$/i) { print "${1}\n"; }')"
214
+
215
+ dotpi echo_info "Starting bluetoothctl monitor process"
216
+ coproc btctl_process { bluetoothctl; }
217
+
218
+ # will trigger a connection, even if destination if off, for write request (BLE)
219
+ # hence the [CHG] ... Connected: no
220
+ timeout_duration=30
221
+ kill_timeout=10
222
+ kill_signal='KILL'
223
+ timeout --kill-after="$kill_timeout" --signal="$kill_signal" "$timeout_duration" gatttool \
224
+ --adapter "$_dotpi_audio_bluetooth_controller" \
225
+ --device "${destination_mac}" \
226
+ --char-write-req \
227
+ --handle 0x0003 \
228
+ --value "${trusted_mac//:/}02" || {
229
+ timeout_status="$?"
230
+ case "$timeout_status" in
231
+ '124')
232
+ timeout_message="time out"
233
+ ;;
234
+
235
+ '125')
236
+ timeout_message="failed"
237
+ ;;
238
+ '126')
239
+ timeout_message="cannot be invoked"
240
+ ;;
241
+ '127')
242
+ timeout_message="not found"
243
+ ;;
244
+ '137')
245
+ timeout_message="killed"
246
+ ;;
247
+ *)
248
+ timeout_message="exit status: ${timeout_status}"
249
+ ;;
250
+ esac
251
+ dotpi_echo_error "Unable to stop ${dotpi_audio_device}: ${timeout_message}"
252
+
253
+ kill ${btctl_process_PID}
254
+ return "$timeout_status"
255
+ }
256
+
257
+ while IFS= read -t "$timeout_duration" -r output ; do
258
+ echo "$output" >&2
259
+
260
+ # destination disconnected
261
+ if [[ "$output" =~ [DEL].*Endpoint.*/org/bluez/.*dev_"${destination_descriptor}" ]] \
262
+ || [[ "$output" =~ [CHG].*Device.*"$destination_mac".*Connected:.*no ]] ; then
263
+
264
+ kill ${btctl_process_PID}
265
+
266
+ dotpi_echo_info "Destination stopped"
267
+ return 0
268
+ fi
269
+ done <&${btctl_process[0]}
270
+
271
+ dotpi_echo_error "Unable to stop ${dotpi_audio_device}, despite no error"
272
+ return 99
273
+ )
274
+
275
+ # turn off audio device
276
+ dotpi_audio_bluetooth_destination_stop() (
277
+ model_normalised="$(dotpi_audio_device_model_normalise "$dotpi_audio_device")"
278
+
279
+ case "$model_normalised" in
280
+
281
+ 'ue boom 2'|'ue boom 3'|'ue megaboom 2'|'ue megaboom 3')
282
+ _dotpi_audio_bluetooth_destination_stop_ue || {
283
+ status="$?"
284
+ dotpi_echo_error "Unable to stop ${dotpi_audio_device}"
285
+ return "$status"
286
+ }
287
+ ;;
288
+
289
+ *)
290
+ dotpi_echo_error "Unable to stop unknown model '${dotpi_audio_device}'"
291
+ return 1
292
+ ;;
293
+
294
+ esac
295
+ )
296
+
297
+ # set and save volume
298
+ dotpi_audio_bluetooth_destination_volume_set() (
299
+ destination_volume="$1"
300
+ destination_device="dev_${dotpi_audio_bluetooth_mac//:/_}"
301
+ destination_transport="$(bluetoothctl transport.list \
302
+ | perl -ne 'if(m/Transport\s+(\/*${destination_device}[^\s]+).*$/) { print "${1}\n"; }')"
303
+
304
+ if [[ -z "$destination_transport" ]] ; then
305
+ dotpi_echo_error "Failed to set volume: audio bluetooth device not found"
306
+ return 1
307
+ fi
308
+
309
+ bluetoothctl transport.volume "$destination_transport" "$destination_volume"
310
+ if (( $? != 0 )) ; then
311
+ dotpi_echo_error "Failed to set bluetooth destination volume"
312
+ return 1
313
+ else
314
+ dotpi_echo_info "Bluetooth destination volume set to ${destination_volume}"
315
+ fi
316
+
317
+ if [[ "$destination_volume" == "$dotpi_audio_volume" ]] ; then
318
+ # no change to write
319
+ return 0
320
+ fi
321
+
322
+ # comment setting in project
323
+ dotpi_configuration_comment --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
324
+ --key dotpi_audio_volume
325
+
326
+ # escape double-quote for now, to quote when writing later
327
+ model_value="$(echo "$model" | dotpi_sed 's/"/\\"/g')"
328
+
329
+ # write changed setting in instance
330
+ dotpi_configuration_write --file "${DOTPI_ROOT}/etc/dotpi_environment_instance.bash" \
331
+ --key dotpi_audio_volume \
332
+ --value "$destination_volume"
333
+
334
+ )
335
+
336
+ # set volume to default
337
+ dotpi_audio_bluetooth_destination_volume_init() (
338
+ if [[ -z "$dotpi_audio_volume" ]] ; then
339
+ dotpi_echo_warning "dotpi_audio_volume not set, keeping default volume"
340
+ return 0
341
+ fi
342
+
343
+ destination_volume="$dotpi_audio_volume"
344
+ dotpi_audio_bluetooth_destination_volume_set "$destination_volume"
345
+ )
346
+