Running PHP on Embedded Devices
PHP Dutch Conference 2009
12.06.2009
Michael Wittke
Leibniz University of Hannover
Institute of Systems Engineering
Department of Computer Science
1 PHP Dutch Conference – Michael Wittke 12.06.2009
Who am I?
Michael Wittke:
PhD student from Hannover:
Research focuses on self-configuration for Mobile Vision Networks:
Networks with high mobility
Needs for a steady re-calibration of the network
I am addicted to PHP:
Programming websites in PHP since 1998
Programmed my website (foodplaner.de) in PHP with more than 2,000
LOCs
foodplaner.de is a platform for planning the nutrition (e.g. food diary,
calorie table with more than 30,000 foods etc.)
…and I use PHP at University for programming
embedded devices, since life is too short for C!
2 PHP Dutch Conference – Michael Wittke 12.06.2009
Distributed Smart Cameras at a glance
What is it?
Each Smart Camera Node:
Includes a PTZ camera or webcam
Local processing resources
(CPU, memory, etc.)
Communication interface to exchange
information with other Smart Camera Nodes
(e.g. wireless ad-hoc or wired network)
Networked Smart Camera Nodes
Cameras can cooperatively solve surveillance tasks
Achieve goals that cannot be achieved with a single camera, e.g.
wide-area object tracking, multi-view observation
3 PHP Dutch Conference – Michael Wittke 12.06.2009
System Architecture for Distributed Smart Cameras
Advances in computer vision make way
for intelligent cameras:
Smart Cameras cooperate and detect
events of interest autonomously
Alarm Management: Smart Cameras
communicate with mobile devices
Research focuses on e.g. system
architecture:
Distributed algorithms, protocols
One example:
Robust Online Camera
Alignment System (ROCAS)
4 PHP Dutch Conference – Michael Wittke 12.06.2009
Embedded DISC
Each mobile phone with built-in CMOS sensor
can be seen as Smart Camera itself:
Due to mass production their price decreases
steadily
But: Processing capacity, networking abilities
and connectivity increases
Makes way for:
Mobile Surveillance Applications
Research focuses on self-configuration for
Mobile Vision Networks:
Dynamic configuration space of mobile nodes
Time synchronization (when? and how long?)
Spatial calibration (where?)
Network Synchronization
5 PHP Dutch Conference – Michael Wittke 12.06.2009
Agenda
Platform-independent Filing System
How to set up PHP on embedded devices
NSLU2 (Network Attached Storage)
Setting up Debian ARM
Nokia N810
Compiling natively
Virtualization / Static Cross-Compiling
Scratchbox / Dynamic Cross-Compiling
The SmartCam Lab
The Power of PHP
e.g. Live Video Streaming (10 LOCs)
6 PHP Dutch Conference – Michael Wittke 12.06.2009
Platform-independent
Filing System
7 PHP Dutch Conference – Michael Wittke 12.06.2009
Filing System vs. File System
(Digital) Filing System File System
• Temporary and permanent • Method for storing and
storage (e.g. of references, organizing computer files and
commands, configurations, the data they contain
settings etc.)
8 PHP Dutch Conference – Michael Wittke 12.06.2009
My Working Place
A wild mixture of (embedded) devices:
Five devices dominate my everyday working life for building
embedded smart cameras:
ThinkPad laptop
(running Ubuntu 7.10)
HP laptop
(running Ubuntu 9.04)
Nokia N 810 wireless Internet
tablet (running MAEMO)
NSLU2 network attached
storage (running Debian ARM)
SNOM IP telephone
(running Debian ARM)
Wireless Internet Tablet NSLU2
IP telephone
Nokia N810
9 PHP Dutch Conference – Michael Wittke 12.06.2009
Problem Statement
Problem:
Linux shell is a command line tool
Approx. 30 commands are used each day
Not all commands are used frequently
Commands are used throughout all devices
Not all devices allow for copying instructions
(e.g. in case of web access from any browser window)
…but I suffer from a short term memory ;)
Bash history is hard to synchronize via multiple devices
List lengths may vary on distinct devices
History is cycle buffer
10 PHP Dutch Conference – Michael Wittke 12.06.2009
Idea
Idea: Cross-device filing system
University’s
Storing data such as
WLAN
Syntax of Linux commands
(e.g., find <path> -i name
<keyword>, …)
References to websites
(e.g. “how to set up Debian
ARM?” etc.)
Configurations
(e.g. setting in Central
/etc/network/interfaces, Database
/etc/wpa_supplicant.conf etc.)
Reading stored by each device
11 PHP Dutch Conference – Michael Wittke 12.06.2009
Specification
Storing interface
Syntax: h <command> <description>
e.g. h ‘echo hello’ ‘print messages on command line’
Running on each device
Easy access via command line
(shell script)
Reading interface
Syntax: h –<option> <keyword>
h –d <keyword>
e.g. h -d ‘print messages’
Output: echo hello
h –r <keyword>
e.g. h -r ‘echo’
Output: echo hello
12 PHP Dutch Conference – Michael Wittke 12.06.2009
Implementing a SOAP-Server in PHP
Communication using function add($command, $desc, $os)
SOAP* {
//business logic for adding
Simple Object Access Protocol }
Lightweight XML-based protocol
for exchanging information function search($s)
between distributed applications {
//business logic for searching
Uniform interface for the clients }
$server = new SoapServer(NULL,
Setup in PHP: array('uri' =>
"http://localhost/"));
Easy to install $server->addFunction('add');
(i.e. SOAP extension) $server->addFunction('search');
4 LOCs for setting up the server $server->handle();
*http://de.wikipedia.org/wiki/SOAP
13 PHP Dutch Conference – Michael Wittke 12.06.2009
Implementing a SOAP-Client in PHP
Client-side $command = $argv[1];$desc = $argv[2]; $os = $argv[3];
Communication
$client = new SoapClient( NULL,
Uniform interface for array( "location" => $url,
clients "uri" => "urn:xmethodsTS",
"style" => SOAP_RPC,
"use" => SOAP_ENCODED));
Setup in PHP:
$p = array(
Easy to install new SoapParam($desc, 'str'),
(--with-xml new SoapParam('c', 'flag'));
compiling option)
$result = $client->__call( "search", $p,
3 LOCs for setting array( "uri"=>"urn:xmethodsTS",
up the client "soapaction"=>"urn:xmethodsTS#search"));
echo $result;
14 PHP Dutch Conference – Michael Wittke 12.06.2009
Wrapping PHP into a Shell Script
Bash script
Bash wrapper script
for calling PHP
Bash script has to be
adapted to the #!/bin/sh // for Nokia N810
operating system //#!/bin/bash // for NSLU2, Ubuntu
(e.g. Nokia N810)
OS="`uname -a`"
Added to /usr/bin for echo "OS: $OS"
easy access echo "command: $1"
echo "description: $2"
php5 /usr/bin/soapclient.php
"$1" "$2" "$OS"
15 PHP Dutch Conference – Michael Wittke 12.06.2009
Example Embedded Devices and Setting up PHP
16 PHP Dutch Conference – Michael Wittke 12.06.2009
Hardware platform #1 for running PHP
NSLU2
(Network Attached
Storage)
ARM processor, 250Mhz
DDR RAM 64MB
External hard disk
Network connection
USB high speed for PC
connectivity
17 PHP Dutch Conference – Michael Wittke 12.06.2009
Software platform for running PHP
Installation of Debian ARM
Change IP settings
(e.g. DHCP server)
Download SlugOS firmware
Flash device
sudo upslug2 -i di-slu2.bin
Grab Debian GNU/Linux lenny (5.0)
from slug-firmware.net and follow
this installation guide:
http://www.cyrius.com/debian/
nslu2/install.html
Installation will take roughly
4 hours
apt-get php5-cli
apt-get php5-xml
18 PHP Dutch Conference – Michael Wittke 12.06.2009
Hardware platform #2 for running PHP
Nokia N 810
High-resolution display (800 x
480 pixels) with up to 65,000
colors
TI OMAP 2420 ARM processor,
400Mhz
DDR RAM 128MB, Flash 256MB
WLAN standard: IEEE 802.11b/g
USB high speed for PC
connectivity
Internet Tablet OS:
maemo Linux based OS2008
19 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
Installing PHP
apt-get install php5-cli
apt-cache search xml | grep php
Welcome to the world of
cross-compiling!
20 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
What is cross-compiling?
Compiling natively is too slow
Use some fast processor (HOST)
to compile software for some
slow processor (TARGET) that
uses a different architecture:
HOST cannot run the
software natively which it
compiles
software is compiled for
another processor
But: Build environments need
to run programs during the
compiling process (CRASH!)
21 PHP Dutch Conference – Michael Wittke 12.06.2009
Compiling on NSLU2 - Running on Nokia N810
Native Compiling on ARM
hardware #include <stdio.h>
Dynamic (file size: 7 KByte)
main()
gcc ./hello.c -o hello {
printf("Hello world!");
file ./hello }
Output:
ELF 32-bit.. dynamically
linked (uses shared libs)
ldd ./hello
Output:
libc.so.6 => /lib/libc.so.6 ..
Executing hello on Nokia N810
-sh: ./hello: not found
22 PHP Dutch Conference – Michael Wittke 12.06.2009
Compiling on NSLU2 - Running on Nokia N810
Native Compiling on ARM
hardware #include <stdio.h>
Static (file size: 500 KByte)
main()
gcc --static ./hello.c -o {
hello printf("Hello world!");
}
file ./hello
Output:
ELF 32-bit.. statically linked
ldd ./hello
Output:
not a dynamic executable
Executing hello on Nokia N810
Hello world
23 PHP Dutch Conference – Michael Wittke 12.06.2009
Compiling on NSLU2 - Running on Nokia N810
Static Compiling of PHP on NSLU2
Static (--static)
Compilation time on NSLU2
• configure: 5 minutes
• make: roughly 3 hours
File size: 15 MByte
(bad for memory performance on
embedded devices)
24 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
What is Virtualization?
Cross-platform virtualization
allows software compiled for a specific
CPU and operating system to run
unmodified on computers with different
CPUs and/or operating systems
Platform virtual machines
http://en.wikipedia.org/wiki/Comparison_of
_platform_virtual_machines
X86: Bochs, VMware
MIPS: GXemul, Imperas, OKL4, OVPsim
ARM: GXemul, Imperas, QEMU
25 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
Virtualizer QEMU
http://www.nongnu.org/qemu
QEMU is a generic and open
source machine emulator and
virtualizer
Shows a good performance by
using dynamic translation
Driver called KQEMU (QEMU
accelerator) achieves great
speed up in case of x86
architectures
26 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
Virtualized Debian-ARM in QEMU
http://www.aurel32.net/info/
debian_arm_qemu.php
Install QEMU
sudo apt-get install qemu
Create image
qemu-img create hd0.img 5G
Download vmlinuz and initrd from debian.org
Start QEMU
qemu-system-arm
-M versatilepb
-kernel vmlinuz-2.6.26-2-versatile
-initrd initrd.gz
-hda hd0.img
-append "root=/dev/ram"
27 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
Static Compiling PHP in QEMU
Static (--static)
Compilation time in QEMU
• configure: 5 minutes
• make: roughly 1.5 hours
File size: 15 MByte as well
Dynamic compiling is not possible due to
distinct libc-versions
28 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
What is the Scratchbox?
Comes from "Linux from scratch" +
"chroot jail" (sandbox).
Virtualization is limited to a minimum
Great speedup
E.g. virtualizing build-environment
E.g. /proc is not emulated
Cross-compilation toolkit designed to
make embedded Linux application
development easier
Supports ARM and x86 targets (PowerPC,
MIPS and CRIS targets are experimental)
Provides glibc and uClibc as C-library
choices
29 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
Scratchbox for Maemo
www.maemo.org
maemo = Nokia N810 operating system
(Debian-based)
Scratchbox for maemo = software
package to implement development
sandboxes (for isolation)
Contains easy-to-use tools to assist cross-
compilation
Supports multiple developers using the
same development system
Supports multiple configurations for each
developer
Supports running non-native binaries on
the host system via instruction set
30 emulators PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
Setting up Maemo Scratchbox
http://maemo.org/maemo_release_documentation/
maemo4.1.x/node4.html
Installing Scratchbox
Download installer script from
http://repository.maemo.org/
Installing Maemo SDK
Download installer script from
http://repository.maemo.org/
Give executable permissions to both scripts
Start sbox and login:
sudo /scratchbox/sbin/sbox_ctl start
/opt/scratchbox/login
31 PHP Dutch Conference – Michael Wittke 12.06.2009
Running PHP on Nokia N810
Dynamic compiling of PHP
Dynamic compiling is possible due
to the sane environment
Dynamic
Compilation time in QEMU
• configure: 2 minutes
• make: 5 minutes
File size: 9 MByte
32 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam
Lab
33 PHP Dutch Conference – Michael Wittke 12.06.2009
Motivation “The SmartCam Lab”
The SmartCam Lab
Lab for students to improve their abilities in
programming embedded devices
Time: 3 days à 4 hours
Goal:
Home protection by video surveillance
Use off-the-shelf and low cost hardware
Low power consumption
Access to the camera’s real-time video
stream via VLC client
Face detection and store recognized
faces in a database for 30 days
34 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
Idea HW/SW
Use NSLU2 with Debian-ARM as
implementation platform
NSLU2 is off-the-shelf:
low cost (< 60 €) & low power (ARM chip)
USB-Webcam: Quickcam Pro 9000
Use PHP in education:
Scripting language (easy to learn)
Professional software tools are available
(e.g. symfony with MVC, ORM etc.)
Available on Debian-ARM as package
Image processing library with PHP interface:
Intel’s OpenCV (image processing library)
35 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
Using PHP to solve the following
tasks
Task 1: Last activities
Website showing the last recognized
faces
Task 2: Detailed view of activity
Captured picture with data
Task 3: Webcam posts recognized face
Webcam captures a picture and start a face
detection algorithm. If a face is recognized,
this is stored in the database.
Task 4: Video Live Stream Server
Live access to the webcam via VLC
36 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
PHP Task 1 – Setting up PHP
Starting up the project
Installing PHP, MySQL and Apache
on the NSLU2
Installing symfony with doctrine as
ORM
Setting up the data model and fixture data
i.e. activity and category
(e.g. category could be the living room or the
office hallway, activity describes the detected
faces)
37 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
PHP and Image Processing
OpenCV (Open Source Computer Vision) is
a library of programming functions mainly
aimed at real time computer vision.
Example applications of the OpenCV
library:
Human-Computer Interaction (HCI)
Object Identification
Segmentation and Recognition
Face Recognition
Gesture Recognition
Motion Tracking
Motion Understanding
Mobile Robotics
Stereo and Multi-Camera Calibration
38 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
PHP and Face Detection #1
Face detection in C
Cross-compiling OpenCV libs
• configure --without-x --
without-python
• Roughly 8 hours on NSLU2
Use facedetect.c from the
opencv/samples/c/
directory
Send events via cURL to the
webserver
• cURL is a command line tool for
transferring files with URL syntax
• Just 20 LOCs
39 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
PHP and external C scripts
Face detection in C/PHP wrapper script
Use facedetect.c and add
fprintf(stdout, "%d\n", num_faces);
return 0;
Execution of an external C program:
$num_faces =
exec("facedetect ...", $dummy, $rcode);
• Does not work in 'safe mode'.
• Length of command line give critical
restrictions
Windows cmd.exe 8k
Windows cygwin bash 32k
Linux bash 128k (may vary)
40 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
PHP Facedetect
Face detection in PHP
www.xarg.org/project/php-facedetect/
Install OpenCV
Install PHP Facedetect
www.xarg.org/download/facedetect-
1.0.0.tar.gz
Recognize faces in any PHP script by the
following command:
$num_faces =
face_detect($image,$path_to_haarcascade)
41 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
PHP and Video Streaming
VideoLAN is a complete software
solution for video streaming and
playback (GPL)
Originally designed to stream MPEG
videos on high bandwidth networks
Full-featured, cross-platform
media player
VLC works on many platforms:
Linux, Windows, Mac OS X, …
VLC can also be used as a
streaming server.
42 PHP Dutch Conference – Michael Wittke 12.06.2009
The SmartCam Lab
#!/usr/bin/php -q //quiet mode
Video Streaming in PHP
$ip = "...."; $port = "1234";
Create a socket for
communication with VLC $sock = socket_create(AF_INET,
client (port 1234) SOCK_STREAM, SOL_TCP);
AF_INET family
socket_bind($sock, $ip, $port);
SOCK_STREAM for TCP socket_listen($mysock, 5);
connection $c = socket_accept($sock);
Pictures are loaded from a socket_write($c,"HTTP/1.1 200 OK\r\n");
local directory socket_write($c,
(e.g. captured via gstreamer) "Content-Type: image/jpeg\r\n");
Loaded pictures are written to socket_write($c, "\r\n");
the socket while (true) {
10 LOCs for implementing a //capture/load pic & write to socket
}
VLC server in PHP socket_close($mysock);
43 PHP Dutch Conference – Michael Wittke 12.06.2009
Summary
Cross-Compiling of PHP
1. Statically on HW (NSLU2)
2. Statically in Virtualizer
(Debian ARM in QEMU)
3. Dynamically in Scratchbox
(for Nokia N810)
Using PHP for
1. SOAP clients/server
~3 - 4 LOC
2. Wrapper for external C programs
~1 LOC
3. Implementing face detection
(OpenCV)
~1 LOC
4. Implementing a VLC server
~ 10 LOCs
44 PHP Dutch Conference – Michael Wittke 12.06.2009
Work to be done
Execution of external programs
has to be improved
e.g. tricky workflow ;)
Starting webserver script and
facedetection by one PHP wrapper
script => I failed
CPU and memory utilization of PHP
should be reduced
Standard libraries for PHP
development have to be ported to
embedded architectures (e.g.
ARM, MIPS etc.)
…
45 PHP Dutch Conference – Michael Wittke 12.06.2009
This is not a Smart
Camera!
Thank you for your attention!
46 PHP Dutch Conference – Michael Wittke 12.06.2009