/*
*
* 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 (C) 2014 KAIST
* @author Janggwan Im <limg00n@kaist.ac.kr>
*
* 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.interfaces.SpecParameter;
import kr.ac.kaist.resl.ltk.generated.parameters.AISpecStopTrigger;
import kr.ac.kaist.resl.ltk.generated.parameters.Custom;
import kr.ac.kaist.resl.ltk.generated.parameters.InventoryParameterSpec;
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.UnsignedShort;
import org.llrp.ltk.types.UnsignedShortArray;
import java.util.LinkedList;
import java.util.List;
/**
* This parameter defines antenna inventory operations.
See also {@link <a href="http://www.epcglobalinc.org/standards/llrp/llrp_1_0_1-standard-20070813.pdf#page=57&view=fit">LLRP Specification Section 10.2.2</a>}
and {@link <a href="http://www.epcglobalinc.org/standards/llrp/llrp_1_0_1-standard-20070813.pdf#page=137&view=fit">LLRP Specification Section 16.2.4.2</a>}
*/
/**
* This parameter defines antenna inventory operations.
See also {@link <a href="http://www.epcglobalinc.org/standards/llrp/llrp_1_0_1-standard-20070813.pdf#page=57&view=fit">LLRP Specification Section 10.2.2</a>}
and {@link <a href="http://www.epcglobalinc.org/standards/llrp/llrp_1_0_1-standard-20070813.pdf#page=137&view=fit">LLRP Specification Section 16.2.4.2</a>}
.
*/
public class AISpec extends TLVParameter implements SpecParameter {
public static final SignedShort TYPENUM = new SignedShort(183);
private static final Logger LOGGER = Logger.getLogger(AISpec.class);
protected UnsignedShortArray antennaIDs;
protected AISpecStopTrigger aISpecStopTrigger;
protected List<InventoryParameterSpec> inventoryParameterSpecList = new LinkedList<InventoryParameterSpec>();
protected List<Custom> customList = new LinkedList<Custom>();
/**
* empty constructor to create new parameter.
*/
public AISpec() {
}
/**
* Constructor to create parameter from binary encoded parameter
* calls decodeBinary to decode parameter.
* @param list to be decoded
*/
public AISpec(LLRPBitList list) {
decodeBinary(list);
}
/**
* Constructor to create parameter from xml encoded parameter
* calls decodeXML to decode parameter.
* @param element to be decoded
*/
public AISpec(Element element) throws InvalidLLRPMessageException {
decodeXML(element);
}
/**
* {@inheritDoc}
*/
public LLRPBitList encodeBinarySpecific() {
LLRPBitList resultBits = new LLRPBitList();
if (antennaIDs == null) {
LOGGER.warn(" antennaIDs not set");
throw new MissingParameterException(
" antennaIDs not set for Parameter of Type AISpec");
}
resultBits.append(antennaIDs.encodeBinary());
if (aISpecStopTrigger == null) {
// single parameter, may not be null
LOGGER.warn(" aISpecStopTrigger not set");
throw new MissingParameterException(" aISpecStopTrigger not set");
} else {
resultBits.append(aISpecStopTrigger.encodeBinary());
}
if (inventoryParameterSpecList == null) {
LOGGER.warn(" inventoryParameterSpecList not set");
//parameter has to be set - throw exception
throw new MissingParameterException(
" inventoryParameterSpecList not set");
} else {
for (InventoryParameterSpec field : inventoryParameterSpecList) {
resultBits.append(field.encodeBinary());
}
}
if (customList == null) {
//just warn - it is optional
LOGGER.info(" customList not set");
} else {
for (Custom field : customList) {
resultBits.append(field.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 (antennaIDs == null) {
LOGGER.warn(" antennaIDs not set");
throw new MissingParameterException(" antennaIDs not set");
} else {
element.addContent(antennaIDs.encodeXML("AntennaIDs", ns));
}
//parameters
if (aISpecStopTrigger == null) {
LOGGER.info("aISpecStopTrigger not set");
throw new MissingParameterException("aISpecStopTrigger not set");
} else {
element.addContent(aISpecStopTrigger.encodeXML(
aISpecStopTrigger.getClass().getSimpleName(), ns));
}
if (inventoryParameterSpecList == null) {
LOGGER.warn(" inventoryParameterSpecList not set");
throw new MissingParameterException(
" inventoryParameterSpecList not set");
}
for (InventoryParameterSpec field : inventoryParameterSpecList) {
element.addContent(field.encodeXML(field.getClass().getName()
.replaceAll(field.getClass()
.getPackage()
.getName() +
".", ""), ns));
}
if (customList == null) {
LOGGER.info("customList not set");
} else {
for (Custom field : customList) {
element.addContent(field.encodeXML(field.getClass().getName()
.replaceAll(field.getClass()
.getPackage()
.getName() +
".", ""), 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;
// array. first 16 bits indicate length of array
fieldCount = new UnsignedShort(binary.subList(position,
UnsignedShort.length())).toShort();
tempLength = (UnsignedShortArray.length() * fieldCount) +
UnsignedShort.length();
antennaIDs = new UnsignedShortArray(binary.subList(position, tempLength));
position += tempLength;
LOGGER.debug("decoding array of type: UnsignedShortArray with " +
tempLength + " length");
//might need padding
// must always be blocks of 8 bites, if it is a bitlist, this might not be automatically the case
if ((tempLength % 8) > 0) {
position += (8 - (tempLength % 8));
LOGGER.info("padding needed for antennaIDs ");
}
// 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(
"AISpec misses non optional parameter of type AISpecStopTrigger");
throw new MissingParameterException(
"AISpec misses non optional parameter of type AISpecStopTrigger");
}
if (binary.get(position)) {
// length can statically be determined for TV Parameters
tempLength = aISpecStopTrigger.length();
}
if ((type != null) && type.equals(AISpecStopTrigger.TYPENUM)) {
aISpecStopTrigger = new AISpecStopTrigger(binary.subList(position,
tempLength));
position += tempLength;
LOGGER.debug(
" aISpecStopTrigger is instantiated with AISpecStopTrigger with length" +
tempLength);
} else {
LOGGER.warn(
"AISpec misses non optional parameter of type AISpecStopTrigger");
throw new MissingParameterException(
"AISpec misses non optional parameter of type AISpecStopTrigger");
}
// list of parameters
inventoryParameterSpecList = new LinkedList<InventoryParameterSpec>();
LOGGER.debug("decoding parameter inventoryParameterSpecList ");
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;
}
//add parameter to list if type number matches
if ((type != null) && type.equals(InventoryParameterSpec.TYPENUM)) {
if (binary.get(position)) {
// length can statically be determined for TV Parameters
tempLength = InventoryParameterSpec.length();
}
inventoryParameterSpecList.add(new InventoryParameterSpec(
binary.subList(position, tempLength)));
LOGGER.debug(
"adding InventoryParameterSpec to inventoryParameterSpecList ");
atLeastOnce = true;
position += tempLength;
}
if (!atLeastOnce) {
//no parameter matched therefore we jump out of the loop
break;
}
}
//if list is still empty no parameter matched
if (inventoryParameterSpecList.isEmpty()) {
LOGGER.warn(
"encoded message does not contain parameter for non optional inventoryParameterSpecList");
throw new MissingParameterException(
"AISpec misses non optional parameter of type InventoryParameterSpec");
}
// list of parameters
customList = new LinkedList<Custom>();
LOGGER.debug("decoding parameter customList ");
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;
}
// custom
if ((type != null) && type.equals(Custom.TYPENUM)) {
Custom cus = new Custom(binary.subList(position, tempLength));
// custom parameters for this parameter
// AISpec
//end parameters
//if none matched continue wasn't called and we add just cus as we found no specific vendor implementation
customList.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 (customList.isEmpty()) {
LOGGER.info(
"encoded message does not contain parameter for optional customList");
}
}
/**
* {@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("AntennaIDs", ns);
if (temp != null) {
antennaIDs = new UnsignedShortArray(temp);
}
element.removeChild("AntennaIDs", ns);
//parameter - not choices - no special actions needed
temp = element.getChild("AISpecStopTrigger", ns);
if (temp != null) {
aISpecStopTrigger = new AISpecStopTrigger(temp);
LOGGER.info(
"setting parameter aISpecStopTrigger for parameter AISpec");
}
if (temp == null) {
LOGGER.warn(
"AISpec misses non optional parameter of type aISpecStopTrigger");
throw new MissingParameterException(
"AISpec misses non optional parameter of type aISpecStopTrigger");
}
element.removeChild("AISpecStopTrigger", ns);
//parameter - not choices - no special actions needed
//we expect a list of parameters
inventoryParameterSpecList = new LinkedList<InventoryParameterSpec>();
tempList = element.getChildren("InventoryParameterSpec", ns);
if ((tempList == null) || tempList.isEmpty()) {
LOGGER.warn(
"AISpec misses non optional parameter of type inventoryParameterSpecList");
throw new MissingParameterException(
"AISpec misses non optional parameter of type inventoryParameterSpecList");
} else {
for (Element e : tempList) {
inventoryParameterSpecList.add(new InventoryParameterSpec(e));
LOGGER.debug(
"adding InventoryParameterSpec to inventoryParameterSpecList ");
}
}
element.removeChildren("InventoryParameterSpec", ns);
//parameter - not choices - no special actions needed
//we expect a list of parameters
customList = new LinkedList<Custom>();
tempList = element.getChildren("Custom", ns);
if ((tempList == null) || tempList.isEmpty()) {
LOGGER.info("AISpec misses optional parameter of type customList");
} else {
for (Element e : tempList) {
customList.add(new Custom(e));
LOGGER.debug("adding Custom to customList ");
}
}
element.removeChildren("Custom", ns);
//custom parameter
tempList = element.getChildren("Custom", ns);
for (Element e : tempList) {
customList.add(new Custom(e));
atLeastOnce = true;
LOGGER.debug("adding custom parameter");
}
element.removeChildren("Custom", ns);
//end custom
if (element.getChildren().size() > 0) {
String message = "AISpec has unknown element " +
((Element) element.getChildren().get(0)).getName();
throw new InvalidLLRPMessageException(message);
}
}
//setters
/**
* set antennaIDs of type UnsignedShortArray .
* @param antennaIDs to be set
*/
public void setAntennaIDs(final UnsignedShortArray antennaIDs) {
this.antennaIDs = antennaIDs;
}
/**
* set aISpecStopTrigger of type AISpecStopTrigger.
* @param aISpecStopTrigger to be set
*/
public void setAISpecStopTrigger(final AISpecStopTrigger aISpecStopTrigger) {
this.aISpecStopTrigger = aISpecStopTrigger;
}
/**
* set inventoryParameterSpecList of type List <InventoryParameterSpec>.
* @param inventoryParameterSpecList to be set
*/
public void setInventoryParameterSpecList(
final List<InventoryParameterSpec> inventoryParameterSpecList) {
this.inventoryParameterSpecList = inventoryParameterSpecList;
}
/**
* set customList of type List <Custom>.
* @param customList to be set
*/
public void setCustomList(final List<Custom> customList) {
this.customList = customList;
}
// end setter
//getters
/**
* get antennaIDs of type UnsignedShortArray.
* @return type UnsignedShortArray to be set
*/
public UnsignedShortArray getAntennaIDs() {
return this.antennaIDs;
}
/**
* get aISpecStopTrigger of type AISpecStopTrigger .
* @return AISpecStopTrigger
*/
public AISpecStopTrigger getAISpecStopTrigger() {
return aISpecStopTrigger;
}
/**
* get inventoryParameterSpecList of type List <InventoryParameterSpec> .
* @return List <InventoryParameterSpec>
*/
public List<InventoryParameterSpec> getInventoryParameterSpecList() {
return inventoryParameterSpecList;
}
/**
* get customList of type List <Custom> .
* @return List <Custom>
*/
public List<Custom> getCustomList() {
return customList;
}
// end getters
//add methods
/**
* add element inventoryParameterSpec of type InventoryParameterSpec .
* @param inventoryParameterSpec of type InventoryParameterSpec
*/
public void addToInventoryParameterSpecList(
InventoryParameterSpec inventoryParameterSpec) {
if (this.inventoryParameterSpecList == null) {
this.inventoryParameterSpecList = new LinkedList<InventoryParameterSpec>();
}
this.inventoryParameterSpecList.add(inventoryParameterSpec);
}
/**
* add element custom of type Custom .
* @param custom of type Custom
*/
public void addToCustomList(Custom custom) {
if (this.customList == null) {
this.customList = new LinkedList<Custom>();
}
this.customList.add(custom);
}
// 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 "AISpec";
}
/**
* return string representation. All field values but no parameters are included
* @return String
*/
public String toString() {
String result = "AISpec: ";
result += ", antennaIDs: ";
result += antennaIDs;
result = result.replaceFirst(", ", "");
return result;
}
}