diff options
author | reillyg <reillyg@chromium.org> | 2015-07-22 10:18:50 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-22 17:19:21 +0000 |
commit | 266b61d4d9fd0b1ed1675fc978d5d1c0a0a20a7d (patch) | |
tree | 5bbc69c2eb5ef1aca1d511c9c2f92e326f6daf40 /tools/usb_gadget | |
parent | 9089e74de5746e4a6095993d93b77855be6e167a (diff) | |
download | chromium_src-266b61d4d9fd0b1ed1675fc978d5d1c0a0a20a7d.zip chromium_src-266b61d4d9fd0b1ed1675fc978d5d1c0a0a20a7d.tar.gz chromium_src-266b61d4d9fd0b1ed1675fc978d5d1c0a0a20a7d.tar.bz2 |
usb_gadget: Define a string descriptor type and use it.
This patch constructs string descriptors using the framework from the
usb_descriptors module instead of raw calls to struct.pack.
BUG=None
Review URL: https://codereview.chromium.org/1243143002
Cr-Commit-Position: refs/heads/master@{#339894}
Diffstat (limited to 'tools/usb_gadget')
-rw-r--r-- | tools/usb_gadget/gadget.py | 8 | ||||
-rw-r--r-- | tools/usb_gadget/usb_descriptors.py | 28 |
2 files changed, 31 insertions, 5 deletions
diff --git a/tools/usb_gadget/gadget.py b/tools/usb_gadget/gadget.py index abcf491..c290750 100644 --- a/tools/usb_gadget/gadget.py +++ b/tools/usb_gadget/gadget.py @@ -442,11 +442,9 @@ class Gadget(object): elif index not in self._strings[lang]: return None else: - string = self._strings[lang][index].encode('UTF-16LE') - header = struct.pack( - '<BB', 2 + len(string), usb_constants.DescriptorType.STRING) - buf = header + string - return buf[:length] + descriptor = usb_descriptors.StringDescriptor( + bString=self._strings[lang][index]) + return descriptor.Encode()[:length] def GetMicrosoftOSDescriptorV1(self, recipient, value, index, length): """Handle a the Microsoft OS 1.0 Descriptor request from the host. diff --git a/tools/usb_gadget/usb_descriptors.py b/tools/usb_gadget/usb_descriptors.py index f5bf315..1ec5dbd 100644 --- a/tools/usb_gadget/usb_descriptors.py +++ b/tools/usb_gadget/usb_descriptors.py @@ -247,6 +247,34 @@ class DescriptorContainer(Descriptor): for descriptor in self._descriptors)) +class StringDescriptor(Descriptor): + """Standard String Descriptor. + + See Universal Serial Bus Specification Revision 2.0 Table 9-16. + """ + + def __init__(self, **kwargs): + self.bString = kwargs.pop('bString', '') + super(StringDescriptor, self).__init__(**kwargs) + + @property + def total_size(self): + return self.struct_size + len(self.bString.encode('UTF-16LE')) + + def Encode(self): + return ( + super(StringDescriptor, self).Encode() + + self.bString.encode('UTF-16LE')) + + def __str__(self): + return '{}\n bString: "{}"'.format( + super(StringDescriptor, self).__str__(), self.bString) + +StringDescriptor.AddComputedField('bLength', 'B', 'total_size') +StringDescriptor.AddFixedField( + 'bDescriptorType', 'B', usb_constants.DescriptorType.STRING) + + class ConfigurationDescriptor(DescriptorContainer): """Standard Configuration Descriptor. |