Introduction to MTP Protocol development

Source: Internet
Author: User
Tags session id

Because the MTP protocol is widely used in mobile devices, it becomes one of the necessary components in the device interconnection products. It is easy to imagine a car in the way, access to the car equipment, listen to the songs on the phone, or at rest, borrow a large car screen to see the phone's high-definition movies, which makes the journey easier and more comfortable.
This article tries to avoid introducing the MTP protocol (the document has been written clearly), mainly for a specific device (Google Nexus 4), introducing MTP development knowledge. 1. MTP Equipment Model

Understand the MTP device model to have basic USB protocol knowledge. The MTP device describes the communication endpoint (Endpoint) of the device through a USB descriptor. The following is a list of four Prince's USB descriptor:
# lsusb-v-D18D1:

Bus 001 Device 003:id 18d1:4ee1 Google Inc. Device descriptor:blength 1 BCDUSB 2.00 bdeviceclass 0 (Defined at Interface level) Bdevicesubclass 0 Bdevicepr
  Otocol 0 bMaxPacketSize0 Idvendor 0X18D1 Google Inc.
  Idproduct 0x4ee1 bcddevice 2.28 imanufacturer 1 LGE iproduct 2 Nexus 4                 Iserial 3 0054700f490d669a bnumconfigurations 1 Configuration descriptor:blength      9 bDescriptorType 2 wtotallength bnuminterfaces 1 bconfigurationvalue
    1 IConfiguration 0 bmattributes 0x80 (bus powered) Maxpower 500mA
      Interface descriptor:blength 9 bDescriptorType 4 Binterfacenumber 0           Balternatesetting 0 bnumendpoints3 Binterfaceclass 255 Vendor specific Class binterfacesubclass 255 vendor specific Subclass BIn
        Terfaceprotocol 0 iinterface 4 MTP Endpoint descriptor:blength 7 bDescriptorType 5 bendpointaddress 0x81 EP 1 in bmattributes 2 Tra Nsfer type Bulk Synch type None Usage type Data Wmaxpac                 Ketsize 0x0200 1x bytes Binterval 0 Endpoint descriptor:blength
          7 bDescriptorType 5 bendpointaddress 0x01 EP 1 out bmattributes 2
        Transfer type Bulk Synch type None Usage type Data                 Wmaxpacketsize 0x0200 1x bytes Binterval 0 Endpoint descriptor:blength 7 bDescriptortype 5 bendpointaddress 0x82 EP 2 in bmattributes 3 Transfer T ype Interrupt Synch type None Usage type Data Wmaxpacke                Tsize 0x001c 1x bytes Binterval 6 Device Qualifier (for other Device speed): blength Ten bDescriptorType 6 Bcdusb 2.00 bdeviceclass 0 (Defined at Interface Lev  EL) Bdevicesubclass 0 bdeviceprotocol 0 bMaxPacketSize0 bnumconfigurations 1 Device
 status:0x0000 (bus powered)

It can be seen that the N4 device provides only one configuration, the configuration provides a interface, and this interface has 3 endpoint (bulk in, bulk out & Interrupt In), is used for MTP protocol communication. 2. Enumeration of MTP devices

MTP protocol, does not stipulate how to determine whether a USB device is a MTP device, can generally use the following laws:
1. The USB_CLASS_PTP (6) is judged by the "bdeviceclass" field of device descriptor;
2. Through Udev, inquire whether the device attribute contains "Id_mtp_device";
3. Enumerate all the interface to inquire whether the corresponding string of its iinterface is "MTP" (This step is necessary for interface devices with multiple configuration and MTP. Because we want to specifically specify the configuration and interface used in USB communication. 3. MTP Communication Model

When the device is connected, the USB host plays the role of initiator, and the action is initiated by initiator, while the device plays responder and responds to the initiator request. In addition, responder can also escalate events to initiator to trigger initiator to do the appropriate action (for example, the device's unlock action, which triggers initiator to retrieve the device's storage list).
Initiator communication with responder is done via the 3 endpoint described above by USB descriptor: Initiator writes requests to the device via bulk out endpoint, responder through bulk in Endpoint returns the requested data to the initiator, and MTP event submits the incident information to endpoint through the interrupt in responder. 4. MTP Communication Data Encapsulation

All data for MTP is encapsulated in a specific container, and the encapsulated format is as follows:

Byte Offset Length Field Name Description
0 4 Containerlength Total amount of the data to is sent (including this header)
4 2 Containertype Defines the type of this container
6 2 Code Operation, Response or Event Code as defined in the MTP specification.
8 4 TransactionID 4.3.3 Transaction IDs.
12 ContainerLength-12 Payload The data which is sent into this phase.
5. Key process of MTP communication

Open session:

_container->length = sizeof (Mtpcontainer) + sizeof (GUINT32);
_container->type = Usb_container_command;
_container->code = oc_opensession;
_container->transactionid = TransactionID + +;
memcpy (_buf, (void *) container, sizeof (Mtpcontainer));
memcpy (_buf + _offset, params, container->length-sizeof (Mtpcontainer));//Here params saved the pre-generated session ID
Libusb_bulk_transfer (Mtpobj->handle, Mtpobj->bulkoutep, _buf, Container->length, &_len, 0); The request is sent through the bulk out endpoint via the LIBUSB bulk transfer
Then wait for the device to respond, that is, read bulk in Endpoint,buffer settings to be at least one of the endpoint maxpacketsize size, otherwise, when transferring large data, LIBUSB will report overflow, the data will be lost:

Libusb_bulk_transfer (Mtpobj->handle, MTPOBJ->BULKINEP, _header, Mtpobj->maxpacketsize, &_len, 0);
To determine the return value of the data obtained, the code should normally be consistent with the requested code, or 0x2001, to indicate that the request completed successfully, and other cases to be handled abnormally. Getdeviceinfo, this command allows you to get basic information about the device, including a list of supported commands, file types, attributes, and event types. All strings in the MTP are utf-16 encoded, and other encoded systems are converted by themselves. Get Storageids, if the device is locked in the case of access, the number of storage devices obtained is 0. You will need to wait for interrupt in endpoint to give the storage add event, and then retrieve the storage information for the device. The getnumobjects, getobjecthandles&getobjectinfo commands allow you to get the number, handle, and basic information (such as file name, size, and so on) for the specified type of file. There are 2 ways to get the contents of the specified file: The GETOBJECT&GETPARTIALOBJECT,MTP protocol indicates that you want to replace GetObject with Getpartialobject. Because Getpartialobject supports random access to files.

At this point, MTP Protocol Development Introduction to the end of the use of LIBUSB, you can refer to the previous article Libusb asynchronous interrupt transmission usage instructions. Combined with LIBUSB asynchronous transmission, we can realize multiple endpoint input processing of single thread and simplify program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.