BowlerKernel
BowlerAbstractCommand.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
32 package com.neuronrobotics.sdk.common;
33 
34 // TODO: Auto-generated Javadoc
42 public abstract class BowlerAbstractCommand implements ISendable {
43 
45  private String opCode;
46 
49 
51  private ByteList data = new ByteList();
52 
54  private int namespaceIndex=0;//default used when no conflicts are expected
55 
62  public byte[] getCallingData() {
63  return data.getBytes();
64  }
65 
71  public void setOpCode(String opCode) {
72  if(opCode.length() != 4) {
73  throw new InvalidDataLengthException();
74  }
75 
76  this.opCode = opCode;
77  }
78 
84  public String getOpCode() {
85  return opCode;
86  }
87 
93  public void setMethod(BowlerMethod method) {
94  this.method = method;
95  }
96 
103  return method;
104  }
105 
111  public byte getLength() {
112  return (byte) (opCode.length() + getCallingData().length);
113  }
114 
124  if (data==null){
125  // TODO: Correct this with JSDK-8
126  throw new InvalidResponseException("No response from device");
127  }
128  // throws
129  if( data.getRPC().equals("_err")) {
130  Integer zone=Integer.valueOf(data.getData().getByte(0));
131  Integer section=Integer.valueOf(data.getData().getByte(1));
132  //System.err.println("Failed!!\n"+data);
133  switch(zone) {
134  default:
135  throw new InvalidResponseException("Unknown error. (" + zone + " " + section + ")");
136  case 0:
137  switch(section) {
138  default:
139  throw new InvalidResponseException("Unknow error in the communications stack. (" + zone + " " + section + ")"+data);
140  case 0x7f:
141  throw new InvalidResponseException("The method provided is invalid.");
142  case 0:
143  throw new InvalidResponseException("RPC undefined on device");
144  case 1:
145  throw new InvalidResponseException("The RPC sent in undefined with GET method.");
146  case 2:
147  throw new InvalidResponseException("The RPC sent in undefined with POST method.");
148  case 3:
149  throw new InvalidResponseException("The RPC sent in undefined with CRITICAL method.");
150  }
151  case 85:
152  switch(section) {
153  default:
154  throw new InvalidResponseException("Unknown co-processor error. (" + zone + " " + section + ")");
155  case 1:
156  case 2:
157  throw new InvalidResponseException("The co-processor did not respond.");
158  }
159  case 1:
160  switch(section) {
161  default:
162  throw new InvalidResponseException("Unknow error in the GET parser. (" + zone + " " + section + ")");
163  case 0:
164  throw new InvalidResponseException("Error with GET parsing, mostlikely the channel mode does not have a GET functionality.");
165  }
166  case 2:
167  switch(section) {
168  default:
169  throw new InvalidResponseException("Unknow error in the POST parser. (" + zone + " " + section + ")");
170  case 0:
171  throw new InvalidResponseException("Failed to properly set the value to the channel.");
172  case 1:
173  throw new InvalidResponseException("Failed to properly set the mode / the given mode type is unknown.");
174  case 2:
175  throw new InvalidResponseException("Failed to set the input channel value.");
176  }
177  case 3:
178  case 6:
179  switch(section) {
180  default:
181  throw new InvalidResponseException("Unknow error in the CRITICAL parser. (" + zone + " " + section + ")");
182  case 0:
183  throw new InvalidResponseException("Failed to configure channel.");
184  case 1:
185  throw new InvalidResponseException("Failed to configure PID channel.");
186  case 3:
187  throw new InvalidResponseException("Invalid name string, either too short or too long "+data);
188  }
189  }
190  }
191 
192  return data;
193  }
194 
202  return new BowlerAbstractCommand() {};
203  }
204 
205  /* (non-Javadoc)
206  * @see com.neuronrobotics.sdk.common.ISendable#getBytes()
207  */
208  public byte[] getBytes() {
209  ByteList buffer = new ByteList();
210  buffer.add(opCode.getBytes());
211  buffer.add(getCallingData());
212  return buffer.getBytes();
213  }
214 
215  /* (non-Javadoc)
216  * @see java.lang.Object#toString()
217  */
218  public String toString() {
219  if(getBytes().length == 0) {
220  return "";
221  }
222  String rtn = "";
223  for(byte x : getBytes()){
224  rtn += String.format("%02x ", x);
225  }
226  rtn = rtn.substring(0, rtn.length()-1);
227  return rtn.toUpperCase();
228  }
229 
235  public void setData(ByteList data) {
236  this.data = data;
237  }
238 
245  return data;
246  }
247 
254  this.namespaceIndex = namespaceIndex;
255  }
256 
262  public int getNamespaceIndex(){
263  return this.namespaceIndex;
264  }
265 
266 }
static BowlerAbstractCommand parse(BowlerDatagram data)
synchronized boolean add(byte data)
Definition: ByteList.java:149