public static void HandleAccessRequest(GXDLMSSettings settings, GXDLMSServer server, GXByteBuffer data,
GXByteBuffer reply, GXDLMSTranslatorStructure xml)
{
//Return error if connection is not established.
if (xml == null && !settings.Connected)
{
reply.Set(GXDLMSServer.GenerateConfirmedServiceError(ConfirmedServiceError.InitiateError,
ServiceError.Service, (byte)Service.Unsupported));
return;
}
//Get long invoke id and priority.
UInt32 invokeId = data.GetUInt32();
int len = GXCommon.GetObjectCount(data);
byte[] tmp = null;
// If date time is given.
if (len != 0)
{
tmp = new byte[len];
data.Get(tmp);
if (xml == null)
{
DataType dt = DataType.DateTime;
if (len == 4)
{
dt = DataType.Time;
}
else if (len == 5)
{
dt = DataType.Date;
}
GXDataInfo info = new GXDataInfo();
info.Type = dt;
GXCommon.GetData(settings, new GXByteBuffer(tmp), info);
}
}
// Get object count.
int cnt = GXCommon.GetObjectCount(data);
if (xml != null)
{
xml.AppendStartTag(Command.AccessRequest);
xml.AppendLine(TranslatorTags.LongInvokeId, "Value", xml.IntegerToHex(invokeId, 2));
xml.AppendLine(TranslatorTags.DateTime, "Value", GXCommon.ToHex(tmp, false));
xml.AppendStartTag(TranslatorTags.AccessRequestBody);
xml.AppendStartTag(TranslatorTags.ListOfAccessRequestSpecification, "Qty",
xml.IntegerToHex(cnt, 2));
}
AccessServiceCommandType type;
for (int pos = 0; pos != cnt; ++pos)
{
type = (AccessServiceCommandType)data.GetUInt8();
if (!(type == AccessServiceCommandType.Get ||
type == AccessServiceCommandType.Set ||
type == AccessServiceCommandType.Action))
{
throw new ArgumentException("Invalid access service command type.");
}
// CI
ObjectType ci = (ObjectType)data.GetUInt16();
byte[] ln = new byte[6];
data.Get(ln);
// Attribute Id
byte attributeIndex = data.GetUInt8();
if (xml != null)
{
xml.AppendStartTag(TranslatorTags.AccessRequestSpecification);
xml.AppendStartTag(Command.AccessRequest, type);
AppendAttributeDescriptor(xml, (int)ci, ln, attributeIndex);
xml.AppendEndTag(Command.AccessRequest, type);
xml.AppendEndTag(TranslatorTags.AccessRequestSpecification);
}
}
if (xml != null)
{
xml.AppendEndTag(TranslatorTags.ListOfAccessRequestSpecification);
xml.AppendStartTag(TranslatorTags.AccessRequestListOfData, "Qty", xml.IntegerToHex(cnt, 2));
}
// Get data count.
cnt = GXCommon.GetObjectCount(data);
for (int pos = 0; pos != cnt; ++pos)
{
GXDataInfo di = new GXDataInfo();
di.xml = xml;
if (xml != null && xml.OutputType == TranslatorOutputType.StandardXml)
{
xml.AppendStartTag(Command.WriteRequest, SingleReadResponse.Data);
}
object value = GXCommon.GetData(settings, data, di);
if (!di.Complete)
{
value = GXCommon.ToHex(data.Data, false, data.Position, data.Size - data.Position);
}
else if (value is byte[])
{
value = GXCommon.ToHex((byte[])value, false);
}
if (xml != null && xml
.OutputType == TranslatorOutputType.StandardXml)
{
xml.AppendEndTag(Command.WriteRequest, SingleReadResponse.Data);
}
}
if (xml != null)
{
xml.AppendEndTag(TranslatorTags.AccessRequestListOfData);
xml.AppendEndTag(TranslatorTags.AccessRequestBody);
xml.AppendEndTag(Command.AccessRequest);
}
}
}