1 | # -*- coding: utf-8 -*-
|
---|
2 | # $Id: usbgadget.py 56295 2015-06-09 14:29:55Z 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: 56295 $"
|
---|
31 |
|
---|
32 |
|
---|
33 | # Validation Kit imports.
|
---|
34 | import testdriver.txsclient as txsclient;
|
---|
35 | import testdriver.reporter as reporter;
|
---|
36 |
|
---|
37 | class UsbGadget(object):
|
---|
38 | """
|
---|
39 | USB Gadget control class using the TesteXecService to talk to the external
|
---|
40 | board behaving like a USB device.
|
---|
41 | The board needs to run an embedded Linux system with the TXS service running.
|
---|
42 | """
|
---|
43 |
|
---|
44 | def __init__(self):
|
---|
45 | self.oTxsSession = None;
|
---|
46 | self.sImpersonation = 'Invalid';
|
---|
47 | self.sGadgetType = 'Invalid';
|
---|
48 |
|
---|
49 | def _loadModule(self, sModule):
|
---|
50 | """
|
---|
51 | Loads the given module on the USB gadget.
|
---|
52 | Returns True on success.
|
---|
53 | Returns False otherwise.
|
---|
54 | """
|
---|
55 | fRc = False;
|
---|
56 | if self.oTxsSession is not None:
|
---|
57 | fRc = self.oTxsSession.syncExecEx('/usr/bin/modprobe', ('/usr/bin/modprobe', sModule));
|
---|
58 | # For the ODroid-XU3 gadget we have to do a soft connect for the attached host to recognise the device.
|
---|
59 | if self.sGadgetType == 'ODroid-XU3':
|
---|
60 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
61 | ('/usr/bin/sh', '-c', 'echo connect > /sys/class/udc/12400000.dwc3/soft_connect'));
|
---|
62 |
|
---|
63 | return fRc;
|
---|
64 |
|
---|
65 | def _unloadModule(self, sModule):
|
---|
66 | """
|
---|
67 | Unloads 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 | # For the ODroid-XU3 gadget we do a soft disconnect before unloading the gadget driver.
|
---|
74 | if self.sGadgetType == 'ODroid-XU3':
|
---|
75 | fRc = self.oTxsSession.syncExecEx('/usr/bin/sh', \
|
---|
76 | ('/usr/bin/sh', '-c', 'echo disconnect > /sys/class/udc/12400000.dwc3/soft_connect'));
|
---|
77 | fRc = self.oTxsSession.syncExecEx('/usr/bin/rmmod', ('/usr/bin/rmmod', sModule));
|
---|
78 |
|
---|
79 | return fRc;
|
---|
80 |
|
---|
81 | def _clearImpersonation(self):
|
---|
82 | """
|
---|
83 | Removes the current impersonation of the gadget.
|
---|
84 | """
|
---|
85 | if self.sImpersonation == 'Invalid':
|
---|
86 | self._unloadModule('g_zero');
|
---|
87 | self._unloadModule('g_mass_storage');
|
---|
88 | self._unloadModule('g_webcam');
|
---|
89 | self._unloadModule('g_ether');
|
---|
90 | return True;
|
---|
91 | elif self.sImpersonation == 'Test':
|
---|
92 | return self._unloadModule('g_zero');
|
---|
93 | elif self.sImpersonation == 'Msd':
|
---|
94 | return self._unloadModule('g_mass_storage');
|
---|
95 | elif self.sImpersonation == 'Webcam':
|
---|
96 | return self._unloadModule('g_webcam');
|
---|
97 | elif self.sImpersonation == 'Network':
|
---|
98 | return self._unloadModule('g_ether');
|
---|
99 | else:
|
---|
100 | reporter.log('Invalid impersonation');
|
---|
101 |
|
---|
102 | return False;
|
---|
103 |
|
---|
104 | def impersonate(self, sImpersonation):
|
---|
105 | """
|
---|
106 | Impersonate a given device.
|
---|
107 | """
|
---|
108 |
|
---|
109 | # Clear any previous impersonation
|
---|
110 | self._clearImpersonation();
|
---|
111 | self.sImpersonation = sImpersonation;
|
---|
112 |
|
---|
113 | if sImpersonation == 'Invalid':
|
---|
114 | return False;
|
---|
115 | elif sImpersonation == 'Test':
|
---|
116 | return self._loadModule('g_zero');
|
---|
117 | elif sImpersonation == 'Msd':
|
---|
118 | # @todo: Not complete
|
---|
119 | return self._loadModule('g_mass_storage');
|
---|
120 | elif sImpersonation == 'Webcam':
|
---|
121 | # @todo: Not complete
|
---|
122 | return self._loadModule('g_webcam');
|
---|
123 | elif sImpersonation == 'Network':
|
---|
124 | return self._loadModule('g_ether');
|
---|
125 | else:
|
---|
126 | reporter.log('Invalid impersonation');
|
---|
127 |
|
---|
128 | return False;
|
---|
129 |
|
---|
130 | def connectTo(self, cMsTimeout, sGadgetType, sHostname, uPort = None):
|
---|
131 | """
|
---|
132 | Connects to the specified target device.
|
---|
133 | Returns True on Success.
|
---|
134 | Returns False otherwise.
|
---|
135 | """
|
---|
136 | if uPort is None:
|
---|
137 | self.oTxsSession = txsclient.openTcpSession(cMsTimeout, sHostname);
|
---|
138 | else:
|
---|
139 | self.oTxsSession = txsclient.openTcpSession(cMsTimeout, sHostname, uPort = uPort);
|
---|
140 | if self.oTxsSession is None:
|
---|
141 | return False;
|
---|
142 |
|
---|
143 | fDone = self.oTxsSession.waitForTask(30*1000);
|
---|
144 | print 'connect: waitForTask -> %s, result %s' % (fDone, self.oTxsSession.getResult());
|
---|
145 | if fDone is True and self.oTxsSession.isSuccess():
|
---|
146 | fRc = True;
|
---|
147 | else:
|
---|
148 | fRc = False;
|
---|
149 |
|
---|
150 | if fRc is True:
|
---|
151 | self.sGadgetType = sGadgetType;
|
---|
152 |
|
---|
153 | return fRc;
|
---|
154 |
|
---|
155 | def disconnectFrom(self):
|
---|
156 | """
|
---|
157 | Disconnects from the target device.
|
---|
158 | """
|
---|
159 | fRc = True;
|
---|
160 |
|
---|
161 | if self.oTxsSession is not None:
|
---|
162 | self._clearImpersonation();
|
---|
163 | fRc = self.oTxsSession.syncDisconnect();
|
---|
164 |
|
---|
165 | return fRc;
|
---|