Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 1 | # Copyright (c) 2013 The Chromium OS Authors. All rights reserved. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | # |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 5 | """ This module manages the platform properties in mttools/platforms """ |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 6 | import json |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 7 | import os |
| 8 | import re |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 9 | import sys |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 10 | from xorg_conf import XorgInputClassParser |
| 11 | |
| 12 | # path to current script directory |
| 13 | script_dir = os.path.dirname(os.path.realpath(__file__)) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 14 | platforms_dir = os.path.realpath(os.path.join(script_dir, '..', 'platforms')) |
| 15 | xorg_conf_path = os.path.realpath(os.path.join(script_dir, '..', |
| 16 | '..', 'xorg-conf')) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 17 | |
| 18 | class PlatformProperties(object): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 19 | """ A class containing hardware and xorg properties for a platform. |
| 20 | |
| 21 | The class can be created from an activity log or by providing |
| 22 | the name of the platform. Information will then be read from the |
| 23 | 'platforms_dir' directory. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 24 | """ |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 25 | def __init__(self, platform=None, log=None): |
| 26 | self.required_axis = [] |
| 27 | self.has_axis = [] |
| 28 | self.device_class = "touchpad" |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 29 | if platform: |
| 30 | basename = os.path.join(platforms_dir, platform) |
| 31 | self.name = platform |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 32 | self.hwprops_file = basename + '.hwprops' |
| 33 | self.props_file = basename + '.props' |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 34 | self.xorg_parser = XorgInputClassParser() |
| 35 | self._ParseHWProperties(open(self.hwprops_file).read()) |
| 36 | self._ParseProperties(open(self.props_file).read()) |
| 37 | self._UpdateDimensions() |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 38 | elif log: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 39 | self.name = '' |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 40 | self._ParseActivityLog(log.activity) |
| 41 | if log.evdev: |
| 42 | self._ParseEvdevLog(log.evdev) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 43 | |
| 44 | def _ParseActivityLog(self, activity_data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 45 | """ Parse property information from an activity log """ |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 46 | activity = json.loads(activity_data) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 47 | self.properties = activity['properties'] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 48 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 49 | hwprops = activity['hardwareProperties'] |
| 50 | self.x_min = int(hwprops['left']) |
| 51 | self.x_max = int(hwprops['right']) |
| 52 | self.x_res = int(hwprops['xResolution']) |
| 53 | self.y_min = int(hwprops['top']) |
| 54 | self.y_max = int(hwprops['bottom']) |
| 55 | self.y_res = int(hwprops['yResolution']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 56 | |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 57 | def _ParseEvdevLog(self, evdev_data): |
Dennis Kempin | 143ef16 | 2014-04-09 13:46:50 -0700 | [diff] [blame^] | 58 | # Look for embedded hwproperties in header. Format: |
| 59 | # absinfo: axis min max 0 0 res |
| 60 | abs_regex = 5 * ' ([0-9]+)' |
| 61 | xregex = re.compile('# absinfo: 53' + abs_regex) |
| 62 | xmatch = xregex.search(evdev_data); |
| 63 | self.x_min = int(xmatch.group(1)) |
| 64 | self.x_max = int(xmatch.group(2)) |
| 65 | self.x_res = int(xmatch.group(5)) |
| 66 | |
| 67 | yregex = re.compile('# absinfo: 54' + abs_regex) |
| 68 | ymatch = yregex.search(evdev_data); |
| 69 | self.y_min = int(ymatch.group(1)) |
| 70 | self.y_max = int(ymatch.group(2)) |
| 71 | self.y_res = int(ymatch.group(5)) |
| 72 | |
| 73 | axis_regex = re.compile('# absinfo: ([0-9]+)') |
| 74 | for match in axis_regex.finditer(evdev_data): |
| 75 | self.has_axis.append(int(match.group(1))) |
| 76 | |
| 77 | # look for axes used in the log itself. |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 78 | # The format of ABS (0003) reports is: |
| 79 | # timestamp 0003 axis value |
| 80 | report_regex = re.compile(' 0003 ([0-9a-f]{4}) ([0-9a-f]+)') |
| 81 | for match in report_regex.finditer(evdev_data): |
| 82 | axis = int(match.group(1), 16) |
| 83 | if axis not in self.required_axis: |
| 84 | self.required_axis.append(axis) |
| 85 | |
Dennis Kempin | 143ef16 | 2014-04-09 13:46:50 -0700 | [diff] [blame^] | 86 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 87 | def _ParseHWProperties(self, data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 88 | """ Parse x and y dimensions and resolution from hwprops file """ |
Dennis Kempin | 143ef16 | 2014-04-09 13:46:50 -0700 | [diff] [blame^] | 89 | abs_regex = 5 * ' ([0-9\-]+)' |
| 90 | xregex = re.compile('A: 35' + abs_regex) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 91 | xmatch = xregex.search(data); |
| 92 | self.x_min = int(xmatch.group(1)) |
| 93 | self.x_max = int(xmatch.group(2)) |
| 94 | self.x_res = int(xmatch.group(5)) |
| 95 | |
Dennis Kempin | 143ef16 | 2014-04-09 13:46:50 -0700 | [diff] [blame^] | 96 | yregex = re.compile('A: 36' + abs_regex) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 97 | ymatch = yregex.search(data); |
| 98 | self.y_min = int(ymatch.group(1)) |
| 99 | self.y_max = int(ymatch.group(2)) |
| 100 | self.y_res = int(ymatch.group(5)) |
| 101 | |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 102 | axis_regex = re.compile('A: ([0-9a-f]+)') |
| 103 | for match in axis_regex.finditer(data): |
| 104 | self.has_axis.append(int(match.group(1), 16)) |
| 105 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 106 | def _ParseProperties(self, data): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 107 | """ Parse properties from file and inject xorg properties. """ |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 108 | self.properties = {} |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 109 | self.ignore_properties = [] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 110 | data = json.loads(data) |
| 111 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 112 | if 'gestures' in data: |
| 113 | self.properties.update(data['gestures']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 114 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 115 | if 'device_class' in data: |
| 116 | self.device_class = data['device_class'] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 117 | |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 118 | if 'ignore' in data: |
| 119 | self.ignore_properties.extend(data['ignore']) |
| 120 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 121 | if xorg_conf_path and 'xorg' in data and 'file' in data['xorg']: |
| 122 | filename = os.path.join(xorg_conf_path, data['xorg']['file']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 123 | input_classes = self.xorg_parser.Parse(file=filename) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 124 | if 'identifier' in data['xorg']: |
| 125 | properties = input_classes[data['xorg']['identifier']] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 126 | self.properties.update(properties) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 127 | if 'identifiers' in data['xorg']: |
| 128 | for identifier in data['xorg']['identifiers']: |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 129 | properties = input_classes[identifier] |
| 130 | self.properties.update(properties) |
| 131 | |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 132 | for prop in self.ignore_properties: |
| 133 | if prop in self.properties: |
| 134 | del self.properties[prop] |
| 135 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 136 | def _UpdateDimensions(self): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 137 | """ Update x/y min/max with xorg properties. |
| 138 | |
| 139 | CMT allows hardware properties to be overwritten by xorg properties. |
| 140 | Do the same in this class. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 141 | """ |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 142 | if 'Active Area Left' in self.properties: |
| 143 | self.x_min = int(self.properties['Active Area Left']) |
| 144 | if 'Active Area Right' in self.properties: |
| 145 | self.x_max = int(self.properties['Active Area Right']) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 146 | if 'Active Area Top' in self.properties: |
| 147 | self.y_min = int(self.properties['Active Area Top']) |
| 148 | if 'Active Area Bottom' in self.properties: |
| 149 | self.y_max = int(self.properties['Active Area Bottom']) |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 150 | |
| 151 | if 'Horizontal Resolution' in self.properties: |
| 152 | self.x_res = int(self.properties['Horizontal Resolution']) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 153 | if 'Vertical Resolution' in self.properties: |
| 154 | self.y_res = int(self.properties['Vertical Resolution']) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 155 | |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 156 | if 'SemiMT Non Linear Area Left' in self.properties: |
| 157 | self.x_min = int(self.properties['SemiMT Non Linear Area Left']) |
| 158 | if 'SemiMT Non Linear Area Right' in self.properties: |
| 159 | self.x_max = int(self.properties['SemiMT Non Linear Area Right']) |
| 160 | if 'SemiMT Non Linear Area Top' in self.properties: |
| 161 | self.y_min = int(self.properties['SemiMT Non Linear Area Top']) |
| 162 | if 'SemiMT Non Linear Area Bottom' in self.properties: |
| 163 | self.y_max = int(self.properties['SemiMT Non Linear Area Bottom']) |
| 164 | |
| 165 | |
| 166 | def Match(self, other, loose, debug=False): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 167 | """ Compare properties and return similarity. |
| 168 | |
| 169 | Compare these properties to another PlatformProperties instance. |
| 170 | The return value is a score between 1. 0 meaning there is a big mismatch |
| 171 | and 1 meaning the properties match completely. |
| 172 | Only a selected range of properties are compared in order to |
| 173 | prevent property adjustments to cause platforms to be mismatched. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 174 | """ |
| 175 | scores = [] |
| 176 | def compare(a, b, what): |
| 177 | value = abs(float(a) - float(b)) |
| 178 | if value > 0: |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 179 | value = min(1, value / max(abs(float(a)), abs(float(b)))) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 180 | scores.append(1-value) |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 181 | if debug: |
| 182 | print "%s: %s == %s" % (what, str(a), str(b)) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 183 | def compare_attr(what): |
| 184 | compare(getattr(self, what), getattr(other, what), what) |
| 185 | def compare_prop(what): |
| 186 | if what not in self.properties or what not in other.properties: |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 187 | scores.append(0) |
| 188 | else: |
| 189 | compare(self.properties[what], other.properties[what], what) |
| 190 | def check_axis(required, available): |
| 191 | for axis in required: |
| 192 | if axis not in available: |
| 193 | scores.append(0) |
| 194 | return |
| 195 | |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 196 | compare_attr('x_min') |
| 197 | compare_attr('x_max') |
| 198 | compare_attr('x_res') |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 199 | compare_attr('y_min') |
| 200 | compare_attr('y_max') |
| 201 | compare_attr('y_res') |
| 202 | if not loose: |
| 203 | compare_prop('Pressure Calibration Offset') |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 204 | |
| 205 | if self.required_axis: |
| 206 | if debug: |
| 207 | print "axis:", self.required_axis, "in", other.has_axis |
| 208 | check_axis(self.required_axis, other.has_axis) |
| 209 | |
| 210 | if other.required_axis: |
| 211 | if debug: |
| 212 | print "axis:", other.required_axis, "in", self.has_axis |
| 213 | check_axis(other.required_axis, self.has_axis) |
| 214 | |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 215 | return reduce(lambda x,y: (x * y), scores) |
| 216 | |
| 217 | class PlatformDatabase(object): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 218 | """ Class for managing platforms. |
| 219 | |
| 220 | This class reads all available platforms from the platforms_dir and allows |
| 221 | to search for matching platforms to an activity log file. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 222 | """ |
| 223 | def __init__(self): |
| 224 | platform_files = [ f for f in os.listdir(platforms_dir) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 225 | if f.endswith('.hwprops') ] |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 226 | self.platforms = {} |
| 227 | for file in platform_files: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 228 | name = file.replace('.hwprops', '') |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 229 | self.platforms[name] = PlatformProperties(platform=name) |
| 230 | |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 231 | def FindMatching(self, log, loose=True, debug=False): |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 232 | """ Find platform matching activity_data. |
| 233 | |
| 234 | Returns the PlatformProperties instance of the platform matching |
| 235 | the activity log data. This method might terminate the program in |
| 236 | case no match can be made, or the match is ambiguous. |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 237 | """ |
| 238 | result = None |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 239 | properties = PlatformProperties(log=log) |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 240 | for name, platform in self.platforms.items(): |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 241 | if debug: |
| 242 | print "#" * 10, name |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 243 | score = platform.Match(properties, loose, debug) |
Dennis Kempin | 1a8a5be | 2013-06-18 11:00:02 -0700 | [diff] [blame] | 244 | if debug: |
| 245 | print name, "score =", score |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 246 | if score > 0.9: |
| 247 | if result: |
Dennis Kempin | 55af9cc | 2013-06-20 15:07:21 -0700 | [diff] [blame] | 248 | if loose: |
| 249 | if debug: |
| 250 | print "-" * 10, "Multiple matches. Try strict" |
| 251 | return self.FindMatching(log, False, debug) |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 252 | print ('multiple matching platforms:', result.name, |
| 253 | 'and', platform.name) |
Dennis Kempin | 19e972b | 2013-06-20 13:21:38 -0700 | [diff] [blame] | 254 | return None |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 255 | result = platform |
| 256 | if not result: |
Dennis Kempin | 17766a6 | 2013-06-17 14:09:33 -0700 | [diff] [blame] | 257 | print 'cannot find matching platform' |
Dennis Kempin | 19e972b | 2013-06-20 13:21:38 -0700 | [diff] [blame] | 258 | return None |
Dennis Kempin | 037675e | 2013-06-14 14:12:39 -0700 | [diff] [blame] | 259 | return result |