Category Archives: Raspberry Pi

Running cec-client with Raspberry PI on Debian or Ubuntu 64-bit

Using cec-client with a raspberry pi on debian or ubuntu does not work out of the box, as the cec-client in the debian and ubuntu repositories has not been compiled with support for the propriary raspberry libraries. In addition, these libraries are 32-bit only. This is a guide in how to get this working.

First you need to set up a 32-bit chroot environment. Install debootstrap and then run

debootstrap --variant=buildd --arch=armhf bullseye /opt/cec-client/

You will then need to download the raspberry pi libraries. Unpack /opt/vc from the tarball into /opt/cec-client (replace url with newer version if needed):

cd /tmp
curl -L https://github.com/raspberrypi/firmware/archive/1.20210108.tar.gz | tar zxf -
cp -r firmware*/opt/ /opt/cec-client/

Add the raspberry pi lib to ldconfig by running

echo "/opt/vc/lib" > /opt/cec-client/etc/ld.so.conf.d/rpi.conf

You can now compile libcec and cec-utils:

chroot /opt/cec-client
ldconfig
apt-get update
apt-get install cmake libudev-dev libxrandr-dev python3-dev swig git 
cd /tmp/ 
git clone https://github.com/Pulse-Eight/platform.git
mkdir platform/build
cd platform/build
cmake .. 
make 
make install
cd /tmp/
git clone https://github.com/Pulse-Eight/libcec.git
mkdir libcec/build
cd libcec/build
cmake -DRPI_INCLUDE_DIR=/opt/vc/include -DRPI_LIB_DIR=/opt/vc/lib ..
make -j4 
make install
ldconfig
cd /tmp/
mkdir libcec/src/cec-client/build/
cd libcec/src/cec-client/build/
make
make install

In order to run cec-client inside the chroot, you will also need access to devices. You can do this by mounting this up before running chroot, but an easier and better way is to use systemd. Many of the examples you will find of how to use cec-client adds the -s flag to run a single command, but without this, cec-client runs until told to quit and listens on commands on stdin. We will use this and let systemd set up a fifo for commands for us. Create the socket and service files:

[Unit]
Description=CEC client socket

[Socket]
ListenFIFO=/run/cec.fifo

[Install]
WantedBy=sockets.target
[Unit]
Description=CEC client
After=network.target

[Service]
Type=simple
Restart=no
RootDirectory=/opt/cec-client
ExecStart=/usr/local/bin/cec-client -d 1
ExecStop=/bin/bash -c "echo q > /run/cec.fifo"
StandardInput=socket
StandardOutput=journal
MountAPIVFS=yes

[Install]
WantedBy=multi-user.target

Then reload systemd, enable and start:

systemctl daemon-reload
systemctl enable cec-client.socket
systemctl start cec-client.socket

You can now send cec-client commands by writing to /run/cec.fifo, for instance to turn on the tv with address 0.0.0.0 run

echo 'on 0.0.0.0' > /run/cec.fifo