def mb_commands_handler(v2x, name, stop_event, outQ, cmd): bypass_mode = vi_bypass[name] diag_start = False if cmd == 'device_id': value = socket.gethostname() elif cmd == 'version': value = xcmodem_ver.get_version() elif cmd == 'diagnostics_enable': value = None if not bypass_mode: diag_start = True bypass_mode = True elif cmd == 'diagnostics_disable': value = None if stop_event is not None: stop_event.set() # terminate mb_diagnostic_screen thread stop_event = None bypass_mode = False if value is not None: reply = '{"modem_command_response":"%s","modem_message":"%s","status":true}\0' % (cmd, value) else: reply = '{"modem_command_response":"%s","status":true}\0' % cmd outQ.put(reply) vi_bypass[name] = bypass_mode if diag_start: thread, stop_event = loop_timer(None, mb_diagnostic_screen, v2x, name, outQ)
def md_request_commands_handler(v2x, name, outQ, req): if req == 'device_id': value = socket.gethostname() elif req == 'version': value = xcmodem_ver.get_version() elif req == 'heartbeat': value = time.time() reply = '{"modem_command_response":"%s","modem_message":"%s","status":true}\0' % (req, value) outQ.put(reply)
def vi_auto_upgrade(self): ver_url = conf_options['web_scp_sw_latest_version_url'] if ver_url is None or ver_url == 'None': return LOG.debug("OTA auto upgrade validation ...") # Use GSM if applicable if conf_options['gsm_enable']: if not self.gsm_instance(): LOG.error("OTA abort due to gsm_app instance fail") return if not self.gsm.start(): LOG.error("OTA abort due to gsm_app start fail") # No need to move on without network connection return # Obtain latest version info cmd = "rm -fr /tmp/upgrade.ver; \ scp -o StrictHostKeyChecking=no -i %s %s@%s /tmp/upgrade.ver" % \ (conf_options['web_scp_pem'], \ conf_options['web_scp_userid'], \ ver_url) # LOG.debug("issuing '%s'" % cmd) if subprocess.call(cmd, shell=True): LOG.error("fail to scp upgrade.ver from %s@%s" % (conf_options['web_scp_userid'], \ ver_url)) LOG.error("OTA abort due to scp fail") return ver, fname = subprocess.check_output("cat /tmp/upgrade.ver", shell=True).split() cver = xcmodem_ver.get_version() LOG.debug("OTA latest=%s current=%s" % (ver, cver)) if ver <= cver: return LOG.info("OTA auto upgrade for %s ..." % ver) # Obtain upgrading package if re.search(r'/', conf_options['web_scp_sw_latest_version_url'], re.M|re.I): delimiter = '/' else: delimiter = ':' pkg = "%s%s%s" % (conf_options['web_scp_sw_latest_version_url'].rsplit(delimiter, 1)[0], \ delimiter, fname) cmd = "scp -o StrictHostKeyChecking=no -i %s %s@%s /tmp" % \ (conf_options['web_scp_pem'], \ conf_options['web_scp_userid'], \ pkg) # LOG.debug("issuing '%s'" % cmd) if subprocess.call(cmd, shell=True): LOG.error("fail to scp %s from %s@%s" % (fname, \ conf_options['web_scp_userid'], \ pkg)) LOG.error("OTA abort due to scp fail") return # Use GSM if applicable if conf_options['gsm_enable']: # Tear off gsm connection self.gsm.stop() # Perform SW upgrade xcmodem_led.all_leds(3) # all leds slow blink LOG.info("OTA auto upgrading ...") LOG.info("System will be reset after software upgrade ...") # directory prep cmd = "rm -fr ../backup/previous; mv -f ../backup/current ../backup/previous; \ mkdir -p ../backup/current; \ cp -f /tmp/%s /tmp/upgrade.ver ../backup/current" % fname # LOG.debug("issuing: " + cmd) if subprocess.call(cmd, shell=True): LOG.error("OTA software upgrade fail to directory prep") cmd = "rm -fr ../backup/current; mv -f ../backup/previous ../backup/current" # LOG.debug("issuing: " + cmd) if subprocess.call(cmd, shell=True): LOG.error("Fail to restore directory !!!") # upgrade now cmd = "cd /tmp; tar xvf %s; ./xcmodem-upgrade.sh" % fname if subprocess.call(cmd, shell=True): LOG.info("OTA software upgrade fail") # Restore previous version ver, fname = subprocess.check_output("cat ../backup/previous/upgrade.ver", shell=True).split() LOG.info("Restoring previous software %s ..." % ver) cmd = "rm -fr ../backup/current; mv -f ../backup/previous ../backup/current; \ cp -f ../backup/current/%s /tmp; \ cd /tmp; tar xvf %s; ./xcmodem-upgrade.sh" % (fname, fname) # LOG.debug("issuing: " + cmd) if subprocess.call(cmd, shell=True): LOG.error("Fail to restore %s !!!" % ver)
def main(sdebug = 0, debug = 0): first = 1 attempt = 1 threads = [] v2x = boardid_inquiry() == 2 if v2x: sys.path.append('../v2x') # V2X specific import xcmodem_md import xcmodem_mk5 LOG.info("OpenXCModem Embedded Software - Rev %s" % xcmodem_ver.get_version()) pairing_registration() vi_cleanup() vi_dev = xcModemVi(port_dict['vi_app']['port'], vi_in_queue, vi_out_queue, sdebug, debug) while True: if (vi_dev.vi_main() or modem_state['vi_app'] == vi_state.DISABLE): if first: first = 0 LOG.info("App Tasks ...") # Android/Mobil App thread if port_dict['mb_app']['enable']: thread = appThread('mb_app', port_dict['mb_app']['port'], mb_in_queue, mb_out_queue, mb_passthru_queue, vi_out_queue) thread.start() threads.append(thread) if port_dict['md_app']['enable']: if v2x: # V2X-MD Master/Client thread thread = xcmodem_md.xcModemMDthread(conf_options['openxc_md_mac'], port_dict['md_app']['port'], md_in_queue, md_out_queue, debug) else: # V2X-MD Slave/Server App thread thread = appThread('md_app', port_dict['md_app']['port'], md_in_queue, md_out_queue, md_passthru_queue, vi_out_queue) thread.start() threads.append(thread) if v2x: # V2X-V2X MK5 thread thread = xcmodem_mk5.mk5Thread('wf_app', wf_in_queue, wf_out_queue) thread.start() threads.append(thread) # GPS thread if conf_options['gps_enable']: sys.path.append('../modem') # GPS is only supported in modem import xcmodem_gps thread = xcmodem_gps.gpsThread(sdebug, debug) thread.start() threads.append(thread) while not exit_flag['vi_app']: if not vi_in_queue.empty(): data = vi_in_queue.get() if modem_state['md_app'] == md_state.OPERATION: if passthru_flag['md_app']: md_passthru_queue.put(data) if modem_state['mb_app'] == app_state.OPERATION: if passthru_flag['mb_app']: mb_passthru_queue.put(data) # and dump to trace file vi_dev.trace_raw_lock.acquire() if vi_dev.fp and vi_dev.trace_enable: new = vi_dev.vi_timestamp(data) vi_dev.fp.write(new) vi_dev.trace_raw_lock.release() else: msleep(1) modem_state['vi_app'] = vi_state.LOST vi_dev.lost_cnt += 1 LOG.info("vi_app state %s %d time" % (modem_state['vi_app'], vi_dev.lost_cnt)) vi_dev.vi_exit() # flush passthru queues while not md_passthru_queue.empty(): md_passthru_queue.get() while not mb_passthru_queue.empty(): mb_passthru_queue.get() if exit_flag['all_app']: LOG.debug("Ending all_app") break; time.sleep(float(conf_options['openxc_vi_discovery_interval'])) attempt += 1 if (attempt > MAX_BRING_UP_ATTEMPT): LOG.debug("vi_app max out %d attempts" % MAX_BRING_UP_ATTEMPT) break; # terminate all threads for k in exit_flag.keys(): exit_flag[k] = 1 # Wait for all threads to complete for t in threads: t.join() LOG.info("Ending xcmodem")