pycrate icon indicating copy to clipboard operation
pycrate copied to clipboard

OER decoding error

Open ttt710516 opened this issue 9 months ago • 6 comments

Hi, ==== The follows are my source code. ==== import time from tci import TCI_Dispatcher, TCI_wsm, TCI_16093_DSRC asn1_initialize = TCI_Dispatcher.TCIMsg

message = { 'version': 3, 'time': int(time.time()), 'frame': ( 'sutCtrl', ( 'request', { 'messageId': 1, 'value': (('TCI-wsm', 'SetInitialState'), True) } ) ) }

asn1_initialize.set_val(message) print(asn1_initialize.to_asn1()) print(asn1_initialize.to_oer().hex())

asn1_initialize.from_oer(asn1_initialize.to_oer())

==== The follows are the result. ==== ➜ tools git:(master) ✗ python3 ex_initialize_gen.py { version 3 -- currentVersion --, time 1742110176, frame sutCtrl : request : { messageId 1, value TCI-wsm.SetInitialState: TRUE } } 00030000000067d67de08680000101ff Traceback (most recent call last): File "ex_initialize_gen.py", line 28, in asn1_initialize.from_oer(asn1_initialize.to_oer()) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 2058, in from_oer self._from_oer(char) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 1623, in _from_oer Comp._from_oer(char) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 670, in _from_oer Cho.from_oer(char) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 2058, in from_oer self._from_oer(char) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 670, in _from_oer Cho.from_oer(char) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 2061, in from_oer self._safechk_bnd(self._val) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 774, in _safechk_bnd Obj._safechk_bnd(Obj._val) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_ext.py", line 186, in _safechk_bnd self._get_val_obj(val[0])._safechk_bnd(val[1]) File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 340, in _safechk_bnd raise(ASN1ObjErr('{0}: {1} value out of constraint, {2!r}'
pycrate_asn1rt.err.ASN1ObjErr: Type: BOOLEAN value out of constraint, None

I tried ber and uper, and they work. So not sure what happened on OER decoding? I didn't put the auto-generated code 'tci.asn'. The file size is large and may not be the root cause.

Sincerely, Kang

ttt710516 avatar Mar 16 '25 07:03 ttt710516

Let me reformat this content again. The follows are my source code.

import time
from tci import TCI_Dispatcher, TCI_wsm, TCI_16093_DSRC
asn1_initialize = TCI_Dispatcher.TCIMsg
#print(asn1_initialize.get_proto())

#message = {'version': 3, 'time': 1742107319736, 'frame': ('sutCtrl', ('request', {'messageId': 12, 'value': ('SetHeading', 1)}))}

message = {
    'version': 3,
    'time': int(time.time()),
    'frame': (
        'sutCtrl', 
        (
            'request', 
            {
                'messageId': 1, 
                'value': (('TCI-wsm', 'SetInitialState'), True)
            }
        )
    )
}

#
asn1_initialize.set_val(message)
print(asn1_initialize.to_asn1())
print(asn1_initialize.to_oer().hex())

asn1_initialize.from_oer(asn1_initialize.to_oer())

The follows are the result.

➜  tools git:(master) ✗ python3 ex_initialize_gen.py
{
  version 3 -- currentVersion --,
  time 1742110176,
  frame sutCtrl : request : {
    messageId 1,
    value TCI-wsm.SetInitialState: TRUE
  }
}
00030000000067d67de08680000101ff
Traceback (most recent call last):
  File "ex_initialize_gen.py", line 28, in <module>
    asn1_initialize.from_oer(asn1_initialize.to_oer())
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 2058, in from_oer
    self._from_oer(char)
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 1623, in _from_oer
    Comp._from_oer(char)
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 670, in _from_oer
    Cho.from_oer(char)
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 2058, in from_oer
    self._from_oer(char)
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 670, in _from_oer
    Cho.from_oer(char)
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 2061, in from_oer
    self._safechk_bnd(self._val)
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_construct.py", line 774, in _safechk_bnd
    Obj._safechk_bnd(Obj._val)
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj_ext.py", line 186, in _safechk_bnd
    self._get_val_obj(val[0])._safechk_bnd(val[1])
  File "/home/ttt12345/.local/lib/python3.8/site-packages/pycrate_asn1rt/asnobj.py", line 340, in _safechk_bnd
    raise(ASN1ObjErr('{0}: {1} value out of constraint, {2!r}'\
pycrate_asn1rt.err.ASN1ObjErr: Type: BOOLEAN value out of constraint, None

I tried ber and uper codec, and they work. So not sure what happened on OER decoding? I didn't put the auto-generated code 'tci.asn'. The file size is large and may not be the root cause.

ttt710516 avatar Mar 16 '25 07:03 ttt710516

i tried other cobinations. It seems the root cuase is OER decoding (encoding OK) can't handle open type correctly.

Sincerely, Kang

ttt710516 avatar Mar 16 '25 08:03 ttt710516

Can you please indicate where the object TCI_Dispatcher.TCIMsg comes from? I don't think the module tci comes from pycrate. This is needed for me to reproduce the issue. Thx

mitshell avatar Mar 16 '25 19:03 mitshell

Hi, I attached the auto-generated code. But I guess the following code of asnobj_ext.py in your project has some issue. val should be alwayws None because Obj never return anything.

tci.py.txt

    def _from_oer(self, char):
        # try to get a defined object from a table constraint
        if self._TAB_LUT and self._const_tab and self._const_tab_at:
            const_obj_type, const_obj = self._get_tab_obj()
            if const_obj_type == CLASET_NONE:
                if not self._SILENT:
                    asnlog('OPEN._from_oer: %s, unable to retrieve a table-looked up object' \
                           % (self.fullname()))
                Obj = None
            elif const_obj_type == CLASET_UNIQ:
                Obj = const_obj
            else:
                # const_obj_type == CLASET_MULT
                # with PER, no tag to select a given object
                Obj = const_obj[0]
        else:
            # TODO: another way to provide a (set of) potential defined object(s)
            # is to look into value constraint self._const_val
            # if we have multiple, then we would have to bruteforce the decoding
            # until a correct one is found !!!
            Obj = None
        #

        val_bytes = ASN1CodecOER.decode_open_type(char)
        # Kang; from_oer never return val
        val = Obj.from_oer(val_bytes) if (Obj is not None) else val_bytes

ttt710516 avatar Mar 17 '25 02:03 ttt710516

OK, thanks for spotting this issue. Would you have a suggestion for a fix?

mitshell avatar Mar 30 '25 20:03 mitshell

I implemented a fix: https://github.com/pycrate-org/pycrate/commit/91b7713c7a53a9b3c1ee2cf58dc5fc79a066a205. Can you pull it and have a look to ensure your issue is adressed @ttt710516 ?

mitshell avatar Apr 28 '25 20:04 mitshell