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.
		
		
		
		
			
				
					475 lines
				
				19 KiB
			
		
		
			
		
	
	
					475 lines
				
				19 KiB
			| 
								 
											6 years ago
										 
									 | 
							
								#!/usr/bin/env python3
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								import os
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								import json
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								import queue
							 | 
						||
| 
								 | 
							
								import threading
							 | 
						||
| 
								 | 
							
								import time
							 | 
						||
| 
								 | 
							
								from collections import OrderedDict, namedtuple
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								from pathlib import Path
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								import psutil
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								import cereal.messaging as messaging
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								from cereal import log
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from cereal.services import SERVICE_LIST
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.common.dict_helpers import strip_deprecated_keys
							 | 
						||
| 
								 | 
							
								from openpilot.common.filter_simple import FirstOrderFilter
							 | 
						||
| 
								 | 
							
								from openpilot.common.params import Params
							 | 
						||
| 
								 | 
							
								from openpilot.common.realtime import DT_TRML
							 | 
						||
| 
								 | 
							
								from openpilot.selfdrive.controls.lib.alertmanager import set_offroad_alert
							 | 
						||
| 
								 | 
							
								from openpilot.system.hardware import HARDWARE, TICI, AGNOS
							 | 
						||
| 
								 | 
							
								from openpilot.system.loggerd.config import get_available_percent
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								from openpilot.system.statsd import statlog
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.common.swaglog import cloudlog
							 | 
						||
| 
								 
											1 year ago
										 
									 | 
							
								from openpilot.system.thermald.power_monitoring import PowerMonitoring
							 | 
						||
| 
								 | 
							
								from openpilot.system.thermald.fan_controller import TiciFanController
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								from openpilot.system.version import terms_version, training_version
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								ThermalStatus = log.DeviceState.ThermalStatus
							 | 
						||
| 
								 | 
							
								NetworkType = log.DeviceState.NetworkType
							 | 
						||
| 
								 | 
							
								NetworkStrength = log.DeviceState.NetworkStrength
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								CURRENT_TAU = 15.   # 15s time constant
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								TEMP_TAU = 5.   # 5s time constant
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								DISCONNECT_TIMEOUT = 5.  # wait 5 seconds before going offroad after disconnect so you get an alert
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								PANDA_STATES_TIMEOUT = round(1000 / SERVICE_LIST['pandaStates'].frequency * 1.5)  # 1.5x the expected pandaState frequency
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								ThermalBand = namedtuple("ThermalBand", ['min_temp', 'max_temp'])
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								HardwareState = namedtuple("HardwareState", ['network_type', 'network_info', 'network_strength', 'network_stats',
							 | 
						||
| 
								 | 
							
								                                             'network_metered', 'nvme_temps', 'modem_temps'])
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								# List of thermal bands. We will stay within this region as long as we are within the bounds.
							 | 
						||
| 
								 | 
							
								# When exiting the bounds, we'll jump to the lower or higher band. Bands are ordered in the dict.
							 | 
						||
| 
								 | 
							
								THERMAL_BANDS = OrderedDict({
							 | 
						||
| 
								 | 
							
								  ThermalStatus.green: ThermalBand(None, 80.0),
							 | 
						||
| 
								 | 
							
								  ThermalStatus.yellow: ThermalBand(75.0, 96.0),
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  ThermalStatus.red: ThermalBand(88.0, 107.),
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  ThermalStatus.danger: ThermalBand(94.0, None),
							 | 
						||
| 
								 | 
							
								})
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								# Override to highest thermal band when offroad and above this temp
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								OFFROAD_DANGER_TEMP = 75
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								prev_offroad_states: dict[str, tuple[bool, str | None]] = {}
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								tz_by_type: dict[str, int] | None = None
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								def populate_tz_by_type():
							 | 
						||
| 
								 | 
							
								  global tz_by_type
							 | 
						||
| 
								 | 
							
								  tz_by_type = {}
							 | 
						||
| 
								 | 
							
								  for n in os.listdir("/sys/devices/virtual/thermal"):
							 | 
						||
| 
								 | 
							
								    if not n.startswith("thermal_zone"):
							 | 
						||
| 
								 | 
							
								      continue
							 | 
						||
| 
								 | 
							
								    with open(os.path.join("/sys/devices/virtual/thermal", n, "type")) as f:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      tz_by_type[f.read().strip()] = int(n.removeprefix("thermal_zone"))
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								def read_tz(x):
							 | 
						||
| 
								 | 
							
								  if x is None:
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    return 0
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  if isinstance(x, str):
							 | 
						||
| 
								 | 
							
								    if tz_by_type is None:
							 | 
						||
| 
								 | 
							
								      populate_tz_by_type()
							 | 
						||
| 
								 | 
							
								    x = tz_by_type[x]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  try:
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    with open(f"/sys/devices/virtual/thermal/thermal_zone{x}/temp") as f:
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      return int(f.read())
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  except FileNotFoundError:
							 | 
						||
| 
								 | 
							
								    return 0
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								def read_thermal(thermal_config):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  dat = messaging.new_message('deviceState', valid=True)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  dat.deviceState.cpuTempC = [read_tz(z) / thermal_config.cpu[1] for z in thermal_config.cpu[0]]
							 | 
						||
| 
								 | 
							
								  dat.deviceState.gpuTempC = [read_tz(z) / thermal_config.gpu[1] for z in thermal_config.gpu[0]]
							 | 
						||
| 
								 | 
							
								  dat.deviceState.memoryTempC = read_tz(thermal_config.mem[0]) / thermal_config.mem[1]
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  dat.deviceState.pmicTempC = [read_tz(z) / thermal_config.pmic[1] for z in thermal_config.pmic[0]]
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  return dat
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								def set_offroad_alert_if_changed(offroad_alert: str, show_alert: bool, extra_text: str | None=None):
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  if prev_offroad_states.get(offroad_alert, None) == (show_alert, extra_text):
							 | 
						||
| 
								 | 
							
								    return
							 | 
						||
| 
								 | 
							
								  prev_offroad_states[offroad_alert] = (show_alert, extra_text)
							 | 
						||
| 
								 | 
							
								  set_offroad_alert(offroad_alert, show_alert, extra_text)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								def hw_state_thread(end_event, hw_queue):
							 | 
						||
| 
								 | 
							
								  """Handles non critical hardware state, and sends over queue"""
							 | 
						||
| 
								 | 
							
								  count = 0
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  prev_hw_state = None
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  modem_version = None
							 | 
						||
| 
								 | 
							
								  modem_nv = None
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  modem_configured = False
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  modem_restarted = False
							 | 
						||
| 
								 | 
							
								  modem_missing_count = 0
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  while not end_event.is_set():
							 | 
						||
| 
								 | 
							
								    # these are expensive calls. update every 10s
							 | 
						||
| 
								 | 
							
								    if (count % int(10. / DT_TRML)) == 0:
							 | 
						||
| 
								 | 
							
								      try:
							 | 
						||
| 
								 | 
							
								        network_type = HARDWARE.get_network_type()
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        modem_temps = HARDWARE.get_modem_temperatures()
							 | 
						||
| 
								 | 
							
								        if len(modem_temps) == 0 and prev_hw_state is not None:
							 | 
						||
| 
								 | 
							
								          modem_temps = prev_hw_state.modem_temps
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        # Log modem version once
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								        if AGNOS and ((modem_version is None) or (modem_nv is None)):
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								          modem_version = HARDWARE.get_modem_version()
							 | 
						||
| 
								 | 
							
								          modem_nv = HARDWARE.get_modem_nv()
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								          if (modem_version is not None) and (modem_nv is not None):
							 | 
						||
| 
								 | 
							
								            cloudlog.event("modem version", version=modem_version, nv=modem_nv)
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								          else:
							 | 
						||
| 
								 | 
							
								            if not modem_restarted:
							 | 
						||
| 
								 | 
							
								              # TODO: we may be able to remove this with a MM update
							 | 
						||
| 
								 | 
							
								              # ModemManager's probing on startup can fail
							 | 
						||
| 
								 | 
							
								              # rarely, restart the service to probe again.
							 | 
						||
| 
								 | 
							
								              modem_missing_count += 1
							 | 
						||
| 
								 | 
							
								              if modem_missing_count > 3:
							 | 
						||
| 
								 | 
							
								                modem_restarted = True
							 | 
						||
| 
								 | 
							
								                cloudlog.event("restarting ModemManager")
							 | 
						||
| 
								 | 
							
								                os.system("sudo systemctl restart --no-block ModemManager")
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								        tx, rx = HARDWARE.get_modem_data_usage()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        hw_state = HardwareState(
							 | 
						||
| 
								 | 
							
								          network_type=network_type,
							 | 
						||
| 
								 | 
							
								          network_info=HARDWARE.get_network_info(),
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								          network_strength=HARDWARE.get_network_strength(network_type),
							 | 
						||
| 
								 | 
							
								          network_stats={'wwanTx': tx, 'wwanRx': rx},
							 | 
						||
| 
								 | 
							
								          network_metered=HARDWARE.get_network_metered(network_type),
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								          nvme_temps=HARDWARE.get_nvme_temperatures(),
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								          modem_temps=modem_temps,
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        )
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        try:
							 | 
						||
| 
								 | 
							
								          hw_queue.put_nowait(hw_state)
							 | 
						||
| 
								 | 
							
								        except queue.Full:
							 | 
						||
| 
								 | 
							
								          pass
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        # TODO: remove this once the config is in AGNOS
							 | 
						||
| 
								 | 
							
								        if not modem_configured and len(HARDWARE.get_sim_info().get('sim_id', '')) > 0:
							 | 
						||
| 
								 | 
							
								          cloudlog.warning("configuring modem")
							 | 
						||
| 
								 | 
							
								          HARDWARE.configure_modem()
							 | 
						||
| 
								 | 
							
								          modem_configured = True
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        prev_hw_state = hw_state
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      except Exception:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        cloudlog.exception("Error getting hardware state")
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    count += 1
							 | 
						||
| 
								 | 
							
								    time.sleep(DT_TRML)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								def thermald_thread(end_event, hw_queue) -> None:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  pm = messaging.PubMaster(['deviceState'])
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  sm = messaging.SubMaster(["peripheralState", "gpsLocationExternal", "controlsState", "pandaStates"], poll="pandaStates")
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  count = 0
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  onroad_conditions: dict[str, bool] = {
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    "ignition": False,
							 | 
						||
| 
								 | 
							
								  }
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  startup_conditions: dict[str, bool] = {}
							 | 
						||
| 
								 | 
							
								  startup_conditions_prev: dict[str, bool] = {}
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  off_ts: float | None = None
							 | 
						||
| 
								 | 
							
								  started_ts: float | None = None
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  started_seen = False
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  startup_blocked_ts: float | None = None
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  thermal_status = ThermalStatus.yellow
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  last_hw_state = HardwareState(
							 | 
						||
| 
								 | 
							
								    network_type=NetworkType.none,
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    network_info=None,
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    network_metered=False,
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    network_strength=NetworkStrength.unknown,
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    network_stats={'wwanTx': -1, 'wwanRx': -1},
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    nvme_temps=[],
							 | 
						||
| 
								 | 
							
								    modem_temps=[],
							 | 
						||
| 
								 | 
							
								  )
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								  all_temp_filter = FirstOrderFilter(0., TEMP_TAU, DT_TRML, initialized=False)
							 | 
						||
| 
								 | 
							
								  offroad_temp_filter = FirstOrderFilter(0., TEMP_TAU, DT_TRML, initialized=False)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								  should_start_prev = False
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  in_car = False
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  engaged_prev = False
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								  params = Params()
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  power_monitor = PowerMonitoring()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  HARDWARE.initialize_hardware()
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								  thermal_config = HARDWARE.get_thermal_config()
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  fan_controller = None
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								  while not end_event.is_set():
							 | 
						||
| 
								 | 
							
								    sm.update(PANDA_STATES_TIMEOUT)
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    pandaStates = sm['pandaStates']
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    peripheralState = sm['peripheralState']
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    peripheral_panda_present = peripheralState.pandaType != log.PandaState.PandaType.unknown
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if sm.updated['pandaStates'] and len(pandaStates) > 0:
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      # Set ignition based on any panda connected
							 | 
						||
| 
								 | 
							
								      onroad_conditions["ignition"] = any(ps.ignitionLine or ps.ignitionCan for ps in pandaStates if ps.pandaType != log.PandaState.PandaType.unknown)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      pandaState = pandaStates[0]
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      in_car = pandaState.harnessStatus != log.PandaState.HarnessStatus.notConnected
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      # Setup fan handler on first connect to panda
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      if fan_controller is None and peripheral_panda_present:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        if TICI:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								          fan_controller = TiciFanController()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    elif (time.monotonic() - sm.recv_time['pandaStates']) > DISCONNECT_TIMEOUT:
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      if onroad_conditions["ignition"]:
							 | 
						||
| 
								 | 
							
								        onroad_conditions["ignition"] = False
							 | 
						||
| 
								 | 
							
								        cloudlog.error("panda timed out onroad")
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # Run at 2Hz, plus rising edge of ignition
							 | 
						||
| 
								 | 
							
								    ign_edge = started_ts is None and onroad_conditions["ignition"]
							 | 
						||
| 
								 | 
							
								    if (sm.frame % round(SERVICE_LIST['pandaStates'].frequency * DT_TRML) != 0) and not ign_edge:
							 | 
						||
| 
								 | 
							
								      continue
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    msg = read_thermal(thermal_config)
							 | 
						||
| 
								 | 
							
								    msg.deviceState.deviceType = HARDWARE.get_device_type()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    try:
							 | 
						||
| 
								 | 
							
								      last_hw_state = hw_queue.get_nowait()
							 | 
						||
| 
								 | 
							
								    except queue.Empty:
							 | 
						||
| 
								 | 
							
								      pass
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    msg.deviceState.freeSpacePercent = get_available_percent(default=100.0)
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    msg.deviceState.memoryUsagePercent = int(round(psutil.virtual_memory().percent))
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    msg.deviceState.gpuUsagePercent = int(round(HARDWARE.get_gpu_usage_percent()))
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    online_cpu_usage = [int(round(n)) for n in psutil.cpu_percent(percpu=True)]
							 | 
						||
| 
								 | 
							
								    offline_cpu_usage = [0., ] * (len(msg.deviceState.cpuTempC) - len(online_cpu_usage))
							 | 
						||
| 
								 | 
							
								    msg.deviceState.cpuUsagePercent = online_cpu_usage + offline_cpu_usage
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    msg.deviceState.networkType = last_hw_state.network_type
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    msg.deviceState.networkMetered = last_hw_state.network_metered
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    msg.deviceState.networkStrength = last_hw_state.network_strength
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    msg.deviceState.networkStats = last_hw_state.network_stats
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if last_hw_state.network_info is not None:
							 | 
						||
| 
								 | 
							
								      msg.deviceState.networkInfo = last_hw_state.network_info
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    msg.deviceState.nvmeTempC = last_hw_state.nvme_temps
							 | 
						||
| 
								 | 
							
								    msg.deviceState.modemTempC = last_hw_state.modem_temps
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    msg.deviceState.screenBrightnessPercent = HARDWARE.get_screen_brightness()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # this subset is only used for offroad
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    temp_sources = [
							 | 
						||
| 
								 | 
							
								      msg.deviceState.memoryTempC,
							 | 
						||
| 
								 | 
							
								      max(msg.deviceState.cpuTempC),
							 | 
						||
| 
								 | 
							
								      max(msg.deviceState.gpuTempC),
							 | 
						||
| 
								 | 
							
								    ]
							 | 
						||
| 
								 | 
							
								    offroad_comp_temp = offroad_temp_filter.update(max(temp_sources))
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    # this drives the thermal status while onroad
							 | 
						||
| 
								 | 
							
								    temp_sources.append(max(msg.deviceState.pmicTempC))
							 | 
						||
| 
								 | 
							
								    all_comp_temp = all_temp_filter.update(max(temp_sources))
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    msg.deviceState.maxTempC = all_comp_temp
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if fan_controller is not None:
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      msg.deviceState.fanSpeedPercentDesired = fan_controller.update(all_comp_temp, onroad_conditions["ignition"])
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    is_offroad_for_5_min = (started_ts is None) and ((not started_seen) or (off_ts is None) or (time.monotonic() - off_ts > 60 * 5))
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    if is_offroad_for_5_min and offroad_comp_temp > OFFROAD_DANGER_TEMP:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								      # if device is offroad and already hot without the extra onroad load,
							 | 
						||
| 
								 | 
							
								      # we want to cool down first before increasing load
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      thermal_status = ThermalStatus.danger
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    else:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      current_band = THERMAL_BANDS[thermal_status]
							 | 
						||
| 
								 | 
							
								      band_idx = list(THERMAL_BANDS.keys()).index(thermal_status)
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      if current_band.min_temp is not None and all_comp_temp < current_band.min_temp:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        thermal_status = list(THERMAL_BANDS.keys())[band_idx - 1]
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								      elif current_band.max_temp is not None and all_comp_temp > current_band.max_temp:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								        thermal_status = list(THERMAL_BANDS.keys())[band_idx + 1]
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    # **** starting logic ****
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    startup_conditions["up_to_date"] = params.get("Offroad_ConnectivityNeeded") is None or params.get_bool("DisableUpdates") or params.get_bool("SnoozeUpdate")
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    startup_conditions["not_uninstalling"] = not params.get_bool("DoUninstall")
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    startup_conditions["accepted_terms"] = params.get("HasAcceptedTerms") == terms_version
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    # with 2% left, we killall, otherwise the phone will take a long time to boot
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    startup_conditions["free_space"] = msg.deviceState.freeSpacePercent > 2
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    startup_conditions["completed_training"] = params.get("CompletedTrainingVersion") == training_version
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    startup_conditions["not_driver_view"] = not params.get_bool("IsDriverViewEnabled")
							 | 
						||
| 
								 | 
							
								    startup_conditions["not_taking_snapshot"] = not params.get_bool("IsTakingSnapshot")
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    # must be at an engageable thermal band to go onroad
							 | 
						||
| 
								 | 
							
								    startup_conditions["device_temp_engageable"] = thermal_status < ThermalStatus.red
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # ensure device is fully booted
							 | 
						||
| 
								 | 
							
								    startup_conditions["device_booted"] = startup_conditions.get("device_booted", False) or HARDWARE.booted()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    # if the temperature enters the danger zone, go offroad to cool down
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    onroad_conditions["device_temp_good"] = thermal_status < ThermalStatus.danger
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    extra_text = f"{offroad_comp_temp:.1f}C"
							 | 
						||
| 
								 | 
							
								    show_alert = (not onroad_conditions["device_temp_good"] or not startup_conditions["device_temp_engageable"]) and onroad_conditions["ignition"]
							 | 
						||
| 
								 | 
							
								    set_offroad_alert_if_changed("Offroad_TemperatureTooHigh", show_alert, extra_text=extra_text)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    # TODO: this should move to TICI.initialize_hardware, but we currently can't import params there
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    if TICI:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      if not os.path.isfile("/persist/comma/living-in-the-moment"):
							 | 
						||
| 
								 | 
							
								        if not Path("/data/media").is_mount():
							 | 
						||
| 
								 | 
							
								          set_offroad_alert_if_changed("Offroad_StorageMissing", True)
							 | 
						||
| 
								 | 
							
								        else:
							 | 
						||
| 
								 | 
							
								          # check for bad NVMe
							 | 
						||
| 
								 | 
							
								          try:
							 | 
						||
| 
								 | 
							
								            with open("/sys/block/nvme0n1/device/model") as f:
							 | 
						||
| 
								 | 
							
								              model = f.read().strip()
							 | 
						||
| 
								 | 
							
								            if not model.startswith("Samsung SSD 980") and params.get("Offroad_BadNvme") is None:
							 | 
						||
| 
								 | 
							
								              set_offroad_alert_if_changed("Offroad_BadNvme", True)
							 | 
						||
| 
								 | 
							
								              cloudlog.event("Unsupported NVMe", model=model, error=True)
							 | 
						||
| 
								 | 
							
								          except Exception:
							 | 
						||
| 
								 | 
							
								            pass
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    # Handle offroad/onroad transition
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    should_start = all(onroad_conditions.values())
							 | 
						||
| 
								 | 
							
								    if started_ts is None:
							 | 
						||
| 
								 | 
							
								      should_start = should_start and all(startup_conditions.values())
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    if should_start != should_start_prev or (count == 0):
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      params.put_bool("IsEngaged", False)
							 | 
						||
| 
								 | 
							
								      engaged_prev = False
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								      HARDWARE.set_power_save(not should_start)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    if sm.updated['controlsState']:
							 | 
						||
| 
								 | 
							
								      engaged = sm['controlsState'].enabled
							 | 
						||
| 
								 | 
							
								      if engaged != engaged_prev:
							 | 
						||
| 
								 | 
							
								        params.put_bool("IsEngaged", engaged)
							 | 
						||
| 
								 | 
							
								        engaged_prev = engaged
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      try:
							 | 
						||
| 
								 | 
							
								        with open('/dev/kmsg', 'w') as kmsg:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								          kmsg.write(f"<3>[thermald] engaged: {engaged}\n")
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      except Exception:
							 | 
						||
| 
								 | 
							
								        pass
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    if should_start:
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      off_ts = None
							 | 
						||
| 
								 | 
							
								      if started_ts is None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								        started_ts = time.monotonic()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								        started_seen = True
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								        if startup_blocked_ts is not None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								          cloudlog.event("Startup after block", block_duration=(time.monotonic() - startup_blocked_ts),
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								                         startup_conditions=startup_conditions, onroad_conditions=onroad_conditions,
							 | 
						||
| 
								 | 
							
								                         startup_conditions_prev=startup_conditions_prev, error=True)
							 | 
						||
| 
								 | 
							
								      startup_blocked_ts = None
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    else:
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      if onroad_conditions["ignition"] and (startup_conditions != startup_conditions_prev):
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								        cloudlog.event("Startup blocked", startup_conditions=startup_conditions, onroad_conditions=onroad_conditions, error=True)
							 | 
						||
| 
								 | 
							
								        startup_conditions_prev = startup_conditions.copy()
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								        startup_blocked_ts = time.monotonic()
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								      started_ts = None
							 | 
						||
| 
								 | 
							
								      if off_ts is None:
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								        off_ts = time.monotonic()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    # Offroad power monitoring
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    voltage = None if peripheralState.pandaType == log.PandaState.PandaType.unknown else peripheralState.voltage
							 | 
						||
| 
								 | 
							
								    power_monitor.calculate(voltage, onroad_conditions["ignition"])
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    msg.deviceState.offroadPowerUsageUwh = power_monitor.get_power_used()
							 | 
						||
| 
								 | 
							
								    msg.deviceState.carBatteryCapacityUwh = max(0, power_monitor.get_car_battery_capacity())
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    current_power_draw = HARDWARE.get_current_power_draw()
							 | 
						||
| 
								 | 
							
								    statlog.sample("power_draw", current_power_draw)
							 | 
						||
| 
								 | 
							
								    msg.deviceState.powerDrawW = current_power_draw
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    som_power_draw = HARDWARE.get_som_power_draw()
							 | 
						||
| 
								 | 
							
								    statlog.sample("som_power_draw", som_power_draw)
							 | 
						||
| 
								 | 
							
								    msg.deviceState.somPowerDrawW = som_power_draw
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								    # Check if we need to shut down
							 | 
						||
| 
								 
											3 years ago
										 
									 | 
							
								    if power_monitor.should_shutdown(onroad_conditions["ignition"], in_car, off_ts, started_seen):
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								      cloudlog.warning(f"shutting device down, offroad since {off_ts}")
							 | 
						||
| 
								 | 
							
								      params.put_bool("DoShutdown", True)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    msg.deviceState.started = started_ts is not None
							 | 
						||
| 
								 | 
							
								    msg.deviceState.startedMonoTime = int(1e9*(started_ts or 0))
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    last_ping = params.get("LastAthenaPingTime")
							 | 
						||
| 
								 | 
							
								    if last_ping is not None:
							 | 
						||
| 
								 | 
							
								      msg.deviceState.lastAthenaPingTime = int(last_ping)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    msg.deviceState.thermalStatus = thermal_status
							 | 
						||
| 
								 | 
							
								    pm.send("deviceState", msg)
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    # Log to statsd
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    statlog.gauge("free_space_percent", msg.deviceState.freeSpacePercent)
							 | 
						||
| 
								 | 
							
								    statlog.gauge("gpu_usage_percent", msg.deviceState.gpuUsagePercent)
							 | 
						||
| 
								 | 
							
								    statlog.gauge("memory_usage_percent", msg.deviceState.memoryUsagePercent)
							 | 
						||
| 
								 | 
							
								    for i, usage in enumerate(msg.deviceState.cpuUsagePercent):
							 | 
						||
| 
								 | 
							
								      statlog.gauge(f"cpu{i}_usage_percent", usage)
							 | 
						||
| 
								 | 
							
								    for i, temp in enumerate(msg.deviceState.cpuTempC):
							 | 
						||
| 
								 | 
							
								      statlog.gauge(f"cpu{i}_temperature", temp)
							 | 
						||
| 
								 | 
							
								    for i, temp in enumerate(msg.deviceState.gpuTempC):
							 | 
						||
| 
								 | 
							
								      statlog.gauge(f"gpu{i}_temperature", temp)
							 | 
						||
| 
								 | 
							
								    statlog.gauge("memory_temperature", msg.deviceState.memoryTempC)
							 | 
						||
| 
								 | 
							
								    for i, temp in enumerate(msg.deviceState.pmicTempC):
							 | 
						||
| 
								 | 
							
								      statlog.gauge(f"pmic{i}_temperature", temp)
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    for i, temp in enumerate(last_hw_state.nvme_temps):
							 | 
						||
| 
								 | 
							
								      statlog.gauge(f"nvme_temperature{i}", temp)
							 | 
						||
| 
								 | 
							
								    for i, temp in enumerate(last_hw_state.modem_temps):
							 | 
						||
| 
								 | 
							
								      statlog.gauge(f"modem_temperature{i}", temp)
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								    statlog.gauge("fan_speed_percent_desired", msg.deviceState.fanSpeedPercentDesired)
							 | 
						||
| 
								 | 
							
								    statlog.gauge("screen_brightness_percent", msg.deviceState.screenBrightnessPercent)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											5 years ago
										 
									 | 
							
								    # report to server once every 10 minutes
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    rising_edge_started = should_start and not should_start_prev
							 | 
						||
| 
								 | 
							
								    if rising_edge_started or (count % int(600. / DT_TRML)) == 0:
							 | 
						||
| 
								 | 
							
								      dat = {
							 | 
						||
| 
								 | 
							
								        'count': count,
							 | 
						||
| 
								 | 
							
								        'pandaStates': [strip_deprecated_keys(p.to_dict()) for p in pandaStates],
							 | 
						||
| 
								 | 
							
								        'peripheralState': strip_deprecated_keys(peripheralState.to_dict()),
							 | 
						||
| 
								 | 
							
								        'location': (strip_deprecated_keys(sm["gpsLocationExternal"].to_dict()) if sm.alive["gpsLocationExternal"] else None),
							 | 
						||
| 
								 | 
							
								        'deviceState': strip_deprecated_keys(msg.to_dict())
							 | 
						||
| 
								 | 
							
								      }
							 | 
						||
| 
								 | 
							
								      cloudlog.event("STATUS_PACKET", **dat)
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								      # save last one before going onroad
							 | 
						||
| 
								 | 
							
								      if rising_edge_started:
							 | 
						||
| 
								 | 
							
								        try:
							 | 
						||
| 
								 | 
							
								          params.put("LastOffroadStatusPacket", json.dumps(dat))
							 | 
						||
| 
								 | 
							
								        except Exception:
							 | 
						||
| 
								 | 
							
								          cloudlog.exception("failed to save offroad status")
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    params.put_bool_nonblocking("NetworkMetered", msg.deviceState.networkMetered)
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								    count += 1
							 | 
						||
| 
								 
											2 years ago
										 
									 | 
							
								    should_start_prev = should_start
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											4 years ago
										 
									 | 
							
								def main():
							 | 
						||
| 
								 | 
							
								  hw_queue = queue.Queue(maxsize=1)
							 | 
						||
| 
								 | 
							
								  end_event = threading.Event()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  threads = [
							 | 
						||
| 
								 | 
							
								    threading.Thread(target=hw_state_thread, args=(end_event, hw_queue)),
							 | 
						||
| 
								 | 
							
								    threading.Thread(target=thermald_thread, args=(end_event, hw_queue)),
							 | 
						||
| 
								 | 
							
								  ]
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  for t in threads:
							 | 
						||
| 
								 | 
							
								    t.start()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  try:
							 | 
						||
| 
								 | 
							
								    while True:
							 | 
						||
| 
								 | 
							
								      time.sleep(1)
							 | 
						||
| 
								 | 
							
								      if not all(t.is_alive() for t in threads):
							 | 
						||
| 
								 | 
							
								        break
							 | 
						||
| 
								 | 
							
								  finally:
							 | 
						||
| 
								 | 
							
								    end_event.set()
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								  for t in threads:
							 | 
						||
| 
								 | 
							
								    t.join()
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								
							 | 
						||
| 
								 
											6 years ago
										 
									 | 
							
								if __name__ == "__main__":
							 | 
						||
| 
								 | 
							
								  main()
							 |