Subversion Repositories shark

Rev

Rev 1676 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1676 tullio 1
%----------------------------------------------------------------------------
2
\section{Communication Ports \footnote{The S.Ha.R.K. communication ports are
3
directly derived from the previous varsions of the Hartik Kernel.}}
4
%----------------------------------------------------------------------------
5
 
6
S.Ha.R.K. communication ports allow tasks to exchange messages. Each
7
port is uniquely identified by a symbolic name (i.e., a string of
8
characters); a task willing to use this communication facility has
9
to open the channel using the \texttt{port\_create()} call, thus becoming
10
the owner of the resource. Any other task that wants to use this communication
11
end-point to send or receive data needs to connect to it by using
12
the \texttt{port\_connect()} primitive.
13
 
14
S.Ha.R.K. offers three types of ports:
15
 
16
\begin{itemize}
17
 
18
\item \texttt{STREAM}: it is a one-to-one communication facility, which
19
can be opened either by the reader or by the writer task. The task
20
executing the \texttt{port\_create()} must specify the message size
21
and maximum number of messages in the queue. The task executing the
22
 
23
\texttt{port\_connect()} must only specify the size of the messages
24
it wants to receive/send, which can be different from the one specified
25
by the owner. For example, a task may open a port for reading messages
26
of 4 bytes, while another task can connect to it to write one-byte
27
messages. This mechanism turns out to be useful for character oriented
28
device drivers which need to fill a given structure, before the message
29
can be processed further by a higher-level task.
30
 
31
\item \texttt{MAILBOX}: it is a many-to-one communication facility, thought
32
for being used in classical client/server mechanisms. This kind of
33
port can only be opened by the reader task (the server) which wants
34
to receive data from writer tasks (the clients). Message size is fixed
35
and defined by the reader.
36
 
37
\item \texttt{STICK}: it is a one-to-many communication facility intended
38
to be used for exchanging periodic state-messages, for which the most
39
recent information is relevant. It can be opened only by the (unique)
40
writer task and the reading tasks must connect to it. It contains
41
just one message and any new message posted by the writer will overwrite
42
the previous one. Messages are non-consumable: a reader task can perform
43
many readings of a given message until the writer posts a new one.
44
\end{itemize}
45
 
46
The first two kinds of port implement the synchronous communication
47
paradigm, while \texttt{STICK} ports implement an asynchronous (state-message)
48
paradigm. It is worth noting that in order to protect the internal
49
data structures, \texttt{STREAM} ports use semaphores for synchronizing
50
the accesses, \texttt{STICK} ports just use a mutual exclusion semaphore,
51
and the \texttt{MAILBOX} ports use both kinds of semaphores.
52
 
53
For this reason, \texttt{MAILBOX} and \texttt{STICK} ports should
54
not be used by critical tasks, whereas \texttt{STREAM} ports can be
55
used by any task requiring a state-message non-blocking semantics.
56
Moreover, the execution time of a transaction depends on the message
57
size (the message is copied in/from the buffer when a send/receive
58
is performed). The semantics associated with each port is graphically
59
illustrated in Figure \ref{fg:port-type}.
60
 
61
An application that uses the communication ports, must register the
62
HARTPORT Module. Please see Volume III - S.Ha.R.K. Modules for details.
63
 
64
\begin{figure}
65
\begin{center}\includegraphics[width=8cm]{port.eps}\end{center}
66
\caption{HARTIK ports.\label{fg:port-type}}
67
\end{figure}
68
 
69
%----------------------------------------------------------------------------
70
\begin{intest}
71
PORT\_CREATE\index{port\_create()}
72
\end{intest}
73
 
74
\begin{description}
75
 
76
\item [\textbf{PORT port\_create(char {*}name, int dim, int num,
77
int type, int mode);}]
78
 
79
\item [\textbf{Description:}]It opens the port identified by the string
80
\texttt{name}.
81
The argument \texttt{dim} specifies the message size in bytes, \texttt{num}
82
specifies the queue size, \texttt{type} the port type (\texttt{STREAM},
83
\texttt{MAILBOX}, or \texttt{STICK}), and \texttt{mode} the access
84
mode (\texttt{READ} or \texttt{WRITE}).
85
 
86
\item [\textbf{Return Value:}] The primitive returns the port identifier,
87
which identifies the connection between the port and the task, and
88
not the port itself, which is identified through its name. A return
89
value -1 indicates that an error is occurred.
90
 
91
\item [\textbf{See also}:] \texttt{port\_delete(), port\_connect(),
92
port\_disconnect(), port\_send(), port\_receive()}.
93
 
94
\end{description}
95
 
96
\begin{description}
97
\item [Example:\label{pg:port-ex}]
98
\item \texttt{TASK demo(void)} \{
99
\item \texttt{~~PORT p; }
100
\item \texttt{~~char msg{[}6{]}; }
101
\item \texttt{~~\ldots{}}
102
\item \texttt{~~/{*} Demo task, of NRT type, opens the \char`\"{}goofy\char`\"{}
103
port {*}/}
104
\item \texttt{~~/{*} and sends a message of 6 bytes. {*}/}
105
\item \texttt{~~p = port\_create(\char`\"{}goofy\char`\"{}, 6, 8, STREAM,
106
WRITE);}
107
\item \texttt{~~\ldots{}}
108
\item \texttt{~~port\_send(p, msg, BLOCK); }
109
\item \texttt{\}}
110
\item \texttt{~}
111
\item \texttt{TASK duro(void)} \{
112
\item \texttt{~~PORT q; }
113
\item \texttt{~~char msg{[}2{]}; }
114
\item \texttt{~~/{*} Duro task (HARD) connects to the \char`\"{}goofy\char`\"{}
115
{*}/}
116
\item \texttt{~~/{*} port and receives messages of 2 bytes {*}/ }
117
\item \texttt{~~q = port\_connect(\char`\"{}goofy\char`\"{}, 2, STREAM,
118
READ);}
119
\item \texttt{~~while (condition) \{}
120
\item \texttt{~~~~\ldots{}}
121
\item \texttt{~~~~if (port\_receive(q, msg, NON\textbackslash{}\_BLOCK) \{}
122
\item \texttt{~~~~~~<action 1>;~/{*} Ready Message! {*}/}
123
\item \texttt{~~~~\}}
124
\item \texttt{~~~~else \{}
125
\item \texttt{~~~~~~<action 2>;~/{*} Message not Ready! {*}/}
126
\item \texttt{~~~~\}}
127
\item \texttt{~~~~\ldots{}}
128
\item \texttt{~~~~task\_endcycle();}
129
\item \texttt{~~\}}
130
\item \texttt{\}}
131
 
132
\end{description}
133
 
134
%----------------------------------------------------------------------------
135
\begin{intest}
136
PORT\_DELETE\index{port\_delete()}
137
\end{intest}
138
 
139
\begin{description}
140
 
141
\item [\textbf{void port\_delete(PORT p)};]
142
 
143
\item [\textbf{Description:}]It destroys the port identified by \texttt{p}.
144
 
145
\item [\textbf{See also}:] \texttt{port\_create(), port\_connect(),
146
port\_disconnect(),
147
port\_send(), port\_receive()}.
148
 
149
\item [\textbf{Example:}]see the example at page \pageref{pg:port-ex}.
150
 
151
\end{description}
152
 
153
%----------------------------------------------------------------------------
154
\begin{intest}
155
PORT\_CONNECT\index{port\_connect()}
156
\end{intest}
157
 
158
\begin{description}
159
 
160
\item [\textbf{PORT port\_connect(char {*}name, int dim, int type,
161
int mode);}]
162
 
163
\item [\textbf{Description:}]It connects the calling task to the port identified
164
by \texttt{name}. The argument \texttt{dim} specifies the message
165
size in bytes, \texttt{type} the port type (\texttt{STREAM}, \texttt{MAILBOX},
166
or \texttt{STICK}), and \texttt{mode} the access mode (\texttt{READ}
167
or \texttt{WRITE}). If the port has not been opened by \texttt{port\_create()},
168
the task is blocked, waiting for port creation. To avoid synchronization
169
delays, connection should be established only \underbar{after} opening
170
the port.
171
 
172
\item [\textbf{Return value:}] The function returns the port identification
173
number in the case of successful operation; else -1 is returned.
174
 
175
\item [\textbf{See also}:] \texttt{port\_create(), port\_delete(),
176
port\_disconnect(), port\_send(), port\_receive()}.
177
 
178
\end{description}
179
 
180
%----------------------------------------------------------------------------
181
\begin{intest}
182
PORT\_DISCONNECT\index{port\_disconnect()}
183
\end{intest}
184
 
185
\begin{description}
186
 
187
\item [\textbf{void port\_disconnect(PORT p)};]
188
 
189
\item [\textbf{Description:}]It closes the connection identified by \texttt{p}.
190
 
191
\item [\textbf{See also}:] \texttt{port\_create(), port\_connect(),
192
port\_delete(), port\_send(), port\_receive()}.
193
 
194
\end{description}
195
 
196
%----------------------------------------------------------------------------
197
\begin{intest}
198
PORT\_SEND\index{port\_send()}
199
\end{intest}
200
 
201
\begin{description}
202
 
203
\item [\textbf{int port\_send(PORT p, char {*}msg, BYTE b);}]
204
 
205
\item [\textbf{Description:}]It sends a message pointed by \texttt{msg} to the
206
port identified by \texttt{p}. Message dimension is defined through
207
\texttt{port\_create()}
208
or \texttt{port\_connect()} and cannot be dynamically changed. The
209
argument \texttt{b} can be \texttt{BLOCK} or \texttt{NON\_BLOCK}.
210
If \texttt{b = BLOCK} and the port queue is full, then the task is
211
blocked until the buffer is freed. If \texttt{b = NON\_BLOCK} and
212
the port queue is full, then the primitive returns 0 and the message
213
is not sent.
214
 
215
\item [\textbf{Return value:}] 1 (TRUE) if the operation can be performed,
216
 
217
 
218
\item [\textbf{See also}:] \texttt{port\_create(), port\_connect(),
219
port\_disconnect(),
220
port\_send(), port\_receive()}.
221
 
222
\item [\textbf{Example:}]see the example at page \pageref{pg:port-ex}.
223
 
224
\end{description}
225
 
226
%----------------------------------------------------------------------------
227
\begin{intest}
228
PORT\_RECEIVE\index{port\_receive()}
229
\end{intest}
230
 
231
\begin{description}
232
 
233
\item [\textbf{int port\_receive(PORT p, char {*}msg, BYTE b);}]
234
 
235
\item [\textbf{Description:}]It receives a message from the port identified by
236
\texttt{p} and copies it in a memory buffer pointed by \texttt{msg}. Message
237
dimension is defined through \texttt{port\_create()} or \texttt{port\_connect()}
238
and cannot be dynamically changed. The argument \texttt{b} can be
239
\texttt{BLOCK} or \texttt{NON\_BLOCK}. If \texttt{b = BLOCK} and the
240
port queue is empty, then the task is blocked until a message is available.
241
If \texttt{b = NON\_BLOCK} and the port queue is empty, then the primitive
242
returns 0 and no message is received.
243
 
244
\item [\textbf{Return value:}] 1 (TRUE) if the operation can be performed,
245
 
246
 
247
\item [\textbf{See also}:] \texttt{port\_create(), port\_connect(),
248
port\_disconnect(), port\_send(), port\_receive()}.
249
 
250
\item [\textbf{Example:}]see the example at page \pageref{pg:port-ex}.
251
 
252
\end{description}