View Javadoc

1   /*
2    *  
3    *  Fosstrak LLRP Commander (www.fosstrak.org)
4    * 
5    *  Copyright (C) 2008 ETH Zurich
6    *
7    *  This program is free software: you can redistribute it and/or modify
8    *  it under the terms of the GNU General Public License as published by
9    *  the Free Software Foundation, either version 3 of the License, or
10   *  (at your option) any later version.
11   *
12   *  This program is distributed in the hope that it will be useful,
13   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   *  GNU General Public License for more details.
16   *
17   *  You should have received a copy of the GNU General Public License
18   *  along with this program.  If not, see <http://www.gnu.org/licenses/> 
19   *
20   */
21  
22  package org.fosstrak.llrp.commander.editors;
23  
24  /**
25   * This is a wrapper class for LLRP Binary Message.
26   * 
27   * @author Haoning Zhang
28   * @author sawielan
29   * @version 1.0
30   */
31  public class BinaryMessage {
32  
33  	private BinarySingleValue reserved, version, msgType, msgLength, msgID;
34  	private BinarySingleValue parameters;
35  	private BinarySingleValue[] paramArr; 
36  	
37  	private final static String INVALID_VALUE = "Invalid";
38  	private final static String CAPTION_RSV = "RSV";
39  	private final static String CAPTION_VERSION = "Version";
40  	private final static String CAPTION_MSG_TYP = "Message Type";
41  	private final static String CAPTION_LENGTH = "Message Length in Bits";
42  	private final static String CAPTION_MSG_ID = "Message ID";
43  	private final static String CAPTION_PARAM = "Fields and Parameters";
44  	
45  	/**
46  	 * Constructor, to set the default values.
47  	 */
48  	public BinaryMessage() {
49  		reserved = initValue(CAPTION_RSV, INVALID_VALUE);
50  		version = initValue(CAPTION_VERSION, INVALID_VALUE);
51  		msgType = initValue(CAPTION_MSG_TYP, INVALID_VALUE);
52  		msgLength = initValue(CAPTION_LENGTH, INVALID_VALUE);
53  		msgID = initValue(CAPTION_MSG_ID, INVALID_VALUE);
54  		parameters = initValue(CAPTION_PARAM, INVALID_VALUE);
55  		paramArr = initArrValue(CAPTION_PARAM, null);
56  	}
57  	
58  	/**
59  	 * Constructor, with XML Message as input.
60  	 * The constructor initialize the message from LLRP XML Message.
61  	 * 
62  	 * @param aContent LLRP XML Message
63  	 * @throws Exception if message is not valid.
64  	 */
65  	public BinaryMessage(String aContent) throws Exception {
66  		
67  		BinaryMessageHelper helper = new BinaryMessageHelper(aContent);
68  		reserved = initValue(CAPTION_RSV, helper.getReserved());
69  		version = initValue(CAPTION_VERSION, helper.getVersion());
70  		msgType = initValue(CAPTION_MSG_TYP, helper.getMessageType());
71  		msgLength = initValue(CAPTION_LENGTH, helper.getLength());
72  		msgID = initValue(CAPTION_MSG_ID, helper.getMessageID());
73  		parameters = initValue(CAPTION_PARAM, helper.getParameters());
74  		paramArr = initArrValue(CAPTION_PARAM, helper.getArrParameters(
75  				BinaryMessageHelper.DEFAULT_LINE_LENGTH,
76  				BinaryMessageHelper.DEFAULT_CHUNK_LENGTH,
77  				BinaryMessageHelper.DEFAULT_CHUNK_DELIMITER));
78  	}
79  	
80  	/**
81  	 * Generate one <code>BinarySingleValue</code> instance.
82  	 * 
83  	 * @param aName Name of the Binary Item
84  	 * @param aValue Value of the Binary Item
85  	 * @return BinarySingleValue instance
86  	 */
87  	private BinarySingleValue initValue(String aName, String aValue) {
88  		BinarySingleValue aEntry = new BinarySingleValue(this);
89  		aEntry.setKey(aName);
90  		aEntry.setValue(aValue);
91  		
92  		return aEntry;
93  	}
94  	
95  	/**
96  	 * parse the parameters chunk into an array of equal length.
97  	 * @param name the name of the parameter array.
98  	 * @param value the value for the resulting binary single value.
99  	 * @return a binary value holding the parameters.
100 	 */
101 	private BinarySingleValue[] initArrValue(String name, String[] value) {
102 		if (null == value) {
103 			BinarySingleValue v = new BinarySingleValue(this);
104 			v.setKey(name);
105 			v.setValue(INVALID_VALUE);
106 			return new BinarySingleValue[] { v };
107 		}
108 		
109 		BinarySingleValue[] arr = new BinarySingleValue[value.length];
110 		for (int i=0; i<value.length; i++) {
111 			arr[i] = new BinarySingleValue(this);
112 			arr[i].setValue(value[i]);
113 		}
114 		arr[0].setKey(name);
115 		return arr;
116 	}
117 	
118 	/**
119 	 * Get <strong>Reserved</strong> value from LLRP message.
120 	 * 
121 	 * @return Reserved value
122 	 */
123 	public BinarySingleValue getReserved() {
124 		return reserved;
125 	}
126 
127 	/**
128 	 * Get <strong>Version</strong> value from LLRP message.
129 	 * 
130 	 * @return Version value
131 	 */
132 	public BinarySingleValue getVersion() {
133 		return version;
134 	}
135 
136 	/**
137 	 * Get <strong>MessageType</strong> value from LLRP message.
138 	 * 
139 	 * @return Message Type value
140 	 */
141 	public BinarySingleValue getMsgType() {
142 		return msgType;
143 	}
144 
145 	/**
146 	 * Get <strong>MessageLength</strong> value from LLRP message.
147 	 * 
148 	 * @return Message Length value
149 	 */
150 	public BinarySingleValue getMsgLength() {
151 		return msgLength;
152 	}
153 
154 	/**
155 	 * Get <strong>MessageID</strong> value from LLRP message.
156 	 * 
157 	 * @return Message ID value
158 	 */
159 	public BinarySingleValue getMsgID() {
160 		return msgID;
161 	}
162 
163 	/**
164 	 * Get <strong>Parameters</strong> value from LLRP message.
165 	 * 
166 	 * @return All parameters value
167 	 */
168 	public BinarySingleValue getParameters() {
169 		return parameters;
170 	}
171 	
172 	/**
173 	 * get the parameters value as an array split. This is a quick fix to 
174 	 * circumvent the eclipse table view in-capability to display line feeds.
175 	 * @return an array holding the parameters as an array.
176 	 */
177 	public BinarySingleValue[] getSplitParameters() {
178 		return paramArr;
179 	}
180 }