def enrich_fixed(self): """ fixed arguments should be enriched here into the datastream. as to speak: serialized. by default, it will try to serialize fixed_arguments from fixed_values """ self.validate() ds = [] if self.fixed_arguments and self.fixed_values: # we have fixed arguments here for i in range(len(self.fixed_arguments)): val = self.fixed_values.get(self.fixed_arguments[i], None) if val: if is_stringlike(val): val = conv.toBytes(val) elif isinstance(val, list): pass else: val = [ val, ] # now just save it into ds ds += val return ds
def ecr_log(data, incoming=False): try: if incoming: incoming = '<' else: incoming = '>' if is_stringlike(data): data = conv.bs2hl(data) # logit to the logfile try: _logfile.write('%s %s\n' % (incoming, conv.toHexString(data))) except: pass try: data = repr(parse_represented_data(data)) _logfile.write('= %s\n' % data) except Exception as e: print("DEBUG: Cannot be represented: %s" % data) print(e) _logfile.write('? did not understand ?\n') data = conv.toHexString(data) print("%s %s" % (incoming, data)) except: import traceback traceback.print_exc() print("| error in log")
def __init__(self, apdu=None): if is_stringlike(apdu): # try to get the list of bytes. apdu = conv.toBytes(apdu.replace(' ', '')) elif isinstance(apdu, APDUPacket): apdu = apdu.to_list() self.apdu = apdu
def __init__(self, data=None): if isinstance(data, int): data = str(data) if is_stringlike(data): # this bcd got instantiated with a value. lets parse it. self._data = BCD.encode_bcd(data) data = None else: super(BCD, self).__init__(data)
def dump(self): ret = [] # first encode our bitmap id. if self._id: ret = [self._id] if is_stringlike(self._data): ret += [ord(c) for c in self._data[:self.length]] else: ret += self._data[:self.length] return ret
def std_serial_log(instance, data, incoming=False): try: if is_stringlike(incoming): data = conv.bs2hl(data) if incoming: print("< %s" % conv.toHexString(data)) else: print("> %s" % conv.toHexString(data)) except: print("| error in log")
def std_serial_log(instance, data, incoming=False): try: if is_stringlike(incoming): data = bs2hl(data) if incoming: print('< %s' % toHexString(data)) else: print('> %s' % toHexString(data)) except Exception: print('| error in log')
def detect(self, datastream): # detects which class to use. if is_stringlike(datastream): # lets convert our string into a bytelist. datastream = conv.toBytes(datastream[:2]) # read the first two bytes of the stream. cc, ci = datastream[:2] #print '<| %s %s' % (hex(cc), hex(ci)) # now look up if we got this packet class: return self.packets.get('%s_%s' % (hex(cc), hex(ci)), self.packets.get('%s' % (hex(cc)), None))
def decode_bcd(cls, something): """ BCDs save two numbers per byte @param something: might be a string or list of bytes @return: a list of numbers. """ if is_stringlike(something): something = conv.bs2hl(something) ret = [] for x in something: ret += list(cls.bcd_split(x)) return ret
def encode_bcd(cls, something, strict=False): """ @param something: a list of numbers, all < 10 @return: a list of bytes. Note: this function fills up numbers missing with 0, except you tell strict to be True. """ if is_stringlike(something): # you gave something like "123456" something = [int(x) for x in something] # check the length if even if len(something) % 2: something = [0] + something ret = [] for i in range(len(something) // 2): ret += [cls.bcd_unite((something[i * 2], something[i * 2 + 1]))] return ret
def parse(cls, blob=""): if is_stringlike(blob): # lets convert our string into a bytelist. blob = conv.toBytes(blob) if isinstance(blob, list): # allright. # first we detect our packetclass Kls = Packets.detect(blob[:2]) if Kls: instance = Kls() # fix for multipackets: if instance.cmd_instr == None: instance.cmd_instr = blob[1] instance.data = blob[2:] if not instance.validate(): debug('Validation Error') return instance else: debug('Unknown Packet')
def parse(cls, blob=''): if is_stringlike(blob): # lets convert our string into a bytelist. blob = toBytes(blob) if type(blob) is list: # allright. # first we detect our packetclass PacketClass = Packets.detect(blob[:2]) if PacketClass: instance = PacketClass() # fix for multipackets: if instance.cmd_instr is None: instance.cmd_instr = blob[1] instance.data = blob[2:] if not instance.validate(): debug('Validation Error') return instance else: debug('Unknown Packet')
def parse_represented_data(data): # represented data if is_stringlike(data): # we assume a bytelist like 10 02 03.... data = conv.toBytes(data) # first of all, serial data starts with 10 02, so everything # starting with 10 will be assumed as "serial packet" and first "demantled" if data[0] == DLE: try: crc, data = dismantle_serial_packet(data) except common.TransportLayerException: pass elif data[0] == ACK: if len(data) == 1: return 'ACK' elif data[0] == NAK: if len(data) == 1: return 'NAK' # then we create the packet and return that. p = Packet.parse(data) return p
def dump(self): # dump the bytes. """ dumps the bytes of the LVAR as one list. the minimum length of the length header can be set with self.LL """ ret = [] if self._id: ret = [self._id] lines = [ self._data, ] for line in lines: length = LVAR.length(len(line)) while len(length) < self.LL: length = [0xF0] + length if is_stringlike(line): ret += length + conv.bs2hl(line) elif isinstance(line, list): ret += length + line else: raise TypeError("Line has unsupported type in LVAR: %s" % type(line)) return ret
def __init__(self, data=None): if is_stringlike(data): self._data = conv.bs2hl(data) self._rangecheck() else: super(LVAR, self).__init__(self._data)