Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def from_bytes(data):
return AmsPacket(
AmsTcpHeader.from_bytes(data[:6]), AmsHeader.from_bytes(data[6:]), data[38:]
)
function_map = {
constants.ADSCOMMAND_READDEVICEINFO: handle_read_device_info,
constants.ADSCOMMAND_READ: handle_read,
constants.ADSCOMMAND_WRITE: handle_write,
constants.ADSCOMMAND_READWRITE: handle_read_write,
constants.ADSCOMMAND_READSTATE: handle_read_state,
constants.ADSCOMMAND_WRITECTRL: handle_writectrl,
constants.ADSCOMMAND_ADDDEVICENOTE: handle_add_devicenote,
constants.ADSCOMMAND_DELDEVICENOTE: handle_delete_devicenote,
constants.ADSCOMMAND_DEVICENOTE: handle_devicenote,
}
response_content = function_map[request.ams_header.command_id]()
return AmsPacket(
amstcp_header=AmsTcpHeader(37 + len(response_content)),
ams_header=AmsHeader(
# swap target and source ams-id and port
target_net_id=request.ams_header.source_net_id,
target_port=request.ams_header.source_port,
source_net_id=request.ams_header.target_net_id,
source_port=request.ams_header.target_port,
# mirror the command id from the request
command_id=request.ams_header.command_id,
# set response flag
state_flags=request.ams_header.state_flags | 0x0001,
data_length=len(response_content),
error_code=0,
invoke_id=request.ams_header.invoke_id,
),
ads_data=response_content
)
def test_amstcpheader(self):
# test for empty data
x = structs.AmsTcpHeader()
self.assertEqual(b"\x00" * 6, x.to_bytes())
self.assertEqual(0, x.length)
# check for correct unpacking
x = structs.AmsTcpHeader.from_bytes(b"\x00\x00\x1f\x00\x00\x00")
# check for correct packing
x.length = 255
self.assertEqual(b"\x00\x00\xff\x00\x00\x00", x.to_bytes())
def from_bytes(data):
assert isinstance(data, (bytes, bytearray))
assert len(data) == 6
return AmsTcpHeader(struct.unpack("
stream = AdsNotificationStream(stamps=[stamp])
ams_header = AmsHeader(
target_net_id=self.ams_net_id,
target_port=self.ams_port,
source_net_id=self.server.ams_addr._ams_addr.netId,
source_port=self.server.port,
command_id=constants.ADSCOMMAND_DEVICENOTE,
state_flags=constants.ADSSTATEFLAG_REQRESP,
data_length=stream.length,
error_code=0,
invoke_id=0,
)
return AmsPacket(
amstcp_header=AmsTcpHeader(length=ams_header.length + stream.length),
ams_header=ams_header,
ads_data=stream.to_bytes(),
)
source_addr = AmsAddr("127.0.0.1.1.1", 59558)
target_addr = AmsAddr("172.38.1.1.1.1", 59559)
header1 = structs.AmsHeader(
target_net_id=target_addr.netIdStruct(),
target_port=target_addr.port,
source_net_id=source_addr.netIdStruct(),
source_port=source_addr.port,
command_id=pyads.constants.ADSCOMMAND_READDEVICEINFO,
state_flags=pyads.constants.ADSSTATEFLAG_COMMAND,
data_length=6,
error_code=1,
invoke_id=2,
)
ads_data = b"\x00" * 6
amstcpheader = structs.AmsTcpHeader(header1.length + len(ads_data))
packet = structs.AmsPacket(amstcpheader, header1, ads_data)
# first six bytes should be amstcpheader
self.assertEqual(packet.to_bytes()[:6], amstcpheader.to_bytes())
# next 32 bytes should be ams header
self.assertEqual(packet.to_bytes()[6:38], header1.to_bytes())
# last bytes should be data
self.assertEqual(packet.to_bytes()[38:44], ads_data)
# check from bytes function
self.assertEqual(
structs.AmsPacket.from_bytes(packet.to_bytes()).to_bytes(),
packet.to_bytes(),
)