1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
7
<year>2000</year><year>2009</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
11
The contents of this file are subject to the Erlang Public License,
12
Version 1.1, (the "License"); you may not use this file except in
13
compliance with the License. You should have received a copy of the
14
Erlang Public License along with this software. If not, it can be
15
retrieved online at http://www.erlang.org/.
17
Software distributed under the License is distributed on an "AS IS"
18
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
19
the License for the specific language governing rights and limitations
24
<title>Port drivers</title>
29
<file>c_portdriver.xml</file>
31
<p>This is an example of how to solve the <seealso marker="example">example problem</seealso> by using a linked in port driver.</p>
32
<image file="../tutorial/port_driver.gif">
33
<icaption>Port Driver Communication.</icaption>
37
<title>Port Drivers</title>
38
<p>A port driver is a linked in driver, that is accessible as a
39
port from an Erlang program. It is a shared library (SO in Unix,
40
DLL in Windows), with special entry points. The Erlang runtime
41
calls these entry points, when the driver is started and when
42
data is sent to the port. The port driver can also send data to
44
<p>Since a port driver is dynamically linked into the emulator
45
process, this is the fastest way of calling C-code from Erlang.
46
Calling functions in the port driver requires no context
47
switches. But it is also the least safe, because a crash in the
48
port driver brings the emulator down too.</p>
52
<title>Erlang Program</title>
53
<p>Just as with a port program, the port communicates with a Erlang
54
process. All communication goes through one Erlang process that
55
is the <em>connected process</em> of the port
56
driver. Terminating this process closes the port driver.</p>
57
<p>Before the port is created, the driver must be loaded. This is
58
done with the function <c>erl_dll:load_driver/1</c>, with the
59
name of the shared library as argument.</p>
60
<p>The port is then created using the BIF <c>open_port/2</c> with
61
the tuple <c>{spawn, DriverName}</c> as the first argument. The
62
string <c>SharedLib</c> is the name of the port driver. The second
63
argument is a list of options, none in this case.</p>
66
-export([start/1, init/1]).
69
case erl_ddll:load_driver(".", SharedLib) of
71
\011{error, already_loaded} -> ok;
72
\011_ -> exit({error, could_not_load_driver})
74
spawn(?MODULE, init, [SharedLib]).
77
register(complex, self()),
78
Port = open_port({spawn, SharedLib}, []),
80
<p>Now it is possible to implement <c>complex5:foo/1</c> and
81
<c>complex5:bar/1</c>. They both send a message to the
82
<c>complex</c> process and receive the reply.</p>
90
complex ! {call, self(), Msg},
95
<p>The <c>complex</c> process encodes the message into a sequence
96
of bytes, sends it to the port, waits for a reply, decodes the
97
reply and sends it back to the caller.
102
{call, Caller, Msg} ->
103
Port ! {self(), {command, encode(Msg)}},
105
\011 {Port, {data, Data}} ->
106
Caller ! {complex, decode(Data)}
110
<p>Assuming that both the arguments and the results from the C
111
functions will be less than 256, a very simple encoding/decoding
112
scheme is employed where <c>foo</c> is represented by the byte
113
1, <c>bar</c> is represented by 2, and the argument/result is
114
represented by a single byte as well.
117
encode({foo, X}) -> [1, X];
118
encode({bar, Y}) -> [2, Y].
120
decode([Int]) -> Int.</pre>
121
<p>The resulting Erlang program, including functionality for
122
stopping the port and detecting port failures is shown below.</p>
123
<codeinclude file="complex5.erl" type="erl"/>
127
<title>C Driver</title>
128
<p>The C driver is a module that is compiled and linked into a
129
shared library. It uses a driver structure, and includes the
130
header file <c>erl_driver.h</c>.</p>
131
<p>The driver structure is filled with the driver name and function
132
pointers. It is returned from the special entry point, declared
133
with the macro <c><![CDATA[DRIVER_INIT(<driver_name>)]]></c>.</p>
134
<p>The functions for receiving and sending data, are combined into
135
a function, pointed out by the driver structure. The data sent
136
into the port is given as arguments, and the data the port
137
sends back is sent with the C-function <c>driver_output</c>.</p>
138
<p>Since the driver is a shared module, not a program, no main
139
function should be present. All function pointers are not used
140
in our example, and the corresponding fields in the
141
<c>driver_entry</c> structure are set to NULL.</p>
142
<p>All functions in the driver, takes a handle (returned from
143
<c>start</c>), that is just passed along by the erlang
144
process. This must in some way refer to the port driver
146
<p>The example_drv_start, is the only function that is called with
147
a handle to the port instance, so we must save this. It is
148
customary to use a allocated driver-defined structure for this
149
one, and pass a pointer back as a reference.</p>
150
<p>It is not a good idea to use a global variable; since the port
151
driver can be spawned by multiple Erlang processes, this
152
driver-structure should be instantiated multiple times.
154
<codeinclude file="port_driver.c" tag="" type="none"></codeinclude>
158
<title>Running the Example</title>
159
<p>1. Compile the C code.</p>
161
unix> <input>gcc -o exampledrv -fpic -shared complex.c port_driver.c</input>
162
windows> <input>cl -LD -MD -Fe exampledrv.dll complex.c port_driver.c</input></pre>
163
<p>2. Start Erlang and compile the Erlang code.</p>
166
Erlang (BEAM) emulator version 5.1
168
Eshell V5.1 (abort with ^G)
169
1> <input>c(complex5).</input>
171
<p>3. Run the example.</p>
173
2> <input>complex5:start("example_drv").</input>
175
3> <input>complex5:foo(3).</input>
177
4> <input>complex5:bar(5).</input>
179
5> <input>complex5:stop().</input>