A method and system for providing an Internet of Things operating environment
By analyzing application code for the runtime system of IoT devices to obtain the priority of callback function and executing it according to priority, the problem of disordered event-driven mechanism in the existing technology is solved, and more efficient incident response and resource utilization are achieved.
Patent Information
- Application Number
- CN202111174828.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-10-09
- Publication Date
- 2025-08-29
- Estimated Expiration
- 2041-10-09
AI Technical Summary
The existing runtime system fails to effectively utilize the real-time characteristics of IoT devices, resulting in disorderly execution of callback functions in the event-driven mechanism, which cannot meet the real-time requirements of IoT devices.
The callback function priority is obtained by parsing the application code, and during the parsing process, the Internet of Things function is requested to be obtained, and the specified priority is registered and added. The callback function is stored using a message queue with priority and a small top heap structure, and the callback function is executed in the order of priority.
It improves the application's response speed to key events, meets the real-time needs of IoT devices, reduces CPU time waste, and reduces system code space resource usage.
Smart Images

Figure CN113946420B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of Internet of Things, and in particular to a method and system for providing an Internet of Things operating environment. Background Art
[0002] A runtime environment, also known as a "runtime system," refers to the environment that runs semi-compiled code on a target machine. A runtime environment is a kind of operating mode between a compiler and an interpreter. For example, the Java runtime environment is called the "Java Runtime Environment" (JRE). The runtime system is the foundation of the software stack for multi-core computer systems and a key component in ensuring the secure and efficient operation of computer applications. The runtime system encapsulates the operating system's resource interfaces and provides runtime support for programming frameworks, including optimized scheduling, memory management, process management, and error handling. Modern operating systems rely on the support of runtime systems.
[0003] To take a simple example, consider the computer's actions (function calls) based on user input (i.e., generating an event, such as clicking a button). Of course, events aren't limited to user input; the core of an event-driven system is naturally events. From an event perspective, the basic structure of an event-driven system consists of an event collector, an event dispatcher, and an event handler. The event collector is responsible for collecting all events, including those from the user (such as mouse and keyboard events), hardware (such as clock events), and software (such as the operating system and the application itself). The event dispatcher is responsible for distributing the events collected by the collector to the target object. The event handler handles the specific event response, which is often not fully determined until the implementation phase, necessitating the use of virtual functions. An event-driven architecture consists of three basic components: events, event handlers, and an event loop. Once generated, events are sent to the event loop, which dispatches each event to a separate event handler. Event A is handled by handler A, and event B is handled by handler B.
[0004] The program of the event-driven mechanism of the runtime system is an infinite loop and exists in the form of a thread. This infinite loop consists of two parts. The first part is to receive and select an event to be processed according to certain conditions, and the second part is the event processing process. The execution process of the program is to select events and process events. When no event is triggered, the program will enter a sleep state due to the failure to query the event queue, thereby releasing the central processing unit (CPU). When the inventor studied this event-driven mechanism, he found that all callback functions in the events of the traditional runtime system were executed out of order, or simply executed in the order of registration, without taking advantage of the real-time characteristics of the Internet of Things devices. The existing runtime system focuses more on edge devices, such as ARMv8-A level devices, and pays less attention to end devices with extremely limited resources. These end devices simply use the existing event-driven mechanism, but do not design and optimize it accordingly for the Internet of Things end devices and in combination with the optimized event-driven mechanism. Summary of the Invention
[0005] Therefore, the purpose of the present invention is to overcome the above-mentioned defects of the prior art and provide a method and system for providing an Internet of Things operating environment.
[0006] The purpose of the present invention is achieved through the following technical solutions:
[0007] According to a first aspect of the present invention, a method for providing an Internet of Things operating environment is provided, comprising: obtaining application code, wherein the application code includes priorities of callback functions corresponding to various events; parsing the above application code to obtain the priorities specified by the user for the callback functions corresponding to the various events, and requesting to obtain corresponding Internet of Things functions during the parsing process; when the corresponding Internet of Things function is requested to be obtained, registering the callback function corresponding to the corresponding event and adding the specified priority to it, and triggering the corresponding callback function when the corresponding event occurs; receiving the triggered callback function and storing it in order from high to low priority; and retrieving and executing the callback function in order of the priority of the callback function.
[0008] According to some embodiments of the present invention, receiving the triggered callback function and storing it in descending order of priority includes:
[0009] Use a priority message queue to receive all triggered callback functions in a blocking manner;
[0010] All callback functions are stored in a message queue with priority in descending order of priority, wherein the message queue with priority adopts a mini-heap structure in order of priority.
[0011] According to some embodiments of the present invention, using a message queue with priority to receive all callback functions in a blocking manner includes:
[0012] When the callback functions in the message queue have not been completely executed, after each callback function with the highest priority in the message queue is retrieved and executed, it is determined whether all callback functions in the message queue have been completely executed;
[0013] When the callback function in the message queue is executed, the message queue waits for the callback function to be passed in.
[0014] According to some embodiments of the present invention, receiving the triggered callback function and storing it in descending order of priority includes:
[0015] Use the message queue to receive all triggered callback functions in a blocking manner;
[0016] Take out all callback functions in the message queue and store them in a small top heap structure in descending order of priority.
[0017] According to some embodiments of the present invention, taking out all callback functions from the message queue and storing all callback functions in a priority-ordered mini-heap structure in descending order of priority includes:
[0018] After executing a callback function in the small top stack, the small top stack obtains all callback functions in the message queue and continues to receive callback functions in the message queue within a predetermined time period. After receiving, the callback functions are sorted according to priority.
[0019] According to some embodiments of the present invention, the storage method of the priority-ordered mini-heap structure includes:
[0020] All callback functions are placed into several nodes of the mini-heap according to the priority order, so that the priority of the callback function in each node of the mini-heap is greater than or equal to the priority of the callback function in the left child node and the right child node of the node.
[0021] According to some embodiments of the present invention, the Internet of Things functions include a hypertext transfer protocol function, a function of inputting and outputting information, and a function of timing triggering corresponding events.
[0022] According to some embodiments of the present invention, in the request to obtain the corresponding Internet of Things function during the parsing process, when multiple requests all need to obtain the same Internet of Things function, a multi-threaded execution method is adopted, wherein the multi-threaded execution method includes:
[0023] A thread is created for each request, and the corresponding request is responded to according to the thread. When responding to the request, all callback functions of the corresponding event are triggered;
[0024] Receive and store all callback functions;
[0025] The callback function with the highest priority is taken out for execution, and this step is repeated until all callback functions are executed, and then the thread is exited.
[0026] According to some embodiments of the present invention, in the request for obtaining the corresponding IoT function with interruption during the parsing process, an interruption execution mode is adopted, and the interruption execution mode includes:
[0027] Make an interrupt response and trigger the callback function of the event;
[0028] Receiving and storing a callback function, wherein the callback function is a callback function in an interrupt, and the priority of the callback function in the interrupt is set to a priority specified by a user or a highest priority;
[0029] Take out the callback function in the interrupt and execute it.
[0030] According to a second aspect of the present invention, there is provided a runtime system for providing an operating environment implemented based on the method of the first aspect of the present invention, which is used to provide an operating environment for an application corresponding to the Internet of Things, comprising: a virtual machine module, a functional component including multiple native modules, an event storage module, and an event main loop module;
[0031] The virtual machine module is used to parse the application code to obtain the priorities specified by the user for the callback functions corresponding to various events, and request the corresponding IoT functions during the parsing process;
[0032] The native module is used to register a callback function corresponding to a corresponding event and add a specified priority to it when a corresponding IoT function is requested, and trigger the corresponding callback function when the corresponding event occurs;
[0033] The event storage module is used to receive the triggered callback functions and store them in descending order of priority;
[0034] The event main loop module is used to retrieve and execute callback functions in order of their priority.
[0035] According to some embodiments of the present invention, the system includes a configuration module for receiving user definitions to tailor part of the native module.
[0036] According to some embodiments of the present invention, the functional component is used to support multi-threaded execution mode and interrupted execution mode, and the functional component includes an event management unit, which is used to send a callback function triggered when a corresponding event occurs to an event storage module.
[0037] Compared with the prior art, the advantages of the present invention are:
[0038] 1. The method of the present invention parses the application code to obtain the priority specified by the user for the callback function corresponding to various events, and requests to obtain the corresponding Internet of Things function during the parsing process; when the corresponding Internet of Things function is requested to be obtained, the callback function corresponding to the corresponding event is registered and the specified priority is added to it, the corresponding callback function is triggered when the corresponding event occurs, and the callback function is stored, retrieved and executed in the order of the priority specified by the callback function, so that the method can give priority to the execution of the callback function with a high priority specified by the user according to the real-time requirements of the task, and set the task with strict timing constraints to a higher priority so that it can be executed first, thereby improving the application's response to key events and further meeting the real-time requirements of the Internet of Things terminal device.
[0039] 2. In the method of the present invention, the callback function is first received in a blocking reception manner. When no callback function is passed in, the method blocks and waits for the callback function to be passed in. Otherwise, the callback function is received and then the subsequent operation logic is executed, thereby improving the system operation efficiency and reducing unnecessary waste of central processing unit (CPU) time. Secondly, the callback function is stored in a small top heap structure in order of priority to ensure that the callback function with the highest priority is taken out each time and the callback function specified by the user can be executed to meet the real-time requirements.
[0040] 3. During the application process, the system of the present invention can receive user definitions to trim some native modules, so that users can flexibly apply the system. After obtaining the original code of the system, users can modify the original code, such as trimming unnecessary native modules through macro definitions in the configuration file, reducing the system code space (footprint) resource usage. By adjusting the system code space resources, it can also meet the needs of more devices. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] The embodiments of the present invention are further described below with reference to the accompanying drawings, in which:
[0042] Figure 1 A flow chart of a method for providing an Internet of Things operating environment according to an embodiment of the present invention;
[0043] Figure 2 A schematic diagram of a structure in which a linked list is used to store callback functions corresponding to corresponding registered events and to specify priorities according to an embodiment of the present invention;
[0044] Figure 3 Schematic diagram of a small top heap structure in order of priority according to an embodiment of the present invention;
[0045] Figure 4 The figure is a schematic diagram of the runtime system structure for providing an Internet of Things operating environment according to an embodiment of the present invention. DETAILED DESCRIPTION
[0046] In order to make the purpose, technical solutions and advantages of the present invention more clearly understood, the present invention is further described in detail below through specific embodiments in conjunction with the accompanying drawings. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0047] As mentioned in the background technology, when the inventors studied the existing event-driven mechanism, they found that all callback functions in the events of the traditional runtime system were executed out of order, or simply executed in the order of registration (i.e., the order of precedence), without taking advantage of the real-time characteristics of the IoT terminal devices; the existing runtime system focuses more on edge devices, such as ARMv8-A level devices, and pays less attention to terminal devices with extremely limited resources, which also simply use the existing event-driven mechanism.
[0048] In response to the above problems, the inventors designed and optimized IoT devices in combination with an optimized event-driven mechanism. The application code is parsed to obtain the priority specified by the user for the callback functions corresponding to various events, and the corresponding IoT functions are requested during the parsing process. When the corresponding IoT function is requested, the callback function corresponding to the corresponding event is registered and the specified priority is added to it. When the corresponding event occurs, the corresponding callback function is triggered, and the callback function is stored, retrieved and executed in the specified priority order of the callback function. This method can give priority to the execution of the callback function with a high priority specified by the user according to the real-time requirements of the task, and set tasks with strict timing constraints to a higher priority so that they can be executed first, thereby improving the application's response to key events and further meeting the real-time requirements of IoT devices.
[0049] Based on the above research, according to one embodiment of the present invention, a method for providing an Internet of Things operating environment is provided, including steps S1, S2, S3, S4 and S5, which will be referred to below. Figure 1 This method is described in detail.
[0050] Step S1: Obtain application code, wherein the application code includes the priorities of callback functions corresponding to various events.
[0051] Step S2: Parse the application code to obtain the priorities specified by the user for callback functions corresponding to various events, and request to obtain corresponding IoT functions during the parsing process.
[0052] According to one embodiment of the present invention, the Internet of Things functions include a hypertext transfer protocol function, a function of inputting and outputting information, and a function of timing triggering corresponding events.
[0053] Step S3: When the corresponding IoT function is requested, register the callback function corresponding to the corresponding event and add a specified priority to it. When the corresponding event occurs, trigger the corresponding callback function.
[0054] According to one embodiment of the present invention, when registering the callback function corresponding to the corresponding event and adding a specified priority for it, its priority is specified as level N (the lowest priority) by default. When requesting to obtain an IoT function with interruption, the specified highest priority is added to the callback function corresponding to the triggered corresponding event. For example, if the smaller the priority value is set, the higher the priority is, then a priority value of 0 indicates the highest level.
[0055] Reference Figure 2 , after registering the callback function corresponding to the corresponding event and adding the specified priority to it, the linked list structure is used to store its callback function and the corresponding priority. The linked list is actually a chain storage structure of a linear list. Unlike an array, it uses a set of arbitrary storage units to store the data in the linear list. The storage units are not necessarily continuous, and the length of the linked list is not fixed, which makes it very convenient to implement node insertion and deletion operations. Among them, the linked list includes a head pointer, the event connected to the head pointer, and the callback function and callback function priority corresponding to the event. Event A and event B are head nodes, and the head nodes are connected to each other. The head node includes a data field and an address. The data field of the head node can store additional information such as the length of the linked list, or it can store no information. The node behind the head node is used as a node for storing callback functions and priorities. Each callback function of the event is stored separately in a node, and the node includes a data field and address for storing the callback function and priority. The nodes are connected in sequence. The data field for storing the callback function and priority is a two-dimensional array, including the callback function and priority corresponding to the event, as shown in the following table:
[0056] event (callback function, priority) Event A (callback function c, 0) Event A (callback function d, 2) Event B (callback function e, 1)
[0057] Step S4: Receive the triggered callback function and store it in order of priority from high to low.
[0058] According to one embodiment of the present invention, step S4 includes steps S41 and S42:
[0059] Step S41: Using a message queue with priority, all triggered callback functions are received in a blocking reception mode. Blocking reception is a data reception mode in which the callback function to be received by the message queue is not ready and the system needs to wait and cannot continue to execute.
[0060] According to one embodiment of the present invention, step S41 includes:
[0061] When the callback functions in the message queue have not been fully executed, each time the highest priority callback function in the message queue is retrieved and executed, it is determined whether all callback functions in the message queue have been fully executed. When the callback functions in the message queue have been fully executed, the message queue waits for incoming callback functions. After execution is complete, it blocks and waits for incoming callback functions. Otherwise, it receives the corresponding callback function and executes the subsequent operation logic, improving system efficiency and reducing unnecessary waste of central processing unit (CPU) time.
[0062] Step S42: All callback functions are stored in a priority message queue in descending order of priority, wherein the priority message queue adopts a small top heap structure in order of priority. Even if a small top heap structure is formed inside the priority message queue, it is ensured that the callback function taken out each time has the highest priority.
[0063] According to one embodiment of the present invention, the storage method of the priority-ordered mini-heap structure includes: placing all callback functions into several nodes of the mini-heap according to the priority order, so that the priority of the callback function in each node of the mini-heap is greater than or equal to the priority of the callback function in the left child node and the right child node of the node. That is, the storage method of the mini-heap for sorting callback functions in order of priority is a kind of heap sorting idea. The following example illustrates this, referring to Figure 3 , A, B, C, D, and E represent different callback functions, and 1, 3, 5, 4, and 7 correspond to the priorities of the callback functions. Among them, the priority of callback function A is 1, which is greater than the priority of callback function B stored in its left child node and callback function C in its right child node. Similarly, the priority of callback function B is also greater than the priority of callback functions D and E.
[0064] Step S5: retrieve and execute the callback functions according to their priority order.
[0065] According to one embodiment of the present invention, step S5 retrieves and executes the callback functions in order of priority of the callback functions, including steps S51 and S52:
[0066] Step S51, take out and execute the callback function from the priority message queue in the order of the priority of the callback function. Each time, take out the callback function with the highest priority from the priority message queue and execute it. After execution, take out the callback function with the highest priority in the priority message queue at this time and execute it. Repeat this process. At the same time, a small top heap structure is formed inside the priority message queue, and the callback function stored at the top of the heap is the highest priority. Each time, the callback function at the top of the small top heap is taken out and executed.
[0067] Step S52: Determine whether all callback functions have been executed. The priority message queue receives callback functions through a blocking receive method. The callback functions are retrieved and executed from the priority message queue in the order of their priority until all callback functions are executed. If there are multiple callback functions with the same priority, they can be executed in the order in which they entered the priority message queue, or one of them can be randomly retrieved and executed.
[0068] According to one embodiment of the present invention, when multiple requests are made to obtain corresponding IoT functions during the parsing process (e.g., when multiple requests are made to obtain a Hypertext Transfer Protocol function), a multi-threaded execution method is adopted. The multi-threaded execution method includes:
[0069] A thread is established for each request, and the corresponding request is responded to according to the thread. When responding to the request, all callback functions of the corresponding event are triggered; among them, the Hypertext Transfer Protocol is a simple request-response protocol, which specifies what kind of message the client may request to send to the server and what kind of response it will get.
[0070] Receive and store all callback functions.
[0071] The highest-priority callback function is selected and executed. This step is repeated until all callback functions have been executed, and then the thread is exited. The order in which each thread ends execution may differ from the order in which it began. Depending on the real-time requirements of the task, the user-specified high-priority callback function can be executed first to meet real-time requirements.
[0072] According to one embodiment of the present invention, when requesting to obtain a corresponding IoT function with interruption (such as requesting to obtain a general input / output function with interruption) during the parsing process, an interruption execution mode is adopted, and the interruption execution mode includes:
[0073] Make an interrupt response and trigger the callback function of the event;
[0074] Receiving and storing a callback function, wherein the callback function is a callback function in an interrupt, and the priority of the callback function in the interrupt is set to a priority specified by a user or a highest priority;
[0075] The present invention assigns the highest priority to the callback function in the interrupt, allowing tasks with strict timing constraints to be executed first, improving the application's response to key events and further meeting the real-time requirements of IoT devices.
[0076] The present invention also provides a runtime system for providing an operating environment, which is used to provide an operating environment for applications corresponding to the Internet of Things, including: a virtual machine module 1, a functional component 2 including multiple native modules 21, an event storage module 3 and an event main loop module 4; Figure 4 The system is described in detail.
[0077] The virtual machine module 1 is used to parse the application code to obtain the priority specified by the user for the callback function corresponding to various events, and request to obtain the corresponding Internet of Things function during the parsing process.
[0078] According to one embodiment of the present invention, the virtual machine module 1 is a lightweight virtual machine that uses a lightweight scripting language (JavaScript) interpreter to parse JavaScript application codes.
[0079] Functional component 2 includes multiple native modules 21. Native modules 21 are used to register callback functions corresponding to corresponding events and assign them a specified priority when a corresponding IoT function is requested. According to one embodiment of the present invention, the native modules 21 include: a Hypertext Transfer Protocol (HTTP) module, a General-purpose input / output (GPIO) module, and a timer module. The HTTP module is used to provide HTTP functionality; the GPIO module is used to provide input and output information; and the timer module is used to provide the function of triggering corresponding events on a regular basis.
[0080] According to one embodiment of the present invention, a functional component is configured to support multi-threaded execution and interrupted execution, both of which are asynchronous execution modes. Asynchronous execution means that in the asynchronous execution mode, the order in which events are executed is not necessarily the same as the order in which they were started.
[0081] According to an embodiment of the present invention, the functional component 2 further includes an event management unit 22 for sending all callback functions corresponding to the event to the event storage module 3 .
[0082] The event storage module 3 is used to receive the triggered callback function and store it in order of priority from high to low.
[0083] The event main loop module 4 is used to retrieve and execute callback functions according to the priority order of the callback functions.
[0084] According to one embodiment of the present invention, the runtime system includes a configuration module for receiving user definitions to tailor part of the native module 21. The configuration module is not shown in the figure. The native module 21 can be directly obtained and used, or the corresponding native module 21 can be tailored according to user needs to improve the flexible use performance of the system.
[0085] According to one embodiment of the present invention, when applying the system of the present invention, it is necessary to obtain the original code of the system. After obtaining the original code, the original code can be directly compiled into a binary program, and the binary program can be burned into the target device for application (i.e., providing an operating environment for the application code). The compiled application code can be burned into the same target device and then run. Among them, the original code includes several native modules 21. When the target device does not need the corresponding native module 21, after obtaining the original code, the original code can be modified and the modified code can be recompiled and run, thereby reducing the system code space (footprint) resource usage. If developers use macro definitions to trim unnecessary native modules 21 (in the configuration file, trim the native modules 21 by setting the corresponding sub-unit macro definitions to "0"; for example, to trim the general-purpose input and output unit (GPIO), define #define GPIO_PKG 0, where setting the corresponding unit to "0" deletes the corresponding native module, and setting it to "1" adds the corresponding native module. The system adds all native modules by default), this can reduce the system code space (footprint) resource usage. It can also meet the needs of more devices by adjusting the system code space resources based on user device requirements. For example, for large, complex embedded applications, smaller space resource usage can maximize system performance, allowing applications to obtain more processor time.
[0086] The present invention is described in detail below using an example.
[0087] When the runtime system of the present invention provides an operating environment for JavaScript application code corresponding to the Internet of Things (IoT), for example, an IoT device periodically sends a Hypertext Transfer Protocol (HTTP) request to an edge server. If a response is received, all callback functions in the event are triggered. The edge server provides users with access to the network and communication with other server devices. If a response is received, all callback functions in the event are triggered. Based on this logic, the operation process of the application corresponding to the IoT device in the runtime system of the present invention is described in detail.
[0088] First, the application corresponding to the IoT device is written in JavaScript code. The user adds timer event a and its corresponding callback functions A and C, and event b and its corresponding callback function B in the JavaScript application code. Callback function A is responsible for the HTTP request, with a priority of 1, a priority of 3, and a priority of 4. The following example illustrates the writing of callback functions corresponding to events and their priorities.
[0089] For example, if you define an event to be responsible for HTTP requests, obtain the callback function and its priority after the request is successful, the code expression is as follows:
[0090]
[0091] The above code means defining an event (i.e. var http = require("http")), which is responsible for requesting http (i.e. http.request(option)). var option{} contains the parameters that need to be called when requesting http, including: uniform resource locator (url), callback function after successful request (function(res,args)), callback function priority (priority: 3), etc.
[0092] Secondly, obtain the runtime system source code and modify the system configuration file. Based on the above application, only the functional components in the system are needed to provide the function of timing triggering corresponding events and the hypertext transfer protocol function. Therefore, other native modules are not needed. For example, if the functional components do not need to provide input information and output information, the general input and output unit used to provide input information and output information will be deleted, that is, it will be deleted through the user's macro definition: #define GPIO_PKG 0. The corresponding native sub-modules are deleted according to the user's requirements for the device to reduce the system code space (footprint) resource usage.
[0093] Then, the system source code with the native submodule deleted is compiled into a binary program, and the binary program is burned into the microcontroller.
[0094] Finally, the corresponding JavaScript code is burned into the same single-chip microcomputer for execution. The callback function is processed at runtime using the event loop drive mechanism with priority formed by the event storage module 3 and the event main loop module 4. The specific operation process is as follows:
[0095] The above-mentioned JavaScript code is parsed by the system's virtual machine module 1, and the corresponding hypertext transfer protocol module is called during the parsing process. When the module is requested to be called, it registers the callback function A and callback function C corresponding to the timer event a, and adds a specified priority 1 to the callback function A and a specified priority 4 to the callback function C. When the timer event occurs, the callback function A and callback function C corresponding to the timer event a are triggered.
[0096] Send callback function A and callback function C to the priority message queue.
[0097] The event main loop module 4 takes out the callback function with the highest priority from the priority message queue for execution. At this time, callback function A has the highest priority, that is, callback function A is taken out for execution.
[0098] Register event b in callback function A, and add the required callback function B and the priority of callback function B to 3 for event b.
[0099] When executing callback function A, callback function D for successful HTTP response and corresponding priority 2 are also written in callback function A (callback function D is responsible for event b occurring when the HTTP request receives a successful response), and then an HTTP request is made, and callback function D is sent to the priority message queue.
[0100] When a successful response to the HTTP request is obtained, there are callback function C and callback function D in the priority message queue, but the priority of callback function D is higher than that of callback function C. Therefore, the event main loop module 4 takes out callback function D from the priority message queue and executes it.
[0101] After the callback function D is successfully executed, event b occurs, triggering the callback function B of event b. The event management unit 22 sends the callback function B to the message queue with priority.
[0102] At this time, the priority message queue includes callback function B and callback function C. It continuously loops and takes out the corresponding callback functions from the priority message queue in the order of the callback functions' priorities for execution, that is, callback function B is executed first and then callback function C. When the message queue is empty, it enters a blocked waiting state (that is, the message queue is always blocked waiting to receive the callback function passed in).
[0103] The above-mentioned runtime system adds a priority designed by the developer to the callback function, and in the event main loop module 4, the callback function with high priority is processed first. The event storage module 3 receives the callback function corresponding to the event in a blocking reception manner. When no callback function is passed in, it blocks and waits for the callback function to be passed in. When a callback function is passed in, it receives the callback function and then performs the subsequent operation logic. The blocking and waiting will release the central processing unit (CPU) occupied by the current event, allowing other events to run, improving the system operation efficiency and reducing unnecessary CPU time waste. Secondly, the event storage module 3 includes a small top heap structure in order of priority to ensure that the callback function with the highest priority taken out by the event main loop module 4 each time can execute the callback function specified by the user to meet the real-time requirements.
[0104] On the other hand, the above-mentioned priority message queue is used to block the reception of the callback function, and at the same time, a small top stack structure is maintained inside the priority message queue. The event main loop module 4 processes the callback function and directly obtains the highest priority callback function from the priority message queue for processing. When a callback function is passed in, the priority message queue receives it immediately. If the currently passed in callback function has always been a high priority, at this time, the event main loop module 4 will always obtain and execute the high priority callback function, which may cause the low priority one to be unable to be executed for a long time.
[0105] Therefore, based on the above analysis, the present invention also provides another method for providing an Internet of Things operating environment, which is as follows.
[0106] In the case where step S1 and step S2 remain unchanged, according to another embodiment of the present invention, another event loop driving mechanism with priority formed by the event storage module 3 and the event main loop module 4 is provided. The event storage module 3 blocks the reception of callback functions through the message queue, and maintains a small top stack structure outside the message queue for sorting and storing callback functions. After each callback function of the small top stack is executed, the event main loop module 4 obtains all callback functions in the message queue and continues to receive callback functions in the message queue within a predetermined time period. If the predetermined time period is exceeded, the reception and sorting are stopped. In this way, by receiving callback functions within a certain time period, it is possible to avoid the situation where low-priority callback functions cannot be executed due to the continuous incoming high-priority callback functions. The specific implementation method is as follows:
[0107] First, receive the triggered callback function and store it in descending order of priority, including steps a1 and a2:
[0108] Step a1: Use the message queue to receive all triggered callback functions in a blocking manner.
[0109] Step a2: Retrieve all callback functions from the message queue and store them in a priority-ordered mini-heap structure in descending order of priority. This priority-ordered mini-heap structure is external to the message queue and is stored in the same manner as the mini-heap structure maintained within the prioritized message queue. If there are multiple callback functions of the same priority, they can be stored in the order in which they entered the message queue.
[0110] According to one embodiment of the present invention, step a2 includes: after executing a callback function in the mini-top stack, the mini-top stack obtains all callback functions in the message queue, and continuously receives the callback functions in the message queue within a predetermined time period, and sorts them according to priority after receiving.
[0111] Secondly, all callback functions are retrieved and executed in order of their priority.
[0112] According to one embodiment of the present invention, when executing each callback function, the message queue will receive the callback functions as long as other callback functions are passed into the message queue. When a callback function is received in the message queue, the mini-heap will obtain all the callback functions in the message queue and sort them after executing the current callback function, and continue to obtain the callback functions in the message queue within a predetermined time period to avoid the situation where a higher-priority callback function is not put into the mini-heap and cannot be executed in time within a short period of time. At the same time, since it only sorts the callback functions after receiving them within a short period of time, it will not cause the low-priority callback functions to be unable to be executed for a long time, further meeting the real-time requirements.
[0113] According to one embodiment of the present invention, the callback functions are retrieved and executed according to the priority order of the callback functions, including steps b1 and b2:
[0114] Step b1: Retrieve and execute callback functions from the priority-ordered mini-heap in the order of their priority. Each time, the callback function with the highest priority is retrieved from the mini-heap and executed. The callback function stored at the top of the mini-heap structure has the highest priority, and the callback function at the top of the mini-heap is executed each time. If there are multiple callback functions with the same priority, they can be executed in the order in which they entered the message queue, or one of them can be randomly selected for execution.
[0115] Step b2, judge whether all callback functions have been executed. If not, then take out the callback function with the highest priority in the small top heap at this time and execute it, cycle this process, after each callback function is executed, the small top heap will continue to obtain the callback function in the message queue within the predetermined duration after execution, when there is a callback function to be passed in within the predetermined time, receive the callback function and deliver it to the small top heap, until it exceeds the predetermined time, again take out and execute the callback function from the small top heap according to the callback function priority order. Wherein, this predetermined time can be set according to user demand, such as being set to 100 milliseconds.
[0116] According to another embodiment of the present invention, in combination with the above another method for providing an Internet of Things operating environment, when the runtime system of the present invention provides an operating environment for the JavaScript application code corresponding to the same Internet of Things, based on this, the operating process of the runtime system is described in detail.
[0117] First, the application corresponding to the IoT device is written in JavaScript code. The user adds timer event a and the corresponding callback functions A and C, event b and the corresponding callback function B in the JavaScript application code. The callback function A is responsible for the HTTP request. The priority of callback function A is 1, the priority of callback function B is 3, and the priority of callback function C is 4.
[0118] Secondly, obtain the source code of the runtime system and modify the system configuration file. Based on the above application, only the functional components in the system need to provide the function of timing triggering corresponding events and the hypertext transfer protocol function. Therefore, other native modules are not required. For example, if the functional components do not need the function of providing input information and output information, the general input and output module used to provide input information and output information will be deleted, that is, it will be deleted by the user through the macro definition: #define GPIO_PKG 0.
[0119] Then, the system source code with the native submodule deleted is compiled into a binary program, and the binary program is burned into the microcontroller.
[0120] Finally, the corresponding JavaScript code is burned into the same MCU and run. The running process is as follows:
[0121] The above-mentioned JavaScript code is parsed by the system's virtual machine module 1, and the corresponding hypertext transfer protocol module is called during the parsing process. When the module is requested to be called, it registers the callback function A and callback function C corresponding to the timer event a, and adds a specified priority 1 to the callback function A and a specified priority 4 to the callback function C. When the timer event occurs, the callback function A and callback function C corresponding to the timer event a are triggered.
[0122] Send callback function A and callback function C to the message queue.
[0123] The small top stack starts to obtain callback function A and callback function C from the message queue. At the same time, the small top stack needs to continue to obtain callback functions within a predetermined time (wait for a certain time, the default setting is to wait 100ms). When no new callback function arrives at the message queue within the predetermined time, the acquisition is stopped and all the callback functions obtained are sorted.
[0124] The event main loop module 4 takes out the callback function with the highest priority from the top stack for execution. At this time, callback function A has the highest priority, so callback function A is taken out for execution.
[0125] Register event b in callback function A, add the required callback function B for event b and add the specified priority 3.
[0126] When executing callback function A, callback function D for successful HTTP response and corresponding priority 2 are also written in callback function A (callback function D is responsible for event b occurring when the HTTP request receives a successful response), and then the HTTP request is made, and callback function D is passed to the top heap through the message queue.
[0127] When a successful response to the HTTP request is obtained, the small top stack obtains the callback function C and callback function D in the message queue at this time, and continues to obtain the callback function within a predetermined time. After the predetermined time, the five callback functions enter and the acquisition is stopped. At this time, the priority of callback function D is higher than the priority of callback function C. Therefore, the event main loop module 4 takes out and executes callback function D from the small top stack.
[0128] After the callback function D is successfully executed, event b occurs, triggering the callback function B of event b. The event management unit 22 sends the callback function B to the message queue.
[0129] The small top heap starts to obtain all callback functions of event b from the message queue. It stops obtaining and sorting after waiting for a certain period of time (the default is 100ms) when no new callback functions arrive. At this time, only callback function B enters. Therefore, the small top heap only includes callback function B and callback function C. Then, the callback function with the highest priority is taken out from the small top heap for execution and the cycle continues.
[0130] It should be noted that although the above describes the various steps in a specific order, it does not mean that the steps must be performed in the above specific order. In fact, some of these steps can be executed concurrently or even in a different order as long as the required functions can be achieved.
[0131] The present invention may be a system, a method and / or a computer program product. The computer program product may include a computer-readable storage medium carrying computer-readable program instructions for causing a processor to implement various aspects of the present invention.
[0132] Computer-readable storage media can be a tangible device that holds and stores the instructions used by an instruction execution device. Computer-readable storage media can, for example, include, but are not limited to, an electrical storage device, a magnetic storage device, an optical storage device, an electromagnetic storage device, a semiconductor storage device, or any suitable combination thereof. More specific examples (non-exhaustive list) of computer-readable storage media include: a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), a static random access memory (SRAM), a portable compact disc read-only memory (CD-ROM), a digital versatile disk (DVD), a memory stick, a floppy disk, a mechanical encoding device, a punch card or a raised structure in a groove on which instructions are stored, for example, and any suitable combination thereof.
[0133] While various embodiments of the present invention have been described above, the above descriptions are intended to be illustrative, non-exhaustive, and not limited to the disclosed embodiments. Many modifications and variations will be apparent to those skilled in the art without departing from the scope and spirit of the described embodiments. The terminology used herein is selected to best explain the principles of the embodiments, their practical applications, or technological improvements in the marketplace, or to enable others skilled in the art to understand the embodiments disclosed herein.
Claims
1. A method for providing an Internet of Things operating environment, characterized in that: include: Obtaining an application code, wherein the application code includes priorities of callback functions corresponding to various events; Parse the above application code to obtain the priorities specified by the user for the callback functions corresponding to various events, and request the corresponding IoT functions during the parsing process; When the corresponding IoT function is requested, register the callback function corresponding to the corresponding event and add the specified priority to it. When the corresponding event occurs, trigger the corresponding callback function; Receive triggered callback functions and store them in descending order of priority, including: A message queue with priority is used to receive all triggered callback functions in a blocking reception manner, which includes: when the callback function in the message queue has not been completed, after each callback function with the highest priority in the message queue is taken out and executed, it is determined whether all callback functions in the message queue have been executed; when the callback function in the message queue has been executed, the message queue waits for the callback function to be transferred in; All callback functions are stored in a priority message queue in descending order of priority, wherein the priority message queue adopts a mini-heap structure in order of priority; The callback functions are retrieved and executed in the order of their priority.
2. The method according to claim 1, characterized in that The callback function triggered by the reception is stored in descending order of priority, including: Use the message queue to receive all triggered callback functions in a blocking manner; Take out all callback functions in the message queue and store them in a small top heap structure in descending order of priority.
3. The method according to claim 2, characterized in that The method of taking out all callback functions from the message queue and storing all callback functions in a small top heap structure in descending order of priority includes: After executing a callback function in the small top stack, the small top stack obtains all callback functions in the message queue and continues to receive callback functions in the message queue within a predetermined time period. After receiving, the callback functions are sorted according to priority.
4. The method according to claim 2, characterized in that The storage method of the priority-ordered mini-heap structure includes: All callback functions are placed into several nodes of the mini-heap according to the priority order, so that the priority of the callback function in each node of the mini-heap is greater than or equal to the priority of the callback function in the left child node and the right child node of the node.
5. The method according to claim 1, wherein The Internet of Things functions include the function of hypertext transfer protocol, the function of inputting and outputting information, and the function of timing triggering corresponding events.
6. The method according to claim 1 or 5, characterized in that In the request to obtain the corresponding Internet of Things function during the parsing process, when multiple requests all need to obtain the same Internet of Things function, a multi-threaded execution method is adopted, wherein the multi-threaded execution method includes: A thread is created for each request, and the corresponding request is responded to according to the thread. When responding to the request, all callback functions of the corresponding event are triggered; Receive and store all callback functions; The callback function with the highest priority is taken out for execution, and this step is repeated until all callback functions are executed, and then the thread is exited.
7. The method according to claim 1 or 5, characterized in that In the request to obtain the corresponding IoT function with interruption during the parsing process, an interruption execution mode is adopted, and the interruption execution mode includes: Make an interrupt response and trigger the callback function of the event; Receiving and storing a callback function, wherein the callback function is a callback function in an interrupt, and the priority of the callback function in the interrupt is set to a priority specified by a user or a highest priority; Take out the callback function in the interrupt and execute it.
8. A runtime system for providing an operating environment implemented based on the method of any one of claims 1 to 7, for providing an operating environment for an application corresponding to the Internet of Things, characterized in that: include: Virtual machine module, functional components containing multiple native modules, event storage module and event main loop module; The virtual machine module is used to parse the application code to obtain the priorities specified by the user for the callback functions corresponding to various events, and request the corresponding IoT functions during the parsing process; The native module is used to register a callback function corresponding to a corresponding event and add a specified priority to it when a corresponding IoT function is requested, and trigger the corresponding callback function when the corresponding event occurs; The event storage module is used to receive the triggered callback functions and store them in descending order of priority; The event main loop module is used to retrieve and execute callback functions in order of their priority.
9. The system according to claim 8, characterized in that The system includes a configuration module for receiving user definition to tailor part of the native module.
10. The system according to claim 8, wherein: The functional component is used to support a multi-threaded execution mode and an interrupted execution mode. The functional component includes an event management unit, which is used to send a callback function triggered when a corresponding event occurs to an event storage module.
Citation Information
Patent Citations
Embedded system as well as interrupt processing method and device thereof
CN103294544A
Method and device for communication between tasks in embedded real-time operating system
CN110825536A