def __init__(self, pkt): self.pkt = pkt try: self.parse_header(pkt) except: _debug.dump(pkt, 'header exception') raise
def read(self, count=1024): if debug: print 'Read from socket: ', count, self.socket answer = self.socket.recv(count, self.timeout) if debug: print 'Got from socket:' _debug.dump(answer, 'Read from socket') return answer
def _process_connection(self): #called from thread if self.debug: print 'process connection to : ', self.connection while 1: #self.connection.is_established_connection(): if self.debug: print 'Wait for MBAP' try: mbap = self.connection.read_MBAP() except: if self.debug: print 'Peer closed connection' mbap = None if self.debug: print 'TcpIpServer, MBAP is: ', mbap if mbap is None: if self.debug: print 'TcpIpServer no MBAP, close connection' break if not mbap.is_message_header(): if debug: print 'TcpIpServer is not message header' break if self.debug: print 'TcpIpServer, read rest of command: ', mbap.length data = self.connection.read(mbap.length) #timeouts? if not data: if self.debug: print 'No data received, close connection and wait' break #connection closed, go wait for another if self.debug: print 'TcpIpServer, got command:' _debug.dump(data, 'command data') if len(data) != mbap.length: if self.debug: print 'TcpIpServer, did not get enough data' break if self.debug: print 'TcpIpServer, got data: ', len(data) response = None response = self.device_server.command( data) #line handler will find the correct device node if response: if self.debug: print 'TcpIpServer, got resposne:' _debug.dump(response.buffer, 'response') mbap.length = len(response.buffer) mbap._update_encoding() self.connection.write(mbap.encoding + response.buffer) else: #no response if self.debug: print 'TcpIpServer, no response to command!' break
def _process_connection(self): #called from thread if self.debug: print 'process connection to : ', self.connection while 1: #self.connection.is_established_connection(): if self.debug: print 'Wait for MBAP' try: mbap = self.connection.read_MBAP() except: if self.debug: print 'Peer closed connection' mbap = None if self.debug: print 'TcpIpServer, MBAP is: ', mbap if mbap is None: if self.debug: print 'TcpIpServer no MBAP, close connection' break if not mbap.is_message_header(): if debug: print 'TcpIpServer is not message header' break if self.debug: print'TcpIpServer, read rest of command: ', mbap.length data = self.connection.read(mbap.length) #timeouts? if not data: if self.debug: print 'No data received, close connection and wait' break #connection closed, go wait for another if self.debug: print 'TcpIpServer, got command:' _debug.dump(data, 'command data') if len(data) != mbap.length: if self.debug: print 'TcpIpServer, did not get enough data' break if self.debug: print 'TcpIpServer, got data: ', len(data) response = None response = self.device_server.command(data) #line handler will find the correct device node if response: if self.debug: print 'TcpIpServer, got resposne:' _debug.dump(response.buffer, 'response') mbap.length = len(response.buffer) mbap._update_encoding() self.connection.write(mbap.encoding + response.buffer) else: #no response if self.debug: print 'TcpIpServer, no response to command!' break
def _parse_screen_update_body(self, pkt, start): i = start last_i = i tokens = [] x = 0 y = 0 token = None delimiter = 0 try: while i < self.pkt_len: #don't run past end of valid data on catenated packets last_delimiter = delimiter delimiter = 0 last_token = token token = ord(pkt[i]) tokens.append((i,token,)) #tokens.append((last_i,last_token,token,i-last_i)) last_i = i if debug > 3: print 'token: %X index: %X' % (token,i) if (token == 0 and last_token == 8) or \ (token == 8 and last_token == 0) or \ (token == 0 and last_token == 0x0F) or \ (token == 0x0F and last_token == 0) or \ (token == 0 and last_token == 0x0A) or \ (token == 0x0F and last_token == 0x0A) or \ (token == 0 and last_token == 0x09) or \ (token == 0x0F and last_token == 0x09) or \ (token == 0 and last_token == 0x0C): #text string or xy follows str_len = ord(pkt[i+1]) if (str_len == 1) and (last_token != 8) and (last_token != 0) and (last_token != 0x0F): i += 2 delimiter = 1 continue if str_len > 0: text = pkt[i+2:i+str_len+2] text = text.split('\x00')[0] #some strings are zero terminated prior to the full length text = text.strip() #remove leading and trailing whitespace. if last_token != 0x0A: #this token preceeds raw binary self.texts[(x,y)] = TextElement(text, x, y) #add text to our dict if debug > 2: print 'found text: ', text i += str_len + 2 continue if (token == 0x08 or token == 0 or token == 0x0F): #back space - clear - so far the last token seems to always be 0D if ord(pkt[i+1]) == 1: #DLE , next four bytes are not position i += 2 delimiter = 1 continue x,y = self.decode_xy(pkt[i+1:i+5]) if x > 640 or y > 640: #raise EInvalidValue('token 8, 1st xy value wrong') pass i += 5 continue if token == 0x09: #define area x,y = self.decode_xy(pkt[i+5:i+9]) flag = x == 640 and y == 434 if x > 640 or y > 640: raise EInvalidValue('token 9, 2nd xy value wrong') x,y = self.decode_xy(pkt[i+1:i+5]) if flag and x == 0 and y == 37: self.texts = {} #a cleared screen has no values... self.screen_cleared = 1 #let the screen object know if debug > 1: print CSI_GREEN+'Clear Screen'+CSI_Reset #clear all texts? if x > 640 or y > 640: raise EInvalidValue('token 9, 1st xy value wrong') i += 9 continue if (token == 0x0A): # draw line... get new xy and continue if ord(pkt[i+1]) == 1: #DLE , next four bytes are not position i += 2 delimiter = 1 continue if last_token == 0x0D: #so far just one case of non-10 length i += 5 continue if last_token == 0 and not last_delimiter: l = ord(pkt[i+1]) + 2 #only have seen 80's here but it might different if l == 80: #only have seen lengths of 80 bytes (640 bits) for this i += l continue #when last 0 token was delimited, it is always an x,y packet try: x,y = self.decode_xy(pkt[i+1:i+5]) if x > 640 or y > 640: raise EInvalidValue('token A, 1st xy value wrong') except: if last_token == 0: #last chance but I don't think we will see this happen l = ord(pkt[i+1]) + 2 i += l continue raise i += 10 #all other tokens follwing A have been 10 long continue if token == 0x0B: i += 32 continue if token == 0x0C: #text block if ord(pkt[i+1]) == 1: #DLE , next four bytes are not position i += 2 delimiter = 1 continue if last_token == 0: #no xy cords, just use xy from last 0 token i += ord(pkt[i+1]) + 2 continue x,y = self.decode_xy(pkt[i+1:i+5]) if x > 640 or y > 640: raise EInvalidValue('token C, xy value wrong') bl = ord(pkt[i+5]) #length of block following bl += ord(pkt[i+6]) * 256 #don't know for sure about this second byte i += (bl + 8) continue if token == 0x0D: #new line x,y = self.decode_xy(pkt[i+5:i+9]) if x > 640 or y > 640: raise EInvalidValue('token D, 2nd xy value wrong') x,y = self.decode_xy(pkt[i+1:i+5]) if x > 640 or y > 640: raise EInvalidValue('token D, 1st xy value wrong') i += 9 continue if token == 0x0E: i += 2 continue if token == 0x03: next_token = ord(pkt[i+1]) if next_token in (1,2,4,5): #add more 2nd bytes that inc index by 2 i += 2 continue raise EInvalidValue('token 3, 2nd byte sucks') if token == 0x01: i += 9 continue if token == 0x02: i += 3 #ord(pkt[i+1]) + 2 continue if token == 0x04: i += 2 continue if token == 0x05: i += 3 continue if token == 0x07: next_token = ord(pkt[i+1]) if next_token == 2: i += 2 continue raise EInvalidValue('token 7, 2nd byte sucks') if token == 0x7F: i += 5 continue if token in range(0x20, 0x31): i += 1 continue raise EInvalidValue('unknown token: %X index: %X' % (token,i)) except: if debug and (start == 0x2E): #print this only once per packet print str(tokens) try: _debug.dump(pkt, 'unknown token: %X index: %X' % (token,i)) except: _debug.dump(pkt, 'unknown token: '+str(token)+' index: '+str(i)) return i #let caller know where the failure occured #scan for text elements and place into XY keyed dictionary #print str(tokens) return None #indicates success
def write(self, buffer): if debug: _debug.dump(buffer, 'TcpConnection write bytes: ') self.socket.send(buffer, self.timeout)
def _parse_screen_update_body(self, pkt, start): i = start last_i = i tokens = [] x = 0 y = 0 token = None delimiter = 0 try: while i < self.pkt_len: #don't run past end of valid data on catenated packets last_delimiter = delimiter delimiter = 0 last_token = token token = ord(pkt[i]) tokens.append(( i, token, )) #tokens.append((last_i,last_token,token,i-last_i)) last_i = i if debug > 3: print 'token: %X index: %X' % (token, i) if (token == 0 and last_token == 8) or \ (token == 8 and last_token == 0) or \ (token == 0 and last_token == 0x0F) or \ (token == 0x0F and last_token == 0) or \ (token == 0 and last_token == 0x0A) or \ (token == 0x0F and last_token == 0x0A) or \ (token == 0 and last_token == 0x09) or \ (token == 0x0F and last_token == 0x09) or \ (token == 0 and last_token == 0x0C): #text string or xy follows str_len = ord(pkt[i + 1]) if (str_len == 1) and (last_token != 8) and ( last_token != 0) and (last_token != 0x0F): i += 2 delimiter = 1 continue if str_len > 0: text = pkt[i + 2:i + str_len + 2] text = text.split( '\x00' )[0] #some strings are zero terminated prior to the full length text = text.strip( ) #remove leading and trailing whitespace. if last_token != 0x0A: #this token preceeds raw binary self.texts[(x, y)] = TextElement( text, x, y) #add text to our dict if debug > 2: print 'found text: ', text i += str_len + 2 continue if ( token == 0x08 or token == 0 or token == 0x0F ): #back space - clear - so far the last token seems to always be 0D if ord(pkt[ i + 1]) == 1: #DLE , next four bytes are not position i += 2 delimiter = 1 continue x, y = self.decode_xy(pkt[i + 1:i + 5]) if x > 640 or y > 640: #raise EInvalidValue('token 8, 1st xy value wrong') pass i += 5 continue if token == 0x09: #define area x, y = self.decode_xy(pkt[i + 5:i + 9]) flag = x == 640 and y == 434 if x > 640 or y > 640: raise EInvalidValue('token 9, 2nd xy value wrong') x, y = self.decode_xy(pkt[i + 1:i + 5]) if flag and x == 0 and y == 37: self.texts = {} #a cleared screen has no values... self.screen_cleared = 1 #let the screen object know if debug > 1: print CSI_GREEN + 'Clear Screen' + CSI_Reset #clear all texts? if x > 640 or y > 640: raise EInvalidValue('token 9, 1st xy value wrong') i += 9 continue if (token == 0x0A): # draw line... get new xy and continue if ord(pkt[ i + 1]) == 1: #DLE , next four bytes are not position i += 2 delimiter = 1 continue if last_token == 0x0D: #so far just one case of non-10 length i += 5 continue if last_token == 0 and not last_delimiter: l = ord( pkt[i + 1] ) + 2 #only have seen 80's here but it might different if l == 80: #only have seen lengths of 80 bytes (640 bits) for this i += l continue #when last 0 token was delimited, it is always an x,y packet try: x, y = self.decode_xy(pkt[i + 1:i + 5]) if x > 640 or y > 640: raise EInvalidValue('token A, 1st xy value wrong') except: if last_token == 0: #last chance but I don't think we will see this happen l = ord(pkt[i + 1]) + 2 i += l continue raise i += 10 #all other tokens follwing A have been 10 long continue if token == 0x0B: i += 32 continue if token == 0x0C: #text block if ord(pkt[ i + 1]) == 1: #DLE , next four bytes are not position i += 2 delimiter = 1 continue if last_token == 0: #no xy cords, just use xy from last 0 token i += ord(pkt[i + 1]) + 2 continue x, y = self.decode_xy(pkt[i + 1:i + 5]) if x > 640 or y > 640: raise EInvalidValue('token C, xy value wrong') bl = ord(pkt[i + 5]) #length of block following bl += ord(pkt[ i + 6]) * 256 #don't know for sure about this second byte i += (bl + 8) continue if token == 0x0D: #new line x, y = self.decode_xy(pkt[i + 5:i + 9]) if x > 640 or y > 640: raise EInvalidValue('token D, 2nd xy value wrong') x, y = self.decode_xy(pkt[i + 1:i + 5]) if x > 640 or y > 640: raise EInvalidValue('token D, 1st xy value wrong') i += 9 continue if token == 0x0E: i += 2 continue if token == 0x03: next_token = ord(pkt[i + 1]) if next_token in ( 1, 2, 4, 5): #add more 2nd bytes that inc index by 2 i += 2 continue raise EInvalidValue('token 3, 2nd byte sucks') if token == 0x01: i += 9 continue if token == 0x02: i += 3 #ord(pkt[i+1]) + 2 continue if token == 0x04: i += 2 continue if token == 0x05: i += 3 continue if token == 0x07: next_token = ord(pkt[i + 1]) if next_token == 2: i += 2 continue raise EInvalidValue('token 7, 2nd byte sucks') if token == 0x7F: i += 5 continue if token in range(0x20, 0x31): i += 1 continue raise EInvalidValue('unknown token: %X index: %X' % (token, i)) except: if debug and (start == 0x2E): #print this only once per packet print str(tokens) try: _debug.dump(pkt, 'unknown token: %X index: %X' % (token, i)) except: _debug.dump( pkt, 'unknown token: ' + str(token) + ' index: ' + str(i)) return i #let caller know where the failure occured #scan for text elements and place into XY keyed dictionary #print str(tokens) return None #indicates success