Erlang

From Openmoko

(Difference between revisions)
Jump to: navigation, search
(Install SciTE)
(Seting up SciTE for openmoko development)
Line 161: Line 161:
  
 
All I have to do now is to re-start X whenever I want to run an uploaded app. This is what the last two menu buttons do in SciTe - stop X on the phone, and start X on the phone.
 
All I have to do now is to re-start X whenever I want to run an uploaded app. This is what the last two menu buttons do in SciTe - stop X on the phone, and start X on the phone.
 +
[[Image:dialer.jpg]]
  
 
Type
 
Type

Revision as of 21:03, 28 October 2008

Contents

Introduction

Erlang is a programming language which has many features more commonly associated with an operating system than with a programming language: concurrent processes, scheduling, memory management, distribution, networking, etc.

Concurrency

- Erlang has extremely lightweight processes whose memory requirements can vary dynamically. Processes have no shared memory and communicate by asynchronous message passing. Erlang supports applications with very large numbers of concurrent processes. No requirements for concurrency are placed on the host operating system.

Distribution

- Erlang is designed to be run in a distributed environment. An Erlang virtual machine is called an Erlang node. A distributed Erlang system is a network of Erlang nodes (typically one per processor). An Erlang node can create parallel processes running on other nodes, which perhaps use other operating systems. Processes residing on different nodes communicate in exactly the same was as processes residing on the same node.

Robustness

- Erlang has various error detection primitives which can be used to structure fault-tolerant systems. For example, processes can monitor the status and activities of other processes, even if these processes are executing on other nodes. Processes in a distributed system can be configured to fail-over to other nodes in case of failures and automatically migrate back to recovered nodes.

Soft real-time

- Erlang supports programming "soft" real-time systems, which require response times in the order of milliseconds. Long garbage collection delays in such systems are unacceptable, so Erlang uses incremental garbage collection techniques.

Hot code upgrade

- Many systems cannot be stopped for software maintenance. Erlang allows program code to be changed in a running system. Old code can be phased out and replaced by new code. During the transition, both old code and new code can coexist. It is thus possible to install bug fixes and upgrades in a running system without disturbing its operation.

Incremental code loading

- Users can control in detail how code is loaded. In embedded systems, all code is usually loaded at boot time. In development systems, code is loaded when it is needed, even when the system is running. If testing uncovers bugs, only the buggy code need be replaced.

External interfaces

- Erlang processes communicate with the outside world using the same message passing mechanism as used between Erlang processes. This mechanism is used for communication with the host operating system and for interaction with programs written in other languages. If required for reasons of efficiency, a special version of this concept allows e.g. C programs to be directly linked into the Erlang runtime system.

Fast and Lean

- Erlang is fast and lean. A general erlang application has lots of processes. If you are used to OOP you can compare a process with an object. One process per object instance (not class). To make sure the neo could cope with this I watched top while I was busy dialing a phone number on the Aphasia dialer. Each of the twelve buttons are a separate process. Each have its own little animation, and if you look carefully you can see they run in parallell and are acting independent of each other. The top statistics show a really low CPU utilization, I have to dial quite fast to exceed 5% CPU. Next test I made was to measure the time it takes to start these 12 processes and have them display themselves on the screen (aka start the dialer app). It takes approximately 0.5 sec!

Installation

Make sure you have an uboot that is compatible, I used Uboot u-boot-gta01bv4-1.2.0+svnnow-r13_0_2632_0.bin

Install Debian, install script is here http://wiki.openmoko.org/wiki/Debian

Follow the instructions. I had to type in the setenv string and then saveenv. Reboot to get the new menu_4 title to show up.

Install erlang

apt-get install erlang-base


Install the GUI package ex11

Take a look at this example of what you can do with ex11 [1]

Download from here [2]

In home directory file .profile

export DISPLAY=:0

Remove "-nolisten tcp" from /etc/X11/xinit/xservrrc

Install xauth package and create .Xauthenticate file

debian-gta01:~# apt-get install xauth
debian-gta01:~# xauth
xauth:  creating new authority file /root/.Xauthority
Using authority file /root/.Xauthority
xauth> q

Copy .Xauthenticate too /


When everything is installed you can try this "hello World" code to verify the platform. It will create an empty window.

-module(test).
-export([start/0]).
-import(ex11_lib,[eMapWindow/1,xDo/2,xFlush/1,xColor/2,xCreateSimpleWindow/7]).
-include("ex11_lib.hrl").
-define (WT,480).
-define (HT,640).

start() -> spawn(fun win/0).

win() ->
{ok, Display} = ex11_lib:xStart("3.1"),
Win = xCreateSimpleWindow(Display,0,0,?WT,?HT,?XC_arrow,xColor(Display,?blue)),
xDo(Display, eMapWindow(Win)),
xFlush(Display),
loop().

loop() ->
receive
Any -> io:format("Unknown Event~p~n",[Any]), loop()
end.

Put this code in a file named test.erl Compile the file with

erlc test.erl

Run from erlang console.

erl
test:start().

Erlang development environment

You can in fact choose any text editor, and use a terminal window to compile and copy compiled erlang beam-files to the phone. However, I have found SciTE to be a nice choice. It has erlang colour coding, bracket matching, code completion and lots of more stuff you are accustomed to find in a programmers editor. And it's easily configurable to handle compilation and error tracking, click on an error message to go to the line where the error is located.

In addition to all this you can set it up to let you run your erlang app on the host, and to copy the compiled app to the phone and run it there. All from within SciTE. This gives a very fast turnaround time, I can compile my app - run on host - copy to neo - run on neo in less than a minute. Part of this fast development cycle is of course due to the fact that erlang is platform agnostic - the same compiled code runs on Arm and Intel Linux and Windows.

Install SciTE

Download from here http://www.scintilla.org/SciTE.html and install.

Seting up SciTE for openmoko development

Before you start configuring SciTE, make sure ssh, scp and rsh works from a terminal window. I have to run this script whenever I connect the neo to the host

#!/bin/bash
ifconfig usb0 192.168.0.200 netmask 255.255.255.0
/sbin/route add -host 192.168.0.202/32 dev usb0
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A POSTROUTING -t nat -j MASQUERADE -s 192.168.0.0/24
iptables -P FORWARD ACCEPT

Start SciTE and select menu Options|Open Global Options and uncomment

#Erlang|erl||\

and at other lines where erlang is a comment. You may have to start SciTE as root to do this.

Create a file named SciTE.properties in your erlang source directory.

# SciTE.properties is the per directory local options file and can be used to
# override settings made in SciTEGlobal.properties, SciTEUser.properties and
# SciTEDirectory.properties.
command.subsystem.1.*.erl=0
command.name.1.*.erl=svn update
command.1.*.erl= svn update $(FileNameExt)
command.name.2.*.erl= svn ci
command.2.*.erl= svn ci -m '$(4)' $(FileNameExt)
command.name.3.*.erl=svn co
command.3.*.erl= svn co $(4)
command.name.4.*.erl=svn status
command.4.*.erl= svn status
command.name.5.*.erl=erl evaluate
command.5.*.erl= erl -eval '$(CurrentSelection)' -noshell -s init stop
command.subsystem.6.*.erl=0
command.name.6.*.erl=copy to neo1973
command.6.*.erl= scp $(FileName).beam   phone:/root/erlang/trunk/
command.subsystem.7.*.erl=0
command.name.7.*.erl=add to subversion
command.7.*.erl= svn add $(FileNameExt)
command.subsystem.8.*.erl=0
command.name.8.*.erl=stop X
command.8.*.erl= rsh root@192.168.0.202 /etc/init.d/zhone-session stop
command.subsystem.9.*.erl=0
command.name.9.*.erl=start X
command.9.*.erl= rsh root@192.168.0.202 /etc/init.d/zhone-session start

I am developing a vertical market product based on Openmoko, so I want my erlang platform alone to run (no window manager or Zhone) when the phone is switched on. This is my /usr/bin/zhone-session. I keep my erlang beam files at /root/erlang/trunk

#zhone &
#matchbox-keyboard-toggle &
erl -noshell -pa /root/erlang/trunk -s main start > debug.txt
#exec matchbox-window-manager -use_titlebar no

All I have to do now is to re-start X whenever I want to run an uploaded app. This is what the last two menu buttons do in SciTe - stop X on the phone, and start X on the phone. File:Dialer.jpg

Type

ssh phone

to get a terminal window and type

tail -f /debug.txt

to read shell printout from the erlang process.

Example Code

Use mdbus in erlang

Dbus = open_port({spawn,"mdbus -s"}, [stream, use_stdio, binary]),
loop(Dbus).

And get the response

loop(Dbus) ->
receive
{Dbus,Msg} -> io:format("Dbus msg:~p~n",[Msg]),loop(Dbus)
end.
Personal tools

Introduction

Erlang is a programming language which has many features more commonly associated with an operating system than with a programming language: concurrent processes, scheduling, memory management, distribution, networking, etc.

Concurrency

- Erlang has extremely lightweight processes whose memory requirements can vary dynamically. Processes have no shared memory and communicate by asynchronous message passing. Erlang supports applications with very large numbers of concurrent processes. No requirements for concurrency are placed on the host operating system.

Distribution

- Erlang is designed to be run in a distributed environment. An Erlang virtual machine is called an Erlang node. A distributed Erlang system is a network of Erlang nodes (typically one per processor). An Erlang node can create parallel processes running on other nodes, which perhaps use other operating systems. Processes residing on different nodes communicate in exactly the same was as processes residing on the same node.

Robustness

- Erlang has various error detection primitives which can be used to structure fault-tolerant systems. For example, processes can monitor the status and activities of other processes, even if these processes are executing on other nodes. Processes in a distributed system can be configured to fail-over to other nodes in case of failures and automatically migrate back to recovered nodes.

Soft real-time

- Erlang supports programming "soft" real-time systems, which require response times in the order of milliseconds. Long garbage collection delays in such systems are unacceptable, so Erlang uses incremental garbage collection techniques.

Hot code upgrade

- Many systems cannot be stopped for software maintenance. Erlang allows program code to be changed in a running system. Old code can be phased out and replaced by new code. During the transition, both old code and new code can coexist. It is thus possible to install bug fixes and upgrades in a running system without disturbing its operation.

Incremental code loading

- Users can control in detail how code is loaded. In embedded systems, all code is usually loaded at boot time. In development systems, code is loaded when it is needed, even when the system is running. If testing uncovers bugs, only the buggy code need be replaced.

External interfaces

- Erlang processes communicate with the outside world using the same message passing mechanism as used between Erlang processes. This mechanism is used for communication with the host operating system and for interaction with programs written in other languages. If required for reasons of efficiency, a special version of this concept allows e.g. C programs to be directly linked into the Erlang runtime system.

Fast and Lean

- Erlang is fast and lean. A general erlang application has lots of processes. If you are used to OOP you can compare a process with an object. One process per object instance (not class). To make sure the neo could cope with this I watched top while I was busy dialing a phone number on the Aphasia dialer. Each of the twelve buttons are a separate process. Each have its own little animation, and if you look carefully you can see they run in parallell and are acting independent of each other. The top statistics show a really low CPU utilization, I have to dial quite fast to exceed 5% CPU. Next test I made was to measure the time it takes to start these 12 processes and have them display themselves on the screen (aka start the dialer app). It takes approximately 0.5 sec!

Installation

Make sure you have an uboot that is compatible, I used Uboot u-boot-gta01bv4-1.2.0+svnnow-r13_0_2632_0.bin

Install Debian, install script is here http://wiki.openmoko.org/wiki/Debian

Follow the instructions. I had to type in the setenv string and then saveenv. Reboot to get the new menu_4 title to show up.

Install erlang

apt-get install erlang-base


Install the GUI package ex11

Take a look at this example of what you can do with ex11 [1]

Download from here [2]

In home directory file .profile

export DISPLAY=:0

Remove "-nolisten tcp" from /etc/X11/xinit/xservrrc

Install xauth package and create .Xauthenticate file

debian-gta01:~# apt-get install xauth
debian-gta01:~# xauth
xauth:  creating new authority file /root/.Xauthority
Using authority file /root/.Xauthority
xauth> q

Copy .Xauthenticate too /


When everything is installed you can try this "hello World" code to verify the platform. It will create an empty window.

-module(test).
-export([start/0]).
-import(ex11_lib,[eMapWindow/1,xDo/2,xFlush/1,xColor/2,xCreateSimpleWindow/7]).
-include("ex11_lib.hrl").
-define (WT,480).
-define (HT,640).

start() -> spawn(fun win/0).

win() ->
{ok, Display} = ex11_lib:xStart("3.1"),
Win = xCreateSimpleWindow(Display,0,0,?WT,?HT,?XC_arrow,xColor(Display,?blue)),
xDo(Display, eMapWindow(Win)),
xFlush(Display),
loop().

loop() ->
receive
Any -> io:format("Unknown Event~p~n",[Any]), loop()
end.

Put this code in a file named test.erl Compile the file with

erlc test.erl

Run from erlang console.

erl
test:start().

Erlang development environment

You can in fact choose any text editor, and use a terminal window to compile and copy compiled erlang beam-files to the phone. However, I have found SciTE to be a nice choice. It has erlang colour coding, bracket matching, code completion and lots of more stuff you are accustomed to find in a programmers editor. And it's easily configurable to handle compilation and error tracking, click on an error message to go to the line where the error is located.

In addition to all this you can set it up to let you run your erlang app on the host, and to copy the compiled app to the phone and run it there. All from within SciTE. This gives a very fast turnaround time, I can compile my app - run on host - copy to neo - run on neo in less than a minute. Part of this fast development cycle is of course due to the fact that erlang is platform agnostic - the same compiled code runs on Arm and Intel Linux and Windows.

Install SciTE

Download from here http://www.scintilla.org/SciTE.html and install.

Seting up SciTE for openmoko development

Before you start configuring SciTE, make sure ssh, scp and rsh works from a terminal window. I have to run this script whenever I connect the neo to the host

#!/bin/bash
ifconfig usb0 192.168.0.200 netmask 255.255.255.0
/sbin/route add -host 192.168.0.202/32 dev usb0
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -A POSTROUTING -t nat -j MASQUERADE -s 192.168.0.0/24
iptables -P FORWARD ACCEPT

Start SciTE and select menu Options|Open Global Options and uncomment

#Erlang|erl||\

and at other lines where erlang is a comment. You may have to start SciTE as root to do this.

Create a file named SciTE.properties in your erlang source directory.

# SciTE.properties is the per directory local options file and can be used to
# override settings made in SciTEGlobal.properties, SciTEUser.properties and
# SciTEDirectory.properties.
command.subsystem.1.*.erl=0
command.name.1.*.erl=svn update
command.1.*.erl= svn update $(FileNameExt)
command.name.2.*.erl= svn ci
command.2.*.erl= svn ci -m '$(4)' $(FileNameExt)
command.name.3.*.erl=svn co
command.3.*.erl= svn co $(4)
command.name.4.*.erl=svn status
command.4.*.erl= svn status
command.name.5.*.erl=erl evaluate
command.5.*.erl= erl -eval '$(CurrentSelection)' -noshell -s init stop
command.subsystem.6.*.erl=0
command.name.6.*.erl=copy to neo1973
command.6.*.erl= scp $(FileName).beam   phone:/root/erlang/trunk/
command.subsystem.7.*.erl=0
command.name.7.*.erl=add to subversion
command.7.*.erl= svn add $(FileNameExt)
command.subsystem.8.*.erl=0
command.name.8.*.erl=stop X
command.8.*.erl= rsh root@192.168.0.202 /etc/init.d/zhone-session stop
command.subsystem.9.*.erl=0
command.name.9.*.erl=start X
command.9.*.erl= rsh root@192.168.0.202 /etc/init.d/zhone-session start

I am developing a vertical market product based on Openmoko, so I want my erlang platform alone to run (no window manager or Zhone) when the phone is switched on. This is my /usr/bin/zhone-session. I keep my erlang beam files at /root/erlang/trunk

#zhone &
#matchbox-keyboard-toggle &
erl -noshell -pa /root/erlang/trunk -s main start > debug.txt
#exec matchbox-window-manager -use_titlebar no

All I have to do now is to re-start X whenever I want to run an uploaded app. This is what the last two menu buttons do in SciTe - stop X on the phone, and start X on the phone.

Type

ssh phone

to get a terminal window and type

tail -f /debug.txt

to read shell printout from the erlang process.

Example Code

Use mdbus in erlang

Dbus = open_port({spawn,"mdbus -s"}, [stream, use_stdio, binary]),
loop(Dbus).

And get the response

loop(Dbus) ->
receive
{Dbus,Msg} -> io:format("Dbus msg:~p~n",[Msg]),loop(Dbus)
end.