Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 1 | """Functions related to hardware features. |
| 2 | |
| 3 | See proto definitions for descriptions of arguments. |
| 4 | """ |
| 5 | |
| 6 | # Needed to load from @proto. Add @unused to silence lint. |
| 7 | load("//config/util/bindings/proto.star", "protos") |
| 8 | load( |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 9 | "@proto//chromiumos/config/api/component.proto", |
| 10 | comp_pb = "chromiumos.config.api", |
| 11 | ) |
| 12 | load( |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 13 | "@proto//chromiumos/config/api/topology.proto", |
| 14 | topo_pb = "chromiumos.config.api", |
| 15 | ) |
| 16 | |
| 17 | _HW_FEAT = topo_pb.HardwareFeatures |
| 18 | |
| 19 | _PRESENT = struct( |
| 20 | UNKNOWN = topo_pb.HardwareFeatures.PRESENT_UNKNOWN, |
| 21 | PRESENT = topo_pb.HardwareFeatures.PRESENT, |
| 22 | NOT_PRESENT = topo_pb.HardwareFeatures.NOT_PRESENT, |
| 23 | ) |
| 24 | |
| 25 | def _bool_to_present(value): |
| 26 | """Returns correct value of present enum depending on value""" |
| 27 | if value == None: |
| 28 | return _PRESENT.UNKNOWN |
| 29 | elif value: |
| 30 | return _PRESENT.PRESENT |
| 31 | else: |
| 32 | return _PRESENT.NOT_PRESENT |
| 33 | |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 34 | def _create_bluetooth(present = True): |
| 35 | """Specify whether bluetooth is present""" |
| 36 | return _HW_FEAT( |
| 37 | bluetooth = _HW_FEAT.Bluetooth( |
| 38 | present = _bool_to_present(present), |
| 39 | ), |
| 40 | ) |
| 41 | |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 42 | def _create_hotwording(supported = True): |
| 43 | """Specify whether hotwording is supported""" |
| 44 | return _HW_FEAT( |
| 45 | hotwording = _HW_FEAT.Hotwording( |
| 46 | present = _bool_to_present(supported), |
| 47 | ), |
| 48 | ) |
| 49 | |
| 50 | def _create_display(internal, external): |
| 51 | """Specify type of display support present.""" |
| 52 | types = { |
| 53 | (False, False): _HW_FEAT.Display.TYPE_UNKNOWN, |
| 54 | (True, False): _HW_FEAT.Display.TYPE_INTERNAL, |
| 55 | (False, True): _HW_FEAT.Display.TYPE_EXTERNAL, |
| 56 | (True, True): _HW_FEAT.Display.TYPE_INTERNAL_EXTERNAL, |
| 57 | } |
| 58 | |
| 59 | return _HW_FEAT( |
| 60 | display = _HW_FEAT.Display( |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 61 | type = types[(internal, external)], |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 62 | ), |
| 63 | ) |
| 64 | |
| 65 | _FORM_FACTOR = struct( |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 66 | CLAMSHELL = _HW_FEAT.FormFactor.CLAMSHELL, |
| 67 | CONVERTIBLE = _HW_FEAT.FormFactor.CONVERTIBLE, |
| 68 | DETACHABLE = _HW_FEAT.FormFactor.DETACHABLE, |
| 69 | CHROMEBASE = _HW_FEAT.FormFactor.CHROMEBASE, |
| 70 | CHROMEBOX = _HW_FEAT.FormFactor.CHROMEBOX, |
| 71 | CHROMEBIT = _HW_FEAT.FormFactor.CHROMEBIT, |
| 72 | CHROMESLATE = _HW_FEAT.FormFactor.CHROMESLATE, |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 73 | ) |
| 74 | |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 75 | _EC = struct( |
| 76 | CHROME = _HW_FEAT.EmbeddedController.EC_CHROME, |
| 77 | WILCO = _HW_FEAT.EmbeddedController.EC_WILCO, |
| 78 | ) |
| 79 | |
| 80 | def _create_ec(ec_type, present = True): |
| 81 | """Specify the embedded controller type.""" |
| 82 | return _HW_FEAT( |
| 83 | embedded_controller = _HW_FEAT.EmbeddedController( |
| 84 | present = _bool_to_present(present), |
| 85 | ec_type = ec_type, |
| 86 | ), |
| 87 | ) |
| 88 | |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 89 | _LOCATION = struct( |
| 90 | SCREEN_TOP_LEFT = _HW_FEAT.Fingerprint.POWER_BUTTON_TOP_LEFT, |
| 91 | KEYBOARD_BOTTOM_LEFT = _HW_FEAT.Fingerprint.KEYBOARD_BOTTOM_LEFT, |
| 92 | KEYBOARD_BOTTOM_RIGHT = _HW_FEAT.Fingerprint.KEYBOARD_BOTTOM_RIGHT, |
| 93 | KEYBOARD_TOP_RIGHT = _HW_FEAT.Fingerprint.KEYBOARD_TOP_RIGHT, |
| 94 | PRESENT = _HW_FEAT.Fingerprint.PRESENT, |
| 95 | NOT_PRESENT = _HW_FEAT.Fingerprint.NOT_PRESENT, |
| 96 | SIDE_RIGHT = _HW_FEAT.Fingerprint.RIGHT_SIDE, |
| 97 | SIDE_LEFT = _HW_FEAT.Fingerprint.LEFT_SIDE, |
| 98 | ) |
| 99 | |
| 100 | def _create_fingerprint(location, board = "", ro_version = ""): |
| 101 | """Specify fingerprint settings""" |
| 102 | return _HW_FEAT( |
| 103 | fingerprint = _HW_FEAT.Fingerprint( |
| 104 | location = location, |
| 105 | board = board, |
| 106 | ro_version = ro_version, |
| 107 | ), |
| 108 | ) |
| 109 | |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 110 | def _create_form_factor(form_factor): |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 111 | """Specify the form factor as a HardwareFeature.""" |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 112 | return _HW_FEAT( |
| 113 | form_factor = _HW_FEAT.FormFactor( |
| 114 | form_factor = form_factor, |
| 115 | ), |
| 116 | ) |
| 117 | |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 118 | _KB_TYPE = struct( |
| 119 | NONE = _HW_FEAT.Keyboard.NONE, |
| 120 | INTERNAL = _HW_FEAT.Keyboard.INTERNAL, |
| 121 | DETACHABLE = _HW_FEAT.Keyboard.DETACHABLE, |
| 122 | ) |
| 123 | |
| 124 | def _create_keyboard( |
| 125 | kb_type, |
| 126 | backlight = False, |
| 127 | pwr_btn_present = False, |
| 128 | numpad_present = False): |
| 129 | """Builds a Topology proto for a keyboard. |
| 130 | |
| 131 | Args: |
| 132 | kb_type: A KeyboardType enum. Required. |
| 133 | backlight: True if a backlight is present. Required. |
| 134 | pwr_btn_present: True if a power button is present. Required. |
| 135 | numpad_present: True if numeric pad is present. |
| 136 | """ |
| 137 | return _HW_FEAT( |
| 138 | keyboard = _HW_FEAT.Keyboard( |
| 139 | keyboard_type = kb_type, |
| 140 | backlight = _bool_to_present(backlight), |
| 141 | power_button = _bool_to_present(pwr_btn_present), |
| 142 | numeric_pad = _bool_to_present(numpad_present), |
| 143 | ), |
| 144 | ) |
| 145 | |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 146 | _STORAGE = struct( |
| 147 | EMMC = comp_pb.Component.Storage.EMMC, |
| 148 | NVME = comp_pb.Component.Storage.NVME, |
| 149 | SATA = comp_pb.Component.Storage.SATA, |
| 150 | ) |
| 151 | |
| 152 | def _create_storage(type): |
| 153 | """Specify storage type.""" |
| 154 | return _HW_FEAT( |
| 155 | storage = _HW_FEAT.Storage( |
| 156 | storage_type = type, |
| 157 | ), |
| 158 | ) |
| 159 | |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 160 | def _create_screen( |
| 161 | touch = False, |
| 162 | inches = 0, |
| 163 | width_px = None, |
| 164 | height_px = None, |
| 165 | pixels_per_in = None): |
| 166 | """Specify features of screen""" |
| 167 | |
| 168 | return _HW_FEAT( |
| 169 | screen = _HW_FEAT.Screen( |
| 170 | touch_support = _bool_to_present(touch), |
| 171 | panel_properties = comp_pb.Component.DisplayPanel.Properties( |
| 172 | diagonal_milliinch = inches * 1000, |
| 173 | width_px = width_px, |
| 174 | height_px = height_px, |
| 175 | pixels_per_in = pixels_per_in, |
| 176 | ), |
| 177 | ), |
| 178 | ) |
| 179 | |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 180 | def _create_touchpad(present = True): |
| 181 | """Specify whether touchpad is present.""" |
| 182 | return _HW_FEAT( |
| 183 | touchpad = _HW_FEAT.Touchpad( |
| 184 | present = _bool_to_present(present), |
| 185 | ), |
| 186 | ) |
| 187 | |
| 188 | # build struct of enums to configure camera |
| 189 | _camera_features = struct( |
| 190 | facing = struct( |
| 191 | UNKNOWN = _HW_FEAT.Camera.FACING_UNKNOWN, |
| 192 | FRONT = _HW_FEAT.Camera.FACING_FRONT, |
| 193 | BACK = _HW_FEAT.Camera.FACING_BACK, |
| 194 | ), |
| 195 | interface = struct( |
| 196 | UNKNOWN = _HW_FEAT.Camera.INTERFACE_UNKNOWN, |
| 197 | USB = _HW_FEAT.Camera.INTERFACE_USB, |
| 198 | MIPI = _HW_FEAT.Camera.INTERFACE_MIPI, |
| 199 | ), |
| 200 | orientation = struct( |
| 201 | UNKNOWN = _HW_FEAT.Camera.ORIENTATION_UNKNOWN, |
| 202 | _0_DEG = _HW_FEAT.Camera.ORIENTATION_0, |
| 203 | _90_DEG = _HW_FEAT.Camera.ORIENTATION_90, |
| 204 | _180_DEG = _HW_FEAT.Camera.ORIENTATION_180, |
| 205 | _270_DEG = _HW_FEAT.Camera.ORIENTATION_270, |
| 206 | ), |
| 207 | ) |
| 208 | |
| 209 | def _create_camera( |
| 210 | facing = _camera_features.facing.UNKNOWN, |
| 211 | flags = 0, |
| 212 | ids = [], |
| 213 | interface = _camera_features.interface.UNKNOWN, |
| 214 | orientation = _camera_features.orientation.UNKNOWN, |
| 215 | privacy_switch = None): |
| 216 | """Take camera features and create a Camera.Device instance. |
| 217 | |
| 218 | Pass one or more of these instances to create_cameras() to build |
| 219 | a full HardwareFeature proto from them. |
| 220 | """ |
| 221 | return _HW_FEAT.Camera.Device( |
| 222 | facing = facing, |
| 223 | flags = flags, |
| 224 | ids = ids, |
| 225 | interface = interface, |
| 226 | orientation = orientation, |
| 227 | privacy_switch = _bool_to_present(privacy_switch), |
| 228 | ) |
| 229 | |
| 230 | def _create_cameras(*args): |
| 231 | """Take one or more cameras and create HardwareFeature from them.""" |
| 232 | return _HW_FEAT( |
| 233 | camera = _HW_FEAT.Camera( |
| 234 | devices = args, |
| 235 | ), |
| 236 | ) |
| 237 | |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 238 | _STYLUS_TYPE = struct( |
| 239 | NONE = _HW_FEAT.Stylus.NONE, |
| 240 | INTERNAL = _HW_FEAT.Stylus.INTERNAL, |
| 241 | EXTERNAL = _HW_FEAT.Stylus.EXTERNAL, |
| 242 | ) |
| 243 | |
| 244 | def _create_stylus(stylus_type): |
| 245 | """Create stylus feature.""" |
| 246 | return _HW_FEAT( |
| 247 | stylus = _HW_FEAT.Stylus( |
| 248 | stylus = stylus_type, |
| 249 | ), |
| 250 | ) |
| 251 | |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 252 | def _create_features( |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 253 | bluetooth = None, |
| 254 | camera = None, |
| 255 | display = None, |
| 256 | ec = _create_ec(_EC.CHROME), # non-chrome ECs are very rare |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 257 | fingerprint = None, |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 258 | form_factor = None, |
| 259 | hotwording = None, |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 260 | keyboard = None, |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 261 | screen = None, |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 262 | storage = None, |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 263 | stylus = None, |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 264 | touchpad = None): |
| 265 | hw_feat = {} |
| 266 | |
| 267 | def _merge(name, feature): |
| 268 | if feature: |
| 269 | hw_feat[name] = getattr(feature, name) |
| 270 | |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 271 | _merge("bluetooth", bluetooth) |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 272 | _merge("camera", camera) |
| 273 | _merge("display", display) |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 274 | _merge("embedded_controller", ec) |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 275 | _merge("fingerprint", fingerprint) |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 276 | _merge("form_factor", form_factor) |
| 277 | _merge("hotwording", hotwording) |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 278 | _merge("keyboard", keyboard) |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 279 | _merge("screen", screen) |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 280 | _merge("storage", storage) |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 281 | _merge("stylus", stylus) |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 282 | _merge("touchpad", touchpad) |
| 283 | |
| 284 | return _HW_FEAT(**hw_feat) |
| 285 | |
| 286 | hw_feat = struct( |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 287 | create_bluetooth = _create_bluetooth, |
| 288 | create_camera = _create_camera, |
| 289 | create_cameras = _create_cameras, |
| 290 | create_display = _create_display, |
| 291 | create_ec = _create_ec, |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 292 | create_fingerprint = _create_fingerprint, |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 293 | create_features = _create_features, |
| 294 | create_form_factor = _create_form_factor, |
| 295 | create_hotwording = _create_hotwording, |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 296 | create_keyboard = _create_keyboard, |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 297 | create_screen = _create_screen, |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 298 | create_storage = _create_storage, |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 299 | create_stylus = _create_stylus, |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 300 | create_touchpad = _create_touchpad, |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 301 | |
| 302 | # export enums |
| 303 | camera_features = _camera_features, |
| 304 | embedded_controller = _EC, |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 305 | form_factor = _FORM_FACTOR, |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 306 | keyboard_type = _KB_TYPE, |
Sean McAllister | 9ecb21e | 2021-04-27 15:57:59 -0600 | [diff] [blame] | 307 | present = _PRESENT, |
| 308 | storage = _STORAGE, |
Sean McAllister | f54fdf2 | 2021-05-06 19:37:41 -0600 | [diff] [blame] | 309 | stylus_type = _STYLUS_TYPE, |
Sean McAllister | 55e703f | 2021-05-04 10:40:11 -0600 | [diff] [blame] | 310 | location = _LOCATION, |
Sean McAllister | 9473537 | 2021-04-22 16:06:33 -0600 | [diff] [blame] | 311 | ) |