111
|
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 -- --
|
145
|
9 -- Copyright (C) 2007-2019, AdaCore --
|
111
|
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 with Ada.Streams; use Ada.Streams;
|
|
35 with Ada; use Ada;
|
|
36
|
|
37 with System; use System;
|
|
38 with System.Communication; use System.Communication;
|
|
39 with System.CRTL; use System.CRTL;
|
|
40
|
|
41 with GNAT.OS_Lib; use GNAT.OS_Lib;
|
|
42
|
|
43 package body GNAT.Serial_Communications is
|
|
44
|
|
45 package OSC renames System.OS_Constants;
|
|
46
|
|
47 use type Interfaces.C.unsigned;
|
|
48
|
|
49 subtype unsigned is Interfaces.C.unsigned;
|
|
50 subtype char is Interfaces.C.char;
|
|
51 subtype unsigned_char is Interfaces.C.unsigned_char;
|
|
52
|
|
53 function fcntl (fd : int; cmd : int; value : int) return int;
|
|
54 pragma Import (C, fcntl, "fcntl");
|
|
55
|
|
56 C_Data_Rate : constant array (Data_Rate) of unsigned :=
|
145
|
57 (B75 => OSC.B75,
|
|
58 B110 => OSC.B110,
|
|
59 B150 => OSC.B150,
|
|
60 B300 => OSC.B300,
|
|
61 B600 => OSC.B600,
|
|
62 B1200 => OSC.B1200,
|
|
63 B2400 => OSC.B2400,
|
|
64 B4800 => OSC.B4800,
|
|
65 B9600 => OSC.B9600,
|
|
66 B19200 => OSC.B19200,
|
|
67 B38400 => OSC.B38400,
|
|
68 B57600 => OSC.B57600,
|
|
69 B115200 => OSC.B115200,
|
|
70 B230400 => OSC.B230400,
|
|
71 B460800 => OSC.B460800,
|
|
72 B500000 => OSC.B500000,
|
|
73 B576000 => OSC.B576000,
|
|
74 B921600 => OSC.B921600,
|
|
75 B1000000 => OSC.B1000000,
|
|
76 B1152000 => OSC.B1152000,
|
|
77 B1500000 => OSC.B1500000,
|
|
78 B2000000 => OSC.B2000000,
|
|
79 B2500000 => OSC.B2500000,
|
|
80 B3000000 => OSC.B3000000,
|
|
81 B3500000 => OSC.B3500000,
|
|
82 B4000000 => OSC.B4000000);
|
111
|
83
|
|
84 C_Bits : constant array (Data_Bits) of unsigned :=
|
|
85 (CS7 => OSC.CS7, CS8 => OSC.CS8);
|
|
86
|
|
87 C_Stop_Bits : constant array (Stop_Bits_Number) of unsigned :=
|
|
88 (One => 0, Two => OSC.CSTOPB);
|
|
89
|
|
90 C_Parity : constant array (Parity_Check) of unsigned :=
|
|
91 (None => 0,
|
|
92 Odd => OSC.PARENB or OSC.PARODD,
|
|
93 Even => OSC.PARENB);
|
|
94
|
|
95 procedure Raise_Error (Message : String; Error : Integer := Errno);
|
|
96 pragma No_Return (Raise_Error);
|
|
97
|
|
98 ----------
|
|
99 -- Name --
|
|
100 ----------
|
|
101
|
|
102 function Name (Number : Positive) return Port_Name is
|
|
103 N : constant Natural := Number - 1;
|
|
104 N_Img : constant String := Natural'Image (N);
|
|
105 begin
|
|
106 return Port_Name ("/dev/ttyS" & N_Img (N_Img'First + 1 .. N_Img'Last));
|
|
107 end Name;
|
|
108
|
|
109 ----------
|
|
110 -- Open --
|
|
111 ----------
|
|
112
|
|
113 procedure Open
|
|
114 (Port : out Serial_Port;
|
|
115 Name : Port_Name)
|
|
116 is
|
|
117 use OSC;
|
|
118
|
|
119 C_Name : constant String := String (Name) & ASCII.NUL;
|
|
120 Res : int;
|
|
121
|
|
122 begin
|
145
|
123 Port.H := Serial_Port_Descriptor (open
|
111
|
124 (C_Name (C_Name'First)'Address, int (O_RDWR + O_NOCTTY + O_NDELAY)));
|
|
125
|
145
|
126 if Port.H = -1 then
|
111
|
127 Raise_Error ("open: open failed");
|
|
128 end if;
|
|
129
|
|
130 -- By default we are in blocking mode
|
|
131
|
145
|
132 Res := fcntl (int (Port.H), F_SETFL, 0);
|
111
|
133
|
|
134 if Res = -1 then
|
|
135 Raise_Error ("open: fcntl failed");
|
|
136 end if;
|
|
137 end Open;
|
|
138
|
|
139 -----------------
|
|
140 -- Raise_Error --
|
|
141 -----------------
|
|
142
|
|
143 procedure Raise_Error (Message : String; Error : Integer := Errno) is
|
|
144 begin
|
|
145 raise Serial_Error with Message
|
|
146 & (if Error /= 0
|
|
147 then " (" & Errno_Message (Err => Error) & ')'
|
|
148 else "");
|
|
149 end Raise_Error;
|
|
150
|
|
151 ----------
|
|
152 -- Read --
|
|
153 ----------
|
|
154
|
|
155 overriding procedure Read
|
|
156 (Port : in out Serial_Port;
|
|
157 Buffer : out Stream_Element_Array;
|
|
158 Last : out Stream_Element_Offset)
|
|
159 is
|
|
160 Len : constant size_t := Buffer'Length;
|
|
161 Res : ssize_t;
|
|
162
|
|
163 begin
|
145
|
164 if Port.H = -1 then
|
111
|
165 Raise_Error ("read: port not opened", 0);
|
|
166 end if;
|
|
167
|
145
|
168 Res := read (Integer (Port.H), Buffer'Address, Len);
|
111
|
169
|
|
170 if Res = -1 then
|
|
171 Raise_Error ("read failed");
|
|
172 end if;
|
|
173
|
|
174 Last := Last_Index (Buffer'First, size_t (Res));
|
|
175 end Read;
|
|
176
|
|
177 ---------
|
|
178 -- Set --
|
|
179 ---------
|
|
180
|
|
181 procedure Set
|
|
182 (Port : Serial_Port;
|
|
183 Rate : Data_Rate := B9600;
|
|
184 Bits : Data_Bits := CS8;
|
|
185 Stop_Bits : Stop_Bits_Number := One;
|
|
186 Parity : Parity_Check := None;
|
|
187 Block : Boolean := True;
|
|
188 Local : Boolean := True;
|
|
189 Flow : Flow_Control := None;
|
|
190 Timeout : Duration := 10.0)
|
|
191 is
|
|
192 use OSC;
|
|
193
|
|
194 type termios is record
|
|
195 c_iflag : unsigned;
|
|
196 c_oflag : unsigned;
|
|
197 c_cflag : unsigned;
|
|
198 c_lflag : unsigned;
|
|
199 c_line : unsigned_char;
|
|
200 c_cc : Interfaces.C.char_array (0 .. 31);
|
|
201 c_ispeed : unsigned;
|
|
202 c_ospeed : unsigned;
|
|
203 end record;
|
|
204 pragma Convention (C, termios);
|
|
205
|
|
206 function tcgetattr (fd : int; termios_p : Address) return int;
|
|
207 pragma Import (C, tcgetattr, "tcgetattr");
|
|
208
|
|
209 function tcsetattr
|
|
210 (fd : int; action : int; termios_p : Address) return int;
|
|
211 pragma Import (C, tcsetattr, "tcsetattr");
|
|
212
|
|
213 function tcflush (fd : int; queue_selector : int) return int;
|
|
214 pragma Import (C, tcflush, "tcflush");
|
|
215
|
|
216 Current : termios;
|
|
217
|
|
218 Res : int;
|
|
219 pragma Warnings (Off, Res);
|
|
220 -- Warnings off, since we don't always test the result
|
|
221
|
|
222 begin
|
145
|
223 if Port.H = -1 then
|
111
|
224 Raise_Error ("set: port not opened", 0);
|
|
225 end if;
|
|
226
|
|
227 -- Get current port settings
|
|
228
|
145
|
229 Res := tcgetattr (int (Port.H), Current'Address);
|
111
|
230
|
|
231 -- Change settings now
|
|
232
|
|
233 Current.c_cflag := C_Data_Rate (Rate)
|
|
234 or C_Bits (Bits)
|
|
235 or C_Stop_Bits (Stop_Bits)
|
|
236 or C_Parity (Parity)
|
|
237 or CREAD;
|
|
238 Current.c_iflag := 0;
|
|
239 Current.c_lflag := 0;
|
|
240 Current.c_oflag := 0;
|
|
241
|
|
242 if Local then
|
|
243 Current.c_cflag := Current.c_cflag or CLOCAL;
|
|
244 end if;
|
|
245
|
|
246 case Flow is
|
|
247 when None =>
|
|
248 null;
|
|
249
|
|
250 when RTS_CTS =>
|
|
251 Current.c_cflag := Current.c_cflag or CRTSCTS;
|
|
252
|
|
253 when Xon_Xoff =>
|
|
254 Current.c_iflag := Current.c_iflag or IXON;
|
|
255 end case;
|
|
256
|
|
257 Current.c_ispeed := Data_Rate_Value (Rate);
|
|
258 Current.c_ospeed := Data_Rate_Value (Rate);
|
|
259 Current.c_cc (VMIN) := char'Val (0);
|
|
260 Current.c_cc (VTIME) := char'Val (Natural (Timeout * 10));
|
|
261
|
|
262 -- Set port settings
|
|
263
|
145
|
264 Res := tcflush (int (Port.H), TCIFLUSH);
|
|
265 Res := tcsetattr (int (Port.H), TCSANOW, Current'Address);
|
111
|
266
|
|
267 -- Block
|
|
268
|
145
|
269 Res := fcntl (int (Port.H), F_SETFL, (if Block then 0 else FNDELAY));
|
111
|
270
|
|
271 if Res = -1 then
|
|
272 Raise_Error ("set: fcntl failed");
|
|
273 end if;
|
|
274 end Set;
|
|
275
|
145
|
276 ------------
|
|
277 -- To_Ada --
|
|
278 ------------
|
|
279
|
|
280 procedure To_Ada (Port : out Serial_Port; Fd : Serial_Port_Descriptor) is
|
|
281 begin
|
|
282 Port.H := Fd;
|
|
283 end To_Ada;
|
|
284
|
111
|
285 -----------
|
|
286 -- Write --
|
|
287 -----------
|
|
288
|
|
289 overriding procedure Write
|
|
290 (Port : in out Serial_Port;
|
|
291 Buffer : Stream_Element_Array)
|
|
292 is
|
|
293 Len : constant size_t := Buffer'Length;
|
|
294 Res : ssize_t;
|
|
295
|
|
296 begin
|
145
|
297 if Port.H = -1 then
|
111
|
298 Raise_Error ("write: port not opened", 0);
|
|
299 end if;
|
|
300
|
145
|
301 Res := write (int (Port.H), Buffer'Address, Len);
|
111
|
302
|
|
303 if Res = -1 then
|
|
304 Raise_Error ("write failed");
|
|
305 end if;
|
|
306
|
|
307 pragma Assert (size_t (Res) = Len);
|
|
308 end Write;
|
|
309
|
|
310 -----------
|
|
311 -- Close --
|
|
312 -----------
|
|
313
|
|
314 procedure Close (Port : in out Serial_Port) is
|
|
315 Res : int;
|
|
316 pragma Unreferenced (Res);
|
|
317
|
|
318 begin
|
145
|
319 if Port.H /= -1 then
|
|
320 Res := close (int (Port.H));
|
111
|
321 end if;
|
|
322 end Close;
|
|
323
|
|
324 end GNAT.Serial_Communications;
|