/// <summary> /// Issues a set data message to the device. /// </summary> /// <param name="targetPath"></param> /// <param name="sourcePath"></param> /// <param name="data"></param> /// <param name="cancellationToken"></param> /// <returns></returns> public async Task SendSetData(RnetPath targetPath, RnetPath sourcePath, byte[] data, CancellationToken cancellationToken) { Contract.Requires(data != null); Contract.Requires(data.Length <= 1024); // only one writer at a time using (await send.LockAsync(cancellationToken)) { // number of packets to send int c = data.Length / 16; if (data.Length % 16 > 0) c++; // send until all data sent for (int i = 0; i < c; i++) { // length of packet (remainder of data, max 16) var r = data.Length - i * 16; var l = r % 16 > 0 ? r % 16 : 16; var d = new byte[l]; Array.Copy(data, i * 16, d, 0, l); // set data must wait for a handshake using (await handshake.EnterAsync(cancellationToken)) { handshakeMessage = null; // send set data packet await Bus.Client.Send(new RnetSetDataMessage( DeviceId, Bus.LocalDevice.DeviceId, targetPath, sourcePath, (byte)i, (byte)c, new RnetData(d))); // wait for handshake while (handshakeMessage == null) await handshake.WaitAsync(cancellationToken); } } } }
/// <summary> /// Issues an event message to the device. /// </summary> /// <param name="path"></param> /// <param name="evt"></param> /// <param name="timestamp"></param> /// <param name="data"></param> /// <returns></returns> public async Task SendEvent(RnetPath path, RnetEvent evt, ushort timestamp, ushort data, RnetPriority priority, CancellationToken cancellationToken) { using (await send.LockAsync(cancellationToken)) using (await handshake.EnterAsync(cancellationToken)) { handshakeMessage = null; // send set data packet await Bus.Client.Send(new RnetEventMessage( DeviceId, Bus.LocalDevice.DeviceId, path, RnetPath.Empty, evt, timestamp, data, priority)); // wait for handshake if (priority == RnetPriority.High) while (handshakeMessage == null) await handshake.WaitAsync(cancellationToken); } }
/// <summary> /// Device has sent us a handshake message. /// </summary> /// <param name="message"></param> /// <returns></returns> async Task ReceiveHandshakeMessage(RnetHandshakeMessage message) { Contract.Requires<ArgumentNullException>(message != null); using (await handshake.EnterAsync()) { handshakeMessage = message; handshake.PulseAll(); } }