Skip to content

Setting up Headunit Desktop on embedded systems

Viktor Verebelyi edited this page Jun 8, 2022 · 14 revisions

The following guide is mainly targeted at Raspberry Pi OS Lite, but most of these steps are applicable to most Linux systems one-way or another

Adding services files

D-Bus

/lib/systemd/system/dbus-launch.service

[Unit]
Description=D-Bus daemon launch service
After=bluetooth.target

[Service]
Type=simple
ExecStartPre=mkdir -p /run/user/1000
ExecStartPre=chown pi:pi /run/user/1000
ExecStart=sudo -u pi dbus-daemon --session --nofork --address=unix:path=/run/user/1000/user_bus_socket --systemd-activation
Restart=always
RestartSec=0

[Install]
WantedBy=multi-user.target

Pulseaudio

/lib/systemd/system/pulseaudio.service

[Unit]
Description=PulseAudio Sound System
Before=sound.target

[Service]
Type=idle
Environment=DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/user_bus_socket"
# ExecStart=/usr/bin/pulseaudio -v
ExecStart=/usr/bin/pulseaudio
User=pi
Group=pi
Restart=always
RestartSec=3

[Install]
WantedBy=multi-user.target

Obex

/lib/systemd/system/obex.service

[Unit]
Description=Bluetooth OBEX service
After=dbus-launch.service

[Service]
Type=idle
User=pi
Group=pi
BusName=org.bluez.obex
Environment=DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/user_bus_socket" XDG_RUNTIME_DIR=/run/user/1000
ExecStart=/usr/lib/bluetooth/obexd -d -n

[Install]
Alias=dbus-org.bluez.obex.service
WantedBy=multi-user.target

Headunit

/lib/systemd/system/headunit.service

[Unit]
Description=Viktorgino's Headunit Desktop Service
After=local-fs.target

[Service]
Type=idle
User=pi
Group=pi
Environment="QT_QPA_PLATFORM=eglfs"
Environment="QT_QPA_EGLFS_ALWAYS_SET_MODE=1"
Environment="QT_QPA_EGLFS_KMS_ATOMIC=1"
Environment="QT_QPA_EGLFS_HIDECURSOR=1"
Environment="QT_QPA_EGLFS_PHYSICAL_WIDTH=155"
Environment="QT_QPA_EGLFS_PHYSICAL_HEIGHT=86"
Environment=DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/user_bus_socket"
WorkingDirectory=/home/pi/hud
ExecStart=/home/pi/hud/headunit-app
Restart=always

[Install]
WantedBy=sysinit.target

Enable services

sudo systemctl enable headunit.service dbus-launch.service pulseaudio.service obex.service

sudo systemctl enable ofono

D-Bus config

/etc/dbus-1/system.d/headunit.conf

<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
 "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
<busconfig>
  <policy group="bluetooth">
	<allow send_destination="org.ofono"/> 
	<allow send_destination="org.bluez"/> 
	<allow send_destination="org.bluez.obex"/> 
  </policy>
</busconfig>

Add user to bluetooth group

sudo usermod -a -G bluetooth pi

udev rules

/etc/udev/rules.d/99-headunit.rules

SUBSYSTEM=="backlight", RUN+="/bin/chmod 0666 /sys/class/backlight/%k/brightness /sys/class/backlight/%k/bl_power"
SUBSYSTEM=="usb" GROUP="plugdev", MODE="0660"

Setup USB automount

Add mount script

/usr/local/bin/usb-mount.sh

#!/bin/bash

ACTION=$1
DEVBASE=$2
DEVICE="/dev/${DEVBASE}"

# See if this drive is already mounted
MOUNT_POINT=$(/bin/mount | /bin/grep ${DEVICE} | /usr/bin/awk '{ print $3 }')

do_mount()
{
	if [[ -n ${MOUNT_POINT} ]]; then
		# Already mounted, exit
		exit 1
	fi
	
	# Get info for this drive: $ID_FS_LABEL, $ID_FS_UUID, and $ID_FS_TYPE
	eval $(/sbin/blkid -o udev ${DEVICE})

	# Figure out a mount point to use
	LABEL=${ID_FS_LABEL}
	if [[ -z "${LABEL}" ]]; then
		LABEL=${DEVBASE}
	elif /bin/grep -q " /media/${LABEL} " /etc/mtab; then
		# Already in use, make a unique one
		LABEL+="-${DEVBASE}"
	fi
	MOUNT_POINT="/media/${LABEL}"

	/bin/mkdir -p ${MOUNT_POINT}

	# Global mount options
	OPTS="rw,relatime"

	# File system type specific mount options
	if [[ ${ID_FS_TYPE} == "vfat" ]]; then
		OPTS+=",users,gid=100,umask=000,shortname=mixed,utf8=1,flush"
	fi

	if ! /bin/mount -o ${OPTS} ${DEVICE} ${MOUNT_POINT}; then
		# Error during mount process: cleanup mountpoint
		/bin/rmdir ${MOUNT_POINT}
		exit 1
	fi
}

do_unmount()
{
	if [[ -n ${MOUNT_POINT} ]]; then
		/bin/umount -l ${DEVICE}
	fi

	# Delete all empty dirs in /media that aren't being used as mount points. 
	for f in /media/* ; do
		if [[ -n $(/usr/bin/find "$f" -maxdepth 0 -type d -empty) ]]; then
			if ! /bin/grep -q " $f " /etc/mtab; then
				if [ -d "$DIR" ]; then
					/bin/rmdir "$f"
				fi
			fi
		fi
	done
}
case "${ACTION}" in
	add)
		do_mount
		;;
	remove)
		do_unmount
		;;
esac

Add mount service

/etc/systemd/system/usb-mount@.service

[Unit]
Description=Mount USB Drive on %i

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/usr/local/bin/usb-mount.sh add %i
ExecStop=/usr/local/bin/usb-mount.sh remove %i

Add udev rules

/etc/udev/rules.d/99-local.rules

KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="add", RUN+="/bin/systemctl start usb-mount@%k.service"
KERNEL=="sd[a-z][0-9]", SUBSYSTEMS=="usb", ACTION=="remove", RUN+="/bin/systemctl stop usb-mount@%k.service"

Reload udev and systemd

sudo udevadm control --reload-rules
sudo systemctl daemon-reload

Source : https://andreafortuna.org/2019/06/26/automount-usb-devices-on-linux-using-udev-and-systemd/

Optional

Setting up a DAC

Comment out the dtparam=audio=on with a # and add the overlay for the choice of your DAC (refer to their respective manuals)

Set ALSA volume to 100%

amixer sset 'Master' 100%

Clone this wiki locally