@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,504 @@
1
+ #!/bin/bash
2
+
3
+ dotpi_audio_device_supported=(
4
+ 'default'
5
+ 'none'
6
+ 'headphones'
7
+ 'hifiberry dac+ adc pro'
8
+ 'hifiberry dac+ adc'
9
+ 'hifiberry dac+ standard'
10
+ 'hifiberry dac+ light'
11
+ 'hifiberry dac zero'
12
+ 'hifiberry miniamp'
13
+ 'hifiberry beocreate'
14
+ 'hifiberry dac+ dsp'
15
+ 'hifiberry dac+ rtc'
16
+ 'hifiberry pro'
17
+ 'hifiberry amp2'
18
+ 'hifiberry dac2 hd'
19
+ 'hifiberry digi+'
20
+ 'hifiberry digi+ pro'
21
+ 'hifiberry amp+'
22
+ 'hifiberry amp3'
23
+ 'bluetooth'
24
+ 'ue boom 2'
25
+ 'ue boom 3'
26
+ 'ue megaboom 2'
27
+ 'ue megaboom 3'
28
+ 'hifiberry dac for raspberry pi 1'
29
+ )
30
+
31
+ _dotpi_audio_device_get_options() {
32
+
33
+ _echo_empty_option_error() {
34
+ dotpi_echo_error "$1 requires a non-empty option argument."
35
+ }
36
+
37
+ # From http://mywiki.wooledge.org/BashFAQ/035
38
+
39
+ while true ; do
40
+ case $1 in
41
+
42
+ -h|-\?|--help)
43
+ _dotpi_usage # Display a usage synopsis.
44
+ return 0
45
+ ;;
46
+
47
+ -m|--model) # Takes an option argument; ensure it has been specified.
48
+ if [ -z "$2" ] ; then
49
+ _dotpi_usage
50
+ _echo_empty_option_error "$1"
51
+ return 1
52
+ else
53
+ model="$2"
54
+ shift
55
+ fi
56
+ ;;
57
+
58
+ -c|--config) # Takes an option argument; ensure it has been specified.
59
+ if [ -z "$2" ] ; then
60
+ _dotpi_usage
61
+ _echo_empty_option_error "$1"
62
+ return 1
63
+ else
64
+ config_file="$2"
65
+ shift
66
+ fi
67
+ ;;
68
+
69
+ --) # End of all options.
70
+ shift
71
+ break
72
+ ;;
73
+
74
+ -?*) # Unknown
75
+ _dotpi_usage
76
+ dotpi_echo_error "Unknown option: $1"
77
+ return 1
78
+ ;;
79
+
80
+ *) # Default case: No more options, so break out of the loop.
81
+ break
82
+
83
+ esac
84
+
85
+ shift
86
+ done
87
+
88
+ }
89
+
90
+ dotpi_audio_device_model_normalise() {
91
+ echo "$1" | tr '[:upper:]' '[:lower:]'
92
+ }
93
+
94
+ # Usage: sudo dotpi audio_device_select --model <model> [--config <config_file_path>]
95
+ # if --config is not provided, it will use the system default config file
96
+ dotpi_audio_device_select() (
97
+ # sub-shell to keep everything local (functions and variables)
98
+
99
+ _dotpi_command="$(basename -- "${FUNCNAME[0]:-"$0"}")"
100
+ _dotpi_usage() {
101
+ echo "Usage: ${_dotpi_command} --model <model> [--config <config_file_path>]"
102
+ }
103
+
104
+ # Initialize all the option variables.
105
+ # This ensures we are not contaminated by variables from the environment.
106
+ model=
107
+ config_file=
108
+
109
+ if ! _dotpi_audio_device_get_options "${@}" ; then
110
+ return $?
111
+ fi
112
+
113
+ if [ -z "$model" ] ; then
114
+ _dotpi_usage
115
+ dotpi_echo_error "missing model option"
116
+ return 1
117
+ fi
118
+
119
+ if [ -z "$config_file" ] ; then
120
+ config_path="$(dotpi_system_get_bootfs_path)"
121
+ if [ -z "$config_path" ] ; then
122
+ dotpi_echo_error "Unable to get default system path for config file"
123
+ _dotpi_usage
124
+ return 1
125
+ fi
126
+ config_filename='config.txt'
127
+ config_file="${config_path}/${config_filename}"
128
+ dotpi_echo_warning "Using default config file ${config_file}"
129
+ fi
130
+
131
+
132
+ if ! [ -w "$config_file" ] ; then
133
+ dotpi_echo_error "Unable to access config file ${config_file} for writing"
134
+ return 1
135
+ fi
136
+
137
+ # lowercase everything
138
+ model_normalised="$(dotpi_audio_device_model_normalise "$model")"
139
+
140
+ case "$model_normalised" in
141
+
142
+ ######## Generic devices
143
+
144
+ 'default')
145
+ dotpi_echo_warning "Not installing a specific audio device, keeping defaults"
146
+ ;;
147
+
148
+ 'none')
149
+ _dotpi_audio_device_disable_all
150
+ ;;
151
+
152
+ 'headphones')
153
+ _dotpi_audio_device_select_headphones
154
+ ;;
155
+
156
+ 'bluetooth')
157
+ dotpi_echo_info "Installing generic bluetooth device"
158
+ _dotpi_audio_device_select_bluetooth
159
+ ;;
160
+
161
+ ######## Specific devices
162
+
163
+ # Raspberry
164
+ # Cf. https://www.raspberrypi.com/documentation/accessories/audio.html#configuration
165
+
166
+ 'raspberry pi dac pro')
167
+ dtoverlay=rpi-dacpro
168
+ _dotpi_audio_device_select_raspberry_pi
169
+ ;;
170
+
171
+ 'raspberry pi dac+')
172
+ dtoverlay=rpi-dacplus
173
+ _dotpi_audio_device_select_raspberry_pi
174
+ ;;
175
+
176
+ 'raspberry pi digiamp+')
177
+ dtoverlay=rpi-digiampplus
178
+ _dotpi_audio_device_select_raspberry_pi
179
+ ;;
180
+
181
+ 'raspberry pi codec zero')
182
+ dtoverlay=rpi-codeczero
183
+ _dotpi_audio_device_select_raspberry_pi
184
+ ;;
185
+
186
+ # HiFiBerry
187
+ # Cf. https://www.hifiberry.com/docs/software/configuring-linux-3-18-x/
188
+
189
+ 'hifiberry dac for raspberry pi 1'|'hifiberry dac+ light'|'hifiberry dac zero'|'hifiberry miniamp'|'hifiberry beocreate'|'hifiberry dac+ dsp'|'hifiberry dac+ rtc')
190
+ dtoverlay=hifiberry-dac
191
+ _dotpi_audio_device_select_hifiberry
192
+ ;;
193
+
194
+ 'hifiberry dac+ standard'|'hifiberry pro'|'hifiberry amp2')
195
+ dtoverlay=hifiberry-dacplus
196
+ _dotpi_audio_device_select_hifiberry
197
+ ;;
198
+
199
+ 'hifiberry dac2 hd')
200
+ dtoverlay=hifiberry-dacplushd
201
+ _dotpi_audio_device_select_hifiberry
202
+ ;;
203
+
204
+ 'hifiberry dac+ adc')
205
+ dtoverlay=hifiberry-dacplusadc
206
+ _dotpi_audio_device_select_hifiberry
207
+ ;;
208
+
209
+ 'hifiberry dac+ adc pro')
210
+ dtoverlay=hifiberry-dacplusadcpro
211
+ _dotpi_audio_device_select_hifiberry
212
+ ;;
213
+
214
+ 'hifiberry digi+')
215
+ dtoverlay=hifiberry-digi
216
+ _dotpi_audio_device_select_hifiberry
217
+ ;;
218
+
219
+ 'hifiberry digi+ pro')
220
+ dtoverlay=hifiberry-digi-pro
221
+ _dotpi_audio_device_select_hifiberry
222
+ ;;
223
+
224
+ 'hifiberry amp+')
225
+ dtoverlay=hifiberry-amp
226
+ _dotpi_audio_device_select_hifiberry
227
+ ;;
228
+
229
+ 'hifiberry amp3')
230
+ dtoverlay=hifiberry-amp3
231
+ _dotpi_audio_device_select_hifiberry
232
+ ;;
233
+
234
+ ####### Ultimate Ears (UE)
235
+
236
+ 'ue boom 2'|'ue boom 3'|'ue megaboom 2'|'ue megaboom 3')
237
+ _dotpi_audio_device_select_bluetooth
238
+ ;;
239
+
240
+
241
+ ########### Unknown device
242
+
243
+ *) # other
244
+ _dotpi_usage
245
+ dotpi_echo_error "Unknown audio device model: ${model}"
246
+ return 1
247
+ ;;
248
+
249
+ esac
250
+
251
+ # use xargs to unquote value
252
+ model_project="$(dotpi_configuration_read \
253
+ --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
254
+ --key dotpi_audio_device \
255
+ | xargs)"
256
+
257
+
258
+ if [[ "$model_normalised" == "$(dotpi_audio_device_model_normalise "$model_project")" ]] ; then
259
+ # no need to update environment
260
+ return 0;
261
+ fi
262
+
263
+ dotpi_echo_warning 'Audio device model is specific to this instance'
264
+
265
+ # comment setting in project
266
+ dotpi_configuration_comment --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
267
+ --key dotpi_audio_device
268
+
269
+ # escape double-quote for now, to quote when writing later
270
+ model_value="$(echo "$model" | dotpi_sed 's/"/\\"/g')"
271
+
272
+ # write changed setting in instance
273
+ # be sure to quote value
274
+ dotpi_configuration_write --file "${DOTPI_ROOT}/etc/dotpi_environment_instance.bash" \
275
+ --key dotpi_audio_device \
276
+ --value \""${model_value}"\"
277
+ )
278
+
279
+ _dotpi_audio_device_jackd_set_configuration() (
280
+ model="$1"
281
+ (cd "${DOTPI_ROOT}/share/jackd" && ln -s -f "dotpi_jackd_${model}.bash" 'dotpi_jackd.bash') || {
282
+ dotpi_echo_error "Unable to change jackd configuration to ${model}"
283
+ return 1
284
+ }
285
+
286
+ # we can change audio device in a temporary file-system, too
287
+ if dotpi_system_is_raspberry_pi ; then
288
+ dotpi_echo_warning "Restart jackd service"
289
+ service='jackd'
290
+ systemctl is-active "$service" > /dev/null 2>&1 \
291
+ && systemctl restart "$service" || {
292
+ dotpi_echo_warning "systemctl unable to restart service ${service}"
293
+ }
294
+ fi
295
+ )
296
+
297
+ _dotpi_audio_device_disable_all() (
298
+ exception_family="$1"
299
+
300
+ if [[ "$exception_family" = "" ]] ; then
301
+ dotpi_echo_info "Disabling all audio devices"
302
+ else
303
+ dotpi_echo_info "Disabling all audio devices except ${exception_family}"
304
+ fi
305
+
306
+ if [[ ! "$exception_family" = "headphones" ]] ; then
307
+ _dotpi_audio_device_disable_headphones
308
+ fi
309
+
310
+ if [[ ! "$exception_family" = "hdmi" ]] ; then
311
+ _dotpi_audio_device_disable_hdmi
312
+ fi
313
+
314
+ if [[ ! "$exception_family" = "raspberry_pi" ]] ; then
315
+ _dotpi_audio_device_disable_raspberry_pi
316
+ fi
317
+
318
+ if [[ ! "$exception_family" = "hifiberry" ]] ; then
319
+ _dotpi_audio_device_disable_hifiberry
320
+ fi
321
+
322
+ )
323
+
324
+ _dotpi_audio_device_select_headphones() (
325
+ dotpi_echo_info "Installing Headphones (default) audio device"
326
+
327
+ _dotpi_audio_device_disable_all headphones
328
+
329
+ # un-comment dtparam=audio as it must be set first
330
+ # (setting at the end the the file fails)
331
+ pattern="$(dotpi_configuration_get_pattern --prefix "# dtparam=audio")"
332
+ perl -pe "s/${pattern}/"'dtparam=audio=on/' -i -- "$config_file"
333
+
334
+ _dotpi_audio_device_jackd_set_configuration "headphones"
335
+ )
336
+
337
+ _dotpi_audio_device_select_bluetooth() (
338
+ _dotpi_audio_device_disable_all bluetooth
339
+
340
+ _dotpi_audio_device_jackd_set_configuration "bluetooth"
341
+ )
342
+
343
+ _dotpi_audio_device_select_raspberry_pi() (
344
+ dotpi_echo_info "Configuring Raspberry Pi audio device '${model}'"
345
+
346
+ _dotpi_audio_device_disable_all raspberry_pi
347
+
348
+ mkdir -p "$(dirname -- "$config_file")"
349
+ cat >> "$config_file" << EOF
350
+
351
+ # dotpi: overlay for audio device '${model}'
352
+ dtoverlay=${dtoverlay}
353
+
354
+ EOF
355
+
356
+ _dotpi_audio_device_jackd_set_configuration "hifiberry"
357
+ )
358
+
359
+
360
+ _dotpi_audio_device_select_hifiberry() (
361
+ dotpi_echo_info "Configuring HiFiBerry audio device '${model}'"
362
+
363
+ _dotpi_audio_device_disable_all hifiberry
364
+
365
+ mkdir -p "$(dirname -- "$config_file")"
366
+ cat >> "$config_file" << EOF
367
+
368
+ # dotpi: overlay for audio device '${model}'
369
+ dtoverlay=${dtoverlay}
370
+
371
+ EOF
372
+
373
+ _dotpi_audio_device_jackd_set_configuration "hifiberry"
374
+ )
375
+
376
+ _dotpi_audio_device_disable_headphones() (
377
+ dotpi_echo_info "Disabling headphones"
378
+ # disable internal audio device
379
+ dotpi_configuration_comment --file "$config_file" --key 'dtparam=audio'
380
+ )
381
+
382
+ _dotpi_audio_device_disable_hdmi() (
383
+ dotpi_echo_info "Disabling HDMI audio"
384
+ # disable HDMI audio
385
+
386
+ # new systems
387
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=vc4-kms-v3d")"
388
+ perl -pe "s/${pattern}/"'${1}${2},noaudio/' -i -- "$config_file"
389
+
390
+ # old systems
391
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=vc4-fkms-v3d")"
392
+ perl -pe "s/${pattern}/"'${1}${2},audio=off/' -i -- "$config_file"
393
+ )
394
+
395
+ _dotpi_audio_device_disable_raspberry_pi() (
396
+ dotpi_echo_info "Disabling Raspberry Pi audio"
397
+
398
+ # disable hifiberry
399
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=rpi-")"
400
+ perl -pe "s/${pattern}/"'${1}# ${2}${3}/' -i -- "$config_file"
401
+ )
402
+
403
+ _dotpi_audio_device_disable_hifiberry() (
404
+ dotpi_echo_info "Disabling HiFiBerry audio"
405
+
406
+ # disable hifiberry
407
+ pattern="$(dotpi_configuration_get_pattern --prefix "dtoverlay=hifiberry-")"
408
+ perl -pe "s/${pattern}/"'${1}# ${2}${3}/' -i -- "$config_file"
409
+ )
410
+
411
+ _dotpi_audio_device_volume_set_alsa() (
412
+ amixer -- sset "$control" "$volume"
413
+ alsactl store
414
+
415
+ if [[ "$volume" == "$dotpi_audio_volume" ]] ; then
416
+ # no change to write
417
+ return 0
418
+ fi
419
+
420
+ # comment setting in project
421
+ dotpi_configuration_comment --file "${DOTPI_ROOT}/etc/dotpi_environment_project.bash" \
422
+ --key dotpi_audio_volume
423
+
424
+ # escape double-quote for now, to quote when writing later
425
+ model_value="$(echo "$model" | dotpi_sed 's/"/\\"/g')"
426
+
427
+ # write changed setting in instance
428
+ dotpi_configuration_write --file "${DOTPI_ROOT}/etc/dotpi_environment_instance.bash" \
429
+ --key dotpi_audio_volume \
430
+ --value "$volume"
431
+ )
432
+
433
+ # set and store volume for the current audio device
434
+ dotpi_audio_device_volume_set() (
435
+ volume="$1"
436
+
437
+ model="$dotpi_audio_device"
438
+ # lowercase everything
439
+ model_normalised="$(dotpi_audio_device_model_normalise "$model")"
440
+
441
+ case "$model_normalised" in
442
+
443
+ ####### Generic devices
444
+
445
+ 'headphones')
446
+ control='PCM'
447
+ _dotpi_audio_device_volume_set_alsa
448
+ ;;
449
+
450
+ 'bluetooth')
451
+ dotpi_audio_bluetooth_destination_volume_set "$volume"
452
+ ;;
453
+
454
+
455
+ ####### Specific devices
456
+
457
+ # HiFiBerry
458
+ # Cf. https://www.hifiberry.com/docs/archive/mixer-controls-on-the-hifiberry-boards/
459
+
460
+
461
+ 'hifiberry dac+ dsp'|'hifiberry dac+ rtc'|'hifiberry dac+ standard'|'hifiberry amp2'|'hifiberry dac+ adc'|'hifiberry dac+ adc pro'|'hifiberry amp3')
462
+ control='Digital'
463
+ _dotpi_audio_device_volume_set_alsa
464
+ ;;
465
+
466
+ 'hifiberry dac2 hd')
467
+ control='DAC'
468
+ _dotpi_audio_device_volume_set_alsa
469
+ ;;
470
+
471
+ 'hifiberry amp+')
472
+ control='Master'
473
+ _dotpi_audio_device_volume_set_alsa
474
+ ;;
475
+
476
+ ####### Ultimate Ears (UE)
477
+
478
+ 'ue boom 2'|'ue boom 3'|'ue megaboom 2'|'ue megaboom 3')
479
+ dotpi_audio_bluetooth_destination_volume_set "$volume"
480
+ ;;
481
+
482
+ ########### Unknown device
483
+
484
+ *) # other
485
+ dotpi_echo_error "Unable to set volume for unknown audio device model: ${model}"
486
+ return 1
487
+ ;;
488
+
489
+
490
+ esac
491
+ )
492
+
493
+ # restore volume for the current audio device
494
+ dotpi_audio_device_volume_init() (
495
+ if [[ -z "$dotpi_audio_volume" ]] ; then
496
+ dotpi_echo_warning "dotpi_audio_volume not set, keeping default volume"
497
+ return 0
498
+ fi
499
+
500
+ destination_volume="$dotpi_audio_volume"
501
+ dotpi_audio_device_volume_set "$destination_volume"
502
+ )
503
+
504
+