Serial port data output method and home intelligent gateway

By establishing a communication session in the home smart gateway and using a pseudo-terminal to transmit serial port data, the problem of difficulty in obtaining data caused by the lack of a serial port in the home smart gateway is solved, and efficient fault diagnosis and repair without the need for a physical serial port is achieved.

CN123185240APending Publication Date: 2026-11-27HISENSE BROADBAND MULTIMEDIA TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510696004.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-27
Publication Date
2026-11-27

AI Technical Summary

Technical Problem

The lack of a serial port in home smart gateways makes it difficult to obtain serial data, increasing the difficulty of troubleshooting and maintenance costs.

Method used

By establishing a communication session with the display terminal in the home smart gateway, using a pseudo terminal for data transmission, registering the first console, redirecting serial port data to the pseudo terminal, and outputting it through the network port, data acquisition without a physical serial port can be achieved.

Benefits of technology

It enables remote or local acquisition of serial port data without disassembling the device, improving fault diagnosis efficiency and system stability, and reducing maintenance difficulty and cost.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123185240A_ABST
    Figure CN123185240A_ABST
Patent Text Reader

Abstract

The embodiment of the present disclosure provides a serial port data output method and a home intelligent gateway. The method comprises the following steps: establishing a communication session with a display terminal, the communication session is used for data transmission based on a pseudo terminal; receiving a redirection instruction sent by the display terminal; in response to the redirection instruction, determining the pseudo terminal corresponding to the communication session; registering a first console, the output object of the first console being the pseudo terminal; and after generating serial port data, sending the serial port data to the display terminal through the first console. The embodiment of the present disclosure solves the problem that it is difficult to obtain serial port data when the home intelligent gateway does not have a serial port.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of gateway technology, and in particular to a method for outputting serial port data and a smart home gateway. Background Technology

[0002] In the debugging of embedded systems and network devices, the serial port, i.e., the serial port output data, is an important way to obtain device status and device logs. Especially in terminal devices such as ONU (Optical Network Unit), serial port data can provide real-time feedback on key information such as hardware operating status and protocol stack anomalies, which is the basis for troubleshooting and performance optimization.

[0003] In related technologies, terminal devices are equipped with serial ports, which can be connected to via serial port converters. After the serial port converter is connected to the debugging terminal, the serial port data of the terminal device can be output to the debugging terminal through the serial port for analysis by debugging personnel. However, to save costs, some ONUs are not configured with serial ports, which poses a challenge to obtaining serial port data. Summary of the Invention

[0004] To address the technical problem of difficulty in obtaining serial port data, this embodiment provides a method for outputting serial port data and a home smart gateway.

[0005] In some embodiments, a method for outputting serial port data is provided for a home smart gateway, the method comprising:

[0006] Establish a communication session with the display terminal; the communication session transmits data based on the pseudo-terminal.

[0007] Receive redirection instructions sent by the display terminal;

[0008] In response to a redirection command, determine the pseudo-terminal corresponding to the communication session;

[0009] Register the first console, whose output object is a pseudo-terminal;

[0010] After generating serial port data, the serial port data is sent to the display terminal through the first console.

[0011] One of the above technical solutions has the following advantages or beneficial effects: This embodiment establishes a pseudo-terminal-based communication session between the smart home gateway and the display terminal, enabling data transmission between them via the pseudo-terminal. The smart home gateway determines the pseudo-terminal corresponding to the communication session based on a redirection instruction, and then registers a first console with the pseudo-terminal as the output object. Thus, after generating serial port data, the first console can obtain the serial port data and transmit it to the display terminal via the pseudo-terminal for analysis by technical personnel. Compared with the prior art, this embodiment redirects the output of serial port data to the pseudo-terminal for data transmission, eliminating the need to configure a serial port on the smart home gateway and solving the technical problem of difficulty in obtaining serial port data when there is no serial port.

[0012] In some embodiments, a method for outputting serial port data is provided, which includes registering a first console corresponding to a pseudo-terminal, comprising:

[0013] The kernel module is loaded through the kernel module loading function. The kernel module is used to register the first console corresponding to the pseudo-terminal based on the terminal path.

[0014] One of the above technical solutions has the following advantages or beneficial effects: registering the first console by loading the kernel module through the kernel module loading function realizes modular functional expansion and improves the system's flexibility and maintainability.

[0015] In some embodiments, a method for outputting serial port data is provided, wherein the method for the kernel module to register a first console corresponding to a pseudo-terminal based on the terminal path includes:

[0016] Initialize the first structure, where the type of the first structure is console, and the write function of the first structure is the message write function;

[0017] Based on the terminal path, a pseudo-terminal is opened using the kernel-mode file open function, and a handle to the pseudo-terminal is obtained.

[0018] The output object of the information writing function is set to a pseudo-terminal according to the handle. The information writing function includes a kernel output function.

[0019] The first structure is registered as the first console using the kernel registration function.

[0020] One of the above technical solutions has the following advantages or beneficial effects: By opening a pseudo-terminal through a kernel-mode file opening function, the system kernel can manage the pseudo-terminal. By initializing a first structure of type console and setting the operation parameters of the first structure, including the kernel output function, writing the object to the pseudo-terminal, and registering it through the kernel registration function, the first console can receive kernel-mode serial port data, and then transmit the serial port data to the display terminal through the pseudo-terminal, realizing real-time output of kernel-mode serial port data.

[0021] In some embodiments, a method for outputting serial port data is provided, the method further comprising:

[0022] In response to the redirection command, a pseudo-terminal is opened using the user-mode file open function, and the file descriptor of the pseudo-terminal is obtained;

[0023] Redirect the output of the system console to a pseudo-terminal based on the file descriptor.

[0024] One of the above technical solutions has the following advantages or beneficial effects: by opening a pseudo terminal through a user-mode file open function, the user-mode application process can access the pseudo terminal; by redirecting the output of the system console to the pseudo terminal, the serial port data transmitted by the user-mode application process to the system console can be transmitted to the display terminal in real time through the pseudo terminal.

[0025] In some embodiments, a method for outputting serial port data is provided, which sends serial port data to a display terminal via a first console, including:

[0026] Since the serial port data is kernel-mode data, local interrupts are disabled using the interrupt disable function.

[0027] The serial port data is sent to the display terminal via the first console;

[0028] When the serial data transmission is complete, the local interrupt is restored through the interrupt recovery function.

[0029] One of the above technical solutions has the following advantages or beneficial effects: by disabling local interrupts when transmitting kernel-mode data, the stability and security of data transmission are ensured, and kernel-mode data corruption is avoided from affecting fault analysis; after transmitting kernel-mode data, local interrupts are restored in a timely manner to ensure the real-time performance of subsequent serial port data transmission and to avoid the accumulation of serial port data for too long, which would affect the accuracy of fault analysis.

[0030] In some embodiments, a method for outputting serial port data is provided, which sends serial port data to a display terminal via a first console, including:

[0031] Since the serial port data is kernel-mode data, the address access space of the pseudo-terminal is set as the first address space, which is used to cache kernel-mode data.

[0032] Send serial port data to the first address space;

[0033] Send the cached data in the first address space to the display terminal;

[0034] Restore the pseudo-terminal's address access space to the address space before writing kernel-mode data.

[0035] One of the above technical solutions has the following advantages or beneficial effects: by restricting the address access space of the pseudo terminal to the dedicated space corresponding to the kernel-mode data when transmitting kernel-mode data, cache confusion between kernel-mode data and user-mode data is avoided, ensuring the isolation and security of data transmission; after the address space is restored, the pseudo terminal can continue to process user-mode data normally, improving the overall stability and flexibility of the system.

[0036] In some embodiments, a method for outputting serial port data is provided, which involves opening a pseudo-terminal using a kernel-mode file opening function based on the terminal path to obtain a handle to the pseudo-terminal. This includes: opening the pseudo-terminal in read-write mode and non-blocking mode using a kernel-mode file opening function to obtain a handle to the pseudo-terminal.

[0037] One of the above technical solutions has the following advantages or beneficial effects: by opening the pseudo terminal in read / write mode and non-blocking mode, the pseudo terminal can transmit serial port data in real time, avoiding blocking when data is highly concurrent, and ensuring the accuracy of fault analysis.

[0038] In some embodiments, a method for outputting serial port data is provided to determine the pseudo terminal corresponding to the communication session, including: querying the pseudo terminal corresponding to the communication session through the standard output function of the communication session to obtain the terminal path of the pseudo terminal.

[0039] One of the above technical solutions has the following advantages or beneficial effects: the terminal path of the pseudo terminal can be obtained quickly and accurately through the standard output function of the communication session, ensuring that serial port data can be reliably transmitted to the display terminal.

[0040] In some embodiments, a method for outputting serial port data is provided, the method further comprising:

[0041] Receive the redirection shutdown command sent by the display terminal;

[0042] Send the redirection disable command to the kernel module unload function;

[0043] Unload the kernel module using the kernel module uninstall function to log out of the first console.

[0044] One of the above technical solutions has the following advantages or beneficial effects: by downloading the kernel module in a timely manner through the kernel module uninstallation function, the first console is logged out, ensuring that the serial port data transmission path can be closed in a timely manner when serial port data is not needed, thus ensuring data security.

[0045] In some embodiments, a home smart gateway is provided, the home smart gateway comprising:

[0046] A network interface that enables communication with the display terminal;

[0047] The controller includes:

[0048] Serial port controller, used to output serial port data;

[0049] The central processing unit (CPU) is connected to both a network interface and a serial port controller. It is used to establish a communication session with the display terminal via the network interface; receive redirection commands sent by the display terminal; determine the pseudo terminal corresponding to the communication session in response to the redirection commands; register a first console, whose output object is the pseudo terminal; and send the serial port data output by the serial port controller to the display terminal through the first console.

[0050] One of the above technical solutions has the following advantages or beneficial effects: The smart home gateway of this disclosure establishes a communication session with the display terminal through a network interface, and executes a redirection instruction through the central processing unit to determine the pseudo-terminal corresponding to the communication session. Then, it registers a first console with the pseudo-terminal as the output object, enabling the serial port controller to output serial port data to the first console. The first console then transmits the serial port data to the display terminal through the pseudo-terminal for analysis by technicians. Compared with the prior art, this disclosure, by redirecting the output of serial port data to the pseudo-terminal and using the pseudo-terminal for data transmission, eliminates the need to configure a serial port on the smart home gateway, thus solving the technical problem of difficulty in obtaining serial port data when there is no serial port. Attached Figure Description

[0051] To more clearly illustrate the technical solutions in this disclosure, the accompanying drawings used in some embodiments of this disclosure will be briefly described below. Obviously, the drawings described below are only drawings of some embodiments of this disclosure, and those skilled in the art can obtain other drawings based on these drawings. In addition, the drawings described below can be regarded as schematic diagrams and are not intended to limit the actual size of the product, the actual flow of the method, the actual timing of the signals, etc. involved in the embodiments of this disclosure.

[0052] Figure 1 This is a schematic diagram of an Internet of Things (IoT) scenario provided according to some embodiments of this disclosure;

[0053] Figure 2 This is a schematic diagram illustrating a serial port data output scenario according to some embodiments;

[0054] Figure 3 This is a schematic diagram of the structure of a home smart gateway according to some embodiments;

[0055] Figure 4 This is a flowchart illustrating a method for controlling a home smart gateway to output serial port data according to some embodiments;

[0056] Figure 5 This is a flowchart illustrating a method for outputting serial port data according to some embodiments;

[0057] Figure 6 This is a flowchart illustrating the overall process of enabling and disabling redirection according to some embodiments;

[0058] Figure 7 This is a schematic diagram illustrating the interaction timing between a home smart gateway and a debugging terminal according to some embodiments;

[0059] Figure 8 This is a schematic diagram illustrating the startup of the Telnet service according to some embodiments;

[0060] Figure 9 This is a schematic diagram illustrating the establishment of a Telnet connection according to some embodiments;

[0061] Figure 10 This is a schematic diagram illustrating a successful Telnet connection according to some embodiments;

[0062] Figure 11 This is a schematic diagram of serial port data output according to some embodiments. Detailed Implementation

[0063] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0064] The terminology used in this application is for the purpose of describing particular embodiments only and is not intended to be limiting of the application. The singular forms “a” and “the” as used in this application and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any or all possible combinations of one or more of the associated listed items.

[0065] Figure 1 This is a schematic diagram of an Internet of Things (IoT) scenario provided according to some embodiments of this disclosure. For example... Figure 1 As shown, within a user's home, there are various terminal devices, including a gateway 100, a router 200, a smart set-top box 300, a smart TV 400, a smart device 500, and a control device 600. Among these terminal devices, some are Internet of Things (IoT) devices. For example, except for the control device 600, all the other devices are IoT devices.

[0066] Gateway 100 converts the optical signal in the fiber optic cable into an electrical signal. Router 200 is connected to gateway 100. Router 200 constructs a local area network (LAN) for the user's home based on the electrical signal output by gateway 100. After terminal devices such as smart set-top box 300 and smart TV 400 join the LAN, they can communicate and connect with devices in the wide area network (WAN), including service 700.

[0067] In some embodiments, gateway 100 is a home smart gateway, which integrates the functions of gateway 100 and router 200, and can also provide a control center and plug-ins for users, providing users with a comprehensive and intelligent home network solution.

[0068] It should be noted that in some embodiments, after deploying the home smart gateway, the router 200 can still be deployed to enhance network coverage and signal stability, ensuring that all terminal devices can connect efficiently from different locations and improving user experience. The home smart gateway and router 200 work together to optimize data transmission paths, reduce latency, and ensure smooth network operation.

[0069] In the aforementioned IoT scenarios, the home smart gateway is the core device connecting various IoT devices within a user's home to the wide area network. A malfunction in the home smart gateway will disrupt normal communication among all IoT devices in the user's home, causing the smart home system to fail and impacting the user's living experience. Therefore, the stability and reliability of the home smart gateway are crucial, requiring regular maintenance and upgrades to ensure its efficient operation.

[0070] To ensure the stable operation of home smart gateways, serial port data can be output when a fault occurs. This serial port data includes printed information such as fault codes, device status, and network connection status logs. This facilitates technicians in quickly locating and repairing problems, ensuring the continuity and stability of the smart home system, and improving user satisfaction. However, some home smart gateways lack a serial port, making it difficult to obtain serial port data and increasing the difficulty of troubleshooting.

[0071] It's important to note that the term "home smart gateway" doesn't refer to the absence of an external physical serial port. Therefore, it cannot be directly connected to a serial converter. Internally, however, a serial port module is still present, used for outputting serial data. Technicians can read the serial data from this module using specific tools by disassembling the gateway. However, disassembling the device increases repair difficulty and costs, prolongs repair time, and may even damage the equipment, hindering its long-term stable operation.

[0072] In some embodiments, serial port data includes user-space data. User-space data is temporary or persistent information generated and processed by the application during its operation in user space, typically directly controlled by the business logic written by the developer. This type of data is generated from user-initiated operations or inter-program interactions, and its core function is to enable interaction between business functions and the user, such as converting user requests into service start / stop commands or recording device operation logs for interface display. In the embodiments of this disclosure, after redirection, the interactive commands or parsing results generated by the debugging personnel through the application's active operations are user-space data. For example, when the debugging personnel send a command to the ONU to query the device status, the input command string, the parsed parameters (such as log levels), and the debugging scripts generated by the tool (such as automated test cases) all belong to user-space data. The function of this type of data is to enable functional interaction between the debugging logic and the device, such as configuring the ONU's port rate, triggering diagnostic tests, or extracting operation logs. If the debugging tool redirects the serial port output to a file, the log content written to the file (such as optical module temperature alarms) also belongs to user-space data and is directly read and written by the application.

[0073] In some embodiments, serial port data includes kernel-mode data. Kernel-mode data is low-level system resource and hardware interaction information directly managed by the operating system kernel, typically generated by kernel modules, drivers, or system calls. This type of data is generated from hardware interrupt handling (such as network packet sending and receiving), kernel object operations (such as file descriptor management), or process scheduling (such as pseudo-terminal device read and write). Its core function is to ensure stable system operation and efficient resource scheduling, such as maintaining the terminal session state of processes, managing the I / O buffer of pseudo-terminals, or performing device file operations.

[0074] To address the technical problem of difficulty in obtaining serial port data from home smart gateways, this disclosure provides a method for outputting serial port data. This method redirects serial port data within the home smart gateway, allowing it to output serial port data to other ports, such as network ports. This enables technicians to obtain serial port data through these ports, facilitating remote or local fault diagnosis. Through this redirection, technicians can access device logs in real time, monitor device status, analyze fault causes, and take appropriate measures without disassembling the device, thus achieving efficient fault resolution.

[0075] Figure 2 This is a schematic diagram illustrating a serial port data output scenario according to some embodiments. For example... Figure 2 As shown, after connecting the gateway 100 and the debugging terminal 800 via a network cable, the gateway 100 can be controlled to redirect serial port data to the network port, and the debugging terminal 800 can receive data through the network port to realize remote or local fault diagnosis and improve maintenance efficiency. The debugging terminal 800 can be a computer.

[0076] In some embodiments, the debugging terminal 800 may be a display terminal, capable of displaying the serial port data sent by the gateway 100 in real time for technical personnel to analyze.

[0077] In some embodiments, redirection can be flexibly configured to support multiple data formats, ensuring data integrity and security, and further improving the accuracy and efficiency of fault diagnosis. For example, redirection can be to a Telnet (remote login protocol) port, i.e., TCP port 23, allowing technicians to remotely access the device and read serial port data via the Telnet protocol. Another example is redirection to an SSH (Secure Shell) port, i.e., TCP port 22, allowing technicians to remotely access the device and read serial port data via the SSH protocol. Both the Telnet and SSH ports are logical network ports, and data from these logical network ports can be transmitted via physical network cable ports (such as Ethernet ports) or wireless networks. After reading serial port data through the Telnet or SSH port, efficient remote diagnosis of the home smart gateway can be performed based on the serial port data.

[0078] Figure 3 This is a schematic diagram of a home smart gateway according to some embodiments. Figure 3 As shown, a home smart gateway may include a controller 110 and a network interface 120.

[0079] The network interface 120 is capable of communicating with the display terminal. For example, the network interface 120 can communicate with the display terminal via a network cable.

[0080] The controller 110 includes a serial port controller 111 and a central processing unit 112. The serial port controller 110 includes a serial port module, which is used by the gateway 100 to output serial port data. If the home smart gateway has a physical serial port, the serial port data from the serial port controller 110 will be output to the physical serial port. If the home smart gateway does not have a physical serial port, the serial port data from the serial port controller 110 will be either stored in a buffer or discarded by default.

[0081] In some embodiments, to ensure successful redirection, the home smart gateway needs to be pre-configured to set redirection rules and target ports, ensuring the stability and security of data transmission. In this way, even if the home smart gateway has no external serial port, it can efficiently acquire real-time logs, ensuring the reliable operation of the home smart gateway.

[0082] Taking the output of serial port data via the Telnet protocol as an example, the pre-configuration of the home smart gateway includes the following: 1. Add a target application for managing and configuring redirection. For example, the name of the target application can be serialToTelnet, which means serial to Telnet (remote login protocol); 2. Add a kernel module to support the serial port data redirection function. For example, the name of the kernel module can be ptyconsole.ko, which means that the kernel module is a console based on a pseudo terminal.

[0083] The target application receives the first command and the second command. The first command includes an enable parameter, such as "on" (enable), to enable redirection, and the second command includes a disable parameter, such as "off" (disable), to disable redirection.

[0084] The target application is configured to perform the following operations to enable redirection upon receiving the first command: redirect user-mode console output to the current terminal, i.e., send user-mode serial data destined for the system console to the current terminal; load the kernel module to synchronously redirect kernel-mode output to the current terminal, i.e., send kernel-mode serial data destined for the system console to the current terminal. Here, the current terminal refers to the output terminal of the Telnet session on the local end. This output terminal is not a physical terminal but a virtual terminal, called a pseudo-terminal. The enable parameter is passed to the kernel module for it to execute the corresponding steps to enable redirection. When redirection needs to be disabled, the disable parameter is passed to the kernel module for it to execute the corresponding steps to disable redirection.

[0085] In user space, the steps for redirecting serial port data include:

[0086] The pseudo-terminal can be identified using the Linux C function `ttyname(STDOUT_FILENO)`. `ttyname(STDOUT_FILENO)` is a standard output function in Linux C, used to obtain the ttyname of the terminal currently in standard output. This current standard output terminal, or the current terminal, is the output terminal of the Telnet session on the local end. In other words, the smart home gateway transmits data to this current terminal during the Telnet session, and then the data is passed from the current terminal to the remote access end of the Telnet session, i.e., the debugging terminal 800. `ttyname` is the pathname of the pseudo-terminal, representing its path, such as ` / dev / pts / 0`. This ttyname allows access to the output terminal.

[0087] Based on the opening parameters, the Linux C function `open(ttyname, O_RONLY|O_WONLY|O_NONBLOCK)` opens the terminal for the current standard output in read / write and non-blocking modes. Specifically, `open(ttyname, O_RONLY|O_WONLY|O_NONBLOCK)` is a user-space file opening function that opens the current terminal in user space, providing the file descriptor for the pseudo-terminal. After the terminal is opened in read / write and non-blocking modes, user-space applications can write user-space serial port data to the corresponding data buffer of the terminal. The data in this buffer will then be transmitted to the debug terminal 800 for display.

[0088] The Linux C function `ioctl(tty, TIOCCONS)` redirects the system console output to the current standard output terminal based on the file descriptor mentioned above. `TIOCCONS` is an ioctl opcode that primarily redirects the system console output to the current terminal. The system console is the default output location for serial port data from the smart home gateway, used to output serial data to a preset storage space, such as a buffer, or to a physical serial port (for gateways with physical serial ports).

[0089] Based on the shutdown parameters, the system console is opened using the Linux C function `open(" / dev / console", O_RONLY|O_WONLY|O_NONBLOCK)` in a non-blocking read / write mode with the three flags enabled.

[0090] By using the Linux C function ioctl(tty, TIOCCONS), the data is redirected to the system console, so that subsequent serial port data is no longer transmitted to the pseudo terminal, but to the system console, thus disabling the redirection.

[0091] In kernel mode, executing the first command `insmod ptyconsole.ko` (loads the kernel module) makes the kernel module `ptyconsole.ko` effective, and kernel output is synchronized to debug terminal 800. Executing the second command `rmmod ptyconsole le` (removes the kernel module) stops kernel output synchronization to debug terminal 800. The `module_init` and `module_exit` functions are Linux kernel functions that specify the execution functions for loading and exiting. Therefore, a kernel module can be loaded by defining an operation function `init_ptyconsole` in the `module_init` function; this function is called the kernel module loading function. Similarly, a kernel module can be unloaded by defining an operation function `cleanup_ptyconsole` in the `module_exit` function; this function is called the kernel module unloading function.

[0092] In some embodiments, after obtaining the ttyname of the terminal currently displaying standard output using the Linux C function ttyname(STDOUT_FILENO), this ttyname is used as a parameter to load ptyconsole.ko. The corresponding command is: insmod ptyc onsole.ko ptyconsole=ttyname.

[0093] In some embodiments, ptyconsole.ko performs redirection in the following ways:

[0094] Define a kernel structure of type `console`, which is a structure type in the Linux kernel. The definition of this kernel structure includes: defining the console name, console flags, and the function to write to the console.

[0095] In some embodiments, the definition of a kernel structure also includes a descriptive text defining the kernel structure: pty_console_desc, which specifies a console description, in which a pty_file handle is defined, through which write operations can be performed on the terminal currently using standard output.

[0096] In some embodiments, the kernel structure is configured as follows:

[0097] The `open_ptyfile` function (a file extension function) opens the current standard output ttyname. The Linux kernel function `filp_open(ttyname, O_RDWR|O_NONBLOCK)` then opens the terminal for the current standard output with read / write and non-blocking flags. `filp_open` is a kernel-mode file opening function; using this function to open a pseudo-terminal allows kernel-mode processes or applications to output serial port data to the pseudo-terminal.

[0098] The console opened above is registered using the Linux kernel function register_console.

[0099] In some embodiments, the kernel structure write function includes a message write function, write_msg, which is configured to perform the following process:

[0100] Check if the pty_file handle correctly opens the current standard output terminal. If it can be opened, continue the subsequent process; otherwise, report an error.

[0101] The Linux kernel function `local_irq_save` saves the current interrupt to a flag and disables local interrupts to prevent interruption during critical code segments (during file writing), ensuring the atomicity of the operation. `local_irq_save` is the interrupt disabling function. This function first interrupts the current serial data transmission process, saves the interrupt flag to determine the interrupt location, and then disables local interrupts, preventing subsequent serial data transmission from being interrupted. Since both user-mode and kernel-mode serial data are transmitted via pseudo-terminals in this embodiment, enabling interrupts before transmitting kernel-mode serial data prevents user-mode serial data from interfering with kernel-mode serial data transmission.

[0102] The current address space limit (fs value) is obtained using the Linux kernel function `get_fs()`; the pseudo-terminal's address access space is restricted to the kernel data segment (KERNEL_DS) using the Linux kernel function `set_fs(KERNEL_DS)`. Since file operations (such as reading and writing) are restricted by address space by default in the Linux kernel, this address space restriction prevents kernel code from accessing user space addresses, ensuring data security. Setting the address space to `KERNEL_DS` allows kernel code to access the kernel address space, i.e., the first address space, thus enabling safe file write operations. Data cached in the first address space will be sent to the display terminal for display.

[0103] Executing `loff_t pos = np_pty_file->f_pos` retrieves the current offset (f_pos) of the file. The current offset specifies the starting position for writing, and `pty_file` is the handle to the current standard output terminal.

[0104] The kernel function `kernel_write` is called to write serial port data to the pseudo-terminal. The parameters of `kernel_write` include `pty_file`, `msg`, `len`, and `&pos`. `pty_file` specifies the serial port data to be written to the pseudo-terminal; `msg` is the data buffer to be written; `len` is the length of the data; and `&pos` is the starting position for writing, which is updated by `kernel_write` during the writing process.

[0105] The function `np_pty_file->f_pos = pos` is executed to update the offset of the pseudo-terminal. `f_pos` represents the position after the write operation. Since the `kernel_write` function can update `pos` but not `f_pos`, `f_pos` needs to be updated after the `kernel_write` function writes the data.

[0106] The set_fs(old_fs) function restores the previous address space limit (fs value) to ensure that subsequent kernel operations are not affected.

[0107] The local_irq_restore(flags) function, also known as the interrupt recovery function, restores the previously saved interrupt state, ensuring that the interrupt is restored to its original state and avoiding impact on the normal operation of the system.

[0108] In some embodiments, the process of a kernel structure unloading a kernel module based on shutdown parameters includes:

[0109] The console structure ptyconsole is unregistered using the kernel function unregister_console(ptyconsole).

[0110] The `close_ptyfile` function checks if `np_pty_file` is empty. If it is not empty, the Linux kernel function `filp_close` closes the file handle.

[0111] In some embodiments, based on the above pre-configuration, the method for controlling the output serial port data of the home smart gateway can be found in [reference needed]. Figure 4 This includes the following steps:

[0112] Step S110: Connect the display terminal to the home smart gateway.

[0113] In some embodiments, the display terminal can be connected to the home smart gateway via a network cable, enabling the display terminal and the home smart gateway to communicate within the same local area network.

[0114] Step S120: Enable the Telnet service of the home smart gateway on the display terminal.

[0115] In some embodiments, to ensure stable transmission and storage of serial port data during normal use of the home smart gateway, the Telnet service of the home smart gateway is disabled by default. When debugging the home smart gateway, the Telnet service must be manually enabled.

[0116] In some embodiments, the smart home gateway embeds a web server, such as BusyBox httpd, to handle HTTP requests. After connecting the display terminal to the smart home gateway, the Telnet service of the smart home gateway can be enabled by accessing a preset web page on the display terminal. After being redirected to the URL of the web page on the display terminal, the web page sends a request to the web server of the smart home gateway to enable the Telnet service. This request can be a request to access the Telnetenable.cgi script located in the / cgi-bin / directory of the smart home gateway. This request also carries control parameters, such as Telnetenable=1. The Telnetenable.cgi script is pre-configured to enable the Telnet service based on these control parameters. Therefore, after receiving the request, the web server maps the request path to the corresponding CGI script and executes the script. The script parses the parameter Telnetenable=1 in the URL, determines that the user's intent is "enable Telnet," and then starts the Telnet service by calling underlying system commands, such as Linux's systemctl, or by directly manipulating the service configuration file.

[0117] In some embodiments, the home smart gateway has Telnet service enabled by default, or if Telnet service has been enabled in other ways, this step can be skipped.

[0118] Step S130: Log in to the Telnet service of the home smart gateway on the display terminal.

[0119] In some embodiments, after enabling the Telnet service of the home smart gateway, a command-line interface can be accessed on the display terminal using tools such as cmd, SecureCRT / XsHelp, etc. Enter the login command in the command-line interface and press Enter to log in to the home smart gateway's Telnet service. The login command can be: `Telnet gatewayIP`, where the gateway IP is the local area network address of the home smart gateway. The home smart gateway, based on this login command, returns a login prompt message, prompting for a username and password. After entering the correct username and password and pressing Enter, a login request is sent to the home smart gateway. The home smart gateway verifies the username and password based on the login request. Upon successful verification, a Telnet session is established with the display terminal.

[0120] In some embodiments, after successful verification, the home smart gateway also returns information about the current Busybox version and the version of the OpenWrt used, allowing debugging personnel to quickly understand the system environment of the home smart gateway and facilitate subsequent operations.

[0121] Step S140: Input a redirection command to the home smart gateway on the display terminal.

[0122] In some embodiments, the redirection command is used to control the home smart gateway to redirect serial port data to a pseudo-terminal of the Telnet session, so that the display terminal can obtain serial port data from the pseudo-terminal in real time via the Telnet protocol. Depending on the pre-configuration of the home smart gateway, the redirection command may be: serialToTelnet on.

[0123] After establishing a Telnet session with the home smart gateway, the redirection command described above can be entered in the command-line interface of the display terminal. This redirection command is transmitted to the home smart gateway through the Telnet session.

[0124] In some embodiments, the home smart gateway redirects serial port data to a Telnet session according to the above redirection instructions, enabling real-time transmission of serial port data to the command-line interface for analysis by debugging personnel.

[0125] To explain the redirection of serial port data, the following section describes the method of outputting serial port data from the perspective of a home smart gateway.

[0126] Figure 5 This is a flowchart illustrating a method for outputting serial port data according to some embodiments. For example... Figure 5 As shown, the method includes the following steps:

[0127] Step S210: Establish a communication session with the display terminal. The communication session transmits data based on the pseudo-terminal.

[0128] In some embodiments, the Telnet service of the home smart gateway is disabled by default. When debugging the home smart gateway, the Telnet service must be manually enabled. After enabling the Telnet service, a Telnet session can be established between the home smart gateway and the display terminal.

[0129] In some embodiments, the Telnet service is enabled by default on the home smart gateway, or the Telnet service has been enabled in other ways, and a Telnet session can be established between the home smart gateway and the display terminal.

[0130] In some embodiments, an SSH session with the display terminal may also be established.

[0131] Step S220: Receive the redirection instruction sent by the display terminal.

[0132] In some embodiments, after the home smart gateway establishes a Telnet session with the display terminal, it can receive redirection instructions sent by the display terminal through the Telnet session.

[0133] Step S230: In response to the redirection instruction, determine the pseudo terminal corresponding to the communication session.

[0134] In some embodiments, the home smart gateway can obtain the ttyname of the terminal currently in standard output through the Linux C function ttyname(STD OUT_FILENO) according to the redirection instruction. This ttyname corresponds to the pseudo terminal of the Telnet session.

[0135] In some embodiments, according to pre-configuration, the home smart gateway can launch the target application, namely se rialToTelnet, based on a redirection instruction. After launching, the target application obtains the ttyname of the terminal currently using standard output through the Linux C function ttyname(STDOUT_FILENO).

[0136] Step S240: Register the first console, whose output object is a pseudo terminal.

[0137] In some embodiments, to enable kernel-mode serial port data to be transmitted to a pseudo-terminal, the pseudo-terminal can be registered as a console using the `register_console` function in the Linux kernel. To distinguish it from the original console in the Linux kernel of the home smart gateway, i.e., the system console, the console registered by the pseudo-terminal is designated as the first console.

[0138] The console is a key component in the Linux kernel used for outputting logs and managing information. After generating kernel-mode serial port data, the Linux kernel will traverse all consoles and send the data to each console.

[0139] In some embodiments, a target application is configured within the home smart gateway. Based on a redirection instruction, the target application loads a kernel module, which then registers the pseudo-terminal as a console.

[0140] The target application loads the kernel module, ptyconsole.ko, based on the ttyname and on parameters. The corresponding commands are: insmod ptyconsole.ko ptyconsole=ttyname.

[0141] The following operations are performed automatically in the kernel module:

[0142] This opens a pseudo-terminal in both non-blocking and read / write modes, obtaining the corresponding file handle. The pseudo-terminal can be opened using the custom function `open_ptyfile`. `open_ptyfile` calls the Linux kernel function `filp_open`, which takes the following parameters: `ttyname`, `O_RDWR` (indicating read / write mode) and `O_NONBLOCK` (indicating non-blocking mode). `filp_open` opens the current standard output terminal with both read / write and non-blocking flags enabled. The return value of `filp_open` is the pseudo-terminal handle, `pty_file`, which is used by the system kernel to access the pseudo-terminal.

[0143] A console is initialized using the `module_init` function. The `module_init` function then initializes the console using the `init_ptyconsole` function. Initialization includes initializing a kernel structure, called the first structure. This kernel structure, after initialization and registration, becomes the required console. Then, the console name, console flags, and console write functions are set. The console name is `ptycon`, the console flags are `CON_ENABLED`|`CON_PRINTBUFFER`, where `CON_ENABLED` indicates the console is enabled, and `CON_PRINTBUFFER` indicates that the contents of the data buffer can be printed. The write function includes the `write` function, which can be the `write_msg` function (message write function), setting the output object of the message write function to the pseudo-terminal. The `write_msg` function can write kernel-mode serial port data to the pseudo-terminal by calling the `kernel_write` function in the kernel. The handle of the `kernel_write` function is the aforementioned `pty_file`.

[0144] Register the pseudo-terminal as a console. The `register_console` function in the Linux kernel can be used to register the console opened above as a console; this console is called the first console.

[0145] In some embodiments, to enable user-mode serial port data to be transmitted to the display terminal, the target application also redirects user-mode serial port data to a pseudo-terminal. After determining the ttyname of the current standard output terminal, the current standard output terminal can be opened using the Linux C function `open(ttyname, O_RDWR|O_WONLY|O_NONBLOCK)` with three non-blocking read / write flags. Then, the Linux C function `ioctl(tty, TIOCCONS)` redirects the data to the current standard output terminal. Here, `tty` corresponds to the terminal device, and `TIOCCONS` is an ioctl opcode in the kernel indicating redirection. Through the operation corresponding to `TIOCCONS`, user-mode serial port data input to the console can be redirected to the specified terminal device, i.e., the pseudo-terminal.

[0146] Step S250: After generating serial port data, send the serial port data to the display terminal through the first console.

[0147] In some embodiments, after generating kernel-mode serial port data, the system kernel of the smart home gateway can traverse all consoles and send the data to each console respectively. The serial port data transmitted to the first console can be transmitted to a display terminal via a Telnet session for remote debugging of the smart home gateway.

[0148] As can be seen from the above embodiments, this disclosure provides a solution to the problem of remote debugging and improves the efficiency of gateway maintenance by using a pseudo-terminal to redirect serial port data for home smart gateways that do not have a serial port.

[0149] In some embodiments, if it is necessary to terminate the transmission of serial data after transmitting some serial data, the serial data redirection function can be turned off to restore the default storage method of serial data and ensure the data security of serial data.

[0150] The entire process of enabling and disabling redirection can be found in [link / reference]. Figure 6 This is a schematic diagram illustrating the overall process of enabling and disabling redirection according to some embodiments. For example... Figure 6 As shown, enabling and disabling redirection includes both user-mode and kernel-mode operations.

[0151] During the redirection process, in user space, the pseudo-terminal is determined using the `ttyname` function, opened using the `open` function, and redirected using the `ioctl` function. By loading the kernel module and passing the pseudo-terminal parameter (ttyname), the kernel module initiates kernel-mode serial port data redirection. In kernel space, the first structure is initialized, then the pseudo-terminal is opened using the `filp_open` function, the pseudo-terminal's write function is set, and finally, the first console is registered using the `register` function.

[0152] During the process of disabling redirection, in user space, the system console is opened using the `open` function, and redirection is performed using the `ioctl` function. In kernel space, the first console is unregistered using the `unregister` function, preventing kernel-mode serial data from being sent to the first console; the pseudo-terminal is closed using the `filp_close` function, controlling the pseudo-terminal to stop transmitting kernel-mode serial data.

[0153] To further explain the serial port data output method of the home smart gateway, the following is an introduction with reference to the interaction timing diagram between the home smart gateway and the display terminal.

[0154] Figure 7 This is a schematic diagram illustrating the interaction timing between a home smart gateway and a display terminal according to some embodiments. Figure 7 In this context, the display terminal can be a computer, such as a laptop used by the debugging personnel.

[0155] like Figure 7 As shown, after a user accesses the target website on the display terminal, the display terminal sends an access request for the target website to the home smart gateway. The home smart gateway parses this access request, obtains the parsing result, and starts the Telnet service based on the parsing result. After starting the Telnet service, the home smart gateway returns the page data of the target website to the display terminal. See also... Figure 8 This page may contain the following information: Telnet enabled. This message indicates that the home smart gateway has successfully enabled the Telnet server.

[0156] In some embodiments, assuming the Telnet service is enabled on the home smart gateway, the user accesses a command-line interface on the display terminal and sends a Telnet connection request to the home smart gateway through this interface. See also Figure 9 The connection request can be: the Telnet gateway URL.

[0157] In some embodiments, the home smart gateway returns a prompt for login information to the display terminal upon receiving a Telnet connection request. This prompt may include username and password input suggestions. After the user enters the correct login information on the display terminal, the display terminal transmits the login information to the home smart gateway. The home smart gateway verifies the login information and then establishes a Telnet connection with the display terminal.

[0158] In some embodiments, see Figure 10 After verifying the login information, the home smart gateway also returns system version information, such as Busybox and OpenWRT version information, to the display terminal. The display terminal receives this information via a Telnet session, allowing technicians to understand the gateway's system status. The display terminal then shows this system version information in its command-line interface.

[0159] In some embodiments, after a user enters a redirection command in the command-line interface of the display terminal, the display terminal sends the redirection command to the home smart gateway.

[0160] In some embodiments, the home smart gateway redirects the system console to a pseudo-terminal according to a redirection instruction, enabling user-mode serial port data to be transmitted to the pseudo-terminal. The kernel module is also loaded according to the redirection instruction, allowing kernel-mode serial port data to also be transmitted to the pseudo-terminal.

[0161] In some embodiments, after a user inputs a debugging operation on the home smart gateway, the gateway executes the corresponding debugging command and generates serial port data, such as debugging logs. This serial port data is transmitted to a display terminal via a pseudo-terminal, where the debugging logs are displayed in real time, facilitating monitoring and analysis by debugging personnel.

[0162] In some embodiments, during the operation of the home smart gateway, even if the user does not input any debugging operations, the home smart gateway may still generate some serial port data, such as service crash logs. This serial port data is also transmitted to the display terminal through a pseudo-terminal, and the display terminal displays it in real time, helping debugging personnel to promptly identify and handle potential problems and ensure the stable operation of the home smart gateway.

[0163] Figure 11 This is a schematic diagram illustrating the real-time output of serial port data according to some embodiments. For example... Figure 11 As shown, serial port data can include information output by ioctl TIOCCONS.

[0164] In some embodiments, if it is no longer necessary to acquire serial port data from the smart home gateway, a redirection shutdown command can be entered in the command-line interface. The display terminal transmits this command to the smart home gateway. After receiving the command, the smart home gateway redirects the output of the system console to a default location, such as the system console, and uninstalls the kernel module through the kernel module uninstallation function to close the first console, so that the first console no longer outputs serial port data. If the smart home gateway generates new serial port data, it will transmit it to the default location.

[0165] Other embodiments of this application will readily occur to those skilled in the art upon consideration of the specification and practice of the disclosure herein. This application is intended to cover any variations, uses, or adaptations of the invention that follow the general principles of this application and include common knowledge or customary techniques in the art not disclosed herein. The specification and examples are to be considered exemplary only, and the true scope and spirit of this application are indicated by the claims. The above embodiments of this application do not constitute a limitation on the scope of protection of this application.

Claims

1. A serial port data output method, characterized in that, For home smart gateways, including: Establish a communication session with the display terminal, wherein the communication session transmits data based on a pseudo-terminal; Receive the redirection command sent by the display terminal; In response to the redirection instruction, determine the pseudo terminal corresponding to the communication session; Register a first console, whose output object is the pseudo-terminal; After generating serial port data, the serial port data is sent to the display terminal through the first console.

2. The serial port data output method according to claim 1, characterized in that, The registration of the first console corresponding to the pseudo-terminal includes: The kernel module is loaded through a kernel module loading function. The kernel module is used to register a first console corresponding to the pseudo terminal based on the terminal path.

3. The serial port data output method according to claim 2, characterized in that, The method by which the kernel module registers a first console corresponding to the pseudo-terminal based on the terminal path includes: Initialize the first structure, wherein the type of the first structure is console, and the write function of the first structure is the information write function; Based on the terminal path, the pseudo-terminal is opened using a kernel-mode file open function to obtain the handle of the pseudo-terminal; The output object of the information writing function is set to the pseudo-terminal according to the handle, and the information writing function includes a kernel output function; The first structure is registered as the first console using the kernel registration function.

4. The serial port data output method according to claim 1, characterized in that, The method further includes: In response to the redirection instruction, the pseudo-terminal is opened using a user-mode file open function, and the file descriptor of the pseudo-terminal is obtained. Based on the file descriptor, the output of the system console is redirected to the pseudo-terminal.

5. The serial port data output method according to claim 1, characterized in that, Sending the serial port data to the display terminal via the first console includes: Since the serial port data is kernel-mode data, local interrupts are disabled using the interrupt disable function. The serial port data is sent to the display terminal via the first console. When the serial port data transmission is complete, the local interrupt is restored through the interrupt recovery function.

6. The serial port data output method according to claim 1, characterized in that, Sending the serial port data to the display terminal via the first console includes: Since the serial port data is kernel-mode data, the address access space of the pseudo-terminal is set as the first address space, which is used to cache the kernel-mode data. Send the serial port data to the first address space; Send the data cached in the first address space to the display terminal; The address access space of the pseudo-terminal is restored to the address space before the kernel-mode data was written.

7. The serial port data output method according to claim 3, characterized in that, The step of opening the pseudo-terminal using a kernel-mode file opening function according to the terminal path to obtain the handle of the pseudo-terminal includes: opening the pseudo-terminal in read-write mode and non-blocking mode using a kernel-mode file opening function to obtain the handle of the pseudo-terminal.

8. The serial port data output method according to claim 1, characterized in that, Determining the pseudo terminal corresponding to the communication session includes: querying the pseudo terminal corresponding to the communication session through the standard output function of the communication session to obtain the terminal path of the pseudo terminal.

9. The serial port data output method according to claim 2, characterized in that, The method further includes: Receive the redirection shutdown command sent by the display terminal; Send the redirection disable instruction to the kernel module unload function; The kernel module is uninstalled using the kernel module uninstallation function to log out of the first console.

10. A home smart gateway, characterized in that, include: A network interface that enables communication with the display terminal; The controller includes: Serial port controller, used to output serial port data; The central processing unit (CPU) is connected to both the network interface and the serial port controller. It is used to establish a communication session with the display terminal via the network interface; receive a redirection command sent by the display terminal; determine the pseudo-terminal corresponding to the communication session in response to the redirection command; register a first console, whose output object is the pseudo-terminal; and send serial port data output by the serial port controller to the display terminal via the first console.