Embedded Linux Systems Programming (2023-02-13 - 2023-02-17)¶
Programming Environment¶
WSL For Core Linux Topics¶
If possible, we should use WSL for core Linux topics. Local editing in Visual Studio Code, local compilation on a virtualized Ubuntu, local running.
Cross Development For Hardware Topics¶
For the more involved hardware related topics, I’ll bring a Raspberry Pi and some network equipment and build up a local network where we can log in from our PCs.
Please check that connecting the course participants’ PCs to some foreign untrusted LAN is permitted IT-wise.
Day 1: Overview¶
Introductory Live Hacking¶
Hammering on a GPIO pin: a typical example of how hardware access works on Linux (from Introductory Live Hacking, Hardware-Wise (sysfs GPIO)).
The Shell¶
Using the Shell’s commandline, an overview is given about Unix system concepts like processes, files, and permissions. Many if not all of these concepts will be viewed programmatically in the remainder of the course. It will be no surprise, for example, that communication with hardware has to do with file I/O.
From The Shell (Bash - “Bourne Again Shell”)
Exercises (from Exercises: The ls Command)
Exercises (from Exercises: Create/Copy/Move/Remove)
Day 2¶
Processes Quick Walk-Through¶
Permissions¶
Development: CMake Quick Intro, And Git Quick Intro¶
Setup Github project for local build: https://github.com/jfasch/2023-02-13
⟶ See instructions on that page
File IO¶
Day 3¶
UART¶
First try: four programs, two on either side (
cat
, andecho
)⟶ weird output when used bidirectionally
Enable “raw mode” 🤔 ⟶ man -s 1 stty
⟶ no special character handling in terminal driver. Want no terminal, want naked UART IO!!
⟶ weird output still weird, but less so
Dedicated input and output programs
First w/o
termios
andTIOCGSERIAL
/TIOCSSERIAL
/ASYNC_LOW_LATENCY
⟶ better
Add
cfmakeraw()
programmatically (the “non-cooked mode” from man -s 1 stty)⟶ this is what we want to see!
“Realtime”:
setserial /dev/ttyUSB0 low_latency
(man -s 8 setserial), only programmaticallysetserial
source code on GithubLinux kernel source code: drivers/tty/tty_io.c
ioctl(fd, TIOCGSERIAL, &serial_struct)
serial_struct.flags |= ASYNC_LOW_LATENCY
ioctl(fd, TIOCSSERIAL, &serial_struct)
tty-bidir-threads.cpp: not separate programs on different fds; two threads hammering on one fd
Livehacking: transform that into event-driven (tty-bidir-events.cpp)
SUSI¶
Advantec Marketingese. Security is the “S” in IoT … that library requires you to run your code as root. Considered cool nonetheless, and marketed heavily by Advantec.
🖕
Exercise¶
Modify that program such that it mimicks cat
:
Is given on single filename argument
Opens that file
Reads its content, and outputs it on standard output
$ ./mycat /etc/passwd
... contents of /etc/passwd here ...
Cross Development¶
Establish a custom built toolchain for Raspberry Pi cross development (from Case Study: Raspberry Toolchain)
Unpacking the toolchain archive: Archiving and Compression
Have a look into how toolchains are made:
Day 4¶
Secure Shell (SSH)¶
Miscellaneous Hardware¶
Group Exercise¶
Send sensor values (W1, and userspace LM73) over CAN
Receive values, and show via a PWM’d LED
Day 5¶
Some OO: PWM display (display-pwm.h, display-pwm.cpp), and associated program (show-pwm-temperature.cpp
Show sine wave: show-pwm-temperature-sine.cpp
Realtime sine waves on 2 LEDs (see
make_realtime()
in show-pwm-temperature-sine.cpp)More OO? Writing temperature to CAN could be viewed as another kind of display. A little C++ interfacery ( display.h, leading to display-can.h, display-can.cpp)
⟶ is-a Display
CIFS/Samba: mounting a Windows share on Linux: https://automationadmin.com/2016/12/mounting-a-samba-share-in-fedora/#gsc.tab=0
Untold¶
More From The Commandline¶
Multithreading (And C++)¶
From Linux Systems Programming: Multithreading:
Race Conditions, and prevention thereof (mutexes)
Communication mechanisms (condition variable)
Atomics
Realtime
From Multithreading