X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=src%2Fbindings%2Frmr-python%2Frmr%2Frmr.py;h=8c9ff5a4601b4413cf4cbfd1c15ade21e771f108;hb=refs%2Fchanges%2F53%2F1053%2F1;hp=984ac3107f8851b420714a61222c235086691717;hpb=91aa7028e6bb9e16c16d119cec608c8395e8e322;p=ric-plt%2Flib%2Frmr.git diff --git a/src/bindings/rmr-python/rmr/rmr.py b/src/bindings/rmr-python/rmr/rmr.py index 984ac31..8c9ff5a 100644 --- a/src/bindings/rmr-python/rmr/rmr.py +++ b/src/bindings/rmr-python/rmr/rmr.py @@ -69,7 +69,7 @@ def _get_mapping_dict(cache={}): RMR_ERR_TIMEOUT 12 message processing call timed out RMR_ERR_UNSET 13 the message hasn't been populated with a transport buffer RMR_ERR_TRUNC 14 received message likely truncated - RMR_ERR_INITFAILED 15 initialisation of something (probably message) failed + RMR_ERR_INITFAILED 15 initialization of something (probably message) failed """ if cache: @@ -93,6 +93,9 @@ def _state_to_status(stateno): return sdict.get(stateno, "UNKNOWN STATE") +_RCONST = _get_constants() + + ############## # PUBLIC API ############## @@ -100,7 +103,13 @@ def _state_to_status(stateno): # These constants are directly usable by importers of this library # TODO: Are there others that will be useful? -RMR_MAX_RCV_BYTES = _get_constants()["RMR_MAX_RCV_BYTES"] + +RMR_MAX_RCV_BYTES = _RCONST["RMR_MAX_RCV_BYTES"] +RMRFL_MTCALL = _RCONST.get("RMRFL_MTCALL", 0x02) # initialization flags +RMRFL_NONE = _RCONST.get("RMRFL_NONE", 0x0) +RMR_OK = _RCONST["RMR_OK"] # useful state constants +RMR_ERR_TIMEOUT = _RCONST["RMR_ERR_TIMEOUT"] +RMR_ERR_RETRY = _RCONST["RMR_ERR_RETRY"] class rmr_mbuf_t(Structure): @@ -210,7 +219,21 @@ def rmr_alloc_msg(vctx, size): Refer to the rmr C documentation for rmr_alloc_msg extern rmr_mbuf_t* rmr_alloc_msg(void* vctx, int size) """ - return rmr_alloc_msg(vctx, size) + return _rmr_alloc_msg(vctx, size) + + +_rmr_free_msg = rmr_c_lib.rmr_free_msg +_rmr_free_msg.argtypes = [c_void_p] +_rmr_free_msg.restype = None + + +def rmr_free_msg(mbuf): + """ + Refer to the rmr C documentation for rmr_free_msg + extern void rmr_free_msg( rmr_mbuf_t* mbuf ) + """ + if mbuf is not None: + _rmr_free_msg(mbuf) _rmr_payload_size = rmr_c_lib.rmr_payload_size @@ -309,22 +332,39 @@ def rmr_bytes2meid(ptr_mbuf, src, length): return _rmr_bytes2meid(ptr_mbuf, src, length) +# this is an alias to rmr_bytes2meid using familiar set/get terminoloigy +rmr_set_meid = rmr_bytes2meid + # CAUTION: Some of the C functions expect a mutable buffer to copy the bytes into; # if there is a get_* function below, use it to set up and return the # buffer properly. - +# extern unsigned char* rmr_get_meid(rmr_mbuf_t* mbuf, unsigned char* dest); +# we don't provide direct access to this function (unless it is asked for) because it is not really useful to provide your own buffer. +# Rather, rmr_get_meid does this for you, and just returns the string. _rmr_get_meid = rmr_c_lib.rmr_get_meid _rmr_get_meid.argtypes = [POINTER(rmr_mbuf_t), c_char_p] _rmr_get_meid.restype = c_char_p -def rmr_get_meid(ptr_mbuf, dest): +def rmr_get_meid(ptr_mbuf): """ - Refer to the rmr C documentation for rmr_get_meid - extern unsigned char* rmr_get_meid(rmr_mbuf_t* mbuf, unsigned char* dest); + Get the managed equipment ID (meid) from the message header. + + Parameters + ---------- + ptr_mbuf: ctypes c_void_p + Pointer to an rmr message buffer + + Returns + ------- + string: + meid """ - return _rmr_get_meid(ptr_mbuf, dest) + sz = _get_constants().get("RMR_MAX_MEID", 64) # size for buffer to fill + buf = create_string_buffer(sz) + _rmr_get_meid(ptr_mbuf, buf) + return buf.value.decode() # decode turns into a string _rmr_get_src = rmr_c_lib.rmr_get_src @@ -400,10 +440,6 @@ def message_summary(ptr_mbuf): if ptr_mbuf.contents.len > RMR_MAX_RCV_BYTES: return "Malformed message: message length is greater than the maximum possible" - meid = get_meid(ptr_mbuf) - if meid == "\000" * _get_constants().get("RMR_MAX_MEID", 32): # special case all nils - meid = None - return { "payload": get_payload(ptr_mbuf), "payload length": ptr_mbuf.contents.len, @@ -413,7 +449,7 @@ def message_summary(ptr_mbuf): "message state": ptr_mbuf.contents.state, "message status": _state_to_status(ptr_mbuf.contents.state), "payload max size": rmr_payload_size(ptr_mbuf), - "meid": meid, + "meid": rmr_get_meid(ptr_mbuf), "message source": get_src(ptr_mbuf), "errno": ptr_mbuf.contents.tp_state, } @@ -450,27 +486,6 @@ def generate_and_set_transaction_id(ptr_mbuf): memmove(ptr_mbuf.contents.xaction, uu_id, sz) -def get_meid(ptr_mbuf): - """ - | Get the managed equipment ID (meid) from the message header. - | This is a 32 byte field and RMr returns all 32 bytes which if the sender did not set will be garbage. - - Parameters - ---------- - ptr_mbuf: ctypes c_void_p - Pointer to an rmr message buffer - - Returns - ------- - string: - meid - """ - sz = _get_constants().get("RMR_MAX_MEID", 64) # size for buffer to fill - buf = create_string_buffer(sz) - rmr_get_meid(ptr_mbuf, buf) - return buf.raw.decode() - - def get_src(ptr_mbuf): """ Get the message source (likely host:port) @@ -483,7 +498,7 @@ def get_src(ptr_mbuf): Returns ------- string: - meid + message source """ sz = _get_constants().get("RMR_MAX_SRC", 64) # size to fill buf = create_string_buffer(sz)