polyorb_hi_drivers_native_uart.adb 13.7 KB
Newer Older
jhugues's avatar
jhugues committed
1 2 3 4 5 6 7 8
------------------------------------------------------------------------------
--                                                                          --
--                          PolyORB HI COMPONENTS                           --
--                                                                          --
--       P O L Y O R B _ H I _ D R I V E R S _ N A T I V E _ U A R T        --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
--                   Copyright (C) 2012-2015 ESA & ISAE.                    --
--                                                                          --
-- PolyORB-HI is free software; you can redistribute it and/or modify under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. PolyORB-HI is distributed in the hope that it will be useful, but  --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
jhugues's avatar
jhugues committed
26 27 28 29 30 31
--                                                                          --
--              PolyORB-HI/Ada is maintained by the TASTE project           --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

32
With Ada.Exceptions;
33
with Ada.Streams;
hugues.jerome's avatar
hugues.jerome committed
34 35 36
with Ada.Unchecked_Conversion;
with Interfaces;

37 38 39 40 41 42 43 44 45 46
with GNAT.Serial_Communications;

with PolyORB_HI.Output;
with PolyORB_HI.Messages;

with PolyORB_HI_Generated.Transport;

--  This package provides support for the Native_UART device driver as
--  defined in the Native_UART AADLv2 model.

47 48 49 50
with System; use System;

with POHICDRIVER_UART; use POHICDRIVER_UART;

51
package body PolyORB_HI_Drivers_Native_UART is
52

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
   type Serial_Conf_T_Acc is access all POHICDRIVER_UART.Serial_Conf_T;
   function To_Serial_Conf_T_Acc is new Ada.Unchecked_Conversion
     (System.Address, Serial_Conf_T_Acc);

   To_GNAT_Baud_Rate : constant array (POHICDRIVER_UART.Baudrate_T) of
     GNAT.Serial_Communications.Data_Rate :=
     (B9600 => GNAT.Serial_Communications.B9600,
      B19200 => GNAT.Serial_Communications.B19200,
      B38400 => GNAT.Serial_Communications.B38400,
      B57600 => GNAT.Serial_Communications.B57600,
      B115200 => GNAT.Serial_Communications.B115200,
      B230400 => GNAT.Serial_Communications.B115200);
   --  XXX does not exist in GCC.4.4.4

   To_GNAT_Parity_Check : constant array (POHICDRIVER_UART.Parity_T) of
     GNAT.Serial_Communications.Parity_Check :=
     (Even => GNAT.Serial_Communications.Even,
      Odd => GNAT.Serial_Communications.Odd);

   To_GNAT_Bits : constant array (7 .. 8) of
     GNAT.Serial_Communications.Data_Bits :=
74
     (7 => GNAT.Serial_Communications.CS8,
75 76
      8 => GNAT.Serial_Communications.CS8);

77 78 79 80 81 82 83 84 85 86
   pragma Suppress (Elaboration_Check, PolyORB_HI_Generated.Transport);
   --  We do not want a pragma Elaborate_All to be implicitely
   --  generated for Transport.

   use Interfaces;
   use PolyORB_HI.Messages;
   use PolyORB_HI.Utils;
   use PolyORB_HI.Output;

   type Node_Record is record
87 88
      --  UART is a simple protocol, we use one port to send, assuming
      --  it can be used in full duplex mode.
89

90 91
      UART_Port   : GNAT.Serial_Communications.Serial_Port;
      UART_Config : Serial_Conf_T; --  ASN.1 Configuration variable
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
   end record;

   Nodes : array (Node_Type) of Node_Record;

   subtype AS_Message_Length_Stream is Ada.Streams.Stream_Element_Array
     (1 .. Message_Length_Size);
   subtype Message_Length_Stream is Stream_Element_Array
     (1 .. Message_Length_Size);

   subtype AS_Full_Stream is Ada.Streams.Stream_Element_Array (1 .. PDU_Size);
   subtype Full_Stream is Stream_Element_Array (1 .. PDU_Size);

   function To_PO_HI_Message_Length_Stream is new Ada.Unchecked_Conversion
     (AS_Message_Length_Stream, Message_Length_Stream);
   function To_PO_HI_Full_Stream is new Ada.Unchecked_Conversion
     (AS_Full_Stream, Full_Stream);

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (Name_Table : PolyORB_HI.Utils.Naming_Table_Type) is
114
      Parity    : GNAT.Serial_Communications.Parity_Check;
115 116
      Use_ASN1 : Boolean := False;

117
   begin
118
      for J in Name_Table'Range loop
119 120 121
         if Name_Table (J).Variable = System.Null_Address then
            --  The structure of the location information is
            --     "serial DEVICE BAUDS DATA_BITS PARITY STOP_BIT"
122

123 124 125 126 127 128 129
            declare
               S : constant String := PolyORB_HI.Utils.To_String
                 (Name_Table (J).Location);
               Device_First, Device_Last : Integer;
               Bauds     : GNAT.Serial_Communications.Data_Rate;
               Bits      : GNAT.Serial_Communications.Data_Bits;
               Stop_Bits : GNAT.Serial_Communications.Stop_Bits_Number;
130

131
               First, Last : Integer;
132 133

            begin
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
               First := S'First;

               --  First parse the prefix "serial"

               Last := Parse_String (S, First, ' ');

               if S (First .. Last) /= "serial" then
                  raise Program_Error with "Invalid configuration";
               end if;

               --  Then, parse the device

               First := Last + 2;
               Last := Parse_String (S, First, ' ');
               Device_First := First;
               Device_Last := Last;

               --  Then, parse the baud rate

               First := Last + 2;
               Last := Parse_String (S, First, ' ');
               begin
                  Bauds := GNAT.Serial_Communications.Data_Rate'Value
                    ('B' & S (First .. Last));
               exception
                  when others =>
                     Put_Line (Normal, "Wrong baud rate: " & S (First .. Last));
                     raise;
               end;

               --  Then, parse the data bits

               First := Last + 2;
               Last := Parse_String (S, First, ' ');

               --  Note: on GNAT GPL 2008 and before, Data_Bits define
               --  the B8 and B7 enumerators; on GNAT GPL 2009 and after,
               --  Data_Bits define the CS8 and CS7 enumerators.

               if S (First) = '8' then
                  Bits := GNAT.Serial_Communications.CS8;
               elsif S (First) = '7' then
                  Bits := GNAT.Serial_Communications.CS7;
               else
                  raise Program_Error with "Wrong bits: " & S (First);
               end if;

               --  Then, parse the parity

               First := Last + 2;
               Last := Parse_String (S, First, ' ');

               if S (First) = 'N' then
                  Parity :=  GNAT.Serial_Communications.None;
               elsif S (First) = 'E' then
                  Parity :=  GNAT.Serial_Communications.Even;
               elsif S (First) = 'O' then
                  Parity :=  GNAT.Serial_Communications.Odd;
               else
                  raise Program_Error with "Wrong parity: " & S (First);
               end if;

               --  Finally, parse the stop_bits

               First := Last + 2;
               Last := Parse_String (S, First, ' ');

               if S (First) = '1' then
                  Stop_Bits :=  GNAT.Serial_Communications.One;
               elsif S (First) = '2' then
                  Stop_Bits :=  GNAT.Serial_Communications.Two;
               else
                  raise Program_Error with "Wrong stop bits: " & S (First);
               end if;

               GNAT.Serial_Communications.Open
                 (Port => Nodes (My_Node).UART_Port,
                  Name => GNAT.Serial_Communications.Port_Name
                    (S (Device_First .. Device_Last)));

               GNAT.Serial_Communications.Set
                 (Port      => Nodes (My_Node).UART_Port,
                  Rate      => Bauds,
                  Bits      => Bits,
                  Stop_Bits => Stop_Bits,
                  Parity    => Parity,
                  Block     => True);

222 223
            exception
               when others =>
224
                  Put_Line (Normal, "Initialization of UART subsystem dead");
225
            end;
226 227 228
         else
            --  We got an ASN.1 configuration variable, use it
            --  directly.
229
	    Use_ASN1 := True;
230 231
            Nodes (J).UART_Config := To_Serial_Conf_T_Acc
              (Name_Table (J).Variable).all;
232

233
         end if;
234 235
      end loop;

236 237 238 239 240 241 242 243 244 245 246 247
      GNAT.Serial_Communications.Open
	(Port => Nodes (My_Node).UART_Port,
	 Name => GNAT.Serial_Communications.Port_Name
	   (Nodes (My_Node).UART_Config.devname));

      if Nodes (My_Node).UART_Config.Use_Paritybit then
	 Parity := To_GNAT_Parity_Check
	   (Nodes (My_Node).UART_Config.Parity);
      else
	 Parity := GNAT.Serial_Communications.None;
      end if;

248 249 250 251 252 253 254 255 256 257 258
      Put_Line (Normal, " -> Using ASN.1: " & Use_ASN1'Img);
      Put_Line (Normal, "Device: " & Nodes (My_Node).UART_Config.devname);
      Put_Line (Normal, "  * Use Parity bits: "
		  & Nodes (My_Node).UART_Config.use_paritybit'Img);
      Put_Line (Normal, "  * Rate: "
		  & To_GNAT_Baud_Rate (Nodes (My_Node).UART_Config.Speed)'Img);
      Put_Line (Normal, "  * Parity: " & Parity'Img);
      Put_Line (Normal, "  * Bits: "
		  & To_GNAT_Bits
		  (Integer (Nodes (My_Node).UART_Config.Bits))'Img);

259 260
      GNAT.Serial_Communications.Set
	(Port   => Nodes (My_Node).UART_Port,
261 262 263 264
         Rate   => To_GNAT_Baud_Rate (Nodes (My_Node).UART_Config.Speed),
         Parity => Parity,
         Bits   => To_GNAT_Bits (Integer (Nodes (My_Node).UART_Config.Bits)),
         Block  => True);
265

266
      Put_Line (Normal, "Initialization of Native_UART subsystem is complete");
267 268 269 270 271 272 273 274 275 276 277 278
   end Initialize;

   -------------
   -- Receive --
   -------------

   procedure Receive is
      use type Ada.Streams.Stream_Element_Offset;

      SEL : AS_Message_Length_Stream;
      SEA : AS_Full_Stream;
      SEO : Ada.Streams.Stream_Element_Offset;
hugues.jerome's avatar
hugues.jerome committed
279
      Packet_Size : Ada.Streams.Stream_Element_Offset;
280 281 282 283 284 285 286 287 288 289 290
      Data_Received_Index : Ada.Streams.Stream_Element_Offset;
   begin

      Main_Loop : loop
         Put_Line ("Using user-provided Native_UART stack to receive");

         --  UART is a character-oriented protocol

         --  1/ Receive message length

         GNAT.Serial_Communications.Read
291
           (Nodes (My_Node).UART_Port, SEL, SEO);
292

hugues.jerome's avatar
hugues.jerome committed
293 294
         Packet_Size := Ada.Streams.Stream_Element_Offset
           (To_Length (To_PO_HI_Message_Length_Stream (SEL)));
295 296
         SEO := Packet_Size;

297 298 299 300
         SEA (1 .. Message_Length_Size) := SEL;

         Data_Received_Index := Message_Length_Size + 1;

301
         while Data_Received_Index <= Packet_Size + Message_Length_Size loop
302 303 304
            --  We must loop to make sure we receive all data

            GNAT.Serial_Communications.Read
305
              (Nodes (My_Node).UART_Port,
306 307
               SEA (Data_Received_Index .. SEO + 1),
               SEO);
308
            Data_Received_Index := 1 + SEO + 1;
309 310 311 312 313 314 315
         end loop;

         --  2/ Receive full message

         if SEO /= SEA'First - 1 then
            Put_Line
              (Normal,
316
               "UART received"
317 318 319 320 321
                 & Ada.Streams.Stream_Element_Offset'Image (SEO)
                 & " bytes");

            --  Deliver to the peer handler

hugues.jerome's avatar
hugues.jerome committed
322 323 324
            begin
               PolyORB_HI_Generated.Transport.Deliver
                 (Corresponding_Entity
325
                    (Unsigned_8 (SEA (Message_Length_Size + 1))),
hugues.jerome's avatar
hugues.jerome committed
326 327 328 329 330 331
                  To_PO_HI_Full_Stream (SEA)
                    (1 .. Stream_Element_Offset (SEO)));
            exception
               when E : others =>
                  Put_Line (Ada.Exceptions.Exception_Information (E));
            end;
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
         else
            Put_Line ("Got error");
         end if;
      end loop Main_Loop;
   end Receive;

   ----------
   -- Send --
   ----------

   function Send
     (Node    : Node_Type;
      Message : Stream_Element_Array;
      Size    : Stream_Element_Offset)
     return Error_Kind
   is
hugues.jerome's avatar
hugues.jerome committed
348 349
      pragma Unreferenced (Node);

350 351 352 353 354 355 356 357 358 359 360 361 362
      --  We cannot cast both array types using
      --  Ada.Unchecked_Conversion because they are unconstrained
      --  types. We cannot either use direct casting because component
      --  types are incompatible. The only time efficient manner to do
      --  the casting is to use representation clauses.

      Msg : Ada.Streams.Stream_Element_Array
        (1 .. Ada.Streams.Stream_Element_Offset (Size));
      pragma Import (Ada, Msg);
      for Msg'Address use Message'Address;

   begin
      Put_Line ("Using user-provided UART stack to send");
363 364
      Put_Line ("Sending through UART "
                  & Nodes (My_Node).UART_Config.devname
365 366 367
                  & Size'Img & " bytes");

      GNAT.Serial_Communications.Write
368
        (Port   => Nodes (My_Node).UART_Port,
369 370 371
         Buffer => Msg);

      return Error_Kind'(Error_None);
372
      --  Note: we have no way to know there was an error here
373

374 375
   end Send;

376
end PolyORB_HI_Drivers_Native_UART;