def re_ip(self, re_text): m_data = common.text2list(re_text) data = common.get_apdu_list(m_data) gprs_pos = common.list2text(data).replace(' ', '').find('45000300') // 2 eth_pos = common.list2text(data).replace(' ', '').find('45100300') // 2 if gprs_pos >= 0: offset = gprs_pos + 4 if data[offset] == '01': self.res_b.setStyleSheet('color: green') self.res_b.setText('成功') offset += 7 ip_text = param.get_ip(data[offset:]) self.S_ip_box.setText(ip_text) port_text = str(int(data[offset + 5] + data[offset + 6], 16)) self.S_port_box.setText(port_text) if eth_pos >= 0: offset = eth_pos + 4 if data[offset] == '01': self.res_b.setStyleSheet('color: green') self.res_b.setText('成功') offset += 7 ip_text = param.get_ip(data[offset:]) self.S_ip_box_2.setText(ip_text) port_text = str(int(data[offset + 5] + data[offset + 6], 16)) self.S_port_box_2.setText(port_text) config.MASTER_WINDOW.receive_signal.disconnect(self.re_ip)
def get_full(self, is_show_level=True, is_show_type=True, is_output_html=True, has_linklayer=True): """get full translate""" if self.is_success: res_text = '<table style="table-layout:fixed; word-wrap:break-word; border-style:solid;">' if is_output_html else '' else: if is_output_html: res_text = '<p style="color: red">报文解析过程出现问题,请检查报文。若报文无问题请反馈665593,谢谢!</p><p> </p>' else: res_text = '报文解析过程出现问题,请检查报文。若报文无问题请反馈665593,谢谢!\n\n' temp_row = None for row in self.res_list: if not has_linklayer and row['priority'] <= 0: continue if row['dtype'] in ['Data']: temp_row = row continue value = row['value'] if isinstance(value, int): value *= 10**int(row['scaler']) if int(row['scaler']) < 0: format_str = '{val:.%df}' % (abs(int(row['scaler']))) value = format_str.format(val=value) print('value:', value) dtype = '' if is_show_type: dtype = '('+temp_row['dtype']+'_'+row['dtype']+')' if temp_row\ else ('('+row['dtype']+')' if row['dtype'] else '') if is_output_html: res_text += '<tr style="{color};">\ <td style="{padding} padding-right: 5px;">{messagerow}</td>\ <td style="{padding} padding-right: 5px;">{brief}{value}{unit}{dtype}</td></tr>'\ .format(color='color: %s;'%config.M_PRIORITY_COLOR[row['priority']],\ padding='padding-left: %d px;'%(row['depth'] * 10) if is_show_level else '',\ messagerow=commonfun.list2text(temp_row['m_list']+row['m_list']\ if temp_row else row['m_list']),\ brief=row['brief'].replace('<', '(').replace('>', ')') +':'\ if row['brief'] else '', dtype=dtype, value=value, unit=row['unit']) else: res_text += '{padding}{messagerow} —— {brief}{value}{unit}{dtype}\n'\ .format(padding=' '*row['depth'] if is_show_level else '',\ messagerow=commonfun.list2text(temp_row['m_list']+row['m_list']\ if temp_row else row['m_list']),\ brief=row['brief'].replace('<', '(').replace('>', ')') +':'\ if row['brief'] else '', dtype=dtype, value=value, unit=row['unit']) temp_row = None if is_output_html: res_text += '</table>' # print(res_text) return res_text
def add_linkLayer(apdu_list, CA_text='00', SA_text='00000001', logic_addr=0, SA_type=0, C_text='43'): """add linklayer""" SA_list = commonfun.text2list(SA_text) SA_list.reverse() # 小端 SA_text = ''.join(SA_list) if len(SA_text) % 2 == 1: SA_text += '0' L_text = '{0:04X}'.format(len(SA_text) // 2 + 9 + len(apdu_list)) L_text = L_text[2:4] + L_text[0:2] SA_param_text = '{0:02X}'.format(((len(SA_text) // 2 - 1) & 0x0f) | ((logic_addr & 0x03) << 4) | ((SA_type & 0x03) << 6)) hcs_clac_aera_text = L_text + C_text + SA_param_text + SA_text + CA_text hcs_calc = commonfun.get_fcs(commonfun.text2list(hcs_clac_aera_text)) hcs_calc = ((hcs_calc << 8) | (hcs_calc >> 8)) & 0xffff # 低位在前 fcs_calc_aera_text = hcs_clac_aera_text + '{0:04X}'.format( hcs_calc) + commonfun.list2text(apdu_list) fcs_calc = commonfun.get_fcs(commonfun.text2list(fcs_calc_aera_text)) fcs_calc = ((fcs_calc << 8) | (fcs_calc >> 8)) & 0xffff # 低位在前 return commonfun.format_text('68' + fcs_calc_aera_text + '{0:04X}'.format(fcs_calc) + '16')
def get_direction(self): """get direction""" if not self.is_success: return '-' depth0_list = [row for row in self.res_list if row['depth'] == 0] service_type = commonfun.list2text(list(filter(lambda row: row['dtype'] == 'service'\ , depth0_list))[0]['m_list']) if service_type[0] in ['0', '1'] and service_type != '01': return '→' else: return '←'
def get_link_replay_apdu(trans_object): """get_link_replay_apdu""" tm1_text = common.list2text(list(filter(lambda row: row['dtype'] == 'date_time'\ , trans_object.res_list))[0]['m_list']) tm_local = time.localtime() weekday = 0 if tm_local.tm_wday == 6 else tm_local.tm_wday + 1 tm2_text = '%04X %02X %02X %02X %02X %02X %02X 0000'\ % (tm_local[0], tm_local[1], tm_local[2],\ weekday, tm_local[3], tm_local[4], tm_local[5]) reply_apdu_text = '81 %s %s' % (trans_object.get_piid(), '80') + tm1_text + tm2_text + tm2_text return reply_apdu_text
def repair_cs(m_text): """repair_cs""" m_list = commonfun.text2list(m_text) offset = 1 link_length = int(m_list[offset + 1] + m_list[offset], 16) if link_length != len(m_list) - 2: right_len = len(m_list) - 2 m_list[offset + 1] = '{0:02X}'.format(right_len >> 8) m_list[offset] = '{0:02X}'.format(right_len & 0xff) offset += 2 # 控制域 ctrol = int(m_list[offset], 16) frame_separation_flag = (ctrol >> 5) & 0x01 # 控制域 offset += 1 # 地址域 server_addr_len = (int(m_list[offset], 16) & 0x0f) + 1 offset += server_addr_len + 1 # 客户机地址 offset += 1 # 帧头校验 hcs_calc = commonfun.get_fcs(m_list[1:offset]) hcs_calc = ((hcs_calc << 8) | (hcs_calc >> 8)) & 0xffff # 低位在前 hcs_now = int(m_list[offset] + m_list[offset + 1], 16) if hcs_now != hcs_calc: m_list[offset] = '{0:02X}'.format(hcs_calc >> 8) m_list[offset + 1] = '{0:02X}'.format(hcs_calc & 0xff) offset += 2 # 分帧 if frame_separation_flag == 1: offset += 2 trans_res = commonfun.TransRes() offset += applayer_do.take_applayer(m_list[offset:], trans_res) fcs_calc = commonfun.get_fcs(m_list[1:offset]) fcs_calc = ((fcs_calc << 8) | (fcs_calc >> 8)) & 0xffff # 低位在前 fcs_now = int(m_list[offset] + m_list[offset + 1], 16) if fcs_now != fcs_calc: m_list[offset] = '{0:02X}'.format(fcs_calc >> 8) m_list[offset + 1] = '{0:02X}'.format(fcs_calc & 0xff) m_text = commonfun.list2text(m_list) return m_text
def get_structed_msg(self, has_linklayer=True): """get_structed_msg""" res_text = '' if self.is_success else '\n\n' temp_row = None for row in self.res_list: if not has_linklayer and row['priority'] <= 0: continue if row['dtype'] in ['Data']: temp_row = row continue res_text += '{padding}{messagerow}\n'\ .format(padding=' '*row['depth'],\ messagerow=commonfun.list2text(temp_row['m_list']+row['m_list'] if temp_row else row['m_list'])) temp_row = None return res_text[:-1] #remove last \n
def get_rpt_replay_apdu(trans_object): """get_rpt_replay_apdu""" piid = trans_object.get_piid() service_choice = trans_object.get_service()[-2:] print('service_choice:', service_choice) if service_choice in ['01', '02']: oad_list = list(map(lambda x: common.list2text(x['m_list']),\ list(filter(lambda row: row['dtype'] == 'OAD' and row['depth'] == 1\ , trans_object.res_list)))) print('oad_list:', oad_list) oad_num = len(oad_list) reply_text = '08%s %s %02X %s 00' % (service_choice, piid, oad_num, ''.join(oad_list)) if service_choice == '03': reply_text = '0803%s 00' % piid return reply_text
def re_esam_info(self, re_text): res_sum = True m_data = common.text2list(re_text) data = common.get_apdu_list(m_data) offset = common.list2text(data).replace(' ', '').find('F1000200') // 2 + 4 if data[offset] == '01': offset += 1 ret = param.get_octet(data[offset:]) self.esam_no_box.setText(ret['octet']) offset += ret['offset'] else: res_sum = False self.esam_no_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) offset = common.list2text(data).replace(' ', '').find('F1000300') // 2 + 4 if data[offset] == '01': offset += 1 ret = param.get_octet(data[offset:]) self.esam_ver_box.setText(ret['octet']) offset += ret['offset'] else: res_sum = False self.esam_ver_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) offset = common.list2text(data).replace(' ', '').find('F1000400') // 2 + 4 if data[offset] == '01': offset += 1 ret = param.get_octet(data[offset:]) self.esam_key_box.setText(ret['octet']) offset += ret['offset'] else: res_sum = False self.esam_key_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) offset = common.list2text(data).replace(' ', '').find('F1000500') // 2 + 4 if data[offset] == '01': offset += 1 self.esam_dialog_tm_box.setText( str(param.get_double_long_unsigned(data[offset:]))) offset += 5 else: res_sum = False self.esam_dialog_tm_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) offset = common.list2text(data).replace(' ', '').find('F1000600') // 2 + 4 if data[offset] == '01': offset += 1 self.esam_dialog_remain_box.setText( str(param.get_double_long_unsigned(data[offset:]))) offset += 5 else: res_sum = False self.esam_dialog_remain_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) offset = common.list2text(data).replace(' ', '').find('F1000700') // 2 + 4 if data[offset] == '01': offset += 3 self.esam_addr_ctr_box.setText( str(param.get_double_long_unsigned(data[offset:]))) offset += 5 self.esam_rpt_ctr_box.setText( str(param.get_double_long_unsigned(data[offset:]))) offset += 5 self.esam_app_radio_box.setText( str(param.get_double_long_unsigned(data[offset:]))) offset += 5 else: res_sum = False self.esam_addr_ctr_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) self.esam_rpt_ctr_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) self.esam_app_radio_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) offset = common.list2text(data).replace(' ', '').find('F1000800') // 2 + 4 if data[offset] == '01': offset += 3 ret = param.get_octet(data[offset:]) self.esam_terminal_ver_box.setText(ret['octet']) offset += ret['offset'] ret = param.get_octet(data[offset:]) self.esam_host_ver_box.setText(ret['octet']) offset += ret['offset'] else: res_sum = False self.esam_terminal_ver_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) self.esam_host_ver_box.setText( '失败:' + base_data.get_dar(int(data[offset + 1], 16))) if res_sum is True: self.res_b.setStyleSheet('color: green') self.res_b.setText('成功') config.MASTER_WINDOW.receive_signal.disconnect(self.re_esam_info)
def get_brief(self): """get brief translate""" if not self.is_success: return '无效报文' if self.is_linklayer_sep: return '链路层分帧报文' brief = {} if not self.is_access_successed: brief['access_res'] = '(访问失败)' depth0_list = [row for row in self.res_list if row['depth'] == 0] depth1_list = [row for row in self.res_list if row['depth'] == 1] depth2_list = [row for row in self.res_list if row['depth'] == 2] try: service_type = commonfun.list2text(list(filter(lambda row: row['dtype'] == 'service'\ , depth0_list))[0]['m_list']) except IndexError: if self.is_ssal: return 'SSAL报文' else: return '错误报文' if service_type[1] == '8': brief['dir'] = '应答' if service_type[0] == '0' else '主动' else: brief['dir'] = '申请' if service_type[0] in ['0', '1'] else '回复' if service_type[1] in ['1']: if brief['dir'] == '申请': brief['service'] = '' brief['content'] = list(filter(lambda row: row['dtype'] == 'enum'\ , depth0_list))[0]['value'] else: brief['service'] = '登录/心跳' brief['content'] = '结果' + list(filter(lambda row: row['dtype'] == 'Result'\ , depth0_list))[0]['value'] elif service_type[1] in ['2']: brief['service'] = '建立应用连接' elif service_type[1] in ['3']: brief['service'] = '断开应用连接' elif service_type[1] in ['5']: brief['service'] = '读取记录' if service_type[-1] in [ '3', '4' ] else '读取' # todo 读取(成功/失败:原因) if service_type[-1] in ['2', '4']: brief['content'] = ','.join([row['value'].split('[索引')[0]\ for row in depth1_list if row['dtype'] == 'OAD']) elif service_type[-1] in ['5']: brief['content'] = '分帧序号%d的数据块'%list(filter(lambda row: row['dtype'] \ == 'long-unsigned', depth0_list))[0]['value'] else: brief['content'] = list(filter(lambda row: row['dtype'] == 'OAD'\ , depth0_list))[0]['value'].split('[索引')[0] elif service_type[1] in ['6']: brief['service'] = '设置后读取' if service_type[-1] == '3' else '设置' if service_type[-1] in ['2', '3']: brief['content'] = ','.join([row['value'].split('[索引')[0]\ for row in depth1_list if row['dtype'] == 'OAD']) else: brief['content'] = list(filter(lambda row: row['dtype'] == 'OAD'\ , depth0_list))[0]['value'].split('[索引')[0] elif service_type[1] in ['7']: brief['service'] = '操作后读取' if service_type[-1] == '3' else '操作' if service_type[-1] in ['2', '3']: brief['content'] = ','.join([row['value'].split('[操作模式')[0]\ for row in depth1_list if row['dtype'] == 'OMD']) else: brief['content'] = list(filter(lambda row: row['dtype'] == 'OMD'\ , depth0_list))[0]['value'].split('[操作模式')[0] elif service_type[1] in ['8']: brief['service'] = '上报' brief['content'] = ','.join([row['value'].split('[索引')[0]\ for row in depth1_list if row['dtype'] == 'OAD']) if service_type[-1] == '3': brief['content'] = '透明数据' elif service_type[1] in ['9']: brief['service'] = {'1': '代理读取', '2': '代理读取记录', '3': '代理设置', '4': '代理设置后读取',\ '5': '代理操作', '6': '代理操作后读取', '7': '代理透明转发'}.get(service_type[-1]) if service_type[-1] in ['1', '2', '3', '4']: brief['content'] = ','.join([row['value'].split('[索引')[0]\ for row in depth2_list if row['dtype'] == 'OAD']) if service_type[-1] in ['5', '6']: brief['content'] = ','.join([row['value'].split('[索引')[0]\ for row in depth2_list if row['dtype'] == 'OAD']) if service_type[-1] in ['7']: brief['content'] = '通道: ' + list(filter(lambda row: row['dtype'] == 'OAD'\ , depth0_list))[0]['value'].split('[索引')[0] elif service_type[1] in ['0']: brief['service'] = '安全传输' elif service_type[1] in ['E']: brief['service'] = '异常响应' return '%s%s%s %s'%(brief.get('access_res', ''), brief.get('dir', ''),\ brief.get('service', ''), brief.get('content', ''))
def get_raw_msg(self, brief): """get_raw_msg""" return commonfun.list2text(list(filter(lambda row: row['brief'].strip() == brief.strip()\ , self.res_list))[0]['m_list']).replace(' ', '').strip()
def get_piid(self): """get_piid""" return commonfun.list2text(list(filter(lambda row: row['dtype'] in ['PIID', 'PIID_ACD']\ , self.res_list))[0]['m_list']).replace(' ', '')
def get_service(self): """get service""" if self.is_ssal: return 'ssal' return commonfun.list2text(list(filter(lambda row: row['dtype'] == 'service'\ , self.res_list))[0]['m_list']).replace(' ', '')
def get_apdu_text(self): """get_apdu_text""" apdu_text = ''.join([commonfun.list2text(row['m_list'])\ for row in self.res_list if row['priority'] > 0]) return apdu_text