Python library for working with the PD Buddy Sink Serial Console Configuration Interface
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

__init__.py 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. """Python bindings for PD Buddy Sink configuration"""
  2. from collections import namedtuple
  3. try:
  4. # Try importing enum from the standard library
  5. import enum
  6. # Make sure Flag is available
  7. enum.Flag
  8. except (ImportError, NameError, AttributeError):
  9. # If something above failed, try aenum instead
  10. import aenum as enum
  11. import serial
  12. import serial.tools.list_ports
  13. class Sink:
  14. """Interface for configuring a PD Buddy Sink"""
  15. vid = 0x1209
  16. pid = 0x9DB5
  17. def __init__(self, sp):
  18. """Open a serial port to communicate with the PD Buddy Sink
  19. :param sp: the serial port of the device
  20. :type sp: str or `serial.tools.list_ports.ListPortInfo`
  21. """
  22. try:
  23. self._port = serial.Serial(sp, baudrate=115200)
  24. except ValueError:
  25. self._port = serial.Serial(sp.device, baudrate=115200)
  26. # Put communications in a known state, cancelling any partially-entered
  27. # command that may be sitting in the buffer.
  28. self.send_command("\x04", newline=False)
  29. def __enter__(self):
  30. return self
  31. def __exit__(self, exc_type, exc_value, traceback):
  32. self._port.close()
  33. def send_command(self, cmd, newline=True):
  34. """Send a command to the PD Buddy Sink, returning the result
  35. :param cmd: the text to send to the Sink
  36. :param newline: whether to append a ``\r\n`` to the command
  37. :type cmd: str
  38. :type newline: bool
  39. :returns: a list of zero or more bytes objects, each being one line
  40. printed as a response to the command.
  41. """
  42. # Build the command
  43. cmd = cmd.encode("utf-8")
  44. if newline:
  45. cmd += b"\r\n"
  46. # Send the command
  47. self._port.write(cmd)
  48. self._port.flush()
  49. # Read the result
  50. answer = b""
  51. while not answer.endswith(b"PDBS) "):
  52. answer += self._port.read(1)
  53. answer = answer.split(b"\r\n")
  54. # Remove the echoed command and prompt
  55. answer = answer[1:-1]
  56. # Raise an exception if the command wasn't recognized
  57. if len(answer) and answer[0] == cmd.strip().split()[0] + b" ?":
  58. raise KeyError("command not found")
  59. return answer
  60. def close(self):
  61. """Close the serial port"""
  62. self._port.close()
  63. def help(self):
  64. """Returns the help text from the PD Buddy Sink"""
  65. return self.send_command("help")
  66. def license(self):
  67. """Returns the license text from the PD Buddy Sink"""
  68. return self.send_command("license")
  69. def boot(self):
  70. """Runs the PD Buddy Sink's DFU bootloader and closes the serial port"""
  71. self._port.write(b"boot\r\n")
  72. self._port.flush()
  73. self.close()
  74. def erase(self):
  75. """Synchronously erases all stored configuration from flash"""
  76. self.send_command("erase")
  77. def write(self):
  78. """Synchronously writes the contents of the configuration buffer to flash"""
  79. self.send_command("write")
  80. def load(self):
  81. """Loads the current configuration from flash into the buffer
  82. :raises: KeyError
  83. """
  84. text = self.send_command("load")
  85. if len(text) > 0 and text[0].startswith(b"No configuration"):
  86. raise KeyError("no configuration")
  87. def get_cfg(self, index=None):
  88. """Reads configuration from flash
  89. :param index: optional index of configuration object in flash to read
  90. :returns: a `SinkConfig` object
  91. """
  92. if index is None:
  93. cfg = self.send_command("get_cfg")
  94. else:
  95. cfg = self.send_command("get_cfg {}".format(index))
  96. return SinkConfig.from_text(cfg)
  97. def get_tmpcfg(self):
  98. """Reads the contents of the configuration buffer
  99. :returns: a `SinkConfig` object
  100. """
  101. cfg = self.send_command("get_tmpcfg")
  102. return SinkConfig.from_text(cfg)
  103. def clear_flags(self):
  104. """Clears all the flags in the configuration buffer"""
  105. self.send_command("clear_flags")
  106. def toggle_giveback(self):
  107. """Toggles the GiveBack flag in the configuration buffer"""
  108. self.send_command("toggle_giveback")
  109. def toggle_hv_preferred(self):
  110. """Toggles the HV_Preferred flag in the configuration buffer"""
  111. self.send_command("toggle_hv_preferred")
  112. def set_v(self, mv):
  113. """Sets the voltage of the configuration buffer, in millivolts"""
  114. out = self.send_command("set_v {}".format(mv))
  115. # If that command gave any output, that indicates an error. Raise an
  116. # exception to make that clear.
  117. if len(out):
  118. raise ValueError(out[0])
  119. def set_vrange(self, vmin, vmax):
  120. """Sets the min and max voltage of the configuration buffer, in millivolts"""
  121. # First, make sure we're sending numbers to the Sink in all valid cases
  122. if vmin is None and vmax is None:
  123. vmin = 0
  124. vmax = 0
  125. out = self.send_command("set_vrange {} {}".format(vmin, vmax))
  126. # If that command gave any output, that indicates an error. Raise an
  127. # exception to make that clear.
  128. if len(out):
  129. raise ValueError(out[0])
  130. def set_i(self, ma):
  131. """Sets the current of the configuration buffer, in milliamperes"""
  132. out = self.send_command("set_i {}".format(ma))
  133. # If that command gave any output, that indicates an error. Raise an
  134. # exception to make that clear.
  135. if len(out):
  136. raise ValueError(out[0])
  137. def set_p(self, mw):
  138. """Sets the power of the configuration buffer, in milliwatts"""
  139. out = self.send_command("set_p {}".format(mw))
  140. # If that command gave any output, that indicates an error. Raise an
  141. # exception to make that clear.
  142. if len(out):
  143. raise ValueError(out[0])
  144. def set_r(self, mr):
  145. """Sets the resistance of the configuration buffer, in milliohms"""
  146. out = self.send_command("set_r {}".format(mr))
  147. # If that command gave any output, that indicates an error. Raise an
  148. # exception to make that clear.
  149. if len(out):
  150. raise ValueError(out[0])
  151. def identify(self):
  152. """Blinks the LED quickly"""
  153. self.send_command("identify")
  154. @property
  155. def output(self):
  156. """The state of the Sink's output
  157. Raises KeyError if the ``output`` command is not available on the Sink.
  158. Raises ValueError if an invalid output is read.
  159. """
  160. value = self.send_command("output")
  161. if value[0] == b"enabled":
  162. return True
  163. elif value[0] == b"disabled":
  164. return False
  165. else:
  166. # If unexpected text is returned, raise an exception indicating a
  167. # firmware error
  168. raise ValueError("unknown output state")
  169. @output.setter
  170. def output(self, state):
  171. if state:
  172. self.send_command("output enable")
  173. else:
  174. self.send_command("output disable")
  175. def get_source_cap(self):
  176. """Gets the most recent Source_Capabilities read by the Sink"""
  177. return read_pdo_list(self.send_command("get_source_cap"))
  178. def set_tmpcfg(self, sc):
  179. """Writes a SinkConfig object to the device's configuration buffer
  180. Note: the value of the status field is ignored; it will always be
  181. `SinkStatus.VALID`.
  182. """
  183. # Set flags
  184. self.clear_flags()
  185. if sc.flags & SinkFlags.GIVEBACK:
  186. self.toggle_giveback()
  187. if sc.flags & SinkFlags.HV_PREFERRED:
  188. self.toggle_hv_preferred()
  189. # Set voltage
  190. self.set_v(sc.v)
  191. # Set voltage range
  192. self.set_vrange(sc.vmin, sc.vmax)
  193. if sc.idim is SinkDimension.CURRENT:
  194. # Set current
  195. self.set_i(sc.i)
  196. elif sc.idim is SinkDimension.POWER:
  197. # Set power
  198. self.set_p(sc.i)
  199. elif sc.idim is SinkDimension.RESISTANCE:
  200. # Set resistance
  201. self.set_r(sc.i)
  202. @classmethod
  203. def get_devices(cls):
  204. """Get an iterable of PD Buddy Sink devices
  205. :returns: an iterable of `serial.tools.list_ports.ListPortInfo` objects
  206. """
  207. return serial.tools.list_ports.grep("{:04X}:{:04X}".format(cls.vid,
  208. cls.pid))
  209. class SinkConfig(namedtuple("SinkConfig", "status flags v vmin vmax i idim")):
  210. """Python representation of a PD Buddy Sink configuration object
  211. ``status`` should be a `SinkStatus` object. ``flags`` should be zero or
  212. more `SinkFlags` values. ``v``, ``vmin``, and ``vmax`` are voltages in
  213. millivolts, ``i`` is the value used to set current in the appropriate
  214. milli- SI unit, and ``idim`` is the dimension of ``i``. `None` is also
  215. an acceptible value for any of the fields.
  216. """
  217. __slots__ = ()
  218. def __str__(self):
  219. """Print the SinkStatus in the manner of the configuration shell"""
  220. s = ""
  221. if self.status is not None:
  222. s += "status: "
  223. if self.status is SinkStatus.EMPTY:
  224. s += "empty"
  225. elif self.status is SinkStatus.VALID:
  226. s += "valid"
  227. elif self.status is SinkStatus.INVALID:
  228. s += "invalid"
  229. s += "\n"
  230. if self.flags is not None:
  231. s += "flags: "
  232. if self.flags is SinkFlags.NONE:
  233. s += "(none)"
  234. else:
  235. if self.flags & SinkFlags.GIVEBACK:
  236. s += "GiveBack"
  237. if self.flags & SinkFlags.HV_PREFERRED:
  238. s += "HV_Preferred"
  239. s += "\n"
  240. if self.v is not None:
  241. s += "v: {:.3f} V\n".format(self.v / 1000.0)
  242. if self.vmin is not None:
  243. s += "vmin: {:.3f} V\n".format(self.vmin / 1000.0)
  244. if self.vmax is not None:
  245. s += "vmax: {:.3f} V\n".format(self.vmax / 1000.0)
  246. if self.i is not None:
  247. if self.idim is SinkDimension.CURRENT:
  248. s += "i: {:.2f} A\n".format(self.i / 1000.0)
  249. if self.idim is SinkDimension.POWER:
  250. s += "p: {:.2f} W\n".format(self.i / 1000.0)
  251. if self.idim is SinkDimension.RESISTANCE:
  252. s += "r: {:.2f} \u03A9\n".format(self.i / 1000.0)
  253. # Return all but the last character of s to remove the trailing newline
  254. if s:
  255. return s[:-1]
  256. else:
  257. return "No configuration"
  258. @classmethod
  259. def from_text(cls, text):
  260. """Creates a SinkConfig from text returned by Sink.send_command
  261. :param text: the text to load
  262. :type text: a list of bytes objects
  263. :returns: a new `SinkConfig` object.
  264. :raises: IndexError
  265. """
  266. # Assume the parameters will all be None
  267. status = None
  268. flags = None
  269. v = None
  270. vmin = None
  271. vmax = None
  272. i = None
  273. idim = None
  274. # Iterate over all lines of text
  275. for line in text:
  276. # If the configuration said invalid index, raise an IndexError
  277. if line.startswith(b"Invalid index"):
  278. raise IndexError("configuration index out of range")
  279. # If there is no configuration, return an empty SinkConfig
  280. elif line.startswith(b"No configuration"):
  281. return cls(None, None, None, None, None, None, None)
  282. # If this line is the status field
  283. elif line.startswith(b"status: "):
  284. line = line.split()[1:]
  285. if line[0] == b"empty":
  286. status = SinkStatus.EMPTY
  287. elif line[0] == b"valid":
  288. status = SinkStatus.VALID
  289. elif line[0] == b"invalid":
  290. status = SinkStatus.INVALID
  291. # If this line is the flags field
  292. elif line.startswith(b"flags: "):
  293. line = line.split()[1:]
  294. flags = SinkFlags.NONE
  295. for word in line:
  296. if word == b"(none)":
  297. # If there are no flags set, stop looking
  298. break
  299. elif word == b"GiveBack":
  300. flags |= SinkFlags.GIVEBACK
  301. elif word == b"HV_Preferred":
  302. flags |= SinkFlags.HV_PREFERRED
  303. # If this line is the v field
  304. elif line.startswith(b"v: "):
  305. word = line.split()[1]
  306. v = round(1000*float(word))
  307. # If this line is the vmin field
  308. elif line.startswith(b"vmin: "):
  309. word = line.split()[1]
  310. vmin = round(1000*float(word))
  311. # If this line is the vmax field
  312. elif line.startswith(b"vmax: "):
  313. word = line.split()[1]
  314. vmax = round(1000*float(word))
  315. # If this line is the i field
  316. elif line.startswith(b"i: "):
  317. word = line.split()[1]
  318. i = round(1000*float(word))
  319. idim = SinkDimension.CURRENT
  320. # If this line is the p field
  321. elif line.startswith(b"p: "):
  322. word = line.split()[1]
  323. i = round(1000*float(word))
  324. idim = SinkDimension.POWER
  325. # If this line is the r field
  326. elif line.startswith(b"r: "):
  327. word = line.split()[1]
  328. i = round(1000*float(word))
  329. idim = SinkDimension.RESISTANCE
  330. # Create a new SinkConfig object with the values we just read
  331. return cls(status=status, flags=flags, v=v, vmin=vmin, vmax=vmax, i=i,
  332. idim=idim)
  333. class SinkStatus(enum.Enum):
  334. """Status field of a PD Buddy Sink configuration object"""
  335. EMPTY = 1
  336. VALID = 2
  337. INVALID = 3
  338. class SinkDimension(enum.Enum):
  339. """Dimension of the value used to set the current requested"""
  340. CURRENT = 1
  341. POWER = 2
  342. RESISTANCE = 3
  343. class SinkFlags(enum.Flag):
  344. """Flags field of a PD Buddy Sink configuration object"""
  345. NONE = 0
  346. GIVEBACK = enum.auto()
  347. HV_PREFERRED = enum.auto()
  348. class UnknownPDO(namedtuple("UnknownPDO", "value")):
  349. """A PDO of an unknown type
  350. ``value`` should be a 32-bit integer representing the PDO exactly as
  351. transmitted.
  352. """
  353. __slots__ = ()
  354. pdo_type = "unknown"
  355. def __str__(self):
  356. """Print the UnknownPDO in the manner of the configuration shell"""
  357. return "{:08X}".format(self.value)
  358. class SrcFixedPDO(namedtuple("SrcFixedPDO", "dual_role_pwr usb_suspend "
  359. "unconstrained_pwr usb_comms dual_role_data unchunked_ext_msg peak_i "
  360. "v i")):
  361. """A Source Fixed PDO
  362. ``dual_role_pwr``, ``usb_suspend``, ``unconstrained_pwr``,
  363. ``usb_comms``, ``dual_role_data``, and ``unchunked_ext_msg`` should be
  364. booleans. ``peak_i`` should be an integer in the range [0, 3]. ``v``
  365. is the voltage in millivolts, and ``i`` is the maximum current in
  366. milliamperes.
  367. """
  368. __slots__ = ()
  369. pdo_type = "fixed"
  370. def __str__(self):
  371. """Print the SrcFixedPDO in the manner of the configuration shell"""
  372. s = self.pdo_type + "\n"
  373. if self.dual_role_pwr:
  374. s += "\tdual_role_pwr: 1\n"
  375. if self.usb_suspend:
  376. s += "\tusb_suspend: 1\n"
  377. if self.unconstrained_pwr:
  378. s += "\tunconstrained_pwr: 1\n"
  379. if self.usb_comms:
  380. s += "\tusb_comms: 1\n"
  381. if self.dual_role_data:
  382. s += "\tdual_role_data: 1\n"
  383. if self.unchunked_ext_msg:
  384. s += "\tunchunked_ext_msg: 1\n"
  385. if self.peak_i:
  386. s += "\tpeak_i: {}\n".format(self.peak_i)
  387. s += "\tv: {:.2f} V\n".format(self.v / 1000.0)
  388. s += "\ti: {:.2f} A".format(self.i / 1000.0)
  389. return s
  390. class TypeCVirtualPDO(namedtuple("TypeCVirtualPDO", "i")):
  391. """A Type-C Current Virtual PDO
  392. ``i`` is the advertised current in milliamperes.
  393. """
  394. __slots__ = ()
  395. pdo_type = "typec_virtual"
  396. def __str__(self):
  397. """Print the TypeCVirtualPDO in the manner of the configuration shell"""
  398. s = self.pdo_type + "\n"
  399. s += "\ti: {:.2f} A".format(self.i / 1000.0)
  400. return s
  401. def read_pdo(text):
  402. """Create a PDO object from partial text returned by Sink.send_command"""
  403. # First, determine the PDO type
  404. pdo_type = text[0].split(b":")[-1].strip().decode("utf-8")
  405. if pdo_type == SrcFixedPDO.pdo_type:
  406. # Set default values (n.b. there are none for v and i)
  407. dual_role_pwr = False
  408. usb_suspend = False
  409. unconstrained_pwr = False
  410. usb_comms = False
  411. dual_role_data = False
  412. unchunked_ext_msg = False
  413. peak_i = 0
  414. # Load a SrcFixedPDO
  415. for line in text[1:]:
  416. fields = line.split(b":")
  417. fields[0] = fields[0].strip()
  418. fields[1] = fields[1].strip()
  419. if fields[0] == b"dual_role_pwr":
  420. dual_role_pwr = (fields[1] == b"1")
  421. elif fields[0] == b"usb_suspend":
  422. usb_suspend = (fields[1] == b"1")
  423. elif fields[0] == b"unconstrained_pwr":
  424. unconstrained_pwr = (fields[1] == b"1")
  425. elif fields[0] == b"usb_comms":
  426. usb_comms = (fields[1] == b"1")
  427. elif fields[0] == b"dual_role_data":
  428. dual_role_data = (fields[1] == b"1")
  429. elif fields[0] == b"unchunked_ext_msg":
  430. unchunked_ext_msg = (fields[1] == b"1")
  431. elif fields[0] == b"peak_i":
  432. peak_i = int(fields[1])
  433. elif fields[0] == b"v":
  434. v = round(1000*float(fields[1].split()[0]))
  435. elif fields[0] == b"i":
  436. i = round(1000*float(fields[1].split()[0]))
  437. # Make the SrcFixedPDO
  438. return SrcFixedPDO(
  439. dual_role_pwr=dual_role_pwr,
  440. usb_suspend=usb_suspend,
  441. unconstrained_pwr=unconstrained_pwr,
  442. usb_comms=usb_comms,
  443. dual_role_data=dual_role_data,
  444. unchunked_ext_msg=unchunked_ext_msg,
  445. peak_i=peak_i,
  446. v=v,
  447. i=i)
  448. elif pdo_type == TypeCVirtualPDO.pdo_type:
  449. # Load a TypeCVirtualPDO
  450. for line in text[1:]:
  451. fields = line.split(b":")
  452. fields[0] = fields[0].strip()
  453. fields[1] = fields[1].strip()
  454. if fields[0] == b"i":
  455. i = round(1000*float(fields[1].split()[0]))
  456. # Make the TypeCVirtualPDO
  457. return TypeCVirtualPDO(i=i)
  458. elif pdo_type == "No Source_Capabilities":
  459. return None
  460. else:
  461. # Make an UnknownPDO
  462. return UnknownPDO(value=int(pdo_type, 16))
  463. def read_pdo_list(text):
  464. """Create a list of PDOs from text returned by Sink.send_command"""
  465. # Get the lines where PDOs start
  466. pdo_start_list = []
  467. for index, line in enumerate(text):
  468. if not line.startswith(b"\t"):
  469. pdo_start_list.append(index)
  470. # Append the number of lines so the last slice will work right
  471. pdo_start_list.append(len(text))
  472. # Read the PDOs
  473. pdo_list = []
  474. for start, end in zip(pdo_start_list[:-1], pdo_start_list[1:]):
  475. pdo = read_pdo(text[start:end])
  476. if pdo is not None:
  477. pdo_list.append(pdo)
  478. return pdo_list
  479. def calculate_pdp(pdo_list):
  480. """Calculate the PDP in watts of a list of PDOs
  481. The result is only guaranteed to be correct if the power supply follows
  482. the USB Power Delivery standard. Since quite a few power supplies
  483. unfortunately do not, this can really only be considered an estimate.
  484. """
  485. max_power = 0
  486. # The Source Power Rules make it so the PDP can be determined by the
  487. # highest power available from any fixed supply PDO.
  488. for pdo in pdo_list:
  489. if pdo.pdo_type == "fixed":
  490. max_power = max(max_power, pdo.v / 1000.0 * pdo.i / 1000.0)
  491. elif pdo.pdo_type == "typec_virtual":
  492. max_power = max(max_power, 5.0 * pdo.i / 1000.0)
  493. return max_power
  494. def follows_power_rules(pdo_list):
  495. """Test whether a list of PDOs follows the Power Rules for PD 2.0
  496. This function is a false-biased approximation; that is, when it returns
  497. False it is definitely correct, but when it returns True it might be
  498. incorrect.
  499. """
  500. # First, estimate the PDP assuming the rules are being followed
  501. pdp = calculate_pdp(pdo_list)
  502. # If there's a typec_virtual PDO, there's no Power Delivery so the Power
  503. # Rules cannot be violated. In truth they're not really being followed
  504. # either since they only apply to Power Delivery, but returning True here
  505. # seems like the safer option.
  506. if pdo_list and pdo_list[0].pdo_type == "typec_virtual":
  507. return True
  508. # Make sure nothing exceeds the PDP
  509. for pdo in pdo_list:
  510. if pdo.pdo_type == "fixed":
  511. if pdp < pdo.v / 1000.0 * pdo.i / 1000.0:
  512. return False
  513. # TODO: in the future, there will be more types of PDO checked here
  514. # Check that the fixed supply PDOs look right
  515. seen_5v = False
  516. seen_9v = False
  517. seen_15v = False
  518. seen_20v = False
  519. seen_normative_voltages = False
  520. if pdp == 0:
  521. # No power is fine
  522. seen_normative_voltages = True
  523. elif pdp <= 15:
  524. # Below 15 W, make sure the PDP is available at 5 V.
  525. for pdo in pdo_list:
  526. if pdo.pdo_type == "fixed" and pdo.v == 5000:
  527. seen_5v = True
  528. if pdo.v / 1000.0 * pdo.i / 1000.0 != pdp:
  529. return False
  530. seen_normative_voltages = seen_5v
  531. elif pdp <= 27:
  532. # Between 15 and 27 W, make sure at least 3 A is available at 5 V and
  533. # the PDP is available at 9 V.
  534. for pdo in pdo_list:
  535. if pdo.pdo_type == "fixed" and pdo.v == 5000:
  536. seen_5v = True
  537. if pdo.i < 3000.0:
  538. return False
  539. elif pdo.pdo_type == "fixed" and pdo.v == 9000:
  540. seen_9v = True
  541. if pdo.v / 1000.0 * pdo.i / 1000.0 != pdp:
  542. return False
  543. seen_normative_voltages = seen_5v and seen_9v
  544. elif pdp <= 45:
  545. # Between 27 and 45 W, make sure at least 3 A is available at 5 and
  546. # 9 V, and the PDP is available at 15 V.
  547. for pdo in pdo_list:
  548. if pdo.pdo_type == "fixed" and pdo.v == 5000:
  549. seen_5v = True
  550. if pdo.i < 3000.0:
  551. return False
  552. elif pdo.pdo_type == "fixed" and pdo.v == 9000:
  553. seen_9v = True
  554. if pdo.i < 3000.0:
  555. return False
  556. elif pdo.pdo_type == "fixed" and pdo.v == 15000:
  557. seen_15v = True
  558. if pdo.v / 1000.0 * pdo.i / 1000.0 != pdp:
  559. return False
  560. seen_normative_voltages = seen_5v and seen_9v and seen_15v
  561. else:
  562. # Above 45 W, make sure at least 3 A is available at 5, 9, and 15 V,
  563. # and the PDP is available at 20 V.
  564. for pdo in pdo_list:
  565. if pdo.pdo_type == "fixed" and pdo.v == 5000:
  566. seen_5v = True
  567. if pdo.i < 3000.0:
  568. return False
  569. elif pdo.pdo_type == "fixed" and pdo.v == 9000:
  570. seen_9v = True
  571. if pdo.i < 3000.0:
  572. return False
  573. elif pdo.pdo_type == "fixed" and pdo.v == 15000:
  574. seen_15v = True
  575. if pdo.i < 3000.0:
  576. return False
  577. elif pdo.pdo_type == "fixed" and pdo.v == 20000:
  578. seen_20v = True
  579. if pdo.v / 1000.0 * pdo.i / 1000.0 != pdp:
  580. return False
  581. seen_normative_voltages = seen_5v and seen_9v and seen_15v and seen_20v
  582. if not seen_normative_voltages:
  583. return False
  584. # TODO: there are several things this currently doesn't test, such as
  585. # variable and battery PDOs.
  586. return True