Files
mi8-platina-linux/scripts/phone-poweroff.py
T
shenandshen 810a467ad3 fix(platina): resolve touchscreen, dual panel, charging reboot, battery guard, camera auth, and internal SIM cellular data
- Add patch 0040: cut panel VSP/VSN always-on rails, attach panel follower to Synaptics TCM touch, add Novatek NT36672A touch node, add BOE panel DTS, and extend DVFS up to 1.84/2.2 GHz.
- Prevent Qualcomm ABL infinite reboot loop on poweroff while charger connected via standby detection in phone-poweroff.py.
- Implement HTTP Basic Auth in camera web preview server with secure digest verification.
- Add battery guard daemon and unit tests to maintain SoC between 50% and 65%.
- Implement Option 2 for cellular data: disable ZRAM swap to prevent Hexagon DMA fragmentation crash, add modem modules to initfs, enable ModemManager/NetworkManager WWAN packages and services, and add phone-cellular-setup.py CLI utility with unit tests.
- Update recipe checksums and verify 109 unit tests and public index boundaries.
2026-11-07 23:27:43 -08:00

84 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Show the verified Power off screen, then request an orderly init shutdown."""
import argparse
import fcntl
import os
from pathlib import Path
import runpy
import signal
import subprocess
import sys
import time
def is_charger_connected():
for name in ('pm660-charger', 'battery', 'qpnp-smbcharger', 'usb'):
supply = Path(f'/sys/class/power_supply/{name}')
if not supply.exists():
continue
try:
online = supply / 'online'
if online.exists() and online.read_text().strip() in ('1', 'true'):
return True
status = supply / 'status'
if status.exists() and status.read_text().strip() in ('Charging', 'Full'):
return True
except OSError:
pass
return False
def enter_charger_standby():
print('platina-poweroff: external power connected; entering low-power suspend standby', flush=True)
state_file = Path('/sys/power/state')
if state_file.exists():
for mode in ('mem', 'freeze'):
try:
state_file.write_text(mode + '\n')
print(f'platina-poweroff: woke from {mode} standby', flush=True)
return
except OSError:
continue
try:
subprocess.run(['pm-suspend'], check=True, timeout=10)
except (OSError, subprocess.SubprocessError):
pass
def request_shutdown(force=False, standby=False):
if not force and (standby or is_charger_connected()):
enter_charger_standby()
return
subprocess.run(['/usr/sbin/poweroff'], check=True, timeout=10)
print('platina-poweroff: init accepted shutdown; keeping screen until shutdown', flush=True)
# Shutdown should terminate this process. If it does not, restore the display
# instead of leaving an indefinite claim that the phone is already off.
time.sleep(30)
raise RuntimeError('Shutdown did not finish within 30 seconds')
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('--preview', action='store_true', help='Show the screen for 5s without shutting down')
parser.add_argument('--force', action='store_true', help='Force immediate poweroff even if external charger is attached')
parser.add_argument('--standby', action='store_true', help='Enter low-power suspend standby rather than full poweroff')
args = parser.parse_args()
if os.geteuid() != 0:
raise ValueError('Run on the phone as root')
signal.signal(signal.SIGTERM, lambda *_: sys.exit(0))
control = runpy.run_path('/usr/local/sbin/platina-display', run_name='poweroff_display')
directory = Path('/usr/local/share/platina/current/poweroff')
with Path('/run/platina-poweroff.lock').open('a') as lock:
fcntl.flock(lock, fcntl.LOCK_EX | fcntl.LOCK_NB)
with control['device']() as fb:
control['show'](fb, directory, 5 if args.preview else 3, 25,
after_display=None if args.preview else lambda: request_shutdown(force=args.force, standby=args.standby))
if __name__ == '__main__':
try:
main()
except (OSError, ValueError, RuntimeError, subprocess.SubprocessError) as error:
print('platina-poweroff:', error, file=sys.stderr)
raise SystemExit(1)