/*
*
* This file was generated by LLRP Code Generator
* see http://llrp-toolkit.cvs.sourceforge.net/llrp-toolkit
* for more information
* Generated on: Mon Mar 10 14:26:47 KST 2014;
*
*/
/*
* Copyright 2007 ETH Zurich
*
* Licensed under the Apache License, Version 2.0 (the "License");
*
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions
* and limitations under the License.
*
*/
package kr.ac.kaist.resl.ltk.generated.parameters;
import org.apache.log4j.Logger;
import org.jdom.Content;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.Namespace;
import org.llrp.ltk.exceptions.InvalidLLRPMessageException;
import org.llrp.ltk.exceptions.MissingParameterException;
import kr.ac.kaist.resl.ltk.generated.LLRPConstants;
import kr.ac.kaist.resl.ltk.generated.enumerations.ROSpecState;
import kr.ac.kaist.resl.ltk.generated.interfaces.SpecParameter;
import kr.ac.kaist.resl.ltk.generated.parameters.AISpec;
import kr.ac.kaist.resl.ltk.generated.parameters.Custom;
import kr.ac.kaist.resl.ltk.generated.parameters.RFSurveySpec;
import kr.ac.kaist.resl.ltk.generated.parameters.ROBoundarySpec;
import kr.ac.kaist.resl.ltk.generated.parameters.ROReportSpec;
import org.llrp.ltk.types.LLRPBitList;
import org.llrp.ltk.types.LLRPMessage;
import org.llrp.ltk.types.SignedShort;
import org.llrp.ltk.types.TLVParameter;
import org.llrp.ltk.types.TVParameter;
import org.llrp.ltk.types.UnsignedByte;
import org.llrp.ltk.types.UnsignedInteger;
import org.llrp.ltk.types.UnsignedShort;
import java.util.LinkedList;
import java.util.List;
/**
* This parameter carries the information of the Reader inventory and survey operation.
See also {@link <a href="http://www.epcglobalinc.org/standards/llrp/llrp_1_0_1-standard-20070813.pdf#page=55&view=fit">LLRP Specification Section 10.2.1</a>}
and {@link <a href="http://www.epcglobalinc.org/standards/llrp/llrp_1_0_1-standard-20070813.pdf#page=135&view=fit">LLRP Specification Section 16.2.4.1</a>}
*/
/**
* This parameter carries the information of the Reader inventory and survey operation.
See also {@link <a href="http://www.epcglobalinc.org/standards/llrp/llrp_1_0_1-standard-20070813.pdf#page=55&view=fit">LLRP Specification Section 10.2.1</a>}
and {@link <a href="http://www.epcglobalinc.org/standards/llrp/llrp_1_0_1-standard-20070813.pdf#page=135&view=fit">LLRP Specification Section 16.2.4.1</a>}
.
*/
public class ROSpec extends TLVParameter {
public static final SignedShort TYPENUM = new SignedShort(177);
private static final Logger LOGGER = Logger.getLogger(ROSpec.class);
protected UnsignedInteger rOSpecID;
protected UnsignedByte priority;
protected ROSpecState currentState;
protected ROBoundarySpec rOBoundarySpec;
protected List<SpecParameter> specParameterList = new LinkedList<SpecParameter>();
protected ROReportSpec rOReportSpec;
/**
* empty constructor to create new parameter.
*/
public ROSpec() {
}
/**
* Constructor to create parameter from binary encoded parameter
* calls decodeBinary to decode parameter.
* @param list to be decoded
*/
public ROSpec(LLRPBitList list) {
decodeBinary(list);
}
/**
* Constructor to create parameter from xml encoded parameter
* calls decodeXML to decode parameter.
* @param element to be decoded
*/
public ROSpec(Element element) throws InvalidLLRPMessageException {
decodeXML(element);
}
/**
* {@inheritDoc}
*/
public LLRPBitList encodeBinarySpecific() {
LLRPBitList resultBits = new LLRPBitList();
if (rOSpecID == null) {
LOGGER.warn(" rOSpecID not set");
throw new MissingParameterException(
" rOSpecID not set for Parameter of Type ROSpec");
}
resultBits.append(rOSpecID.encodeBinary());
if (priority == null) {
LOGGER.warn(" priority not set");
throw new MissingParameterException(
" priority not set for Parameter of Type ROSpec");
}
resultBits.append(priority.encodeBinary());
if (currentState == null) {
LOGGER.warn(" currentState not set");
throw new MissingParameterException(
" currentState not set for Parameter of Type ROSpec");
}
resultBits.append(currentState.encodeBinary());
if (rOBoundarySpec == null) {
// single parameter, may not be null
LOGGER.warn(" rOBoundarySpec not set");
throw new MissingParameterException(" rOBoundarySpec not set");
} else {
resultBits.append(rOBoundarySpec.encodeBinary());
}
if (specParameterList == null) {
LOGGER.warn(" specParameterList not set");
//parameter has to be set - throw exception
throw new MissingParameterException(" specParameterList not set");
} else {
for (SpecParameter field : specParameterList) {
resultBits.append(field.encodeBinary());
}
}
if (rOReportSpec == null) {
// optional parameter, may be null
LOGGER.info(" rOReportSpec not set");
} else {
resultBits.append(rOReportSpec.encodeBinary());
}
return resultBits;
}
/**
* {@inheritDoc}
*/
public Content encodeXML(String name, Namespace ns) {
// element in namespace defined by parent element
Element element = new Element(name, ns);
// child element are always in default LLRP namespace
ns = Namespace.getNamespace("llrp", LLRPConstants.LLRPNAMESPACE);
if (rOSpecID == null) {
LOGGER.warn(" rOSpecID not set");
throw new MissingParameterException(" rOSpecID not set");
} else {
element.addContent(rOSpecID.encodeXML("ROSpecID", ns));
}
if (priority == null) {
LOGGER.warn(" priority not set");
throw new MissingParameterException(" priority not set");
} else {
element.addContent(priority.encodeXML("Priority", ns));
}
if (currentState == null) {
LOGGER.warn(" currentState not set");
throw new MissingParameterException(" currentState not set");
} else {
element.addContent(currentState.encodeXML("CurrentState", ns));
}
//parameters
if (rOBoundarySpec == null) {
LOGGER.info("rOBoundarySpec not set");
throw new MissingParameterException("rOBoundarySpec not set");
} else {
element.addContent(rOBoundarySpec.encodeXML(
rOBoundarySpec.getClass().getSimpleName(), ns));
}
if (specParameterList == null) {
LOGGER.warn(" specParameterList not set");
throw new MissingParameterException(" specParameterList not set");
}
for (SpecParameter field : specParameterList) {
element.addContent(field.encodeXML(field.getClass().getName()
.replaceAll(field.getClass()
.getPackage()
.getName() +
".", ""), ns));
}
if (rOReportSpec == null) {
LOGGER.info("rOReportSpec not set");
} else {
element.addContent(rOReportSpec.encodeXML(
rOReportSpec.getClass().getSimpleName(), ns));
}
return element;
}
/**
* {@inheritDoc}
*/
protected void decodeBinarySpecific(LLRPBitList binary) {
int position = 0;
int tempByteLength;
int tempLength = 0;
int count;
SignedShort type;
int fieldCount;
Custom custom;
rOSpecID = new UnsignedInteger(binary.subList(position,
UnsignedInteger.length()));
position += UnsignedInteger.length();
priority = new UnsignedByte(binary.subList(position,
UnsignedByte.length()));
position += UnsignedByte.length();
currentState = new ROSpecState(binary.subList(position,
ROSpecState.length()));
position += ROSpecState.length();
// look ahead to see type
// may be optional or exactly once
type = null;
tempByteLength = 0;
tempLength = 0;
try {
// if first bit is one it is a TV Parameter
if (binary.get(position)) {
// do not take the first bit as it is always 1
type = new SignedShort(binary.subList(position + 1, 7));
} else {
type = new SignedShort(binary.subList(position +
RESERVEDLENGTH, TYPENUMBERLENGTH));
tempByteLength = new UnsignedShort(binary.subList(position +
RESERVEDLENGTH + TYPENUMBERLENGTH,
UnsignedShort.length())).toShort();
tempLength = 8 * tempByteLength;
}
} catch (IllegalArgumentException le) {
// if an IllegalArgumentException is thrown, list was not long enough so the parameter is missing
LOGGER.warn(
"ROSpec misses non optional parameter of type ROBoundarySpec");
throw new MissingParameterException(
"ROSpec misses non optional parameter of type ROBoundarySpec");
}
if (binary.get(position)) {
// length can statically be determined for TV Parameters
tempLength = rOBoundarySpec.length();
}
if ((type != null) && type.equals(ROBoundarySpec.TYPENUM)) {
rOBoundarySpec = new ROBoundarySpec(binary.subList(position,
tempLength));
position += tempLength;
LOGGER.debug(
" rOBoundarySpec is instantiated with ROBoundarySpec with length" +
tempLength);
} else {
LOGGER.warn(
"ROSpec misses non optional parameter of type ROBoundarySpec");
throw new MissingParameterException(
"ROSpec misses non optional parameter of type ROBoundarySpec");
}
// list of parameters
specParameterList = new LinkedList<SpecParameter>();
LOGGER.debug("decoding parameter specParameterList ");
while (position < binary.length()) {
// store if one parameter matched
boolean atLeastOnce = false;
// look ahead to see type
if (binary.get(position)) {
// do not take the first bit as it is always 1
type = new SignedShort(binary.subList(position + 1, 7));
} else {
type = new SignedShort(binary.subList(position +
RESERVEDLENGTH, TYPENUMBERLENGTH));
tempByteLength = new UnsignedShort(binary.subList(position +
RESERVEDLENGTH + TYPENUMBERLENGTH,
UnsignedShort.length())).toShort();
tempLength = 8 * tempByteLength;
}
//choiceRef
if ((type != null) && type.equals(AISpec.TYPENUM)) {
if (binary.get(position)) {
// length can statically be determined for TV Parameters
tempLength = AISpec.length();
}
specParameterList.add(new AISpec(binary.subList(position,
tempLength)));
LOGGER.debug("adding AISpec to specParameterList ");
position += tempLength;
atLeastOnce = true;
}
if ((type != null) && type.equals(RFSurveySpec.TYPENUM)) {
if (binary.get(position)) {
// length can statically be determined for TV Parameters
tempLength = RFSurveySpec.length();
}
specParameterList.add(new RFSurveySpec(binary.subList(
position, tempLength)));
LOGGER.debug("adding RFSurveySpec to specParameterList ");
position += tempLength;
atLeastOnce = true;
}
// custom
if ((type != null) && type.equals(Custom.TYPENUM)) {
Custom cus = new Custom(binary.subList(position, tempLength));
// custom parameters for this parameter
//end parameters
//if none matched continue wasn't called and we add just cus as we found no specific vendor implementation
specParameterList.add(cus);
position += tempLength;
atLeastOnce = true;
}
if (!atLeastOnce) {
//no parameter matched therefore we jump out of the loop
break;
}
}
//if list is still empty no parameter matched
if (specParameterList.isEmpty()) {
LOGGER.warn(
"encoded message does not contain parameter for non optional specParameterList");
throw new MissingParameterException(
"ROSpec misses non optional parameter of type SpecParameter");
}
// look ahead to see type
// may be optional or exactly once
type = null;
tempByteLength = 0;
tempLength = 0;
try {
// if first bit is one it is a TV Parameter
if (binary.get(position)) {
// do not take the first bit as it is always 1
type = new SignedShort(binary.subList(position + 1, 7));
} else {
type = new SignedShort(binary.subList(position +
RESERVEDLENGTH, TYPENUMBERLENGTH));
tempByteLength = new UnsignedShort(binary.subList(position +
RESERVEDLENGTH + TYPENUMBERLENGTH,
UnsignedShort.length())).toShort();
tempLength = 8 * tempByteLength;
}
} catch (IllegalArgumentException le) {
// if an IllegalArgumentException is thrown, list was not long enough so the parameter is missing
LOGGER.info("ROSpec misses optional parameter of type ROReportSpec");
}
if (binary.get(position)) {
// length can statically be determined for TV Parameters
tempLength = rOReportSpec.length();
}
if ((type != null) && type.equals(ROReportSpec.TYPENUM)) {
rOReportSpec = new ROReportSpec(binary.subList(position, tempLength));
position += tempLength;
LOGGER.debug(
" rOReportSpec is instantiated with ROReportSpec with length" +
tempLength);
} else {
LOGGER.info("ROSpec misses optional parameter of type ROReportSpec");
}
}
/**
* {@inheritDoc}
*/
public void decodeXML(Element element) throws InvalidLLRPMessageException {
List<Element> tempList = null;
boolean atLeastOnce = false;
Custom custom;
Element temp = null;
// child element are always in default LLRP namespace
Namespace ns = Namespace.getNamespace(LLRPConstants.LLRPNAMESPACE);
temp = element.getChild("ROSpecID", ns);
if (temp != null) {
rOSpecID = new UnsignedInteger(temp);
}
element.removeChild("ROSpecID", ns);
temp = element.getChild("Priority", ns);
if (temp != null) {
priority = new UnsignedByte(temp);
}
element.removeChild("Priority", ns);
temp = element.getChild("CurrentState", ns);
if (temp != null) {
currentState = new ROSpecState(temp);
}
element.removeChild("CurrentState", ns);
//parameter - not choices - no special actions needed
temp = element.getChild("ROBoundarySpec", ns);
if (temp != null) {
rOBoundarySpec = new ROBoundarySpec(temp);
LOGGER.info("setting parameter rOBoundarySpec for parameter ROSpec");
}
if (temp == null) {
LOGGER.warn(
"ROSpec misses non optional parameter of type rOBoundarySpec");
throw new MissingParameterException(
"ROSpec misses non optional parameter of type rOBoundarySpec");
}
element.removeChild("ROBoundarySpec", ns);
//choices - must check all possible subtypes
//list of Choice Type Parameter
specParameterList = new LinkedList<SpecParameter>();
// for each possible subtype get all childs
tempList = element.getChildren("AISpec", ns);
for (Element e : tempList) {
specParameterList.add(new AISpec(e));
LOGGER.debug("adding AISpec to specParameterList ");
atLeastOnce = true;
}
element.removeChildren("AISpec", ns);
tempList = element.getChildren("RFSurveySpec", ns);
for (Element e : tempList) {
specParameterList.add(new RFSurveySpec(e));
LOGGER.debug("adding RFSurveySpec to specParameterList ");
atLeastOnce = true;
}
element.removeChildren("RFSurveySpec", ns);
// check for all custom parameters for this parameter
// check for regular custom parameter
tempList = element.getChildren("Custom", ns);
for (Element e : tempList) {
specParameterList.add(new Custom(e));
LOGGER.debug("adding SpecParameter to specParameterList ");
}
element.removeChildren("Custom", ns);
//end allowed parameters
if (!atLeastOnce) {
LOGGER.warn(
"ROSpec misses non optional parameter of type specParameterList");
throw new MissingParameterException(
"ROSpec misses non optional parameter of type specParameterList");
}
atLeastOnce = false;
//parameter - not choices - no special actions needed
temp = element.getChild("ROReportSpec", ns);
if (temp != null) {
rOReportSpec = new ROReportSpec(temp);
LOGGER.info("setting parameter rOReportSpec for parameter ROSpec");
}
if (temp == null) {
LOGGER.info("ROSpec misses optional parameter of type rOReportSpec");
}
element.removeChild("ROReportSpec", ns);
if (element.getChildren().size() > 0) {
String message = "ROSpec has unknown element " +
((Element) element.getChildren().get(0)).getName();
throw new InvalidLLRPMessageException(message);
}
}
//setters
/**
* set rOSpecID of type UnsignedInteger .
* @param rOSpecID to be set
*/
public void setROSpecID(final UnsignedInteger rOSpecID) {
this.rOSpecID = rOSpecID;
}
/**
* set priority of type UnsignedByte .
* @param priority to be set
*/
public void setPriority(final UnsignedByte priority) {
this.priority = priority;
}
/**
* set currentState of type ROSpecState .
* @param currentState to be set
*/
public void setCurrentState(final ROSpecState currentState) {
this.currentState = currentState;
}
/**
* set rOBoundarySpec of type ROBoundarySpec.
* @param rOBoundarySpec to be set
*/
public void setROBoundarySpec(final ROBoundarySpec rOBoundarySpec) {
this.rOBoundarySpec = rOBoundarySpec;
}
/**
* set specParameterList of type List <SpecParameter>.
* @param specParameterList to be set
*/
public void setSpecParameterList(
final List<SpecParameter> specParameterList) {
this.specParameterList = specParameterList;
}
/**
* set rOReportSpec of type ROReportSpec.
* @param rOReportSpec to be set
*/
public void setROReportSpec(final ROReportSpec rOReportSpec) {
this.rOReportSpec = rOReportSpec;
}
// end setter
//getters
/**
* get rOSpecID of type UnsignedInteger.
* @return type UnsignedInteger to be set
*/
public UnsignedInteger getROSpecID() {
return this.rOSpecID;
}
/**
* get priority of type UnsignedByte.
* @return type UnsignedByte to be set
*/
public UnsignedByte getPriority() {
return this.priority;
}
/**
* get currentState of type ROSpecState.
* @return ROSpecState
*/
public ROSpecState getCurrentState() {
return currentState;
}
/**
* get rOBoundarySpec of type ROBoundarySpec .
* @return ROBoundarySpec
*/
public ROBoundarySpec getROBoundarySpec() {
return rOBoundarySpec;
}
/**
* get specParameterList of type List <SpecParameter> .
* @return List <SpecParameter>
*/
public List<SpecParameter> getSpecParameterList() {
return specParameterList;
}
/**
* get rOReportSpec of type ROReportSpec .
* @return ROReportSpec
*/
public ROReportSpec getROReportSpec() {
return rOReportSpec;
}
// end getters
//add methods
/**
* add element specParameter of type SpecParameter .
* @param specParameter of type SpecParameter
*/
public void addToSpecParameterList(SpecParameter specParameter) {
if (this.specParameterList == null) {
this.specParameterList = new LinkedList<SpecParameter>();
}
this.specParameterList.add(specParameter);
}
// end add
/**
* For TLV Parameter length can not be determined at compile time. This method therefore always returns 0.
* @return Integer always zero
*/
public static Integer length() {
return 0;
}
/**
* {@inheritDoc}
*/
public SignedShort getTypeNum() {
return TYPENUM;
}
/**
* {@inheritDoc}
*/
public String getName() {
return "ROSpec";
}
/**
* return string representation. All field values but no parameters are included
* @return String
*/
public String toString() {
String result = "ROSpec: ";
result += ", rOSpecID: ";
result += rOSpecID;
result += ", priority: ";
result += priority;
result += ", currentState: ";
result += currentState;
result = result.replaceFirst(", ", "");
return result;
}
}