Repository navigation
Expand file tree
/
Copy pathXmodemCommunicator.cs
More file actions
240 lines (213 loc) · 9.25 KB
/
Copy pathXmodemCommunicator.cs
File metadata and controls
240 lines (213 loc) · 9.25 KB
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
namespace XModemProtocol {
using Communication;
using Environment;
using EventData;
using Exceptions;
using Operations;
using Options;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Threading;
public class XModemCommunicator {
#region Fields
IContext _context = new Context();
CancellationTokenSource _tokenSource;
#endregion
#region Constructors
public XModemCommunicator() {
_context.PacketsBuilt += (s, e) => {
PacketsBuilt?.Invoke(this, e);
};
_context.StateUpdated += (s, e) => {
StateUpdated?.Invoke(this, e);
};
_context.ModeUpdated += (s, e) => {
ModeUpdated?.Invoke(this, e);
};
}
public XModemCommunicator(SerialPort port) : this(new Communicator(port)) { }
public XModemCommunicator(ICommunicator communicator) : this() {
Communicator = communicator;
}
#endregion
#region Properties
/// <summary>
/// Polynomial used CRC lookup table. Defaulted to the polynomial
/// X^16 + X^12 + X^5 + 1 (0x1021).
/// </summary>
public int Polynomial {
get { return _context.Polynomial; }
set { _context.Polynomial = value; }
}
/// <summary>
/// SerialPort to be used to create an instance of the
/// XModemProtocol.Communication.Communicator class.
/// </summary>
public SerialPort Port {
set {
if (value == null)
_context.Communicator = null;
else
_context.Communicator = new Communicator(value);
}
}
/// <summary>
/// Accepts an instance of a class that implements
/// the XModemProtocol.Communication.ICommunicator interface.
/// Object will be used to facilitate the transfer of bytes.
/// </summary>
public ICommunicator Communicator { set { _context.Communicator = value; } }
/// <summary>
/// Data received from transfer or data to be sent.
/// </summary>
public IEnumerable<byte> Data {
set { _context.Data = value?.ToList(); }
get { return new List<byte>(_context.Data); }
}
/// <summary>
/// Returns the current state of XModemProtocol.XModemCommunicator.
/// </summary>
public XModemStates State => _context.State;
/// <summary>
/// Accepts an instance of a class that implements the
/// XModemProtocol.Options.IXModemProtocolOptions interface.
/// This contains the bytes that XModemProtocol.XModemCommunicator
/// will use to facilitate transfer along with some other options
/// to customize how XModemProtocol.XModemCommunicator operates.
/// By default, is instance of
/// XModemProtocol.Options.XModemProtocolOptions.
/// </summary>
public IXModemProtocolOptions Options {
set { _context.Options = value; }
get { return _context.Options; }
}
/// <summary>
/// Mode to be used by XModemProtocol.XModemCommunicator.
/// If using Receive operation, CRC will upgrade to OneK automatically.
/// </summary>
public XModemMode Mode {
get { return _context.Mode; }
set { _context.Mode = value; }
}
#endregion
#region Operations
/// <summary>
/// Puts XModemProtocol.XModemCommuniator in the sender role
/// awaiting initialization byte from receiver.
/// </summary>
public void Send() => PerformOperation(SendSetup);
/// <summary>
/// Puts XModemProtocol.XModemCommuniator in the receiver role
/// sending the initialization byte.
/// </summary>
public void Receive() => PerformOperation(ReceiveSetup);
/// <summary>
/// Cancels operation currently running. No effect if no operation running.
/// </summary>
public void CancelOperation() => _tokenSource?.Cancel();
#endregion
#region Events
#region Sender Only Events
/// <summary>
/// Fires asynchronously whenever XModemProtocol.XModemCommunicator finishes building packets.
/// </summary>
public event EventHandler<PacketsBuiltEventArgs> PacketsBuilt;
/// <summary>
/// Fires when XModemProtocol.XModemCommunicator is ready to send a packet.
/// A blocking method will prevent packet from being sent. Does not fire
/// when sending IXModemProtocolOptions.EOT.
/// </summary>
public event EventHandler<PacketToSendEventArgs> PacketToSend;
#endregion
#region Receiver Only Events
/// <summary>
/// Fires after a successful packet has been received by
/// XModemProtocol.XModemCommunicator. This event must complete
/// before XModemProtocol.XModemCommunicator will send
/// IXModemProtocolOptions.ACK. Does not fire when IXModemProtocolOptions.EOT is received.
/// </summary>
public event EventHandler<PacketReceivedEventArgs> PacketReceived;
#endregion
#region Shared Events
/// <summary>
/// Fires before the operation begins, and determines whether
/// operation will run or not. Will not fire if Data contains
/// no bytes, and performing Send operation.
/// </summary>
/// <returns>Whether to perform operation(true) or not(false).</returns>
public event Func<bool> OperationPending;
/// <summary>
/// Fires when the state of XModemProtocol.XModemCommunicator is updated.
/// </summary>
public event EventHandler<StateUpdatedEventArgs> StateUpdated;
/// <summary>
/// Fires when the mode of XModemProtocol.XModemCommunicator is updated.
/// </summary>
public event EventHandler<ModeUpdatedEventArgs> ModeUpdated;
/// <summary>
/// Fires if the operation is aborted. XModemProtocol.XModemCommunicator
/// will not return to being idle until event completes.
/// </summary>
public event EventHandler<AbortedEventArgs> Aborted;
/// <summary>
/// Fires the operation completes successfully.
/// XModemProtocol.XModemCommunicator will not return to
/// being idle until event completes.
/// </summary>
public event EventHandler<CompletedEventArgs> Completed;
#endregion
#endregion
#region Support Methods
private void SendCancel() => _context.SendCancel();
private IOperation SendSetup() {
IOperation operation = new SendOperation();
operation.PacketToSend += (s, e) => PacketToSend?.Invoke(this, e);
if (_context.Data == null || _context.Data.Count == 0)
throw new XModemProtocolException(new AbortedEventArgs(XModemAbortReason.BufferEmpty));
return operation;
}
private IOperation ReceiveSetup() {
IOperation operation = new ReceiveOperation();
operation.PacketReceived += (s, e) => PacketReceived?.Invoke(this, e);
_context.Data = new List<byte>();
return operation;
}
private void PerformOperation(Func<IOperation> setup) {
try {
if (_context.State != XModemStates.Idle)
throw new XModemProtocolException(new AbortedEventArgs(XModemAbortReason.StateNotIdle));
_context.State = XModemStates.Initializing;
if (_context.Communicator is NullCommunicator)
throw new XModemProtocolException(new AbortedEventArgs(XModemAbortReason.CommunicatorIsNull));
IOperation operation = setup();
if (OperationPending != null && OperationPending() == false)
throw new XModemProtocolException(new AbortedEventArgs(XModemAbortReason.CancelledByOperationPendingEvent));
_tokenSource = new CancellationTokenSource();
_context.Token = _tokenSource.Token;
_context.Communicator.Flush();
operation.Go(_context);
Completed?.Invoke(this, new CompletedEventArgs(_context.Data));
}
catch (XModemProtocolException ex) {
switch(ex.AbortArgs.Reason) {
case XModemAbortReason.Cancelled:
case XModemAbortReason.ConsecutiveNAKLimitExceeded:
SendCancel();
break;
}
Aborted?.Invoke(this, new AbortedEventArgs(ex.AbortArgs.Reason));
}
finally {
_context.State = XModemStates.Idle;
}
}
#endregion
#region Finalizer
~XModemCommunicator() {
CancelOperation();
}
#endregion
}
}