Skip to content

Commit

Permalink
Assing Basic BSM Message Decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Wiens committed May 8, 2024
1 parent d9e9ccf commit 2b4c1a5
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import us.dot.its.jpo.ode.api.asn1.Decoder;
import us.dot.its.jpo.ode.api.models.messages.BsmDecodedMessage;
import us.dot.its.jpo.ode.api.models.messages.DecodedMessage;
import us.dot.its.jpo.ode.model.OdeBsmData;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.scheduling.annotation.EnableScheduling;

Expand All @@ -22,8 +27,20 @@ public static void main(String[] args) {
System.out.println("Started Conflict Monitor API");
System.out.println("Conflict Monitor API docs page found here: http://localhost:8081/swagger-ui/index.html");
System.out.println("Startup Complete");

BsmDecodedMessage bsm = (BsmDecodedMessage)Decoder.decode("00145144ad0b7947c2ed9ad2748035a4e8ff880000000fd2229199307d7d07d0b17fff05407d12720038c000fe72c107b001ea88fffeb4002127c0009000000fdfffe3ffff9407344704000041910120100000000efc10609c26e900e11f61a947802127c0009000000fdfffe3ffff9407453304000041910120100000008ffffe501ca508100000000000a508100000404804000000849f00024000003f7fff8ffffe501ca508100000fe501ca508100000fffe501ca51c10000000024000003f7fff8ffffe501ca51c1");




System.out.println("Fully Decoded BSM" + bsm.getData());
// OdeBsmData decodeTest;
// try {
// decodeTest = OdeBsmDataCreatorHelper.createOdeBsmData("<?xml version=\"1.0\"?><OdeAsn1Data><metadata><bsmSource>RV</bsmSource><logFileName/><recordType>bsmTx</recordType><securityResultCode>success</securityResultCode><receivedMessageDetails><locationData><latitude/><longitude/><elevation/><speed/><heading/></locationData><rxSource>RV</rxSource></receivedMessageDetails><encodings><encodings><elementName>unsecuredData</elementName><elementType>MessageFrame</elementType><encodingRule>UPER</encodingRule></encodings></encodings><payloadType>us.dot.its.jpo.ode.model.OdeAsn1Payload</payloadType><serialId><streamId>f48cd9fa-cf32-4542-9cfd-4ff0542d56b0</streamId><bundleSize>1</bundleSize><bundleId>0</bundleId><recordId>0</recordId><serialNumber>0</serialNumber></serialId><odeReceivedAt>2024-05-07T18:45:29.796137Z</odeReceivedAt><schemaVersion>6</schemaVersion><maxDurationTime>0</maxDurationTime><recordGeneratedAt/><recordGeneratedBy/><sanitized>false</sanitized><odePacketID/><odeTimStartDateTime/><originIp>10.165.5.230</originIp></metadata><payload><dataType>MessageFrame</dataType><data><MessageFrame><messageId>20</messageId><value><BasicSafetyMessage><coreData><msgCnt>105</msgCnt><id>21234841</id><secMark>29609</secMark><lat>403192909</lat><long>-1117049264</long><elev>14321</elev><accuracy><semiMajor>40</semiMajor><semiMinor>40</semiMinor><orientation>8100</orientation></accuracy><transmission><forwardGears/></transmission><speed>542</speed><heading>13208</heading><angle>127</angle><accelSet><long>2001</long><lat>2001</lat><vert>-127</vert><yaw>0</yaw></accelSet><brakes><wheelBrakes>01111</wheelBrakes><traction><unavailable/></traction><abs><unavailable/></abs><scs><unavailable/></scs><brakeBoost><unavailable/></brakeBoost><auxBrakes><unavailable/></auxBrakes></brakes><size><width>320</width><length>1280</length></size></coreData><partII><PartIIcontent><partII-Id>0</partII-Id><partII-Value><VehicleSafetyExtensions><pathHistory><crumbData><PathHistoryPoint><latOffset>-14434</latOffset><lonOffset>6167</lonOffset><elevationOffset>8</elevationOffset><timeOffset>1268</timeOffset></PathHistoryPoint><PathHistoryPoint><latOffset>-32277</latOffset><lonOffset>13981</lonOffset><elevationOffset>12</elevationOffset><timeOffset>2522</timeOffset></PathHistoryPoint></crumbData></pathHistory><pathPrediction><radiusOfCurve>4079</radiusOfCurve><confidence>120</confidence></pathPrediction></VehicleSafetyExtensions></partII-Value></PartIIcontent><PartIIcontent><partII-Id>2</partII-Id><partII-Value><SupplementalVehicleExtensions><classification>54</classification><classDetails><keyType>54</keyType><hpmsType><none/></hpmsType></classDetails><vehicleData><height>58</height><mass>109</mass></vehicleData><weatherReport><isRaining><error/></isRaining><rainRate>65535</rainRate><precipSituation><unknown/></precipSituation><solarRadiation>65535</solarRadiation><friction>101</friction><roadFriction>0</roadFriction></weatherReport><weatherProbe><airTemp>51</airTemp></weatherProbe></SupplementalVehicleExtensions></partII-Value></PartIIcontent></partII></BasicSafetyMessage></value></MessageFrame></data></payload></OdeAsn1Data>");
// System.out.println(decodeTest);
// } catch (XmlUtilsException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}

// @Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package us.dot.its.jpo.ode.api.asn1;

import lombok.extern.slf4j.Slf4j;
import us.dot.its.jpo.ode.api.models.Message;
import us.dot.its.jpo.ode.api.models.MessageType;
import us.dot.its.jpo.ode.api.models.TypePayload;
import us.dot.its.jpo.ode.coder.OdeBsmDataCreatorHelper;
import us.dot.its.jpo.ode.api.models.messages.BsmDecodedMessage;
import us.dot.its.jpo.ode.api.models.messages.DecodedMessage;
import us.dot.its.jpo.ode.model.Asn1Encoding;
import us.dot.its.jpo.ode.model.Asn1Encoding.EncodingRule;
import us.dot.its.jpo.ode.model.OdeAsn1Data;
import us.dot.its.jpo.ode.model.OdeAsn1Payload;
import us.dot.its.jpo.ode.model.OdeBsmData;
import us.dot.its.jpo.ode.model.OdeBsmMetadata;
import us.dot.its.jpo.ode.model.OdeBsmMetadata.BsmSource;
import us.dot.its.jpo.ode.model.OdeBsmPayload;
import us.dot.its.jpo.ode.model.OdeData;
import us.dot.its.jpo.ode.model.OdeHexByteArray;
import us.dot.its.jpo.ode.model.OdeLogMetadata.RecordType;
Expand All @@ -20,13 +21,19 @@
import us.dot.its.jpo.ode.model.OdeMsgPayload;
import us.dot.its.jpo.ode.model.ReceivedMessageDetails;
import us.dot.its.jpo.ode.model.RxSource;
import us.dot.its.jpo.ode.plugin.j2735.builders.BsmBuilder;
import us.dot.its.jpo.ode.util.JsonUtils;
import us.dot.its.jpo.ode.util.XmlUtils;
import us.dot.its.jpo.ode.util.XmlUtils.XmlUtilsException;
import us.dot.its.jpo.ode.context.AppContext;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

import java.io.*;
import java.nio.charset.StandardCharsets;
Expand All @@ -50,7 +57,7 @@ public class Decoder {
public static final int HEADER_MINIMUM_SIZE = 20;
public static final int bufferSize = 2048;

public static Message decode(String inputAsn1){
public static DecodedMessage decode(String inputAsn1){

// Identify Message Type and Cut off any extra characters
TypePayload payload = identifyAsn1(inputAsn1);
Expand All @@ -59,22 +66,21 @@ public static Message decode(String inputAsn1){
// Convert Payload to Pojo and add Metadata
OdeData data = getAsOdeData(payload);



XmlUtils xmlUtils = new XmlUtils();

try {
// Convert to XML for ASN.1 Decoder
String xml = xmlUtils.toXml(data);
System.out.println("XML" + xml);

// Send String through ASN.1 Decoder to get Decoded XML Data
String decodedXml = decodeXmlWithAcm(xml);
// String decodedXml = decodeXmlWithAcm(xml);
String decodedXml = mockDecodeXmlWithAcm(xml);

if(payload.getType() == MessageType.BSM){
OdeBsmData bsm = OdeBsmDataCreatorHelper.createOdeBsmData(decodedXml);
Message<OdeBsmData> message = new Message();
message.setAsn1Text(inputAsn1);
message.setDecodedMessage(bsm);
if(payload.getType().equals(MessageType.BSM)){
OdeBsmData bsm = createOdeBsmData(decodedXml);
DecodedMessage message = new BsmDecodedMessage(bsm, inputAsn1, MessageType.BSM, "");
return message;
}

} catch (JsonProcessingException e) {
Expand All @@ -88,7 +94,7 @@ public static Message decode(String inputAsn1){
return null;
}

public static Message decodeAsType(String inputAsn1, MessageType type){
public static DecodedMessage decodeAsType(String inputAsn1, MessageType type){
String payload = removeHeader(inputAsn1, type);

if(payload != null){
Expand Down Expand Up @@ -143,6 +149,31 @@ public static OdeData getBsmAsOdeData(String encodedPayload){
return new OdeAsn1Data(metadata, payload);
}

public static OdeBsmData createOdeBsmData(String consumedData) throws XmlUtilsException {
ObjectNode consumed = XmlUtils.toObjectNode(consumedData);

JsonNode metadataNode = consumed.findValue(AppContext.METADATA_STRING);
if (metadataNode instanceof ObjectNode) {
ObjectNode object = (ObjectNode) metadataNode;
object.remove(AppContext.ENCODINGS_STRING);
}

OdeBsmMetadata metadata = (OdeBsmMetadata) JsonUtils.fromJson(
metadataNode.toString(), OdeBsmMetadata.class);

/*
* ODE-755 and ODE-765 Starting with schemaVersion=5 receivedMessageDetails
* will be present in BSM metadata. None should be present in prior versions.
*/
if (metadata.getSchemaVersion() <= 4) {
metadata.setReceivedMessageDetails(null);
}

OdeBsmPayload payload = new OdeBsmPayload(
BsmBuilder.genericBsm(consumed.findValue("BasicSafetyMessage")));
return new OdeBsmData(metadata, payload);
}




Expand Down Expand Up @@ -240,15 +271,22 @@ public static TypePayload identifyAsn1(String hexPacket){
}


public static String mockDecodeXmlWithAcm(String xmlMessage){
return "<?xml version=\"1.0\"?><OdeAsn1Data><metadata><bsmSource>RV</bsmSource><logFileName/><recordType>bsmTx</recordType><securityResultCode>success</securityResultCode><receivedMessageDetails><locationData><latitude/><longitude/><elevation/><speed/><heading/></locationData><rxSource>RV</rxSource></receivedMessageDetails><encodings><encodings><elementName>unsecuredData</elementName><elementType>MessageFrame</elementType><encodingRule>UPER</encodingRule></encodings></encodings><payloadType>us.dot.its.jpo.ode.model.OdeAsn1Payload</payloadType><serialId><streamId>8829c539-e684-40b7-a786-692acd3f897a</streamId><bundleSize>1</bundleSize><bundleId>0</bundleId><recordId>0</recordId><serialNumber>0</serialNumber></serialId><odeReceivedAt>2024-05-08T20:47:53.830130272Z</odeReceivedAt><schemaVersion>6</schemaVersion><maxDurationTime>0</maxDurationTime><recordGeneratedAt/><recordGeneratedBy/><sanitized>false</sanitized><odePacketID/><odeTimStartDateTime/><originIp/></metadata><payload><dataType>MessageFrame</dataType><data><MessageFrame><messageId>20</messageId><value><BasicSafetyMessage><coreData><msgCnt>18</msgCnt><id>B42DE51F</id><secMark>2998</secMark><lat>0</lat><long>0</long><elev>0</elev><accuracy><semiMajor>0</semiMajor><semiMinor>0</semiMinor><orientation>8100</orientation></accuracy><transmission><forwardGears/></transmission><speed>1315</speed><heading>6448</heading><angle>-1</angle><accelSet><long>0</long><lat>0</lat><vert>50</vert><yaw>0</yaw></accelSet><brakes><wheelBrakes>00000</wheelBrakes><traction><on/></traction><abs><on/></abs><scs><on/></scs><brakeBoost><unavailable/></brakeBoost><auxBrakes><unavailable/></auxBrakes></brakes><size><width>250</width><length>590</length></size></coreData><partII><BSMpartIIExtension><partII-Id>0</partII-Id><partII-Value><VehicleSafetyExtensions><pathHistory><crumbData><PathHistoryPoint><latOffset>-795</latOffset><lonOffset>2109</lonOffset><elevationOffset>0</elevationOffset><timeOffset>62789</timeOffset></PathHistoryPoint></crumbData></pathHistory><pathPrediction><radiusOfCurve>32767</radiusOfCurve><confidence>180</confidence></pathPrediction></VehicleSafetyExtensions></partII-Value></BSMpartIIExtension><BSMpartIIExtension><partII-Id>2</partII-Id><partII-Value><SupplementalVehicleExtensions><classification>0</classification><classDetails><keyType>0</keyType><hpmsType><none/></hpmsType></classDetails><vehicleData/><weatherReport><isRaining><error/></isRaining><rainRate>65535</rainRate><precipSituation><unknown/></precipSituation><solarRadiation>65535</solarRadiation><friction>101</friction><roadFriction>0</roadFriction></weatherReport><weatherProbe><airTemp>52</airTemp><airPressure>71</airPressure><rainRates><statusFront><off/></statusFront><rateFront>0</rateFront></rainRates></weatherProbe></SupplementalVehicleExtensions></partII-Value></BSMpartIIExtension><BSMpartIIExtension><partII-Id>1</partII-Id><partII-Value><SpecialVehicleExtensions><vehicleAlerts><notUsed>0</notUsed><sirenUse><notInUse/></sirenUse><lightsUse><notInUse/></lightsUse><multi><unavailable/></multi><events><notUsed>0</notUsed><event>1000000000000000</event></events></vehicleAlerts></SpecialVehicleExtensions></partII-Value></BSMpartIIExtension></partII></BasicSafetyMessage></value></MessageFrame></data></payload></OdeAsn1Data>";
}


public static String decodeXmlWithAcm(String xmlMessage) throws Exception {


System.out.println("Decoding Message: " + xmlMessage);
log.info("Decoding message: {}", xmlMessage);

// Save XML to temp file
String tempDir = FileUtils.getTempDirectoryPath();
String tempFileName = "asn1-codec-java-" + UUID.randomUUID().toString() + ".xml";
log.info("Temp file name: {}", tempFileName);
System.out.println("Temp File Name: " + tempFileName);
Path tempFilePath = Path.of(tempDir, tempFileName);
File tempFile = new File(tempFilePath.toString());
FileUtils.writeStringToFile(tempFile, xmlMessage, StandardCharsets.UTF_8);
Expand All @@ -260,9 +298,10 @@ public static String decodeXmlWithAcm(String xmlMessage) throws Exception {
Process process = pb.start();
String result = IOUtils.toString(process.getInputStream(), StandardCharsets.UTF_8);
log.info("Result: {}", result);
System.out.println("Decode Result: " + result);

// Clean up temp file
tempFile.delete();
// tempFile.delete();

return result;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package us.dot.its.jpo.ode.api.models;

public class UploadAsn1 {
MessageType type;
String asn1;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package us.dot.its.jpo.ode.api.models.messages;

import us.dot.its.jpo.ode.api.models.MessageType;
import us.dot.its.jpo.ode.model.OdeBsmData;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@ToString
@Setter
@Getter
public class BsmDecodedMessage extends DecodedMessage{
public OdeBsmData data;

public BsmDecodedMessage(OdeBsmData data, String asn1Text, MessageType type, String decodeErrors) {
super(asn1Text, type, decodeErrors);
this.data = data;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@


package us.dot.its.jpo.ode.api.models.messages;

import java.time.Instant;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import us.dot.its.jpo.ode.api.models.MessageType;

@ToString
@Setter
@EqualsAndHashCode
@Getter
public class DecodedMessage {
String asn1Text;
long decodeTime;
String decodeErrors;
String type;

public DecodedMessage(String asn1Text, MessageType type, String decodeErrors){
this.asn1Text = asn1Text;
this.decodeTime = Instant.now().toEpochMilli();
this.decodeErrors = decodeErrors;
this.type = type.name();
}
}



0 comments on commit 2b4c1a5

Please sign in to comment.