blob: c37a3b9bb7af1e840dcd2b0c2d3d3e29eb64345e [file] [log] [blame]
Sean McAllister94735372021-04-22 16:06:33 -06001"""Functions related to hardware features.
2
3See proto definitions for descriptions of arguments.
4"""
5
6# Needed to load from @proto. Add @unused to silence lint.
7load("//config/util/bindings/proto.star", "protos")
8load(
Sean McAllister9ecb21e2021-04-27 15:57:59 -06009 "@proto//chromiumos/config/api/component.proto",
10 comp_pb = "chromiumos.config.api",
11)
12load(
Sean McAllister94735372021-04-22 16:06:33 -060013 "@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
25def _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 McAllister9ecb21e2021-04-27 15:57:59 -060034def _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 McAllister94735372021-04-22 16:06:33 -060042def _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
50def _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 McAllister9ecb21e2021-04-27 15:57:59 -060061 type = types[(internal, external)],
Sean McAllister94735372021-04-22 16:06:33 -060062 ),
63 )
64
65_FORM_FACTOR = struct(
Sean McAllister9ecb21e2021-04-27 15:57:59 -060066 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 McAllister94735372021-04-22 16:06:33 -060073)
74
Sean McAllister9ecb21e2021-04-27 15:57:59 -060075_EC = struct(
76 CHROME = _HW_FEAT.EmbeddedController.EC_CHROME,
77 WILCO = _HW_FEAT.EmbeddedController.EC_WILCO,
78)
79
80def _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 McAllister55e703f2021-05-04 10:40:11 -060089_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
100def _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 McAllister94735372021-04-22 16:06:33 -0600110def _create_form_factor(form_factor):
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600111 """Specify the form factor as a HardwareFeature."""
Sean McAllister94735372021-04-22 16:06:33 -0600112 return _HW_FEAT(
113 form_factor = _HW_FEAT.FormFactor(
114 form_factor = form_factor,
115 ),
116 )
117
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600118_KB_TYPE = struct(
119 NONE = _HW_FEAT.Keyboard.NONE,
120 INTERNAL = _HW_FEAT.Keyboard.INTERNAL,
121 DETACHABLE = _HW_FEAT.Keyboard.DETACHABLE,
122)
123
124def _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 McAllister9ecb21e2021-04-27 15:57:59 -0600146_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
152def _create_storage(type):
153 """Specify storage type."""
154 return _HW_FEAT(
155 storage = _HW_FEAT.Storage(
156 storage_type = type,
157 ),
158 )
159
Sean McAllister55e703f2021-05-04 10:40:11 -0600160def _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 McAllister9ecb21e2021-04-27 15:57:59 -0600180def _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
209def _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
230def _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 McAllisterf54fdf22021-05-06 19:37:41 -0600238_STYLUS_TYPE = struct(
239 NONE = _HW_FEAT.Stylus.NONE,
240 INTERNAL = _HW_FEAT.Stylus.INTERNAL,
241 EXTERNAL = _HW_FEAT.Stylus.EXTERNAL,
242)
243
244def _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 McAllister94735372021-04-22 16:06:33 -0600252def _create_features(
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600253 bluetooth = None,
254 camera = None,
255 display = None,
256 ec = _create_ec(_EC.CHROME), # non-chrome ECs are very rare
Sean McAllister55e703f2021-05-04 10:40:11 -0600257 fingerprint = None,
Sean McAllister94735372021-04-22 16:06:33 -0600258 form_factor = None,
259 hotwording = None,
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600260 keyboard = None,
Sean McAllister55e703f2021-05-04 10:40:11 -0600261 screen = None,
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600262 storage = None,
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600263 stylus = None,
Sean McAllister94735372021-04-22 16:06:33 -0600264 touchpad = None):
265 hw_feat = {}
266
267 def _merge(name, feature):
268 if feature:
269 hw_feat[name] = getattr(feature, name)
270
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600271 _merge("bluetooth", bluetooth)
Sean McAllister55e703f2021-05-04 10:40:11 -0600272 _merge("camera", camera)
273 _merge("display", display)
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600274 _merge("embedded_controller", ec)
Sean McAllister55e703f2021-05-04 10:40:11 -0600275 _merge("fingerprint", fingerprint)
Sean McAllister94735372021-04-22 16:06:33 -0600276 _merge("form_factor", form_factor)
277 _merge("hotwording", hotwording)
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600278 _merge("keyboard", keyboard)
Sean McAllister55e703f2021-05-04 10:40:11 -0600279 _merge("screen", screen)
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600280 _merge("storage", storage)
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600281 _merge("stylus", stylus)
Sean McAllister94735372021-04-22 16:06:33 -0600282 _merge("touchpad", touchpad)
283
284 return _HW_FEAT(**hw_feat)
285
286hw_feat = struct(
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600287 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 McAllister55e703f2021-05-04 10:40:11 -0600292 create_fingerprint = _create_fingerprint,
Sean McAllister94735372021-04-22 16:06:33 -0600293 create_features = _create_features,
294 create_form_factor = _create_form_factor,
295 create_hotwording = _create_hotwording,
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600296 create_keyboard = _create_keyboard,
Sean McAllister55e703f2021-05-04 10:40:11 -0600297 create_screen = _create_screen,
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600298 create_storage = _create_storage,
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600299 create_stylus = _create_stylus,
Sean McAllister94735372021-04-22 16:06:33 -0600300 create_touchpad = _create_touchpad,
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600301
302 # export enums
303 camera_features = _camera_features,
304 embedded_controller = _EC,
Sean McAllister94735372021-04-22 16:06:33 -0600305 form_factor = _FORM_FACTOR,
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600306 keyboard_type = _KB_TYPE,
Sean McAllister9ecb21e2021-04-27 15:57:59 -0600307 present = _PRESENT,
308 storage = _STORAGE,
Sean McAllisterf54fdf22021-05-06 19:37:41 -0600309 stylus_type = _STYLUS_TYPE,
Sean McAllister55e703f2021-05-04 10:40:11 -0600310 location = _LOCATION,
Sean McAllister94735372021-04-22 16:06:33 -0600311)