  def main() -> NoReturn:
    unpack_gps_meas, size_gps_meas = dict_unpacker(gps_measurement_report, True)
    unpack_gps_meas_sv, size_gps_meas_sv = dict_unpacker(gps_measurement_report_sv, True)
  
    unpack_glonass_meas, size_glonass_meas = dict_unpacker(glonass_measurement_report, True)
    unpack_glonass_meas_sv, size_glonass_meas_sv = dict_unpacker(glonass_measurement_report_sv, True)
  
    unpack_oemdre_meas, size_oemdre_meas = dict_unpacker(oemdre_measurement_report, True)
    unpack_oemdre_meas_sv, size_oemdre_meas_sv = dict_unpacker(oemdre_measurement_report_sv, True)
  
    log_types = [
      LOG_GNSS_GPS_MEASUREMENT_REPORT,
      LOG_GNSS_GLONASS_MEASUREMENT_REPORT,
      LOG_GNSS_OEMDRE_MEASUREMENT_REPORT,
    ]
    pub_types = ['qcomGnss']
    unpack_position, _ = dict_unpacker(position_report)
    log_types.append(LOG_GNSS_POSITION_REPORT)
    pub_types.append("gpsLocation")
  
    # connect to modem
    diag = ModemDiag()
  
    # NV enable OEMDRE
    # TODO: it has to reboot for this to take effect
    DIAG_NV_READ_F = 38
    DIAG_NV_WRITE_F = 39
    NV_GNSS_OEM_FEATURE_MASK = 7165
  
 
</s> remove def main() -> NoReturn:
  unpack_gps_meas, size_gps_meas = dict_unpacker(gps_measurement_report, True)
  unpack_gps_meas_sv, size_gps_meas_sv = dict_unpacker(gps_measurement_report_sv, True)
 </s> add  </s> remove   opcode, payload = send_recv(diag, DIAG_NV_WRITE_F, pack('<HI', NV_GNSS_OEM_FEATURE_MASK, 1))
  opcode, payload = send_recv(diag, DIAG_NV_READ_F, pack('<H', NV_GNSS_OEM_FEATURE_MASK))

  def try_setup_logs(diag, log_types):
    for _ in range(5):
      try:
        setup_logs(diag, log_types)
        break
      except Exception:
        pass

  def disable_logs(sig, frame):
    os.system("mmcli -m 0 --location-disable-gps-raw --location-disable-gps-nmea")
    cloudlog.warning("rawgpsd: shutting down")
    try_setup_logs(diag, [])
    cloudlog.warning("rawgpsd: logs disabled")
    sys.exit(0)
  signal.signal(signal.SIGINT, disable_logs)
  try_setup_logs(diag, log_types)
  cloudlog.warning("rawgpsd: setup logs done")
 </s> add   setup_logs(diag, LOG_TYPES) </s> remove   os.system("mmcli -m 0 --command='AT+QGPSCFG=\"dpoenable\",0'")
  os.system("mmcli -m 0 --location-enable-gps-raw --location-enable-gps-nmea")
 </s> add   mmcli("--command='AT+QGPSCFG=\"dpoenable\",0'")
  # don't automatically turn on GNSS on powerup
  mmcli("--command='AT+QGPSCFG=\"autogps\",0'")
  mmcli("--location-enable-gps-raw --location-enable-gps-nmea")
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
  def main() -> NoReturn:
    unpack_gps_meas, size_gps_meas = dict_unpacker(gps_measurement_report, True)
    unpack_gps_meas_sv, size_gps_meas_sv = dict_unpacker(gps_measurement_report_sv, True)
  
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
def try_setup_logs(diag, log_types):
  for _ in range(5):
    try:
      setup_logs(diag, log_types)
      break
    except Exception:
      cloudlog.exception("setup logs failed, trying again")
  else:
    raise Exception(f"setup logs failed, {log_types=}")

def mmcli(cmd: str) -> None:
  for _ in range(5):
    try:
      subprocess.check_call(f"mmcli -m 0 {cmd}", shell=True)
      break
    except subprocess.CalledProcessError:
      cloudlog.exception("rawgps.mmcli_command_failed")
  else:
    raise Exception(f"failed to execute mmcli command {cmd=}")

def setup_quectel(diag: ModemDiag):
  # enable OEMDRE in the NV
    # TODO: it has to reboot for this to take effect
    DIAG_NV_READ_F = 38
    DIAG_NV_WRITE_F = 39
    NV_GNSS_OEM_FEATURE_MASK = 7165
  
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 rawgpsd: more robust + simple test (#25977)

* rawgps cleanup

* wait for modem manager

* cleanup

Co-authored-by: Comma Device <device@comma.ai> 
selfdrive/sensord/rawgps/rawgpsd.py