def handle_modprobe_ehcihcd(insert=True): if insert: command = 'modprobe' else: command = 'rmmod' # launch modprobe timeout of 30 seconds v = OSTools.polling_popen([command, 'ehci_hcd'], timeout=30.0) # polling_popen returns False upon failure if v: (ret, err) = v else: ret, err = '', '' # rety 5 times while there is an error retry_count = 5 while err and (retry_count > 0): v = OSTools.polling_popen([command, 'ehci_hcd'], timeout=30.0) if v: ret, err = v if ret: logger.info("Process %s returned without error" % command) break retry_count -= 1 if err: logger.error("Unable to excecute %s successfuly (%d left)." % (command, retry_count)) return (ret, err)
def goto_known_state(): # kill pppd pid = OSTools.pppd_pid() if pid != 0: OSTools.pppd_terminate(pid) # call rmmod logger.warn("Cleaning up, removed ehci_hcd.") OSTools.polling_popen(['rmmod', '-f', 'ehci_hcd'], timeout=30.0) # remove power from usb hub set_gpio_usbhub_power(on=False)
def _send_i2cget_read(self, port, chipadr, register): i2c_chip = '0x%02x' % int(chipadr) i2c_register = '0x%02x' % int(register) args = ['i2cget', '-y', str(port), i2c_chip, i2c_register] # process open return OSTools.polling_popen(args, 5.0)
def main(): UPTIME = 0 START = datetime.utcnow() while True: UPTIME = datetime.utcnow() - START uptime = UPTIME.seconds # check if network is up pppdstatus = OSTools.pppd_status() if pppdstatus: heartbeat_int = 30.0 else: heartbeat_int = 60.0 if pppdstatus: try: rwip = OSTools.net_get_ip_address('ppp0') except socket.error: logger.exception("Error getting local IP") rwip = '0.0.0.0' try: send_heartbeat(Constants.URLJsonAPI, uptime, rwip) except: logger.exception("Unknown Error") time.sleep(heartbeat_int)
def turn_on_usbhub(): # two stages, first enable GPIO210 then modprobe usb_ehci logger.info("Applying power to USB hub controller.") # set gpio. wait to get a lock if need be. set_gpio_usbhub_power(on=True) handle_modprobe_ehcihcd(insert=True) # check /dev/ttyUSB0 time.sleep(5) ## Check to see if we have a mf112 connected try: (ret, err) = OSTools.polling_popen(['lsusb'], 2.0) lines = ret.strip().split('\n') for line in lines: sects = line.split(' ') if len(sects) > 6: if sects[5] == "19d2:0103": os.popen('usb_modeswitch -v 0x19d2 -p 0x0103 -V 19d2 -P 0x0031 -M 5553424312345679000000000000061b000000020000000000000000000000') logger.info("MF112 detected, running modeswitch") time.sleep(10) except: logger.exception("Unable to check usb devices") #### REMOVE! #time.sleep(5) #os.popen('usb_modeswitch -v 0x19d2 -p 0x0103 -V 19d2 -P 0x0031 -M 5553424312345679000000000000061b000000020000000000000000000000') #time.sleep(5) retry_count = 5 while retry_count > 0: if exists('/dev/ttyUSB0'): logger.info("Modem detected on ttyUSB0!") break else: logger.error("Modem still not detected, %d retry attempts left." % retry_count) time.sleep(4) retry_count -= 1 return exists('/dev/ttyUSB0')
def _send_i2cset_write(self, port, chipadr, register, byte, mask=0): """Converts parameters to strings, and invokes i2cset""" i2c_chip = "0x%02x" % int(chipadr) i2c_register = "0x%02x" % int(register) i2c_byte = "0x%02x" % int(byte) # the default arguments passed to the function # -y : no confirmation prompt # PORT : the /dev/i2c-? port number to use # CHIP : chip address # REG : the register to write to # BYTE : the byte of data to write args = ['i2cset', '-y', str(port), i2c_chip, i2c_register, i2c_byte] # A mask is used to select individual leds # the register is read from the chip, and masked by MASK if mask: i2c_mask = "0x%02x" % int(mask) if mask: # arm linux i2cset requires the mask as a positional argument args.insert(2, '-m') args.insert(3, i2c_mask) return OSTools.polling_popen(args, 5.0)
def ready_internet(self): self.logger.info("Attempting to connect to the internet.") ## open a pppd instance OSTools.pppd_launch() ## wait for pppd to settle time.sleep(5) retrycount = 5 while retrycount > 0: ## to see if ppp is up if OSTools.pppd_status(): ## ppp is up, try ping x = self._try_network() if x is not None: self.disp.control_led('net', True) self.disp.control_led('neterr', False) self.logger.info("Ping success.") return True else: self.disp.control_led('neterr', True) self.disp.control_led('net', False) self.logger.info("Sleeping for 10s.") time.sleep(10) else: # ppp is not up ## check if process is running ppp_pid = OSTools.pppd_pid() if ppp_pid == 0: ## ppp has quit raise InternetConnectionError('pppd unexpectedly quit!') else: ## wait 10 seconds, and retry time.sleep(10) retrycount -= 1 self.logger.info("Rechecking network, remaining attempts %d." % retrycount) OSTools.pppd_terminate(OSTools.pppd_pid()) return False
def stop_internet(self): self.logger.info("Dropping net connection.") self.disp.control_led('net', False) return OSTools.pppd_terminate(OSTools.pppd_pid())