Fix candidate build metadata and runtime safeguards
Offline project checks / check (push) Canceled after 0s
Offline project checks / check (push) Canceled after 0s
Repair kernel patch formatting and refresh kernel/camera package revisions and checksums. Integrate battery guard into generated images, correct charging controls and emergency shutdown handling, and reject invalid camera authentication configuration. Extend recipe validation and regression coverage, and update build and installation documentation. Kernel, camera and image compilation remain pending on the build server.
This commit is contained in:
@@ -49,9 +49,9 @@ class BatteryPolicy:
|
||||
if current_state is not None:
|
||||
self.charging_state = current_state
|
||||
|
||||
# Temperature safety cutoff: prevent thermal runaway and pouch swelling
|
||||
# Power-supply sysfs reports temperature in tenths of a degree Celsius.
|
||||
if temp is not None:
|
||||
deg_c = temp / 10.0 if temp > 150 else float(temp)
|
||||
deg_c = temp / 10.0
|
||||
if deg_c >= self.max_temp:
|
||||
self.thermal_throttled = True
|
||||
self.charging_state = False
|
||||
@@ -62,6 +62,9 @@ class BatteryPolicy:
|
||||
self.thermal_throttled = False
|
||||
else:
|
||||
return False
|
||||
elif self.thermal_throttled:
|
||||
self.charging_state = False
|
||||
return False
|
||||
|
||||
if capacity >= self.high:
|
||||
self.charging_state = False
|
||||
@@ -126,66 +129,46 @@ def is_battery_discharging(battery_dir, charger_dir=None):
|
||||
|
||||
|
||||
def trigger_shutdown(executor=subprocess.run):
|
||||
"""Sync filesystem and execute safe poweroff."""
|
||||
print('platina-battery-guard: CRITICAL: battery <= 5% and discharging; emergency poweroff to protect filesystem', flush=True)
|
||||
"""Request orderly init shutdown without relying on a display or suspend helper."""
|
||||
print('platina-battery-guard: CRITICAL: battery below shutdown threshold and discharging; emergency poweroff', flush=True)
|
||||
try:
|
||||
os.sync()
|
||||
except Exception:
|
||||
pass
|
||||
bin_candidates = [
|
||||
shutil.which('platina-poweroff'),
|
||||
'/usr/local/sbin/platina-poweroff',
|
||||
shutil.which('poweroff'),
|
||||
'/usr/sbin/poweroff',
|
||||
]
|
||||
target_bin = next((b for b in bin_candidates if b and (Path(b).exists() or shutil.which(b))), '/usr/sbin/poweroff')
|
||||
return executor([target_bin], check=True)
|
||||
candidates = ['/usr/sbin/poweroff', '/sbin/poweroff', shutil.which('poweroff')]
|
||||
attempted = set()
|
||||
for command in candidates:
|
||||
if not command or command in attempted:
|
||||
continue
|
||||
attempted.add(command)
|
||||
try:
|
||||
return executor([command], check=True, timeout=10)
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
continue
|
||||
raise RuntimeError('No orderly poweroff command succeeded')
|
||||
|
||||
|
||||
def set_charging(battery_dir, charger_dir, enable):
|
||||
applied = False
|
||||
val = '1' if enable else '0'
|
||||
|
||||
# 1. Try battery charging_enabled
|
||||
if battery_dir:
|
||||
for node in ('charging_enabled', 'charge_control_limit_max'):
|
||||
target = battery_dir / node
|
||||
if target.exists():
|
||||
try:
|
||||
target.write_text(val + '\n')
|
||||
applied = True
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
# 2. Try charger module charging_enabled
|
||||
if charger_dir:
|
||||
for node in ('charging_enabled',):
|
||||
target = charger_dir / node
|
||||
if target.exists():
|
||||
try:
|
||||
target.write_text(val + '\n')
|
||||
applied = True
|
||||
except OSError:
|
||||
pass
|
||||
# 3. Fallback: throttle charger current to 0 on disable
|
||||
if not enable:
|
||||
cur_target = charger_dir / 'current_max'
|
||||
if cur_target.exists():
|
||||
try:
|
||||
cur_target.write_text('0\n')
|
||||
applied = True
|
||||
except OSError:
|
||||
pass
|
||||
else:
|
||||
cur_target = charger_dir / 'current_max'
|
||||
if cur_target.exists():
|
||||
try:
|
||||
cur_target.write_text('1500000\n')
|
||||
applied = True
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
return applied
|
||||
"""Use charging switches only; USB input-current policy belongs to the driver/USB helper."""
|
||||
for supply in (battery_dir, charger_dir):
|
||||
if supply is None:
|
||||
continue
|
||||
behaviour = supply / 'charge_behaviour'
|
||||
try:
|
||||
supported = behaviour.read_text().replace('[', '').replace(']', '').split()
|
||||
if {'auto', 'inhibit-charge'} <= set(supported):
|
||||
behaviour.write_text(('auto' if enable else 'inhibit-charge') + '\n')
|
||||
return True
|
||||
except OSError:
|
||||
pass
|
||||
target = supply / 'charging_enabled'
|
||||
if target.exists():
|
||||
try:
|
||||
target.write_text(('1' if enable else '0') + '\n')
|
||||
return True
|
||||
except OSError:
|
||||
pass
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
@@ -219,16 +202,21 @@ def main():
|
||||
if capacity is not None:
|
||||
# Emergency low-battery safety shutdown: prevent brownout filesystem corruption
|
||||
if policy.should_shutdown(capacity, is_discharging=discharging):
|
||||
trigger_shutdown()
|
||||
break
|
||||
try:
|
||||
trigger_shutdown()
|
||||
except RuntimeError as error:
|
||||
print(f'platina-battery-guard: {error}; retrying next poll', file=sys.stderr, flush=True)
|
||||
# Keep monitoring until init actually terminates the daemon.
|
||||
|
||||
action = policy.evaluate(capacity, temp=temp)
|
||||
set_charging(battery_dir, charger_dir, action)
|
||||
if policy.thermal_throttled:
|
||||
applied = set_charging(battery_dir, charger_dir, action)
|
||||
if not applied:
|
||||
status_str = 'Charging control unavailable (no writable charging switch); input limit unchanged'
|
||||
elif policy.thermal_throttled:
|
||||
status_str = 'Charging paused (thermal limit exceeded)'
|
||||
else:
|
||||
status_str = 'Charging enabled' if action else 'Charging paused (protective threshold reached)'
|
||||
temp_str = f', temp={temp/10:.1f}°C' if temp is not None and temp > 150 else (f', temp={temp}°C' if temp is not None else '')
|
||||
temp_str = f', temp={temp/10:.1f}°C' if temp is not None else ''
|
||||
power_mode = 'discharging' if discharging else 'external-power'
|
||||
print(f'platina-battery-guard: capacity={capacity}%{temp_str}, power={power_mode}, threshold=[{args.low}%-{args.high}%], state={status_str}', flush=True)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user