def __init__(self, app, start_cmd, enable_param, auto_run_param, manual_ctrl_param, app_type, check_crash, permissions, opts): self.app = app # main activity self.start_cmd = start_cmd # read enable param self.enable_param = enable_param self.enable_struct = get_struct_name(enable_param) if enable_param is not None else None # read auto run param self.auto_run_struct = get_struct_name(auto_run_param) if auto_run_param is not None else None # read manual run param self.manual_ctrl_param = manual_ctrl_param if manual_ctrl_param is not None else None self.manual_ctrl_struct = get_struct_name(manual_ctrl_param) if manual_ctrl_param is not None else None # if it's a service app, we do not kill if device is too hot self.app_type = app_type # app permissions self.permissions = permissions # app options self.opts = opts self.own_apk = "/sdcard/apks/" + self.app + ".apk" self.has_own_apk = os.path.exists(self.own_apk) self.is_installed = False self.is_enabled = False self.last_is_enabled = False self.is_auto_runnable = False self.is_running = False self.manual_ctrl_status = self.MANUAL_IDLE self.manually_ctrled = False self.init = False self.check_crash = check_crash
def update_ip(msg): val = 'N/A' try: result = subprocess.check_output(["ifconfig", "wlan0"], encoding='utf8') val = re.findall(r"inet addr:((\d+\.){3}\d+)", result)[0][0] except: pass setattr(msg.dragonConf, get_struct_name('dp_ip_addr'), val) return msg
def check_dependencies(msg, conf): passed = True # if has dependency and the depend param val is not in depend_vals, we dont update that conf val # this should reduce chance of reading unnecessary params dependencies = conf.get('depends') if dependencies is not None: for dependency in dependencies: if getattr(msg.dragonConf, get_struct_name(dependency['name'])) not in dependency['vals']: passed = False break return passed
def update_ip(msg): val = 'N/A' s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) try: # doesn't even have to be reachable s.connect(('10.255.255.255', 1)) IP = s.getsockname()[0] except: IP = 'N/A' finally: s.close() setattr(msg.dragonConf, get_struct_name('dp_ip_addr'), IP) return msg
def set_message(msg, conf): val = params.get(conf['name'], encoding='utf8') if val is not None: val = val.rstrip('\x00') else: val = conf.get('default') params.put(conf['name'], str(val)) struct_val = to_struct_val(conf['name'], val) orig_val = struct_val if struct_val is not None: if conf.get('min') is not None: struct_val = max(struct_val, conf.get('min')) if conf.get('max') is not None: struct_val = min(struct_val, conf.get('max')) if orig_val != struct_val: params.put(conf['name'], str(struct_val)) setattr(msg.dragonConf, get_struct_name(conf['name']), struct_val) return msg
def confd_thread(): sm = messaging.SubMaster(['thermal']) pm = messaging.PubMaster(['dragonConf']) last_dp_msg = None last_modified = None update_params = False frame = 0 locale = getprop("persist.sys.locale").rstrip('\n') last_autoshutdown = False last_sec = None autoshutdown_frame = 0 last_charging_ctrl = False last_started = False dashcam_next_frame = 0 dashcam_folder_exists = False dashcam_mkdir_retry = 0 # thermal started = False free_space = 0. online = False battery_percent = 0. overheat = False while True: start_sec = sec_since_boot() if frame % 6 == 0: sm.update() if sm.updated['thermal']: started = sm['thermal'].started free_space = sm['thermal'].freeSpace online = sm['thermal'].usbOnline battery_percent = sm['thermal'].batteryPercent overheat = sm['thermal'].thermalStatus >= 2 msg = messaging.new_message('dragonConf') if last_dp_msg is not None: msg.dragonConf = last_dp_msg ''' =================================================== force update when start status changed =================================================== ''' if last_started != started: update_params = True ''' =================================================== we only need to check last_modified every 3 seconds if val is modified, we then proceed to read params =================================================== ''' if not update_params and frame % 6 == 0: try: modified = params.get('dp_last_modified', encoding='utf8').rstrip('\x00') except AttributeError: modified = str(floor(time.time())) if last_modified != modified: update_params = True last_modified = modified ''' =================================================== push param vals to message =================================================== ''' if update_params: for conf in confs: conf_type = conf.get('conf_type') if 'param' in conf_type and 'struct' in conf_type: update_this_conf = True update_once = conf.get('update_once') if update_once is not None and update_once is True and frame > 0: update_this_conf = False if frame > 0 and update_this_conf: dependencies = conf.get('depends') # if has dependency and the depend param val is not in depend_vals, we dont update that conf val # this should reduce chance of reading all params if dependencies is not None: for dependency in dependencies: if getattr(msg.dragonConf, get_struct_name(dependency['name']) ) not in dependency['vals']: update_this_conf = False if update_this_conf: val = params.get(conf['name'], encoding='utf8') if val is not None: val = val.rstrip('\x00') struct_val = to_struct_val(conf['name'], val) orig_val = struct_val if conf.get('min') is not None: struct_val = max(struct_val, conf.get('min')) if conf.get('max') is not None: struct_val = min(struct_val, conf.get('max')) if orig_val != struct_val: params.put(conf['name'], str(struct_val)) setattr(msg.dragonConf, get_struct_name(conf['name']), struct_val) ''' =================================================== push ip addr every 10 secs to message =================================================== ''' if frame % 20 == 0: val = 'N/A' try: result = subprocess.check_output(["ifconfig", "wlan0"], encoding='utf8') val = re.findall(r"inet addr:((\d+\.){3}\d+)", result)[0][0] except: pass msg.dragonConf.dpIpAddr = val ''' =================================================== push is_updating status every 5 secs to message =================================================== ''' if frame % 10 == 0: val = params.get('dp_is_updating', encoding='utf8').rstrip('\x00') setattr(msg.dragonConf, get_struct_name('dp_is_updating'), to_struct_val('dp_is_updating', val)) ''' =================================================== push once =================================================== ''' if frame == 0: setattr(msg.dragonConf, get_struct_name('dp_locale'), locale) put_nonblocking('dp_is_updating', '0') ''' =================================================== we can have some logic here =================================================== ''' if msg.dragonConf.dpAssistedLcMinMph > msg.dragonConf.dpAutoLcMinMph: put_nonblocking('dp_auto_lc_min_mph', str(msg.dragonConf.dpAssistedLcMinMph)) msg.dragonConf.dpAutoLcMinMph = msg.dragonConf.dpAssistedLcMinMph if msg.dragonConf.dpAtl: msg.dragonConf.dpAllowGas = True msg.dragonConf.dpDynamicFollow = 0 msg.dragonConf.dpAccelProfile = 0 msg.dragonConf.dpGearCheck = False if msg.dragonConf.dpAppWaze: msg.dragonConf.dpDrivingUi = False if not msg.dragonConf.dpDriverMonitor: msg.dragonConf.dpUiFace = False msg.dragonConf.dpThermalStarted = started msg.dragonConf.dpThermalOverheat = overheat ''' =================================================== publish msg =================================================== ''' last_dp_msg = msg.dragonConf update_params = False pm.send('dragonConf', msg) ''' =================================================== hotspot on boot we do it after 30 secs just in case =================================================== ''' if frame == 60 and params.get("dp_hotspot_on_boot") == b'1': os.system("service call wifi 37 i32 0 i32 1 &") ''' =================================================== dashcam =================================================== ''' dashcam = msg.dragonConf.dpDashcam if frame % 2 == 0 and dashcam: while not dashcam_folder_exists and dashcam_mkdir_retry <= 5: try: if not os.path.exists(DASHCAM_VIDEOS_PATH): os.makedirs(DASHCAM_VIDEOS_PATH) else: dashcam_folder_exists = True except OSError: pass dashcam_mkdir_retry += 1 if dashcam_folder_exists: if started: if frame >= dashcam_next_frame - 2: now = datetime.datetime.now() file_name = now.strftime("%Y-%m-%d_%H-%M-%S") os.system( "screenrecord --bit-rate %s --time-limit %s %s%s.mp4 &" % (DASHCAM_BIT_RATES, DASHCAM_DURATION, DASHCAM_VIDEOS_PATH, file_name)) dashcam_next_frame = frame + DASHCAM_DURATION * 2 else: dashcam_next_frame = 0 if frame % 120 == 0 and ((free_space < DASHCAM_FREESPACE_LIMIT) or (get_used_spaces() > DASHCAM_KEPT)): try: files = [ f for f in sorted(os.listdir(DASHCAM_VIDEOS_PATH)) if os.path.isfile(DASHCAM_VIDEOS_PATH + f) ] os.system("rm -fr %s &" % (DASHCAM_VIDEOS_PATH + files[0])) except (IndexError, FileNotFoundError, OSError): pass ''' =================================================== auto shutdown =================================================== ''' autoshutdown = msg.dragonConf.dpAutoShutdown if frame % 20 == 0 and autoshutdown: sec = msg.dragonConf.dpAutoShutdownIn * 60 * 2 if last_autoshutdown != autoshutdown or last_sec != sec or started or online: autoshutdown_frame = frame + sec if not started and not online and sec > 0 and frame >= autoshutdown_frame: os.system('LD_LIBRARY_PATH="" svc power shutdown') last_sec = sec last_autoshutdown = autoshutdown ''' =================================================== battery ctrl every 30 secs PowerMonitor in thermald turns back on every mins so lets turn it off more frequent =================================================== ''' charging_ctrl = msg.dragonConf.dpChargingCtrl if last_charging_ctrl != charging_ctrl: set_battery_charging(True) if charging_ctrl and frame % 60 == 0: if battery_percent >= msg.dragonConf.dpDischargingAt and get_battery_charging( ): set_battery_charging(False) elif battery_percent <= msg.dragonConf.dpChargingAt and not get_battery_charging( ): set_battery_charging(True) last_charging_ctrl = charging_ctrl ''' =================================================== make it 2 hz =================================================== ''' last_started = started frame += 1 sleep = 0.5 - (sec_since_boot() - start_sec) if sleep > 0: time.sleep(sleep)
def confd_thread(): sm = messaging.SubMaster(['thermal']) pm = messaging.PubMaster(['dragonConf']) last_dp_msg = None frame = 0 update_params = False modified = None last_modified = None last_modified_check = None started = False free_space = 1 battery_percent = 0 overheat = False last_charging_ctrl = False last_started = False dashcam = Dashcam() last_dashcam_recorded = False while True: start_sec = sec_since_boot() msg = messaging.new_message('dragonConf') if last_dp_msg is not None: msg.dragonConf = last_dp_msg ''' =================================================== load thermald data every 3 seconds =================================================== ''' if frame % (HERTZ * 3) == 0: started, free_space, battery_percent, overheat = pull_thermald( frame, sm, started, free_space, battery_percent, overheat) setattr(msg.dragonConf, get_struct_name('dp_thermal_started'), started) setattr(msg.dragonConf, get_struct_name('dp_thermal_overheat'), overheat) ''' =================================================== hotspot on boot we do it after 30 secs just in case =================================================== ''' if frame == (HERTZ * 30) and param_get("dp_hotspot_on_boot", "bool", False): os.system("service call wifi 37 i32 0 i32 1 &") ''' =================================================== check dp_last_modified every second =================================================== ''' if not update_params: last_modified_check, modified = get_last_modified( LAST_MODIFIED_SYSTEMD, last_modified_check, modified) if last_modified != modified: update_params = True last_modified = modified ''' =================================================== conditionally set update_params to true =================================================== ''' # force updating param when `started` changed if last_started != started: update_params = True last_started = started if frame == 0: update_params = True ''' =================================================== conditionally update dp param base on stock param =================================================== ''' if update_params and params.get("LaneChangeEnabled") == b"1": params.put("dp_steering_on_signal", "0") ''' =================================================== push param vals to message =================================================== ''' if update_params: msg = update_conf_all(confs, msg, frame == 0) update_params = False ''' =================================================== push once =================================================== ''' if frame == 0: setattr(msg.dragonConf, get_struct_name('dp_locale'), get_locale()) put_nonblocking('dp_is_updating', '0') ''' =================================================== push ip addr every 10 secs =================================================== ''' if frame % (HERTZ * 10) == 0: msg = update_ip(msg) ''' =================================================== push is_updating status every 5 secs =================================================== ''' if frame % (HERTZ * 5) == 0: msg = update_updating(msg) ''' =================================================== update msg based on some custom logic =================================================== ''' msg = update_custom_logic(msg) ''' =================================================== battery ctrl every 30 secs PowerMonitor in thermald turns back on every mins so lets turn it off more frequent =================================================== ''' if frame % (HERTZ * 30) == 0: last_charging_ctrl = process_charging_ctrl(msg, last_charging_ctrl, battery_percent) ''' =================================================== dashcam =================================================== ''' if msg.dragonConf.dpDashcam and frame % HERTZ == 0: dashcam.run(started, free_space) last_dashcam_recorded = True if last_dashcam_recorded and not msg.dragonConf.dpDashcam: dashcam.stop() last_dashcam_recorded = False ''' =================================================== finalise =================================================== ''' last_dp_msg = msg.dragonConf pm.send('dragonConf', msg) frame += 1 sleep = DELAY - (sec_since_boot() - start_sec) if sleep > 0: time.sleep(sleep)
def update_updating(msg): setattr( msg.dragonConf, get_struct_name('dp_is_updating'), to_struct_val('dp_is_updating', param_get("dp_is_updating", "bool", False))) return msg
def confd_thread(): sm = messaging.SubMaster(['deviceState']) pm = messaging.PubMaster(['dragonConf']) last_dp_msg = None frame = 0 update_params = False modified = None last_modified = None last_modified_check = None started = False free_space = 1 last_started = False dashcamd = Dashcamd() is_eon = EON rk = Ratekeeper(HERTZ, print_delay_threshold=None) # Keeps rate at 2 hz uploader_thread = None while True: if uploader_thread is None: uploader_thread = threading.Thread(target=gpx_uploader_thread) uploader_thread.start() msg = messaging.new_message('dragonConf') if last_dp_msg is not None: msg.dragonConf = last_dp_msg ''' =================================================== load thermalState data every 3 seconds =================================================== ''' if frame % (HERTZ * 3) == 0: sm.update(0) if sm.updated['deviceState']: started = sm['deviceState'].started free_space = sm['deviceState'].freeSpacePercent ''' =================================================== hotspot on boot we do it after 30 secs just in case =================================================== ''' if is_eon and frame == (HERTZ * 30) and param_get("dp_hotspot_on_boot", "bool", False): os.system("service call wifi 37 i32 0 i32 1 &") ''' =================================================== check dp_last_modified every second =================================================== ''' if not update_params: last_modified_check, modified = get_last_modified(LAST_MODIFIED_SYSTEMD, last_modified_check, modified) if last_modified != modified: update_params = True last_modified = modified ''' =================================================== conditionally set update_params to true =================================================== ''' # force updating param when `started` changed if last_started != started: update_params = True if frame == 0: update_params = True ''' =================================================== push param vals to message =================================================== ''' if update_params: msg = update_conf_all(confs, msg, frame == 0) update_params = False ''' =================================================== push once =================================================== ''' if frame == 0: setattr(msg.dragonConf, get_struct_name('dp_locale'), params.get("dp_locale")) # mirror EndToEndToggle to dp_lane_less_model_ctrl first time, after all put_nonblocking('dp_lane_less_mode_ctrl', params.get('EndToEndToggle')) ''' =================================================== push ip addr every 10 secs =================================================== ''' if frame % (HERTZ * 10) == 0: msg = update_ip(msg) ''' =================================================== update msg based on some custom logic =================================================== ''' msg = update_custom_logic(msg) ''' =================================================== battery ctrl every 30 secs PowerMonitor in thermald turns back on every mins so lets turn it off more frequent =================================================== ''' # if frame % (HERTZ * 30) == 0: # last_charging_ctrl = process_charging_ctrl(msg, last_charging_ctrl, battery_percent) ''' =================================================== dashcam =================================================== ''' if msg.dragonConf.dpDashcamd and frame % HERTZ == 0: dashcamd.run(started, free_space) # ''' # =================================================== # appd # =================================================== # ''' # if msg.dragonConf.dpAppd: # appd.update(started) ''' =================================================== finalise =================================================== ''' last_dp_msg = msg.dragonConf last_started = started pm.send('dragonConf', msg) frame += 1 rk.keep_time()