Skip to content

Commit

Permalink
Add basic encode support for ReaderEventNotification
Browse files Browse the repository at this point in the history
Adds support for encoring ReaderEventNotification messages including
the most basic ReaderEventNotificationData, ConnectionAttemptEvent.

new methods in llrp_proto:
* encode_ReaderEventNotification
* encode_ConnectionAttemptEvent

new test for ReaderEventNotification

fixed a bug with encoding UTCTimeStamps (wrong message header was used)
  • Loading branch information
fifieldt committed Nov 16, 2019
1 parent 18b101f commit 0237de1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
57 changes: 52 additions & 5 deletions sllurp/llrp_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,13 +835,33 @@ def decode_ReaderEventNotification(data):
return msg


def encode_ReaderEventNotification(msg):
logger.debug(func())
msg_header = '!BII'
msg_header_len = struct.calcsize(msg_header)
conntype = Message_struct['READER_EVENT_NOTIFICATION']['type']
ID = msg['ID']
# resolve the ReaderEventNotification Data
req = msg['ReaderEventNotificationData']
data = encode('ReaderEventNotificationData')(req)
logger.debug('ReaderEventNotificationData in ReaderEventNotification: %s', hexlify(data))
# add the ReaderEventNotification header
#msg_data = struct.pack(msg_header, conntype,
# len(data) + msg_header_len, ID)

#logger.debug('ReaderEventNotificationHeader in ReaderEventNotification: %s', hexlify(msg_data))
#data = msg_data + data
logger.debug('ReaderEventNotification data: %s', hexlify(data))
return data

Message_struct['READER_EVENT_NOTIFICATION'] = {
'type': 63,
'fields': [
'Ver', 'Type', 'ID',
'ReaderEventNotificationData'
],
'decode': decode_ReaderEventNotification
'decode': decode_ReaderEventNotification,
'encode': encode_ReaderEventNotification
}


Expand Down Expand Up @@ -913,9 +933,9 @@ def decode_UTCTimestamp(data):

def encode_UTCTimestamp(par):
msgtype = Message_struct['UTCTimestamp']['type']
msg = '!HHQ'
msg_header = '!HHQ'
msg_len = struct.calcsize(msg_header)
data = struct.pack(msg, msgtype, msg_len, par['Microseconds'])
data = struct.pack(msg_header, msgtype, msg_len, par['Microseconds'])
return data


Expand Down Expand Up @@ -3350,14 +3370,26 @@ def decode_ConnectionAttemptEvent(data):

return par, data[length:]

def encode_ConnectionAttemptEvent(msg):
logger.debug(func())
msgtype = Message_struct['ConnectionAttemptEvent']['type']
msg_header = '!HHH'
msg_len = struct.calcsize(msg_header)
status = ConnEvent_Name2Type[msg['Status']]
data = struct.pack(msg_header, msgtype, msg_len, status)
logger.debug('ConnectionAttemptEvent data: %s', hexlify(data))
return data



Message_struct['ConnectionAttemptEvent'] = {
'type': 256,
'fields': [
'Type',
'Status'
],
'decode': decode_ConnectionAttemptEvent
'decode': decode_ConnectionAttemptEvent,
'encode': encode_ConnectionAttemptEvent
}


Expand Down Expand Up @@ -3467,6 +3499,20 @@ def decode_ReaderEventNotificationData(data):
return par, body


def encode_ReaderEventNotificationData(msg):
# XXX Does not implement most fields.
logger.debug(func())
msg_header = '!HH'
msg_header_len = struct.calcsize(msg_header)
eventtype = Message_struct['ReaderEventNotificationData']['type']
# add the timestamp
data = encode('UTCTimestamp')(msg['UTCTimestamp'])
data += encode('ConnectionAttemptEvent')(msg['ConnectionAttemptEvent'])
data = struct.pack(msg_header, eventtype,
len(data) + msg_header_len) + data
logger.debug('ReaderEventNotificationData: %s', hexlify(data))
return data

Message_struct['ReaderEventNotificationData'] = {
'type': 246,
'fields': [
Expand All @@ -3484,7 +3530,8 @@ def decode_ReaderEventNotificationData(data):
'ConnectionCloseEvent',
'SpecLoopEvent'
],
'decode': decode_ReaderEventNotificationData
'decode': decode_ReaderEventNotificationData,
'encode': encode_ReaderEventNotificationData
}


Expand Down
22 changes: 22 additions & 0 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ def test_decode(self):
client.transport = MockConn('')
client.dataReceived(data)

def test_encode(self):
expected_result = binascii.unhexlify('043f000000200000000000f600160080'
'000c0000000000000000010000060000')
msg_dict = {'READER_EVENT_NOTIFICATION': {
'Ver': 1,
'Type': 63,
'ID': 0,
'ReaderEventNotificationData':
{
'UTCTimestamp':
{
'Microseconds': 0
},
'ConnectionAttemptEvent' :
{
'Status': 'Success'
}
}
}}
llrp_msg = sllurp.llrp.LLRPMessage(msgdict=msg_dict)
self.assertEqual(expected_result, llrp_msg.msgbytes)


class TestDecodeROAccessReport (unittest.TestCase):
_r = """
Expand Down

0 comments on commit 0237de1

Please sign in to comment.