Communication protocol encoding method and system
By using the communication protocol general description configuration file, protocol encoding based on object instance is realized, which solves the problems of large workload of communication protocol encoding, redundant code and difficult maintenance in the prior art, and realizes universal, dynamic coding and efficient maintenance of protocols. .
Patent Information
- Application Number
- CN202211663503.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-12-23
- Publication Date
- 2025-02-21
- Estimated Expiration
- 2042-12-23
AI Technical Summary
The prior art requires a lot of development and maintenance work when encoding communication protocols, and lacks support for private protocols, especially TLV encoding protocols, resulting in code redundancy, maintenance difficulties and inability to reuse.
By using the communication protocol general description configuration file, protocol encoding based on object instances is realized, encoding processing of byte items, duplicate items, subprotocol items and encryption items is supported, and flexible configuration of multi-byte numerical type fields byte order, field byte order, and multi-byte numerical type field .
It realizes universal and dynamic encoding of communication protocols, satisfies universality and scalability, reduces development costs and time, and improves code reusability and maintenance efficiency.
Smart Images

Figure CN116016691B_ABST
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of coding technology, and in particular relates to a communication protocol coding method and system. Background Art
[0002] The statements in this section merely provide background information related to the present invention and do not necessarily constitute prior art.
[0003] In the Internet of Things and Internet communications, a large number of communication protocols are used in device communications, including standard protocols and private protocols. In particular, when there are no relevant standards, various equipment manufacturers use a large number of private protocols specified within the enterprise. Whether it is a standard protocol or a private protocol, the protocol coding running on these devices requires a lot of development and maintenance work.
[0004] Communication protocol encoding methods are usually implemented by writing code in a "hard-coded" way. For each communication protocol, a set of codes needs to be developed specifically to encode the communication protocol data. The code writing workload is large and takes a long time. Due to defect repair or demand changes, even if the protocol frame structure is the same, but the protocol instruction message contains different data, the code must be repeatedly written for encoding, resulting in code redundancy, difficult maintenance, and difficult code reuse.
[0005] Although some methods have been proposed for some protocol encodings, these methods do not support the TLV (or TTLV) encoding protocols commonly used in private protocols or the support is not simple and friendly enough. There is a lack of support for the flexible configuration of big-endian or little-endian for multi-byte numerical type fields in the encoding. The protocol description is not considered comprehensively in the encoding and cannot meet the universality requirements. General protocol parsing and decoding are only applicable to fixed-length messages or some cases of variable-length protocols and are not extensible. That is, when a protocol cannot be parsed or encoded by the existing code, special customized modifications can only be made to the existing code, and after the modification, it cannot be used for other communication protocols and cannot be reused. Summary of the invention
[0006] In order to solve the above problems, the present invention proposes a communication protocol encoding method and system, which realizes universal encoding according to a communication protocol general description configuration file and generates a protocol message based on original data or object instances.
[0007] According to some embodiments, the present invention adopts the following technical solutions:
[0008] A communication protocol encoding method comprises the following steps:
[0009] Obtain the protocol item definition corresponding to the general description file of the protocol instruction according to the protocol ID;
[0010] Initialize the byte array variable for message storage;
[0011] Get the object instance to be encoded, the protocol data item set and the message, and perform protocol block encoding;
[0012] Traverse the protocol data items, and for each protocol data item, determine whether it is a byte item, a repeated item, a sub-protocol item, or an encrypted item, and perform corresponding encoding processing;
[0013] Insert the encoded byte array into the message;
[0014] Calculate and fill the length of the protocol data area into the message;
[0015] Calculate the checksum function and fill the result value into the message to form the final message data.
[0016] As an optional implementation, the object instance to be encoded can be replaced with a JSON object or a JSON string.
[0017] As an optional implementation, if the protocol data item is a byte item that does not contain a bit item, the bytes are read according to the byte item definition, and the result is converted according to the defined numerical type to obtain the corresponding byte array; if a multi-byte numerical type is defined and the byte order is little endian, the bytes are processed in reverse order;
[0018] If the byte item contains a bit item, encode the bit item first, obtain the value after the operation according to the bit item definition, convert it into a byte array, and then perform byte item encoding according to the byte item definition that does not contain a bit item to obtain the conversion result.
[0019] As an optional implementation, if the protocol data item is a repeated item and is a TLV type repeated item, read the Tag according to the definition and obtain the value Value of the Tag in the attribute object, and then call the byte item encoding to obtain the byte array of Value, and then obtain the byte length Length of Value, and obtain the encoding result of Tag, Length, and Value.
[0020] As an optional implementation, if the protocol data item is a repeated item data and a non-TLV type repeated item, the protocol block encoding is called according to its protocol definition data item set and value to obtain the encoded byte array.
[0021] As an optional implementation, if it is sub-protocol item data, the protocol ID corresponding to the sub-protocol description file is obtained according to the definition, the protocol item set of the sub-protocol is loaded, and then the protocol block encoding is performed on it.
[0022] As an optional implementation, if it is an encrypted item, the protocol block encoding is called according to the protocol data item set inside the encrypted data item to obtain the original array of the message, and the encryption function is used for calculation to obtain the encrypted message array.
[0023] As an optional implementation, if there are nested protocol data items inside the protocol data item, the protocol block encoding method is recursively called in order from the upper layer to the lower layer to implement encoding.
[0024] As an optional implementation, if there is a valueExp expression, a custom encoding method or a custom encryption method in the protocol encoding process, the custom method is used first or the expression is operated on, otherwise the conventional encoding method or the built-in encryption method within the protocol is used for encoding and encryption.
[0025] A communication protocol encoding system, comprising:
[0026] The file reading module reads the general description file of the protocol instruction according to the protocol ID, verifies the file format and elements, and parses the corresponding protocol item definition elements in the file after passing the verification;
[0027] An element conversion module converts the protocol definition item elements obtained by the file reading module into class object instances in an object-oriented language and obtains a set definition of protocol data items;
[0028] The protocol encoding module is configured to obtain an instance of an object to be encoded or a JSON object or a JSON string, a set of protocol data items, traverse the protocol data items, and for each protocol data item, determine whether it is a byte item, a repeated item, a sub-protocol item, and an encrypted item, perform corresponding encoding processing on the object to be encoded, execute protocol encoding, calculate the message length and check value, and fill them into the message to obtain the final communication message data.
[0029] Compared with the prior art, the present invention has the following beneficial effects:
[0030] The present invention provides a protocol coding method based on a general description method of a communication protocol, which can realize universal and dynamic coding of communication protocols in most cases, meet the versatility and have extensibility. The protocol coding method realizes the coverage of most communication decoding requirements, and can realize dynamic coding of new protocols without re-coding. Even if extremely rare special customized protocols appear, custom coding, generation of checksums, encryption, etc. can be realized by writing custom classes that inherit the base class, so it has extensibility. Therefore, the method realizes the rapid coding of the protocol through versatility and extensibility, has higher development efficiency, and reduces development costs.
[0031] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, preferred embodiments are given below and described in detail with reference to the accompanying drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0032] The accompanying drawings in the specification, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.
[0033] Figure 1 It is composed of general description of communication protocol;
[0034] Figure 2 It is a protocol encoding flow chart;
[0035] Figure 3a , 3b is a coding example. DETAILED DESCRIPTION
[0036] The present invention will be further described below in conjunction with the accompanying drawings and embodiments.
[0037] It should be noted that the following detailed descriptions are all illustrative and intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meanings as those commonly understood by those skilled in the art to which the present invention belongs.
[0038] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is also intended to include the plural form. In addition, it should be understood that when the terms "comprising" and / or "including" are used in this specification, it indicates the presence of features, steps, operations, devices, components and / or combinations thereof.
[0039] like Figure 1 As shown, a set of communication protocols can be composed of multiple instructions. The same set of protocols uses the same or similar protocol frame structure, and different protocol instructions have different data part structures; the communication protocol general description method describes each protocol instruction.
[0040] The protocol instruction consists of a set of protocol general description items, a set of protocol matching items, and a set of protocol definition items.
[0041] The protocol general description item set contains multiple protocol general description items. The protocol general description item is a general description item of the protocol, which defines the unique ID of the protocol instruction, the corresponding class name for storing the protocol instruction parsing or encoding results, the check item definition, the check data area definition, the message data area length item definition, and the message data area definition.
[0042] The protocol matching item set defines the matching conditions for the software system to automatically match the protocol instructions. The system compares and matches the data packets according to these matching conditions. If all conditions are met, it means that the match is successful. The protocol data items in this protocol instruction configuration file can be used to parse the packets. There are mainly two types of byte condition items and bit condition items.
[0043] The protocol data item set defines the constituent elements of the protocol - protocol data items. Protocol data items include byte items, bit items, repeated items (including TLV-type repeated items and other types of repeated items), tag items, sub-protocol items, encryption blocks and other types.
[0044] The above protocol items are explained below according to public attributes and private attributes.
[0045] 1. Description of common attributes of protocol items:
[0046] length: length in Byte. Only in bitDefinition is the unit in Bit.
[0047] lengthRef: length reference, indicating that the length value is referenced from other attribute values.
[0048] shift: offset, indicating the offset in the definition.
[0049] objectType: defines the data type in the object-oriented language (such as Java and C#) corresponding to the value of the item, including: Byte, Short, Integer, Long, Float, Double, HEXSTRING (hexadecimal string). When the data is a complex object, it can be a HashMap or a custom class.
[0050] property: defines the attribute name of the entity object in the object-oriented language (Java, C#, etc.) corresponding to the item value.
[0051] propertyName: defines the Chinese name of the attribute of the entity object in the object-oriented language corresponding to the item value.
[0052] ignore: indicates whether to ignore this configuration item when parsing or encoding. The default value is false, which means it is not ignored; true means it is ignored and no parsing or encoding is required.
[0053] showExp: Display expression, a logical expression that indicates whether it appears in the message. True indicates it appears, and false indicates it does not appear.
[0054] valueExp: value expression, the property value comes from the result of the expression or custom method (function) operation (the data type before the operation is required to be compatible with the data type of the result after the operation). For example, the expression "batteryVoltage / 1000" and the custom function "customFunction(cmd,studentsCount)".
[0055] encoding: encoding format. When Byte is converted to String, the encoding format used by the string.
[0056] byteOrder: byte order, BIG_ENDIAN for big endian or LITTLE_ENDIAN for little endian. If not configured, the default is big endian.
[0057] 2. General description of the protocol:
[0058] id element: the unique id of the protocol instruction, which cannot be repeated and is generally named according to the constraints of "product (model)-protocol version-instruction-encoding / decoding".
[0059] className element: the class name corresponding to the protocol instruction data storage, which can be a custom class or HashMap.
[0060] byteOrder element: The byte order used by the multi-byte value of the protocol instruction, BIG_ENDIAN for big endian or LITTLE_ENDIAN for little endian.
[0061] checkCodeItem element: check item, message check item (such as CRC) definition. check indicates whether to perform message check, checkType is the name of the built-in check function, customFunction is the user-defined extended check function, which has a higher priority than the built-in function. Shift is the offset, which can be 0, a positive number or a negative number. A negative number indicates an offset from the end of the message.
[0062] checkDataBlock element: verification data area definition, begin attribute is the starting position of the verification message content in the message, and end attribute is the ending position; begin and end values can be 0, positive or negative.
[0063] dataLengthItem element: data area length item definition. encode indicates whether to calculate the length value of the data area during protocol encoding. shift and length are the offset and length values calculated in bytes. bitShift and bitLength are the offset and length values calculated in bits.
[0064] dataBlock element: data area definition, the begin attribute is the starting position of the verification message content in the message, and the end attribute is the ending position; the values of begin and end can be 0, positive or negative.
[0065] description element: annotations related to the protocol instructions.
[0066] 3. Protocol matching item description:
[0067] conditionByte element: the matching byte condition definition, value is the value to be compared, operator is the operator (the default is the equal sign, which can be =, >, >=, <, <=, in, etc.), that is, the value of the length bytes starting from shift in the message is calculated by the operator and value. When the result is true, the matching item element matches successfully.
[0068] conditionBits element: a collection of conditionBit elements, which can only be a subordinate element of the conditionByte element and a parent element of the conditionBit element.
[0069] conditionBit element: defines the matching bit condition, value is the value to be compared, and operator is the operator; the element can only be defined in the subordinate elements of conditionBits.
[0070] 4. Description of protocol data items:
[0071] byteDef element: protocol byte item definition, protocol item defined by byte. bitDefList element: a collection of protocol bit item definitions, which can only be a child element of the byteDef element and a parent element of the bitDef element.
[0072] bitDef element: defines a protocol bit item, which defines data based on bits. The element can only be defined in the child elements of the bitDefList element.
[0073] Repeater element: indicates a repeating item (array or collection) in a protocol item. times indicates that the number of repetitions is a fixed value. timesRef indicates that the number of repetitions is a reference to another attribute. Only one of times and timesRef needs to be set. If both are set, the value of timesRef takes precedence. When the reference attribute of timesRef is found at the same level or at different levels, the reference attribute at the same level takes precedence. When the timesRef reference value crosses levels, the attribute must be identified in the abc format (for example, student.course.math).
[0074] Repeater supports two types of repeating items, one is TLV-type (TLV and TTLV) repeating items, and the other is other repeating items (general structure arrays). The value of itemType is one of TLV, TTLV or "". When it is TLV, it means that the data is a TLV repeating item; when it is TTLV, it means that the data is a TTLV repeating item; when itemType is TLV or TTLV, tagFieldLength, typeFieldLength, lengthFieldOption, and lengthFieldLength need to be set, and at this time, the child element of repeater can only be a tag element;
[0075] When the value of itemType is "", it means that the data item is another repeating item and does not need to be set. In this case, the child element of repeater can be any protocol item element. tagFieldLength indicates the length of the tag field in the protocol message, typeFieldLength indicates the length corresponding to the data type field, lengthFieldOption indicates whether the length field needs to be configured, and lengthFieldLength indicates the length of the field where the value value is located.
[0076] tagDef element: The definition of the tag in TLV encoding. It can only be a child element of the repeater element. The tag attribute indicates the tag id. The length attribute needs to be explicitly configured only when encoding. It indicates the length of bytes required to encode the V (value) in the TLV. It does not need to be configured when decoding. The sub-elements of tag can be nested under byteDef, repeater, subProtocol, encryptBlock and other elements.
[0077] subProtocol element: a subprotocol data item, indicating that the protocol item defines the protocol definition corresponding to the data from the embedded subprotocol instruction XML definition, protocolId is the protocol ID, protocolFileName is the protocol file name, and when the data corresponding to the subprotocol is a complex object (the subprotocol data contains multiple basic types of attributes), the value of objectType can be a HashMap or a custom class.
[0078] encryptBlock element: encryption (data) block, indicating that the data part corresponding to the protocol message is an encrypted data block. Decryption is required before decoding, and encoding is required before encryption. algorithm is the built-in encryption algorithm, customFunction is the user-defined extended encryption algorithm (with a higher priority than the built-in encryption algorithm), and key is the encryption and decryption key.
[0079] Description method:
[0080] 1. Create an XML file and create the root element protocol.
[0081] 2. According to the protocol content and the general description method attribute description, create protocol general description item elements under the root element and fill in the attribute content, including id, className, byteOrder, checkCodeItem, checkDataBlock, dataLengthItem, dataBlock, and description.
[0082] 3. According to the protocol content and the general description method attribute description, create a protocol matching item collection element conditionItems under the root element and fill in the attribute content. Configure the elements according to the unique characteristics of the protocol instructions, including conditionByte, conditionBits, and conditionBit elements.
[0083] According to the protocol content and the general description method attribute description, create the protocol data item collection element definitionItems under the root element and define the sub-elements. Define the protocol items in the order of the protocol specification, configure variable references, expressions and custom methods, variable-length strings and string encodings, define loop items (TLV categories and other categories) and nesting according to the array structure type, and configure encryption blocks, sub-protocols, etc.; the elements that need to be configured include byteDef, bitDefList, bitDef, repeater, tagDef, encryptBlock, and subProtocol elements.
[0084] A communication protocol general description file of a specific communication instruction is established based on the general description method.
[0085] like Figure 2 As shown, the protocol encoding process of the present invention includes the following steps:
[0086] 1. Obtain the protocol item definition corresponding to the general description file of the protocol instruction according to the passed-in protocol ID, and proceed to step 2.
[0087] 2. Initialize the byte array variable byteAll for storing the message and go to step 3.
[0088] 3. Protocol block encoding. Pass in the object instance or json object or json string to be encoded, the protocol data item set and the message storage variable byteAll, and execute the protocol block encoding method. The method in the bold box in the figure is called the protocol block encoding method, which can encode any set of protocol data items.
[0089] 3.1 The protocol block encoding method loops through the protocol data item set internally, and encodes each protocol data item according to the data item type. If the protocol data item is a byte item, jump to step 3.2; if the protocol data item is a duplicate item, jump to step 3.3; if the protocol data item is a sub-protocol item, jump to step 3.4; if the protocol data item is an encrypted item, jump to step 3.5; after the traversal is completed, the message array byteAll is returned, and the process goes to step 4.
[0090] 3.2 Byte item encoding process: If it is a byte item that does not contain a bit item, read the bytes according to the byte item definition, and then convert the result according to the defined numeric type to obtain the corresponding byte array; if a multi-byte numeric type (such as Integer, Float, etc.) is defined and the byte order is little endian, the bytes are processed in reverse order. If the byte item contains a bit item, encode the bit item first, obtain the value after the operation according to the bit item definition, convert it to a byte array, and then encode the byte item according to the byte item definition that does not contain a bit item to obtain the conversion result. After the encoding is completed, go to step 3.6.
[0091] 3.3 Repeated item data encoding process: Get the number of loops according to the definition, and encode the protocol data items in a loop. There are two cases when encoding. First, if it is a TLV type repeated item, read the Tag according to the definition and get the value of the Tag in the attribute object, and then call the byte item encoding to get the byte array of Value, and then get the byte length Length of Value, and get the encoding result of Tag, Length, and Value; if there are nested protocol data items in the Tag item, call the protocol block encoding for encoding. Second, if it is other repeated items, pass in its protocol definition data item set and value (type is Map / List / Array), call the protocol block encoding, and you can get the encoded byte array. After the encoding is completed, go to step 3.6.
[0092] 3.4 Sub-protocol item data encoding process: According to the definition, obtain the protocol ID corresponding to the sub-protocol description file, load the protocol item set of the sub-protocol, and then perform protocol block encoding on it. After the encoding is completed, go to step 3.6.
[0093] 3.5 Encryption item encoding process: Call the protocol block encoding according to the protocol data item set inside the encrypted data item, obtain the original message array, perform operations according to the encryption function, obtain the encrypted message array, and go to step 3.6.
[0094] 3.6 The encoded byte array is inserted into the header of byteAll.
[0095] 3.7 During the above-mentioned protocol data item encoding process, if there are nested protocol data items inside the protocol data item, the encoding can be implemented by recursively calling the protocol block encoding method in the order from the upper layer to the lower layer.
[0096] 3.8 If there is a valueExp expression or a custom encoding method or a custom encryption method during the protocol encoding process, the custom method or expression operation is used first, otherwise the conventional encoding method or built-in encryption method within the protocol is used for encoding and encryption. When an exception occurs, the error message is recorded and the encoding is terminated.
[0097] Implementation of custom encoding methods. You can inherit the encoding base class through a custom class and overload the encoding algorithm of the base class. The method can pass in the class object to be encoded and the encoded message byte array. You can write custom encoding logic inside and return the encoded byte array. Each custom encoding class only implements one custom encoding algorithm and is configured through the configuration file to turn on or off loading, ensuring that only the required custom extension classes are loaded in the project.
[0098] 4. Calculate and fill the length of the protocol data area into the message array byteAll, and go to step 5.
[0099] 5. Calculate the checksum function, fill the result value into the message array byteAll, and go to step 6.
[0100] 6. After encoding is completed, the result array byteAll is returned and the program ends.
[0101] The protocol encoding example based on the protocol general description file has the following inputs: JSON data to be encoded and the protocol ID; the output is the encoded hexadecimal message data, as shown in Figure 3.
[0102] Those skilled in the art will appreciate that embodiments of the present invention may be provided as methods, systems, or computer program products. Therefore, the present invention may take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0103] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of processes and / or blocks in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0104] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0105] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0106] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. For those skilled in the art, the present invention may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made by those skilled in the art within the spirit and principle of the present invention without creative labor shall be included in the protection scope of the present invention.
Claims
1. A communication protocol encoding method, characterized in that: The following steps are involved: Obtain the protocol item definition corresponding to the general description file of the protocol instruction according to the protocol ID; Initialize the byte array variable for message storage; Get the object instance to be encoded, the protocol data item set and the message, and perform protocol block encoding; Traverse the protocol data items, and for each protocol data item, determine whether it is a byte item, a repeated item, a sub-protocol item, or an encrypted item, and perform corresponding encoding processing; specifically, if the protocol data item is a byte item that does not contain a bit item, read the bytes according to the byte item definition, convert the result according to the defined numeric type, and obtain the corresponding byte array; if a multi-byte numeric type is defined and the byte order is little endian, the bytes are processed in reverse order; If the byte item contains a bit item, encode the bit item first, obtain the value after the operation according to the bit item definition, convert it into a byte array, and then perform byte item encoding according to the byte item definition that does not contain a bit item to obtain the conversion result; If the protocol data item is a repeated item and is a TLV type repeated item, read the Tag according to the definition and obtain the value of the Tag in the attribute object. Then call the byte item encoding to obtain the byte array of Value, and then obtain the byte length of Value. The encoding result of Tag, Length, and Value is obtained. Insert the encoded byte array into the message; Calculate and fill the length of the protocol data area into the message; Calculate the checksum function and fill the result value into the message to form the final message data.
2. A communication protocol encoding method as claimed in claim 1, characterized in that: To encode an object instance, replace it with a json object or a json string.
3. A communication protocol encoding method as claimed in claim 1, characterized in that: If the protocol data item is a repeated item and is not a TLV repeated item, the protocol block encoding is called according to its protocol definition data item set and value to obtain the encoded byte array.
4. A communication protocol encoding method as claimed in claim 1, characterized in that: If it is sub-protocol item data, obtain the protocol ID corresponding to the sub-protocol description file according to the definition, load the protocol item set of the sub-protocol, and then perform protocol block encoding on it.
5. A communication protocol encoding method as claimed in claim 1, characterized in that: If it is an encrypted item, call the protocol block encoding according to the protocol data item set inside the encrypted data item to obtain the original array of the message, perform the operation according to the encryption function, and obtain the encrypted message array.
6. A communication protocol encoding method as claimed in claim 1, characterized in that: If there are nested protocol data items inside the protocol data item, the protocol block encoding method is recursively called in order from the upper layer to the lower layer to achieve encoding.
7. A communication protocol encoding method as claimed in claim 1, characterized in that: If there is a valueExp expression, a custom encoding method, or a custom encryption method during the protocol encoding process, the custom method is used first or the expression is operated on. Otherwise, the conventional encoding method or built-in encryption method within the protocol is used for encoding and encryption.
8. A communication protocol coding system, characterized in that: include: The file reading module reads the general description file of the protocol instruction according to the protocol ID, verifies the file format and elements, and parses the corresponding protocol item definition elements in the file after passing the verification; An element conversion module converts the protocol definition item elements obtained by the file reading module into class object instances in an object-oriented language and obtains a set definition of protocol data items; The protocol encoding module is configured to obtain an object instance or a JSON object or a JSON string to be encoded, a set of protocol data items, traverse the protocol data items, and for each protocol data item, determine whether it is a byte item, a repeated item, a sub-protocol item, and an encrypted item. Specifically, if the protocol data item is a byte item that does not contain a bit item, the bytes are read according to the byte item definition, and the result is converted according to the defined numerical type to obtain the corresponding byte array; if a multi-byte numerical type is defined and the byte order is little endian, the bytes are processed in reverse order; If the byte item contains a bit item, encode the bit item first, obtain the value after the operation according to the bit item definition, convert it into a byte array, and then perform byte item encoding according to the byte item definition that does not contain a bit item to obtain the conversion result; If the protocol data item is a repeated item and is a TLV type repeated item, read the Tag according to the definition and obtain the value of the Tag in the attribute object. Then call the byte item encoding to obtain the byte array of Value, and then obtain the byte length of Value. The encoding result of Tag, Length, and Value is obtained. The object to be encoded is processed accordingly, the protocol encoding is executed, the message length and check value are calculated, and then filled into the message to obtain the final communication message data.
Citation Information
Patent Citations
Communication data protocol universal description method based on XML
CN109525364A
Rail transit data processing method and device
CN115098446A