yafred / asn1-tool

A java tool to generate Java encoders and decoders from ASN.1 specifications.
MIT License
43 stars 22 forks source link

java.lang.NullPointerException thrown when BIT STRING #21

Closed filatan closed 1 year ago

filatan commented 1 year ago

I am trying to compile the attached ASN file but I am getting the following error. I have done some troubleshooting and narrowed the cause to the following line in the ASN file:

ITS-Container.txt

DrivingLaneStatus ::= BIT STRING (SIZE (1..13))

When I remove this line the file is compiled correctly.

Any idea why is this line is not accepted by the compiler?

Thank you

yafred commented 1 year ago

It's fixed ... do you need a version or do you try with master ?

filatan commented 1 year ago

Thank you, I was using the compiler found here https://github.com/yafred/asn1-tool/releases After compiling the branch master and using the jar it works.

I have another issue because I am encoding an object using the generated code of an ASN and even that the verify operation works and doesn't return any error when I try to decode the same object I get the following error:

Exception in thread "main" java.lang.Exception: Unexpected byte (expected: -119, actual: -118 at com.yafred.asn1.runtime.BERReader.mustMatchTag(BERReader.java:93)

Any idea about why could this happen?

Thank you

yafred commented 1 year ago

can you put more details about the error ?

filatan commented 1 year ago

I am trying to decompile a PDU which I get in Hexadecimal but I am always getting a mismatch error. Could you please indicate to which format I should convert my HEX PDU to decode it?

The PDU is "0201D5222116C0EA91108B26C49181E42899C460790A267D3316C8F6EF22833000000000DBBA1F0F9C3C0070100000"

Thank you

yafred commented 1 year ago

Sorry, I can't To decode some data, you need to know what PDU you are expecting and which Encoding Rules were used by the sender. I can just say that this data is not Tag/Length form

Serfermor commented 1 year ago

I'm trying to use the compiler to encode a DENM message (https://forge.etsi.org/rep/ITS/asn1/denm_en302637_3) My code right now look like this.

public class CreateDENMMessage {

public static void main(String[] args) {
    // Create an instance of DENM
    DENM denmMessage = new DENM();

    // Set Header Information (if required)
    ItsPduHeader header = new ItsPduHeader();
    header.setProtocolVersion(2);
    header.setMessageID(MessageID.DENM);
    header.setStationID(1280119804);
    // Set header attributes here
    denmMessage.setHeader(header);

    // Create a DecentralizedEnvironmentalNotificationMessage
    DecentralizedEnvironmentalNotificationMessage denm = new DecentralizedEnvironmentalNotificationMessage();

    ManagementContainer manage = new ManagementContainer();

    ActionID action = new ActionID();
    action.setOriginatingStationID(1280119804);
    action.setSequenceNumber(1);
    manage.setActionID(action);

    manage.setDetectionTime(622991125); //622991127886
    manage.setReferenceTime(622991129); //622991127890
    manage.setTermination(Termination.Enum.ISCANCELLATION);

    ReferencePosition refpos = new ReferencePosition();
    refpos.setLatitude(367474912);
    refpos.setLongitude(-45555876);

    PosConfidenceEllipse ellipse = new PosConfidenceEllipse();
    ellipse.setSemiMajorConfidence(400);
    ellipse.setSemiMinorConfidence(400);
    ellipse.setSemiMajorOrientation(0);
    refpos.setPositionConfidenceEllipse(ellipse);

    Altitude alti = new Altitude();
    alti.setAltitudeValue(10717);
    alti.setAltitudeConfidence(AltitudeConfidence.Enum.ALT_000_02);
    refpos.setAltitude(alti);

    manage.setEventPosition(refpos);
    manage.setRelevanceDistance(RelevanceDistance.Enum.LESSTHAN500M);
    manage.setRelevanceTrafficDirection(RelevanceTrafficDirection.Enum.ALLTRAFFICDIRECTIONS);
    manage.setValidityDuration(7);
    manage.setTransmissionInterval(500);
    manage.setStationType(15);

    denm.setManagement(manage);

    SituationContainer situ = new SituationContainer();

    situ.setInformationQuality(0);

    CauseCode event = new CauseCode();
    event.setCauseCode(CauseCodeType.ACCIDENT);
    event.setSubCauseCode(0);
    situ.setEventType(event);

    situ.setLinkedCause();
    situ.setEventHistory();

    denm.setSituation(situ);

    LocationContainer loca = new LocationContainer();

    Speed event_sp = new Speed();
    event_sp.setSpeedValue(0);
    event_sp.setSpeedConfidence(127);
    loca.setEventSpeed(event_sp);
    loca.setRoadType(null);

    Heading event_h = new Heading();
    event_h.setHeadingValue(0);
    event_h.setHeadingConfidence(127);
    loca.setEventPositionHeading(event_h);

    loca.setTraces();
    denm.setLocation(loca);

    // Add the DecentralizedEnvironmentalNotificationMessage to the DENM
    denmMessage.setDenm(denm);

    // Validate the DENM message (if required)
    try {
        //DENM.validate(denmMessage);
    } catch (Exception e) {
        // Handle validation errors
        e.printStackTrace();
    }

// Encode a DENM
ByteArrayOutputStream bufferOut = new ByteArrayOutputStream();
BERWriter berWriter = new BERWriter(bufferOut);
try{
    DENM.writePdu(denmMessage, berWriter);

} catch (Exception e) {
    e.printStackTrace();
}

byte[] berEncoded = bufferOut.toByteArray();
    // Convert the byte array to a hex string
    String berHex = bytesToHex(berEncoded);
    System.out.println("Encoded DENM in BER hex: " + berHex);

}

The compilation is error free but the hex string is not the expected, any idea why would I'm not getting the correct hex string? It can be tested on this website (https://www.marben-products.com/decoder-asn1-automotive/) 'decode from direct input' option.

The encoded hex string although it is not correct it can be decoded back to the original values defined in the script pasted.

Thanks in advance.