private static bool CheckHdlcAddress(
bool server,
GXDLMSSettings settings,
GXByteBuffer reply,
int index)
{
int source, target;
// Get destination and source addresses.
target = GXCommon.GetHDLCAddress(reply);
source = GXCommon.GetHDLCAddress(reply);
if (server)
{
// Check that server addresses match.
if (settings.ServerAddress != 0
&& settings.ServerAddress != target)
{
if (reply.GetUInt8(reply.Position) == (int)Command.Snrm)
{
settings.ServerAddress = target;
}
else
{
throw new GXDLMSException(
"Destination addresses do not match. It is "
+ target.ToString() + ". It should be "
+ settings.ServerAddress.ToString() + ".");
}
}
else
{
settings.ServerAddress = target;
}
// Check that client addresses match.
if (settings.ClientAddress != 0 && settings.ClientAddress != source)
{
if (reply.GetUInt8(reply.Position) == (int)Command.Snrm)
{
settings.ClientAddress = source;
}
else
{
throw new GXDLMSException(
"Source addresses do not match. It is "
+ source.ToString() + ". It should be "
+ settings.ClientAddress.ToString()
+ ".");
}
}
else
{
settings.ClientAddress = source;
}
}
else
{
// Check that client addresses match.
if (settings.ClientAddress != target)
{
//If echo.
if (settings.ClientAddress == source &&
settings.ServerAddress == target)
{
reply.Position = (index + 1);
}
return false;
}
// Check that server addresses match.
if (settings.ServerAddress != source)
{
//Check logical and physical address separately.
//This is done because some meters might send four bytes
//when only two bytes is needed.
int readLogical, readPhysical, logical, physical;
GetServerAddress(source, out readLogical, out readPhysical);
GetServerAddress(settings.ServerAddress, out logical, out physical);
if (readLogical != logical || readPhysical != physical)
{
return false;
}
}
}
return true;
}