gnoga_2.1.2_5f127c56/deps/simple_components/gnat-sockets-connection_state_machine-modbus_client-synchronous.adb

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
--                                                                    --
--  package                         Copyright (c)  Dmitry A. Kazakov  --
--     GNAT.Sockets.Connection_State_Machine.      Luebeck            --
--     MODBUS_Client.Synchronous                   Spring, 2015       --
--  Implementation                                                    --
--                                Last revision :  09:02 17 Oct 2020  --
--                                                                    --
--  This  library  is  free software; you can redistribute it and/or  --
--  modify it under the terms of the GNU General Public  License  as  --
--  published by the Free Software Foundation; either version  2  of  --
--  the License, or (at your option) any later version. This library  --
--  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. See the GNU  --
--  General  Public  License  for  more  details.  You  should  have  --
--  received  a  copy  of  the GNU General Public License along with  --
--  this library; if not, write to  the  Free  Software  Foundation,  --
--  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.    --
--                                                                    --
--  As a special exception, if other files instantiate generics from  --
--  this unit, or you link this unit with other files to produce  an  --
--  executable, this unit does not by  itself  cause  the  resulting  --
--  executable to be covered by the GNU General Public License. This  --
--  exception  does not however invalidate any other reasons why the  --
--  executable file might be covered by the GNU Public License.       --
--____________________________________________________________________--

with Ada.Exceptions;     use Ada.Exceptions;
with Ada.IO_Exceptions;  use Ada.IO_Exceptions;
with Ada.Real_Time;      use Ada.Real_Time;
with Synchronization;    use Synchronization;

package body GNAT.Sockets.Connection_State_Machine.MODBUS_Client.
             Synchronous is

   procedure Bits_Read
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Values    : Bit_Array;
                Code      : Function_Code;
                Unit      : Unit_No
             )  is
   begin
      if Client.Result.Mode = Bits then
         declare
            Result : Bits_Result renames Client.Result.Bits.all;
         begin
            Result.Length := Values'Length;
            Result.Data (1..Values'Length) := Values;
         end;
      else
         Client.Result := (Mode => None);
      end if;
      Client.Event.Complete;
   end Bits_Read;

   procedure Bits_Written
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                From      : Bit_Address;
                To        : Bit_Address;
                Code      : Function_Code;
                Unit      : Unit_No
             )  is
   begin
      Client.Result := (Mode => None);
      Client.Event.Complete;
   end Bits_Written;

   procedure Cancel (Client : in out MODBUS_Synchronous_Client) is
   begin
      Client.Event.Cancel;
   end Cancel;

   procedure Connect
             (  Client         : in out MODBUS_Synchronous_Client;
                Host           : String;
                Port           : Port_Type := MODBUS_Port;
                Max_Connect_No : Positive  := Positive'Last;
                Timeout        : Duration  := Duration'Last
             )  is
      Lock     : Holder (Client'Unchecked_Access);
      Deadline : constant Time := abs Timeout;
   begin
      Shutdown (Client);
      select
         Client.Event.Wait_For_Release;
      or delay until Deadline;
         Raise_Exception
         (  Timeout_Error'Identity,
            "Shutdown timeout expired"
         );
      end select;
      Connect
      (  Client.Listener.all,
         Client'Unchecked_Access,
         Host,
         Port,
         Max_Connect_No
      );
      select
         Client.Event.Wait_For_Connection;
      or delay until Deadline;
         Raise_Exception
         (  Timeout_Error'Identity,
            "Connection timeout expired"
         );
      end select;
   end Connect;

   procedure Connected (Client : in out MODBUS_Synchronous_Client) is
   begin
       Connected (MODBUS_Client (Client));
       Client.Event.Set;
   end Connected;

   procedure Exception_Status_Received
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Status    : Unsigned_8;
                Unit      : Unit_No
             )  is
   begin
      Client.Result := (Exception_Status, Status);
      Client.Event.Complete;
   end Exception_Status_Received;

   procedure Failed
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Error     : Exception_Code;
                Code      : Function_Code;
                Unit      : Unit_No
             )  is
   begin
      Client.Result := (Failure, Error);
      Client.Event.Complete;
   end Failed;

   function FC1
            (  Client    : access MODBUS_Synchronous_Client;
               Reference : Reference_ID;
               From      : Bit_Address;
               To        : Bit_Address;
               Unit      : Unit_No  := 255;
               Timeout   : Duration := Duration'Last
            )  return Bit_Array is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client);
      Result   : aliased Bits_Result;
   begin
      Client.Result := (Bits, Result'Unchecked_Access);
      Wait_Until_RTU_Silence_Time (Client.all, Deadline);
      Send_FC1 (Client.all, Reference, From, To, Unit);
      Wait (Client.all, Bits, Deadline);
      return Result.Data (1..Result.Length);
   end FC1;

   function FC2
            (  Client    : access MODBUS_Synchronous_Client;
               Reference : Reference_ID;
               From      : Bit_Address;
               To        : Bit_Address;
               Unit      : Unit_No  := 255;
               Timeout   : Duration := Duration'Last
            )  return Bit_Array is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client);
      Result   : aliased Bits_Result;
   begin
      Client.Result := (Bits, Result'Unchecked_Access);
      Wait_Until_RTU_Silence_Time (Client.all, Deadline);
      Send_FC2 (Client.all, Reference, From, To, Unit);
      Wait (Client.all, Bits, Deadline);
      return Result.Data (1..Result.Length);
   end FC2;

   function FC3
            (  Client    : access MODBUS_Synchronous_Client;
               Reference : Reference_ID;
               From      : Word_Address;
               To        : Word_Address;
               Unit      : Unit_No  := 255;
               Timeout   : Duration := Duration'Last
            )  return Word_Array is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client);
      Result   : aliased Words_Result;
   begin
      Client.Result := (Words, Result'Unchecked_Access);
      Wait_Until_RTU_Silence_Time (Client.all, Deadline);
      Send_FC3 (Client.all, Reference, From, To, Unit);
      Wait (Client.all, Words, Deadline);
      return Result.Data (1..Result.Length);
   end FC3;

   function FC4
            (  Client    : access MODBUS_Synchronous_Client;
               Reference : Reference_ID;
               From      : Word_Address;
               To        : Word_Address;
               Unit      : Unit_No  := 255;
               Timeout   : Duration := Duration'Last
            )  return Word_Array is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client);
      Result   : aliased Words_Result;
   begin
      Client.Result := (Words, Result'Unchecked_Access);
      Wait_Until_RTU_Silence_Time (Client.all, Deadline);
      Send_FC4 (Client.all, Reference, From, To, Unit);
      Wait (Client.all, Words, Deadline);
      return Result.Data (1..Result.Length);
   end FC4;

   procedure FC5
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Address   : Bit_Address;
                Value     : Boolean;
                Unit      : Unit_No  := 255;
                Timeout   : Duration := Duration'Last
             )  is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client'Unchecked_Access);
   begin
      Wait_Until_RTU_Silence_Time (Client, Deadline);
      Send_FC5 (Client, Reference, Address, Value, Unit);
      Wait (Client, None, Deadline);
   end FC5;

   procedure FC6
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Address   : Word_Address;
                Value     : Unsigned_16;
                Unit      : Unit_No  := 255;
                Timeout   : Duration := Duration'Last
             )  is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client'Unchecked_Access);
   begin
      Wait_Until_RTU_Silence_Time (Client, Deadline);
      Send_FC6 (Client, Reference, Address, Value, Unit);
      Wait (Client, None, Deadline);
   end FC6;

   function FC7
            (  Client    : access MODBUS_Synchronous_Client;
               Reference : Reference_ID;
               Unit      : Unit_No  := 255;
               Timeout   : Duration := Duration'Last
            )  return Unsigned_8 is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client);
   begin
      Wait_Until_RTU_Silence_Time (Client.all, Deadline);
      Send_FC7 (Client.all, Reference, Unit);
      Wait (Client.all, Exception_Status, Deadline);
      return Client.Result.Status;
   end FC7;

   procedure FC15
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Values    : Bit_Array;
                Unit      : Unit_No  := 255;
                Timeout   : Duration := Duration'Last
             )  is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client'Unchecked_Access);
   begin
      Wait_Until_RTU_Silence_Time (Client, Deadline);
      Send_FC15 (Client, Reference, Values, Unit);
      Wait (Client, None, Deadline);
   end FC15;

   procedure FC16
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Values    : Word_Array;
                Unit      : Unit_No  := 255;
                Timeout   : Duration := Duration'Last
             )  is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client'Unchecked_Access);
   begin
      Wait_Until_RTU_Silence_Time (Client, Deadline);
      Send_FC16 (Client, Reference, Values, Unit);
      Wait (Client, None, Deadline);
   end FC16;

   procedure FC22
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Address   : Word_Address;
                And_Mask  : Unsigned_16;
                Or_Mask   : Unsigned_16;
                Unit      : Unit_No  := 255;
                Timeout   : Duration := Duration'Last
             )  is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client'Unchecked_Access);
   begin
      Wait_Until_RTU_Silence_Time (Client, Deadline);
      Send_FC22 (Client, Reference, Address, And_Mask, Or_Mask, Unit);
      Wait (Client, None, Deadline);
   end FC22;

   function FC23
            (  Client    : access MODBUS_Synchronous_Client;
               Reference : Reference_ID;
               From      : Word_Address;
               To        : Word_Address;
               Values    : Word_Array;
               Unit      : Unit_No  := 255;
               Timeout   : Duration := Duration'Last
            )  return Word_Array is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client);
      Result   : aliased Words_Result;
   begin
      Client.Result := (Words, Result'Unchecked_Access);
      Wait_Until_RTU_Silence_Time (Client.all, Deadline);
      Send_FC23 (Client.all, Reference, From, To, Values, Unit);
      Wait (Client.all, Words, Deadline);
      return Result.Data (1..Result.Length);
   end FC23;

   function FC24
            (  Client    : access MODBUS_Synchronous_Client;
               Reference : Reference_ID;
               Address   : Word_Address;
               Unit      : Unit_No  := 255;
               Timeout   : Duration := Duration'Last
            )  return Word_Array is
      Deadline : constant Time := abs Timeout;
      Lock     : Holder (Client);
      Result   : aliased Words_Result;
   begin
      Client.Result := (Words, Result'Unchecked_Access);
      Wait_Until_RTU_Silence_Time (Client.all, Deadline);
      Send_FC24 (Client.all, Reference, Address, Unit);
      Wait (Client.all, Words, Deadline);
      return Result.Data (1..Result.Length);
   end FC24;

   procedure Finalize (Object : in out Holder) is
   begin
      Object.Client.Event.Release;
   end Finalize;

   function Get_RTU_Checksum_Mode
            (  Client : MODBUS_Synchronous_Client
            )  return Boolean is
   begin
      return Get_RTU_Checksum_Mode (MODBUS_Client (Client));
   end Get_RTU_Checksum_Mode;

   function Get_RTU_Silence_Time
            (  Client : MODBUS_Synchronous_Client
            )  return Duration is
   begin
      return Get_RTU_Silence_Time (MODBUS_Client (Client));
   end Get_RTU_Silence_Time;

   procedure Initialize (Object : in out Holder) is
   begin
      Object.Client.Event.Seize;
   end Initialize;

   procedure Released (Client : in out MODBUS_Synchronous_Client) is
   begin
      Released (MODBUS_Client (Client));
      Client.Event.Set;
   end Released;

   procedure Set_RTU_Checksum_Mode
             (  Client : in out MODBUS_Synchronous_Client;
                Enable : Boolean
             )  is
   begin
      Set_RTU_Checksum_Mode (MODBUS_Client (Client), Enable);
   end Set_RTU_Checksum_Mode;

   procedure Set_RTU_Silence_Time
             (  Client  : in out MODBUS_Synchronous_Client;
                Silence : Duration
             )  is
   begin
      Set_RTU_Silence_Time (MODBUS_Client (Client), Silence);
   end Set_RTU_Silence_Time;

   procedure Wait
             (  Client   : in out MODBUS_Synchronous_Client;
                Expected : Result_Mode;
                Deadline : Time
             )  is
   begin
      select
         Client.Event.Wait_For_Completion;
      or delay until Deadline;
         Raise_Exception (Timeout_Error'Identity, Expired_Text);
      end select;
      if Client.Result.Mode /= Expected then
         if Client.Result.Mode = Failure then
            Raise_Exception
            (  MODBUS_Error'Identity,
               Error_Text (Client.Result.Error)
            );
         else
            Raise_Exception
            (  Program_Error'Identity,
               "Unexpected response result"
            );
         end if;
      end if;
   end Wait;

   procedure Words_Read
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                Values    : Word_Array;
                Code      : Function_Code;
                Unit      : Unit_No
             )  is
   begin
      if Client.Result.Mode = Words then
         declare
            Result : Words_Result renames Client.Result.Words.all;
         begin
            Result.Length := Values'Length;
            Result.Data (1..Values'Length) := Values;
         end;
      else
         Client.Result := (Mode => None);
      end if;
      Client.Event.Complete;
   end Words_Read;

   procedure Write
             (  Stream : access Root_Stream_Type'Class;
                Object : Event_Type
             )  is
   begin
      null;
   end Write;

   procedure Words_Written
             (  Client    : in out MODBUS_Synchronous_Client;
                Reference : Reference_ID;
                From      : Word_Address;
                To        : Word_Address;
                Code      : Function_Code;
                Unit      : Unit_No
             )  is
   begin
      Client.Result := (Mode => None);
      Client.Event.Complete;
   end Words_Written;

   protected body Event_Type is

      entry Cancel when Wait_For_Completion'Count = 0
               and then Wait_For_Connection'Count = 0
               and then Wait_For_Release'Count = 0 is
      begin
         null;
      end Cancel;

      entry Lounge when Owner = Null_Task_ID is
      begin
         Owner := Lounge'Caller;
         Count := 1;
         Done  := False;
      end Lounge;

      entry Seize when True is
      begin
         if Seize'Caller = Owner then
            Count := Count + 1;
            Done  := False;
         else
            requeue Lounge with abort;
         end if;
      end Seize;

      procedure Release is
      begin
         if Owner = Current_Task then
            Count := Count - 1;
            if Count = 0 then
               Owner := Null_Task_ID;
            end if;
         else
            Raise_Exception
            (  Program_Error'Identity,
               "Mutex ownership error"
            );
         end if;
      end Release;

      procedure Complete is
      begin
         Done := True;
      end Complete;

      procedure Set is
      begin
         case Get_Session_State (Client.all) is
            when Session_Down =>
               Ready := False;
               Down  := True;
            when Session_Disconnected | Session_Connecting |
                 Session_Handshaking  | Session_Busy       =>
               Ready := False;
               Down  := False;
            when Session_Active | Session_Connected =>
               Ready := True;
               Down  := False;
         end case;
      end Set;

      entry Wait_For_Completion
         when Done or else Down or else Event_Type.Cancel'Count > 0 is
      begin
         if Done then
            return;
         end if;
         case Get_Session_State (Client.all) is
            when Session_Down =>
               Ready := False;
               Down  := True;
               declare
                  Error : Exception_Occurrence;
               begin
                  Get_Occurrence (Client.all, Error);
                  if Exception_Identity (Error) /= Null_Id then
                     Reraise_Occurrence (Error);
                  else
                     Raise_Exception
                     (  Status_Error'Identity,
                        "Unable to connect"
                     );
                  end if;
               end;
            when Session_Disconnected | Session_Connecting |
                 Session_Handshaking  | Session_Busy       =>
               Ready := False;
               Down  := False;
            when Session_Active | Session_Connected =>
               Ready := True;
               Down  := False;
         end case;
         Raise_Exception
         (  Status_Error'Identity,
            "Connection was lost during execution of the request"
         );
      end Wait_For_Completion;

      entry Wait_For_Release
         when Down or else Event_Type.Cancel'Count > 0 is
      begin
         if Get_Session_State (Client.all) /= Session_Down then
            Down := False;
            if Event_Type.Cancel'Count > 0 then
               Raise_Exception (Cancel_Error'Identity, "Canceled");
            else
               requeue Wait_For_Release with abort;
            end if;
         end if;
      end Wait_For_Release;

      entry Wait_For_Connection
         when Ready or else Down or else Event_Type.Cancel'Count > 0 is
      begin
         case Get_Session_State (Client.all) is
            when Session_Down =>
               Ready := False;
               Down  := True;
               declare
                  Error : Exception_Occurrence;
               begin
                  Get_Occurrence (Client.all, Error);
                  if Exception_Identity (Error) /= Null_Id then
                     Reraise_Occurrence (Error);
                  else
                     Raise_Exception
                     (  Status_Error'Identity,
                        "Unable to connect"
                     );
                  end if;
               end;
            when Session_Disconnected | Session_Connecting |
                 Session_Handshaking  | Session_Busy       =>
               Ready := False;
               Down  := False;
               if Event_Type.Cancel'Count > 0 then
                  Raise_Exception (Cancel_Error'Identity, "Canceled");
               else
                  requeue Wait_For_Connection with abort;
               end if;
            when Session_Active | Session_Connected =>
               Ready := True;
               Down  := False;
         end case;
      end Wait_For_Connection;

   end Event_Type;

end GNAT.Sockets.Connection_State_Machine.MODBUS_Client.Synchronous;