Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion firmware/main/hal/board/stackchan.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ class Pmic : public Axp2101 {
// (W1C) so subsequent polls only fire on new presses.
bool ConsumePekLongPress()
{
uint8_t status = ReadReg(0x49);
// ReadReg() returns 0xFF on I2C failure, which would look like every
// IRQ bit is set and falsely trigger a shutdown. ReadRegs() leaves the
// caller-provided buffer untouched on failure, so initialise it to a
// safe value and fail closed when the PMIC cannot be read.
uint8_t status = 0;
ReadRegs(0x49, &status, 1);
if (status & 0x04) {
WriteReg(0x49, 0x04);
return true;
Expand Down
23 changes: 23 additions & 0 deletions firmware/tests/test_power_key_fail_safe.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""Host-side source contract for fail-safe PMIC power-key polling."""

from pathlib import Path
import unittest


ROOT = Path(__file__).resolve().parents[1]
BOARD = (ROOT / "main/hal/board/stackchan.cc").read_text()


class TestPowerKeyFailSafe(unittest.TestCase):
def test_failed_status_read_cannot_look_like_a_long_press(self):
handler = BOARD.split("bool ConsumePekLongPress()", 1)[1].split(
"bool IsExternalPowerConnected()", 1
)[0]

self.assertIn("uint8_t status = 0;", handler)
self.assertIn("ReadRegs(0x49, &status, 1);", handler)
self.assertNotIn("ReadReg(0x49)", handler)


if __name__ == "__main__":
unittest.main()