1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: usbgadget.py 58937 2015-12-01 13:04:30Z 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: 58937 $"
|
---|
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 | fRc = fRc and self.connectUsb();
|
---|
75 | return fRc;
|
---|
76 |
|
---|
77 | def _unloadModule(self, sModule):
|
---|
78 | """
|
---|
79 | Unloads the given module on the USB gadget.
|
---|
80 | Returns True on success.
|
---|
81 | Returns False otherwise.
|
---|
82 | """
|
---|
83 | fRc = False;
|
---|
84 | if self.oTxsSession is not None:
|
---|
85 | self.disconnectUsb();
|
---|
86 | fRc = self.oTxsSession.syncExecEx('/usr/bin/rmmod', ('/usr/bin/rmmod', sModule));
|
---|
87 |
|
---|
88 | return fRc;
|
---|
89 |
|
---|
90 | def _clearImpersonation(self):
|
---|
91 | """
|
---|
92 | Removes the current impersonation of the gadget.
|
---|
93 | """
|
---|
94 | if self.sImpersonation == g_ksGadgetImpersonationInvalid:
|
---|
95 | self._unloadModule('g_zero');
|
---|
96 | self._unloadModule('g_mass_storage');
|
---|
97 | self._unloadModule('g_webcam');
|
---|
98 | self._unloadModule('g_ether');
|
---|
99 | return True;
|
---|
100 | elif self.sImpersonation == g_ksGadgetImpersonationTest:
|
---|
101 | return self._unloadModule('g_zero');
|
---|
102 | elif self.sImpersonation == g_ksGadgetImpersonationMsd:
|
---|
103 | return self._unloadModule('g_mass_storage');
|
---|
104 | elif self.sImpersonation == g_ksGadgetImpersonationWebcam:
|
---|
105 | return self._unloadModule('g_webcam');
|
---|
106 | elif self.sImpersonation == g_ksGadgetImpersonationEther:
|
---|
107 | return self._unloadModule('g_ether');
|
---|
108 | else:
|
---|
109 | reporter.log('Invalid impersonation');
|
---|
110 |
|
---|
111 | return False;
|
---|
112 |
|
---|
113 | def disconnectUsb(self):
|
---|
114 | """
|
---|
115 | Disconnects the USB gadget from the host. (USB connection not network
|
---|
116 | connection used for control)
|
---|
117 | """
|
---|
118 | if self.sGadgetType == g_ksGadgetTypeODroidXu3:
|
---|
119 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
120 | ('/usr/bin/sh', '-c', 'echo disconnect > /sys/class/udc/12400000.dwc3/soft_connect'));
|
---|
121 | elif self.sGadgetType == g_ksGadgetTypeBeaglebone:
|
---|
122 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
123 | ('/usr/bin/sh', '-c', 'echo disconnect > /sys/class/udc/musb-hdrc.0.auto/soft_connect'));
|
---|
124 | return fRc;
|
---|
125 |
|
---|
126 | def connectUsb(self):
|
---|
127 | """
|
---|
128 | Connect the USB gadget to the host.
|
---|
129 | """
|
---|
130 | if self.sGadgetType == g_ksGadgetTypeODroidXu3:
|
---|
131 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
132 | ('/usr/bin/sh', '-c', 'echo connect > /sys/class/udc/12400000.dwc3/soft_connect'));
|
---|
133 | elif self.sGadgetType == g_ksGadgetTypeBeaglebone:
|
---|
134 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
135 | ('/usr/bin/sh', '-c', 'echo connect > /sys/class/udc/musb-hdrc.0.auto/soft_connect'));
|
---|
136 | return fRc;
|
---|
137 |
|
---|
138 | def impersonate(self, sImpersonation):
|
---|
139 | """
|
---|
140 | Impersonate a given device.
|
---|
141 | """
|
---|
142 |
|
---|
143 | # Clear any previous impersonation
|
---|
144 | self._clearImpersonation();
|
---|
145 | self.sImpersonation = sImpersonation;
|
---|
146 |
|
---|
147 | if sImpersonation == g_ksGadgetImpersonationInvalid:
|
---|
148 | return False;
|
---|
149 | elif sImpersonation == g_ksGadgetImpersonationTest:
|
---|
150 | return self._loadModule('g_zero');
|
---|
151 | elif sImpersonation == g_ksGadgetImpersonationMsd:
|
---|
152 | # @todo: Not complete
|
---|
153 | return self._loadModule('g_mass_storage');
|
---|
154 | elif sImpersonation == g_ksGadgetImpersonationWebcam:
|
---|
155 | # @todo: Not complete
|
---|
156 | return self._loadModule('g_webcam');
|
---|
157 | elif sImpersonation == g_ksGadgetImpersonationEther:
|
---|
158 | return self._loadModule('g_ether');
|
---|
159 | else:
|
---|
160 | reporter.log('Invalid impersonation');
|
---|
161 |
|
---|
162 | return False;
|
---|
163 |
|
---|
164 | def connectTo(self, cMsTimeout, sGadgetType, sHostname, uPort = None):
|
---|
165 | """
|
---|
166 | Connects to the specified target device.
|
---|
167 | Returns True on Success.
|
---|
168 | Returns False otherwise.
|
---|
169 | """
|
---|
170 | if uPort is None:
|
---|
171 | self.oTxsSession = txsclient.openTcpSession(cMsTimeout, sHostname);
|
---|
172 | else:
|
---|
173 | self.oTxsSession = txsclient.openTcpSession(cMsTimeout, sHostname, uPort = uPort);
|
---|
174 | if self.oTxsSession is None:
|
---|
175 | return False;
|
---|
176 |
|
---|
177 | fDone = self.oTxsSession.waitForTask(30*1000);
|
---|
178 | print 'connect: waitForTask -> %s, result %s' % (fDone, self.oTxsSession.getResult());
|
---|
179 | if fDone is True and self.oTxsSession.isSuccess():
|
---|
180 | fRc = True;
|
---|
181 | else:
|
---|
182 | fRc = False;
|
---|
183 |
|
---|
184 | if fRc is True:
|
---|
185 | self.sGadgetType = sGadgetType;
|
---|
186 |
|
---|
187 | return fRc;
|
---|
188 |
|
---|
189 | def disconnectFrom(self):
|
---|
190 | """
|
---|
191 | Disconnects from the target device.
|
---|
192 | """
|
---|
193 | fRc = True;
|
---|
194 |
|
---|
195 | if self.oTxsSession is not None:
|
---|
196 | self._clearImpersonation();
|
---|
197 | fRc = self.oTxsSession.syncDisconnect();
|
---|
198 |
|
---|
199 | return fRc;
|
---|