Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encode issue #34

Open
gsmgxm opened this issue Apr 27, 2021 · 2 comments
Open

encode issue #34

gsmgxm opened this issue Apr 27, 2021 · 2 comments

Comments

@gsmgxm
Copy link

gsmgxm commented Apr 27, 2021

We met a issue, when use python version:

payload whished:
.... 0x20 0x00 0x00 0x00 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x80 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

result:
.... 0x20 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0xff 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x01 0x00 0x00 0x00 0x80 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00

Here is our code:
class SApiBbbSendVsbDataReq(prophy.with_metaclass(prophy.struct_generator, prophy.struct)):
_descriptor = [
('transactionId', prophy.u32),
('portId', EBbbPortId),
('vsbTxRegionId', prophy.u32),
('transmissionEnabled', prophy.u32),
('dataLength', prophy.u32),
('data', prophy.array(prophy.u64, size=32)),
]

msg_type = 'SApiBbbSendVsbDataReq'
msg_name = 'API_BBB_SEND_VSB_DATA_REQ_MSG'
hwapi_msg = fsp.syscom.get_hwapi_message(msg_type=msg_type, msg_name=msg_name)
msg_obj = hwapi_msg.get_msg_obj()
msg_obj.transactionId = 0x0000001b
msg_obj.portId = get_EBbbPortId(cell.cpri_link[0])
msg_obj.vsbTxRegionId = 0x00000000
msg_obj.transmissionEnabled = 0x00000001
msg_obj.dataLength = 0x00000020
msg_obj.data[0] = 0xffffffffffffffff
msg_obj.data[1] = 0x0000000000000000
msg_obj.data[2] = 0x0000000800000001

We find between datalength and data there is extra “00 00 00 00”.
I trace the code, found in site-packages\prophy\composite.py
def encode(self, endianness):
data = b""

for field in self._descriptor:
    data += (self._get_padding(len(data), field.type._ALIGNMENT))
    data += field.encode_fcn(self, field.type, getattr(self, field.name, None), endianness)

    if field.type._PARTIAL_ALIGNMENT:
        data += self._get_padding(len(data), field.type._PARTIAL_ALIGNMENT)

data += self._get_padding(len(data), self._ALIGNMENT)

return data

**data += (self._get_padding(len(data), field.type._ALIGNMENT))

len(data) = 20, field.type._ALIGNMENT = 8 ( next data is array u64)**

Why add padding after the previous data(or say 'before the next data') based on next data’s field.type._ALIGNMENT ?

This is why “00000000” was added in payload between “datalength” and “data”.

Is this the right behavior?

Can you help us check it?

@kamichal
Copy link
Collaborator

kamichal commented Apr 27, 2021

Why add padding after the previous data(or say 'before the next data') based on next data’s field.type._ALIGNMENT ?

This is how C++ structures work. It's natural. Pointers to int64_t have to be aligned to 8 bytes. If you need "pack" feature, check out prophy.struct_packed:

https://prophy.readthedocs.io/en/latest/python_codec.html?highlight=packed#packed-mode

You may also refer to test for example of usage.
https://github.com/aurzenligl/prophy/blob/master/prophy/tests/test_array_ext_sized.py

@gsmgxm
Copy link
Author

gsmgxm commented May 12, 2021

thank you very much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants