def on_new_client(clientsocket, addr): try: while True: # msg = clientsocket.recv(2024) # print('connected by',addr) data = clientsocket.recv(5000) print(sys.stderr, 'received raw "%s"', data) print('type raw:', type(data)) #unpacked_data = unpacker.unpack(data) #print(sys.stderr, 'unpacked:', unpacked_data) #m = str(msg) #print('message',m) imei = binascii.hexlify(data) imei2 = "null" print('data hexlified - ', imei) print('type', type(imei)) print('data ascii ed - ', imei2) print('type', type(imei2)) #l = 'LOAD' #p = hex(crc16(imei)) str_data = str(imei) parsed_data = str_data[26:30] first_data = str_data[2:6] length = '0501' print('first-data', first_data) print('length', length) print('parsed-data', parsed_data) #p = str(p) #s = p[2:6] error_check = 'ECHE' print('error_check - ', error_check) strink = first_data + length + parsed_data + error_check + '0d0a' ret1 = str.encode(strink) print('pre_output', ret1) mystr = strink[4:12] print(mystr) ret1 = str.encode(mystr) crc16 = libscrc.x25(binascii.unhexlify(ret1)) ra = hex(crc16) print('pre_errorc', ra) ra = str(ra) error_check = ra[2:6] print('final_error', error_check) strink = first_data + length + parsed_data + error_check + '0d0a' print('final_string', strink) ret2 = str.encode(strink) ret1 = binascii.unhexlify(ret2) # print('crc-',hex(crc16(ret1))) print('ret - ', ret1) print('ret type - ', type(ret1)) clientsocket.send(ret1) clientsocket.close() except socket.error as message: print(message) clientsocket.close()
def check_frame(self): if self.r[0] != flag or self.r[-1] != flag: if self.verbose: print("Flags not OK! first= 0x{0:02x} last= 0x{1:02x}".format( self.r[0], self.r[-1])) return False # strip flags self.r = self.r[1:-1] packagelen = int.from_bytes(self.r[0:2], byteorder="big") & 0xfff if (packagelen) != len(self.r): if self.verbose: print( "Length not OK! from header={0:4d} received={0:4d}".format( packagelen, len(self.r))) return False crc1 = libscrc.x25(self.r[0:-2]) self.crc2 = int.from_bytes(self.r[-2:], byteorder="little") if crc1 != self.crc2: if self.verbose: print( "CRC not OK! calculated= 0x{0:04x} sent= 0x{1:04x}".format( crc1, self.crc2)) return False frameheader = self.r[3:12] dataheader = self.r[12:18] self.N_lines = self.r[N_header - 1] if self.debug: print("Package length = {0:4d}".format(packagelen)) print("Nr of registers = {0:4d}".format(self.N_lines)) print("\nFlag:", end="\t\t\t\t\t\t") print("{0:02x}".format(flag), end=" ") print("\nHeader:", end="\t\t\t\t\t\t") for i in range(N_header): print("{0:02x}".format(self.r[i]), end=" ") print("") # strip header and trailer self.r = self.r[N_header:-2] return True
def crc_check(string_crc, error_check): crc16 = libscrc.x25(string_crc) var = 0 final_crc = "" for j in hex(crc16): if var > 1: final_crc = final_crc + j var += 1 if len(final_crc) == 3: final_crc = "0" + final_crc elif len(final_crc) == 2: final_crc = "00" + final_crc elif len(final_crc) == 1: final_crc = "000" + final_crc print(f"Generated crc: {final_crc}") print(f"sent by terminal: {error_check}") if final_crc == error_check: return 1 else: return 0
def on_new_client(clientsocket, addr): try: while True: # msg = clientsocket.recv(2024) # print('connected by',addr) data = clientsocket.recv(5000) print(sys.stderr, 'received raw "%s"', data) print('type :', type(data)) #unpacked_data = unpacker.unpack(data) #print(sys.stderr, 'unpacked:', unpacked_data) #m = str(msg) #print('message',m) imei = binascii.hexlify(data) imei2 = "null" # print('hexlified - ',imei) # print('hexlified len - ', type(imei)) # print('hexlified string - ',str(imei)) # str_imei = str(imei) # print('hexlified string length - ', len(str_data)) #print('type',type(imei)) #print('data ascii ed - ',imei2) #print('type',type(imei2)) #l = 'LOAD' #p = hex(crc16(imei)) str_data = str(imei) #print('01',str_data[8:10]) if str_data[8:10] == '01': #login packet parsed_data = str_data[26:30] first_data = str_data[2:6] #7878 length = '0501' #print('first-data', first_data) #print('length', length) #print('parsed-data', parsed_data) #p = str(p) #s = p[2:6] print('str_data', str_data) error_check = 'ECHE' #print('error_check - ',error_check) strink = first_data + length + parsed_data + error_check + '0d0a' ret1 = str.encode(strink) #print('pre_output',ret1) # print('strink - ',strink) mystr = strink[4:12] #print(mystr) ret1 = str.encode(mystr) crc16 = libscrc.x25(binascii.unhexlify(ret1)) ra = hex(crc16) #print('pre_errorc', ra) ra = str(ra) error_check = ra[2:6] #print('final_error',error_check) strink = first_data + length + parsed_data + error_check + '0d0a' print('final_string', strink) ret2 = str.encode(strink) # print(type(ret2)) print(ret2) ret1 = binascii.unhexlify(ret2) #print('crc-',hex(crc16(ret1))) print('ret - ', ret1) #print('ret type - ',type(ret1)) clientsocket.send(ret1) elif str_data[8:10] == '13': #status packet first_data = str_data[2:6] last = '0a0d' #print('first-data',first_data) len = '0513' #print('length',len) serial_no = str_data[18:22] # print('serial_no',serial_no) final = len + serial_no re = str.encode(final) crc16 = libscrc.x25(binascii.unhexlify(re)) ra = str(hex(crc16)) error_check = ra[2:6] #print('errror_check',error_check) finalout = first_data + final + error_check + last #print('final', finalout) ret2 = str.encode(finalout) #print('encode',ret2) ret1 = binascii.unhexlify(ret2) #print('ret - ', ret1) #print('ret type - ', type(ret1)) p = str_data[1::] p = p.replace("'", "") #print('after', p) reqbeat = p[8:18] result = status_packet(reqbeat) print("result of status packet", result) clientsocket.send(ret1) elif str_data[8:10] == '12': #data packet p = str_data[1::] p = p.replace("'", "") # print('after', p) s = p len = s[4:6] protocolno = s[6:8] date = s[8:20] satellites = s[20:22] lat = s[22:30] long = s[30:38] speed = s[38:40] course_status = s[40:44] mcc = s[44:48] mnc = s[48:50] lac = s[50:54] cell_id = s[54:60] serial_no = s[60:64] resdate = date_fun(date) latitude = latloncalc(lat) longitude = latloncalc(long) speedans = speedcalc(speed) course = course_status_fun(course_status) mccans = mcc_fun(mcc) print('date -', resdate, 'lat - ', latitude, 'lon - ', longitude, 'speed - ', speedans, 'course - ', course, 'mcc - ', mccans) # print('len -', len, '__', # 'protocolno', protocolno, '__', # 'date', date, '__', # 'satellites', satellites, '__', # 'lat', lat, '__', # 'long', long, '__', # 'speed', speed, '__', # 'course_status', course_status, '__', # 'mcc ', mcc, '__', # 'mnc ', mnc, '__', # 'lac ', lac, '__', # 'cell_id ', cell_id, '__', # 'serial_no', serial_no, '__') else: print('something new----', str_data) print('closing connection....') clientsocket.close() except socket.error as message: print('something wrong is happening', message) clientsocket.close()
def generateCrc(self, assign = True): self._crc = libscrc.x25(bytes(self._data[self.getAddress():self.getFcs()])).to_bytes(2, byteorder='little') if assign: self.assignBytes(self.getFcs(), self._crc, calcCrc = False)
ret1 = str.encode(first_data+length+parsed_data+s+'0d0a') print('final output',ret1) ret2 = str.encode('LOAD') ret1 = binascii.unhexlify(ret1) #print('crc-',hex(crc16(ret1))) print('ret - ',ret1) print('ret type - ',type(ret1)) clientsocket.send(ret1) clientsocket.close() except socket.error as message: print(message) clientsocket.close() import libscrc import binascii mystr = '0d010866968031423081000f' ret1 = str.encode(mystr) crc16 = libscrc.x25(binascii.a2b_hex('0d010866968031423081000f')) print('binascii', binascii.a2b_hex('0d010866968031423081000f')) print('unhexlify', ret1=binascii.unhexlify(ret1)) print(crc16) ans2 = libscrc.x25(b'0d010866968031423081000f') print('ans input as string', ans2)
def handle_client(conn, addr): print(f"New connection : [{addr}]") connected = True sno = 0 snoh = 0 snot = 0 terminal_id = "" snoc = 0 login = "******" while connected: #if login=="login_done": # message=input("Enter the message you want to send: ") # cpacket_length=len(message)+12 # cpl=hex(cpacket_length) # mk=0 # ghj="" # for i in cpl: # if mk>1: # ghj=ghj+i # mk+=1 # if len(ghj)==1: # ghj="0"+ghj # command_len=hex(len(message)+4) # jjk="" # mk=0 # for j in command_len: # if mk>1: # jjk=jjk+j # mk+=1 # if len(jjk)==1: # jjk="0"+jjk # enmessage=message.encode().hex() # if snoc<65353: # snoc+=1 # else: # snoc=0 # uu=0 # ss="" # snohmc=hex(snoc) # for k in snohmc: # if uu>1: # ss=ss+k # uu+=1 # if len(ss)==1: # ss="000"+ss # elif len(ss)==2: # ss="00"+ss # elif len(ss)==3: # ss="0"+ss # nu=0 # return_crc="" # for cp in hex(libscrc.x25(bytes.fromhex(ghj+"80"+jjk+"0000"+enmessage+"0002"+ss))): # if nu>1: # return_crc=return_crc+cp # nu+=1 # if len(return_crc)==3: # return_crc="0"+return_crc # command="7878"+ghj+"80"+jjk+"0000"+enmessage+"0002"+ss+return_crc+"0d0a" # command=bytes.fromhex(command) # conn.send(command) # return_crc="" #command="787810800900004487888823000200011dab0d0a" #scommand=bytes.fromhex(command) #print(scommand) #conn.send(scommand) #login="******" start_bit = conn.recv(2) if start_bit.hex() == '7878': packet_length = conn.recv(1) protocol = conn.recv(1) print(protocol.hex()) if protocol.hex() == '01': #login packet complete terminal_id = conn.recv(8) code = conn.recv(2) time_zone = conn.recv(2) snol = conn.recv(2) error_check = conn.recv(2) stop_bit = conn.recv(2) snoh = 0 snot = 0 snoc = 0 if sno < 65353: sno += 1 else: sno = 0 uu = 0 ss = "" snom = hex(sno) for k in snom: if uu > 1: ss = ss + k uu += 1 if len(ss) == 1: ss = "000" + ss elif len(ss) == 2: ss = "00" + ss elif len(ss) == 3: ss = "0" + ss print(f"serial number of the message: {ss}") string_crc = packet_length + protocol + terminal_id + code + time_zone + snol print(f"Login packet recieved: {string_crc.hex()}") bollen = crc_check(string_crc, error_check.hex()) if bollen: nu = 0 return_crc = "" for cp in hex(libscrc.x25(bytes.fromhex("0501" + ss))): if nu > 1: return_crc = return_crc + cp nu += 1 if len(return_crc) == 3: return_crc = "0" + return_crc elif len(return_crc) == 2: return_crc = "00" + return_crc elif len(return_crc) == 1: return_crc = "000" + return_crc response = "7878" + "05" + "01" + ss + return_crc + "0d0a" response = bytes.fromhex(response) conn.send(response) return_crc = "" print("response of login sent") Data_Base.login_insert(terminal_id.hex(), snol.hex()) else: print("[Invalid crc]") elif protocol.hex() == '23': #heart beat packet complete info_content = conn.recv(1) voltage_level = conn.recv(2) gsm_signal = conn.recv(1) port_status = conn.recv(2) snoj = conn.recv(2) error_check = conn.recv(2) stop_bit = conn.recv(2) if snoh < 65353: snoh += 1 else: snoh = 0 uu = 0 ss = "" snohm = hex(snoh) for k in snohm: if uu > 1: ss = ss + k uu += 1 if len(ss) == 1: ss = "000" + ss elif len(ss) == 2: ss = "00" + ss elif len(ss) == 3: ss = "0" + ss print(f"serial number of the message: {ss}") string_crc = packet_length + protocol + info_content + voltage_level + gsm_signal + port_status + snoj print(f"Heart Beat packet recieved: {string_crc.hex()}") bollen = crc_check(string_crc, error_check.hex()) if bollen: nu = 0 for cp in hex(libscrc.x25(bytes.fromhex("0523" + ss))): if nu > 1: return_crc = return_crc + cp nu += 1 if len(return_crc) == 3: return_crc = "0" + return_crc elif len(return_crc) == 2: return_crc = "00" + return_crc elif len(return_crc) == 1: return_crc = "000" + return_crc response = "7878" + "05" + "23" + ss + return_crc + "0d0a" print(f"response sent: {response}") response = bytes.fromhex(response) conn.send(response) return_crc = "" print("[response of heart beat sent]") Data_Base.heartbeat_insert(info_content.hex(), voltage_level.hex(), gsm_signal.hex(), snoj.hex()) login = "******" else: print("[Invalid crc]") elif protocol.hex() == '22': #GPS packet complete date_time = conn.recv(6) quantity = conn.recv(1) latitude = conn.recv(4) longitude = conn.recv(4) speed = conn.recv(1) course = conn.recv(2) mcc = conn.recv(2) mnc = conn.recv(1) lac = conn.recv(2) cell_id = conn.recv(3) acc = conn.recv(1) data_upload = conn.recv(1) gps_reupload = conn.recv(1) snogg = conn.recv(2) error_check = conn.recv(2) stop_bit = conn.recv(2) print(f"Date Time: {date_time}") print(f"Latitude: {latitude}, {int(latitude.hex(),16)}") print(f"Longitude: {longitude}, {int(longitude.hex(),16)}") print(datetime.now()) Data_Base.location_insert(terminal_id.hex(), datetime.now(), int(latitude.hex(), 16), int(longitude.hex(), 16), course.hex(), mcc.hex(), mnc.hex(), lac.hex(), cell_id.hex(), data_upload.hex(), gps_reupload.hex(), snogg.hex()) print("[No response]") elif protocol.hex() == '8a': #time packet incomplete snotg = conn.recv(2) error_check = conn.recv(2) stop_bit = conn.recv(2) if snot < 65353: snot += 1 else: snot = 0 uu = 0 ss = "" snotm = hex(snot) for k in snotm: if uu > 1: ss = ss + k uu += 1 if len(ss) == 1: ss = "000" + ss elif len(ss) == 2: ss = "00" + ss elif len(ss) == 3: ss = "0" + ss print(f"serial number of the message: {ss}") string_crc = packet_length + protocol + snotg print(f"Time packet recieved: {string_crc.hex()}") bollen = crc_check(string_crc, error_check.hex()) if bollen: nu = 0 for cp in hex(libscrc.x25(bytes.fromhex("0b8a" + ss))): if nu > 1: return_crc = return_crc + cp nu += 1 if len(return_crc) == 3: return_crc = "0" + return_crc elif len(return_crc) == 2: return_crc = "00" + return_crc elif len(return_crc) == 1: return_crc = "000" + return_crc response = "7878" + "05" + "8a" + ss + return_crc + "0d0a" response = bytes.fromhex(response) conn.send(response) return_crc = "" print("[response of time sent]") else: print("[Invalid crc]") elif protocol.hex() == '2c': #wifi packet full_packet = conn.recv(72) print(f"wifi packet recieved: {full_packet.hex()}") print("[No response]") elif protocol.hex() == '28': full_packet = conn.recv(61) #LBS packet print("[No response]") elif start_bit == '7979': #information transmission packet packet_length = conn.recv(2) protocol = conn.recv(1) print(protocol) length = int(packet_length.hex()) full_packet = conn.recv(length + 1) print(f"info packet recieved: {full_packet.hex()}") print("[No response]") conn.close()