def _command(self, cmd, timeout=10): if len(cmd) + len(COMMISSIONER_PROMPT) >= TTY_COLS: raise commissioner.Error('Command too long: "{}"'.format(cmd)) self._write_line(cmd) self._expect(cmd, timeout) response = [] while timeout > 0: line = self._read_line() logging.info('Read line: [{}]'.format(line)) if line: if re.match(COMMISSIONER_PROMPT, line): break response.append(line) else: self._sleep(0.1) timeout -= 0.1 if timeout <= 0: raise Exception('Failed to find end of response'.format( self._port)) logging.info('Send command[{}] done!'.format(cmd)) return response
def MGMT_ED_SCAN(self, channelMask, count, period, scanDuration, dstAddr, timeout): self._execute_and_check('energy scan {} {} {} {} {}'.format( channelMask, count, period, scanDuration, dstAddr, )) self._sleep(timeout) result = self._execute_and_check('energy report {}'.format(dstAddr)) if result[0] == 'null': raise commissioner.Error( 'No energy report found for {}'.format(dstAddr)) try: result = json.loads(' '.join(result[:-1])) return OTCommissioner.EnergyReport( channelMask=self._channel_mask_from_json_obj( result['ChannelMask']), energyList=self._hex_to_bytes(result['EnergyList']), ) except Exception as e: raise_(commissioner.Error, repr(e), sys.exc_info()[2])
def getCommissioningLogs(self): processed_logs = [] thci_logs = self._command("grep \"\\[ thci \\]\" {}".format( self.log_file)) for log in thci_logs: encrypted_packet = PlatformDiagnosticPacket() if "JOIN_FIN.req:" in log: hex_value = encrypted_packet.split("JOIN_FIN.req:")[-1].strip() payload = list(bytearray.fromhex(hex_value)) encrypted_packet.Direction = PlatformDiagnosticPacket_Direction.IN encrypted_packet.Type = PlatformDiagnosticPacket_Type.JOIN_FIN_req encrypted_packet.TLVsLength = len(payload) encrypted_packet.TLVs = PlatformPackets.read( encrypted_packet.Type, payload) elif "JOIN_FIN.rsp:" in log: hex_value = encrypted_packet.split("JOIN_FIN.rsp:")[-1].strip() payload = list(bytearray.fromhex(hex_value)) encrypted_packet.Direction = PlatformDiagnosticPacket_Direction.OUT encrypted_packet.Type = PlatformDiagnosticPacket_Type.JOIN_FIN_rsp encrypted_packet.TLVsLength = len(payload) encrypted_packet.TLVs = PlatformPackets.read( encrypted_packet.Type, payload) else: raise commissioner.Error( "bad commissioner thci log: {}".format(log)) processed_logs.append(encrypted_packet) return processed_logs
def isActive(self): response = self._execute_and_check('active') if response[0].startswith('true'): return True elif response[0].startswith('false'): return False else: raise commissioner.Error('Unrecognized result "{}"'.format( response[0]))
def _expect(self, expected, timeout=10): logging.info('Expecting [{}]'.format(expected)) while timeout > 0: line = self._read_line() logging.info('Got line [{}]'.format(line)) if line == expected: logging.info('Expected [{}]'.format(expected)) return self._sleep(0.1) raise commissioner.Error( 'Failed to find expected string [{}]'.format(expected))
def __init__(self, config, handler, simulator=None): super(OTCommissioner, self).__init__(config) self.enable_dtls_debug_logging = True self.logging_level = 'debug' self.keep_alive_interval = 40 self.max_connection_num = 100 self.log_file = '/tmp/commissioner.log' self._simulator = simulator self._handler = handler self._lines = [] config_path = '/tmp/commissioner.{}.json'.format(uuid.uuid4()) self._write_config(config_path=config_path, config=config) response = self._command('{} init "{}"'.format(COMMISSIONER_CTL, config_path)) if self._command('echo $?')[0] != '0': raise commissioner.Error('Failed to init, error:\n{}'.format( '\n'.join(response)))
def _check_response(response): if response[-1] != '[done]': raise commissioner.Error('Error message:\n{}'.format( '\n'.join(response))) return response
def makeHarnessCommissioner(config, serial_handler): if not isinstance(serial_handler, serial.Serial): raise commissioner.Error("expect a serial handler") return OTCommissioner(config, serial_handler)