semi_e37 0.2.0

High-Speed SECS Message Services
Documentation
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
//! # GENERIC SERVICES
//! 
//! Defines the full functionality of the [HSMS] protocol without modification
//! by any subsidiary standards. This involves the sending of messages of
//! particular types and at particular times as allowed by the protocol.
//! 
//! ---------------------------------------------------------------------------
//! 
//! To use the [Generic Services]:
//! 
//! - Build [Message]s which use a [Message ID] and [Message Contents]:
//!   - [Data Message]
//!   - [Select.req]
//!   - [Select.rsp]
//!   - [Deselect.req]
//!   - [Deselect.rsp]
//!   - [Linktest.req]
//!   - [Linktest.rsp]
//!   - [Reject.req]
//!   - [Separate.req]
//! - Create an [Client] by providing the [New Client] function with
//!   [Parameter Settings].
//! - Manage the [Connection State] with the [Connect Procedure] and
//!   [Disconnect Procedure].
//! - Manage the [Selection State] with the [Select Procedure],
//!   [Deselect Procedure], and [Separate Procedure].
//! - Receive [Data Message]s with the hook provided by the
//!   [Connect Procedure].
//! - Test connection integrity with the [Linktest Procedure].
//! - Send [Data Message]s with the [Data Procedure].
//! - Send [Reject.req] messages [Reject Procedure].
//! 
//! [HSMS]:                 crate
//! [Generic Services]:     crate::generic
//! [Client]:               Client
//! [New Client]:           Client::new
//! [Connect Procedure]:    Client::connect
//! [Disconnect Procedure]: Client::disconnect
//! [Select Procedure]:     Client::select
//! [Deselect Procedure]:   Client::deselect
//! [Separate Procedure]:   Client::separate
//! [Linktest Procedure]:   Client::linktest
//! [Data Procedure]:       Client::data
//! [Reject Procedure]:     Client::reject
//! [Message]:              Message
//! [Message ID]:           MessageID
//! [Message Contents]:     MessageContents
//! [Data Message]:         MessageContents::DataMessage
//! [Select.req]:           MessageContents::SelectRequest
//! [Select.rsp]:           MessageContents::SelectResponse
//! [Deselect.req]:         MessageContents::DeselectRequest
//! [Deselect.rsp]:         MessageContents::DeselectResponse
//! [Linktest.req]:         MessageContents::LinktestRequest
//! [Linktest.rsp]:         MessageContents::LinktestResponse
//! [Reject.req]:           MessageContents::RejectRequest
//! [Separate.req]:         MessageContents::SeparateRequest
//! [Connection State]:     crate::primitive::ConnectionState
//! [Selection State]:      SelectionState
//! [Parameter Settings]:   ParameterSettings

use std::{
  collections::HashMap,
  io::{
    Error,
    ErrorKind,
  },
  net::SocketAddr,
  ops::{
    Deref,
    DerefMut,
  },
  sync::{
    atomic::Ordering::Relaxed,
    Arc,
    Mutex,
    mpsc::{
      channel,
      Receiver,
      Sender,
    },
  },
  thread::{
    self,
    JoinHandle,
  },
  time::Duration,
};
use atomic::Atomic;
use bytemuck::NoUninit;
use oneshot::Sender as SendOnce;
use crate::{
  PresentationType,
  primitive,
};

pub use crate::primitive::ConnectionMode;

/// ## CLIENT
/// 
/// Encapsulates the full functionality of the [HSMS] protocol without
/// reference to any subsidiary standards, known as the [Generic Services].
/// 
/// [HSMS]:             crate
/// [Generic Services]: crate::generic
pub struct Client {
  parameter_settings: ParameterSettings,
  primitive_client: Arc<primitive::Client>,
  selection_state: Atomic<SelectionState>,
  selection_mutex: Mutex<()>,
  outbox: Mutex<HashMap<u32, (MessageID, SendOnce<Option<Message>>)>>,
  system: Mutex<u32>,
}

/// ## CONNECTION PROCEDURES
/// **Based on SEMI E37-1109§6.3-6.5**
/// 
/// Encapsulates the parts of the [Client]'s functionality dealing with
/// establishing and breaking a TCP/IP connection.
/// 
/// - [New Client]
/// - [Connect Procedure]
/// - [Disconnect Procedure]
/// 
/// [Client]:               Client
/// [New Client]:           Client::new
/// [Connect Procedure]:    Client::connect
/// [Disconnect Procedure]: Client::disconnect
impl Client {
  /// ### NEW CLIENT
  /// 
  /// 
  /// Creates a [Client] in the [NOT CONNECTED] state, ready to initiate the
  /// [Connect Procedure].
  /// 
  /// [Client]:            Client
  /// [Connect Procedure]: Client::connect
  /// [NOT CONNECTED]:     primitive::ConnectionState::NotConnected
  pub fn new(
    parameter_settings: ParameterSettings
  ) -> Arc<Self> {
    Arc::new(Client {
      parameter_settings,
      primitive_client: primitive::Client::new(),
      selection_state:  Default::default(),
      selection_mutex:  Default::default(),
      outbox:           Default::default(),
      system:           Default::default(),
    })
  }

  /// ### CONNECT PROCEDURE
  /// **Based on SEMI E37-1109§6.3.4-6.3.7**
  /// 
  /// Connects the [Client] to the Remote Entity.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [NOT CONNECTED] state to use this
  /// procedure.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connect Procedure] has two different behaviors based on the
  /// [Connection Mode] provided to it:
  /// - [PASSIVE] - The socket address of the Local Entity must be provided,
  ///   and the [Client] listens for and accepts the [Connect Procedure] when
  ///   initiated by the Remote Entity.
  /// - [ACTIVE] - The socket address of the Remote Entity must be provided,
  ///   and the [Client] initiates the [Connect Procedure] and waits up to the
  ///   time specified by [T5] for the Remote Entity to respond.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Upon completion of the [Connect Procedure], the [T8] parameter is set as
  /// the TCP stream's read and write timeout, and the [CONNECTED] state is
  /// entered.
  /// 
  /// [Connection State]:  primitive::ConnectionState
  /// [NOT CONNECTED]:     primitive::ConnectionState::NotConnected
  /// [CONNECTED]:         primitive::ConnectionState::Connected
  /// [Connection Mode]:   primitive::ConnectionMode
  /// [PASSIVE]:           primitive::ConnectionMode::Passive
  /// [ACTIVE]:            primitive::ConnectionMode::Active
  /// [Client]:            Client
  /// [Connect Procedure]: Client::connect
  /// [T5]:                ParameterSettings::t5
  /// [T8]:                ParameterSettings::t8
  pub fn connect(
    self: &Arc<Self>,
    entity: &str,
  ) -> Result<(SocketAddr, Receiver<(MessageID, semi_e5::Message)>), Error> {
    // Connect Primitive Client
    let (socket, rx_receiver) = self.primitive_client.connect(entity, self.parameter_settings.connect_mode, self.parameter_settings.t5, self.parameter_settings.t8)?;
    // Create Channel
    let (data_sender, data_receiver) = channel::<(MessageID, semi_e5::Message)>();
    // Start RX Thread
    let clone: Arc<Client> = self.clone();
    thread::spawn(move || {clone.receive(rx_receiver, data_sender)});
    // Finish
    Ok((socket, data_receiver))
  }

  /// ### DISCONNECT PROCEDURE
  /// **Based on SEMI E37-1109§6.4-6.5**
  /// 
  /// Disconnects the [Client] from the Remote Entity.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [CONNECTED] state to use this
  /// procedure.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Upon completion of the [Disconnect Procedure], the [NOT CONNECTED] state
  /// is entered.
  /// 
  /// [Connection State]:     primitive::ConnectionState
  /// [NOT CONNECTED]:        primitive::ConnectionState::NotConnected
  /// [CONNECTED]:            primitive::ConnectionState::Connected
  /// [Client]:               Client
  /// [Disconnect Procedure]: Client::disconnect
  pub fn disconnect(
    self: &Arc<Self>,
  ) -> Result<(), Error> {
    // TO: NOT CONNECTED
    let result: Result<(), Error> = self.primitive_client.disconnect();
    // TO: NOT SELECTED
    let _guard = self.selection_mutex.lock().unwrap();
    if let SelectionState::Selected = self.selection_state.load(Relaxed) {
      self.selection_state.store(SelectionState::NotSelected, Relaxed);
    }
    // Finish
    result
  }
}

/// ## MESSAGE EXCHANGE PROCEDURES
/// **Based on SEMI E37-1109§7**
/// 
/// Encapsulates the parts of the [Client]'s functionality dealing with
/// exchanging [Message]s.
/// 
/// - [Data Procedure] - [Data Message]s
/// - [Select Procedure] - [Select.req] and [Select.rsp]
/// - [Deselect Procedure] - [Deselect.req] and [Deselect.rsp]
/// - [Linktest Procedure] - [Linktest.req] and [Linktest.rsp]
/// - [Separate Procedure] - [Separate.req]
/// - [Reject Procedure] - [Reject.req]
/// 
/// [Message]:            Message
/// [Client]:             Client
/// [Select Procedure]:   Client::select
/// [Data Procedure]:     Client::data
/// [Deselect Procedure]: Client::deselect
/// [Linktest Procedure]: Client::linktest
/// [Separate Procedure]: Client::separate
/// [Reject Procedure]:   Client::reject
/// [Data Message]:       MessageContents::DataMessage
/// [Select.req]:         MessageContents::SelectRequest
/// [Select.rsp]:         MessageContents::SelectResponse
/// [Deselect.req]:       MessageContents::DeselectRequest
/// [Deselect.rsp]:       MessageContents::DeselectResponse
/// [Linktest.req]:       MessageContents::LinktestRequest
/// [Linktest.rsp]:       MessageContents::LinktestResponse
/// [Reject.req]:         MessageContents::RejectRequest
/// [Separate.req]:       MessageContents::SeparateRequest
impl Client {
  /// ### RECEIVE PROCEDURE
  /// 
  /// An [Client] in the [CONNECTED] state will automatically receive
  /// [Message]s and respond based on their [Message Contents] and the current
  /// [Selection State].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Data Message]
  /// 
  /// - [NOT SELECTED] - The [Client] will respond by transmitting a
  ///   [Reject.req] message, rejecting the [HSMS Data Procedure] and
  ///   completing the [HSMS Reject Procedure].
  /// - [SELECTED], Primary [Data Message] - The [Client] will send the
  ///   [Data Message] to the hook provided by the [Connect Procedure].
  /// - [SELECTED], Response [Data Message] - The [Client] will respond by
  ///   correllating the message to a previously sent Primary [Data Message],
  ///   finishing a previously initiated [Data Procedure] if successful,
  ///   or if unsuccessful by transmitting a [Reject.req] message, rejecting
  ///   the [Data Procedure] and completing the [Reject Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Select.req]:
  /// 
  /// - [NOT SELECTED] - The [Client] will respond with a [Select.rsp]
  ///   accepting and completing the [Select Procedure].
  /// - [SELECTED] - The [Client] will respond with a [Select.rsp] message
  ///   rejecting the [Select Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Select.rsp]:
  /// 
  /// - [NOT SELECTED] - The [Client] will complete the [Select Procedure].
  /// - [SELECTED] - The [Client] will respond with a [Reject.req] message,
  ///   completing the [Reject Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Deselect.req]:
  /// 
  /// - Not yet implemented.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Deselect.rsp]:
  /// 
  /// - Not yet implemented.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Linktest.req]:
  /// 
  /// - The [Client] will respond with a [Linktest.rsp], completing the
  ///   [Linktest Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Linktest.rsp]:
  /// 
  /// - The [Client] will respond by correllating the message to a previously
  ///   sent [Linktest.req] message, finishing a previously initiated
  ///   [Linktest Procedure] if successful, or if unsuccessful by transmitting
  ///   a [Reject.req] message, completing the [Reject Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Reject.req]:
  /// 
  /// - Not yet implemented.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### [Separate.req]:
  /// 
  /// - [NOT SELECTED] - The [Client] will not do anything.
  /// - [SELECTED] - The [Client] will complete the [Separate Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// #### Unknown [Primitive Message]:
  /// 
  /// - The [Client] will respond by transmitting a [Reject.req] message,
  ///   completing the [Reject Procedure]. 
  /// 
  /// [Primitive Message]:  primitive::Message
  /// [Connection State]:   primitive::ConnectionState
  /// [NOT CONNECTED]:      primitive::ConnectionState::NotConnected
  /// [CONNECTED]:          primitive::ConnectionState::Connected
  /// [Message]:            Message
  /// [Message Contents]:   MessageContents
  /// [Data Message]:       MessageContents::DataMessage
  /// [Select.req]:         MessageContents::SelectRequest
  /// [Select.rsp]:         MessageContents::SelectResponse
  /// [Deselect.req]:       MessageContents::DeselectRequest
  /// [Deselect.rsp]:       MessageContents::DeselectResponse
  /// [Linktest.req]:       MessageContents::LinktestRequest
  /// [Linktest.rsp]:       MessageContents::LinktestResponse
  /// [Reject.req]:         MessageContents::RejectRequest
  /// [Separate.req]:       MessageContents::SeparateRequest
  /// [Client]:             Client
  /// [Connect Procedure]:  Client::connect
  /// [Select Procedure]:   Client::select
  /// [Data Procedure]:     Client::data
  /// [Deselect Procedure]: Client::deselect
  /// [Linktest Procedure]: Client::linktest
  /// [Separate Procedure]: Client::separate
  /// [Reject Procedure]:   Client::reject
  /// [Selection State]:    SelectionState
  /// [NOT SELECTED]:       SelectionState::NotSelected
  /// [SELECTED]:           SelectionState::Selected
  /// [SELECT INITIATED]:   SelectionState::SelectInitiated
  /// [DESELECT INITIATED]: SelectionState::DeselectInitiated
  fn receive(
    self: &Arc<Self>,
    rx_receiver: Receiver<primitive::Message>,
    rx_sender: Sender<(MessageID, semi_e5::Message)>,
  ) {
    for primitive_message in rx_receiver {
      let primitive_header = primitive_message.header;
      match Message::try_from(primitive_message) {
        Ok(rx_message) => match rx_message.contents {
          // RX: Data Message
          MessageContents::DataMessage(data) => {
            match self.selection_state.load(Relaxed) {
              // IS: SELECTED
              SelectionState::Selected => {
                // RX: Primary Data Message
                if data.function % 2 == 1 {
                  // INBOX: New Transaction
                  if rx_sender.send((rx_message.id, data)).is_err() {break}
                }
                // RX: Response Data Message
                else {
                  // OUTBOX: Find Transaction
                  let mut outbox = self.outbox.lock().unwrap();
                  let mut optional_transaction: Option<u32> = None;
                  for (outbox_id, (message_id, _)) in outbox.deref() {
                    if *message_id == rx_message.id {
                      optional_transaction = Some(*outbox_id);
                      break;
                    }
                  }
                  // OUTBOX: Transaction Found
                  if let Some(transaction) = optional_transaction {
                    // OUTBOX: Complete Transaction
                    let (_, sender) = outbox.deref_mut().remove(&transaction).unwrap();
                    sender.send(Some(Message{
                      id: rx_message.id,
                      contents: MessageContents::DataMessage(data),
                    })).unwrap();
                  }
                  // OUTBOX: Transaction Not Found
                  else {
                    // TX: Reject.req 
                    if self.primitive_client.transmit(Message {
                      id: rx_message.id,
                      contents: MessageContents::RejectRequest(0, RejectReason::TransactionNotOpen as u8)
                    }.into()).is_err() {break}
                  }
                }
              },
              // IS: NOT SELECTED
              _ => {
                // TX: Reject.req
                if self.primitive_client.transmit(Message {
                  id: rx_message.id,
                  contents: MessageContents::RejectRequest(0, RejectReason::EntityNotSelected as u8)
                }.into()).is_err() {break}
              },
            }
          },
          // RX: Select.req
          MessageContents::SelectRequest => {
            match self.selection_mutex.try_lock() {
              Ok(_guard) => {
                match self.selection_state.load(Relaxed) {
                  // IS: NOT SELECTED
                  SelectionState::NotSelected => {
                    // TX: Select.rsp Success
                    if self.primitive_client.transmit(Message {
                      id: rx_message.id,
                      contents: MessageContents::SelectResponse(SelectStatus::Success as u8),
                    }.into()).is_err() {break};
                    // TO: SELECTED
                    self.selection_state.store(SelectionState::Selected, Relaxed);
                  },
                  // IS: SELECTED
                  SelectionState::Selected => {
                    // TX: Select.rsp Already Active
                    if self.primitive_client.transmit(Message {
                      id: rx_message.id,
                      contents: MessageContents::SelectResponse(SelectStatus::AlreadyActive as u8),
                    }.into()).is_err() {break};
                  },
                  // IS: SELECT INITIATED
                  // TODO: Find way to reimplement this under the current scheme.
                  /*SelectionState::SelectInitiated(session_id) => {
                    // RX: Valid Simultaneous Select
                    if rx_message.id.session == *session_id {
                      // TX: Select.rsp Success
                      if self.primitive_client.transmit(HsmsMessage {
                        id: rx_message.id,
                        contents: HsmsMessageContents::SelectResponse(SelectStatus::Success as u8),
                      }.into()).is_err() {break};
                    }
                    // RX: Invalid Simultaneous Select
                    else {
                      // TX: Select.rsp Already Active
                      if self.primitive_client.transmit(HsmsMessage {
                        id: rx_message.id,
                        contents: HsmsMessageContents::SelectResponse(SelectStatus::AlreadyActive as u8),
                      }.into()).is_err() {break};
                    }
                  },*/
                }
              },
              Err(_) => {
                // Todo: probably appropriate to put something here, maybe to do with the simulatenous select procedure?
              },
            }
          },
          // RX: Select.rsp
          MessageContents::SelectResponse(select_status) => {
            // OUTBOX: Find Transaction
            let mut outbox = self.outbox.lock().unwrap();
            let mut optional_transaction: Option<u32> = None;
            for (outbox_id, (message_id, _)) in outbox.deref() {
              if *message_id == rx_message.id {
                optional_transaction = Some(*outbox_id);
                break;
              }
            }
            // OUTBOX: Transaction Found
            if let Some(transaction) = optional_transaction {
              // OUTBOX: Complete Transaction
              let (_, sender) = outbox.deref_mut().remove(&transaction).unwrap();
              sender.send(Some(Message{
                id: rx_message.id,
                contents: MessageContents::SelectResponse(select_status),
              })).unwrap();
            }
            // OUTBOX: Transaction Not Found
            else {
              // TX: Reject.req
              if self.primitive_client.transmit(Message {
                id: rx_message.id,
                contents: MessageContents::RejectRequest(0, RejectReason::TransactionNotOpen as u8)
              }.into()).is_err() {break}
            }
          },
          // RX: Deselect.req
          MessageContents::DeselectRequest => {
            todo!()
          },
          // RX: Deselect.rsp
          MessageContents::DeselectResponse(_deselect_status) => {
            todo!()
          },
          // RX: Linktest.req
          MessageContents::LinktestRequest => {
            // TX: Linktest.rsp
            if self.primitive_client.transmit(Message{
              id: rx_message.id,
              contents: MessageContents::LinktestResponse,
            }.into()).is_err() {break};
          },
          // RX: Linktest.rsp
          MessageContents::LinktestResponse => {
            // OUTBOX: Find Transaction
            let mut outbox = self.outbox.lock().unwrap();
            let mut optional_transaction: Option<u32> = None;
            for (outbox_id, (message_id, _)) in outbox.deref() {
              if *message_id == rx_message.id {
                optional_transaction = Some(*outbox_id);
                break;
              }
            }
            // OUTBOX: Transaction Found
            if let Some(transaction) = optional_transaction {
              // OUTBOX: Complete Transaction
              let (_, sender) = outbox.deref_mut().remove(&transaction).unwrap();
              sender.send(Some(rx_message)).unwrap();
            }
            // OUTBOX: Transaction Not Found
            else {
              // TX: Reject.req
              if self.primitive_client.transmit(Message {
                id: rx_message.id,
                contents: MessageContents::RejectRequest(SessionType::LinktestRequest as u8, RejectReason::TransactionNotOpen as u8),
              }.into()).is_err() {break}
            }
          },
          // RX: Reject.req
          MessageContents::RejectRequest(_message_type, _reason_code) => {
            // OUTBOX: Find Transaction
            let mut outbox = self.outbox.lock().unwrap();
            let mut optional_transaction: Option<u32> = None;
            for (outbox_id, (message_id, _)) in outbox.deref() {
              if *message_id == rx_message.id {
                optional_transaction = Some(*outbox_id);
                break;
              }
            }
            // OUTBOX: Transaction Found
            if let Some(transaction) = optional_transaction {
              // OUTBOX: Reject Transaction
              let (_, sender) = outbox.deref_mut().remove(&transaction).unwrap();
              sender.send(None).unwrap();
            }
          },
          // RX: Separate.req
          MessageContents::SeparateRequest => {
            let _guard: std::sync::MutexGuard<'_, ()> = self.selection_mutex.lock().unwrap();
            if let SelectionState::Selected = self.selection_state.load(Relaxed) {
              self.selection_state.store(SelectionState::NotSelected, Relaxed);
            }
          },
        },
        Err(reject_reason) => {
          // TX: Reject.req
          if self.primitive_client.transmit(Message {
            id: MessageID {
              session: primitive_header.session_id,
              system: primitive_header.system,
            },
            contents: MessageContents::RejectRequest(match reject_reason {
              RejectReason::UnsupportedPresentationType => primitive_header.presentation_type,
              _ => primitive_header.session_type,
            }, reject_reason as u8),
          }.into()).is_err() {break}
        },
      }
    }
    // OUTBOX: CLEAR
    for (_, (_, sender)) in self.outbox.lock().unwrap().deref_mut().drain() {
      let _ = sender.send(None);
    }
  }

  /// ### TRANSMIT PROCEDURE
  /// **Based on SEMI E37-1109§7.2**
  /// 
  /// Serializes a [Message] and transmits it over the TCP/IP connection.
  /// If a reply is expected, this function will then wait up to the time
  /// specified for the requisite response [Message] to be recieved.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [CONNECTED] state to use this
  /// procedure.
  /// 
  /// [Message]:          Message
  /// [Connection State]: primitive::ConnectionState
  /// [NOT CONNECTED]:    primitive::ConnectionState::NotConnected
  /// [CONNECTED]:        primitive::ConnectionState::Connected
  fn transmit(
    self: &Arc<Self>,
    message: Message,
    reply_expected: bool,
    delay: Duration,
  ) -> Result<Option<Message>, Error> {
    let (receiver, system) = {
      // OUTBOX: LOCK
      let outbox_lock = if reply_expected {Some(self.deref().outbox.lock().unwrap())} else {None};
      // TX
      let message_id = message.id;
      match self.primitive_client.transmit(message.into()) {
        // TX: Success
        Ok(()) => {
          match outbox_lock {
            // REPLY NOT EXPECTED: Finish
            None => return Ok(None),
            // REPLY EXPECTED
            Some(mut outbox) => {
              // OUTBOX: Create Transaction
              let (sender, receiver) = oneshot::channel::<Option<Message>>();
              let system = {
                let mut system_guard = self.deref().system.lock().unwrap();
                let system_counter = system_guard.deref_mut();
                let system = *system_counter;
                *system_counter += 1;
                system
              };
              outbox.deref_mut().insert(system, (message_id, sender));
              (receiver, system)
            }
          }
        },
        // TX: Failure
        Err(error) => {
          // TO: NOT CONNECTED, NOT SELECTED
          let _ = self.disconnect();
          return Err(error)
        },
      }
    };
    // RX
    let rx_result = receiver.recv_timeout(delay);
    // OUTBOX: Remove Transaction
    let mut outbox = self.outbox.lock().unwrap();
    outbox.deref_mut().remove(&system);
    match rx_result {
      // RX: Success
      Ok(rx_message) => return Ok(rx_message),
      // RX: Failure
      Err(_e) => return Ok(None),
    }
  }

  /// ### DATA PROCEDURE
  /// **Based on SEMI E37-1109§7.5-7.6**
  /// 
  /// Asks the [Client] to initiate the [Data Procedure] by transmitting a
  /// [Data Message] and waiting for the corresponding response to be received
  /// if it is necessary to do so.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [CONNECTED] state and the
  /// [Selection State] must be in the [SELECTED] state to use this procedure.
  /// 
  /// When a Response [Data Message] is necessary, the [Client] will wait
  /// to receive it for the amount of time specified by [T3] before it will
  /// consider it a communications failure and initiate the
  /// [Disconnect Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Although not done within this function, a [Client] in the [CONNECTED]
  /// state will automatically respond to having received a [Data Message]
  /// based on its contents and the current [Selection State]:
  /// - [NOT SELECTED] - The [Client] will respond by transmitting a
  ///   [Reject.req] message, rejecting the [HSMS Data Procedure] and
  ///   completing the [HSMS Reject Procedure].
  /// - [SELECTED], Primary [Data Message] - The [Client] will send the
  ///   [Data Message] to the hook provided by the [Connect Procedure].
  /// - [SELECTED], Response [Data Message] - The [Client] will respond by
  ///   correllating the message to a previously sent Primary [Data Message],
  ///   finishing a previously initiated [Data Procedure] if successful,
  ///   or if unsuccessful by transmitting a [Reject.req] message, rejecting
  ///   the [Data Procedure] and completing the [Reject Procedure].
  /// 
  /// [Connection State]:     primitive::ConnectionState
  /// [CONNECTED]:            primitive::ConnectionState::Connected
  /// [Selection State]:      SelectionState
  /// [NOT SELECTED]:         SelectionState::NotSelected
  /// [SELECTED]:             SelectionState::Selected
  /// [T3]:                   ParameterSettings::t3
  /// [Client]:               Client
  /// [Connect Procedure]:    Client::connect
  /// [Disconnect Procedure]: Client::disconnect
  /// [Data Procedure]:       Client::data
  /// [Reject Procedure]:     Client::reject
  /// [Data Message]:         MessageContents::DataMessage
  /// [Reject.req]:           MessageContents::RejectRequest
  pub fn data(
    self: &Arc<Self>,
    id: MessageID,
    message: semi_e5::Message,
  ) -> JoinHandle<Result<Option<semi_e5::Message>, Error>> {
    let clone: Arc<Client> = self.clone();
    let reply_expected: bool = message.function % 2 == 1 && message.w;
    thread::spawn(move || {
      match clone.selection_state.load(Relaxed) {
        // IS: NOT SELECTED
        SelectionState::NotSelected => return Err(Error::from(ErrorKind::AlreadyExists)),
        // IS: SELECTED
        SelectionState::Selected => {
          // TX: Data Message
          match clone.transmit(
            Message {
              id,
              contents: MessageContents::DataMessage(message),
            },
            reply_expected,
            clone.parameter_settings.t3,
          )?{
            // RX: Response
            Some(rx_message) => {
              match rx_message.contents {
                // RX: Data
                MessageContents::DataMessage(data_message) => return Ok(Some(data_message)),
                // RX: Reject.req
                MessageContents::RejectRequest(_type, _reason) => return Err(Error::from(ErrorKind::PermissionDenied)),
                // RX: Unknown
                _ => return Err(Error::from(ErrorKind::InvalidData)),
              }
            },
            // RX: No Response
            None => {
              // REPLY EXPECTED
              if reply_expected {
                // TO: NOT CONNECTED
                clone.disconnect()?;
                Err(Error::from(ErrorKind::ConnectionAborted))
                // TODO: HSMS-SS does NOT disconnect when the Data Procedure fails, may require this behavior to be optional.
              }
              // REPLY NOT EXPECTED
              else {
                return Ok(None);
              }
            },
          }
        },
      }
    })
  }

  /// ### SELECT PROCEDURE
  /// **Based on SEMI E37-1109§7.3-7.4**
  /// 
  /// Asks the [Client] to initiate the [Select Procedure] by transmitting a
  /// [Select.req] message and waiting for the corresponding [Select.rsp]
  /// message to be received.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [CONNECTED] state and the
  /// [Selection State] must be in the [NOT SELECTED] state to use this
  /// procedure.
  /// 
  /// The [Client] will wait to receive the [Select.rsp] for the amount
  /// of time specified by [T6] before it will consider it a communications
  /// failure and initiate the [Disconnect Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Although not done within this function, a [Client] in the [CONNECTED]
  /// state will automatically respond to having received a [Select.req]
  /// message based on its current [Selection State]:
  /// - [NOT SELECTED] - The [Client] will respond with a [Select.rsp]
  ///   accepting and completing the [Select Procedure].
  /// - [SELECTED] - The [Client] will respond with a [Select.rsp] message
  ///   rejecting the [Select Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Upon completion of the [Select Procedure], the [SELECTED] state
  /// is entered.
  /// 
  /// [Connection State]:     primitive::ConnectionState
  /// [CONNECTED]:            primitive::ConnectionState::Connected
  /// [Selection State]:      SelectionState
  /// [NOT SELECTED]:         SelectionState::NotSelected
  /// [SELECTED]:             SelectionState::Selected
  /// [T6]:                   ParameterSettings::t6
  /// [Client]:               Client
  /// [Disconnect Procedure]: Client::disconnect
  /// [Select Procedure]:     Client::select
  /// [Select.req]:           MessageContents::SelectRequest
  /// [Select.rsp]:           MessageContents::SelectResponse
  pub fn select(
    self: &Arc<Self>,
    id: MessageID,
  ) -> JoinHandle<Result<(), Error>> {
    let clone: Arc<Client> = self.clone();
    thread::spawn(move || {
      'disconnect: {
        let _guard = clone.selection_mutex.lock();
        match clone.selection_state.load(Relaxed) {
          SelectionState::NotSelected => {
            // TX: Select.req
            match clone.transmit(
              Message {
                id,
                contents: MessageContents::SelectRequest,
              },
              true,
              clone.parameter_settings.t6,
            )?{
              // RX: Response
              Some(rx_message) => {
                match rx_message.contents {
                  // RX: Select.rsp
                  MessageContents::SelectResponse(select_status) => {
                    // RX: Select.rsp Success
                    if select_status == SelectStatus::Success as u8 {
                      // TO: SELECTED
                      clone.selection_state.store(SelectionState::Selected, Relaxed);
                      return Ok(())
                    }
                    // RX: Select.rsp Failure
                    else {
                      return Err(Error::from(ErrorKind::PermissionDenied))
                    }
                  },
                  // RX: Reject.req
                  MessageContents::RejectRequest(_type, _reason) => return Err(Error::from(ErrorKind::PermissionDenied)),
                  // RX: Unknown
                  _ => return Err(Error::from(ErrorKind::InvalidData)),
                }
              },
              // RX: No Response
              None => {
                // TO: NOT CONNECTED, NOT SELECTED
                break 'disconnect;
              },
            }
          },
          SelectionState::Selected => {
            return Err(Error::from(ErrorKind::AlreadyExists))
          },
        }
      }
      clone.disconnect()?;
      Err(Error::from(ErrorKind::ConnectionAborted))
    })
  }

  /// ### DESELECT PROCEDURE (TODO)
  /// **Based on SEMI E37-1109§7.7**
  /// 
  /// Asks the [Client] to initiate the [Deselect Procedure] by transmitting a
  /// [Deselect.req] message and waiting for the corresponding [Deselect.rsp]
  /// message to be received.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [CONNECTED] state and the
  /// [Selection State] must be in the [SELECTED] state to use this procedure.
  /// 
  /// The [Client] will wait to receive the [Deselect.rsp] for the amount of
  /// time specified by [T6] before it will consider it a communications
  /// failure and initiate the [Disconnect Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Although not done within this function, a [Client] in the [CONNECTED]
  /// state will automatically respond to having received a [Deselect.req]
  /// message based on its current [Selection State]:
  /// - [NOT SELECTED] - The [Client] will respond with a [Deselect.rsp]
  ///   rejecting the [Deselect Procedure].
  /// - [SELECTED] - The [Client] will respond with a [Deselect.rsp] accepting
  ///   and completing the [Deselect Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Upon completion of the [Deselect Procedure], the [NOT SELECTED] state is
  /// entered.
  /// 
  /// [Connection State]:     primitive::ConnectionState
  /// [CONNECTED]:            primitive::ConnectionState::Connected
  /// [Selection State]:      SelectionState
  /// [NOT SELECTED]:         SelectionState::NotSelected
  /// [SELECTED]:             SelectionState::Selected
  /// [T6]:                   ParameterSettings::t6
  /// [Client]:               Client
  /// [Disconnect Procedure]: Client::disconnect
  /// [Deselect Procedure]:   Client::deselect
  /// [Deselect.req]:         MessageContents::DeselectRequest
  /// [Deselect.rsp]:         MessageContents::DeselectResponse
  pub fn deselect(
    self: &Arc<Self>,
  ) -> Result<(), Error> {
    todo!()
  }

  /// ### LINKTEST PROCEDURE
  /// **Based on SEMI E37-1109§7.8**
  /// 
  /// Asks the [Client] to initiate the [Linktest Procedure] by transmitting a
  /// [Linktest.req] message and waiting for the corresponding [Linktest.rsp]
  /// message to be received.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [CONNECTED] state to use this
  /// procedure.
  /// 
  /// The [Client] will wait to receive the [Linktest.rsp] for the amount of
  /// time specified by [T6] before it will consider it a communications
  /// failure and initiate the [Disconnect Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Although not done within this function, a [Client] in the
  /// [CONNECTED] state will automatically respond to having received a
  /// [Linktest.req] message:
  /// - The [Client] will respond with a [Linktest.rsp], completing the
  ///   [Linktest Procedure].
  /// 
  /// [Connection State]:     primitive::ConnectionState
  /// [CONNECTED]:            primitive::ConnectionState::Connected
  /// [Selection State]:      SelectionState
  /// [NOT SELECTED]:         SelectionState::NotSelected
  /// [SELECTED]:             SelectionState::Selected
  /// [T6]:                   ParameterSettings::t6
  /// [Client]:               Client
  /// [Disconnect Procedure]: Client::disconnect
  /// [Linktest Procedure]:   Client::linktest
  /// [Linktest.req]:         MessageContents::LinktestRequest
  /// [Linktest.rsp]:         MessageContents::LinktestResponse
  pub fn linktest(
    self: &Arc<Self>,
    system: u32,
  ) -> JoinHandle<Result<(), Error>> {
    let clone: Arc<Client> = self.clone();
    thread::spawn(move || {
      // TX: Linktest.req
      match clone.transmit(
        Message {
          id: MessageID {
            session: 0xFFFF,
            system,
          },
          contents: MessageContents::LinktestRequest,
        },
        true,
        clone.parameter_settings.t6,
      )?{
        // RX: Response
        Some(rx_message) => {
          match rx_message.contents {
            // RX: Linktest.rsp
            MessageContents::LinktestResponse => Ok(()),
            // RX: Reject.req
            MessageContents::RejectRequest(_type, _reason) => Err(Error::from(ErrorKind::PermissionDenied)),
            // RX: Unknown
            _ => Err(Error::from(ErrorKind::InvalidData)),
          }
        },
        // RX: No Response
        None => {
          // TO: NOT CONNECTED, NOT SELECTED
          clone.disconnect()?;
          Err(Error::from(ErrorKind::ConnectionAborted))
        },
      }
    })
  }

  /// ### SEPARATE PROCEDURE
  /// **Based on SEMI E37-1109§7.9**
  /// 
  /// Asks the [Client] to initiate the [Separate Procedure] by transmitting a
  /// [Separate.req] message.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [CONNECTED] state and the
  /// [Selection State] must be in the [SELECTED] state to use this procedure.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Although not done within this function, a [Client] in the [CONNECTED]
  /// state will automatically respond to having received a [Separate.req]
  /// message based on its current [Selection State]:
  /// - [NOT SELECTED] - The [Client] will not do anything.
  /// - [SELECTED] - The [Client] will complete the [Separate Procedure].
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Upon completion of the [Separate Procedure], the [NOT SELECTED] state is
  /// entered.
  /// 
  /// [Connection State]:   primitive::ConnectionState
  /// [CONNECTED]:          primitive::ConnectionState::Connected
  /// [Selection State]:    SelectionState
  /// [NOT SELECTED]:       SelectionState::NotSelected
  /// [SELECTED]:           SelectionState::Selected
  /// [Client]:             Client
  /// [Separate Procedure]: Client::separate
  /// [Separate.req]:       MessageContents::SeparateRequest
  pub fn separate(
    self: &Arc<Self>,
    id: MessageID,
  ) -> JoinHandle<Result<(), Error>> {
    let clone: Arc<Client> = self.clone();
    thread::spawn(move || {
      let _guard = clone.selection_mutex.lock().unwrap();
      match clone.selection_state.load(Relaxed) {
        // IS: NOT SELECTED
        SelectionState::NotSelected => {
          Err(Error::from(ErrorKind::PermissionDenied))
        },
        // IS: SELECTED
        SelectionState::Selected => {
          // TX: Separate.req
          clone.transmit(
            Message {
              id,
              contents: MessageContents::SeparateRequest,
            },
            false,
            clone.parameter_settings.t6,
          )?;
          // TO: NOT SELECTED
          clone.selection_state.store(SelectionState::NotSelected, Relaxed);
          Ok(())
        },
      }
    })
  }

  /// ### REJECT PROCEDURE (TODO)
  /// **Based on SEMI E37-1109§7.10**
  /// 
  /// Asks the [Client] to initiate the [Reject Procedure] by transmitting a
  /// [Reject.req] message.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// The [Connection State] must be in the [CONNECTED] state to use this
  /// procedure.
  /// 
  /// -------------------------------------------------------------------------
  /// 
  /// Although not done within this function, a [Client] in the [CONNECTED]
  /// state will automatically respond to having received a [Reject.req]:
  /// - Not yet implemented.
  /// 
  /// [Connection State]: primitive::ConnectionState
  /// [CONNECTED]:        primitive::ConnectionState::Connected
  /// [Selection State]:  SelectionState
  /// [NOT SELECTED]:     SelectionState::NotSelected
  /// [SELECTED]:         SelectionState::Selected
  /// [Client]:           Client
  /// [Reject Procedure]: Client::reject
  /// [Reject.req]:       MessageContents::RejectRequest
  pub fn reject(
    self: &Arc<Self>,
    _reason: RejectReason,
  ) -> Result<(), Error> {
    todo!()
  }
}

/// ## SELECTION STATE
/// **Based on SEMI E37-1109§5.5.2**
/// 
/// The [CONNECTED] state has two substates, [NOT SELECTED] and [SELECTED].
/// 
/// The [Client] moves between them based on whether it has established
/// a session with another entity according to the [Select Procedure],
/// [Deselect Procedure], and [Separate Procedure].
/// 
/// [CONNECTED]:          primitive::ConnectionState::Connected
/// [NOT SELECTED]:       SelectionState::NotSelected
/// [SELECTED]:           SelectionState::Selected
/// [Client]:             Client
/// [Select Procedure]:   Client::select
/// [Deselect Procedure]: Client::deselect
/// [Separate Procedure]: Client::separate
#[derive(Clone, Copy, Debug, PartialEq, NoUninit)]
#[repr(u8)]
pub enum SelectionState {
  /// ### NOT SELECTED
  /// **Based on SEMI E37-1109§5.5.2.1**
  /// 
  /// In this state, the [Client] is ready to initiate the [Select Procedure]
  /// but has either not yet done so, or has terminated a previous session.
  /// 
  /// [Client]:           Client
  /// [Select Procedure]: Client::select
  NotSelected,

  /// ### SELECTED
  /// **Based on SEMI E37-1109§5.5.2.2**
  /// 
  /// In this state, the [Client] has successfully initiated the
  /// [Select Procedure] and is able to send and receive [Data Message]s.
  /// 
  /// [Client]:           Client
  /// [Select Procedure]: Client::select
  /// [Data Message]:     MessageContents::DataMessage
  Selected,
}
impl Default for SelectionState {
  /// ### DEFAULT SELECTION STATE
  /// **Based on SEMI E37-1109§5.4**
  /// 
  /// Provides the [NOT SELECTED] state by default.
  /// 
  /// [NOT SELECTED]: SelectionState::NotSelected
  fn default() -> Self {
    SelectionState::NotSelected
  }
}

/// ## PARAMETER SETTINGS
/// **Based on SEMI E37-1109§10.2**
/// 
/// The required set of paramters which an [HSMS] implementation must provide,
/// and which the [Client] will abide by.
/// 
/// [HSMS]:   crate
/// [Client]: Client
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ParameterSettings {
  /// ### CONNECT MODE
  /// 
  /// Specifies the [Connection Mode] the [Client] will provide to
  /// the [Primitive Client] to use when performing the [Connect Procedure]:
  /// [PASSIVE] to wait for an incoming connection, or [ACTIVE] to initiate
  /// an outgoing connection.
  /// 
  /// [Primitive Client]:  primitive::Client
  /// [Client]:            Client
  /// [Connect Procedure]: Client::connect
  /// [Connection Mode]:   ConnectionMode
  /// [PASSIVE]:           ConnectionMode::Passive
  /// [ACTIVE]:            ConnectionMode::Active
  pub connect_mode: ConnectionMode,

  /// ### T3: REPLY TIMEOUT
  /// 
  /// The maximum amount of time that the [Client] will wait after sending
  /// a Primary [Data Message] to receive the appropriate Response
  /// [Data Message] before it must initiate the [Disconnect Procedure].
  /// 
  /// [Client]:               Client
  /// [Disconnect Procedure]: Client::disconnect
  /// [Data Message]:         MessageContents::DataMessage
  pub t3: Duration,

  /// ### T5: CONNECTION SEPARATION TIMEOUT
  /// 
  /// The minimum amount of time that the [Client] must wait between successive
  /// attempts to initiate the [Connect Procedure] with a [Connect Mode] of
  /// [ACTIVE].
  /// 
  /// [Client]:            Client
  /// [Connect Procedure]: Client::connect
  /// [Connect Mode]:      ParameterSettings::connect_mode
  /// [ACTIVE]:            ConnectionMode::Active
  pub t5: Duration,

  /// ### T6: CONTROL TRANSACTION TIMEOUT
  /// 
  /// The maximum amount of time that the [Client] will wait after sending a
  /// [Select Request], [Deselect Request], or [Linktest Request] to receive
  /// the appropriate [Select Response], [Deselect Response], or
  /// [Linktest Response] before it must initiate the [Disconnect Procedure].
  /// 
  /// [Client]:               Client
  /// [Disconnect Procedure]: Client::disconnect
  /// [Select Request]:       MessageContents::SelectRequest
  /// [Select Response]:      MessageContents::SelectResponse
  /// [Deselect Request]:     MessageContents::DeselectRequest
  /// [Deselect Response]:    MessageContents::DeselectResponse
  /// [Linktest Request]:     MessageContents::LinktestRequest
  /// [Linktest Response]:    MessageContents::LinktestResponse
  pub t6: Duration,

  /// ### T7: NOT SELECTED TIMEOUT
  /// 
  /// The maximum amount of time that the [Client] will wait after being
  /// placed in the [NOT SELECTED] state before it must initiate the
  /// [Disconnect Procedure].
  /// 
  /// [Client]:               Client
  /// [Disconnect Procedure]: Client::disconnect
  /// [NOT SELECTED]:         SelectionState::NotSelected
  pub t7: Duration,

  /// ### T8: NETWORK INTERCHARACTER TIMEOUT
  /// 
  /// The amount of time that the [Client] will provide to the
  /// [Primitive Client] to use as the maximum amount of time it may wait while
  /// sending or receiving data between successive characters in the same
  /// [Primitive Message] before it must initiate the [Disconnect Procedure].
  /// 
  /// [Primitive Client]:     primitive::Client
  /// [Disconnect Procedure]: primitive::Client::disconnect
  /// [Primitive Message]:    primitive::Message
  /// [Client]:               Client
  pub t8: Duration,
}
impl Default for ParameterSettings {
  /// ### DEFAULT PARAMETER SETTINGS
  /// **Based on SEMI E37-1109§10.2**
  /// 
  /// Provides [Parameter Settings] with these values, with timeouts as shown
  /// in the 'typical values' column in Table 10.
  /// 
  /// - [Connect Mode] of [PASSIVE]
  /// - [T3] of 45 seconds
  /// - [T5] of 10 seconds
  /// - [T6] of 5 seconds
  /// - [T7] of 10 seconds
  /// - [T8] of 5 seconds
  /// 
  /// [Parameter Settings]: ParameterSettings
  /// [PASSIVE]:            ConnectionMode::Passive
  /// [Connect Mode]:       ParameterSettings::connect_mode
  /// [T3]:                 ParameterSettings::t3
  /// [T5]:                 ParameterSettings::t5
  /// [T6]:                 ParameterSettings::t6
  /// [T7]:                 ParameterSettings::t7
  /// [T8]:                 ParameterSettings::t8
  fn default() -> Self {
    Self {
      connect_mode: ConnectionMode::default(),
      t3: Duration::from_secs(45),
      t5: Duration::from_secs(10),
      t6: Duration::from_secs(5),
      t7: Duration::from_secs(10),
      t8: Duration::from_secs(5),
    }
  }
}

/// ## MESSAGE
/// **Based on SEMI E37-1109§8.2-8.3**
/// 
/// Data using the structure defined by the [Generic Services], enforcing
/// compliance as determined by a [Presentation Type] of 0, broken down into
/// its [Message ID] and [Message Contents].
/// 
/// [Generic Services]:  crate::generic
/// [Presentation Type]: PresentationType
/// [Message ID]:        MessageID
/// [Message Contents]:  MessageContents
#[derive(Clone, Debug)]
pub struct Message {
  pub id: MessageID,
  pub contents: MessageContents,
}
impl From<Message> for primitive::Message {
  /// ### PRIMITIVE MESSAGE FROM GENERIC MESSAGE
  /// 
  /// Due to the fact that valid [Generic Message]s are a subset of valid
  /// [Primitive Message]s, this operation is infallible.
  /// 
  /// [Generic Message]:   Message
  /// [Primitive Message]: primitive::Message
  fn from(message: Message) -> Self {
    match message.contents {
      MessageContents::DataMessage(e5_message) => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : message.id.session,
            byte_2            : ((e5_message.w as u8) << 7) | e5_message.stream,
            byte_3            : e5_message.function,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::DataMessage as u8,
            system            : message.id.system,
          },
          text: match e5_message.text {
            Some(item) => Vec::<u8>::from(item),
            None => vec![],
          },
        }
      },
      MessageContents::SelectRequest => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : message.id.session,
            byte_2            : 0,
            byte_3            : 0,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::SelectRequest as u8,
            system            : message.id.system,
          },
          text: vec![],
        }
      },
      MessageContents::SelectResponse(select_status) => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : message.id.session,
            byte_2            : 0,
            byte_3            : select_status,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::SelectResponse as u8,
            system            : message.id.system,
          },
          text: vec![],
        }
      },
      MessageContents::DeselectRequest => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : message.id.session,
            byte_2            : 0,
            byte_3            : 0,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::DeselectRequest as u8,
            system            : message.id.system,
          },
          text: vec![],
        }
      },
      MessageContents::DeselectResponse(deselect_status) => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : message.id.session,
            byte_2            : 0,
            byte_3            : deselect_status,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::DeselectResponse as u8,
            system            : message.id.system,
          },
          text: vec![],
        }
      },
      MessageContents::LinktestRequest => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : 0xFFFF,
            byte_2            : 0,
            byte_3            : 0,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::LinktestRequest as u8,
            system            : message.id.system,
          },
          text: vec![],
        }
      },
      MessageContents::LinktestResponse => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : 0xFFFF,
            byte_2            : 0,
            byte_3            : 0,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::LinktestResponse as u8,
            system            : message.id.system,
          },
          text: vec![],
        }
      },
      MessageContents::RejectRequest(message_type, reason_code) => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : message.id.session,
            byte_2            : message_type,
            byte_3            : reason_code,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::RejectRequest as u8,
            system            : message.id.system,
          },
          text: vec![],
        }
      },
      MessageContents::SeparateRequest => {
        primitive::Message {
          header: primitive::MessageHeader {
            session_id        : message.id.session,
            byte_2            : 0,
            byte_3            : 0,
            presentation_type : PresentationType::SecsII as u8,
            session_type      : SessionType::SeparateRequest as u8,
            system            : message.id.system,
          },
          text: vec![],
        }
      },
    }
  }
}
impl TryFrom<primitive::Message> for Message {
  type Error = RejectReason;

  /// ## GENERIC MESSAGE FROM PRIMITIVE MESSAGE
  /// 
  /// Due to the fact that valid [Generic Message]s are a subset of valid
  /// [Primitive Message]s, this operation is fallable when the
  /// [Primitive Message] is not a [Generic Message].
  /// 
  /// [Generic Message]:   Message
  /// [Primitive Message]: primitive::Message
  fn try_from(message: primitive::Message) -> Result<Self, Self::Error> {
    if message.header.presentation_type != 0 {return Err(RejectReason::UnsupportedPresentationType)}
    Ok(Message {
      id: MessageID {
        session: message.header.session_id,
        system: message.header.system,
      },
      contents: match message.header.session_type {
        0 => {
          MessageContents::DataMessage(semi_e5::Message{
            stream   : message.header.byte_2 & 0b0111_1111,
            function : message.header.byte_3,
            w        : message.header.byte_2 & 0b1000_0000 > 0,
            text     : match semi_e5::Item::try_from(message.text) {
              // Valid Item
              Ok(text) => Some(text),
              // Invalid Item
              Err(error) => {
                match error {
                  // Empty Text: Considered Valid Here
                  semi_e5::Error::EmptyText => {None},
                  // Other Error: Malformed Data
                  _ => {return Err(RejectReason::MalformedData)}
                }
              },
            },
          })
        },
        1 => {
          if message.header.byte_2 != 0 {return Err(RejectReason::MalformedData)}
          if message.header.byte_3 != 0 {return Err(RejectReason::MalformedData)}
          if !message.text.is_empty()   {return Err(RejectReason::MalformedData)}
          MessageContents::SelectRequest
        },
        2 => {
          if message.header.byte_2 != 0 {return Err(RejectReason::MalformedData)}
          if !message.text.is_empty()   {return Err(RejectReason::MalformedData)}
          MessageContents::SelectResponse(message.header.byte_3)
        },
        3 => {
          if message.header.byte_2 != 0 {return Err(RejectReason::MalformedData)}
          if message.header.byte_3 != 0 {return Err(RejectReason::MalformedData)}
          if !message.text.is_empty()   {return Err(RejectReason::MalformedData)}
          MessageContents::DeselectRequest
        },
        4 => {
          if message.header.byte_2 != 0 {return Err(RejectReason::MalformedData)}
          if !message.text.is_empty()   {return Err(RejectReason::MalformedData)}
          MessageContents::DeselectResponse(message.header.byte_3)
        },
        5 => {
          if message.header.session_id != 0xFFFF {return Err(RejectReason::MalformedData)}
          if message.header.byte_2     != 0      {return Err(RejectReason::MalformedData)}
          if message.header.byte_3     != 0      {return Err(RejectReason::MalformedData)}
          if !message.text.is_empty()            {return Err(RejectReason::MalformedData)}
          MessageContents::LinktestRequest
        },
        6 => {
          if message.header.session_id != 0xFFFF {return Err(RejectReason::MalformedData)}
          if message.header.byte_2     != 0      {return Err(RejectReason::MalformedData)}
          if message.header.byte_3     != 0      {return Err(RejectReason::MalformedData)}
          if !message.text.is_empty()            {return Err(RejectReason::MalformedData)}
          MessageContents::LinktestResponse
        },
        7 => {
          if !message.text.is_empty() {return Err(RejectReason::MalformedData)}
          MessageContents::RejectRequest(message.header.byte_2, message.header.byte_3)
        },
        9 => {
          if message.header.byte_2 != 0 {return Err(RejectReason::MalformedData)}
          if message.header.byte_3 != 0 {return Err(RejectReason::MalformedData)}
          if !message.text.is_empty()   {return Err(RejectReason::MalformedData)}
          MessageContents::SeparateRequest
        },
        _ => {return Err(RejectReason::UnsupportedSessionType)}
      },
    })
  }
}

/// ## MESSAGE ID
/// **Based on SEMI E37-1109§8.2**
/// 
/// The uniquely identifying components of a [Message] in forming a valid
/// transaction, including the [Session ID] and [System Bytes].
/// 
/// [Message]:      Message
/// [Session ID]:   MessageID::session
/// [System Bytes]: MessageID::system
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MessageID {
  /// ### SESSION ID
  /// **Based on SEMI E37-1109§8.2.6.1**
  /// 
  /// Provides an association between [Message]s across multiple
  /// transactions, particularly to link the [Select Procedure] and
  /// [Deselect Procedure] to subsequent [Data Message]s.
  /// 
  /// [Select Procedure]:   Client::select
  /// [Deselect Procedure]: Client::deselect
  /// [Message]:            Message
  /// [Data Message]:       MessageContents::DataMessage
  pub session: u16,

  /// ### SYSTEM BYTES
  /// **Based on SEMI E37-1109§8.2.6.7**
  /// 
  /// Identifies a transaction uniquely among the set of open transactions.
  pub system: u32,
}

/// ## MESSAGE CONTENTS
/// **Based on SEMI E37-1109§8.3.1-8.3.21**
/// 
/// The contents of a [Message], broken down by its [Session Type]:
/// 
/// - [SECS-II] formatted [Data Message]
/// - [Select.req]
/// - [Select.rsp]
/// - [Deselect.req]
/// - [Deselect.rsp]
/// - [Linktest.req]
/// - [Linktest.rsp]
/// - [Reject.req]
/// - [Separate.req]
/// 
/// [SECS-II]:      semi_e5
/// [Message]:      Message
/// [Session Type]: SessionType
/// [Data Message]: MessageContents::DataMessage
/// [Select.req]:   MessageContents::SelectRequest
/// [Select.rsp]:   MessageContents::SelectResponse
/// [Deselect.req]: MessageContents::DeselectRequest
/// [Deselect.rsp]: MessageContents::DeselectResponse
/// [Linktest.req]: MessageContents::LinktestRequest
/// [Linktest.rsp]: MessageContents::LinktestResponse
/// [Reject.req]:   MessageContents::RejectRequest
/// [Separate.req]: MessageContents::SeparateRequest
#[repr(u8)]
#[derive(Clone, Debug)]
pub enum MessageContents {
  /// ## DATA MESSAGE
  /// **Based on SEMI E37-1109§8.3.1-8.3.3**
  /// 
  /// A [Message] with a [Session Type] of 0, used by the initiator of or
  /// responding entity in the [Data Procedure] to send data.
  /// 
  /// Contains [SECS-II] formatted data.
  /// 
  /// [SECS-II]:        semi_e5
  /// [Message]:        Message
  /// [Session Type]:   SessionType
  /// [Data Procedure]: Client::data
  DataMessage(semi_e5::Message) = SessionType::DataMessage as u8,

  /// ## SELECT REQUEST
  /// **Based on SEMI E37-1109§8.3.4**
  /// 
  /// A [Message] with a [Session Type] of 1, used by the initiator of the
  /// [Select Procedure] for establishing communications.
  /// 
  /// [Message]:          Message
  /// [Select Procedure]: Client::select
  /// [Session Type]:     SessionType
  SelectRequest = SessionType::SelectRequest as u8,

  /// ## SELECT RESPONSE
  /// **Based on SEMI E37-1109§8.3.5-8.3.7**
  /// 
  /// A [Message] with a [Session Type] of 2, used by the responding
  /// entity in the [Select Procedure].
  /// 
  /// Contains a [Select Status], indicating the success or failure mode of
  /// the [Select Procedure].
  /// 
  /// [Message]:          Message
  /// [Select Procedure]: Client::select
  /// [Session Type]:     SessionType
  /// [Select Status]:    SelectStatus
  SelectResponse(u8) = SessionType::SelectResponse as u8,

  /// ## DESELECT REQUEST
  /// **Based on SEMI E37-1109§8.3.8-8.3.10**
  /// 
  /// A [Message] with a [Session Type] of 3, used by the initiator of the
  /// [Deselect Procedure] for breaking communications.
  /// 
  /// [Message]:            Message
  /// [Deselect Procedure]: Client::deselect
  /// [Session Type]:       SessionType
  DeselectRequest = SessionType::DeselectRequest as u8,

  /// ## DESELECT RESPONSE
  /// **Based on SEMI E37-1109§8.3.11-8.3.13**
  /// 
  /// An [Message] with a [Session Type] of 4, used by the responding entity
  /// in the [Deselect Procedure].
  /// 
  /// Contains a [Deselect Status], indicating the success or failure mode of
  /// the [Deselect Procedure].
  /// 
  /// [Message]:            Message
  /// [Deselect Procedure]: Client::deselect
  /// [Session Type]:       SessionType
  /// [Deselect Status]:    DeselectStatus
  DeselectResponse(u8) = SessionType::DeselectResponse as u8,

  /// ## LINKTEST REQUEST
  /// **Based on SEMI E37-1109§8.3.14-8.3.16**
  /// 
  /// A [Message] with a [Session Type] of 5, used by the initiator of the
  /// [Linktest Procedure] for checking communications stability.
  /// 
  /// [Message]:            Message
  /// [Session Type]:       SessionType
  /// [Linktest Procedure]: Client::linktest
  LinktestRequest = SessionType::LinktestRequest as u8,

  /// ## LINKTEST RESPONSE
  /// **Based on SEMI E37-1109§8.3.17-8.3.19**
  /// 
  /// A [Message] with a [Session Type] of 6, used by the responding entity
  /// in the [Linktest Procedure].
  /// 
  /// [Message]:            Message
  /// [Session Type]:       SessionType
  /// [Linktest Procedure]: Client::linktest
  LinktestResponse = SessionType::LinktestResponse as u8,

  /// ## REJECT REQUEST
  /// **Based on SEMI E37-1109§8.3.20-8.3.21**
  /// 
  /// A [Message] with a [Session Type] of 7, used by the responding entity
  /// in the [Reject Procedure].
  /// 
  /// Contains the [Presentation Type] or [Session Type] of the [Message] being
  /// rejected, and the [Reason Code] indicating why the message was rejected.
  /// 
  /// [Message]:           Message
  /// [Reject Procedure]:  Client::reject
  /// [Presentation Type]: PresentationType
  /// [Session Type]:      SessionType
  /// [Reason Code]:       RejectReason
  RejectRequest(u8, u8) = SessionType::RejectRequest as u8,

  /// ## SEPARATE REQUEST
  /// **Based on SEMI E37-1109§8.3.22**
  /// 
  /// A [Message] with a [Session Type] of 9, used by the initiator of the
  /// [Separate Procedure] for breaking communications.
  /// 
  /// [Message]:            Message
  /// [Separate Procedure]: Client::separate
  /// [Session Type]:       SessionType
  SeparateRequest = SessionType::SeparateRequest as u8,
}

/// ## SESSION TYPE
/// **Based on SEMI E37-1109§8.2.6.5-8.2.6.6**
/// 
/// Defines the type of [Message] being sent.
/// 
/// Values 11-127 are reserved for Subsidiary Standards.
/// 
/// Values 8, 10, and 128-255 are reserved and may not be used.
/// 
/// [Message]: Message
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SessionType {
  /// ### DATA MESSAGE
  /// 
  /// Denotes a [SECS-II] formatted [Data Message].
  /// 
  /// [SECS-II]:      semi_e5
  /// [Data Message]: MessageContents::DataMessage 
  DataMessage = 0,

  /// ### SELECT REQUEST
  /// 
  /// Denotes a [Select.req] message.
  /// 
  /// [Select.req]: MessageContents::SelectRequest
  SelectRequest = 1,

  /// ### SELECT RESPONSE
  /// 
  /// Denotes a [Select.rsp] message.
  /// 
  /// [Select.rsp]: MessageContents::SelectResponse
  SelectResponse = 2,

  /// ### DESELECT REQUEST
  /// 
  /// Denotes a [Deselect.req] message.
  /// 
  /// [Deselect.req]: MessageContents::DeselectRequest
  DeselectRequest = 3,

  /// ### DESELECT RESPONSE
  /// 
  /// Denotes a [Deselect.rsp] message.
  /// 
  /// [Deselect.rsp]: MessageContents::DeselectResponse
  DeselectResponse = 4,

  /// ### LINKTEST REQUEST
  /// 
  /// Denotes a [Linktest.req] message.
  /// 
  /// [Linktest.req]: MessageContents::LinktestRequest
  LinktestRequest = 5,

  /// ### LINKTEST RESPONSE
  /// 
  /// Denotes a [Linktest.rsp] message.
  /// 
  /// [Linktest.rsp]: MessageContents::LinktestResponse
  LinktestResponse = 6,

  /// ### REJECT REQUEST
  /// 
  /// Denotes a [Reject.req] message.
  /// 
  /// [Reject.req]: MessageContents::RejectRequest
  RejectRequest = 7,

  /// ### SEPARATE REQUEST
  /// 
  /// Denotes a [Separate.req] message.
  /// 
  /// [Separate.req]: MessageContents::SeparateRequest
  SeparateRequest = 9,
}

/// ## SELECT STATUS
/// **Based on SEMI E37-1109§8.3.7.2**
/// 
/// [Byte 3] of a [Deselect.rsp] message, used as the indication of success or
/// reason for failure of the [Select Procedure].
/// 
/// Values 4-127 are reserved for Subsidiary Standards.
/// 
/// Values 128-255 are reserved for the Local Entity.
/// 
/// [Byte 3]:           primitive::MessageHeader::byte_3
/// [Deselect.rsp]:     MessageContents::DeselectResponse
/// [Select Procedure]: Client::select
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum SelectStatus {
  Success       = 0,
  AlreadyActive = 1,
  NotReady      = 2,
  Exhausted     = 3,
}

/// ## DESELECT STATUS
/// **Based on SEMI E37-1109§8.3.13.2**
/// 
/// [Byte 3] of a [Deselect.rsp] message, used as the indication of success or
/// reason for failure of the [Deselect Procedure].
/// 
/// Values 3-127 are reserved for Subsidiary Standards.
/// 
/// Values 128-255 are reserved for the Local Entity.
/// 
/// [Byte 3]:             primitive::MessageHeader::byte_3
/// [Deselect.rsp]:       MessageContents::DeselectResponse
/// [Deselect Procedure]: Client::deselect
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DeselectStatus {
  Success        = 0,
  NotEstablished = 1,
  Busy           = 2,
}

/// ## REJECT REASON
/// **Based on SEMI E37-1109§8.3.21.3**
/// 
/// [Byte 3] of a [Reject.req] message, specifying the reason a message has
/// been rejected in the [Reject Procedure].
/// 
/// Values 4-127 are reserved for Subsidiary Standards.
/// 
/// Values 0, and 128-255 are reserved for the Local Entity.
/// 
/// [Byte 3]:           primitive::MessageHeader::byte_3
/// [Reject.req]:       MessageContents::RejectRequest
/// [Reject Procedure]: Client::reject
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum RejectReason {
  /// ### MALFORMED DATA
  /// **Local Entity Specific Reason**
  /// 
  /// A [Message] was recieved which was valid according to the
  /// [Primitive Services] but invalid according to the [Generic Services].
  /// 
  /// [Message]:            primitive::Message
  /// [Primitive Services]: primitive
  /// [Generic Services]:   crate::generic
  MalformedData = 0,

  /// ### SESSION TYPE NOT SUPPORTED
  /// 
  /// A [Message] was received whose [Session Type] value is not allowed.
  /// 
  /// [Message]:      primitive::Message
  /// [Session Type]: SessionType
  UnsupportedSessionType = 1,

  /// ### PRESENTATION TYPE NOT SUPPORTED
  /// 
  /// A [Message] was received whose [Presentation Type] value is not allowed.
  /// 
  /// [Message]:           primitive::Message
  /// [Presentation Type]: crate::PresentationType
  UnsupportedPresentationType = 2,

  /// ### TRANSACTION NOT OPEN
  /// 
  /// A [Select.rsp], [Deselect.rsp], or [Linktest.rsp] was recieved when there
  /// was no outstanding [Select.req], [Deselect.req], or [Linktest.req] which
  /// corresponded to it.
  /// 
  /// [Select.req]:   MessageContents::SelectRequest
  /// [Select.rsp]:   MessageContents::SelectResponse
  /// [Deselect.req]: MessageContents::DeselectRequest
  /// [Deselect.rsp]: MessageContents::DeselectResponse
  /// [Linktest.req]: MessageContents::LinktestRequest
  /// [Linktest.rsp]: MessageContents::LinktestResponse
  TransactionNotOpen = 3,

  /// ### ENTITY NOT SELECTED
  /// 
  /// A [Data Message] was recieved when not in the [SELECTED] state.
  /// 
  /// [Data Message]: MessageContents::DataMessage
  /// [SELECTED]:     SelectionState::Selected
  EntityNotSelected = 4,
}