forked from thrandre/ZSharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZWavePort.cs
More file actions
388 lines (355 loc) · 12.4 KB
/
Copy pathZWavePort.cs
File metadata and controls
388 lines (355 loc) · 12.4 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
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
/*
* ZSharp - C# Z-Wave Implementation
* HiØ 2011
* H11D13 / iAutomation@Home
* Author: thomrand
*/
using System;
using System.IO.Ports;
using System.Threading;
using System.Collections.Generic;
using System.Diagnostics;
using System.Collections.Concurrent;
namespace ZSharp
{
/// <summary>
/// A class that deals with low level serial communications with a Z-Wave USB Controller.
/// </summary>
internal class ZWavePort
{
public class UnsubscribedMessageEventArgs : EventArgs
{
public ZWaveMessage Message;
public UnsubscribedMessageEventArgs(ZWaveMessage message) : base()
{
this.Message = message;
}
}
public event EventHandler UnsubscribedMessageEvent;
public void FireUnsubscribedMessageEvent(ZWaveMessage message)
{
Utils.SafeEventFire(this, new UnsubscribedMessageEventArgs(message), UnsubscribedMessageEvent);
}
private SerialPort _sp;
private byte[] buff2 = new byte[1024];
private ConcurrentQueue<ZWaveJob> _jobQueue = new ConcurrentQueue<ZWaveJob>();
/// <summary>
/// The current ZwaveJob. This is just held as the message on the top of the _jobQueue. The current job is only dequeued once it is complete.
/// </summary>
private ZWaveJob CurrentJob
{
get
{
ZWaveJob curJob = null;
if (_jobQueue.TryPeek(out curJob))
{
return curJob;
}
else return null;
}
}
/// <summary>
/// Create and initialize a new communication port.
/// </summary>
public ZWavePort()
{
this._sp = new SerialPort();
_sp.DataReceived += _sp_DataReceived;
this._sp.Parity = Parity.None;
this._sp.BaudRate = 115200;
this._sp.Handshake = Handshake.None;
this._sp.StopBits = StopBits.One;
this._sp.DtrEnable = true;
this._sp.RtsEnable = true;
this._sp.NewLine = Environment.NewLine;
//this._runner = new Thread(new ThreadStart(Run));
}
/// <summary>
/// Handles the event and dumps all bytes to be read to a buffer to be handled by DataReceivedHandler
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void _sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
var sp = sender as SerialPort;
var cnt = sp.BytesToRead;
DebugLogger.Logger.Trace(string.Format("Event Type {0}, Bytes: {1}", e.EventType.ToString(), cnt));
if (cnt > 0)
{
byte[] ba = new byte[cnt];
sp.Read(ba, 0, cnt);
DataReceivedHandler(ba);
}
}
/// <summary>
/// Handler when data is received on the port
/// </summary>
/// <param name="buf">buffer received on the port</param>
void DataReceivedHandler(byte[] buf)
{
var job = this.CurrentJob;
switch (buf[ZWaveProtocol.MessageBufferOffsets.ResponseType])
{
case ZWaveProtocol.SOF:
DebugLogger.Logger.Trace("Received: SOF");
ProcessMessage(new ArraySegment<byte>(buf), job);
break;
case ZWaveProtocol.CAN:
DebugLogger.Logger.Trace("Received: CAN");
ResendCurrentJob(resendReason.ReceivedCAN);
break;
case ZWaveProtocol.NAK:
DebugLogger.Logger.Trace("Received: NAK");
ResendCurrentJob(resendReason.ReceivedNAK);
break;
case ZWaveProtocol.ACK:
DebugLogger.Logger.Trace("Received: ACK");
HandleACKForCurrentJob();
if(buf.Length > 1 && buf[ZWaveProtocol.MessageBufferOffsets.ResponseType + 1] == ZWaveProtocol.SOF)
ProcessMessage(new ArraySegment<byte>(buf, 1, buf.Length - 1), job);
break;
default:
DebugLogger.Logger.Trace("Critical error. Out of frame flow.");
break;
}
}
/// <summary>
/// Message processing
/// </summary>
/// <param name="bufSeg">An ArraySegment for the buffer to be processed</param>
/// <param name="job">The ZwaveJob to be processed</param>
private void ProcessMessage(ArraySegment<byte> bufSeg, ZWaveJob job)
{
// Read the length byte
byte len = bufSeg.Array[ZWaveProtocol.MessageBufferOffsets.MessageLength + bufSeg.Offset];
// Read rest of the frame
byte[] message = Utils.ByteSubstring(bufSeg.Array,
bufSeg.Offset,
bufSeg.Count);
DebugLogger.Logger.Trace("Received: " + Utils.ByteArrayToString(message));
ZWaveMessage zMessage = null;
try
{
zMessage = new ZWaveMessage(message);
//Checksum is correct
SendACKToPort();
}
catch (MessageChecksumInvalidException ex)
{
DebugLogger.Logger.Error("Message Checksum invalid. Sending NAK.\nMessage: {0}", Utils.ByteArrayToString(message));
SendNAKToPort();
return;
}
if (job == null)
{
// Incoming response?
DebugLogger.Logger.Trace("*** Incoming response");
this.FireUnsubscribedMessageEvent(zMessage);
}
else
{
if (job.AwaitACK)
{
// We wanted an ACK instead. Resend...
ResendCurrentJob(resendReason.ExpectingACK);
}
else
{
job.AddResponse(zMessage);
this.FireUnsubscribedMessageEvent(zMessage);
}
}
}
//Send a NAK (Negative Acknowledgement) to the port
private void SendNAKToPort()
{
DebugLogger.Logger.Trace("Sending: NAK");
this._sp.Write(new byte[] { ZWaveProtocol.NAK }, 0, 1);
}
/// <summary>
/// Send an ACK to the port
/// </summary>
private void SendACKToPort()
{
DebugLogger.Logger.Trace("Sending: ACK");
this._sp.Write(new byte[] { ZWaveProtocol.ACK }, 0, 1);
}
/// <summary>
/// Set appropriate job fields when an ACK is received
/// </summary>
private void HandleACKForCurrentJob()
{
var job = this.CurrentJob;
if (job != null)
{
if (job.AwaitACK && !job.AwaitResponse)
{
job.AwaitResponse = true;
job.AwaitACK = false;
}
}
}
/// <summary>
/// Resends the CurrentJob to the port
/// </summary>
/// <param name="reason">The reason the job is to be resent. This affects which flags are set on the job object</param>
private void ResendCurrentJob(resendReason reason)
{
var job = this.CurrentJob;
DebugLogger.Logger.Trace(string.Format("{0}", job.ToString()));
switch (reason)
{
case resendReason.ExpectingACK:
job.AwaitResponse = false;
break;
case resendReason.ReceivedNAK:
case resendReason.ReceivedCAN:
job.AwaitACK = false;
job.JobStarted = false;
break;
default:
break;
}
SendJob(job);
}
/// <summary>
/// Sends a job to the port. If the job Sendcount is >= 3 the job will be canceled
/// </summary>
/// <param name="job">ZwaveJob to be sent</param>
private void SendJob(ZWaveJob job)
{
DebugLogger.Logger.Trace(string.Format("{0}", job.ToString()));
if (job.SendCount >= 3)
{
job.CancelJob();
}
else
{
ZWaveMessage msg = job.Request;
if (msg != null)
{
DebugLogger.Logger.Trace(string.Format("Sending Message:{0}", msg.ToString()));
job.Start();
job.Resend = false;
job.AwaitACK = true;
job.SendCount++;
this._sp.Write(msg.Message, 0, msg.Message.Length);
}
}
}
/// <summary>
/// Handles processing of the next job on the queue
/// </summary>
private void ProcessJobQueue()
{
var job = this.CurrentJob;
DebugLogger.Logger.Trace(string.Format("{0}", job.ToString()));
if (job == null || job.IsDone)
{
if (_jobQueue.TryDequeue(out job))
{
DebugLogger.Logger.Trace(string.Format("Current Job null or Complete. Dequeueing."));
job.JobFinished -= job_JobFinished;
job.ResendRequired -= job_ResendRequired;
job = this.CurrentJob;
DebugLogger.Logger.Trace(string.Format("Current Job: {0}", job != null ? job.ToString() : "null"));
}
else
{
DebugLogger.Logger.Error("Error DeQueuing finished CurrentJob");
}
}
if (job != null)
{
job.JobFinished += job_JobFinished;
job.ResendRequired += job_ResendRequired;
SendJob(job);
}
}
/// <summary>
/// Event handler for job resend
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void job_ResendRequired(object sender, EventArgs e)
{
var job = sender as ZWaveJob;
SendJob(job);
}
/// <summary>
/// Event handler when job IsDone is set
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void job_JobFinished(object sender, EventArgs e)
{
ProcessJobQueue();
}
/// <summary>
/// Open the port.
/// </summary>
public bool Open()
{
DebugLogger.Logger.Trace("Opening port: " + this._sp.PortName);
if (!this._sp.IsOpen)
{
for (int i = 5; i > 0; i--)
{
String port = "COM" + i;
this._sp.PortName = port;
try
{
this._sp.Open();
}
catch (Exception e)
{
DebugLogger.Logger.Error(string.Format("ZWave controller not found at port: {0}\n{1}", this._sp.PortName, e.ToString()));
}
if (this._sp.IsOpen)
break;
}
if (this._sp.IsOpen)
{
DebugLogger.Logger.Trace("Found ZWave controller at port: " + this._sp.PortName);
//this._runner.Start();
return true;
}
else
{
DebugLogger.Logger.Error("ZWave controller not found");
return false;
}
}
else
{
return true;
}
}
/// <summary>
/// Close the port
/// </summary>
public void Close()
{
this._sp.Close();
this._sp.Dispose();
}
/// <summary>
/// Enqueue a new job to be sent by the controller
/// </summary>
public void EnqueueJob(ZWaveJob job)
{
this._jobQueue.Enqueue(job);
if (job == CurrentJob)
{
//Process the new job
ProcessJobQueue();
}
}
private enum resendReason
{
ExpectingACK,
ReceivedNAK,
ReceivedCAN
}
}
}