public record ServiceRequest : System.ValueType, System.IEquatable<SharpMeter.Core.Protocol.ServiceRequest>
Namespace: SharpMeter.Core.Protocol
Inheritance
Inherits from: System.ValueType
Implements: System.IEquatable<SharpMeter.Core.Protocol.ServiceRequest>
Constructors
| Name | Description |
|---|---|
ServiceRequest(ServiceCode Code, ReadOnlyMemory<byte> Payload) |
Represents an L7 PSEM service request with its payload. |
ServiceRequest(ServiceCode Code, ReadOnlyMemory Payload)
ServiceRequest.ServiceRequest(ServiceCode Code, ReadOnlyMemory<byte> Payload)
Represents an L7 PSEM service request with its payload.
Parameters
| Name | Type | Description |
|---|---|---|
Code | SharpMeter.Core.Protocol.ServiceCode | The service request code. |
Payload | ReadOnlyMemory<byte> | The service-specific payload bytes. |
Properties
| Name | Description |
|---|---|
Code |
The service request code. |
EncodedLength |
Gets the total encoded length of this request. |
Payload |
The service-specific payload bytes. |
Code
ServiceCode ServiceRequest.Code { get; init; }
The service request code.
EncodedLength
int ServiceRequest.EncodedLength { get; }
Gets the total encoded length of this request.
Payload
ReadOnlyMemory<byte> ServiceRequest.Payload { get; init; }
The service-specific payload bytes.
Methods
| Name | Description |
|---|---|
Disconnect() static |
Creates a Disconnect request. |
Encode(Span<byte> destination) |
Encodes this service request into a byte buffer for inclusion in a PSEM frame. |
Identify() static |
Creates an Identify request. |
Logoff() static |
Creates a Logoff request. |
Logon(ushort userId, string userName) static |
Creates a Logon request. |
Negotiate(…) static |
Creates a Negotiate request. |
ReadFull(ushort tableId) static |
Creates a full table read request. |
ReadOffset(…) static |
Creates a partial (offset) table read request. |
Security(ReadOnlySpan<byte> password) static |
Creates a Security (password) request. |
Terminate() static |
Creates a Terminate request. |
Wait() static |
Creates a Wait (keep-alive) request. |
WriteFull(ushort tableId, ReadOnlySpan<byte> data) static |
Creates a full table write request. |
WriteOffset(…) static |
Creates a write-with-offset request. |
Disconnect()
ServiceRequest ServiceRequest.Disconnect()
Creates a Disconnect request.
Encode(Span destination)
int ServiceRequest.Encode(Span<byte> destination)
Encodes this service request into a byte buffer for inclusion in a PSEM frame.
Parameters
| Name | Type | Description |
|---|---|---|
destination | Span<byte> | The destination span. |
Returns: Number of bytes written.
Identify()
ServiceRequest ServiceRequest.Identify()
Creates an Identify request.
Logoff()
ServiceRequest ServiceRequest.Logoff()
Creates a Logoff request.
Logon(ushort userId, string userName)
ServiceRequest ServiceRequest.Logon(ushort userId, string userName)
Creates a Logon request.
Parameters
| Name | Type | Description |
|---|---|---|
userId | ushort | The user ID (2 bytes). |
userName | string | The username (max 10 bytes, null-padded). |
Negotiate(ushort maxPacketSize, byte maxPackets, BaudRate> baudRates)
ServiceRequest ServiceRequest.Negotiate(ushort maxPacketSize, byte maxPackets, ReadOnlySpan<BaudRate> baudRates)
Creates a Negotiate request.
Parameters
| Name | Type | Description |
|---|---|---|
maxPacketSize | ushort | Maximum packet size. |
maxPackets | byte | Maximum number of packets. |
baudRates | ReadOnlySpan<SharpMeter.Core.Protocol.BaudRate> | Supported baud rates. |
ReadFull(ushort tableId)
ServiceRequest ServiceRequest.ReadFull(ushort tableId)
Creates a full table read request.
Parameters
| Name | Type | Description |
|---|---|---|
tableId | ushort | The table ID to read. |
ReadOffset(ushort tableId, int offset, ushort count)
ServiceRequest ServiceRequest.ReadOffset(ushort tableId, int offset, ushort count)
Creates a partial (offset) table read request.
Parameters
| Name | Type | Description |
|---|---|---|
tableId | ushort | The table ID to read. |
offset | int | Byte offset within the table (0 to 0xFFFFFF). |
count | ushort | Number of bytes to read. |
Security(ReadOnlySpan password)
ServiceRequest ServiceRequest.Security(ReadOnlySpan<byte> password)
Creates a Security (password) request.
Parameters
| Name | Type | Description |
|---|---|---|
password | ReadOnlySpan<byte> | The password bytes (max 20 bytes, null-padded). |
Terminate()
ServiceRequest ServiceRequest.Terminate()
Creates a Terminate request.
Wait()
ServiceRequest ServiceRequest.Wait()
Creates a Wait (keep-alive) request.
WriteFull(ushort tableId, ReadOnlySpan data)
ServiceRequest ServiceRequest.WriteFull(ushort tableId, ReadOnlySpan<byte> data)
Creates a full table write request.
Parameters
| Name | Type | Description |
|---|---|---|
tableId | ushort | The table ID to write. |
data | ReadOnlySpan<byte> | The data to write. |
WriteOffset(ushort tableId, int offset, ReadOnlySpan data)
ServiceRequest ServiceRequest.WriteOffset(ushort tableId, int offset, ReadOnlySpan<byte> data)
Creates a write-with-offset request.
Parameters
| Name | Type | Description |
|---|---|---|
tableId | ushort | The table ID to write. |
offset | int | Byte offset within the table (0 to 0xFFFFFF). |
data | ReadOnlySpan<byte> | The data to write. |
Type Relationships
classDiagram
style ServiceRequest fill:#f9f,stroke:#333,stroke-width:2px
ServiceRequest --|> ValueType : inherits
ServiceRequest ..|> ServiceRequest~ : implements
View Source
/// <summary>
/// Represents an L7 PSEM service request with its payload.
/// </summary>
/// <param name = "Code">The service request code.</param>
/// <param name = "Payload">The service-specific payload bytes.</param>
public readonly record struct ServiceRequest(ServiceCode Code, ReadOnlyMemory<byte> Payload)
{
#region Factory Methods
/// <summary>Creates an Identify request.</summary>
public static ServiceRequest Identify() => new(ServiceCode.Identify, ReadOnlyMemory<byte>.Empty);
/// <summary>Creates a Terminate request.</summary>
public static ServiceRequest Terminate() => new(ServiceCode.Terminate, ReadOnlyMemory<byte>.Empty);
/// <summary>Creates a Disconnect request.</summary>
public static ServiceRequest Disconnect() => new(ServiceCode.Disconnect, ReadOnlyMemory<byte>.Empty);
/// <summary>Creates a Logoff request.</summary>
public static ServiceRequest Logoff() => new(ServiceCode.Logoff, ReadOnlyMemory<byte>.Empty);
/// <summary>Creates a Wait (keep-alive) request.</summary>
public static ServiceRequest Wait() => new(ServiceCode.Wait, ReadOnlyMemory<byte>.Empty);
/// <summary>Creates a Logon request.</summary>
/// <param name = "userId">The user ID (2 bytes).</param>
/// <param name = "userName">The username (max 10 bytes, null-padded).</param>
public static ServiceRequest Logon(ushort userId, string userName)
{
ArgumentNullException.ThrowIfNull(userName);
var payload = new byte[12];
payload[0] = (byte)(userId >> 8);
payload[1] = (byte)(userId & 0xFF);
var nameBytes = Encoding.ASCII.GetBytes(userName);
Array.Copy(nameBytes, 0, payload, 2, Math.Min(nameBytes.Length, 10));
return new ServiceRequest(ServiceCode.Logon, payload);
}
/// <summary>Creates a Security (password) request.</summary>
/// <param name = "password">The password bytes (max 20 bytes, null-padded).</param>
public static ServiceRequest Security(ReadOnlySpan<byte> password)
{
var payload = new byte[20];
password[..Math.Min(password.Length, 20)].CopyTo(payload);
return new ServiceRequest(ServiceCode.Security, payload);
}
/// <summary>Creates a Negotiate request.</summary>
/// <param name = "maxPacketSize">Maximum packet size.</param>
/// <param name = "maxPackets">Maximum number of packets.</param>
/// <param name = "baudRates">Supported baud rates.</param>
public static ServiceRequest Negotiate(ushort maxPacketSize, byte maxPackets, ReadOnlySpan<BaudRate> baudRates)
{
var payload = new byte[3 + baudRates.Length];
payload[0] = (byte)(maxPacketSize >> 8);
payload[1] = (byte)(maxPacketSize & 0xFF);
payload[2] = maxPackets;
for (var i = 0; i < baudRates.Length; i++)
payload[3 + i] = (byte)baudRates[i];
return new ServiceRequest((ServiceCode)((byte)ServiceCode.NegotiateBase + baudRates.Length), payload);
}
/// <summary>Creates a full table read request.</summary>
/// <param name = "tableId">The table ID to read.</param>
public static ServiceRequest ReadFull(ushort tableId)
{
var payload = new byte[2];
payload[0] = (byte)(tableId >> 8);
payload[1] = (byte)(tableId & 0xFF);
return new ServiceRequest(ServiceCode.ReadFull, payload);
}
/// <summary>Creates a partial (offset) table read request.</summary>
/// <param name = "tableId">The table ID to read.</param>
/// <param name = "offset">Byte offset within the table (0 to 0xFFFFFF).</param>
/// <param name = "count">Number of bytes to read.</param>
public static ServiceRequest ReadOffset(ushort tableId, int offset, ushort count)
{
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, 0xFFFFFF);
var payload = new byte[7];
payload[0] = (byte)(tableId >> 8);
payload[1] = (byte)(tableId & 0xFF);
payload[2] = (byte)((offset >> 16) & 0xFF);
payload[3] = (byte)((offset >> 8) & 0xFF);
payload[4] = (byte)(offset & 0xFF);
payload[5] = (byte)(count >> 8);
payload[6] = (byte)(count & 0xFF);
return new ServiceRequest(ServiceCode.ReadOffset, payload);
}
/// <summary>Creates a full table write request.</summary>
/// <param name = "tableId">The table ID to write.</param>
/// <param name = "data">The data to write.</param>
public static ServiceRequest WriteFull(ushort tableId, ReadOnlySpan<byte> data)
{
var payload = new byte[2 + data.Length];
payload[0] = (byte)(tableId >> 8);
payload[1] = (byte)(tableId & 0xFF);
data.CopyTo(payload.AsSpan(2));
return new ServiceRequest(ServiceCode.WriteFull, payload);
}
/// <summary>Creates a write-with-offset request.</summary>
/// <param name = "tableId">The table ID to write.</param>
/// <param name = "offset">Byte offset within the table (0 to 0xFFFFFF).</param>
/// <param name = "data">The data to write.</param>
public static ServiceRequest WriteOffset(ushort tableId, int offset, ReadOnlySpan<byte> data)
{
ArgumentOutOfRangeException.ThrowIfNegative(offset);
ArgumentOutOfRangeException.ThrowIfGreaterThan(offset, 0xFFFFFF);
var payload = new byte[5 + data.Length];
payload[0] = (byte)(tableId >> 8);
payload[1] = (byte)(tableId & 0xFF);
payload[2] = (byte)((offset >> 16) & 0xFF);
payload[3] = (byte)((offset >> 8) & 0xFF);
payload[4] = (byte)(offset & 0xFF);
data.CopyTo(payload.AsSpan(5));
return new ServiceRequest(ServiceCode.WriteOffset, payload);
}
#endregion
#region Methods
/// <summary>
/// Encodes this service request into a byte buffer for inclusion in a PSEM frame.
/// </summary>
/// <param name = "destination">The destination span.</param>
/// <returns>Number of bytes written.</returns>
public int Encode(Span<byte> destination)
{
destination[0] = (byte)Code;
Payload.Span.CopyTo(destination[1..]);
return 1 + Payload.Length;
}
/// <summary>Gets the total encoded length of this request.</summary>
public int EncodedLength => 1 + Payload.Length;
#endregion
}