blob: 52f17b4d8bd1d0598e51a5cc143e6bdffc3f07ed [file] [log] [blame]
Andrew Lambe04a1ae2020-04-07 17:37:30 -06001"""Functions related to hardware topology.
2
3See proto definitions for descriptions of arguments.
4"""
C Shapiroe4657e32020-01-24 13:32:11 -06005
Andrew Lamb35013072020-05-04 11:05:48 -06006# Needed to load from @proto. Add @unused to silence lint.
Andrew Lambe04a1ae2020-04-07 17:37:30 -06007load("//config/util/bindings/proto.star", "protos")
Prathmesh Prabhu72f8a002020-04-10 09:57:53 -07008load(
9 "@proto//chromiumos/config/api/topology.proto",
10 topo_pb = "chromiumos.config.api",
11)
12load(
13 "@proto//chromiumos/config/api/hardware_topology.proto",
14 hw_topo_pb = "chromiumos.config.api",
15)
16load(
17 "@proto//chromiumos/config/api/component.proto",
18 comp_pb = "chromiumos.config.api",
19)
Sean McAllister94735372021-04-22 16:06:33 -060020load("//config/util/hw_features.star", "hw_feat")
David Burger32589c52020-04-08 07:11:06 -060021
Sean McAllister9eb2afe2020-10-19 15:18:23 +000022_PRESENT = struct(
23 UNKNOWN = topo_pb.HardwareFeatures.PRESENT_UNKNOWN,
24 PRESENT = topo_pb.HardwareFeatures.PRESENT,
25 NOT_PRESENT = topo_pb.HardwareFeatures.NOT_PRESENT,
26)
27
Jett Rink28124522020-02-05 16:26:37 -070028_AUDIO_CODEC = struct(
Andrew Lambe04a1ae2020-04-07 17:37:30 -060029 RT5682 = topo_pb.HardwareFeatures.Audio.RT5682,
30 ALC5682I = topo_pb.HardwareFeatures.Audio.ALC5682I,
31 ALC5682 = topo_pb.HardwareFeatures.Audio.ALC5682,
YH Lin9e123f52021-04-13 19:52:18 -070032 DA7219 = topo_pb.HardwareFeatures.Audio.DA7219,
33 NAU88L25B = topo_pb.HardwareFeatures.Audio.NAU88L25B,
34)
35
36_AMPLIFIER = struct(
Duncan Laurie6a174e42020-04-20 14:42:32 -070037 MAX98357 = topo_pb.HardwareFeatures.Audio.MAX98357,
38 MAX98373 = topo_pb.HardwareFeatures.Audio.MAX98373,
39 MAX98360 = topo_pb.HardwareFeatures.Audio.MAX98360,
Yong Zhi35cd10d2020-07-16 20:56:50 -050040 RT1015 = topo_pb.HardwareFeatures.Audio.RT1015,
rasheed.hsuehab30ef22020-10-28 11:47:34 +080041 ALC1011 = topo_pb.HardwareFeatures.Audio.ALC1011,
YH Lin9e123f52021-04-13 19:52:18 -070042 RT1015P = topo_pb.HardwareFeatures.Audio.RT1015P,
43 ALC1019 = topo_pb.HardwareFeatures.Audio.ALC1019,
Jett Rink28124522020-02-05 16:26:37 -070044)
45
Jett Rink82da31e2020-03-13 11:46:26 -060046_FP_LOC = struct(
Jett Rinke027f2f2020-04-14 12:11:23 -060047 NOT_PRESENT = topo_pb.HardwareFeatures.Fingerprint.NOT_PRESENT,
Andrew Lambe04a1ae2020-04-07 17:37:30 -060048 POWER_BUTTON_TOP_LEFT = topo_pb.HardwareFeatures.Fingerprint.POWER_BUTTON_TOP_LEFT,
49 KEYBOARD_BOTTOM_LEFT = topo_pb.HardwareFeatures.Fingerprint.KEYBOARD_BOTTOM_LEFT,
50 KEYBOARD_BOTTOM_RIGHT = topo_pb.HardwareFeatures.Fingerprint.KEYBOARD_BOTTOM_RIGHT,
51 KEYBOARD_TOP_RIGHT = topo_pb.HardwareFeatures.Fingerprint.KEYBOARD_TOP_RIGHT,
YH Lin1684b562021-04-01 17:46:52 -070052 RIGHT_SIDE = topo_pb.HardwareFeatures.Fingerprint.RIGHT_SIDE,
53 LEFT_SIDE = topo_pb.HardwareFeatures.Fingerprint.LEFT_SIDE,
Jett Rink82da31e2020-03-13 11:46:26 -060054)
55
Jett Rinke27c7052020-03-19 11:42:05 -060056_STORAGE = struct(
Sean McAllistera84b7342020-06-23 18:08:48 -060057 EMMC = comp_pb.Component.Storage.EMMC,
58 NVME = comp_pb.Component.Storage.NVME,
YH Lin1745dd22020-11-25 09:18:34 -080059 SATA = comp_pb.Component.Storage.SATA,
Jett Rinke27c7052020-03-19 11:42:05 -060060)
61
Jett Rink0858d222020-03-19 11:27:54 -060062_KB_TYPE = struct(
Andrew Lambe04a1ae2020-04-07 17:37:30 -060063 NONE = topo_pb.HardwareFeatures.Keyboard.NONE,
64 INTERNAL = topo_pb.HardwareFeatures.Keyboard.INTERNAL,
65 DETACHABLE = topo_pb.HardwareFeatures.Keyboard.DETACHABLE,
Jett Rink0858d222020-03-19 11:27:54 -060066)
67
Jett Rink67f60862020-04-09 13:20:42 -060068_STYLUS = struct(
69 NONE = topo_pb.HardwareFeatures.Stylus.NONE,
70 INTERNAL = topo_pb.HardwareFeatures.Stylus.INTERNAL,
71 EXTERNAL = topo_pb.HardwareFeatures.Stylus.EXTERNAL,
72)
73
Andrew Lambf723e842020-06-19 10:12:19 -060074_REGION = struct(
75 SCREEN = topo_pb.HardwareFeatures.Button.SCREEN,
76 KEYBOARD = topo_pb.HardwareFeatures.Button.KEYBOARD,
77)
78
79_EDGE = struct(
80 LEFT = topo_pb.HardwareFeatures.Button.LEFT,
81 RIGHT = topo_pb.HardwareFeatures.Button.RIGHT,
82 TOP = topo_pb.HardwareFeatures.Button.TOP,
83 BOTTOM = topo_pb.HardwareFeatures.Button.BOTTOM,
84)
85
Ren-Pei Zeng0bf96352020-09-28 18:44:42 +080086_CAMERA_FLAGS = struct(
87 SUPPORT_1080P = topo_pb.HardwareFeatures.Camera.FLAGS_SUPPORT_1080P,
88 SUPPORT_AUTOFOCUS = topo_pb.HardwareFeatures.Camera.FLAGS_SUPPORT_AUTOFOCUS,
89)
90
Sean McAllister17b4aa42020-10-15 19:54:24 +000091_EC_TYPE = struct(
92 UNKNOWN = topo_pb.HardwareFeatures.EmbeddedController.EC_TYPE_UNKNOWN,
93 CHROME = topo_pb.HardwareFeatures.EmbeddedController.EC_CHROME,
94 WILCO = topo_pb.HardwareFeatures.EmbeddedController.EC_WILCO,
95)
96
Kevin Sheltona1788ee2021-02-18 22:13:13 +000097_TPM_TYPE = struct(
98 UNKNOWN = topo_pb.HardwareFeatures.TrustedPlatformModule.TPM_TYPE_UNKNOWN,
99 THIRD_PARTY = topo_pb.HardwareFeatures.TrustedPlatformModule.THIRD_PARTY,
YH Linbf6fce22021-04-21 13:43:53 -0700100 GSC_H1B = topo_pb.HardwareFeatures.TrustedPlatformModule.GSC_H1B,
101 GSC_H1D = topo_pb.HardwareFeatures.TrustedPlatformModule.GSC_H1D,
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000102)
103
Andrew Lambf723e842020-06-19 10:12:19 -0600104# Starlark doesn't support converting enums to their names. Add helper fns. to
105# do so.
106def _button_region_to_str(region):
107 return {
108 _REGION.SCREEN: "SCREEN",
109 _REGION.KEYBOARD: "KEYBOARD",
110 }.get(region, "UNKNOWN")
111
112def _button_edge_to_str(edge):
113 return {
114 _EDGE.LEFT: "LEFT",
115 _EDGE.RIGHT: "RIGHT",
116 _EDGE.TOP: "TOP",
117 _EDGE.BOTTOM: "BOTTOM",
118 }.get(edge, "UNKNOWN")
119
Jett Rinkcb41fb42020-04-10 16:32:17 -0600120def _make_fw_config(mask, id):
121 """Builds a HardwareFeatures.FirmwareConfiguration proto.
122
123 Takes a 32-bit mask for the field and an id. Shifts the id
124 into the mask region and checks that the value fits within the bit mask.
125 """
126 lsb_bit_set = (~mask + 1) & mask
127 shifted_id = id * lsb_bit_set
128 if shifted_id & mask != shifted_id:
129 fail("Specified id %d out of range [0, %d]" % (id, mask // lsb_bit_set))
130
131 return topo_pb.HardwareFeatures.FirmwareConfiguration(
132 value = shifted_id,
133 mask = mask,
134 )
135
Jett Rink67f60862020-04-09 13:20:42 -0600136def _accumulate_fw_config(existing_fw_config, new_fw_config):
Josie Nordrum206be1b2020-06-04 12:20:16 -0600137 """Adds fw config to existing fw config."""
Jett Rink67f60862020-04-09 13:20:42 -0600138 if existing_fw_config.mask & new_fw_config.mask:
139 fail("FW_CONFIG masks cannot overlap! 0x%x and 0x%x" %
Andrew Lamb35013072020-05-04 11:05:48 -0600140 (existing_fw_config.mask, new_fw_config.mask))
Jett Rink67f60862020-04-09 13:20:42 -0600141
142 existing_fw_config.value += new_fw_config.value
143 existing_fw_config.mask += new_fw_config.mask
144
145def _accumulate_fw_configs(result_hw_features, fw_configs):
146 for fw_config in fw_configs:
147 _accumulate_fw_config(result_hw_features.fw_config, fw_config)
148
Sean McAllister94735372021-04-22 16:06:33 -0600149def _create_design_features(form_factor = hw_feat.form_factor.CLAMSHELL):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600150 """Builds a HardwareFeatures proto with form_factor."""
151 return topo_pb.HardwareFeatures(
152 form_factor = topo_pb.HardwareFeatures.FormFactor(
153 form_factor = form_factor,
154 ),
155 )
C Shapiroe4657e32020-01-24 13:32:11 -0600156
Sean McAllister94735372021-04-22 16:06:33 -0600157_DEFAULT_FF = [hw_feat.form_factor.CLAMSHELL, hw_feat.form_factor.CONVERTIBLE]
158
159def _create_features(form_factors = _DEFAULT_FF):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600160 """Builds a HardwareFeatures proto for each of form_factors."""
161 return [_create_design_features(ff) for ff in form_factors]
C Shapiroe4657e32020-01-24 13:32:11 -0600162
Jett Rink28124522020-02-05 16:26:37 -0700163def _bool_to_present(value):
Jett Rink4a7cd452020-04-10 15:46:05 -0600164 """Returns correct value of present enum depending on value"""
165 if value == None:
Sean McAllister9eb2afe2020-10-19 15:18:23 +0000166 return _PRESENT.UNKNOWN
Jett Rink4a7cd452020-04-10 15:46:05 -0600167 elif value:
Sean McAllister9eb2afe2020-10-19 15:18:23 +0000168 return _PRESENT.PRESENT
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600169 else:
Sean McAllister9eb2afe2020-10-19 15:18:23 +0000170 return _PRESENT.NOT_PRESENT
Jett Rink28124522020-02-05 16:26:37 -0700171
C Shapirod2365312020-05-18 14:46:48 -0500172def _create_screen(
YH Lin13432982020-08-18 13:18:40 -0700173 id = None,
174 description = None,
Josie Nordrumc83fdca2020-07-08 10:58:18 -0600175 inches = 0,
C Shapirod2365312020-05-18 14:46:48 -0500176 width_px = None,
177 height_px = None,
178 pixels_per_in = None,
179 touch = False,
180 fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600181 """Builds a Topology proto for a screen."""
182 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700183
C Shapirod2365312020-05-18 14:46:48 -0500184 hw_features.screen.panel_properties = comp_pb.Component.DisplayPanel.Properties(
185 diagonal_milliinch = inches * 1000,
186 width_px = width_px,
187 height_px = height_px,
188 pixels_per_in = pixels_per_in,
189 )
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600190 hw_features.screen.touch_support = _bool_to_present(touch)
Jett Rink28124522020-02-05 16:26:37 -0700191
Jett Rink4a7cd452020-04-10 15:46:05 -0600192 _accumulate_fw_configs(hw_features, fw_configs)
193
YH Lin13432982020-08-18 13:18:40 -0700194 if touch:
195 screen_id = id if id else "TOUCHSCREEN"
196 screen_desc = description if description else "Touchscreen"
197 else:
198 screen_id = id if id else "SCREEN"
199 screen_desc = description if description else "Default screen"
200
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600201 return topo_pb.Topology(
YH Lin13432982020-08-18 13:18:40 -0700202 id = screen_id,
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600203 type = topo_pb.Topology.SCREEN,
YH Lin13432982020-08-18 13:18:40 -0700204 description = {"EN": screen_desc},
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600205 hardware_feature = hw_features,
206 )
Jett Rink28124522020-02-05 16:26:37 -0700207
Andrew Lambe7b9f482020-05-15 12:00:42 -0600208def _create_form_factor(form_factor, fw_configs = [], id = None, description = None):
209 """Builds a Topology proto for a form factor.
210
211 Args:
Andrew Lamb5636e402020-05-22 20:07:53 -0600212 form_factor: A FormFactorType enum. Required.
Andrew Lambe7b9f482020-05-15 12:00:42 -0600213 fw_configs: A list of FirmwareConfiguration protos for the form factor.
214 id: A string identifier for the Topology. If not passed, a default is
215 provided based on form_factor.
216 description: An English description for the Topology. If not passed, a
217 default is provided based on form_factor.
218 """
219 if not id:
220 id = {
Sean McAllister94735372021-04-22 16:06:33 -0600221 hw_feat.form_factor.CLAMSHELL: "CLAMSHELL",
222 hw_feat.form_factor.CONVERTIBLE: "CONVERTIBLE",
Jeff Chase00395532021-05-05 15:41:43 -0400223 hw_feat.form_factor.CHROMEBASE: "CHROMEBASE",
Sean McAllister94735372021-04-22 16:06:33 -0600224 hw_feat.form_factor.CHROMEBOX: "CHROMEBOX",
Andrew Lambe7b9f482020-05-15 12:00:42 -0600225 }[form_factor]
226
227 if not description:
228 description = {
Sean McAllister94735372021-04-22 16:06:33 -0600229 hw_feat.form_factor.CLAMSHELL: "Device cannot rotate past 180 degrees",
230 hw_feat.form_factor.CONVERTIBLE: "Device can rotate 360 degrees",
Jeff Chase00395532021-05-05 15:41:43 -0400231 hw_feat.form_factor.CHROMEBASE: "Desktop chrome all-in-one.",
Sean McAllister94735372021-04-22 16:06:33 -0600232 hw_feat.form_factor.CHROMEBOX: "Desktop chrome device.",
Andrew Lambe7b9f482020-05-15 12:00:42 -0600233 }[form_factor]
234
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600235 hw_features = topo_pb.HardwareFeatures()
Jett Rink3e3ef612020-01-28 11:59:17 -0700236
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600237 hw_features.form_factor.form_factor = form_factor
Jett Rink3e3ef612020-01-28 11:59:17 -0700238
Jett Rink67f60862020-04-09 13:20:42 -0600239 _accumulate_fw_configs(hw_features, fw_configs)
240
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600241 return topo_pb.Topology(
242 id = id,
243 type = topo_pb.Topology.FORM_FACTOR,
244 description = {"EN": description},
245 hardware_feature = hw_features,
246 )
Jett Rink3e3ef612020-01-28 11:59:17 -0700247
Duncan Laurie6a174e42020-04-20 14:42:32 -0700248def _create_audio(id, description, codec = None, speaker_amp = None, headphone_codec = None, fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600249 """Builds a Topology proto for audio."""
250 hw_features = topo_pb.HardwareFeatures()
Jett Rink3e3ef612020-01-28 11:59:17 -0700251
Duncan Laurie6a174e42020-04-20 14:42:32 -0700252 if codec:
253 hw_features.audio.audio_codec = codec
254 if speaker_amp:
255 hw_features.audio.speaker_amp = speaker_amp
256 if headphone_codec:
257 hw_features.audio.headphone_codec = headphone_codec
Jett Rink28124522020-02-05 16:26:37 -0700258
Jett Rink4a7cd452020-04-10 15:46:05 -0600259 _accumulate_fw_configs(hw_features, fw_configs)
260
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600261 return topo_pb.Topology(
262 id = id,
263 type = topo_pb.Topology.AUDIO,
264 description = {"EN": description},
265 hardware_feature = hw_features,
266 )
Jett Rink28124522020-02-05 16:26:37 -0700267
Jett Rink67f60862020-04-09 13:20:42 -0600268def _create_stylus(id, description, stylus_type, fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600269 """Builds a Topology proto for a stylus."""
270 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700271
Jett Rink67f60862020-04-09 13:20:42 -0600272 hw_features.stylus.stylus = stylus_type
273
274 _accumulate_fw_configs(hw_features, fw_configs)
275
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600276 return topo_pb.Topology(
277 id = id,
278 type = topo_pb.Topology.STYLUS,
279 description = {"EN": description},
280 hardware_feature = hw_features,
281 )
Jett Rink28124522020-02-05 16:26:37 -0700282
YH Lin6b861e02021-03-01 18:23:16 -0800283def _create_keyboard(backlight, pwr_btn_present, kb_type, numpad_present = False, fw_configs = [], id = None, description = None):
Andrew Lamb0cc271d2020-05-20 11:05:14 -0600284 """Builds a Topology proto for a keyboard.
285
286 Args:
Andrew Lamb5636e402020-05-22 20:07:53 -0600287 backlight: True if a backlight is present. Required.
288 pwr_btn_present: True if a power button is present. Required.
289 kb_type: A KeyboardType enum. Required.
YH Lin6b861e02021-03-01 18:23:16 -0800290 numpad_present: True if numeric pad is present.
Andrew Lamb0cc271d2020-05-20 11:05:14 -0600291 fw_configs: A list of FirmwareConfiguration protos for the form factor.
292 id: A string identifier for the Topology. If not passed, a default is
293 provided.
294 description: An English description for the Topology. If not passed, a
295 default is provided.
296 """
297
298 if not id:
299 id = "KB_{backlight}".format(
300 backlight = "BL" if backlight else "NO_BL",
301 )
302
303 if not description:
304 # Starlark doesn't seem to have a way to find the enum name with
305 # reflection.
306 if kb_type == topo_pb.HardwareFeatures.Keyboard.INTERNAL:
307 type_str = "Internal"
308 elif kb_type == topo_pb.HardwareFeatures.Keyboard.DETACHABLE:
309 type_str = "Detachable"
310 else:
311 type_str = "Unknown type"
312
313 description = "{type} keyboard {backlight} backlight".format(
314 type = type_str,
315 backlight = "with" if backlight else "without",
316 )
317
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600318 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700319
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600320 hw_features.keyboard.keyboard_type = kb_type
321 hw_features.keyboard.backlight = _bool_to_present(backlight)
322 hw_features.keyboard.power_button = _bool_to_present(pwr_btn_present)
YH Lin6b861e02021-03-01 18:23:16 -0800323 hw_features.keyboard.numeric_pad = _bool_to_present(numpad_present)
Jett Rink28124522020-02-05 16:26:37 -0700324
Jett Rink67f60862020-04-09 13:20:42 -0600325 _accumulate_fw_configs(hw_features, fw_configs)
326
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600327 return topo_pb.Topology(
328 id = id,
329 type = topo_pb.Topology.KEYBOARD,
330 description = {"EN": description},
331 hardware_feature = hw_features,
332 )
Jett Rink28124522020-02-05 16:26:37 -0700333
Jett Rinkba590b52020-05-01 11:35:16 -0600334def _create_thermal(id, description, fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600335 """Builds a Topology proto for thermal."""
336 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700337
Jett Rinkba590b52020-05-01 11:35:16 -0600338 _accumulate_fw_configs(hw_features, fw_configs)
Jett Rink28124522020-02-05 16:26:37 -0700339
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600340 return topo_pb.Topology(
341 id = id,
342 type = topo_pb.Topology.THERMAL,
343 description = {"EN": description},
344 hardware_feature = hw_features,
345 )
Jett Rink28124522020-02-05 16:26:37 -0700346
Ricardo Ribaldab9b17b32021-01-13 17:10:20 +0100347def _make_camera_device(
348 interface,
349 facing,
350 orientation,
351 flags,
352 ids,
353 privacy_switch_present = None):
Ren-Pei Zengce869dd2020-08-18 01:29:37 +0800354 """Builds a HardwareFeatures.Camera.Device proto."""
355 camera_pb = topo_pb.HardwareFeatures.Camera
356 device = camera_pb.Device()
357
Ren-Pei Zengce869dd2020-08-18 01:29:37 +0800358 device.interface = {
359 "usb": camera_pb.INTERFACE_USB,
360 "mipi": camera_pb.INTERFACE_MIPI,
361 }[interface]
362 device.facing = {
363 "back": camera_pb.FACING_BACK,
364 "front": camera_pb.FACING_FRONT,
365 }[facing]
366 device.orientation = {
367 0: camera_pb.ORIENTATION_0,
368 90: camera_pb.ORIENTATION_90,
369 180: camera_pb.ORIENTATION_180,
370 270: camera_pb.ORIENTATION_270,
371 }[orientation]
Ren-Pei Zeng0bf96352020-09-28 18:44:42 +0800372 device.flags = flags
373 device.ids = ids
Ren-Pei Zengce869dd2020-08-18 01:29:37 +0800374
Ricardo Ribaldab9b17b32021-01-13 17:10:20 +0100375 if privacy_switch_present:
376 device.privacy_switch = _bool_to_present(privacy_switch_present)
377
Ren-Pei Zengce869dd2020-08-18 01:29:37 +0800378 return device
379
380def _create_camera(
381 id,
382 description,
383 fw_configs = [],
Ren-Pei Zengce869dd2020-08-18 01:29:37 +0800384 camera_devices = []):
385 """Builds a Topology proto for cameras.
386
387 Args:
388 id: A string identifier for the Topology.
389 description: An English description for the Topology.
390 fw_configs: A list of FirmwareConfiguration protos for the form factor.
391 camera_devices: A list of HardwareFeatures.Camera.Device protos.
Ren-Pei Zengce869dd2020-08-18 01:29:37 +0800392 """
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600393 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700394
Ren-Pei Zeng9b5682d2020-10-14 17:37:30 +0800395 camera = hw_features.camera
396 camera.devices = camera_devices
Jett Rink4a7cd452020-04-10 15:46:05 -0600397
398 _accumulate_fw_configs(hw_features, fw_configs)
Jett Rink28124522020-02-05 16:26:37 -0700399
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600400 return topo_pb.Topology(
401 id = id,
402 type = topo_pb.Topology.CAMERA,
403 description = {"EN": description},
404 hardware_feature = hw_features,
405 )
Jett Rink28124522020-02-05 16:26:37 -0700406
Andrew Lamb35013072020-05-04 11:05:48 -0600407def _create_sensor(
408 id,
409 description,
410 fw_configs = [],
411 lid_accel_present = None,
Josie Nordrumfd8bc5d2020-06-08 17:03:25 -0600412 base_accel_present = None,
413 lid_gyro_present = None,
414 base_gyro_present = None,
415 lid_magno_present = None,
Cheng Yueh8a682d42020-10-05 13:05:38 +0800416 base_magno_present = None,
417 lid_light_present = None,
418 base_light_present = None):
Jett Rink67f60862020-04-09 13:20:42 -0600419 """Builds a Topology proto for accelerometer/gyroscrope/magnometer sensors."""
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600420 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700421
Jett Rink67f60862020-04-09 13:20:42 -0600422 _accumulate_fw_configs(hw_features, fw_configs)
423
424 if lid_accel_present:
425 hw_features.accelerometer.lid_accelerometer = _bool_to_present(lid_accel_present)
426
Josie Nordrumfd8bc5d2020-06-08 17:03:25 -0600427 if base_accel_present:
428 hw_features.accelerometer.base_accelerometer = _bool_to_present(base_accel_present)
429
430 if lid_gyro_present:
431 hw_features.gyroscope.lid_gyroscope = _bool_to_present(lid_gyro_present)
432
Josie Nordrum20a8e602020-06-08 22:33:12 -0600433 if base_gyro_present:
434 hw_features.gyroscope.base_gyroscope = _bool_to_present(base_gyro_present)
Jett Rink67f60862020-04-09 13:20:42 -0600435
Josie Nordrumfd8bc5d2020-06-08 17:03:25 -0600436 if lid_magno_present:
437 hw_features.magnetometer.lid_magnetometer = _bool_to_present(lid_magno_present)
438
439 if base_magno_present:
440 hw_features.magnetometer.base_magnetometer = _bool_to_present(base_magno_present)
441
Cheng Yueh8a682d42020-10-05 13:05:38 +0800442 if lid_light_present:
443 hw_features.light_sensor.lid_lightsensor = _bool_to_present(lid_light_present)
444
445 if base_light_present:
446 hw_features.light_sensor.base_lightsensor = _bool_to_present(base_light_present)
447
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600448 return topo_pb.Topology(
449 id = id,
Jett Rink67f60862020-04-09 13:20:42 -0600450 type = topo_pb.Topology.ACCELEROMETER_GYROSCOPE_MAGNETOMETER,
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600451 description = {"EN": description},
452 hardware_feature = hw_features,
453 )
Jett Rink28124522020-02-05 16:26:37 -0700454
Jett Rink4a7cd452020-04-10 15:46:05 -0600455def _create_fingerprint(id, description, location, board = None, fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600456 """Builds a Topology proto for a fingerprint reader."""
457 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700458
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600459 hw_features.fingerprint.location = location
460 if board:
461 hw_features.fingerprint.board = board
Tom Hughesdfc35402020-06-29 16:02:09 -0700462 if board == "bloonchipper":
Tom Hughes9a0d49e2020-10-30 16:44:35 -0700463 hw_features.fingerprint.ro_version = "bloonchipper_v2.0.5938-197506c1"
Jett Rink82da31e2020-03-13 11:46:26 -0600464
Jett Rink4a7cd452020-04-10 15:46:05 -0600465 _accumulate_fw_configs(hw_features, fw_configs)
466
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600467 return topo_pb.Topology(
468 id = id,
469 type = topo_pb.Topology.FINGERPRINT,
470 description = {"EN": description},
471 hardware_feature = hw_features,
472 )
Jett Rink28124522020-02-05 16:26:37 -0700473
Jett Rink4a7cd452020-04-10 15:46:05 -0600474def _create_proximity_sensor(id, description, fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600475 """Builds a Topology proto for a proximity sensor."""
476 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700477
Jett Rink4a7cd452020-04-10 15:46:05 -0600478 _accumulate_fw_configs(hw_features, fw_configs)
479
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600480 return topo_pb.Topology(
481 id = id,
482 type = topo_pb.Topology.PROXIMITY_SENSOR,
483 description = {"EN": description},
484 hardware_feature = hw_features,
485 )
Jett Rink28124522020-02-05 16:26:37 -0700486
Jett Rinkba590b52020-05-01 11:35:16 -0600487def _create_daughter_board(
488 id,
489 description,
490 fw_configs = [],
491 usbc_count = 0,
492 usba_count = 0,
493 lte_support = False,
Vincent Palatin0ebdfd32021-04-23 15:54:43 +0200494 lte_model = None,
Jett Rinkba590b52020-05-01 11:35:16 -0600495 hdmi_support = False):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600496 """Builds a Topology proto for a daughter board."""
497 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700498
Jett Rinkba590b52020-05-01 11:35:16 -0600499 _accumulate_fw_configs(hw_features, fw_configs)
Jett Rink67f60862020-04-09 13:20:42 -0600500
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600501 hw_features.usb_c.count.value = usbc_count
502 hw_features.usb_a.count.value = usba_count
503 hw_features.lte.present = _bool_to_present(lte_support)
Vincent Palatin0ebdfd32021-04-23 15:54:43 +0200504 hw_features.lte.model = lte_model
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600505 hw_features.hdmi.present = _bool_to_present(hdmi_support)
Jett Rink3e3ef612020-01-28 11:59:17 -0700506
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600507 return topo_pb.Topology(
508 id = id,
509 type = topo_pb.Topology.DAUGHTER_BOARD,
510 description = {"EN": description},
511 hardware_feature = hw_features,
512 )
Jett Rink3e3ef612020-01-28 11:59:17 -0700513
Jett Rink67f60862020-04-09 13:20:42 -0600514def _create_non_volatile_storage(id, description, storage_type, fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600515 """Builds a Topology proto for non-volatile storage."""
516 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700517
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600518 hw_features.storage.storage_type = storage_type
Jett Rinke27c7052020-03-19 11:42:05 -0600519
Jett Rink67f60862020-04-09 13:20:42 -0600520 _accumulate_fw_configs(hw_features, fw_configs)
521
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600522 return topo_pb.Topology(
523 id = id,
524 type = topo_pb.Topology.NON_VOLATILE_STORAGE,
525 description = {"EN": description},
526 hardware_feature = hw_features,
527 )
Jett Rink28124522020-02-05 16:26:37 -0700528
Jett Rink4a7cd452020-04-10 15:46:05 -0600529def _create_wifi(id, description, fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600530 """Builds a Topology proto for a WiFi chip."""
531 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700532
Jett Rink4a7cd452020-04-10 15:46:05 -0600533 _accumulate_fw_configs(hw_features, fw_configs)
534
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600535 return topo_pb.Topology(
536 id = id,
537 type = topo_pb.Topology.WIFI,
538 description = {"EN": description},
539 hardware_feature = hw_features,
540 )
Jett Rink28124522020-02-05 16:26:37 -0700541
Vincent Palatin0ebdfd32021-04-23 15:54:43 +0200542def _create_lte_board(id, description, lte_present, fw_configs = [], model = None):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600543 """Builds a Topology proto for a LTE board."""
544 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700545
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600546 hw_features.lte.present = _bool_to_present(lte_present)
Vincent Palatin0ebdfd32021-04-23 15:54:43 +0200547 hw_features.lte.model = model
Jett Rink28124522020-02-05 16:26:37 -0700548
Jett Rink4a7cd452020-04-10 15:46:05 -0600549 _accumulate_fw_configs(hw_features, fw_configs)
550
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600551 return topo_pb.Topology(
552 id = id,
553 type = topo_pb.Topology.LTE_BOARD,
554 description = {"EN": description},
555 hardware_feature = hw_features,
556 )
Jett Rink28124522020-02-05 16:26:37 -0700557
Jett Rink67f60862020-04-09 13:20:42 -0600558def _create_sd_reader(id, description, fw_configs = []):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600559 """Builds a Topology proto for a SD reader."""
560 hw_features = topo_pb.HardwareFeatures()
Jett Rink28124522020-02-05 16:26:37 -0700561
Jett Rink67f60862020-04-09 13:20:42 -0600562 _accumulate_fw_configs(hw_features, fw_configs)
563
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600564 return topo_pb.Topology(
565 id = id,
566 type = topo_pb.Topology.SD_READER,
567 description = {"EN": description},
568 hardware_feature = hw_features,
569 )
Jett Rink937839f2020-03-26 12:09:49 -0600570
Jett Rinkba590b52020-05-01 11:35:16 -0600571def _create_motherboard_usb(
572 id,
573 description,
574 fw_configs = [],
575 usbc_count = 0,
576 usba_count = 0):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600577 """Builds a Topology proto for a motherboard."""
578 hw_features = topo_pb.HardwareFeatures()
Jett Rink937839f2020-03-26 12:09:49 -0600579
Jett Rinkba590b52020-05-01 11:35:16 -0600580 _accumulate_fw_configs(hw_features, fw_configs)
Jett Rink937839f2020-03-26 12:09:49 -0600581
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600582 hw_features.usb_c.count.value = usbc_count
583 hw_features.usb_a.count.value = usba_count
Jett Rink937839f2020-03-26 12:09:49 -0600584
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600585 return topo_pb.Topology(
586 id = id,
587 type = topo_pb.Topology.MOTHERBOARD_USB,
588 description = {"EN": description},
589 hardware_feature = hw_features,
590 )
Jett Rink28124522020-02-05 16:26:37 -0700591
Sean McAllister94735372021-04-22 16:06:33 -0600592def _create_bluetooth(id, description, bt_component, fw_configs = [], present = True):
C Shapiroa681fad2020-04-15 17:05:03 -0500593 """Builds a Topology proto for bluetooth."""
594 hw_features = topo_pb.HardwareFeatures()
595
Sean McAllister94735372021-04-22 16:06:33 -0600596 hw_features.bluetooth.present = _bool_to_present(present)
C Shapiroa681fad2020-04-15 17:05:03 -0500597 hw_features.bluetooth.component = bt_component
598
599 _accumulate_fw_configs(hw_features, fw_configs)
600
601 return topo_pb.Topology(
602 id = id,
603 type = topo_pb.Topology.BLUETOOTH,
604 description = {"EN": description},
605 hardware_feature = hw_features,
606 )
607
Josie Nordrum206be1b2020-06-04 12:20:16 -0600608def _create_barreljack(id, description, bj_present, fw_configs = []):
609 """Builds a Topology proto for barreljack."""
610 hw_features = topo_pb.HardwareFeatures()
611
612 hw_features.barreljack.present = _bool_to_present(bj_present)
613
614 _accumulate_fw_configs(hw_features, fw_configs)
615
616 return topo_pb.Topology(
617 id = id,
618 type = topo_pb.Topology.BARRELJACK,
619 description = {"EN": description},
620 hardware_feature = hw_features,
621 )
622
Andrew Lambf723e842020-06-19 10:12:19 -0600623def _create_power_button(region, edge, position, id = None, description = None):
624 """Builds a Topology proto for a power button.
625
626 Args:
627 region: A HardwareFeatures.Button.Region enum. Required.
628 edge: A HardwareFeatures.Button.Edge enum. Required.
629 position: The percentage for button center position to the display's
630 width/height in primary landscape screen orientation. If edge is
631 LEFT or RIGHT, specifies the button's center position as a fraction
632 of region's height relative to the top of region. For TOP and
633 BOTTOM, specifies the position as a fraction of region width
634 relative to the left side of region. Must be in the range
635 [0.0, 1.0]. Required.
636 id: A string identifier for the Topology. If not passed, a default is
637 provided.
638 description: An English description for the Topology. If not passed, a
639 default is provided.
640 """
641 if not id:
642 id = "{region}_{edge}_POWER_BUTTON".format(
643 region = _button_region_to_str(region),
644 edge = _button_edge_to_str(edge),
645 )
646
647 if not description:
648 description = "Power button on the {edge} edge of the {region}".format(
649 region = _button_region_to_str(region).lower(),
650 edge = _button_edge_to_str(edge).lower(),
651 )
652 return topo_pb.Topology(
653 id = id,
654 type = topo_pb.Topology.POWER_BUTTON,
655 description = {"EN": description},
656 hardware_feature = topo_pb.HardwareFeatures(
657 power_button = topo_pb.HardwareFeatures.Button(
658 region = region,
659 edge = edge,
660 position = position,
661 ),
662 ),
663 )
664
Andrew Lamb26e72022020-06-19 12:14:32 -0600665def _create_volume_button(region, edge, position, id = None, description = None):
666 """Builds a Topology proto for a volume button.
667
668 Args:
669 region: A HardwareFeatures.Button.Region enum. Required.
670 edge: A HardwareFeatures.Button.Edge enum. Required.
671 position: The percentage for button center position to the display's
672 width/height in primary landscape screen orientation. If edge is
673 LEFT or RIGHT, specifies the button's center position as a fraction
674 of region's height relative to the top of region. For TOP and
675 BOTTOM, specifies the position as a fraction of region width
676 relative to the left side of region. Must be in the range
677 [0.0, 1.0]. Required.
678 id: A string identifier for the Topology. If not passed, a default is
679 provided.
680 description: An English description for the Topology. If not passed, a
681 default is provided.
682 """
683 if not id:
684 id = "{region}_{edge}_VOLUME_BUTTON".format(
685 region = _button_region_to_str(region),
686 edge = _button_edge_to_str(edge),
687 )
688
689 if not description:
690 description = "Volume button on the {edge} edge of the {region}".format(
691 region = _button_region_to_str(region).lower(),
692 edge = _button_edge_to_str(edge).lower(),
693 )
694 return topo_pb.Topology(
695 id = id,
696 type = topo_pb.Topology.POWER_BUTTON,
697 description = {"EN": description},
698 hardware_feature = topo_pb.HardwareFeatures(
699 volume_button = topo_pb.HardwareFeatures.Button(
700 region = region,
701 edge = edge,
702 position = position,
703 ),
704 ),
705 )
706
Sean McAllister4d7ecde2020-10-19 15:32:45 +0000707def _create_ec(present = True, ec_type = _EC_TYPE.CHROME, id = None):
Sean McAllister17b4aa42020-10-15 19:54:24 +0000708 """Builds a Topology proto for an embedded controller.
709
710 Args:
Sean McAllister4d7ecde2020-10-19 15:32:45 +0000711 present: flag indicating whether the device has an EC at all
712 ec_type: An EmbeddedControllerType enum
Sean McAllister17b4aa42020-10-15 19:54:24 +0000713 id: A string identifier for the Topology. If not passed, a default is
714 provided.
715 """
716 hw_features = topo_pb.HardwareFeatures()
717 hw_features.embedded_controller.ec_type = ec_type
Sean McAllister4d7ecde2020-10-19 15:32:45 +0000718 hw_features.embedded_controller.present = _bool_to_present(present)
Sean McAllister17b4aa42020-10-15 19:54:24 +0000719
720 return topo_pb.Topology(
721 id = id or "ec",
722 type = topo_pb.Topology.EC,
723 hardware_feature = hw_features,
724 )
725
Sean McAllister4d7ecde2020-10-19 15:32:45 +0000726# enumerate the common cases
727_EC_NONE = _create_ec(present = False, ec_type = _EC_TYPE.UNKNOWN)
728_EC_CHROME = _create_ec(ec_type = _EC_TYPE.CHROME)
729_EC_WILCO = _create_ec(ec_type = _EC_TYPE.WILCO)
730
Zhuohao Lee8d9f1fb2021-02-01 21:35:07 +0800731def _create_touch(id, description, fw_configs = []):
732 """Builds a Topology proto for touch."""
733 hw_features = topo_pb.HardwareFeatures()
734
735 _accumulate_fw_configs(hw_features, fw_configs)
736
737 return topo_pb.Topology(
738 id = id,
739 type = topo_pb.Topology.TOUCH,
740 description = {"EN": description},
741 hardware_feature = hw_features,
742 )
743
YH Linbf6fce22021-04-21 13:43:53 -0700744def _create_tpm(tpm_type = _TPM_TYPE.GSC_H1B, id = None, fw_configs = []):
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000745 """Builds a Topology proto for a trusted platform module.
746
747 Args:
748 tpm_type: A TrustedPlatformModuleType enum
749 id: A string identifier for the Topology. If not passed, a default is
750 provided.
YH Linbf6fce22021-04-21 13:43:53 -0700751 fw_configs: A list of FirmwareConfiguration protos for the tpm.
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000752 """
753 hw_features = topo_pb.HardwareFeatures()
754 hw_features.trusted_platform_module.tpm_type = tpm_type
755
YH Linbf6fce22021-04-21 13:43:53 -0700756 _accumulate_fw_configs(hw_features, fw_configs)
757
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000758 return topo_pb.Topology(
759 id = id or "TPM",
760 type = topo_pb.Topology.TPM,
761 hardware_feature = hw_features,
762 )
763
Toni Barzicf802a702021-05-18 19:40:23 -0700764def _create_microphone_mute_switch(present = False):
765 """Builds a Topology proto for an microphone mute switch.
766
767 Args:
768 present: flag indicating whether the device has an microphone mute
769 switch
770 """
771 hw_features = topo_pb.HardwareFeatures()
772 hw_features.microphone_mute_switch.present = _bool_to_present(present)
773
774 return topo_pb.Topology(
775 id = "Default",
776 type = topo_pb.Topology.MICROPHONE_MUTE_SWITCH,
777 hardware_feature = hw_features,
778 )
779
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000780# enumerate the common cases
781_TPM_THIRD_PARTY = _create_tpm(tpm_type = _TPM_TYPE.THIRD_PARTY)
YH Linbf6fce22021-04-21 13:43:53 -0700782_TPM_GSC_H1B = _create_tpm(tpm_type = _TPM_TYPE.GSC_H1B)
783_TPM_GSC_H1D = _create_tpm(tpm_type = _TPM_TYPE.GSC_H1D)
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000784
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600785def _create_hardware_topology(
786 screen = None,
787 form_factor = None,
788 audio = None,
789 stylus = None,
790 keyboard = None,
791 thermal = None,
792 camera = None,
793 accelerometer_gyroscope_magnetometer = None,
794 fingerprint = None,
795 proximity_sensor = None,
796 daughter_board = None,
797 non_volatile_storage = None,
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600798 wifi = None,
799 lte_board = None,
800 sd_reader = None,
C Shapiroa681fad2020-04-15 17:05:03 -0500801 motherboard_usb = None,
Josie Nordrum206be1b2020-06-04 12:20:16 -0600802 bluetooth = None,
Andrew Lambf723e842020-06-19 10:12:19 -0600803 barreljack = None,
Andrew Lamb26e72022020-06-19 12:14:32 -0600804 power_button = None,
Sean McAllister17b4aa42020-10-15 19:54:24 +0000805 volume_button = None,
Zhuohao Lee8d9f1fb2021-02-01 21:35:07 +0800806 ec = None,
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000807 touch = None,
Toni Barzicf802a702021-05-18 19:40:23 -0700808 tpm = None,
809 microphone_mute_switch = None):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600810 """Builds a HardwareTopology proto from Topology protos."""
Jett Rink28124522020-02-05 16:26:37 -0700811
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600812 # Only allow form_factor topologies for form factors
813 if screen and screen.type != topo_pb.Topology.SCREEN:
814 fail("Invalid screen topology")
Jett Rink28124522020-02-05 16:26:37 -0700815
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600816 if form_factor and form_factor.type != topo_pb.Topology.FORM_FACTOR:
817 fail("Invalid form factor topology")
Jett Rink3e3ef612020-01-28 11:59:17 -0700818
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600819 if audio and audio.type != topo_pb.Topology.AUDIO:
820 fail("Invalid audio topology")
Jett Rink28124522020-02-05 16:26:37 -0700821
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600822 if stylus and stylus.type != topo_pb.Topology.STYLUS:
823 fail("Invalid stylus topology")
Jett Rink28124522020-02-05 16:26:37 -0700824
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600825 if keyboard and keyboard.type != topo_pb.Topology.KEYBOARD:
826 fail("Invalid keyboard topology")
Jett Rink28124522020-02-05 16:26:37 -0700827
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600828 if thermal and thermal.type != topo_pb.Topology.THERMAL:
829 fail("Invalid thermal topology")
Jett Rink28124522020-02-05 16:26:37 -0700830
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600831 if camera and camera.type != topo_pb.Topology.CAMERA:
832 fail("Invalid camera topology")
Jett Rink28124522020-02-05 16:26:37 -0700833
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600834 if accelerometer_gyroscope_magnetometer and accelerometer_gyroscope_magnetometer.type != topo_pb.Topology.ACCELEROMETER_GYROSCOPE_MAGNETOMETER:
835 fail("Invalid accelerometer/gyroscope/magnetometer topology")
Jett Rink28124522020-02-05 16:26:37 -0700836
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600837 if fingerprint and fingerprint.type != topo_pb.Topology.FINGERPRINT:
838 fail("Invalid fingerprint topology")
Jett Rink28124522020-02-05 16:26:37 -0700839
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600840 if proximity_sensor and proximity_sensor.type != topo_pb.Topology.PROXIMITY_SENSOR:
841 fail("Invalid proximity sensor topology")
Jett Rink28124522020-02-05 16:26:37 -0700842
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600843 if daughter_board and daughter_board.type != topo_pb.Topology.DAUGHTER_BOARD:
844 fail("Invalid daughter board topology")
Jett Rink28124522020-02-05 16:26:37 -0700845
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600846 if non_volatile_storage and non_volatile_storage.type != topo_pb.Topology.NON_VOLATILE_STORAGE:
847 fail("Invalid non-volatile storage topology")
Jett Rink28124522020-02-05 16:26:37 -0700848
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600849 if wifi and wifi.type != topo_pb.Topology.WIFI:
850 fail("Invalid wifi topology")
Jett Rink28124522020-02-05 16:26:37 -0700851
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600852 if lte_board and lte_board.type != topo_pb.Topology.LTE_BOARD:
853 fail("Invalid lte board topology")
Jett Rink28124522020-02-05 16:26:37 -0700854
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600855 if sd_reader and sd_reader.type != topo_pb.Topology.SD_READER:
856 fail("Invalid lte board topology")
Jett Rink3e3ef612020-01-28 11:59:17 -0700857
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600858 if motherboard_usb and motherboard_usb.type != topo_pb.Topology.MOTHERBOARD_USB:
859 fail("Invalid motherboard usb board topology")
Jett Rink937839f2020-03-26 12:09:49 -0600860
C Shapiroa681fad2020-04-15 17:05:03 -0500861 if bluetooth and bluetooth.type != topo_pb.Topology.BLUETOOTH:
862 fail("Invalid bluetooth topology")
863
Josie Nordrum206be1b2020-06-04 12:20:16 -0600864 if barreljack and barreljack.type != topo_pb.Topology.BARRELJACK:
865 fail("Invalid barreljack topology")
866
Andrew Lambf723e842020-06-19 10:12:19 -0600867 if power_button and power_button.type != topo_pb.Topology.POWER_BUTTON:
868 fail("Invalid power button topology")
869
Andrew Lamb26e72022020-06-19 12:14:32 -0600870 if volume_button and volume_button.type != topo_pb.Topology.POWER_BUTTON:
871 fail("Invalid volume button topology")
872
Sean McAllister17b4aa42020-10-15 19:54:24 +0000873 if ec and ec.type != topo_pb.Topology.EC:
874 fail("Invalid ec type")
875
Zhuohao Lee8d9f1fb2021-02-01 21:35:07 +0800876 if touch and touch.type != topo_pb.Topology.TOUCH:
877 fail("Invalid touch topology")
878
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000879 if tpm and tpm.type != topo_pb.Topology.TPM:
880 fail("Invalid tpm type")
881
Toni Barzicf802a702021-05-18 19:40:23 -0700882 if microphone_mute_switch and microphone_mute_switch.type != topo_pb.Topology.MICROPHONE_MUTE_SWITCH:
883 fail("Invalid microphone mute switch type")
884
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600885 return hw_topo_pb.HardwareTopology(
886 screen = screen,
887 form_factor = form_factor,
888 audio = audio,
889 stylus = stylus,
890 keyboard = keyboard,
891 thermal = thermal,
892 camera = camera,
893 accelerometer_gyroscope_magnetometer = accelerometer_gyroscope_magnetometer,
894 fingerprint = fingerprint,
895 proximity_sensor = proximity_sensor,
896 daughter_board = daughter_board,
897 non_volatile_storage = non_volatile_storage,
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600898 wifi = wifi,
899 lte_board = lte_board,
900 sd_reader = sd_reader,
901 motherboard_usb = motherboard_usb,
C Shapiroa681fad2020-04-15 17:05:03 -0500902 bluetooth = bluetooth,
Josie Nordrum206be1b2020-06-04 12:20:16 -0600903 barreljack = barreljack,
Andrew Lambf723e842020-06-19 10:12:19 -0600904 power_button = power_button,
Andrew Lamb26e72022020-06-19 12:14:32 -0600905 volume_button = volume_button,
Sean McAllister17b4aa42020-10-15 19:54:24 +0000906 ec = ec,
Zhuohao Lee8d9f1fb2021-02-01 21:35:07 +0800907 touch = touch,
Kevin Sheltona1788ee2021-02-18 22:13:13 +0000908 tpm = tpm,
Toni Barzicf802a702021-05-18 19:40:23 -0700909 microphone_mute_switch = microphone_mute_switch,
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600910 )
Jett Rink3e3ef612020-01-28 11:59:17 -0700911
912def _accumulate_presence(existing_present, new_present):
Sean McAllister9eb2afe2020-10-19 15:18:23 +0000913 if existing_present == _PRESENT.PRESENT:
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600914 return existing_present
Sean McAllister9eb2afe2020-10-19 15:18:23 +0000915 elif new_present != _PRESENT.UNKNOWN:
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600916 return new_present
917 else:
918 return existing_present
Jett Rink3e3ef612020-01-28 11:59:17 -0700919
Jett Rink4a7cd452020-04-10 15:46:05 -0600920def _accumulate_usbc(existing_usbc, new_usbc):
921 existing_usbc.count.value += new_usbc.count.value
922
923def _accumulate_usba(existing_usba, new_usba):
924 existing_usba.count.value += new_usba.count.value
925
926def _accumulate_lte(existing_lte, new_lte):
927 existing_lte.present = _accumulate_presence(existing_lte.present, new_lte.present)
Vincent Palatin0ebdfd32021-04-23 15:54:43 +0200928 if new_lte.present == _PRESENT.PRESENT:
929 existing_lte.model = new_lte.model
Jett Rink4a7cd452020-04-10 15:46:05 -0600930
931def _accumulate_hdmi(existing_hdmi, new_hdmi):
932 existing_hdmi.present = _accumulate_presence(existing_hdmi.present, new_hdmi.present)
933
Andrew Lamba921d782020-08-31 14:47:35 -0600934def _convert_to_hw_features(hardware_topology):
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600935 """Converts a HardwareTopology proto to a HardwareFeatures proto."""
936 result = topo_pb.HardwareFeatures()
Jett Rink3e3ef612020-01-28 11:59:17 -0700937
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600938 # Need to make deep-copy otherwise we change the has_ message serialization
939 copy = proto.from_textpb(hw_topo_pb.HardwareTopology, proto.to_textpb(hardware_topology))
Jett Rink3e3ef612020-01-28 11:59:17 -0700940
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600941 # Handle all possible screen hardware features attributes
Jett Rink4a7cd452020-04-10 15:46:05 -0600942 _accumulate_fw_config(result.fw_config, copy.screen.hardware_feature.fw_config)
943
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600944 if copy.screen.hardware_feature.screen != topo_pb.HardwareFeatures.Screen():
945 result.screen = copy.screen.hardware_feature.screen
Jett Rink3e3ef612020-01-28 11:59:17 -0700946
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600947 # Handle all possible form factor hardware features attributes
Jett Rink67f60862020-04-09 13:20:42 -0600948 _accumulate_fw_config(result.fw_config, copy.form_factor.hardware_feature.fw_config)
949
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600950 if copy.form_factor.hardware_feature.form_factor != topo_pb.HardwareFeatures.FormFactor():
951 result.form_factor = copy.form_factor.hardware_feature.form_factor
Jett Rink3e3ef612020-01-28 11:59:17 -0700952
Jett Rinkcb41fb42020-04-10 16:32:17 -0600953 # Handle all possible audio features attributes
954 _accumulate_fw_config(result.fw_config, copy.audio.hardware_feature.fw_config)
Jett Rink67f60862020-04-09 13:20:42 -0600955
Jett Rinkcb41fb42020-04-10 16:32:17 -0600956 if copy.audio.hardware_feature.audio != topo_pb.HardwareFeatures.Audio():
957 result.audio = copy.audio.hardware_feature.audio
Jett Rink67f60862020-04-09 13:20:42 -0600958
959 # Handle all possible stylus hardware features attributes
960 _accumulate_fw_config(result.fw_config, copy.stylus.hardware_feature.fw_config)
961
962 if copy.stylus.hardware_feature.stylus != topo_pb.HardwareFeatures.Stylus():
963 result.stylus = copy.stylus.hardware_feature.stylus
964
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600965 # Handle all possible keyboard hardware features attributes
Jett Rink67f60862020-04-09 13:20:42 -0600966 _accumulate_fw_config(result.fw_config, copy.keyboard.hardware_feature.fw_config)
967
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600968 if copy.keyboard.hardware_feature.keyboard != topo_pb.HardwareFeatures.Keyboard():
969 result.keyboard = copy.keyboard.hardware_feature.keyboard
Jett Rink28124522020-02-05 16:26:37 -0700970
Jett Rinkcb41fb42020-04-10 16:32:17 -0600971 # Handle all possible thermal features attributes
972 _accumulate_fw_config(result.fw_config, copy.thermal.hardware_feature.fw_config)
Jett Rink28124522020-02-05 16:26:37 -0700973
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600974 # Handle all possible camera features attributes
Jett Rink4a7cd452020-04-10 15:46:05 -0600975 _accumulate_fw_config(result.fw_config, copy.camera.hardware_feature.fw_config)
976
Andrew Lambe04a1ae2020-04-07 17:37:30 -0600977 if copy.camera.hardware_feature.camera != topo_pb.HardwareFeatures.Camera():
978 result.camera = copy.camera.hardware_feature.camera
Jett Rink28124522020-02-05 16:26:37 -0700979
Jett Rink67f60862020-04-09 13:20:42 -0600980 # Handle all possible sensor attributes
981 _accumulate_fw_config(result.fw_config, copy.accelerometer_gyroscope_magnetometer.hardware_feature.fw_config)
982
983 if copy.accelerometer_gyroscope_magnetometer.hardware_feature.accelerometer != topo_pb.HardwareFeatures.Accelerometer():
984 result.accelerometer = copy.accelerometer_gyroscope_magnetometer.hardware_feature.accelerometer
985
986 if copy.accelerometer_gyroscope_magnetometer.hardware_feature.gyroscope != topo_pb.HardwareFeatures.Gyroscope():
987 result.gyroscope = copy.accelerometer_gyroscope_magnetometer.hardware_feature.gyroscope
988
989 if copy.accelerometer_gyroscope_magnetometer.hardware_feature.magnetometer != topo_pb.HardwareFeatures.Magnetometer():
990 result.magnetometer = copy.accelerometer_gyroscope_magnetometer.hardware_feature.magnetometer
991
Jett Rink4a7cd452020-04-10 15:46:05 -0600992 if copy.accelerometer_gyroscope_magnetometer.hardware_feature.light_sensor != topo_pb.HardwareFeatures.LightSensor():
993 result.light_sensor = copy.accelerometer_gyroscope_magnetometer.hardware_feature.light_sensor
994
Jett Rinkcb41fb42020-04-10 16:32:17 -0600995 # Handle all possible fingerprint hardware features attributes
996 _accumulate_fw_config(result.fw_config, copy.fingerprint.hardware_feature.fw_config)
997
998 if copy.fingerprint.hardware_feature.fingerprint != topo_pb.HardwareFeatures.Fingerprint():
999 result.fingerprint = copy.fingerprint.hardware_feature.fingerprint
1000
1001 # Handle all possible proximity sensor hardware features attributes
1002 _accumulate_fw_config(result.fw_config, copy.proximity_sensor.hardware_feature.fw_config)
1003
1004 # Handle all possible daughter board hardware features attributes
1005 _accumulate_fw_config(result.fw_config, copy.daughter_board.hardware_feature.fw_config)
1006
1007 if copy.daughter_board.hardware_feature.usb_c != topo_pb.HardwareFeatures.UsbC():
1008 _accumulate_usbc(result.usb_c, copy.daughter_board.hardware_feature.usb_c)
1009
1010 if copy.daughter_board.hardware_feature.usb_a != topo_pb.HardwareFeatures.UsbA():
1011 _accumulate_usba(result.usb_a, copy.daughter_board.hardware_feature.usb_a)
1012
1013 if copy.daughter_board.hardware_feature.lte != topo_pb.HardwareFeatures.Lte():
1014 _accumulate_lte(result.lte, copy.daughter_board.hardware_feature.lte)
1015
1016 if copy.daughter_board.hardware_feature.hdmi != topo_pb.HardwareFeatures.Hdmi():
1017 _accumulate_hdmi(result.hdmi, copy.daughter_board.hardware_feature.hdmi)
1018
1019 # Handle all possible non volatile storage hardware features attributes
1020 _accumulate_fw_config(result.fw_config, copy.non_volatile_storage.hardware_feature.fw_config)
1021
1022 if copy.non_volatile_storage.hardware_feature.storage != topo_pb.HardwareFeatures.Storage():
1023 result.storage = copy.non_volatile_storage.hardware_feature.storage
1024
Jett Rinkcb41fb42020-04-10 16:32:17 -06001025 # Handle all possible wifi hardware features attributes
1026 _accumulate_fw_config(result.fw_config, copy.wifi.hardware_feature.fw_config)
1027
1028 # Handle all possible lte board attributes
1029 _accumulate_fw_config(result.fw_config, copy.lte_board.hardware_feature.fw_config)
1030
1031 if copy.lte_board.hardware_feature.lte != topo_pb.HardwareFeatures.Lte():
Vincent Palatin0ebdfd32021-04-23 15:54:43 +02001032 _accumulate_lte(result.lte, copy.lte_board.hardware_feature.lte)
Jett Rinkcb41fb42020-04-10 16:32:17 -06001033
1034 # Handle all possible sd reader hardware features attributes
1035 _accumulate_fw_config(result.fw_config, copy.sd_reader.hardware_feature.fw_config)
1036
1037 # Handle all possible motherboard usb features attributes
1038 _accumulate_fw_config(result.fw_config, copy.motherboard_usb.hardware_feature.fw_config)
1039
1040 if copy.motherboard_usb.hardware_feature.usb_c != topo_pb.HardwareFeatures.UsbC():
1041 _accumulate_usbc(result.usb_c, copy.motherboard_usb.hardware_feature.usb_c)
1042
1043 if copy.motherboard_usb.hardware_feature.usb_a != topo_pb.HardwareFeatures.UsbA():
1044 _accumulate_usba(result.usb_a, copy.motherboard_usb.hardware_feature.usb_a)
1045
C Shapiroa681fad2020-04-15 17:05:03 -05001046 # Handle all possible bluetooth features attributes
1047 _accumulate_fw_config(result.fw_config, copy.bluetooth.hardware_feature.fw_config)
1048
1049 if copy.bluetooth.hardware_feature.bluetooth != topo_pb.HardwareFeatures.Bluetooth():
1050 result.bluetooth = copy.bluetooth.hardware_feature.bluetooth
1051
Josie Nordrum206be1b2020-06-04 12:20:16 -06001052 # Handle all possible barreljack features
1053 _accumulate_fw_config(result.fw_config, copy.barreljack.hardware_feature.fw_config)
1054
1055 if copy.barreljack.hardware_feature.barreljack != topo_pb.HardwareFeatures.BarrelJack():
1056 result.barreljack = copy.barreljack.hardware_feature.barreljack
Andrew Lamb90b168c2020-06-22 10:42:30 -06001057
1058 if copy.power_button.hardware_feature.power_button != topo_pb.HardwareFeatures.Button():
1059 result.power_button = copy.power_button.hardware_feature.power_button
1060
1061 if copy.volume_button.hardware_feature.volume_button != topo_pb.HardwareFeatures.Button():
1062 result.volume_button = copy.volume_button.hardware_feature.volume_button
1063
Toni Barzicf802a702021-05-18 19:40:23 -07001064 if copy.microphone_mute_switch.hardware_feature.microphone_mute_switch != topo_pb.HardwareFeatures.MicrophoneMuteSwitch():
1065 result.microphone_mute_switch = copy.microphone_mute_switch.hardware_feature.microphone_mute_switch
1066
Zhuohao Lee8d9f1fb2021-02-01 21:35:07 +08001067 # Handle all possible touch hardware features
1068 _accumulate_fw_config(result.fw_config, copy.touch.hardware_feature.fw_config)
1069
Andrew Lambe04a1ae2020-04-07 17:37:30 -06001070 return result
Jett Rink3e3ef612020-01-28 11:59:17 -07001071
C Shapiroe4657e32020-01-24 13:32:11 -06001072hw_topo = struct(
C Shapiro436f3212020-01-25 07:56:31 -06001073 create_design_features = _create_design_features,
C Shapiroe4657e32020-01-24 13:32:11 -06001074 create_features = _create_features,
Jett Rink28124522020-02-05 16:26:37 -07001075 create_screen = _create_screen,
Jett Rink3e3ef612020-01-28 11:59:17 -07001076 create_form_factor = _create_form_factor,
Jett Rinkf30986f2020-02-18 13:28:41 -07001077 create_audio = _create_audio,
Jett Rink28124522020-02-05 16:26:37 -07001078 create_stylus = _create_stylus,
1079 create_keyboard = _create_keyboard,
1080 create_thermal = _create_thermal,
1081 create_camera = _create_camera,
Jett Rink67f60862020-04-09 13:20:42 -06001082 create_sensor = _create_sensor,
Jett Rink28124522020-02-05 16:26:37 -07001083 create_fingerprint = _create_fingerprint,
1084 create_proximity_sensor = _create_proximity_sensor,
1085 create_daughter_board = _create_daughter_board,
1086 create_non_volatile_storage = _create_non_volatile_storage,
Jett Rink28124522020-02-05 16:26:37 -07001087 create_wifi = _create_wifi,
1088 create_lte_board = _create_lte_board,
1089 create_sd_reader = _create_sd_reader,
Jett Rink937839f2020-03-26 12:09:49 -06001090 create_motherboard_usb = _create_motherboard_usb,
C Shapiroa681fad2020-04-15 17:05:03 -05001091 create_bluetooth = _create_bluetooth,
Josie Nordrum206be1b2020-06-04 12:20:16 -06001092 create_barreljack = _create_barreljack,
Jett Rink3e3ef612020-01-28 11:59:17 -07001093 create_hardware_topology = _create_hardware_topology,
Andrew Lambf723e842020-06-19 10:12:19 -06001094 create_power_button = _create_power_button,
Andrew Lamb26e72022020-06-19 12:14:32 -06001095 create_volume_button = _create_volume_button,
Zhuohao Lee8d9f1fb2021-02-01 21:35:07 +08001096 create_touch = _create_touch,
Toni Barzicf802a702021-05-18 19:40:23 -07001097 create_microphone_mute_switch = _create_microphone_mute_switch,
Jett Rink3e3ef612020-01-28 11:59:17 -07001098 convert_to_hw_features = _convert_to_hw_features,
Ren-Pei Zengce869dd2020-08-18 01:29:37 +08001099 make_camera_device = _make_camera_device,
Jett Rink67f60862020-04-09 13:20:42 -06001100 make_fw_config = _make_fw_config,
Sean McAllister94735372021-04-22 16:06:33 -06001101 ff = hw_feat.form_factor,
YH Lin9e123f52021-04-13 19:52:18 -07001102 amplifier = _AMPLIFIER,
Jett Rink28124522020-02-05 16:26:37 -07001103 audio_codec = _AUDIO_CODEC,
Jett Rink82da31e2020-03-13 11:46:26 -06001104 fp_loc = _FP_LOC,
Jett Rinke27c7052020-03-19 11:42:05 -06001105 storage = _STORAGE,
Jett Rink0858d222020-03-19 11:27:54 -06001106 kb_type = _KB_TYPE,
Jett Rink67f60862020-04-09 13:20:42 -06001107 stylus = _STYLUS,
Andrew Lambf723e842020-06-19 10:12:19 -06001108 region = _REGION,
1109 edge = _EDGE,
Ren-Pei Zeng0bf96352020-09-28 18:44:42 +08001110 camera_flags = _CAMERA_FLAGS,
Sean McAllister9eb2afe2020-10-19 15:18:23 +00001111 present = _PRESENT,
Sean McAllister4d7ecde2020-10-19 15:32:45 +00001112
1113 # embedded controller exports
1114 ec_type = _EC_TYPE,
1115 create_ec = _create_ec,
1116 EC_NONE = _EC_NONE,
1117 EC_CHROME = _EC_CHROME,
1118 EC_WILCO = _EC_WILCO,
Kevin Sheltona1788ee2021-02-18 22:13:13 +00001119
1120 # trusted platform module exports
1121 tpm_type = _TPM_TYPE,
1122 create_tpm = _create_tpm,
1123 TPM_THIRD_PARTY = _TPM_THIRD_PARTY,
YH Linbf6fce22021-04-21 13:43:53 -07001124 TPM_GSC_H1B = _TPM_GSC_H1B,
1125 TPM_GSC_H1D = _TPM_GSC_H1D,
C Shapiro436f3212020-01-25 07:56:31 -06001126)