Debian Codegraph @

gcc/ada/libgnat/g-sercom__linux.adb

raw
1 ------------------------------------------------------------------------------
2 -- --
3 -- GNAT COMPILER COMPONENTS --
4 -- --
5 -- G N A T . S E R I A L _ C O M M U N I C A T I O N S --
6 -- --
7 -- B o d y --
8 -- --
9 -- Copyright (C) 2007-2025, AdaCore --
10 -- --
11 -- GNAT is free software; you can redistribute it and/or modify it under --
12 -- terms of the GNU General Public License as published by the Free Soft- --
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
31
32 -- This is the GNU/Linux implementation of this package
33 --
34 -- Testing on GNU/Linux can be done with socat & stty tools.
35 --
36 -- First in a terminal create a virtual serial port:
37 --
38 -- * First solution, the terminal is one of the side of the channel
39 -- characters written with Write into the port will be displayed
40 -- there and characters typed into the terminal will be send to the
41 -- channel and will be received by a Read call.
42 --
43 -- $ socat PTY,link=/tmp/virtual-tty,raw,echo=1 -
44 --
45 -- * Second solution, the virtual channel contains two side and the
46 -- program can Read and Write date to it.
47 --
48 -- $ socat PTY,link=/tmp/virtual-tty,raw,echo=1 \
49 -- PTY,link=/tmp/virtual-tty,raw,echo=1
50 --
51 -- Connect to this virtual serial port with:
52 --
53 -- Open (Port => P, Name => "/tmp/virtual-tty");
54 --
55 -- Do any settings using the Set routine below, then you can check
56 -- the serial port configuration with:
57 --
58 -- $ stty --file /tmp/virtual-tty
59 --
60
61 with Ada.Streams; use Ada.Streams;
62
63 with System; use System;
64 with System.Communication; use System.Communication;
65 with System.CRTL; use System.CRTL;
66
67 with GNAT.OS_Lib; use GNAT.OS_Lib;
68
69 package body GNAT.Serial_Communications is
70
71 package OSC renames System.OS_Constants;
72
73 use type Interfaces.C.unsigned;
74
75 subtype unsigned is Interfaces.C.unsigned;
76 subtype char is Interfaces.C.char;
77 subtype unsigned_char is Interfaces.C.unsigned_char;
78
79 function fcntl (fd : int; cmd : int; value : int) return int;
80 pragma Import (C, fcntl, "fcntl");
81
82 C_Data_Rate : constant array (Data_Rate) of unsigned :=
83 [B75 => OSC.B75,
84 B110 => OSC.B110,
85 B150 => OSC.B150,
86 B300 => OSC.B300,
87 B600 => OSC.B600,
88 B1200 => OSC.B1200,
89 B2400 => OSC.B2400,
90 B4800 => OSC.B4800,
91 B9600 => OSC.B9600,
92 B19200 => OSC.B19200,
93 B38400 => OSC.B38400,
94 B57600 => OSC.B57600,
95 B115200 => OSC.B115200,
96 B230400 => OSC.B230400,
97 B460800 => OSC.B460800,
98 B500000 => OSC.B500000,
99 B576000 => OSC.B576000,
100 B921600 => OSC.B921600,
101 B1000000 => OSC.B1000000,
102 B1152000 => OSC.B1152000,
103 B1500000 => OSC.B1500000,
104 B2000000 => OSC.B2000000,
105 B2500000 => OSC.B2500000,
106 B3000000 => OSC.B3000000,
107 B3500000 => OSC.B3500000,
108 B4000000 => OSC.B4000000];
109
110 C_Bits : constant array (Data_Bits) of unsigned :=
111 [CS7 => OSC.CS7, CS8 => OSC.CS8];
112
113 C_Stop_Bits : constant array (Stop_Bits_Number) of unsigned :=
114 [One => 0, Two => OSC.CSTOPB];
115
116 C_Parity : constant array (Parity_Check) of unsigned :=
117 [None => 0,
118 Odd => OSC.PARENB or OSC.PARODD,
119 Even => OSC.PARENB];
120
121 procedure Raise_Error (Message : String; Error : Integer := Errno);
122 pragma No_Return (Raise_Error);
123
124 ----------
125 -- Name --
126 ----------
127
128 function Name (Number : Positive) return Port_Name is
129 N : constant Natural := Number - 1;
130 N_Img : constant String := Natural'Image (N);
131 begin
132 return Port_Name ("/dev/ttyS" & N_Img (N_Img'First + 1 .. N_Img'Last));
133 end Name;
134
135 ----------
136 -- Open --
137 ----------
138
139 procedure Open
140 (Port : out Serial_Port;
141 Name : Port_Name)
142 is
143 use OSC;
144
145 C_Name : constant String := String (Name) & ASCII.NUL;
146 Res : int;
147
148 begin
149 Port.H := Serial_Port_Descriptor (open
150 (C_Name (C_Name'First)'Address, int (O_RDWR + O_NOCTTY + O_NDELAY)));
151
152 if Port.H = -1 then
153 Raise_Error ("open: open failed");
154 end if;
155
156 -- By default we are in blocking mode
157
158 Res := fcntl (int (Port.H), F_SETFL, 0);
159
160 if Res = -1 then
161 Raise_Error ("open: fcntl failed");
162 end if;
163 end Open;
164
165 -----------------
166 -- Raise_Error --
167 -----------------
168
169 procedure Raise_Error (Message : String; Error : Integer := Errno) is
170 begin
171 raise Serial_Error with Message
172 & (if Error /= 0
173 then " (" & Errno_Message (Err => Error) & ')'
174 else "");
175 end Raise_Error;
176
177 ----------
178 -- Read --
179 ----------
180
181 overriding procedure Read
182 (Port : in out Serial_Port;
183 Buffer : out Stream_Element_Array;
184 Last : out Stream_Element_Offset)
185 is
186 Len : constant size_t := Buffer'Length;
187 Res : ssize_t;
188
189 begin
190 if Port.H = -1 then
191 Raise_Error ("read: port not opened", 0);
192 end if;
193
194 Res := read (Integer (Port.H), Buffer'Address, Len);
195
196 if Res = -1 then
197 Raise_Error ("read failed");
198 end if;
199
200 Last := Last_Index (Buffer'First, size_t (Res));
201 end Read;
202
203 ---------
204 -- Set --
205 ---------
206
207 procedure Set
208 (Port : Serial_Port;
209 Rate : Data_Rate := B9600;
210 Bits : Data_Bits := CS8;
211 Stop_Bits : Stop_Bits_Number := One;
212 Parity : Parity_Check := None;
213 Block : Boolean := True;
214 Local : Boolean := True;
215 Flow : Flow_Control := None;
216 Timeout : Duration := 10.0)
217 is
218 use OSC;
219
220 subtype speed_t is unsigned;
221
222 type termios is record
223 c_iflag : unsigned;
224 c_oflag : unsigned;
225 c_cflag : unsigned;
226 c_lflag : unsigned;
227 c_line : unsigned_char;
228 c_cc : Interfaces.C.char_array (0 .. 31);
229 c_ispeed : speed_t;
230 c_ospeed : speed_t;
231 end record;
232 pragma Convention (C, termios);
233
234 function tcgetattr (fd : int; termios_p : Address) return int;
235 pragma Import (C, tcgetattr, "tcgetattr");
236
237 function tcsetattr
238 (fd : int; action : int; termios_p : Address) return int;
239 pragma Import (C, tcsetattr, "tcsetattr");
240
241 function tcflush (fd : int; queue_selector : int) return int;
242 pragma Import (C, tcflush, "tcflush");
243
244 function cfsetospeed (termios_p : Address; speed : speed_t) return int;
245 pragma Import (C, cfsetospeed, "cfsetospeed");
246
247 function cfsetispeed (termios_p : Address; speed : speed_t) return int;
248 pragma Import (C, cfsetispeed, "cfsetispeed");
249
250 Current : termios;
251
252 Res : int := 0;
253 pragma Warnings (Off, Res);
254 -- Warnings off, since we don't always test the result
255
256 begin
257 if Port.H = -1 then
258 Raise_Error ("set: port not opened", 0);
259 end if;
260
261 -- Get current port settings
262
263 Res := tcgetattr (int (Port.H), Current'Address);
264
265 -- Change settings now
266
267 Current.c_cflag := C_Bits (Bits)
268 or C_Stop_Bits (Stop_Bits)
269 or C_Parity (Parity)
270 or CREAD;
271
272 Current.c_iflag := 0;
273 Current.c_lflag := 0;
274 Current.c_oflag := 0;
275
276 if Local then
277 Current.c_cflag := Current.c_cflag or CLOCAL;
278 end if;
279
280 case Flow is
281 when None =>
282 null;
283
284 when RTS_CTS =>
285 Current.c_cflag := Current.c_cflag or CRTSCTS;
286
287 when Xon_Xoff =>
288 Current.c_iflag := Current.c_iflag or IXON;
289 end case;
290
291 Current.c_ispeed := Data_Rate_Value (Rate);
292 Current.c_ospeed := Data_Rate_Value (Rate);
293
294 -- See man termios for descriptions about the different modes
295
296 if Block and then Timeout = 0.0 then
297 -- MIN > 0, TIME == 0 (blocking read)
298 Current.c_cc (VMIN) := char'Val (1);
299 Current.c_cc (VTIME) := char'Val (0);
300
301 else
302 -- MIN == 0, TIME > 0 (read with timeout)
303 -- MIN == 0, TIME == 0 (polling read)
304 Current.c_cc (VMIN) := char'Val (0);
305 Current.c_cc (VTIME) := char'Val (Natural (Timeout * 10));
306
307 Current.c_lflag := Current.c_lflag or not ICANON;
308 end if;
309
310 Res := cfsetispeed (Current'Address, C_Data_Rate (Rate));
311
312 if Res = -1 then
313 Raise_Error ("set: cfsetispeed failed");
314 end if;
315
316 Res := cfsetospeed (Current'Address, C_Data_Rate (Rate));
317
318 if Res = -1 then
319 Raise_Error ("set: cfsetospeed failed");
320 end if;
321
322 -- Set port settings
323
324 Res := tcflush (int (Port.H), TCIFLUSH);
325 Res := tcsetattr (int (Port.H), TCSANOW, Current'Address);
326
327 -- Block
328
329 if Block then
330 -- In blocking mode, remove the non-blocking flags set while
331 -- opening the serial port (see Open).
332 Res := fcntl (int (Port.H), F_SETFL, 0);
333 end if;
334
335 if Res = -1 then
336 Raise_Error ("set: fcntl failed");
337 end if;
338 end Set;
339
340 ------------
341 -- To_Ada --
342 ------------
343
344 procedure To_Ada (Port : out Serial_Port; Fd : Serial_Port_Descriptor) is
345 begin
346 Port.H := Fd;
347 end To_Ada;
348
349 -----------
350 -- Write --
351 -----------
352
353 overriding procedure Write
354 (Port : in out Serial_Port;
355 Buffer : Stream_Element_Array)
356 is
357 Len : constant size_t := Buffer'Length;
358 Res : ssize_t;
359
360 begin
361 if Port.H = -1 then
362 Raise_Error ("write: port not opened", 0);
363 end if;
364
365 Res := write (int (Port.H), Buffer'Address, Len);
366
367 if Res = -1 then
368 Raise_Error ("write failed");
369 end if;
370
371 pragma Assert (size_t (Res) = Len);
372 end Write;
373
374 -----------
375 -- Close --
376 -----------
377
378 procedure Close (Port : in out Serial_Port) is
379 Res : int;
380 pragma Unreferenced (Res);
381
382 begin
383 if Port.H /= -1 then
384 Res := close (int (Port.H));
385 Port.H := -1;
386 end if;
387 end Close;
388
389 end GNAT.Serial_Communications;