Transplanting method and device of operating system driver

By creating a driver read and write operation function set and signal processing module at the driver layer of the target operating system, and encapsulating the data unit read and write function function and semaphore response connection function at the application layer, the complexity problem of the hardware driver porting process is solved, and simplified hardware interaction logic and efficient development process are realized.

CN120216019AActive Publication Date: 2025-06-27WUHAN VOCATIONAL COLLEGE OF SOFTWARE & ENG (WUHAN OPEN UNIV)
View PDF 6 Cites 0 Cited by

Patent Information

Application Number
CN202510188664.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-20
Publication Date
2025-06-27
Estimated Expiration
2045-02-20

AI Technical Summary

Technical Problem

The existing technology is difficult to effectively simplify the hardware driver porting process, especially when the target operating system and hardware architecture change, resulting in developers needing to redevelop or deeply adjust the code, increasing development complexity and cost.

Method used

By creating a set of driver read and write operation functions at the driver layer of the target operating system, and using the signal processing mechanism of the target operating system to simulate the hardware interrupt mechanism, encapsulate the underlying register operation, and simplify the hardware interaction logic. Then, a data unit read-write function set and semaphore response connection function are created at the application layer, these functions are combined with the functional functions in the open source demo program, replace the original register read-write statement, and add the semaphore response connection function.

Benefits of technology

It realizes a highly transparent processing of hardware interaction logic, allowing developers to directly call functional functions in the target hardware open source demo program, simplifying the development process, significantly shortening the development cycle, reducing costs, and improving the reusability and maintainability of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120216019A_ABST
    Figure CN120216019A_ABST
Patent Text Reader

Abstract

The invention discloses a transplanting method and device for an operating system driver, and the method comprises the steps: determining a driver read-write operation function set created by using an input / output control function according to an I / O mapping mode of target hardware; obtaining a driving process number sending function and a driving semaphore sending function created based on a signal processing mechanism of the target operating system; creating a data unit read-write performance function set based on the drive read-write operation function set; creating a semaphore response connection function based on the driving process number sending function and the driving semaphore sending function; and obtaining a performance function program file in an open source modem program of the target hardware, replacing a data unit read-write statement in the performance function program file with the data unit read-write performance function set, and adding the semaphore response connection function into the performance function program file to obtain a transplanted performance function program file. According to the method, the hardware interaction is simplified and the code reusability is improved by packaging the read-write operation of the bottom layer and simulating the hardware interruption.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to a method and device for transplanting an operating system driver. Background Art

[0002] In modern computer systems, hardware drivers play a crucial role. As a bridge between the operating system and hardware devices, they are responsible for managing device resources, processing I / O requests, and providing a stable interface for upper-layer application programs. With the popularization and increasing complexity of embedded systems, hardware manufacturers usually provide demo versions of drivers and sample codes for specific devices so that users can quickly start developing application programs suitable for their own needs. These demo programs often contain two key parts: register access I / O functions and functional functions.

[0003] Register access I / O functions are the core of underlying hardware operations. They directly determine how to interact with the hardware, such as reading or writing data at specific addresses, configuring hardware parameters, etc. Such functions are highly dependent on the specific hardware design. Therefore, when transplanted to a new environment, corresponding adjustments must be made according to the new hardware architecture and operating system characteristics.

[0004] Functional functions, on the other hand, focus more on implementing specific business logics or algorithms, such as controlling the state of an LED lamp, calculating sensor data, etc. This part of the code is usually independent of specific hardware details and can be reused on different platforms as long as the underlying I / O operations it depends on are correctly implemented. Summary of the Invention

[0005] In order to simplify the workload of transplanting the demo program to the target operating system driver layer, embodiments of the present invention provide a method and device for transplanting an operating system driver.

[0006] In a first aspect, embodiments of the present invention provide a method for transplanting an operating system driver, which may include:

[0007] In the driver layer of the target operating system, according to the I / O mapping mode of the target hardware, determine a set of driver read / write operation functions created using input / output control functions; the set of driver read / write operation functions includes functions for reading data units of the target hardware and functions for writing data to data units of the target hardware;

[0008] Obtain a driver process ID sending function and a driver semaphore sending function created based on the signal processing mechanism of the target operating system;

[0009] In the application layer of the target operating system, based on the set of driver read / write operation functions, create a set of data unit read / write functional functions;

[0010] Create a semaphore response connection function based on the drive process number sending function and the drive semaphore sending function;

[0011] Obtain the function program file in the open-source demo program of the target hardware, use the data unit reading and writing function set to replace the data unit reading and writing statements in the function program file, and add the semaphore response connection function to the function program file to obtain the transplanted function program file.

[0012] In one or some alternative embodiments of the embodiments of the present application, the creating a data unit reading and writing function set based on the drive reading and writing operation function set includes:

[0013] Call the function in the drive reading and writing operation function set for reading the data unit of the target hardware to create a data unit reading function; the data unit reading function includes assignment statements for the value and address of the data unit;

[0014] Call the function in the drive reading and writing operation function set for writing data to the data unit of the target hardware to create a data unit writing function; the data unit writing function includes assignment statements for the data unit address and value.

[0015] In one or some alternative embodiments of the embodiments of the present application, the obtaining the drive process number sending function and the drive semaphore sending function created based on the signal processing mechanism of the target operating system includes:

[0016] Obtain the drive process number sending function created using the input / output control function; the drive process number sending function includes the process number;

[0017] Based on the signal processing mechanism of the target operating system, determine the open-source semaphore sending function;

[0018] Construct the drive semaphore sending function based on the process number and the open-source semaphore sending function.

[0019] In one or some alternative embodiments of the embodiments of the present application, after obtaining the function program file in the open-source demo program of the target hardware, it further includes:

[0020] Add a function for opening the drive and a function for closing the drive to the function program file;

[0021] Call the function for opening the drive and the function for closing the drive in the function program file.

[0022] In one or some alternative embodiments of the embodiments of the present application, after obtaining the function function program file in the open-source demo program of the target hardware, the following is further included:

[0023] Based on the target operating system, modify the register base address in the function function program file.

[0024] In one or some alternative embodiments of the embodiments of the present application, the target operating system includes Linux, maxOS, FreeBSD;

[0025] In the driver layer of the target operating system, according to the I / O mapping method of the target hardware, determine the driver read / write operation function set created using the input / output control function, including:

[0026] In the driver layer of the target operating system, according to the I / O mapping method of the target hardware, determine the driver read / write operation function set created using the ioctl function.

[0027] In a second aspect, an embodiment of the present invention provides a device for transplanting an operating system driver, which may include:

[0028] A driver read / write module, configured to determine, in the driver layer of the target operating system, a driver read / write operation function set created using the input / output control function according to the I / O mapping method of the target hardware; the driver read / write operation function set includes a function for reading a data unit of the target hardware and a function for writing data to the data unit of the target hardware;

[0029] A driver signal processing module, configured to obtain a driver process number sending function and a driver semaphore sending function created based on the signal processing mechanism of the target operating system;

[0030] An application read / write module, configured to create a data unit read / write function set in the application layer of the target operating system based on the driver read / write operation function set;

[0031] An application signal processing module, configured to create a semaphore response connection function based on the driver process number sending function and the driver semaphore sending function;

[0032] A program transplantation module, configured to obtain the function function program file in the open-source demo program of the target hardware, replace the data unit read / write statements in the function function program file with the data unit read / write function set, and add the semaphore response connection function to the function function program file to obtain the transplanted function function program file.

[0033] In a third aspect, an embodiment of the present invention provides a computer-readable storage medium, on which a computer program / instructions are stored, and when the computer program / instructions are executed by a processor, the above-mentioned method for transplanting an operating system driver is implemented.

[0034] In a fourth aspect, an embodiment of the present invention provides a computer program product, including a computer program / instructions, and when the computer program / instructions are executed by a processor, the above-mentioned method for transplanting an operating system driver is implemented.

[0035] In a fifth aspect, an embodiment of the present invention provides a computer device, including a memory, a processor, and a computer program stored on the memory, and when the processor executes the computer program, the above-mentioned method for transplanting an operating system driver is implemented.

[0036] The beneficial effects of the above technical solutions provided by the embodiments of the present invention at least include:

[0037] An embodiment of the present invention provides a method for transplanting an operating system driver. The method creates a set of driver read and write operation functions according to the I / O mapping method of the hardware in the driver layer of the target operating system, including functions for reading and writing hardware data units. Then, a driver process ID sending function and a driver semaphore sending function created based on the signal processing mechanism of the target operating system are obtained. Next, a set of data unit read and write function functions and a semaphore response connection function are created in the application layer. Finally, the function program file in the target hardware open-source demo program is obtained, the data unit read and write statements therein are replaced with data unit read and write function functions, and the semaphore response connection function is added to the function program, thereby completing the transplantation. This method encapsulates the underlying register operations in the driver read and write operation function set and uses the signal processing mechanism in the target operating system to simulate the hardware interrupt mechanism, achieving a highly transparent processing of the hardware interaction logic, enabling developers to directly call the function functions in the target hardware open-source demo program to meet the requirements of the application layer, making full use of the already debugged demo program provided by the hardware manufacturer, avoiding the process of re-development and in-depth technology digestion, and thus significantly shortening the development cycle, reducing costs, and improving the code reusability and maintainability.

[0038] Other features and advantages of the present invention will be described in the following specification, and part of them will become obvious from the specification or be understood by implementing the present invention. The objectives and other advantages of the present invention can be achieved and obtained through the structures specifically pointed out in the written specification and the accompanying drawings.

[0039] The technical solutions of the present invention will be further described in detail below through the accompanying drawings and embodiments. Description of the Drawings

[0040] The accompanying drawings are used to provide a further understanding of the present invention, and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and do not constitute a limitation to the present invention. In the accompanying drawings:

[0041] Figure 1 It is a schematic flowchart of the method for transplanting an operating system driver provided by an embodiment of the present invention;

[0042] Figure 2 It is a schematic structural diagram of the device for transplanting an operating system driver provided by an embodiment of the present application. Detailed implementation manners

[0043] Hereinafter, exemplary embodiments of the present disclosure will be described in more detail with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided so that the present disclosure can be more thoroughly understood and the scope of the present disclosure can be fully conveyed to those skilled in the art.

[0044] The inventors found that in the actual development process of operating system drivers, if the demo program is directly and simply transplanted into the driver program, the functional functions, register access I / O functions, and underlying drivers will be mixed together, resulting in the inability to call the functional functions and register I / O access functions. At this time, technical digestion is required, and then combined with the hardware itself, re-development is necessary, which will inevitably affect the development progress.

[0045] In addition, for some complex hardware configurations, such as CAN buses, flash access, etc., even with the reference of the demo programs provided by manufacturers, it is still a time-consuming and error-prone task to write a driver program suitable for the target operating system from scratch. Especially in multi-task operating systems such as Unix-like, there is a distinction between user space and kernel space, and the traditional interrupt handling mechanism is no longer applicable. Developers also need to consider the issue of cross-layer communication additionally to ensure that hardware events can be timely transmitted to the application program.

[0046] At the same time, the existing transplantation methods may cause the functional functions to be tightly coupled with the underlying hardware operations, so that every time the hardware platform is changed, this part of the code needs to be re-developed or adjusted significantly. Based on this, the inventors have made further research and developed the present invention to provide a method and device for transplanting an operating system driver.

[0047] Embodiment 1

[0048] In Embodiment 1 of the present invention, a method for transplanting an operating system driver is provided. Referring to Figure 1 as shown, the method may include the following steps S101-S105:

[0049] S101: At the driver layer of the target operating system, determine a set of driver read / write operation functions created using input / output control functions according to the I / O mapping method of the target hardware. The set of driver read / write operation functions includes functions for reading data units of the target hardware and functions for writing data to data units of the target hardware.

[0050] S102: Obtain a driver process ID sending function and a driver semaphore sending function created based on the signal processing mechanism of the target operating system.

[0051] S103: At the application layer of the target operating system, create a set of data unit read / write function capabilities based on the set of driver read / write operation functions.

[0052] S104: Create a semaphore response connection function based on the driver process ID sending function and the driver semaphore sending function.

[0053] S105: Obtain the functional function program file in the open-source demo program of the target hardware, replace the data unit read / write statements in the functional function program file with the set of data unit read / write function capabilities, and add the semaphore response connection function to the functional function program file to obtain the transplanted functional function program file.

[0054] The embodiment of the present invention provides a method for transplanting an operating system driver. This method creates a set of driver read / write operation functions according to the I / O mapping method of the hardware at the driver layer of the target operating system, including functions for reading and writing hardware data units. Then, it obtains a driver process ID sending function and a driver semaphore sending function created based on the signal processing mechanism of the target operating system. Next, it creates a set of data unit read / write function capabilities and a semaphore response connection function at the application layer. Finally, it obtains the functional function program file in the open-source demo program of the target hardware, replaces the register read / write statements therein with the data unit read / write function capabilities, and adds the semaphore response connection function to the functional program, thus completing the transplantation. This method encapsulates the underlying register operations in the set of driver read / write operation functions and uses the signal processing mechanism in the target operating system to simulate the hardware interrupt mechanism, achieving a highly transparent processing of the hardware interaction logic. This enables developers to directly call the functional functions in the open-source demo program of the target hardware to meet the requirements of the application layer, fully utilize the already debugged demo program provided by the hardware manufacturer, avoid the process of re-development and in-depth technical digestion, and thus significantly shorten the development cycle, reduce costs, and improve the code reusability and maintainability.

[0055] In the above step S101, at the driver layer of the target operating system, according to the I / O mapping mode of the target hardware, a set of driver read / write operation functions created using input / output control functions is determined. The set of driver read / write operation functions includes a function for reading data units of the target hardware and a function for writing data to the data units of the target hardware. Among them, the target operating system includes systems such as Unix-like and Windows. Unix-like systems include Linux, maxOS, FreeBSD, etc.

[0056] Specifically, it can be that, first, at the driver layer of the target operating system, the appropriate input / output control function is determined according to the I / O mapping mode of the target hardware. The input / output control function will be used to implement the reading and writing of data units of the target hardware. There will be differences between different hardware and operating systems, so it is necessary to select an appropriate input / output control function. For example, ioctl is used in Linux, and DeviceIoControl may be used in Windows.

[0057] Then, according to the data units of the target hardware, a set of driver read / write operation functions is obtained. This set of functions includes a function for reading data of the target hardware and a function for writing data to the target hardware. Taking the target hardware as the CAN bus as an example, there will be dedicated commands such as CANBUS_IOCGETREG for calling the function to read the register value and CANBUS_IOCSETREG for calling the function to write data to the register.

[0058] To facilitate the understanding of this solution by those skilled in the art, the following takes the target operating system as Linux and the target hardware as the CAN bus as an example to detail the execution steps of the above step S101:

[0059] In the kernel space of Linux, according to the I / O mapping mode of the CAN bus, a set of driver read / write operation functions created using the ioctl function is determined. This set of functions includes two types of functions, namely a function for reading data units of the CAN bus and a function for writing data to the data units of the CAN bus.

[0060] Taking the data unit as a register as an example, an example of the function for reading the value of the register in the CAN bus is as follows:

[0061]

[0062] In the above example, the function name is CANBUS_ioctl, which is created by referring to the input / output control function ioctl in Linux. The function prototype of ioctl is ioctl(struct inode *inode, struct file *fp, unsigned long cCmd, unsigned long arg). Among them, cCmd represents the command. When cCmd is equal to "CANBUS_IOCGETREG", it means using the function to read the CAN bus data unit. The read / write command is passed by the application program. arg is the parameter, which contains the address and data. In the above example, arg contains a register address parameter. The function flexibly processes the incoming address and data by dereferencing the arg pointer.

[0063] In the above example, "ioarg = *(unsigned short *)arg" means obtaining the register address according to the I / O mapping method of the CAN bus chip and storing it in the parameter ioarg.

[0064] "addr = ioarg & 0xff" means taking the lower 8 bits of ioarg as the register address.

[0065] "*(usCANAddr + 0) = addr" means writing the register address to the position of usCANAddr + 0. Among them, usCANAddr is a pointer to the CAN bus register.

[0066] "value = *(usCANAddr + 1)" means reading the value of the register from the position of usCANAddr + 1.

[0067] "ioarg = value << 8" means shifting the value of the register 8 bits to the left.

[0068] "*(unsigned short *)arg = ioarg" means storing the value of the register back into the parameter arg to complete the reading of the register value.

[0069] An example of the function for writing data to the register in the CAN bus is as follows:

[0070]

[0071] In the above example, "ioarg = *(unsigned short *)arg" means obtaining the register address and value according to the I / O mapping method of the CAN bus chip. The lower 8 bits are the address, and the higher 8 bits are the value, and storing them in the parameter ioarg.

[0072] "addr = ioarg & 0xff" means taking the lower 8 bits of ioarg as the address of the register.

[0073] "*(usCANAddr + 0) = addr" means writing the register address to the position of usCANAddr + 0, where usCANAddr is a pointer to the CAN bus register.

[0074] "value = ioarg >> 8" means taking the higher 8 bits as the value of the register.

[0075] "*(usCANAddr + 1) = value" means writing the data to the position of usCANAddr + 1, that is, into the register, completing the data writing to the register.

[0076] In the embodiments of the present application, the above-mentioned driver read and write operation function set in step S101 ensures that the driver program can correctly interact with the hardware by precisely controlling the read and write operations of the target hardware. By using the driver functions adapted to the hardware I / O mapping method, data units can be efficiently read and written, ensuring the accurate transmission of data.

[0077] In the above step S102, obtain the driver process number sending function and the driver semaphore sending function created based on the signal processing mechanism of the target operating system. Specifically, it includes the following steps S1021 - S1023:

[0078] S1021: Obtain the driver process number sending function created using the input / output control function. Among them, the driver process number sending function includes the process number.

[0079] Specifically, it can be that the driver process number sending function allows the application program to inform the driver program of its process ID through the input / output control function, so that the driver knows which process to send the signal to. By passing the command, the process number of the application program is passed to the driver layer.

[0080] Taking the target operating system as Linux and the input / output control function as ioctl as an example, the example of the driver process number sending function is as follows:

[0081]

[0082] In the above example, the input parameter arg represents the process number of the application program, and "app_pid = arg" means storing the process number in the parameter app_pid. Thereafter, the driver can use the parameter app_pid to determine the target process number when sending the signal.

[0083] S1022: Based on the signal processing mechanism of the target operating system, determine the open-source semaphore sending function.

[0084] Specifically, it can be to select an appropriate semaphore sending function based on the signal processing mechanism of the target operating system. In the Linux system, the send_sig function is usually used to send signals to the application program.

[0085] S1023: Construct a driver semaphore sending function based on the process ID and an open-source semaphore sending function.

[0086] Specifically, it can be to construct a driver semaphore sending function by combining the process ID and the semaphore sending function. When the hardware triggers an interrupt, the driver will trigger the driver semaphore sending function to find the process ID corresponding to the application program and send the semaphore through send_sig.

[0087] Taking the target operating system as Linux and the semaphore sending function as send_sig as an example, the example of the driver semaphore sending function is as follows:

[0088] static irqreturn_t CANBUS_interrupt(int irq,void*dev_id)

[0089] {

[0090] ptask = find_task_by_vpid(app_pid);

[0091] if(ptask!= NULL)

[0092] {

[0093] send_sig(CANBUS_SIGNAL,ptask,1);

[0094] }

[0095] return IRQ_RETVAL(IRQ_HANDLED);

[0096] }

[0097] In the above example, CANBUS_interrupt is the name of the driver semaphore sending function, and "ptask = find_task_by_vpid(app_pid)" means to find the corresponding task ID ptask according to the process ID app_pid. Among them, the function find_task_by_vpid() is a function in the Linux kernel.

[0098] "send_sig(CANBUS_SIGNAL,ptask,1)" means to send the semaphore CANBUS_SIGNAL to the application program with the task ID ptask.

[0099] "return IRQ_RETVAL(IRQ_HANDLED)" is a specific value returned in the Linux kernel to inform the Linux kernel driver that the semaphore sending function has been successfully run.

[0100] In the embodiments of the present application, in the above step S102, by obtaining the process number of the application program and combining the signal processing mechanism of the target operating system, the driver can send signals to the correct process in a timely and accurate manner, trigger an interrupt response, and ensure the efficient cooperation and system stability between hardware and software. This enables hardware events to smoothly affect the behavior of the application program and simulate the interrupt function of the hardware device.

[0101] In the above step S103, in the application layer of the target operating system, a data unit read / write function set is created based on the driver read / write operation function set. Specifically, it includes the following steps S1031 - S1032:

[0102] S1031: Call the function in the driver read / write operation function set for reading the target hardware data unit to create a data unit reading function. The data unit reading function includes assignment statements for the value and address of the data unit.

[0103] Specifically, it can be that in the application layer, based on assigning the address of the target hardware data unit, and then calling the function in the driver for reading the target hardware data unit to obtain the value of the target hardware data unit, thereby realizing the acquisition of the target hardware data unit.

[0104] Then, encapsulate the above operation of obtaining the target hardware data unit in the application layer to obtain a data unit reading function.

[0105] Taking the function in the driver read / write operation function set for reading the target hardware data unit obtained in the above step S101 as an example, the data unit reading function obtained through the above step S1031 is as follows:

[0106] unsigned char ReadSJAReg(unsigned char RegAdr)

[0107] {

[0108] unsigned short arg;

[0109] arg = RegAdr;

[0110] if(ioctl(SJA1000_fd,CANBUS_IOCGETREG,&arg)!= 0)

[0111] {

[0112] return -1;

[0113] }

[0114] else

[0115] {

[0116] return arg & 0xff;

[0117] }

[0118] }

[0119] In the above example, ReadSJAReg is the name of the function for reading data units. "arg = RegAdr" is an assignment statement for the address. RegAdr is the input parameter representing the address of the target hardware data unit.

[0120] "ioctl(SJA1000_fd, CANBUS_IOCGETREG, &arg)" is to call the function in the driver read / write operation function set obtained in the above step S101 for reading the target hardware data unit, find the corresponding value according to the address of the target hardware data unit, and assign the value of the data unit to the parameter arg and return it. Among them, the parameter SJA1000_fd is a file descriptor used to represent an open device file, such as the device file descriptor representing the SJA1000 CAN bus controller.

[0121] "return arg & 0xff" means that after "ioctl(SJA1000_fd, CANBUS_IOCGETREG, &arg)" is successfully executed, the lower 8 bits of the parameter arg are returned, that is, the value of the target hardware data unit.

[0122] S1032: Call the function in the driver read / write operation function set for writing data to the target hardware data unit to create a function for writing data units; the function for writing data units includes assignment statements for the data unit address and value.

[0123] Specifically, in the application layer, based on assigning the address of the target hardware data unit, and then calling the function in the driver for writing data to the target hardware data unit, so as to write data to the target hardware data unit.

[0124] Then, encapsulate the above operation of writing data to the target hardware data unit in the application layer to obtain a function for writing data units.

[0125] Taking the function in the drive read / write operation function set obtained in the above step S101 for writing data to the target hardware data unit as an example, the data unit write function example obtained through the above step S1032 is as follows:

[0126] char WriteSJAReg(unsigned char RegAdr,unsigned char Value)

[0127] {

[0128] unsigned short arg;

[0129] arg = Value << 8 | RegAdr;

[0130] if(ioctl(SJA1000_fd,CANBUS_IOCSETREG,&arg) != 0)

[0131] {

[0132] return -1;

[0133] }

[0134] else

[0135] {

[0136] return 0;

[0137] }

[0138] }

[0139] In the above example, WriteSJAReg is the name of the data unit write function. "arg = Value << 8 | RegAdr" is an assignment statement for the data unit address and value, which is used to combine the data unit address and value into a 16-bit value. The high 8 bits are the value, and the low 8 bits are the address, and then it is assigned to the parameter arg. Among them, Value and RegAdr are input parameters, representing the value and address of the target hardware data unit respectively.

[0140] "ioctl(SJA1000_fd,CANBUS_IOCGETREG,&arg)" is to call the function in the drive read / write operation function set obtained in the above step S101 for reading the target hardware data unit, find the corresponding value according to the address of the target hardware data unit, and assign the value of the data unit to the parameter arg and return it.

[0141] After the successful execution of "ioctl(SJA1000_fd, CANBUS_IOCGETREG, &arg)", the data unit writing function will return 0, indicating that the data has been successfully written to the target hardware data unit.

[0142] In the embodiment of the present application, the above step S103 simplifies the hardware access operation and improves the code maintainability and reusability by encapsulating the driver read and write operation functions in the application layer to create data unit read and write functions. The application layer only needs to call these encapsulated functions to implement the reading and writing of the target hardware data unit without paying attention to the specific implementation details of the underlying driver. This encapsulation makes the hardware control operation more abstract and flexible, helps to reduce the code changes during subsequent function modifications, and improves the overall stability and scalability of the system.

[0143] In the above step S104, a semaphore response connection function is created based on the driver process ID sending function and the driver semaphore sending function.

[0144] Specifically, in the application layer, the process ID of the driver application is notified through the driver process ID sending function to ensure that the driver can correctly send the semaphore to the corresponding process. Then, the semaphore sent by the driver semaphore sending function to the application is bound to the corresponding signal processing function, so that when a signal is received, the corresponding processing logic can be triggered. This step is similar to the binding of the driver and the interrupt function, making the semaphore response function automatically called when the signal arrives, thereby realizing the response processing of hardware events.

[0145] Taking the driver process ID sending function and the driver semaphore sending function obtained in the above step S102 as an example, the semaphore response connection function created through the above step S104 is as follows:

[0146] ioctl(SJA1000_fd, COMMAND_CHANGE_PID, getpid());

[0147] sig_act.sa_handler = SJA1000_signal;

[0148] sig_act.sa_flags = 0;

[0149] sigemptyset(&sig_act.sa_mask);

[0150] ret = sigaction(CANBUS_SIGNAL, &sig_act, NULL);

[0151] In the above example, "ioctl(SJA1000_fd, COMMAND_CHANGE_PID, getpid())" is the function for sending the driver process ID, and CANBUS_SIGNAL is the semaphore sent by the driver semaphore sending function to the application program.

[0152] "sig_act.sa_handler = SJA1000_signal" means setting the signal handling function to the SJA1000_signal function. When the signal CANBUS_SIGNAL is received, the SJA1000_signal function will be called.

[0153] "sig_act.sa_flags = 0" means setting the flag bit of the signal handling function to 0, indicating that no special flags are used, such as not automatically restarting system calls, etc.

[0154] "sigemptyset(&sig_act.sa_mask)" means clearing the signal mask to ensure that no other signals are blocked additionally during the execution of the signal handling function. This helps to avoid deadlocks or other problems during signal processing.

[0155] "ret = sigaction(CANBUS_SIGNAL, &sig_act, NULL)" means binding the semaphore CANBUS_SIGNAL sent by the driver semaphore sending function to the application program with the corresponding signal handling function sig_act. The sigaction function is a system call in the Linux system for handling signals.

[0156] In the embodiment of this application, the above step S104 creates a semaphore response connection function to ensure that the application program can correctly respond to the signals sent by the driver, realizes the binding of the semaphore and the signal handling function, thereby realizing the automatic processing of driver signals, simulating the interrupt handling mechanism in the driver, enabling this method to implement interrupt handling without modifying the relevant demo program for interrupt handling. It can trigger the processing logic in the application program in a timely manner when a hardware event occurs, improving the response speed and reliability of the system and avoiding the need for manual intervention.

[0157] In the above step S105, obtain the function function program file in the open-source demo program of the target hardware, replace the data unit read and write statements in the function function program file with the data unit read and write function set, and add the semaphore response connection function to the function function program file to obtain the transplanted function function program file. Specifically, it includes the following steps S1051 - S1054:

[0158] S1051: Obtain the function function program file in the open-source demo program of the target hardware.

[0159] Specifically, it can be to download or extract the function function program file in the open-source demo program that has been debugged and passed by the hardware manufacturer. Usually, these program files will contain the direct operation logic for the hardware, such as the original programs of non-operating systems like single-chip microcontrollers or DSPs. These program files may be developed based on bare metal or application programs in an embedded development environment.

[0160] S1052: Add and call a function to open the driver and a function to close the driver in the function function program file.

[0161] Specifically, it can be to add and call the control function for the driver in the function function program file to control the life cycle of the driver, which is the basic process of driver development. Specifically, it includes a function to open the driver and a function to close the driver.

[0162] The function open() to open the driver will perform operations such as device initialization and resource allocation and return a file descriptor. Opening the driver is the first step for the user program to interact with the driver.

[0163] The function close() to close the driver will release resources and perform cleanup operations to ensure that the hardware device can be safely closed when it is no longer in use.

[0164] When transplanting the program, it is necessary to encapsulate and call these two operations in the function function program file to ensure the normal communication with the target hardware. This is the basic operation for the interaction between the driver and the application program, ensuring that the device driver can be correctly opened, used, and closed when using the driver.

[0165] S1053: Modify the register base address in the function function program file based on the target operating system.

[0166] Specifically, it can be to consult the hardware documentation, identify the register addresses of the target hardware, and modify the original register base address in the function function program file to ensure that they are consistent with the hardware configuration in the current operating system environment.

[0167] S1054: Use the data unit read / write function set to replace the data unit read / write statements in the function function program file, and add the semaphore response connection function to the function function program file to obtain the transplanted function function program file.

[0168] Specifically, it can be to correspondingly replace the original hardware read / write operations in the function function program file with the register read / write function set obtained in the above step 103.

[0169] Meanwhile, add the semaphore response connection function obtained in step S104 to the functional function program file to ensure that the application can normally respond to signals when a hardware event is triggered.

[0170] This method creates a dedicated set of driver read and write operation functions according to the I / O mapping method of the hardware in the driver layer of the target operating system, and constructs a process ID sending function and a semaphore sending function based on the operating system signal processing mechanism, effectively improving the porting efficiency from a single-threaded driver to the target operating system driver. Meanwhile, in the application layer, use the above read and write operation function set to create data unit read and write functional functions, combine these functions with the functional functions in the open-source demo program, replace the original register read and write statements, and add a semaphore response connection function, thereby reducing the code coupling degree and enhancing cross-platform reusability. It not only ensures the stability and compatibility of the ported functional functions in the target operating system environment, simplifies the porting process of complex hardware configurations, greatly promotes the development efficiency, reduces the porting difficulty, but also provides a standardized development process for team collaboration, ensuring the rapid progress of the project and the convenience of long-term maintenance.

[0171] Embodiment 2

[0172] Based on the same inventive concept, an embodiment of the present invention further provides a porting device for an operating system driver. Referring to Figure 2 as shown, the device includes:

[0173] A driver read and write module 101, configured to determine a set of driver read and write operation functions created using input / output control functions according to the I / O mapping method of the target hardware in the driver layer of the target operating system; the set of driver read and write operation functions includes a function for reading a target hardware data unit and a function for writing data to the target hardware data unit;

[0174] A driver signal processing module 102, configured to obtain a driver process ID sending function and a driver semaphore sending function created based on the signal processing mechanism of the target operating system;

[0175] An application read and write module 103, configured to create a set of data unit read and write functional functions based on the set of driver read and write operation functions in the application layer of the target operating system;

[0176] An application signal processing module 104, configured to create a semaphore response connection function based on the driver process ID sending function and the driver semaphore sending function;

[0177] The program transplantation module 105 is used to obtain the functional function program file in the open-source demo program of the target hardware, replace the data unit read / write statements in the functional function program file with the data unit read / write function set, and add the semaphore response connection function to the functional function program file to obtain the transplanted functional function program file.

[0178] Embodiment III

[0179] Based on the same inventive concept, an embodiment of the present invention further provides a computer-readable storage medium, on which a computer program / instructions are stored, and when the computer program / instructions are executed by a processor, the operating system driver transplantation method described in Embodiment I above is implemented.

[0180] Embodiment IV

[0181] Based on the same inventive concept, an embodiment of the present invention further provides a computer program product, including a computer program / instructions, and when the computer program / instructions are executed by a processor, the operating system driver transplantation method described in Embodiment I above is implemented.

[0182] Embodiment V

[0183] Based on the same inventive concept, an embodiment of the present invention further provides a computer device, including a memory, a processor, and a computer program stored on the memory, and when the processor executes the computer program, the operating system driver transplantation method described in Embodiment I above is implemented.

[0184] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, a system, or a computer program product. Therefore, the present invention can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk memories and optical memories, etc.) containing computer-usable program code.

[0185] The present invention is described with reference to the 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 flow and / or block in the flowchart and / or block diagram, and the combination of flows 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 the processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing devices to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing devices generate for implementing in the process Figure 1 one process or multiple processes and / or blocks Figure 1means for the functions specified in one or more boxes.

[0186] These computer program instructions may also be stored in a computer-readable memory that can direct a computer or other programmable data processing apparatus to operate in a particular manner, such that the instructions stored in the computer-readable memory produce a manufacture including an instruction means that implements the functions specified in one Figure 1 or more processes and / or boxes Figure 1 or the functions specified in one or more boxes.

[0187] These computer program instructions may also be loaded onto a computer or other programmable data processing apparatus, such that a series of operational steps are performed on the computer or other programmable apparatus to produce a computer-implemented process, whereby the instructions executed on the computer or other programmable apparatus provide steps for implementing the functions specified in one Figure 1 or more processes and / or boxes Figure 1 or the functions specified in one or more boxes.

[0188] Obviously, those skilled in the art can make various modifications and variations to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalent technologies, the present invention is also intended to include these modifications and variations.

Claims

1. A method for transplanting an operating system driver, characterized in that: include: At the driver layer of the target operating system, according to the I / O mapping mode of the target hardware, a driver read and write operation function set created by using the input and output control function is determined; The driver read and write operation function set includes a function for reading a target hardware data unit and a function for writing data to the target hardware data unit; Acquire a driver process number sending function and a driver semaphore sending function created based on a signal processing mechanism of the target operating system; At the application layer of the target operating system, based on the driver read and write operation function set, a data unit read and write function set is created; Based on the driver process number sending function and the driver semaphore sending function, a semaphore response connection function is created; Obtain the function function program file in the open source demo program of the target hardware, use the data unit read and write function function set to replace the data unit read and write statements in the function function program file, add the semaphore response connection function to the function function program file, and obtain the transplanted function function program file.

2. The method according to claim 1, characterized in that The step of creating a data unit read and write function set based on the drive read and write operation function set includes: Calling a function in the driver read-write operation function set for reading a target hardware data unit to create a data unit read function function; the data unit read function function includes an assignment statement for a value and an address of the data unit; The function used to write data to the target hardware data unit in the driver read-write operation function set is called to create a data unit write function function; the data unit write function function includes an assignment statement of a data unit address and a value.

3. The method according to claim 1, characterized in that The step of obtaining a driver process number sending function and a driver semaphore sending function created based on a signal processing mechanism of the target operating system includes: Obtaining a drive process number sending function created using an input / output control function; the drive process number sending function includes a process number; Based on the signal processing mechanism of the target operating system, determine an open source semaphore sending function; The driving semaphore sending function is constructed based on the process number and the open source semaphore sending function.

4. The method according to claim 1, characterized in that: After obtaining the function program file in the open source demo program of the target hardware, it also includes: Adding a function for opening the driver and a function for closing the driver in the function function program file; The function for opening the driver and the function for closing the driver are called in the function function program file.

5. The method according to claim 1, characterized in that After obtaining the function program file in the open source demo program of the target hardware, it also includes: Based on the target operating system, the register base address in the function program file is modified.

6. The method according to claim 1, characterized in that The target operating systems include Linux, maxOS, and FreeBSD; At the driver layer of the target operating system, determine the driver read and write operation function set created using the input and output control function according to the I / O mapping method of the target hardware, including: At the driver layer of the target operating system, a driver read and write operation function set created using the ioctl function is determined according to the I / O mapping method of the target hardware.

7. An operating system driven transplantation device, characterized in that: include: A driver read / write module is used to determine, at the driver layer of the target operating system, a driver read / write operation function set created using the input / output control function according to the I / O mapping mode of the target hardware; the driver read / write operation function set includes a function for reading a target hardware data unit and a function for writing data to the target hardware data unit; A driving signal processing module, used for obtaining a driving process number sending function and a driving semaphore sending function created based on a signal processing mechanism of the target operating system; An application read-write module, used to create a data unit read-write function set based on the driver read-write operation function set at the application layer of the target operating system; An application signal processing module is used to create a semaphore response connection function based on the driver process number sending function and the driver semaphore sending function; A program transplantation module is used to obtain the function function program file in the open source demo program of the target hardware, use the data unit read and write function function set to replace the data unit read and write statements in the function function program file, add the semaphore response connection function to the function function program file, and obtain the transplanted function function program file.

8. A computer-readable storage medium having a computer program / instruction stored thereon, characterized in that: When the computer program / instruction is executed by a processor, the operating system driven transplantation method described in any one of claims 1 to 6 is implemented.

9. A computer program product comprising a computer program / instructions, characterized in that When the computer program / instruction is executed by a processor, the operating system driven transplantation method described in any one of claims 1 to 6 is implemented.

10. A computer device comprising a memory, a processor and a computer program stored in the memory, characterized in that: The processor executes the computer program to implement the operating system driven transplantation method according to any one of claims 1 to 6.

Citation Information

Patent Citations

  • Application program transplanting method and device

    CN112748940A

  • Virtual device generation method and device, drive program test method and device, equipment and medium

    CN115408007A

  • Block device complex driving method and driving system

    CN117331616A

  • Unsupervised confrontation sample detection method

    CN118537630A

  • Program transplantation method, device and equipment and computer storage medium

    CN118819628A