Disclosure of Invention
Aiming at the problems in the prior art, the invention provides an ASN.1 coding and decoding module and a message data coding and decoding method, which can realize the maximum compression ratio of a message, so that the message transmission is safe and reliable and the transmission efficiency is high.
The invention is realized by the following technical scheme:
the invention provides an ASN.1 coding and decoding module which is suitable for an OTA protocol; the module includes:
ASN.1 type library including all base classes for encoding and decoding;
ASN.1 type processing library, including coding and decoding processing method entries corresponding to all coding and decoding types;
the PERALIGNED DECODER class defines a decoding processing method corresponding to all decoding types;
the Decoder class is a father class of the PERALIGNED Decoder class and an implementation class of the IDecoder class;
an IDecoder class, declaring classes for decoded external interfaces;
an IASN1 typeDecoder class which is an interface declaration class of decoding processing methods corresponding to all decoding types;
the PERALIGNED ENCODER class defines the encoding processing method corresponding to all encoding types;
the Encoder class is a father class of the PERALIGNED Encoder class and is an implementation class of the IEncoder class;
IEncoder type, which is an external interface declaration type of codes;
an IASN1TypesEncoder class which is an interface declaration class of the coding processing method corresponding to all coding types;
and the factory class CoderFactory is used for acquiring the IDecoder class or the IEncoder class.
The ASN.1 coding and decoding module defines an ASN.1 coding and decoding mode and provides coding and decoding interfaces of the ASN.1 and OTA. When coding and decoding are carried out, a factory class is utilized to obtain a coding and decoding interface, and a corresponding coding and decoding method is found according to a coding type.
Preferably, the ASN.1 type library comprises a String, an Integer, Boolean, and an Element attribute Element type.
A message data coding and decoding method comprises the following steps:
step S01, receiving message data sent by TBOX to TSP and analyzing to obtain byte array of OTA service request or service response;
step S02, decoding the message header in the byte array and generating a Java message header object; decoding a message main body in the byte array, and generating a Java message main body object corresponding to the OTA message main body by contrasting a protocol entity mapping table; decoding protocol main body data in the byte array, and generating a Java protocol main body data object corresponding to the OTA protocol main body data by contrasting a protocol entity mapping table;
step S03, encoding the Java message head object, the Java message main body object and the Java protocol main body data object and generating a byte array;
step S04, assembling the message header, the message body and the protocol body data as the message data sent to the TBOX by the TSP;
the decoding of the message body and the protocol body data in step S02 and the encoding of the message body and the protocol body data in step S03 are implemented by the asn.1 codec module.
The method of the invention realizes the coding and decoding of the ASN.1 transmitting the message through the transmitting mechanism of Java language. The method uses the unaligned compression coding rule (unaligned-PER) of the ASN.1 to achieve the maximum compression ratio of the message.
Preferably, the decoding process of the header in step S02 is: directly decoding the message header in the byte array into a Java message header object;
the encoding process of the header in step S03 is: and directly coding the Java message header object into a byte array.
Preferably, the decoding process of the message body in step S02 is:
reading binary data of a message body from an input stream, acquiring a decoding external interface statement class IDecoder corresponding to a coding type through a factory class CoderFactory of an ASN.1 coding and decoding module, further acquiring a decoding method corresponding to the decoding type through an ASN.1 type processing library, and further generating a Java message body object;
the encoding process of the message body in step S03 is:
acquiring an encoding external interface statement class IEncoder corresponding to the encoding type through a factory class CoderFactory, further acquiring an encoding method corresponding to the encoding type through an ASN.1 type processing library, and further encoding the Java message main body object.
Preferably, the decoding process of the protocol body data in step S02 is:
reading binary data of a protocol body from an input stream, acquiring a decoding external interface statement class IDecoder corresponding to a coding type through a factory class CoderFactory of an ASN.1 coding and decoding module, further acquiring a decoding method corresponding to the decoding type through an ASN.1 type processing library, and further generating a Java protocol body data object;
the encoding process of the protocol body data in step S03 is:
and acquiring an encoding external interface statement class IEncoder corresponding to the encoding type through a factory class CoderFactory, further acquiring an encoding method corresponding to the encoding type through an ASN.1 type processing library, and further encoding the Java protocol main body data object.
Preferably, the construction process of the asn.1 type library includes: encoding a String type, an Integer type, a Boolean type and an Element attribute Element type.
Preferably, before encoding the Integer type, the maximum minimum interval of the field is declared:
if the range is less than or equal to 255, encoding a bit field of a minimum size of the range;
if the range happens to be 256, then encode as an octet alignment bit field;
if the range is 257 to 64K, then encoding into two octet aligned bit fields;
if the range is greater than 64K, then the encoding is an octal alignment bit field, which is the minimum octal number of encoded values.
Preferably, the construction process of the ASN.1 type library further comprises the following steps: and checking and verifying the attributes in the class, wherein if the range, the attribute and the length exceed the limits of the OTA protocol, the attribute verification in the class is abnormal.
Preferably, in the coding and decoding process, if the data length exceeds the limitation of the OTA protocol, the coding and decoding are judged to be abnormal.
The invention has the following beneficial effects:
the ASN.1 coding and decoding module and the message data coding and decoding method of the invention remove the dependence on ASN.1 files and are more convenient for problem troubleshooting. Meanwhile, due to the fact that a JAVA annotation mode is used, the ASN.1 object is written more simply and conveniently, the byte array coded by the ASN.1 is directly converted into the object, or the JAVA object is converted into the byte array in the ASN.1 format, the JAVA object is directly operated by service development, the ASN.1 coding does not need to be related, and development is more convenient.
Detailed Description
The following are specific embodiments of the present invention and are further described with reference to the drawings, but the present invention is not limited to these embodiments.
ASN.1 as a high-level abstract description language has the following characteristics:
(1) cross-platform cross-language usage. Which represents the data structure information in a highly abstract form. When data structure information needs to be transmitted in a computer network, the ASN.1 provides corresponding coding and decoding rules and transmits the data structure information through corresponding bit patterns.
(2) ASN.1 makes the standard protocols established by different organizations all adopt the same standard representation form, and ensures interoperability.
(3) Asn.1 provides rich data structures and flexible extension mechanisms, and thus can describe more complex protocol contents.
Therefore, an ASN (application service network) coding and decoding module, a coder and decoder and a message data coding and decoding method suitable for the OTA protocol are constructed based on the ASN.1 language, and the logic of the conversion between the Java object and the ASN.1 coded message is realized by using Java reflection.
Referring to fig. 1, the present invention provides an asn coding/decoding module suitable for OTA protocol; the module includes:
ASN.1 type library including all base classes for encoding and decoding;
ASN.1 type processing library including coding and decoding processing method entries corresponding to all coding and decoding types;
the PERALIGNED DECODER class defines a decoding processing method corresponding to all decoding types;
the Decoder class is a father class of the PERALIGNED Decoder class and an implementation class of the IDecoder class;
an IDecoder class which declares classes for the decoded external interfaces;
an IASN1 typeDecoder class which is an interface declaration class of decoding processing methods corresponding to all decoding types;
the PERALIGNED ENCODER class defines the encoding processing method corresponding to all encoding types;
the Decoder class is a father class of the PERALIGNED ENCODER class and an implementation class of the IEncoder class;
IEncoder type, which is an external interface declaration type of codes;
an IASN1TypesEncoder class which is an interface declaration class of the coding processing method corresponding to all coding types;
and the factory class CoderFactory is used for acquiring the IDecoder class or the IEncoder class.
The ASN.1 type library ASN.1-orientations is a direct or indirect base class of all codecs that provides a definition of classes, with elements injected in annotated form in the protocol mapping entity. The ASN.1 type library comprises basic types such as character String, Integer Integer, Boolean, data set Choice \ Sequence of, NULL type, bit String type BitString, octet String type OctetString, floating point type Real, enumeration type Enum and the like, and Element attribute Element types.
The construction process of the ASN.1 type library comprises the following steps: encoding basic types such as String, Integer, Boolean, data set Choice \ Sequence of, NULL type, bit String type BitString, octet String type OctetString, floating point type Real, enumeration type Enum, and Element attribute Element type.
String type ASN1 String: all String types except BitString and OctetString include PrintableString, BMPString, UTF8String, etc. The present system uses PrintableString by default. The number of bytes occupied by PrintableString after coding is the same as that before coding.
Integer type ASN1 Integer: integer, which includes integers and negatives, may not be-0. The system declares the maximum minimum interval of the field before encoding the shapes. Coding according to different intervals:
1. if the range is less than or equal to 255, encoding a bit field of a minimum size of the range;
2. if the range happens to be 256, the value is encoded as an octet alignment bit field;
3. if the range is 257 to 64K, the value is encoded as a two octet aligned bit field;
4. if the range is greater than 64K, the range is ignored and the value is encoded as an octal alignment bit field, which is the minimum octal number for the value. In the latter case, the latter process also encodes a length field (typically one octet) to indicate the length of the encoding. For other cases, the length of the code is independent of the value being coded and is not explicitly coded.
Boolean ASN1 Boolean: the boolean-coded payload is either all 0 or all 1 octets. The header byte starts with 0x01, the length-encoded byte is 0x01, and the payload content depends on the value of the boolean value.
The data set is of a Choice \ Sequence of type ASN1Choice \ ASN1Sequence of: sequence and set type is a type used to collect related data elements as an independent decodable element, and the content of encoding is determined by the fields and types defined in the protocol entity, and encoding is performed in sequence unless there are fields marked optional (isopotential) or default (hasDefaultValue).
NULL type ASN1 NULL: placeholder type, if present, defaults to 0;
bit string type BitString ASN1 BitString: for use in transferring a single byte, the first bit of the bit string is placed into the 8 th bit of the first payload byte; if the last payload byte is not filled, the empty bits are filled with 0 and the number of 0's is stored in the byte whose header represents the padding data.
Octet string type OctetString ASN1 OctetString: the byte array is passed, similar to the BitString type, with the header encoded and the octets copied directly into the past.
Floating point type Real ASN1 Real: the floating point type comprises special values PLUS-INFINITY (positive INFINITY), MINUS-INFINITY (negative INFINITY) and NOT-A-NUMBER (non-numeric) and an M \ B \ E triplet equation consisting of positive 0 and negative 0, wherein M is a mantissa, B is a radix (can be only 2 or 10), and E is an mutexponent. Different coding schemes are used with different cardinalities:
1. radix =2 coding adopts a hardware floating point conversion coding mode with higher efficiency;
2. the radix =10 coding adopts a binary coded decimal number coding mode.
Enumeration type Enum ASN1 Enum: and traversing and encoding the enumerated values.
Element attribute Element:
ISOPtional: declaring that in an element corresponding to an entity class, the value is true or false, the value is valid when a user transfers Sequence, whether the element is optional is defined, and if true and the element is empty, the value is ignored when the group participates.
hasDefaultValue: and declaring that the value in the element corresponding to the entity class is true or false, wherein the value is valid when the user transfers the Sequence, and defining whether the element has a default value, and if the element is true and the element is empty, ignoring the value when the group parameter exists.
hasTag: declaring whether the value of an element corresponding to the entity class is true or false, defining whether the element needs to define tag, and if true, numbering the types by using the defined tag class and tag.
TagClass: the numbering is valid when the hasTag is true, and four tagClass are available, namely univarial, application, context-specific and private, and the default is context-specific.
Tag: the actual coding start value, valid when hasTag is true, is 0 by default.
isimpiicittag: declaring that in the element corresponding to the entity class, the value is true or false, defining whether the element is valid, and the default is true.
The construction process of the ASN.1 type library further comprises the following steps: and checking and verifying the attributes in the class, wherein if the range, the attribute and the length exceed the limits of the OTA protocol, the attribute verification in the class is abnormal.
The ASN.1 type processing library ASN.1-metadata provides all types of corresponding encoding/decoding processing method entries of the encoder/decoder, and the corresponding metadata class is found by declaring animation in the elements of the mapping entity class.
The PERALIGNED ENCODER and PERALIGNED DECODER define all types of processing methods provided by the codec. Some of the treatments are listed below:
PERALIGNED ENCODER class: during encoding, whether the length, type and range corresponding to the field conform to the protocol mapping or not is checked.
Table one: PERALIGNED ENCODER-LIKE
Table two: PERALIGNED DECoders:
the Encoder and Decoder are fathers of the PERALIGNED ENcoder and the PERALIGNED DECODER respectively, and provide realization of an external interface and realization of a processing method interface.
The IEncoder and the IDecoder are external interface declaration types of encoding/decoding respectively, and the external interface calls the IEncoder and the IDecoder which are generated correspondingly through a factory. Their implementation classes are Encoder and Decode.
The IASN1 typeEncoder and the IASN1 typeDecoder are interface declaration classes of processing methods of the types of the coder/decoder respectively, and define corresponding processing method interfaces of all coding types. Finding out the corresponding metadata class according to the field type in the protocol, and acquiring a specific implementation method through the interface class of the IASN1TypesEncoder and the IASN1 TypesDecoder.
The CoderFactory factory class is used to obtain the IDecoder or IEncoder interface. And also for generating the metadata class corresponding to the IASN1 preparedElementData.
Referring to fig. 2, the present invention provides a codec, which includes an OTA codec module, a protocol entity mapping module, and the asn.1 codec module. The protocol entity mapping module defines entity mapping of all OTA protocols, and specifically comprises a protocol entity mapping table which can acquire Java protocol entities corresponding to the OTA protocol entities. And the OTA coding and decoding module is used for encrypting and decrypting the message data between the TBOX and the TSP.
As shown in fig. 3, the present invention provides a message data encoding and decoding method, which is specifically applied to an OTA encoding and decoding module, and the method includes:
step S01, receiving message data sent by TBOX to TSP and analyzing to obtain byte array of OTA service request or service response;
step S02, decoding the message header in the byte array and generating a Java message header object; decoding a message main body in the byte array, and generating a Java message main body object corresponding to the OTA message main body by contrasting a protocol entity mapping table; decoding protocol main body data in the byte array, and generating a Java protocol main body data object corresponding to the OTA protocol main body data by contrasting a protocol entity mapping table;
step S03, encoding the Java message head object, the Java message main body object and the Java protocol main body data object and generating a byte array;
step S04, assembling the message header, the message body and the protocol body data as the message data sent to the TBOX by the TSP;
the decoding of the message body and the protocol body data in step S02 and the encoding of the message body and the protocol body data in step S03 are implemented by the asn.1 codec module.
In step S01, TBOX _ otareq: the Request class, representing an OTA service Request or response, includes Header, Body and protocol Body data. Firstly, receiving message data sent to the TSP by the TBOX, namely the byte number of the OTA message, and analyzing the message data. And then converting the OTARequest into a byte array.
The decoding process of the header in step S02 is: and directly decoding the message header in the byte array into a Java message header object. TBOX _ DispatcherHeader: the Header of the protocol map contains the total Byte length of the current message, the total number of bytes (Byte) of Dispatcher Header and Dispatcher Body. unwrappidpitcher header (): the DispatcherHeader is decoded without the encoding algorithm. The Header object is returned.
The decoding process of the message body in step S02 is: binary data of a message body is read from an input stream, a decoding external interface statement class IDecoder corresponding to a coding type is obtained through a factory class CoderFactory of an ASN.1 coding and decoding module, a decoding method corresponding to a decoding type is obtained through an ASN.1 type processing library, and a Java message body object is generated. TBOX _ DispatcherBody: the body of the protocol map includes an AID (Application classification indicating transferred Application Data), an MID (unique identification of a message under the same AID), an Event Creation Time (message Creation Time), an Application Data Length (message body Byte Length), and the like. Specifically, unwrappidispatchebody (): binary data is read from the InputStream for decoding and a dispatchbody object is generated.
The decoding process of the protocol body data in step S02 is: reading binary data of a protocol body from an input stream, acquiring a decoding external interface statement class IDecoder corresponding to a coding type through a factory class CoderFactory of an ASN.1 coding and decoding module, further acquiring a decoding method corresponding to a decoding type through an ASN.1 type processing library, and further generating a Java protocol body data object. Specifically, unwrappalindrordata (): binary data is read from the InputStream for decoding, and a java protocol body data object is generated. And acquiring an IDecoder method corresponding to a fixed coding type (fixed as PER/Unaligned) through CoderFactory, wherein the transmission parameter is a JAVA object class for receiving decoded data. And reading the data in the InputStream for decoding.
The encoding process of the header in step S03 is: and directly coding the Java message header object into a byte array. wrapppitcher header (): and (3) coding the DispatcherHeader, and directly converting the TBOX _ DispatcherHeader class of the group in the parameter into a byte [ ] array without a coding algorithm.
The encoding process of the message body in step S03 is: acquiring an encoding external interface statement class IEncoder corresponding to the encoding type through a factory class CoderFactory, further acquiring an encoding method corresponding to the encoding type through an ASN.1 type processing library, and further encoding the Java message main body object. Specifically, bob needs to perform asn.1 encoding separately, and performs different instance encoding according to different instances.
The encoding process of the protocol body data in step S03 is: and acquiring an encoding external interface statement class IEncoder corresponding to the encoding type through a factory class CoderFactory, further acquiring an encoding method corresponding to the encoding type through an ASN.1 type processing library, and further encoding the Java protocol main body data object. Specifically, a JAVA protocol main data object is coded, an IEncoder method corresponding to a fixed coding type (here, fixed as PER/Unaligned) is obtained through CoderFactory, a parameter is a JAVA object class needing coding, and after the coding is completed, data is written into BitAlrayOutputStream.
In step S04, the total data length including Header, Body, and appdata is calculated and set in the Header, and finally, the complete data message is output.
In addition, the method of the invention also comprises the step of judging that the coding and the decoding are abnormal when the data length exceeds the limitation of the OTA protocol in the coding and decoding process. Different exceptions may be captured during encoding/decoding of different data types, for example, when encoding an Integer type, if the data Length exceeds 0x4000, an exception reminder "Not supported for this version. When the enumeration type is decoded, if the data length < =0, then an "Unable to present and item!" exception reminder, etc. is thrown. OTA encoding/decoding defines TBOX _ AdapterException exception class, and the class inherits RuntimeException.
The invention uses the Unaligned compression coding rule (Unaligned-PER) of the ASN.1 to achieve the maximum compression ratio of the message. The invention can realize the coding modes of BER, DER, PER and the like at the same time, but mainly uses Unaligned-PER as the main mode.
The invention solves the dependence on the ASN.1 file and is more convenient to carry out problem troubleshooting. Meanwhile, due to the fact that a JAVA annotation mode is used, the ASN.1 object is written more simply and conveniently, the byte array coded by the ASN.1 is directly converted into the object, or the JAVA object is converted into the byte array in the ASN.1 format, the JAVA object is directly operated by service development, the ASN.1 coding does not need to be related, and development is more convenient.
It will be appreciated by persons skilled in the art that the embodiments of the invention described above and shown in the drawings are given by way of example only and are not limiting of the invention. The objects of the present invention have been fully and effectively accomplished. The functional and structural principles of the present invention have been shown and described in the examples, and any variations or modifications of the embodiments of the present invention may be made without departing from the principles.