An event-driven message processing method
Through the event-driven message processing method, the initiation and processing of event messages are decoupled by the message queue and event message processing table, and the problems of low processing efficiency and reduced event initiation efficiency in the prior art are solved, and efficient event message processing is achieved.
Patent Information
- Application Number
- CN202210844766.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-07-19
- Publication Date
- 2025-06-24
- Estimated Expiration
- 2042-07-19
AI Technical Summary
In the prior art, the initiation and processing of event messages are coupled together, resulting in low processing efficiency, wasting the multi-task characteristics and CPU multi-core characteristics of the embedded real-time operating system, and the increasing number of functional blocks reduce the efficiency of event initiation.
The event-driven message processing method is adopted to receive the driver message structure through the message queue, define the driver message structure variable, event processing structure and event message processing table, determine the message processing function pointer according to the type of event message, and determine the processing method of the message by judging the ownership attribute.
Decoupling between the initiator of event messages and the processor is realized. Through the isolation of message queues, the processing efficiency of event messages is improved, and the processing efficiency is avoided. Even if the number of processors increases, it will not affect the efficiency of event initiation.
Smart Images

Figure CN115248736B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure generally relates to the field of computer control technology, and particularly relates to an event-driven message processing method. Background Art
[0002] Event initiation - processing is a common processing method in computer control systems. In the field of embedded control, the initiation of event messages and the processing of event messages are usually coupled together; in this coupling method, multiple functional blocks need to be bound together for processing, which results in low processing efficiency during the binding process of multiple functional blocks, wasting the multitasking characteristics of the embedded real-time operating system and the multi-core characteristics of the CPU. At the same time, the increased number of functional blocks also reduces the efficiency of event initiation. Summary of the Invention
[0003] In view of the above-mentioned defects or deficiencies in the prior art, it is desirable to provide an event-driven message processing method that can solve the above technical problems.
[0004] The present application provides an event-driven message processing method, including:
[0005] Receiving a driver message structure through a message queue, where the driver message structure stores the type, ownership attribute, pointer to the event message, and length of the event message; the type of the event message is an enumerated variable;
[0006] Defining a driver message structure variable and assigning the received driver message structure to the driver message structure variable;
[0007] Defining an event processing structure, where the event processing structure includes an event processing type and a message processing function pointer corresponding to the event processing type; the type of the event processing type is an enumerated variable;
[0008] Defining an event message processing table, where the event message processing table includes multiple event processing structures;
[0009] According to the type of the event message in the driver message structure variable, determining the message processing function pointer through the event message processing table; running the message processing function pointed to by the message processing function pointer in an addressing manner;
[0010] When it is determined that the ownership attribute of the event message in the driver message structure variable is 0x00, copying the driver message structure variable to the global data space for use as public data; when it is determined that the ownership attribute of the event message in the driver message structure variable is 0x01, sending the driver message structure variable to the task to be used.
[0011] According to the technical solution provided by the embodiment of the present application, the driving message structure is implemented by the sending end through the following steps:
[0012] Create a message queue based on the ucos queue creation function OSQCreate;
[0013] Receive the address of the driving message structure and the handle of the message queue;
[0014] Send the driving message structure through the message queue based on the ucos queue sending function OSQPost.
[0015] According to the technical solution provided by the embodiment of the present application, the driving message structure is generated through the following steps:
[0016] Define the driving message structure, and the members of the driving message structure include the type of the event message, the ownership attribute, the pointer to the event message, and the length of the event message; the type of the event message is an enumerated variable;
[0017] Assign values to the members of the driving message structure in the sending thread.
[0018] According to the technical solution provided by the embodiment of the present application, it further includes: setting the blocking time for receiving the driving message structure.
[0019] According to the technical solution provided by the embodiment of the present application, the method for assigning values to the driving message structure in the sending thread is specifically as follows. In the sending thread:
[0020] Assign the type of the event message to the type member of the event message in the driving message structure;
[0021] Assign the ownership attribute of the event message to the ownership attribute member in the driving message structure;
[0022] Use the dynamic memory allocation function malloc provided by the C standard library to apply for a storage space, the size of the storage space is the size of the memory space occupied by the event message, store the event message in the applied storage space, obtain a pointer to the event message, and assign it to the pointer member of the event message in the driving message structure;
[0023] Assign the length of the event message to the length member of the event message in the driving message structure.
[0024] According to the technical solution provided by the embodiment of the present application, before assigning values to the driving message structure in the sending thread, it further includes: initializing the driving message structure.
[0025] According to the technical solution provided by the embodiments of the present application, the event-driven message processing method further includes: defining a user data structure; the message processing function pointer is a navigation data fusion function pointer, and the navigation data fusion function pointer points to a navigation data fusion function, and the navigation data fusion function is used for:
[0026] Obtain the pointer to the event message and the length of the event message in the drive message structure;
[0027] Use the memory copy function in the C standard library to copy the event message with the length of the event message to the user data structure with the pointer to the event message as the starting address, and perform Kalman filter calculation using the data in the user data structure.
[0028] The beneficial effect of the present application is that: in the sending thread, with the message queue as the carrier, the type, ownership attribute, pointer to the event message, and the length of the event message are sent to the receiving end in the form of a drive message structure. After receiving the drive message structure, the receiving end determines the message processing function pointer corresponding to the event message type through the defined event message processing table, and runs the message processing function in an addressing manner to facilitate the processing of the event message. Finally, by judging the ownership attribute of the event message in the drive message structure, it can be adaptively and directionally allocated to the global data space or sent to the task thread that needs to use it, thereby realizing the decoupling of the initiator and the processor of the event message. Through the isolation of the message queue, the initiator only needs to send the information of the event message through the message queue without caring about the processing logic of the processor, so that the processor can process the event message with the highest efficiency according to the system scheduling. For the situation where the number of the same event processors increases, due to the isolation of the message queue, the initiation efficiency of the message event will not be reduced. Description of the Drawings
[0029] By reading the detailed description of the non-limiting embodiments with reference to the following drawings, other features, purposes, and advantages of the present application will become more obvious:
[0030] Figure 1 It is a structural diagram of an event-driven message processing method provided by the present application. Detailed Embodiments
[0031] The following further elaborates the present application in conjunction with the drawings and embodiments. It can be understood that the specific embodiments described herein are only used to explain the related invention and are not a limitation to the invention. Additionally, it should be noted that for the convenience of description, only the parts related to the invention are shown in the drawings.
[0032] It should be noted that, without conflict, the embodiments in this application and the features in the embodiments may be combined with each other. The following will describe this application in detail with reference to the drawings and in combination with the embodiments.
[0033] Please refer to Figure 1 An event-driven message processing method provided for this application, including:
[0034] S1: Receive a driver message structure through a message queue. The driver message structure stores the type of the event message, the ownership attribute, a pointer to the event message, and the length of the event message; the type of the event message is an enumerated variable;
[0035] S2: Define a driver message structure variable, and assign the received driver message structure to the driver message structure variable;
[0036] S3: Define an event processing structure, which includes an event processing type and a message processing function pointer corresponding to the event processing type; the type of the event processing type is an enumerated variable;
[0037] S4: Define an event message processing table, which includes multiple event processing structures;
[0038] S5: According to the type of the event message in the driver message structure variable, determine the message processing function pointer through the event message processing table; run the message processing function pointed to by the message processing function pointer in an addressing manner;
[0039] S6: When it is judged that the ownership attribute of the event message in the driver message structure variable is 0x00, copy the driver message structure variable to the global data space for use as public data; when it is judged that the ownership attribute of the event message in the driver message structure variable is 0x01, send the driver message structure variable to the task to be used.
[0040] Specifically, the type of the ownership attribute MsgType of the event message is an unsigned character type, including: global message Pub (value is 0x00), local message Pri (value is 0x01);
[0041] Specifically, the pointer MsgPtr to the event message applies for memory in the storage space using the dynamic memory application function malloc;
[0042] For the convenience of explaining the working principle of this application, a specific example is used for explanation;
[0043] S1: Receive the driver message structure struct ThreadEvent through the message queue. For example, the received driver message structure struct ThreadEvent includes:
[0044] ① The type of the event message EventType: navigation data type. The navigation data type includes: longitude Long, latitude Lati, altitude Alti, X-axis speed value XSpeed, Y-axis speed value YSpeed, Z-axis speed value ZSpeed, X-axis acceleration value XAcc, Y-axis acceleration value YAcc, Z-axis acceleration value ZAcc, X-axis angular velocity value XAng, Y-axis angular velocity value Yang, Z-axis angular velocity value ZAng, roll angle value Roll, pitch angle value Pitch, yaw angle value Yaw. Among them, the type of each variable is float type;
[0045] ② The ownership attribute MsgType: global message Pub (value is 0x00);
[0046] ③ The pointer MsgPtr pointing to the event message;
[0047] ④ The length len of the event message;
[0048] S2: Define the driver message structure variable revent, and assign the received driver message structure struct ThreadEvent to the driver message structure variable revent;
[0049] S3: Define the event processing structure struct EventProcessor. The event processing structure struct EventProcessor includes the event processing type and the message processing function pointer corresponding to the event processing type. The type of the event processing type is an enumerated variable;
[0050] Specifically, the event processing type EventType in the event processing structure and the message processing function pointer correspond one by one;
[0051] S4: Define the event message processing table. The event message processing table includes multiple event processing structures. In this example, Table 1 is used for illustration:
[0052]
[0053]
[0054] Table 1
[0055] S5: Determine the message handling function pointer through the event message processing table according to the type of the event message of the drive message structure variable; Run the message handling function pointed to by the message handling function pointer in an addressing manner;
[0056] In this example, the type of the event message in the drive message structure struct ThreadEvent received is the navigation data type; As can be seen from Table 1 above, the message handling function pointer corresponding to this navigation data type is: the navigation data fusion function pointer NAV_Tackle, and the message handling function is the navigation data fusion function;
[0057] S6: Determine that the ownership attribute of the event message of the drive message structure variable is 0x01, and send the drive message structure variable to the task to be used.
[0058] Working principle: By using a message queue as a carrier in the sending thread, send the type, ownership attribute, pointer to the event message, and the length of the event message to the receiving end in the form of a drive message structure. After receiving the drive message structure, the receiving end determines the message handling function pointer corresponding to the event message type through the defined event message processing table, and runs the message handling function pointed to by the message handling function pointer in an addressing manner to facilitate the processing of the event message. Finally, by judging the ownership attribute of the event message in the drive message structure, it can be adaptively and directionally allocated to the global data space or sent to the task thread that needs to use it, thereby realizing the decoupling of the initiator and the processor of the event message. Through the isolation of the message queue, the initiator only needs to send the information of the event message through the message queue without caring about the processing logic of the processor, so that the processor can process the event message with the highest efficiency according to the system scheduling. For the situation where the number of the same event processors increases, due to the isolation of the message queue, the initiation efficiency of the message event will not be reduced.
[0059] In some embodiments, the drive message structure is implemented by the sending end through the following steps:
[0060] Create a message queue based on the ucos queue creation function OSQCreate;
[0061] Receive the address of the drive message structure and the handle of the message queue;
[0062] Send the drive message structure through the message queue based on the ucos queue sending function OSQPost.
[0063] Specifically, the ucos refers to an open-source preemptive multitasking real-time operating system.
[0064] In some embodiments, receiving the driver message structure through the message queue in step S1 is specifically as follows:
[0065] At the receiving end, the task receiving function OSQPend is used to receive and process the driver message structure. The input parameters of the task receiving function OSQPend include:
[0066] ① The handle of the message queue;
[0067] ② The address where the received data is stored, that is, the address of the driver message structure variable revent.
[0068] In some embodiments, it further includes: setting the blocking time for receiving the driver message structure. Preferably, the setting of the blocking time for receiving the driver message structure is used as an input parameter of the task receiving function OSQPend. For example, the blocking time is set to portMAX_DELAY, indicating that if the queue is empty, it will block indefinitely.
[0069] In some embodiments, the driver message structure is generated through the following steps:
[0070] Define the driver message structure. The members of the driver message structure include the type of the event message, the ownership attribute, the pointer to the event message, and the length of the event message; the type of the event message is an enumerated variable;
[0071] In the sending thread, assign values to each member of the driver message structure, that is, assign values to the four members in the driver message structure: the type of the event message, the ownership attribute, the pointer to the event message, and the length of the event message.
[0072] In some embodiments, the method of assigning values to the driver message structure in the sending thread is specifically as follows. In the sending thread:
[0073] Assign the type of the event message to the type member of the event message in the driver message structure;
[0074] Assign the ownership attribute of the event message to the ownership attribute member in the driver message structure;
[0075] Use the dynamic memory allocation function malloc provided by the C standard library to apply for a storage space. The size of the storage space is the size of the memory space occupied by the event message. Store the event message in the applied storage space to obtain a pointer to the event message, and assign it to the pointer member of the event message in the driver message structure;
[0076] Assign the length of the event message to the length member of the event message in the drive message structure.
[0077] Specifically, the storage space can be, for example: the heap space in the operating system.
[0078] In some embodiments, before assigning values to the drive message structure in the sending thread, it further includes: initializing the drive message structure. That is, clearing all members of the drive message structure.
[0079] In some embodiments, the event-driven message processing method further includes: defining a user data structure; the message processing function pointer is a navigation data fusion function pointer, the navigation data fusion function pointer points to a navigation data fusion function, and the navigation data fusion function is used for:
[0080] Obtain the pointer to the event message and the length of the event message in the drive message structure;
[0081] Use the memory copy function in the C standard library to copy the event message with the length of the event message starting from the pointer to the event message into the data structure, and perform Kalman filter calculation using the data in the user data structure.
[0082] Thus, it realizes storing the data of the drive message structure into the user data structure, avoiding the event handler directly operating on the data of the event message trigger end, and further avoiding data tampering when multiple event handlers operate on the data simultaneously; at the same time, directly assigning values according to the data pointer and length to the user data structure can naturally parse the data, which is convenient for further use.
[0083] The above description is only the preferred embodiments of the present application and the description of the applied technical principles. Those skilled in the art should understand that the scope of the invention involved in the present application is not limited to the technical solutions formed by the specific combination of the above technical features, but should also cover other technical solutions formed by any combination of the above technical features or their equivalent features without departing from the inventive concept. For example, the technical solutions formed by mutually replacing the above features with the technical features (but not limited to) having similar functions disclosed in the present application.
Claims
1. An event-driven message processing method, characterized in that, including: Receiving a driver message structure through a message queue, where the driver message structure stores the type of an event message, an ownership attribute, a pointer to the event message, and the length of the event message; the type of the event message is an enumerated variable; Defining a driver message structure variable and assigning the received driver message structure to the driver message structure variable; Defining an event handling structure, where the event handling structure includes an event handling type and a message handling function pointer corresponding to the event handling type; the type of the event handling type is an enumerated variable; Defining an event message handling table, where the event message handling table includes multiple event handling structures; Determining the message handling function pointer through the event message handling table according to the type of the event message in the driver message structure variable; Running the message handling function pointed to by the message handling function pointer in an addressing manner; When it is determined that the ownership attribute of the event message in the driver message structure variable is 0x00, copying the driver message structure variable to the global data space for use as public data; when it is determined that the ownership attribute of the event message in the driver message structure variable is 0x01, sending the driver message structure variable to a task to be used.
2. The event-driven message processing method according to claim 1, wherein The driver message structure is implemented by the sending end through the following steps: Creating a message queue based on the ucos queue creation function OSQCreate; Receiving the address of the driver message structure and the handle of the message queue; Sending the driver message structure through the message queue based on the ucos queue sending function OSQPost.
3. The event-driven message processing method according to claim 2, wherein The driver message structure is generated through the following steps: Defining a driver message structure, where the members of the driver message structure include the type of an event message, an ownership attribute, a pointer to the event message, and the length of the event message; the type of the event message is an enumerated variable; Assigning values to the members of the driver message structure in a sending thread.
4. The event-driven message processing method according to claim 3, wherein Also including: Setting the blocking time for receiving the driver message structure.
5. The event-driven message processing method according to claim 3, wherein The method of assigning values to the driver message structure in a sending thread is specifically as follows: in the sending thread: Assigning the type of the event message to the event message type member in the driver message structure; Assigning the ownership attribute of the event message to the ownership attribute member in the driver message structure; Using the dynamic memory allocation function malloc provided by the C standard library to apply for a storage space, where the size of the storage space is the size of the memory space occupied by the event message, storing the event message in the applied storage space to obtain a pointer to the event message, and assigning it to the pointer to the event message member in the driver message structure; Assigning the length of the event message to the event message length member in the driver message structure.
6. The event-driven message processing method according to claim 3, wherein Before assigning values to the driver message structure in the sending thread, it also includes: initializing the driver message structure.
7. The event-driven message processing method according to any one of claims 1-6, characterized in that, Also including: Define a user data structure; the message processing function pointer is a navigation data fusion function pointer, and the navigation data fusion function pointer points to a navigation data fusion function, and the navigation data fusion function is used for: Obtain the pointer to the event message and the length of the event message in the drive message structure; Use the memory copy function in the C standard library to copy the event message with the length of the event message to the user data structure starting from the pointer to the event message, and perform Kalman filter calculation using the data in the user data structure.
Citation Information
Patent Citations
Window message processing method based on MiniGUI
CN104793931A
Event driving method and device, equipment and readable storage medium
CN108563493A