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-2012, 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 Windows implementation of this package
33
34with Ada.Unchecked_Deallocation; use Ada;
35with Ada.Streams;                use Ada.Streams;
36
37with System;               use System;
38with System.Communication; use System.Communication;
39with System.CRTL;          use System.CRTL;
40with System.OS_Constants;
41with System.Win32;         use System.Win32;
42with System.Win32.Ext;     use System.Win32.Ext;
43
44package body GNAT.Serial_Communications is
45
46   package OSC renames System.OS_Constants;
47
48   --  Common types
49
50   type Port_Data is new HANDLE;
51
52   C_Bits      : constant array (Data_Bits) of Interfaces.C.unsigned := (8, 7);
53   C_Parity    : constant array (Parity_Check) of Interfaces.C.unsigned :=
54                   (None => NOPARITY, Odd => ODDPARITY, Even => EVENPARITY);
55   C_Stop_Bits : constant array (Stop_Bits_Number) of Interfaces.C.unsigned :=
56                   (One => ONESTOPBIT, Two => TWOSTOPBITS);
57
58   -----------
59   -- Files --
60   -----------
61
62   procedure Raise_Error (Message : String; Error : DWORD := GetLastError);
63   pragma No_Return (Raise_Error);
64
65   -----------
66   -- Close --
67   -----------
68
69   procedure Close (Port : in out Serial_Port) is
70      procedure Unchecked_Free is
71        new Unchecked_Deallocation (Port_Data, Port_Data_Access);
72
73      Success : BOOL;
74
75   begin
76      if Port.H /= null then
77         Success := CloseHandle (HANDLE (Port.H.all));
78         Unchecked_Free (Port.H);
79
80         if Success = Win32.FALSE then
81            Raise_Error ("error closing the port");
82         end if;
83      end if;
84   end Close;
85
86   ----------
87   -- Name --
88   ----------
89
90   function Name (Number : Positive) return Port_Name is
91      N_Img : constant String := Positive'Image (Number);
92   begin
93      return Port_Name ("COM" & N_Img (N_Img'First + 1 .. N_Img'Last) & ':');
94   end Name;
95
96   ----------
97   -- Open --
98   ----------
99
100   procedure Open
101     (Port : out Serial_Port;
102      Name : Port_Name)
103   is
104      C_Name  : constant String := String (Name) & ASCII.NUL;
105      Success : BOOL;
106      pragma Unreferenced (Success);
107
108   begin
109      if Port.H = null then
110         Port.H := new Port_Data;
111      else
112         Success := CloseHandle (HANDLE (Port.H.all));
113      end if;
114
115      Port.H.all := CreateFileA
116        (lpFileName            => C_Name (C_Name'First)'Address,
117         dwDesiredAccess       => GENERIC_READ or GENERIC_WRITE,
118         dwShareMode           => 0,
119         lpSecurityAttributes  => null,
120         dwCreationDisposition => OPEN_EXISTING,
121         dwFlagsAndAttributes  => 0,
122         hTemplateFile         => 0);
123
124      if Port.H.all = 0 then
125         Raise_Error ("cannot open com port");
126      end if;
127   end Open;
128
129   -----------------
130   -- Raise_Error --
131   -----------------
132
133   procedure Raise_Error (Message : String; Error : DWORD := GetLastError) is
134   begin
135      raise Serial_Error with Message & " (" & DWORD'Image (Error) & ')';
136   end Raise_Error;
137
138   ----------
139   -- Read --
140   ----------
141
142   overriding procedure Read
143     (Port   : in out Serial_Port;
144      Buffer : out Stream_Element_Array;
145      Last   : out Stream_Element_Offset)
146   is
147      Success   : BOOL;
148      Read_Last : aliased DWORD;
149
150   begin
151      if Port.H = null then
152         Raise_Error ("read: port not opened", 0);
153      end if;
154
155      Success :=
156        ReadFile
157          (hFile                => HANDLE (Port.H.all),
158           lpBuffer             => Buffer (Buffer'First)'Address,
159           nNumberOfBytesToRead => DWORD (Buffer'Length),
160           lpNumberOfBytesRead  => Read_Last'Access,
161           lpOverlapped         => null);
162
163      if Success = Win32.FALSE then
164         Raise_Error ("read error");
165      end if;
166
167      Last := Last_Index (Buffer'First, size_t (Read_Last));
168   end Read;
169
170   ---------
171   -- Set --
172   ---------
173
174   procedure Set
175     (Port      : Serial_Port;
176      Rate      : Data_Rate        := B9600;
177      Bits      : Data_Bits        := CS8;
178      Stop_Bits : Stop_Bits_Number := One;
179      Parity    : Parity_Check     := None;
180      Block     : Boolean          := True;
181      Local     : Boolean          := True;
182      Flow      : Flow_Control     := None;
183      Timeout   : Duration         := 10.0)
184   is
185      pragma Unreferenced (Local);
186
187      Success      : BOOL;
188      Com_Time_Out : aliased COMMTIMEOUTS;
189      Com_Settings : aliased DCB;
190
191   begin
192      if Port.H = null then
193         Raise_Error ("set: port not opened", 0);
194      end if;
195
196      Success := GetCommState (HANDLE (Port.H.all), Com_Settings'Access);
197
198      if Success = Win32.FALSE then
199         Success := CloseHandle (HANDLE (Port.H.all));
200         Port.H.all := 0;
201         Raise_Error ("set: cannot get comm state");
202      end if;
203
204      Com_Settings.BaudRate        := DWORD (Data_Rate_Value (Rate));
205      Com_Settings.fParity         := 1;
206      Com_Settings.fBinary         := Bits1 (System.Win32.TRUE);
207      Com_Settings.fOutxDsrFlow    := 0;
208      Com_Settings.fDsrSensitivity := 0;
209      Com_Settings.fDtrControl     := OSC.DTR_CONTROL_ENABLE;
210      Com_Settings.fInX            := 0;
211      Com_Settings.fRtsControl     := OSC.RTS_CONTROL_ENABLE;
212
213      case Flow is
214         when None =>
215            Com_Settings.fOutX        := 0;
216            Com_Settings.fOutxCtsFlow := 0;
217
218         when RTS_CTS =>
219            Com_Settings.fOutX        := 0;
220            Com_Settings.fOutxCtsFlow := 1;
221
222         when Xon_Xoff =>
223            Com_Settings.fOutX        := 1;
224            Com_Settings.fOutxCtsFlow := 0;
225      end case;
226
227      Com_Settings.fAbortOnError   := 0;
228      Com_Settings.ByteSize        := BYTE (C_Bits (Bits));
229      Com_Settings.Parity          := BYTE (C_Parity (Parity));
230      Com_Settings.StopBits        := BYTE (C_Stop_Bits (Stop_Bits));
231
232      Success := SetCommState (HANDLE (Port.H.all), Com_Settings'Access);
233
234      if Success = Win32.FALSE then
235         Success := CloseHandle (HANDLE (Port.H.all));
236         Port.H.all := 0;
237         Raise_Error ("cannot set comm state");
238      end if;
239
240      --  Set the timeout status
241
242      if Block then
243         Com_Time_Out := (others => 0);
244      else
245         Com_Time_Out :=
246           (ReadTotalTimeoutConstant => DWORD (1000 * Timeout),
247            others                   => 0);
248      end if;
249
250      Success :=
251        SetCommTimeouts
252          (hFile          => HANDLE (Port.H.all),
253           lpCommTimeouts => Com_Time_Out'Access);
254
255      if Success = Win32.FALSE then
256         Raise_Error ("cannot set the timeout");
257      end if;
258   end Set;
259
260   -----------
261   -- Write --
262   -----------
263
264   overriding procedure Write
265     (Port   : in out Serial_Port;
266      Buffer : Stream_Element_Array)
267   is
268      Success   : BOOL;
269      Temp_Last : aliased DWORD;
270
271   begin
272      if Port.H = null then
273         Raise_Error ("write: port not opened", 0);
274      end if;
275
276      Success :=
277        WriteFile
278          (hFile                  => HANDLE (Port.H.all),
279           lpBuffer               => Buffer'Address,
280           nNumberOfBytesToWrite  => DWORD (Buffer'Length),
281           lpNumberOfBytesWritten => Temp_Last'Access,
282           lpOverlapped           => null);
283
284      if Success = Win32.FALSE
285        or else Stream_Element_Offset (Temp_Last) /= Buffer'Length
286      then
287         Raise_Error ("failed to write data");
288      end if;
289   end Write;
290
291end GNAT.Serial_Communications;
292