/library/src/main/java/com/openxc/messages/formatters/binary/BinaryDeserializer.java

https://github.com/openxc/openxc-android · Java · 278 lines · 253 code · 25 blank · 0 comment · 96 complexity · 493d35ac175b0900ebf269779e584b20 MD5 · raw file

  1. package com.openxc.messages.formatters.binary;
  2. import android.util.Log;
  3. import com.openxc.BinaryMessages;
  4. import com.openxc.messages.CanMessage;
  5. import com.openxc.messages.Command;
  6. import com.openxc.messages.Command.CommandType;
  7. import com.openxc.messages.CommandResponse;
  8. import com.openxc.messages.DiagnosticRequest;
  9. import com.openxc.messages.DiagnosticResponse;
  10. import com.openxc.messages.EventedSimpleVehicleMessage;
  11. import com.openxc.messages.MultiFrameResponse;
  12. import com.openxc.messages.NamedVehicleMessage;
  13. import com.openxc.messages.SimpleVehicleMessage;
  14. import com.openxc.messages.UnrecognizedMessageTypeException;
  15. import com.openxc.messages.VehicleMessage;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. public class BinaryDeserializer {
  19. private final static String TAG = "BinaryDeserializer";
  20. private BinaryDeserializer() {
  21. throw new IllegalStateException("BinaryDeserializer class");
  22. }
  23. public static VehicleMessage deserialize(InputStream data)
  24. throws UnrecognizedMessageTypeException {
  25. VehicleMessage result = null;
  26. try {
  27. BinaryMessages.VehicleMessage message =
  28. BinaryMessages.VehicleMessage.parseFrom(data);
  29. if(message != null) {
  30. result = deserialize(message);
  31. }
  32. } catch(IOException e) {
  33. Log.w(TAG, "Unable to deserialize from binary stream", e);
  34. }
  35. return result;
  36. }
  37. private static NamedVehicleMessage deserializeNamedMessage(
  38. BinaryMessages.VehicleMessage binaryMessage) throws UnrecognizedMessageTypeException {
  39. BinaryMessages.SimpleMessage simpleMessage =
  40. binaryMessage.getSimpleMessage();
  41. String name;
  42. name = simpleMessage.getName();
  43. Object value = null;
  44. BinaryMessages.DynamicField field =
  45. simpleMessage.getValue();
  46. if(field.getTypeValue() == 2) {
  47. value = field.getNumericValue();
  48. } else if(field.getTypeValue() == 3) {
  49. value = field.getBooleanValue();
  50. } else if(field.getTypeValue() == 1) {
  51. value = field.getStringValue();
  52. }
  53. Object event = null;
  54. if(simpleMessage.hasEvent()) {
  55. field = simpleMessage.getEvent();
  56. if(field.getTypeValue() == 2) {
  57. event = field.getNumericValue();
  58. } else if(field.getTypeValue() == 3) {
  59. event = field.getBooleanValue();
  60. } else if(field.getTypeValue() == 1) {
  61. event = field.getStringValue();
  62. }
  63. }
  64. if(event == null) {
  65. if(value == null) {
  66. return new NamedVehicleMessage(name);
  67. } else {
  68. return new SimpleVehicleMessage(name, value);
  69. }
  70. } else {
  71. return new EventedSimpleVehicleMessage(name, value, event);
  72. }
  73. }
  74. private static CanMessage deserializeCanMessage(
  75. BinaryMessages.VehicleMessage binaryMessage) {
  76. BinaryMessages.CanMessage canMessage = binaryMessage.getCanMessage();
  77. return new CanMessage(canMessage.getBus(),
  78. canMessage.getId(),
  79. canMessage.getData().toByteArray());
  80. }
  81. private static Command deserializeDiagnosticCommand(
  82. BinaryMessages.ControlCommand command)
  83. throws UnrecognizedMessageTypeException {
  84. if(!command.hasDiagnosticRequest()) {
  85. throw new UnrecognizedMessageTypeException(
  86. "Diagnostic command missing request details");
  87. }
  88. BinaryMessages.DiagnosticControlCommand diagnosticCommand =
  89. command.getDiagnosticRequest();
  90. String action;
  91. if(diagnosticCommand.getActionValue() == 0) {
  92. throw new UnrecognizedMessageTypeException(
  93. "Diagnostic command missing action");
  94. } else if(diagnosticCommand.getAction() ==
  95. BinaryMessages.DiagnosticControlCommand.Action.ADD) {
  96. action = DiagnosticRequest.ADD_ACTION_KEY;
  97. } else if(diagnosticCommand.getAction() ==
  98. BinaryMessages.DiagnosticControlCommand.Action.CANCEL) {
  99. action = DiagnosticRequest.CANCEL_ACTION_KEY;
  100. } else {
  101. throw new UnrecognizedMessageTypeException(
  102. "Unrecognized action: " + diagnosticCommand.getAction());
  103. }
  104. BinaryMessages.DiagnosticRequest serializedRequest =
  105. diagnosticCommand.getRequest();
  106. DiagnosticRequest request = new DiagnosticRequest(
  107. serializedRequest.getBus(),
  108. serializedRequest.getMessageId(),
  109. serializedRequest.getMode());
  110. request.setPayload(serializedRequest.getPayload().toByteArray());
  111. request.setPid(serializedRequest.getPid());
  112. request.setMultipleResponses(serializedRequest.getMultipleResponses());
  113. request.setFrequency(serializedRequest.getFrequency());
  114. request.setName(serializedRequest.getName());
  115. return new Command(request, action);
  116. }
  117. private static Command deserializeCommand(
  118. BinaryMessages.VehicleMessage binaryMessage)
  119. throws UnrecognizedMessageTypeException {
  120. BinaryMessages.ControlCommand command =
  121. binaryMessage.getControlCommand();
  122. CommandType commandType;
  123. if(command.getTypeValue() != 0) {
  124. BinaryMessages.ControlCommand.Type serializedType = command.getType();
  125. if(serializedType.equals(BinaryMessages.ControlCommand.Type.VERSION)) {
  126. commandType = CommandType.VERSION;
  127. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.DEVICE_ID)) {
  128. commandType = CommandType.DEVICE_ID;
  129. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.PLATFORM)) {
  130. commandType = CommandType.PLATFORM;
  131. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.PASSTHROUGH)) {
  132. commandType = CommandType.PASSTHROUGH;
  133. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.ACCEPTANCE_FILTER_BYPASS)) {
  134. commandType = CommandType.AF_BYPASS;
  135. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.PAYLOAD_FORMAT)) {
  136. commandType = CommandType.PAYLOAD_FORMAT;
  137. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.SD_MOUNT_STATUS)) {
  138. commandType = CommandType.SD_MOUNT_STATUS;
  139. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.RTC_CONFIGURATION)) {
  140. commandType = CommandType.RTC_CONFIGURATION;
  141. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.DIAGNOSTIC)) {
  142. commandType = CommandType.DIAGNOSTIC_REQUEST;
  143. } else {
  144. throw new UnrecognizedMessageTypeException(
  145. "Unrecognized command type in command: " +
  146. command.getType());
  147. }
  148. } else {
  149. throw new UnrecognizedMessageTypeException(
  150. "Command missing type");
  151. }
  152. Command deserializedCommand;
  153. if(commandType.equals(CommandType.DIAGNOSTIC_REQUEST)) {
  154. deserializedCommand = deserializeDiagnosticCommand(command);
  155. } else {
  156. deserializedCommand = new Command(commandType);
  157. }
  158. return deserializedCommand;
  159. }
  160. private static VehicleMessage deserializeDiagnosticResponse(
  161. BinaryMessages.VehicleMessage binaryMessage) {
  162. BinaryMessages.DiagnosticResponse serializedResponse =
  163. binaryMessage.getDiagnosticResponse();
  164. int totalSize = serializedResponse.getTotalSize(); // totalSize field only exists for Multiframe response
  165. if (totalSize > 0) {
  166. com.google.protobuf.ByteString byteString = serializedResponse.getPayload();
  167. String payload = "0x";
  168. for(int cnt=0; cnt<byteString.size(); cnt++) {
  169. int singleByte = byteString.byteAt(cnt);
  170. singleByte = (singleByte < 0) ? singleByte + 256 : singleByte;
  171. payload += (Integer.toHexString(singleByte / 16) + Integer.toHexString( singleByte % 16));
  172. }
  173. MultiFrameResponse response = new MultiFrameResponse(
  174. serializedResponse.getFrame(),
  175. serializedResponse.getTotalSize(),
  176. serializedResponse.getMessageId(),
  177. serializedResponse.getSerializedSize(),
  178. payload);
  179. response.setBus(serializedResponse.getBus());
  180. response.setMode(serializedResponse.getMode());
  181. return response;
  182. }
  183. DiagnosticResponse response = new DiagnosticResponse(
  184. serializedResponse.getBus(),
  185. serializedResponse.getMessageId(),
  186. serializedResponse.getMode());
  187. response.setPid(serializedResponse.getPid());
  188. response.setPayload(serializedResponse.getPayload().toByteArray());
  189. response.setNegativeResponseCode(
  190. DiagnosticResponse.NegativeResponseCode.get(
  191. serializedResponse.getNegativeResponseCode()));
  192. if(serializedResponse.getValue().getTypeValue() == 2) {
  193. response.setValue(serializedResponse.getValue().getNumericValue());
  194. }
  195. return response;
  196. }
  197. private static CommandResponse deserializeCommandResponse(
  198. BinaryMessages.VehicleMessage binaryMessage)
  199. throws UnrecognizedMessageTypeException {
  200. BinaryMessages.CommandResponse response =
  201. binaryMessage.getCommandResponse();
  202. CommandType commandType;
  203. if(response.getTypeValue() != 0) {
  204. BinaryMessages.ControlCommand.Type serializedType = response.getType();
  205. if(serializedType.equals(BinaryMessages.ControlCommand.Type.VERSION)) {
  206. commandType = CommandType.VERSION;
  207. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.DEVICE_ID)) {
  208. commandType = CommandType.DEVICE_ID;
  209. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.PLATFORM)){
  210. commandType = CommandType.PLATFORM;
  211. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.PASSTHROUGH)){
  212. commandType = CommandType.PASSTHROUGH;
  213. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.ACCEPTANCE_FILTER_BYPASS)){
  214. commandType = CommandType.AF_BYPASS;
  215. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.PAYLOAD_FORMAT)){
  216. commandType = CommandType.PAYLOAD_FORMAT;
  217. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.SD_MOUNT_STATUS)){
  218. commandType = CommandType.SD_MOUNT_STATUS;
  219. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.RTC_CONFIGURATION)){
  220. commandType = CommandType.RTC_CONFIGURATION;
  221. } else if(serializedType.equals(BinaryMessages.ControlCommand.Type.DIAGNOSTIC)) {
  222. commandType = CommandType.DIAGNOSTIC_REQUEST;
  223. } else {
  224. throw new UnrecognizedMessageTypeException(
  225. "Unrecognized command type in response: " +
  226. response.getType());
  227. }
  228. } else {
  229. throw new UnrecognizedMessageTypeException(
  230. "Command response missing type");
  231. }
  232. String message = response.getMessage();
  233. return new CommandResponse(commandType, response.getStatus(), message);
  234. }
  235. private static VehicleMessage deserialize(
  236. BinaryMessages.VehicleMessage binaryMessage)
  237. throws UnrecognizedMessageTypeException {
  238. if(binaryMessage.getTypeValue() == 2) {
  239. return deserializeNamedMessage(binaryMessage);
  240. } else if(binaryMessage.getTypeValue() == 1) {
  241. return deserializeCanMessage(binaryMessage);
  242. } else if(binaryMessage.getTypeValue() == 5) {
  243. return deserializeCommandResponse(binaryMessage);
  244. } else if(binaryMessage.getTypeValue() == 4) {
  245. return deserializeCommand(binaryMessage);
  246. } else if(binaryMessage.getTypeValue() == 3) {
  247. return deserializeDiagnosticResponse(binaryMessage);
  248. } else {
  249. throw new UnrecognizedMessageTypeException(
  250. "Binary message type not recognized");
  251. }
  252. }
  253. }