1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: usbgadget.py 58907 2015-11-29 17:06:00Z vboxsync $
|
---|
3 | # pylint: disable=C0302
|
---|
4 |
|
---|
5 | """
|
---|
6 | VirtualBox USB gadget control class
|
---|
7 | """
|
---|
8 |
|
---|
9 | __copyright__ = \
|
---|
10 | """
|
---|
11 | Copyright (C) 2014-2015 Oracle Corporation
|
---|
12 |
|
---|
13 | This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
14 | available from http://www.virtualbox.org. This file is free software;
|
---|
15 | you can redistribute it and/or modify it under the terms of the GNU
|
---|
16 | General Public License (GPL) as published by the Free Software
|
---|
17 | Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
18 | VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
19 | hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
20 |
|
---|
21 | The contents of this file may alternatively be used under the terms
|
---|
22 | of the Common Development and Distribution License Version 1.0
|
---|
23 | (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
24 | VirtualBox OSE distribution, in which case the provisions of the
|
---|
25 | CDDL are applicable instead of those of the GPL.
|
---|
26 |
|
---|
27 | You may elect to license modified versions of this file under the
|
---|
28 | terms and conditions of either the GPL or the CDDL or both.
|
---|
29 | """
|
---|
30 | __version__ = "$Revision: 58907 $"
|
---|
31 |
|
---|
32 |
|
---|
33 | # Validation Kit imports.
|
---|
34 | import testdriver.txsclient as txsclient;
|
---|
35 | import testdriver.reporter as reporter;
|
---|
36 |
|
---|
37 | ## @name USB gadget type string constants.
|
---|
38 | ## @{
|
---|
39 | g_ksGadgetTypeInvalid = 'Invalid';
|
---|
40 | g_ksGadgetTypeBeaglebone = 'BeagleBone';
|
---|
41 | g_ksGadgetTypeODroidXu3 = 'ODroidXu3';
|
---|
42 | ## @}
|
---|
43 |
|
---|
44 | ## @name USB gadget imeprsonation string constants.
|
---|
45 | ## @{
|
---|
46 | g_ksGadgetImpersonationInvalid = 'Invalid';
|
---|
47 | g_ksGadgetImpersonationTest = 'Test';
|
---|
48 | g_ksGadgetImpersonationMsd = 'Msd';
|
---|
49 | g_ksGadgetImpersonationWebcam = 'Webcam';
|
---|
50 | g_ksGadgetImpersonationEther = 'Ether';
|
---|
51 | ## @}
|
---|
52 |
|
---|
53 | class UsbGadget(object):
|
---|
54 | """
|
---|
55 | USB Gadget control class using the TesteXecService to talk to the external
|
---|
56 | board behaving like a USB device.
|
---|
57 | The board needs to run an embedded Linux system with the TXS service running.
|
---|
58 | """
|
---|
59 |
|
---|
60 | def __init__(self):
|
---|
61 | self.oTxsSession = None;
|
---|
62 | self.sImpersonation = g_ksGadgetImpersonationInvalid;
|
---|
63 | self.sGadgetType = g_ksGadgetTypeInvalid;
|
---|
64 |
|
---|
65 | def _loadModule(self, sModule):
|
---|
66 | """
|
---|
67 | Loads the given module on the USB gadget.
|
---|
68 | Returns True on success.
|
---|
69 | Returns False otherwise.
|
---|
70 | """
|
---|
71 | fRc = False;
|
---|
72 | if self.oTxsSession is not None:
|
---|
73 | fRc = self.oTxsSession.syncExecEx('/usr/bin/modprobe', ('/usr/bin/modprobe', sModule));
|
---|
74 | # For the ODroid-XU3 gadget we have to do a soft connect for the attached host to recognise the device.
|
---|
75 | if self.sGadgetType == g_ksGadgetTypeODroidXu3:
|
---|
76 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
77 | ('/usr/bin/sh', '-c', 'echo connect > /sys/class/udc/12400000.dwc3/soft_connect'));
|
---|
78 | elif self.sGadgetType == g_ksGadgetTypeBeaglebone:
|
---|
79 | # Do a soft disconnect/reconnect cycle or the device will not be recognised as high speed after the first test.
|
---|
80 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
81 | ('/usr/bin/sh', '-c', 'echo disconnect > /sys/class/udc/musb-hdrc.0.auto/soft_connect'));
|
---|
82 | if fRc:
|
---|
83 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
84 | ('/usr/bin/sh', '-c', 'echo connect > /sys/class/udc/musb-hdrc.0.auto/soft_connect'));
|
---|
85 | return fRc;
|
---|
86 |
|
---|
87 | def _unloadModule(self, sModule):
|
---|
88 | """
|
---|
89 | Unloads the given module on the USB gadget.
|
---|
90 | Returns True on success.
|
---|
91 | Returns False otherwise.
|
---|
92 | """
|
---|
93 | fRc = False;
|
---|
94 | if self.oTxsSession is not None:
|
---|
95 | # For the ODroid-XU3 gadget we do a soft disconnect before unloading the gadget driver.
|
---|
96 | if self.sGadgetType == g_ksGadgetTypeODroidXu3:
|
---|
97 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
98 | ('/usr/bin/sh', '-c', 'echo disconnect > /sys/class/udc/12400000.dwc3/soft_connect'));
|
---|
99 | elif self.sGadgetType == g_ksGadgetTypeBeaglebone:
|
---|
100 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
101 | ('/usr/bin/sh', '-c', 'echo disconnect > /sys/class/udc/musb-hdrc.0.auto/soft_connect'));
|
---|
102 |
|
---|
103 | fRc = self.oTxsSession.syncExecEx('/usr/bin/rmmod', ('/usr/bin/rmmod', sModule));
|
---|
104 |
|
---|
105 | return fRc;
|
---|
106 |
|
---|
107 | def _clearImpersonation(self):
|
---|
108 | """
|
---|
109 | Removes the current impersonation of the gadget.
|
---|
110 | """
|
---|
111 | if self.sImpersonation == g_ksGadgetImpersonationInvalid:
|
---|
112 | self._unloadModule('g_zero');
|
---|
113 | self._unloadModule('g_mass_storage');
|
---|
114 | self._unloadModule('g_webcam');
|
---|
115 | self._unloadModule('g_ether');
|
---|
116 | return True;
|
---|
117 | elif self.sImpersonation == g_ksGadgetImpersonationTest:
|
---|
118 | return self._unloadModule('g_zero');
|
---|
119 | elif self.sImpersonation == g_ksGadgetImpersonationMsd:
|
---|
120 | return self._unloadModule('g_mass_storage');
|
---|
121 | elif self.sImpersonation == g_ksGadgetImpersonationWebcam:
|
---|
122 | return self._unloadModule('g_webcam');
|
---|
123 | elif self.sImpersonation == g_ksGadgetImpersonationEther:
|
---|
124 | return self._unloadModule('g_ether');
|
---|
125 | else:
|
---|
126 | reporter.log('Invalid impersonation');
|
---|
127 |
|
---|
128 | return False;
|
---|
129 |
|
---|
130 | def impersonate(self, sImpersonation):
|
---|
131 | """
|
---|
132 | Impersonate a given device.
|
---|
133 | """
|
---|
134 |
|
---|
135 | # Clear any previous impersonation
|
---|
136 | self._clearImpersonation();
|
---|
137 | self.sImpersonation = sImpersonation;
|
---|
138 |
|
---|
139 | if sImpersonation == g_ksGadgetImpersonationInvalid:
|
---|
140 | return False;
|
---|
141 | elif sImpersonation == g_ksGadgetImpersonationTest:
|
---|
142 | return self._loadModule('g_zero');
|
---|
143 | elif sImpersonation == g_ksGadgetImpersonationMsd:
|
---|
144 | # @todo: Not complete
|
---|
145 | return self._loadModule('g_mass_storage');
|
---|
146 | elif sImpersonation == g_ksGadgetImpersonationWebcam:
|
---|
147 | # @todo: Not complete
|
---|
148 | return self._loadModule('g_webcam');
|
---|
149 | elif sImpersonation == g_ksGadgetImpersonationEther:
|
---|
150 | return self._loadModule('g_ether');
|
---|
151 | else:
|
---|
152 | reporter.log('Invalid impersonation');
|
---|
153 |
|
---|
154 | return False;
|
---|
155 |
|
---|
156 | def connectTo(self, cMsTimeout, sGadgetType, sHostname, uPort = None):
|
---|
157 | """
|
---|
158 | Connects to the specified target device.
|
---|
159 | Returns True on Success.
|
---|
160 | Returns False otherwise.
|
---|
161 | """
|
---|
162 | if uPort is None:
|
---|
163 | self.oTxsSession = txsclient.openTcpSession(cMsTimeout, sHostname);
|
---|
164 | else:
|
---|
165 | self.oTxsSession = txsclient.openTcpSession(cMsTimeout, sHostname, uPort = uPort);
|
---|
166 | if self.oTxsSession is None:
|
---|
167 | return False;
|
---|
168 |
|
---|
169 | fDone = self.oTxsSession.waitForTask(30*1000);
|
---|
170 | print 'connect: waitForTask -> %s, result %s' % (fDone, self.oTxsSession.getResult());
|
---|
171 | if fDone is True and self.oTxsSession.isSuccess():
|
---|
172 | fRc = True;
|
---|
173 | else:
|
---|
174 | fRc = False;
|
---|
175 |
|
---|
176 | if fRc is True:
|
---|
177 | self.sGadgetType = sGadgetType;
|
---|
178 |
|
---|
179 | return fRc;
|
---|
180 |
|
---|
181 | def disconnectFrom(self):
|
---|
182 | """
|
---|
183 | Disconnects from the target device.
|
---|
184 | """
|
---|
185 | fRc = True;
|
---|
186 |
|
---|
187 | if self.oTxsSession is not None:
|
---|
188 | self._clearImpersonation();
|
---|
189 | fRc = self.oTxsSession.syncDisconnect();
|
---|
190 |
|
---|
191 | return fRc;
|
---|