USB connection method and device, electronic device and storage medium

By enumerating and switching USB devices to NCM mode, IP-based transmission channels are built, which solves the problems of insufficient flexibility and high cost of USB connection methods in the prior art, improves development efficiency and reduces costs.

CN113297123BActive Publication Date: 2024-12-31GUANGDONG OPPO MOBILE TELECOMMUNICATIONS CORP LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202110564993.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-05-24
Publication Date
2024-12-31
Estimated Expiration
2041-05-24

AI Technical Summary

Technical Problem

The existing USB host and USB device connection method, especially in the vehicle field, has problems of insufficient flexibility and high cost. For example, Google's AOA protocol cannot support WebSocket, and Apple's EAP protocol requires MFI authentication and encryption chip integration, which adds The cost of software and hardware.

Method used

By enumerating the target USB device when detecting the USB connection request, re-enuming it as an NCM device, and switching to NCM mode using the preset network connection state machine, sending a switching command to switch the device to NCM mode, thereby building an IP-based transmission channel , supports standard Android API data transmission, reducing dependence on special drivers.

Benefits of technology

It improves the development efficiency of upper-level applications, supports more network libraries, reduces the cost of software and hardware, and solves the problem that the Google AOA protocol cannot establish WebSocket in the connection between the car and mobile terminals, and does not need to add encryption chips.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113297123B_ABST
    Figure CN113297123B_ABST
Patent Text Reader

Abstract

The present application discloses a USB connection method for a USB host, comprising: when the USB host detects a USB connection request, enumerates to determine the target USB device, re-enumerates the target USB device as a target NCM device, and uses a preset network connection state machine to switch the current mode to the NCM mode and configure the target NCM device, and sends a switching command to the target USB device so that the target USB device switches to the NCM mode. The USB connection method of the embodiment of the present application can build an IP-based transmission channel by switching to the NCM mode, adding a specific network type ‑NCM to the USB connection, so that the upper-layer application or other services can use the standard Android API to transmit data in the NCM mode. The present application also discloses a USB connection method for a USB device, a USB connection device, an electronic device, and a storage medium.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of communication technology, and in particular to a USB connection method, a USB connection device, an electronic device and a storage medium for a USB host. Background Art

[0002] For USB hosts and USB devices connected via USB, data transmission is usually carried out using USB protocols or USB modes. With the development of intelligent services, the services of upper-layer applications are becoming more and more diverse. For some special applications, it is often necessary to install special drivers on the USB host and it takes a long time, resulting in a poor user experience.

[0003] Especially in the field of vehicles, when a mobile phone is connected to a vehicle via USB, there are two main ways to connect the existing mobile phone and the vehicle. One is Google's AOA. The AOA protocol only supports a simple data receiving and sending interface, but does not support the existing network library at the application layer. If the developer wants to establish a WebSocket on the mobile phone and the vehicle based on business needs, AOA is not supported in this case, resulting in the AOA protocol being not flexible enough for developers. The other is Apple's EAP protocol. Currently, to use the EAP protocol, you must do MFI certification. First, apply for qualification certification on the MFI official website. Secondly, you need to purchase Apple's encryption chip and integrate it into the vehicle, which increases the cost of software and hardware. At present, some technologies enumerate USB connections as virtual network cards, but there is no specific implementation method, or the enumeration process is more complicated, usually after two or more enumerations, and the implementation method is more complicated. Summary of the invention

[0004] In view of this, embodiments of the present application provide a USB connection method for a USB host, a USB connection method for a USB device, a USB connection device, an electronic device, and a storage medium.

[0005] The present application provides a USB connection method for a USB host, comprising:

[0006] When a USB connection request is detected, enumerate to determine the target USB device;

[0007] Re-enumerate the target USB device as a target NCM device, and use a preset network connection state machine to switch the current mode to the NCM mode and configure the target NCM device;

[0008] A switching command is sent to the target USB device so that the target USB device switches to the NCM mode.

[0009] The present application also provides a USB connection method for a USB device, comprising:

[0010] Receive the switching command sent by the USB host;

[0011] Switching the current mode to the NCM mode according to the switching command;

[0012] A second device state configuration and a second network connection configuration are performed using the network connection state machine, and a connection is established with the host USB device according to the second device state configuration and the second network connection configuration.

[0013] The present application also provides a USB connection device for a USB host, comprising:

[0014] An enumeration module, used for enumerating to determine a target USB device when a USB connection request is detected;

[0015] A re-enumeration module, used for re-enumerating the target USB device as a target NCM device, switching the current mode to the NCM mode using a preset network connection state machine, and configuring the target NCM device;

[0016] The sending module is used to send a switching command to the target USB device so that the target USB device switches to the NCM mode.

[0017] The present application also provides a USB connection device for a USB device, comprising:

[0018] A receiving module, used for receiving a switching command sent by a USB host;

[0019] A switching module, configured to switch the current mode to the NCM mode according to the switching command;

[0020] The configuration module is used to perform a second device state configuration and a second network connection configuration using the network connection state machine, and establish a connection with the host USB device according to the second device state configuration and the second network connection configuration.

[0021] The present application also provides an electronic device, characterized in that the electronic device includes a memory and a processor, the memory stores a computer program, and when the computer program is executed by the processor, the USB connection method of the above embodiment is implemented.

[0022] The present application also provides a non-volatile computer-readable storage medium of a computer program, and when the computer program is executed by one or more processors, the USB connection method of the above embodiment is implemented.

[0023] In this way, the USB connection method, connection device, and electronic device of the embodiment of the present application are enumerated to determine the target USB device when a USB connection request is detected, and the target USB device is re-enumerated as a target NCM device, and the current mode is switched to the NCM mode and the target NCM device is configured using a preset network connection state machine, and a switching command is sent to the target USB device so that the target USB device switches to the NCM mode. After the USB device is connected to the USB host, a transmission channel based on IP can be constructed by switching to the NCM mode, and a specific network type-NCM is added to the USB connection, so that the upper-layer application or other services can use the standard Android API to transmit data in the NCM mode. Compared with the USB mode, using a standard network card to transmit data for the upper-layer application, on the one hand, there is no need to add special drivers, and on the other hand, the network library of the supported business is more, thereby effectively improving the development efficiency of the upper-layer application. Secondly, when the USB device is successfully connected to the NCM mode of the USB host, for other upper-layer applications, network services such as webSocket and other network data transmission can be established through the IP address and port of the network card. It solves the problem that the Google AOA protocol cannot establish Websocket in the current connection method between the vehicle computer and the mobile terminal, and supports the existing network library of the application layer. In addition, there is no need to add an additional encryption chip to be integrated in the vehicle computer, which effectively reduces the cost of software and hardware. BRIEF DESCRIPTION OF THE DRAWINGS

[0024] The above and / or additional aspects and advantages of the present application will become obvious and easily understood from the description of the embodiments in conjunction with the following drawings.

[0025] Figure 1 It is a schematic diagram of a USB connection method flow chart of certain implementation modes of the present application;

[0026] Figure 2 is a module diagram of a USB connection device in some embodiments of the present application;

[0027] Figure 3 It is a schematic diagram of a USB connection method flow chart of certain implementation modes of the present application;

[0028] Figure 4 It is a schematic diagram of a USB connection method flow chart of certain implementation modes of the present application;

[0029] Figure 5 is a schematic diagram of a network state machine flow chart of certain implementation modes of the present application;

[0030] Figure 6 It is a schematic diagram of a USB connection method flow chart of certain implementation modes of the present application;

[0031] Figure 7 is a module diagram of a USB connection device in some embodiments of the present application;

[0032] Figure 8 It is a schematic diagram of a USB connection method flow chart of certain implementation modes of the present application;

[0033] Fig. 9 It is a schematic diagram of a USB connection method flow chart of certain implementation modes of the present application;

[0034] Fig.10 is a module diagram of a USB connection device in some embodiments of the present application;

[0035] Fig.11 It is a schematic diagram of a USB connection method flow chart of certain implementation modes of the present application;

[0036] Fig.12 is a module diagram of a USB connection device in some embodiments of the present application;

[0037] Fig.13 It is a flowchart of the USB connection method of certain embodiments of the present application. Specific implementation methods

[0038] Embodiments of the present application are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present application, and should not be construed as limiting the present application.

[0039] See also Figure 1 , the present application provides a USB connection method for a USB host, comprising:

[0040] S10: enumerate to determine the target USB device when a USB connection request is detected;

[0041] S20: re-enumerate the target USB device as a target NCM device, and use a preset network connection state machine to switch the current mode to the NCM mode and configure the target NCM device;

[0042] S30: Send a switching command to the target USB device so that the target USB device switches to the NCM mode.

[0043] Accordingly, see Figure 2The embodiment of the present application also provides a USB connection device 100, and the USB connection method of the embodiment of the present application can be implemented by the USB connection device 100. The USB connection device 100 includes an enumeration module 110, a re-enumeration module 120 and a sending module 130. S10 can be implemented by the enumeration module 110, S20 can be implemented by the re-enumeration module 120, and S30 can be implemented by the sending module 130. In other words, the enumeration module 110 is used to enumerate to determine the target USB device when a USB connection request is detected. The re-enumeration module 120 is used to re-enumerate the target USB device as a target NCM device, and use a preset network connection state machine to switch the current mode to the NCM mode and configure the target NCM device. The sending module 130 is used to send a switching command to the target USB device so that the target USB device switches to the NCM mode.

[0044] The embodiment of the present application also provides an electronic device. The electronic device includes a memory and a processor. A computer program is stored in the memory, and the processor is used to enumerate and determine a target USB device when a USB connection request is detected, re-enumerate the target USB device as a target NCM device, and use a preset network connection state machine to switch the current mode to the NCM mode and configure the target NCM device, and send a switching command to the target USB device so that the target USB device switches to the NCM mode.

[0045] Specifically, the USB connection method is used for the connection between a USB host and a USB device and the subsequent data transmission. The USB host is a device that acts as a master device in the USB bus and is responsible for managing data transmission and port management in the USB bus. In one example, a USB flash drive (USB mass storage device) communicates with a PC, and the PC can be a USB host. In another example, a mobile phone communicates with a vehicle or a car computer, and the car computer can be a USB host. USB hosts include but are not limited to: PC computers, vehicles, tablets, wearable devices, mobile terminals and other electronic devices that are master devices in the USB bus. An electronic device that is connected to a USB host for communication is a USB device.

[0046] This application uses the car computer as the USB host and the mobile phone as the USB device to implement the embodiment.

[0047] It is understandable that there are currently two solutions for wired interconnection between mobile phones and car computers: Google's AOA and Apple's EAP. First of all, the AOA protocol only supports simple data transmission and reception interfaces, but it is not supported by the existing network library at the application layer. If developers want to establish WebSockets on the mobile phone and the car computer based on business needs, AOA does not support this situation, so Google's AOA protocol is not flexible enough for developers. In addition, for Apple's EAP protocol, MFI certification must be done to use the EAP protocol when establishing a connection. First, apply for qualification certification on the MFI official website. Secondly, it is necessary to add Apple's encryption chip and integrate it into the car computer, which increases the cost of software and hardware.

[0048] In step S10, the USB connection method of the present application enumerates to determine the target USB device when a USB connection request is detected. When a USB device is connected to a USB host, the USB host performs enumeration to find the USB device connected thereto, that is, to determine the target USB device.

[0049] In one example, enumeration may include the following process. First, after the USB device is powered on, the USB host hub detects a voltage change and uses its own interrupt endpoint to feed back information to the main controller that a device is connected. After the USB host learns that a new USB device is connected, it resets the hub. At the same time, the USB host continues to send Get_Port_Status requests to the hub to inquire whether the device is reset successfully. The USB host determines the speed type of the connected device and detects whether the connected full-speed device supports high-speed mode by detecting the high and low voltages of the differential line when the USB bus is idle. Furthermore, the USB host controller assigns a unique address to the USB device through a Set_Address request. After completing this transmission, the USB device, i.e., the target USB device, enters the address state (Address State), and then enables the new address to continue communicating with the USB host, and the USB host obtains relevant information about the target USB device.

[0050] Furthermore, in step S20, the target USB device is re-enumerated as a target NCM device, and the current mode is switched to the NCM mode by using a preset network connection state machine and the target NCM device is configured.

[0051] Specifically, after the USB host determines the target USB device, it re-enumerates. Re-enumeration includes re-enumerating the target USB device to the target NCM device, wherein the target NCM device supports the NCM mode. The target NCM device includes a virtual network card or other devices that can be used to establish a network connection. In addition, the enumeration can be performed by randomly or by specifying a certain NCM device as the target NCM device. For ease of explanation, the following embodiments are carried out with the virtual network card as the target NCM device.

[0052] The target USB device is re-enumerated as a target NCM device, and the target NCM device is configured using a preset network connection state machine, so as to switch the current USB connection mode to the NCM mode. Among them, the NCM mode is the Network Connection Management (NCM) mode. In the NCM mode, the USB host and the target USB device can perform data transmission through the network connection management, and the data transmission can be transmitted through an IP channel, including transmission protocols such as IPv4 and IPv6. When the switching is completed, the USB host and the USB device are connected through the NCM mode, or an IP transmission channel based on a virtual network card is established between the USB host and the USB device, so that the subsequent data transmission between the USB host and the USB device is IP transmission.

[0053] The preset network connection state machine may include states such as preparation, successful connection, and failed connection. The preset network connection state machine is used to switch the current mode to the NCM mode and configure the target NCM device, that is, to complete the configuration of the target NCM device and the final switching in various states of the network connection state machine. When the configuration fails, the connection can be terminated or returned to the original USB connection method through the network connection state machine. Among them, the configuration includes setting the URL, gateway, IP address, port, etc. of the virtual network card. That is, through the automatic configuration of the network connection, the application layer of the target USB device and the USB host can transmit data through the relevant parameters configured by the network connection state machine, such as the IP address. For IPv4, the peer discovery and connection can be performed through the ARP (Address Resolution Protocol) and ICMP router discovery (Router Discovery) of IPv4. For IPv6, the peer discovery and connection can be performed through the neighbor discovery protocol NDP (Neighbor Discovery Protocol).

[0054] Preferably, see Figure 3 In some embodiments, the configuration includes a first device state configuration and a first network connection configuration, and step S20 includes:

[0055] S21: Performing a first device state configuration on the target NCM device using the network connection state machine;

[0056] S22: If the first device state configuration is successful, change the state of the network connection state machine and perform the first network connection configuration.

[0057] In some implementations, S21 and S22 may be implemented by the re-enumeration module 120. In other words, the re-enumeration module 120 is used to use the network connection state machine to perform a first device state configuration on the target NCM device, and if the first device state configuration is successful, change the state of the network connection state machine and perform a first network connection configuration.

[0058] In some implementations, the processor is configured to use the network connection state machine to perform a first device state configuration on the target NCM device, and if the first device state configuration is successful, change the state of the network connection state machine and perform the first network connection configuration.

[0059] Specifically, the first device state configuration includes the device state configuration of the target NCM device, such as the device name, device performance, device version, device available transmission type, etc. When the first device state configuration is successful, the network connection state machine changes the state and performs the first network connection configuration. The first network connection configuration includes IP configuration, etc. to implement the network connection configuration of the virtual network card.

[0060] Preferably, see Figure 4 In some embodiments, step S21 includes:

[0061] S211: Generate a switching event and change the network connection state machine to a first state according to the switching event;

[0062] S212: Performing a first device state configuration on the target NCM device and loading a driver of the target NCM device, wherein the first device state configuration at least includes setting a device name, setting a transmission type, and registering a NetworkAgent;

[0063] Accordingly, step S22 includes:

[0064] S221: If the first device state configuration is successful, change the network connection state machine to the second state;

[0065] S222: Perform a first network connection configuration on the target NCM device, where the first network connection configuration at least includes IP configuration;

[0066] In some embodiments, S211 and S212, S221 and S222 can be implemented by the re-enumeration module 120. In other words, the re-enumeration module 120 is used to generate a switching event and change the network connection state machine to the first state according to the switching event, perform a first device state configuration on the target NCM device and load the driver of the target NCM device, the first device state configuration at least includes setting the device name, setting the transmission type and registering the NetworkAgent, if the first device state configuration is successful, change the network connection state machine to the second state, perform a first network connection configuration on the target NCM device, and the first network connection configuration at least includes IP configuration.

[0067] In certain embodiments, the processor is used to generate a switching event and change the network connection state machine to a first state according to the switching event, perform a first device state configuration on the target NCM device and load a driver for the target NCM device, the first device state configuration at least includes setting the device name, setting the transmission type and registering the NetworkAgent, if the first device state configuration is successful, change the network connection state machine to a second state, perform a first network connection configuration on the target NCM device, the first network connection configuration at least includes IP configuration.

[0068] Specifically, after re-enumerating and determining the target USB device, the driver layer of the USB host generates a switching event and sends the switching event to the framework layer FrameWork of the USB host, adds SystemService to the framework layer FrameWork of the USB host, and presets a network connection state machine in SystemService. The network connection state machine includes multiple network states, and in the process of establishing a connection, the state switching is configured to manage the establishment of the connection.

[0069] See also Figure 5In one example, the network state machine may include DefaultState, DisconnectedState, ReadyState, NetWorkConfigurationState, and ConnectedState. When the USB host switches the current mode to the NCM mode, the driver layer generates a switching event UEvent and sends it to the FrameWork. After the SystemService of the FrameWork listens to it, the network state machine enters the first state ReadyState from the DisconnectedState according to the switching event UEvent, and configures the virtual network card to the first device state, wherein the first device state configuration at least includes setting the device name such as the network card name of the virtual network card, setting the transmission type, and registering the NetworkAgent. The transmission type can be set by adding the transmission type in NetworkCapabilities as: TRANSPORT_USB_NCM. The subsequent upper-layer application can set the TransportType to TRANSPORT_USB_NCM when constructing the NetworkRequest object, and the upper-layer application can perform data transmission through this target NCM device by calling the Android standard API. In addition, NetworkCapabilities may also include the following parameters:

[0070] NET_CAPABILITY_NOT_METERED (This network is not metered)

[0071] NET_CAPABILITY_NOT_ROAMING (indicates that this network is not roaming)

[0072] NET_CAPABILITY_NOT_SUSPENDED (indicates that this network is not currently suspended)

[0073] NET_CAPABILITY_TRUSTED (indicates that the user has indicated implicit trust for this network)

[0074] setLinkUpstreamBandwidthKbps(1024*1024)(upstream bandwidth 1Gbps)

[0075] setLinkDownstreamBandwidthKbps(1024*1024)(downstream bandwidth 1Gbps)

[0076] The state of the virtual network card, ie, the network card parameters and state of the virtual network card, can be determined by configuring the first device state.

[0077] After the virtual network card is determined, the driver of the virtual network card can be automatically loaded through the network connection, so that subsequent data transmission can be carried out.

[0078] In this way, for some special applications, the USB device that is connected to the USB host for the first time needs to install the driver provided by a third party before it can be used. For multiple applications, the drivers of multiple devices need to be installed when they are used for the first time, and more system garbage may be generated. This application can improve the connection efficiency of USB by switching to NCM mode and automatically installing the driver, without waiting for a long time, and providing a better user experience. At the same time, because the application does not need to rely on the interface to install the driver, the coupling degree between the application and the USB connection is reduced to a certain extent.

[0079] In some embodiments, before the current mode is switched to the NCM mode, the determined target USB device may be further detected, including but not limited to determining whether it supports the NCM mode or whether it is in the blacklist or whitelist, that is, by detecting the target USB device to determine whether it can continue to switch to the NCM mode.

[0080] When the configuration of the first device state of the virtual network card is successful, the network state machine enters the second state NetWorkConfigurationState state, and configures the first network connection of the virtual network card, wherein the first network connection at least includes IP configuration. The IP configuration at least includes: website, gateway, port, etc. At the same time, the IP configuration can communicate with Networkstack and Netd through IpClientProxy. Conversely, when the configuration of the first device state of the virtual network card fails, the network state machine re-enters the DisconnectedState state.

[0081] Further, when the configuration of the first network connection is successful, the state of ConnectedState is entered, and the target USB device successfully establishes a connection with the USB host in NCM mode. On the contrary, if the configuration of the first network connection fails, the state of DisconnectedState is entered again.

[0082] In this way, by presetting the network connection state machine and its network state machine to manage switching and network connection, on the one hand, the switching event and its related configuration can be associated to improve the efficiency of switching. On the other hand, the preset network connection state machine provides a control interface for the system layer. For upper-layer applications, they only need to understand how to use the interface to conveniently control the system and obtain information about each service of the system without having to understand the specific implementation method of the interface. Its implementation method is simple and effective, and it is easy to manage, which reduces the coupling with the application to a certain extent.

[0083] It is understandable that when the first network connection configuration is successful, the network is available, and the USB device is successfully connected to the NCM mode of the USB host. For other upper layer applications, network services such as webSocket and other network data transmission can be established through the IP address and port of the network card.

[0084] This application determines the target USB device by enumerating when a USB connection request is detected, re-enumerates the target USB device as a target NCM device, and uses a preset network connection state machine to switch the current mode to NCM mode and configure the target NCM device, and sends a switching command to the target USB device to switch the target USB device to NCM mode. In this way, after the USB device is connected to the USB host, an IP-based transmission channel can be built by switching to NCM mode, adding a specific network type-NCM to the USB connection, so that upper-level applications or other services can use standard Android APIs to transmit data in NCM mode. Compared with the USB mode, using a standard network card to transmit data does not require the addition of special drivers for upper-level applications, and on the other hand, it supports more network libraries for services, thereby effectively improving the development efficiency of upper-level applications. Secondly, for some special applications, USB devices that are connected to the USB host for the first time need to install drivers provided by a third party before they can be used. This requires installing drivers for multiple devices when used for the first time, and may generate a lot of system garbage. This application can improve the connection efficiency of USB by switching to NCM mode and automatically installing the driver. There is no need to wait for a long time, and the user experience is better. At the same time, because the application does not need to rely on the interface to install the driver, the coupling degree between the application and the USB connection is reduced to a certain extent. Furthermore, the problem that the Google AOA protocol in the current connection method between the car computer and the mobile terminal cannot establish a Websocket is solved, and the existing network library of the application layer is supported. In addition, there is no need to add an additional encryption chip integrated on the car computer, which effectively reduces the cost of software and hardware.

[0085] Preferably, see Figure 6 In one embodiment, before step S20, the following steps may also be included:

[0086] S01: Send a first instruction to a target USB device and determine whether the target USB device meets a preset condition according to a first instruction response returned by the target USB device;

[0087] S02: When the USB device meets the preset condition, a second instruction is sent to the target USB device to determine NCM information of the NCM mode.

[0088] Accordingly, see Figure 7The USB connection device of the embodiment of the present application further includes a judgment module 140, and S01 and S02 can be implemented by the judgment module 140. In other words, the judgment module 140 is used to send a first instruction to the target USB device and judge whether the target USB device meets the preset condition according to the first instruction response returned by the target USB device, and when the USB device meets the preset condition, send a second instruction to the target USB device to determine the NCM information of the NCM mode.

[0089] In some embodiments, the processor is used to send a first instruction to the target USB device and determine whether the target USB device meets a preset condition based on the first instruction response returned by the target USB device. When the USB device meets the preset condition, a second instruction is sent to the target USB device to determine the NCM information of the NCM mode.

[0090] Specifically, after the target USB device is determined by enumeration, the USB host sends a first instruction to the target USB device, wherein the first instruction may include an authentication instruction 0X81, and the relevant version information of the USB device is obtained through the authentication instruction 0X81. After the target USB device receives the authentication instruction 0X81, it returns its own version information to the USB host in the first instruction response, and the USB host determines whether the target USB device meets the preset conditions according to the first instruction response returned by the target USB device. Among them, the preset conditions include whether the target USB device supports the NCM mode, or whether the target USB device can be switched by querying the black and white lists, etc.

[0091] Preferably, see Figure 8 In one implementation, step S01 may further include:

[0092] S011: Send a first instruction to the target USB device;

[0093] S012: receiving a first command response returned by the target USB device to obtain preset parameters of the target USB device;

[0094] S013: Query whether the target USB device supports NCM mode according to preset parameters;

[0095] S014: If the target USB device supports the NCM mode, determine whether the USB device meets a preset condition;

[0096] S015: If the target USB device does not support the NCM mode, it is determined that the USB device does not meet the preset condition.

[0097] In some embodiments, S011-S015 may be implemented by the determination module 140. In other words, the determination module 140 is used to send a first instruction to the target USB device, receive a first instruction response returned by the target USB device to obtain preset parameters of the target USB device, query whether the target USB device supports the NCM mode according to the preset parameters, and if the target USB device supports the NCM mode, determine that the USB device meets the preset conditions; if the target USB device does not support the NCM mode, determine that the USB device does not meet the preset conditions.

[0098] In some embodiments, the processor is used to send a first instruction to the target USB device, receive a first instruction response returned by the target USB device to obtain preset parameters of the target USB device, and query whether the target USB device supports NCM mode based on the preset parameters. If the target USB device supports NCM mode, it is determined that the USB device meets the preset conditions; if the target USB device does not support NCM mode, it is determined that the USB device does not meet the preset conditions.

[0099] Specifically, the USB host sends an authentication instruction 0X81, i.e., a first instruction, to the target USB device, and receives a first instruction response returned by the target USB device, wherein the first instruction may include the authentication instruction 0X81, and obtains preset parameters of the USB device through the authentication instruction 0X81, wherein the preset parameters include relevant version information of the target USB device, NCM version information, etc. Further, the USB host inquires whether the target USB device supports the NCM mode according to the preset parameters, i.e., relevant version information, etc. returned by the first instruction response. If the NCM mode is supported, it means that the preset condition is met. If the target USB device does not support the NCM mode, it is determined that the USB device does not meet the preset condition.

[0100] In another embodiment, the USB host presets a black and white list, and the black and white list records data of multiple USB devices. Among them, for the USB devices in the white list, the connection transmission can continue, otherwise, for the USB devices in the black list, the connection is stopped. After the target USB device is determined, it is determined whether the target USB device supports the NCM mode. When the target USB device supports the NCM mode, it is further determined whether the target USB device is in the black and white list. If it is not in the black and white list database, or in the white list of the black and white list, it means that the preset condition is met. In another embodiment, it is also possible not to determine whether the target USB device supports the NCM mode, but directly determine whether the preset condition is met through the black and white list.

[0101] It is understandable that if the preset conditions are not met, the switching is stopped, and the original USB connection mode can be used, or the USB connection can be stopped. There is no restriction on this, and the subsequent business process is constructed according to the actual situation.

[0102] Further, when the USB device meets the preset condition, a second instruction is sent to the target USB device to determine the NCM information of the NCM mode. The second instruction includes instruction 0X82, which may include the NCM version information of the USB host. When the target USB device receives the 0X82 instruction including the NCM version information, the NCM version information of the USB host may be sent to the framework protocol layer, and the NCM version information may be queried, the USB host may be authenticated, or the NCM version may be upgraded.

[0103] In this way, by judging whether the target USB device meets the preset conditions to detect whether it can switch to NCM mode, it can effectively avoid switching to USB devices that do not support NCM mode or are in the blacklist, thus ensuring the effectiveness of the USB connection method to a certain extent.

[0104] See also Fig. 9 , the present application also provides a USB connection method for a USB device, comprising:

[0105] S40: receiving a switching command sent by the USB host;

[0106] S50: Switch the current mode to the NCM mode according to the switching command;

[0107] S60: Perform a second device state configuration and a second network connection configuration using the network connection state machine, and establish a connection with the host USB device according to the second device state configuration and the second network connection configuration.

[0108] Accordingly, see Fig.10 The embodiment of the present application also provides a USB connection device 200, and the USB connection method of the embodiment of the present application can be implemented by the USB connection device 200. The USB connection device 200 includes a receiving module 210, a switching module 220 and a configuration module 230. S40 can be implemented by the receiving module 210, S50 can be implemented by the switching module 220, and S60 can be implemented by the configuration module 230. In other words, the receiving module 210 is used to receive a switching command sent by the USB host. The switching module 220 is used to switch the current mode to the NCM mode according to the switching command. The configuration module 230 is used to use the network connection state machine to perform a second device state configuration and a second network connection configuration, and establish a connection with the host USB device according to the second device state configuration and the second network connection configuration.

[0109] The embodiment of the present application also provides an electronic device. The electronic device includes a memory and a processor. The memory stores a computer program, and the processor is used to receive a switching command sent by a USB host, switch the current mode to the NCM mode according to the switching command, use the network connection state machine to perform a second device state configuration and a second network connection configuration, and establish a connection with the host USB device according to the second device state configuration and the second network connection configuration.

[0110] It is understandable that the USB connection method is used for the connection between the USB host and the USB device and the subsequent data transmission. A USB device is a device that acts as a slave device in the USB bus. In one example, a USB flash drive (USB mass storage device) communicates with a PC, and the USB flash drive can be a USB device. In another example, a mobile phone communicates with a vehicle or a car computer, and the mobile phone can be a USB device. USB devices include but are not limited to: mobile phones, tablet computers, wearable devices, smart terminals and other electronic devices that are slave devices in the USB bus. An electronic device that is connected to a USB device for communication is a USB host.

[0111] Specifically, when a USB device is inserted into a USB host, the USB host will perform enumeration of the USB device, and its enumeration method is basically the same as the above embodiment, which will not be repeated here. After the detection is completed, the USB host will send a switching command to the USB device. The USB device receives the switching command from the USB host and switches the current mode to the NCM mode according to the switching command. The switching method is basically the same as the above embodiment, which will not be repeated here.

[0112] Preferably, see Fig.11 In one embodiment, before step S40, the method further includes:

[0113] S03: receiving a first instruction sent by the USB host;

[0114] S04: Generate a first command response based on the preset parameters according to the first command and send the first command response to the USB host;

[0115] S05: receiving a second instruction sent by the USB host;

[0116] S06: Determine NCM information of the NCM mode according to the second instruction.

[0117] Accordingly, see Fig.12The USB connection device 200 of the embodiment of the present application further includes a determination module 240, and S03-S06 can be implemented by the determination module 240. In other words, the determination module 240 is used to receive a first instruction sent by the USB host, generate a first instruction response according to the preset parameters based on the first instruction and send the first instruction response to the USB host, receive a second instruction sent by the USB host, and determine the NCM information of the NCM mode according to the second instruction.

[0118] In some embodiments, the processor is used to receive a first instruction sent by a USB host, generate a first instruction response based on preset parameters according to the first instruction and send the first instruction response to the USB host, receive a second instruction sent by the USB host, and determine NCM information of the NCM mode according to the second instruction.

[0119] Specifically, the USB device receives a first instruction sent by the USB host. The first instruction may include an authentication instruction 0X81. After receiving the authentication instruction 0X81, a first instruction response is generated according to preset parameters according to the first instruction and the first instruction response is sent to the USB host. The preset parameters include at least NCM version information, etc. Correspondingly, the USB host determines whether the target USB device meets the preset conditions according to the first instruction response returned by the target USB device. The preset conditions include whether the target USB device supports the NCM mode, or whether the target USB device can be switched by querying the black and white lists, etc.

[0120] Further, a second instruction sent by the USB host is received. The second instruction includes instruction 0X82, which may include NCM version information of the USB host. When the target USB device receives the 0X82 instruction including the NCM version information, the NCM version information of the USB host may be sent to the framework protocol layer to determine the NCM information of the NCM mode, including querying and determining the NCM version information, authenticating the USB host to determine whether communication can continue or whether the NCM version needs to be upgraded.

[0121] In this way, whether the NCM mode switching between the USB host and the USB device is possible and the related NCM version information can be determined through the first instruction and the second instruction.

[0122] Furthermore, in step S60, the network connection state machine is used to perform a second device state configuration and a second network connection configuration, and a connection is established with the host USB device according to the second device state configuration and the second network connection configuration.

[0123] Specifically, after the USB device switches to the NCM mode, the NCM mode is managed according to a preset network connection state machine, or in other words, the second device state configuration and the second network connection configuration are performed on the target NCM device determined by re-enumeration.

[0124] Preferably, see Fig.13 In one implementation, step S60 may further include:

[0125] S601: Generate a switching event according to a second instruction and change a preset network connection state machine to a first state according to the switching event;

[0126] S602: Perform a second device status configuration, where the second device status configuration at least includes setting a device name, setting a transmission type, and registering a NetworkAgent;

[0127] S603: If the second device state configuration is successful, change the network connection state machine to the second state;

[0128] S604: Perform a second network connection configuration, where the second network connection configuration at least includes an IP configuration;

[0129] S605: If the second network connection configuration is successful, determine to establish a USB connection with the USB host.

[0130] In some embodiments, S601-S605 can be implemented by the configuration module 230. In other words, the configuration module 230 is used to generate a switching event according to the second instruction and change the preset network connection state machine to the first state according to the switching event, perform a second device state configuration, and the second device state configuration at least includes setting the device name, setting the transmission type, and registering the NetworkAgent. If the second device state configuration is successful, change the network connection state machine to the second state, perform a second network connection configuration, and the second network connection configuration at least includes IP configuration. If the second network connection configuration is successful, determine to establish a USB connection with the USB host.

[0131] In some embodiments, the processor is used to generate a switching event according to the second instruction and change the preset network connection state machine to the first state according to the switching event, and perform a second device state configuration, the second device state configuration at least includes setting the device name, setting the transmission type and registering the NetworkAgent. If the second device state configuration is successful, the network connection state machine is changed to the second state, and the second network connection configuration is performed. The second network connection configuration at least includes IP configuration. If the second network connection configuration is successful, it is determined to establish a USB connection with the USB host.

[0132] Specifically, the network connection state machine SystemService and the network state machine are preset in the framework layer FrameWork of the USB device. The network state machine includes various network states, and in the process of establishing a connection, the state switching is configured to manage the establishment of the connection.

[0133] In one example, the network state machine includes DefaultState, DisconnectedState, ReadyState, NetWorkConfigurationState, and ConnectedState. When the USB device switches the current mode to NCM mode, the driver layer generates a switching event UEvent and sends it to FrameWork. After the SystemService of FrameWork listens to it, the network state machine enters the first state such as ReadyState from DisconnectedState according to the switching event UEvent, and configures the second device state. The second device state configuration at least includes setting the device name such as the network card name of the virtual network card, setting the transmission type, and registering the NetworkAgent.

[0134] When the configuration of the second device state is successful, the network state machine enters the second state, such as the NetWorkConfigurationState state, and configures the second network connection, wherein the second network connection at least includes IP configuration. The IP configuration at least includes: website, gateway, port, etc. At the same time, the IP configuration can communicate with Networkstack and Netd through IpClientProxy. Conversely, when the configuration of the second device state fails, the network state machine re-enters the DisconnectedState state.

[0135] Furthermore, when the configuration of the second network connection is successful, the state of ConnectedState is entered, and the target USB device and the USB host establish a connection in NCM mode. On the contrary, if the configuration of the second network connection fails, the state of DisconnectedState is entered again.

[0136] In this way, the switching and network connection are managed by the preset network connection state machine and its network state machine. On the one hand, the switching event and its related configuration can be associated to improve the efficiency of the switching. On the other hand, the preset network connection state machine provides a control interface for the system layer. For the upper-layer application, it only needs to understand how to use the interface to conveniently control the system and obtain information about each service of the system without understanding the specific implementation method of the interface. Its implementation method is simple and effective, and it is easy to manage, which reduces the coupling with the application to a certain extent.

[0137] The USB connection method, connection device, and electronic device of the embodiment of the present application receive a switching command sent by the USB host, switch the current mode to the NCM mode according to the switching command, and manage the NCM mode according to the preset network connection state machine to establish a USB connection with the target USB device. A virtual network card is generated on the USB device side, an IP-based transmission channel is constructed, and a specific network type-NCM is added to the USB connection, so that the upper-layer application or other service can use the standard Android API to transmit data in the NCM mode through this virtual network card. Compared with the USB mode, using a standard network card to transmit data does not require the upper-layer application to add special drivers on the one hand, and on the other hand, it supports more network libraries for services, thereby effectively improving the development efficiency of the upper-layer application.

[0138] In summary, the present application enumerates the target USB device when the USB host detects a USB connection request, re-enumerates the target USB device as a target NCM device, and uses a preset network connection state machine to switch the current mode to the NCM mode and configure the target NCM device, and sends a switching command to the target USB device so that the target USB device switches to the NCM mode. The USB device receives the switching command sent by the USB host, switches the current mode to the NCM mode according to the switching command, uses the network connection state machine to perform a second device state configuration and a second network connection configuration, and establishes a connection with the host USB device according to the second device state configuration and the second network connection configuration. It has at least the following beneficial effects:

[0139] 1. After the USB device is connected to the USB host, it can be switched to NCM mode to build an IP-based transmission channel, adding a specific network type - NCM - to the USB connection, so that upper-layer applications or other services can use standard Android APIs to transmit data in NCM mode. Compared with USB mode, using a standard network card to transmit data does not require the addition of special drivers for upper-layer applications, and supports more network libraries for services, thereby effectively improving the development efficiency of upper-layer applications.

[0140] Second, for some special applications, the USB device that is connected to the USB host for the first time needs to install the driver provided by a third party before it can be used. For multiple applications, the drivers of multiple devices need to be installed when they are used for the first time, and a lot of system garbage may be generated. This application can improve the USB connection efficiency by switching to NCM mode and automatically installing the driver, without waiting for a long time, and providing a better user experience. At the same time, because the application does not need to rely on the interface to install the driver, the coupling between the application and the USB connection is reduced to a certain extent.

[0141] Third, it solves the problem that the Google AOA protocol cannot establish Websocket in the current connection method between the car computer and the mobile terminal, and supports the existing network library of the application layer. In addition, there is no need to add an additional encryption chip to be integrated in the car computer, which effectively reduces the cost of software and hardware.

[0142] Fourth, the switching and network connection are managed by the preset network connection state machine and its network state machine. On the one hand, the switching event and its related configuration can be associated to improve the efficiency of switching. On the other hand, the preset network connection state machine provides a control interface for the system layer. For the upper-layer application, it only needs to understand how to use the interface to conveniently control the system and obtain information about each service of the system without understanding the specific implementation method of the interface. Its implementation method is simple and effective, and it is easy to manage, which reduces the coupling with the application to a certain extent.

[0143] The present application also provides a computer-readable storage medium, one or more non-volatile computer-readable storage media storing a computer program, which implements the USB connection method of any of the above embodiments when the computer program is executed by one or more processors.

[0144] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiments can be implemented by instructing related software through a computer program. The program can be stored in a non-volatile computer-readable storage medium, and when the program is executed, it can include the processes of the embodiments of the above-mentioned methods. The storage medium can be a disk, an optical disk, a read-only memory (ROM), etc.

[0145] The above embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the present application. It should be pointed out that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the present application shall be subject to the attached claims.

Claims

1. A USB connection method for a USB host, characterized in that: include: When a USB connection request is detected, enumerate to determine the target USB device; Re-enumerate the target USB device as a target NCM device, and use a preset network connection state machine to switch the current mode to the NCM mode and configure the target NCM device; Sending a switching command to the target USB device so that the target USB device switches to the NCM mode; The configuration includes a first device state configuration and a first network connection configuration, and the configuration of the target NCM device using the network connection state machine includes: Performing the first device state configuration on the target NCM device using a network connection state machine; If the first device state configuration is successful, changing the state of the network connection state machine and performing the first network connection configuration; The using the network connection state machine to configure the first device state of the target NCM device includes: Generate a switching event and change the network connection state machine to a first state according to the switching event; Performing the first device state configuration on the target NCM device and loading the driver of the target NCM device, wherein the first device state configuration at least includes setting a device name, setting a transmission type, and registering a NetworkAgent; If the first device state configuration is successful, changing the state of the network connection state machine and performing the first network connection configuration includes: If the first device state configuration is successful, changing the network connection state machine to a second state; Performing the first network connection configuration on the target NCM device, where the first network connection configuration at least includes IP configuration; The USB connection method of the USB host further includes: The system service and the network state machine of the network connection state machine are preset in the framework layer of the USB device.

2. The USB connection method according to claim 1, characterized in that: Before re-enumerating the target USB device as a target NCM device, the following steps are further included: Sending a first instruction to the target USB device and judging whether the target USB device meets a preset condition according to a first instruction response returned by the target USB device; When the USB device meets the preset condition, a second instruction is sent to the target USB device to determine NCM information of the NCM mode.

3. The USB connection method according to claim 2, characterized in that: The sending of the first instruction to the target USB device and judging whether the target USB device meets a preset condition according to the first instruction response returned by the target USB device comprises: Sending the first instruction to the target USB device; Receiving a first command response returned by the target USB device to obtain preset parameters of the target USB device; Querying whether the target USB device supports the NCM mode according to the preset parameters; If the target USB device supports the NCM mode, determining that the USB device meets the preset condition; If the target USB device does not support the NCM mode, it is determined that the USB device does not meet the preset condition.

4. A USB connection method for a USB device, characterized in that: include: Receive the switching command sent by the USB host; Switch the current mode to the NCM mode according to the switching command; Performing a second device state configuration and a second network connection configuration using a network connection state machine, and establishing a connection with the host USB device according to the second device state configuration and the second network connection configuration; Before receiving the switching command sent by the USB host, the method further includes: Receive a first instruction sent by the USB host; Generate a first command response based on preset parameters according to the first command and send the first command response to the USB host; Receive a second instruction sent by the USB host; Determine NCM information of the NCM mode according to the second instruction; The method of using the network connection state machine to perform a second device state configuration and a second network connection configuration, and establishing a connection with the host USB device according to the second device state configuration and the second network connection configuration includes: generating a switching event according to the second instruction and changing the network connection state machine to a first state according to the switching event; Performing a second device status configuration, wherein the second device status configuration at least includes setting a device name, setting a transmission type, and registering a NetworkAgent; If the second device state configuration is successful, changing the network connection state machine to a second state; Performing a second network connection configuration, wherein the second network connection configuration at least includes an IP configuration; If the second network connection configuration is successful, determining to establish the connection with the host USB device; The USB connection method of the USB device further includes: The system service and the network state machine of the network connection state machine are preset in the framework layer of the USB device.

5. A USB connection device for a USB host, characterized in that: include: An enumeration module, used for enumerating to determine a target USB device when a USB connection request is detected; A re-enumeration module, used for re-enumerating the target USB device as a target NCM device, switching the current mode to the NCM mode using a preset network connection state machine, and configuring the target NCM device; A sending module, used for sending a switching command to the target USB device so that the target USB device switches to the NCM mode; The configuration includes a first device state configuration and a first network connection configuration, and the re-enumeration module is further used to use a network connection state machine to perform the first device state configuration on the target NCM device; if the first device state configuration is successful, change the state of the network connection state machine and perform the first network connection configuration; Generate a switching event and change the network connection state machine to a first state according to the switching event; Performing the first device state configuration on the target NCM device and loading the driver of the target NCM device, wherein the first device state configuration at least includes setting a device name, setting a transmission type, and registering a NetworkAgent; If the first device state configuration is successful, changing the network connection state machine to a second state; Performing the first network connection configuration on the target NCM device, where the first network connection configuration at least includes IP configuration; The configuration module is also used to preset the system service and the network state machine of the network connection state machine in the framework layer of the USB device.

6. A USB connection device for a USB device, characterized in that: include: A receiving module, used for receiving a switching command sent by a USB host; A switching module, used for switching the current mode to the NCM mode according to the switching command; A configuration module for using the network connection state machine to configure the second device state and the second network connection configuration, and establish a connection with the host USB device according to the second device state configuration and the second network connection configuration; The USB connection device further includes a determination module, the determination module being configured to receive a first instruction sent by the USB host; generate a first instruction response using preset parameters according to the first instruction and send the first instruction response to the USB host; receive a second instruction sent by the USB host; and determine NCM information of the NCM mode according to the second instruction; The configuration module is further used to generate a switching event according to the second instruction and change the network connection state machine to the first state according to the switching event; perform a second device state configuration, the second device state configuration at least includes setting a device name, setting a transmission type and registering a NetworkAgent; if the second device state configuration is successful, change the network connection state machine to the second state; perform a second network connection configuration, the second network connection configuration at least includes an IP configuration; if the second network connection configuration is successful, determine to establish the connection with the host USB device; The configuration module is also used to preset the system service and the network state machine of the network connection state machine in the framework layer of the USB device.

7. An electronic device, characterized in that: The electronic device includes a memory and a processor, wherein a computer program is stored in the memory, and when the computer program is executed by the processor, the USB connection method according to any one of claims 1 to 3 or 4 is implemented.

8. A non-volatile computer-readable storage medium for a computer program, characterized in that: When the computer program is executed by one or more processors, the USB connection method according to any one of claims 1 to 3 or 4 is implemented.