~nuclearbob/ubuntu-test-cases/bootspeed-nfss

« back to all changes in this revision

Viewing changes to scripts/device_info.py

MergedĀ upstreamĀ changes

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#!/usr/bin/env python
2
2
 
 
3
import logging
3
4
import re
4
5
import subprocess
 
6
import time
 
7
 
 
8
from ncd_usb import set_relay
 
9
 
 
10
log = logging.getLogger()
 
11
logging.basicConfig(level=logging.INFO)
 
12
 
 
13
 
 
14
class DeviceError(Exception):
 
15
    pass
5
16
 
6
17
 
7
18
class TouchDevice(object):
8
19
    def __init__(self, devtype, serial, relay_url=None, bank=None,
9
 
                 power_pin=None, volume_pin=None):
 
20
                 power_pin=None, volume_down_pin=None, volume_up_pin=None):
10
21
        self.devtype = devtype
11
22
        self.serial = serial
12
23
        self.relay_url = relay_url
13
24
        self.bank = bank
14
25
        self.power_pin = power_pin
15
 
        self.volume_pin = volume_pin
 
26
        self.volume_down_pin = volume_down_pin
 
27
        self.volume_up_pin = volume_up_pin
 
28
 
 
29
    def get_serial(self):
 
30
        return self.serial
 
31
 
 
32
    def get_state(self):
 
33
        """
 
34
        Check adb and fastboot to determine the state a device is in.
 
35
        Possible return values are:
 
36
            device, recovery, unknown, bootloader, disconnected
 
37
        """
 
38
        pattern = "{}\t(.+)\n".format(self.serial)
 
39
        adb_devices = subprocess.check_output(['adb', 'devices'])
 
40
        found = re.search(pattern, adb_devices)
 
41
        if not found:
 
42
            #Otherwise, check fastboot
 
43
            fastboot_devices = subprocess.check_output(['fastboot', 'devices'])
 
44
            found = re.search(pattern, fastboot_devices)
 
45
        if found:
 
46
            state = found.group(1)
 
47
            return state
 
48
        else:
 
49
            return 'disconnected'
 
50
 
 
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
 
54
        # goes wrong
 
55
        subprocess.check_call(['timeout', '10', 'adb', '-s',
 
56
                               self.serial, 'shell', 'pwd'])
 
57
 
 
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'])
 
67
        else:
 
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)
 
73
 
 
74
    def wait_for_fastboot(self, timeout=120):
 
75
        if timeout > 10:
 
76
            wait = 10
 
77
        else:
 
78
            wait = timeout
 
79
        waited = 0
 
80
        while waited < timeout:
 
81
            state = self.get_state()
 
82
            if state == 'fastboot':
 
83
                return 0
 
84
            time.sleep(wait)
 
85
            waited += wait
 
86
        else:
 
87
            state = self.get_state()
 
88
            if state == 'fastboot':
 
89
                return 0
 
90
            log.error("Timed out waiting for fastboot. Recover device "
 
91
                      "manually")
 
92
            raise DeviceError("Device in state: {0}, still not available "
 
93
                              "after {1} seconds".format(state, timeout))
 
94
 
 
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")
 
98
        try:
 
99
            subprocess.check_call(['timeout', str(timeout), 'adb', '-s',
 
100
                                  self.serial, 'wait-for-device'])
 
101
        except:
 
102
            log.error("Timed out waiting for device.")
 
103
            raise
 
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))
 
110
        else:
 
111
            log.info("Device is now available")
 
112
            return 0
 
113
 
 
114
    def force_bootloader(self):
 
115
        bootloader_func = getattr(
 
116
            self, '_{}_to_bootloader'.format(self.devtype))
 
117
        if bootloader_func and callable(bootloader_func):
 
118
            bootloader_func()
 
119
        else:
 
120
            raise DeviceError("Full recovery not possible with this device")
 
121
 
 
122
    def _mako_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)
 
126
        time.sleep(10)
 
127
        set_relay(self.relay_url, self.bank, self.power_pin, 0)
 
128
        time.sleep(10)
 
129
        #Enter the bootloader
 
130
        set_relay(self.relay_url, self.bank, self.volume_down_pin, 1)
 
131
        set_relay(self.relay_url, self.bank, self.power_pin, 1)
 
132
        time.sleep(5)
 
133
        set_relay(self.relay_url, self.bank, self.volume_down_pin, 0)
 
134
        set_relay(self.relay_url, self.bank, self.power_pin, 0)
 
135
 
 
136
    def _krillin_to_bootloader(self):
 
137
        # On Krillin, the following sequence should take us to fastboot
 
138
        # regardless of the initial state of the device
 
139
        set_relay(self.relay_url, self.bank, self.volume_down_pin, 1)
 
140
        set_relay(self.relay_url, self.bank, self.volume_up_pin, 1)
 
141
        set_relay(self.relay_url, self.bank, self.power_pin, 1)
 
142
        time.sleep(15)
 
143
        set_relay(self.relay_url, self.bank, self.power_pin, 0)
 
144
        time.sleep(6)
 
145
        set_relay(self.relay_url, self.bank, self.volume_down_pin, 0)
 
146
        set_relay(self.relay_url, self.bank, self.volume_up_pin, 0)
 
147
 
16
148
 
17
149
# When looking at the relay webUI for the mapping, we consider all
18
150
# ports and banks to start numbering from 0
36
168
    "mako-03": TouchDevice("mako", "04c6714ed7c863f2"),
37
169
    "mako-04": TouchDevice("mako", "04df89cf0f9d0933",
38
170
                           relay_url="http://qa-relay-control.ubuntu-ci",
39
 
                           bank=1, power_pin=4, volume_pin=5),
 
171
                           bank=1, power_pin=4, volume_down_pin=5),
40
172
    "mako-05": TouchDevice("mako", "01b22f82dc5cec63",
41
173
                           relay_url="http://10.98.4.100",
42
 
                           bank=0, power_pin=0, volume_pin=1),
 
174
                           bank=0, power_pin=0, volume_down_pin=1),
43
175
    "mako-06": TouchDevice("mako", "04ed70928fdc13ba",
44
176
                           relay_url="http://10.98.4.100",
45
 
                           bank=0, power_pin=2, volume_pin=3),
 
177
                           bank=0, power_pin=2, volume_down_pin=3),
46
178
    "mako-07": TouchDevice("mako", "01e2f64788556934",
47
179
                           relay_url="http://10.98.4.100",
48
 
                           bank=0, power_pin=4, volume_pin=5),
 
180
                           bank=0, power_pin=4, volume_down_pin=5),
49
181
    "mako-08": TouchDevice("mako", "04ea16a163930769",
50
182
                           relay_url="http://10.98.4.100",
51
 
                           bank=0, power_pin=6, volume_pin=7),
 
183
                           bank=0, power_pin=6, volume_down_pin=7),
52
184
    "mako-09": TouchDevice("mako", "04fda12ea08fe3c7"),
53
185
    "mako-10": TouchDevice("mako", "01ce848e48dfa6a2"),
54
186
    "mako-11": TouchDevice("mako", "04ed727c929709ba"),
56
188
    #from what it says on the wiki (our numbers start at 0)
57
189
    "mako-12": TouchDevice("mako", "00693fd555c9186a",
58
190
                           relay_url="http://qa-relay-control.ubuntu-ci",
59
 
                           bank=0, power_pin=1, volume_pin=2),
 
191
                           bank=0, power_pin=1, volume_down_pin=2),
60
192
    "mako-13": TouchDevice("mako", "0084e99c5315731b",
61
193
                           relay_url="http://qa-relay-control.ubuntu-ci",
62
 
                           bank=0, power_pin=3, volume_pin=4),
 
194
                           bank=0, power_pin=3, volume_down_pin=4),
63
195
    "mako-14": TouchDevice("mako", "007c6d84d348838e",
64
196
                           relay_url="http://qa-relay-control.ubuntu-ci",
65
 
                           bank=0, power_pin=5, volume_pin=6),
 
197
                           bank=0, power_pin=5, volume_down_pin=6),
66
198
    "mako-15": TouchDevice("mako", "00763b4a61ce0f87",
67
199
                           relay_url="http://qa-relay-control.ubuntu-ci",
68
 
                           bank=1, power_pin=0, volume_pin=1),
 
200
                           bank=1, power_pin=0, volume_down_pin=1),
69
201
    "mako-16": TouchDevice("mako", "017121eacf5282c4",
70
202
                           relay_url="http://qa-relay-control.ubuntu-ci",
71
 
                           bank=1, power_pin=2, volume_pin=3),
 
203
                           bank=1, power_pin=2, volume_down_pin=3),
72
204
    #mako-17 has a broken screen but should work, on ashes
73
205
    "mako-17": TouchDevice("mako", "04e0d2f6d3cab77d"),
74
206
    "mako-18": TouchDevice("mako", "027b981a4c1110dd",
75
207
                           relay_url="http://10.98.4.100",
76
 
                           bank=1, power_pin=0, volume_pin=1),
 
208
                           bank=1, power_pin=0, volume_down_pin=1),
77
209
    "mako-19": TouchDevice("mako", "021c8cdfd5d38602"),
78
210
    "mako-20": TouchDevice("mako", "05083705e0d29402",
79
211
                           relay_url="http://10.98.4.100",
80
 
                           bank=1, power_pin=2, volume_pin=3),
 
212
                           bank=1, power_pin=2, volume_down_pin=3),
81
213
    "ps-manta-01": TouchDevice("manta", "R32D203DDZR"),
82
214
    "manta-01": TouchDevice("manta", "R32D102RPZL"),
83
215
    "manta-02": TouchDevice("manta", "R32D102RPPK"),
92
224
}
93
225
 
94
226
 
95
 
def get_state(serial):
96
 
    """
97
 
    Check adb and fastboot to determine the state a device is in.
98
 
    Possible return values are:
99
 
        device, recovery, unknown, bootloader, disconnected
100
 
    """
101
 
    pattern = "{}\t(.+)\n".format(serial)
102
 
    adb_devices = subprocess.check_output(['adb', 'devices'])
103
 
    found = re.search(pattern, adb_devices)
104
 
    if not found:
105
 
        #Otherwise, check fastboot
106
 
        fastboot_devices = subprocess.check_output(['fastboot', 'devices'])
107
 
        found = re.search(pattern, fastboot_devices)
108
 
    if found:
109
 
        state = found.group(1)
110
 
        return state
111
 
    else:
112
 
        return 'disconnected'
113
 
 
114
 
 
115
 
def get_serial(name):
116
 
    return DEVICES.get(name).serial
117
 
 
118
 
 
119
 
def get_power(name):
120
 
    device = DEVICES.get(name)
121
 
    return (device.relay_url, device.bank, device.power_pin,
122
 
            device.volume_pin)
 
227
def get_device(name):
 
228
    # This raises KeyError if we don't have any record of that device
 
229
    return DEVICES[name]