Skip to content

Commit

Permalink
handle_line EVL input error handling
Browse files Browse the repository at this point in the history
* handle (and log) unknown TPI response codes. Fixes #45
* handle TPI timestamps if enabled. Fixes #43
* validate TPI input and verify checksum
  • Loading branch information
rct committed Jul 7, 2016
1 parent 58cb586 commit ee92bb6
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions core/envisalink.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import time, datetime, sys
import time, datetime, sys, re
from tornado import gen
from tornado.tcpclient import TCPClient
from tornado.iostream import IOStream, StreamClosedError
Expand Down Expand Up @@ -98,28 +98,50 @@ def send_command(self, code, data, checksum = True):
pass

@gen.coroutine
def handle_line(self, input):
if input == '':
def handle_line(self, rawinput):
if rawinput == '':
return

input = rawinput.strip()
if config.ENVISALINKLOGRAW == True:
logger.debug('RX RAW < "' + str(input).strip() + '"')
logger.debug('RX RAW < "' + str(input) + '"')

if re.match(r'^\d\d:\d\d:\d\d ',input):
evltime = input[:8]
input = input[9:]

if not re.match(r'^[0-9a-fA-F]{5,}$', input):
logger.warning('Received invalid TPI message: ' + repr(rawinput));
return

code = int(input[:3])
parameters = input[3:][:-2]
try:
event = getMessageType(int(code))
except KeyError:
logger.warning('Received unknown TPI code: "%s", parameters: "%s"' %
(input[:3], parameters))
return

rcksum = int(input[-2:], 16)
ccksum = int(get_checksum(input[:3],parameters), 16)
if rcksum != ccksum:
logger.warning('Received invalid TPI checksum %02X vs %02X: "%s"' %
(rcksum, ccksum, input))
return

code=int(input[:3])
parameters=input[3:][:-4]
event = getMessageType(int(code))
message = self.format_event(event, parameters)
logger.debug('RX < ' +str(code)+' - '+message)

try:
handler = "handle_%s" % evl_ResponseTypes[code]['handler']
handler = "handle_%s" % event['handler']
except KeyError:
handler = "handle_event"

try:
func = getattr(self, handler)
if handler != 'handle_login':
events.put('proxy', None, input)
events.put('proxy', None, rawinput)
except AttributeError:
raise CodeError("Handler function doesn't exist")

Expand Down

0 comments on commit ee92bb6

Please sign in to comment.