Move wrapped C library to subpackage
[ric-plt/xapp-frame-py.git] / ricxappframe / rmr / rmrclib / rmrclib.py
1 # ==================================================================================
2 #       Copyright (c) 2019-2020 Nokia
3 #       Copyright (c) 2018-2020 AT&T Intellectual Property.
4 #
5 #   Licensed under the Apache License, Version 2.0 (the "License");
6 #   you may not use this file except in compliance with the License.
7 #   You may obtain a copy of the License at
8 #
9 #          http://www.apache.org/licenses/LICENSE-2.0
10 #
11 #   Unless required by applicable law or agreed to in writing, software
12 #   distributed under the License is distributed on an "AS IS" BASIS,
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #   See the License for the specific language governing permissions and
15 #   limitations under the License.
16 # ==================================================================================
17 import ctypes
18 import json
19
20 # Subpackage that creates and publishes a reference to the C shared library.
21 # Intended to be private; RMR-python and xapp-frame-py users should not need this.
22 # Sphinx and autodoc mock this module to run without the .so file present.
23
24 # https://docs.python.org/3.7/library/ctypes.html
25 # https://stackoverflow.com/questions/2327344/ctypes-loading-a-c-shared-library-that-has-dependencies/30845750#30845750
26 # make sure you do a set -x LD_LIBRARY_PATH /usr/local/lib/;
27 rmr_c_lib = ctypes.CDLL("librmr_si.so", mode=ctypes.RTLD_GLOBAL)
28 _rmr_get_consts = rmr_c_lib.rmr_get_consts
29 _rmr_get_consts.argtypes = []
30 _rmr_get_consts.restype = ctypes.c_char_p
31
32
33 def get_constants(cache={}):
34     """
35     Gets constants published by RMR and caches for subsequent calls.
36     TODO: are there constants that end user applications need?
37     """
38     if cache:
39         return cache
40
41     js = _rmr_get_consts()  # read json string
42     cache = json.loads(str(js.decode()))  # create constants value object as a hash
43     return cache
44
45
46 def get_mapping_dict(cache={}):
47     """
48     Builds a state mapping dict from constants and caches for subsequent calls.
49     Relevant constants at this writing include:
50
51     RMR_OK              0   state is good
52     RMR_ERR_BADARG      1   argument passd to function was unusable
53     RMR_ERR_NOENDPT     2   send/call could not find an endpoint based on msg type
54     RMR_ERR_EMPTY       3   msg received had no payload; attempt to send an empty message
55     RMR_ERR_NOHDR       4   message didn't contain a valid header
56     RMR_ERR_SENDFAILED  5   send failed; errno has nano reason
57     RMR_ERR_CALLFAILED  6   unable to send call() message
58     RMR_ERR_NOWHOPEN    7   no wormholes are open
59     RMR_ERR_WHID        8   wormhole id was invalid
60     RMR_ERR_OVERFLOW    9   operation would have busted through a buffer/field size
61     RMR_ERR_RETRY       10  request (send/call/rts) failed, but caller should retry (EAGAIN for wrappers)
62     RMR_ERR_RCVFAILED   11  receive failed (hard error)
63     RMR_ERR_TIMEOUT     12  message processing call timed out
64     RMR_ERR_UNSET       13  the message hasn't been populated with a transport buffer
65     RMR_ERR_TRUNC       14  received message likely truncated
66     RMR_ERR_INITFAILED  15  initialization of something (probably message) failed
67
68     """
69     if cache:
70         return cache
71
72     rmr_consts = get_constants()
73     for key in rmr_consts:  # build the state mapping dict
74         if key[:7] in ["RMR_ERR", "RMR_OK"]:
75             en = int(rmr_consts[key])
76             cache[en] = key
77
78     return cache
79
80
81 def state_to_status(stateno):
82     """
83     Converts a msg state integer to a status string.
84
85     Returns "UNKNOWN STATE" if the int value is not known.
86     """
87     sdict = get_mapping_dict()
88     return sdict.get(stateno, "UNKNOWN STATE")