8
from ncd_usb import set_relay
10
log = logging.getLogger()
11
logging.basicConfig(level=logging.INFO)
14
class DeviceError(Exception):
18
class TouchDevice(object):
19
def __init__(self, devtype, serial, relay_url=None, bank=None,
20
power_pin=None, volume_down_pin=None, volume_up_pin=None):
21
self.devtype = devtype
23
self.relay_url = relay_url
25
self.power_pin = power_pin
26
self.volume_down_pin = volume_down_pin
27
self.volume_up_pin = volume_up_pin
34
Check adb and fastboot to determine the state a device is in.
35
Possible return values are:
36
device, recovery, unknown, bootloader, disconnected
38
pattern = "{}\t(.+)\n".format(self.serial)
39
adb_devices = subprocess.check_output(['adb', 'devices'])
40
found = re.search(pattern, adb_devices)
42
#Otherwise, check fastboot
43
fastboot_devices = subprocess.check_output(['fastboot', 'devices'])
44
found = re.search(pattern, fastboot_devices)
46
state = found.group(1)
51
def check_adb_shell(self):
52
# Run a quick command in adb to see if the device is responding
53
# subprocess will handle raising an exception if anything
55
subprocess.check_call(['timeout', '10', 'adb', '-s',
56
self.serial, 'shell', 'pwd'])
58
def reimage_from_fastboot(self):
59
#Starting from fastboot mode, put a known-good image on the device
60
log.info("Flashing the last stable image")
61
if self.devtype == "krillin":
62
subprocess.check_output(['ubuntu-device-flash', '--serial',
63
self.serial, '--channel',
64
'ubuntu-touch/ubuntu-rtm/14.09-proposed',
65
'--bootstrap', '--revision=104',
66
'--password', 'ubuntuci'])
68
subprocess.check_output(['ubuntu-device-flash', '--serial',
69
self.serial, '--channel',
70
'ubuntu-touch/stable', '--bootstrap',
71
'--password', 'ubuntuci'])
72
return self.wait_for_device(600)
74
def wait_for_fastboot(self, timeout=120):
80
while waited < timeout:
81
state = self.get_state()
82
if state == 'fastboot':
87
state = self.get_state()
88
if state == 'fastboot':
90
log.error("Timed out waiting for fastboot. Recover device "
92
raise DeviceError("Device in state: {0}, still not available "
93
"after {1} seconds".format(state, timeout))
95
def wait_for_device(self, timeout=120):
96
# Wait for the device to come up to a good/booted state
97
log.info("Waiting for the device to become available")
99
subprocess.check_call(['timeout', str(timeout), 'adb', '-s',
100
self.serial, 'wait-for-device'])
102
log.error("Timed out waiting for device.")
104
dev_state = self.get_state()
105
if dev_state != 'device':
106
log.error("Device in state: {0}, still not available after "
107
"{1} seconds".format(dev_state, timeout))
108
raise DeviceError("Timed out waiting for device to respond after "
109
"{1} seconds".format(dev_state, timeout))
111
log.info("Device is now available")
114
def force_bootloader(self):
115
bootloader_func = getattr(
116
self, '_{}_to_bootloader'.format(self.devtype))
117
if bootloader_func and callable(bootloader_func):
120
raise DeviceError("Full recovery not possible with this device")
122
def _flo_to_bootloader(self):
123
log.info("Forcing the device to enter the bootloader")
124
#Power off the device from any state
125
set_relay(self.relay_url, self.bank, self.power_pin, 1)
127
set_relay(self.relay_url, self.bank, self.power_pin, 0)
129
set_relay(self.relay_url, self.bank, self.volume_down_pin, 1)
130
set_relay(self.relay_url, self.bank, self.power_pin, 1)
132
set_relay(self.relay_url, self.bank, self.power_pin, 0)
134
set_relay(self.relay_url, self.bank, self.volume_down_pin, 0)
136
def _mako_to_bootloader(self):
137
log.info("Forcing the device to enter the bootloader")
138
#Power off the device from any state
139
set_relay(self.relay_url, self.bank, self.power_pin, 1)
141
set_relay(self.relay_url, self.bank, self.power_pin, 0)
143
#Enter the bootloader
144
set_relay(self.relay_url, self.bank, self.volume_down_pin, 1)
145
set_relay(self.relay_url, self.bank, self.power_pin, 1)
147
set_relay(self.relay_url, self.bank, self.volume_down_pin, 0)
148
set_relay(self.relay_url, self.bank, self.power_pin, 0)
150
def _krillin_to_bootloader(self):
151
# On Krillin, the following sequence should take us to fastboot
152
# regardless of the initial state of the device
153
set_relay(self.relay_url, self.bank, self.volume_down_pin, 1)
154
set_relay(self.relay_url, self.bank, self.volume_up_pin, 1)
155
set_relay(self.relay_url, self.bank, self.power_pin, 1)
157
set_relay(self.relay_url, self.bank, self.power_pin, 0)
159
set_relay(self.relay_url, self.bank, self.volume_down_pin, 0)
160
set_relay(self.relay_url, self.bank, self.volume_up_pin, 0)
163
# When looking at the relay webUI for the mapping, we consider all
164
# ports and banks to start numbering from 0
166
"krillin-01": TouchDevice("krillin", "JB011018"),
167
"krillin-02": TouchDevice("krillin", "JB010894"),
168
"krillin-03": TouchDevice("krillin", "JB015156"),
169
"krillin-04": TouchDevice("krillin", "JB006885"),
170
"krillin-05": TouchDevice("krillin", "JB015256"),
171
"krillin-06": TouchDevice("krillin", "JW010687"),
172
"krillin-07": TouchDevice("krillin", "JW011999"),
173
"krillin-08": TouchDevice("krillin", "JW013513"),
174
"krillin-09": TouchDevice("krillin", "JW010053"),
175
"krillin-10": TouchDevice("krillin", "JB012976"),
176
"ps-mako-01": TouchDevice("mako", "0090f741e3d141bc"),
177
"ps-mako-02": TouchDevice("mako", "04ccca120acd4dea"),
178
"ps-mako-03": TouchDevice("mako", "04cb53b598546534"),
179
"ps-mako-04": TouchDevice("mako", "04cbcc545f5328a5"),
180
"mako-01": TouchDevice("mako", "01aa3d7a5dcba4a2"),
181
"mako-02": TouchDevice("mako", "01ade38b552014d4"),
182
"mako-03": TouchDevice("mako", "04c6714ed7c863f2"),
183
"mako-04": TouchDevice("mako", "04df89cf0f9d0933",
184
relay_url="http://qa-relay-control.ubuntu-ci",
185
bank=1, power_pin=4, volume_down_pin=5),
186
"mako-05": TouchDevice("mako", "01b22f82dc5cec63",
187
relay_url="http://10.98.4.100",
188
bank=0, power_pin=0, volume_down_pin=1),
189
"mako-06": TouchDevice("mako", "04ed70928fdc13ba",
190
relay_url="http://10.98.4.100",
191
bank=0, power_pin=2, volume_down_pin=3),
192
"mako-07": TouchDevice("mako", "01e2f64788556934",
193
relay_url="http://10.98.4.100",
194
bank=0, power_pin=4, volume_down_pin=5),
195
"mako-08": TouchDevice("mako", "04ea16a163930769",
196
relay_url="http://10.98.4.100",
197
bank=0, power_pin=6, volume_down_pin=7),
198
"mako-09": TouchDevice("mako", "04fda12ea08fe3c7"),
199
"mako-10": TouchDevice("mako", "01ce848e48dfa6a2"),
200
"mako-11": TouchDevice("mako", "04ed727c929709ba"),
201
#If looking at the LAB wiki page, subtract 1 from the bank and pin numbers
202
#from what it says on the wiki (our numbers start at 0)
203
"mako-12": TouchDevice("mako", "00693fd555c9186a",
204
relay_url="http://qa-relay-control.ubuntu-ci",
205
bank=0, power_pin=1, volume_down_pin=2),
206
"mako-13": TouchDevice("mako", "0084e99c5315731b",
207
relay_url="http://qa-relay-control.ubuntu-ci",
208
bank=0, power_pin=3, volume_down_pin=4),
209
"mako-14": TouchDevice("mako", "007c6d84d348838e",
210
relay_url="http://qa-relay-control.ubuntu-ci",
211
bank=0, power_pin=5, volume_down_pin=6),
212
"mako-15": TouchDevice("mako", "00763b4a61ce0f87",
213
relay_url="http://qa-relay-control.ubuntu-ci",
214
bank=1, power_pin=0, volume_down_pin=1),
215
"mako-16": TouchDevice("mako", "017121eacf5282c4",
216
relay_url="http://qa-relay-control.ubuntu-ci",
217
bank=1, power_pin=2, volume_down_pin=3),
218
#mako-17 has a broken screen but should work, on ashes
219
"mako-17": TouchDevice("mako", "04e0d2f6d3cab77d"),
220
"mako-18": TouchDevice("mako", "027b981a4c1110dd",
221
relay_url="http://10.98.4.100",
222
bank=1, power_pin=0, volume_down_pin=1),
223
"mako-19": TouchDevice("mako", "021c8cdfd5d38602"),
224
"mako-20": TouchDevice("mako", "05083705e0d29402",
225
relay_url="http://10.98.4.100",
226
bank=1, power_pin=2, volume_down_pin=3),
227
"ps-manta-01": TouchDevice("manta", "R32D203DDZR"),
228
"manta-01": TouchDevice("manta", "R32D102RPZL"),
229
"manta-02": TouchDevice("manta", "R32D102RPPK"),
230
"manta-03": TouchDevice("manta", "R32D200N4YH"),
231
"manta-05": TouchDevice("manta", "R32D203DMBY"), # Out of lab for now
232
"flo-01": TouchDevice("flo", "09f306dc"),
233
"flo-02": TouchDevice("flo", "08dbee36"),
234
"flo-03": TouchDevice("flo", "09d55fa8"),
235
"flo-04": TouchDevice("flo", "09e68682"),
236
"flo-05": TouchDevice("flo", "0a22f7cf",
237
relay_url="http://10.98.4.101",
238
bank=0, power_pin=0, volume_down_pin=2),
239
"flo-06": TouchDevice("flo", "08f09bb0"),
243
def get_device(name):
244
# This raises KeyError if we don't have any record of that device