diff --git a/lora_comms.py b/lora_comms.py index 86d09dc..a697c3e 100644 --- a/lora_comms.py +++ b/lora_comms.py @@ -62,11 +62,17 @@ def make_pktbuf(s): process_msgfunc_t = CFUNCTYPE(None, PktBuf, POINTER(PktBuf)) -_lib = CDLL('liblora_test.dylib') - -_lib._strobe_state_size.restype = c_size_t -_lib._strobe_state_size.argtypes = () -_strobe_state_u64_cnt = (_lib._strobe_state_size() + 7) // 8 +try: + _lib = CDLL('liblora_test.dylib') +except OSError: + _lib = None + +if _lib is not None: + _lib._strobe_state_size.restype = c_size_t + _lib._strobe_state_size.argtypes = () + _strobe_state_u64_cnt = (_lib._strobe_state_size() + 7) // 8 +else: + _strobe_state_u64_cnt = 1 class CommsSession(Structure,StructureRepr): _fields_ = [ @@ -89,22 +95,23 @@ class CommsState(Structure,StructureRepr): ('cs_prevmsgrespbuf', c_uint8 * 64), ] -_lib._comms_state_size.restype = c_size_t -_lib._comms_state_size.argtypes = () - -if _lib._comms_state_size() != sizeof(CommsState): # pragma: no cover - raise RuntimeError('CommsState structure size mismatch!') - -for func, ret, args in [ - ('comms_init', None, (POINTER(CommsState), process_msgfunc_t, - POINTER(PktBuf))), - ('comms_process', None, (POINTER(CommsState), PktBuf, POINTER(PktBuf))), - ('strobe_seed_prng', None, (POINTER(c_uint8), c_ssize_t)), - ]: - f = getattr(_lib, func) - f.restype = ret - f.argtypes = args - locals()[func] = f +if _lib is not None: + _lib._comms_state_size.restype = c_size_t + _lib._comms_state_size.argtypes = () + + if _lib._comms_state_size() != sizeof(CommsState): # pragma: no cover + raise RuntimeError('CommsState structure size mismatch!') + + for func, ret, args in [ + ('comms_init', None, (POINTER(CommsState), process_msgfunc_t, + POINTER(PktBuf))), + ('comms_process', None, (POINTER(CommsState), PktBuf, POINTER(PktBuf))), + ('strobe_seed_prng', None, (POINTER(c_uint8), c_ssize_t)), + ]: + f = getattr(_lib, func) + f.restype = ret + f.argtypes = args + locals()[func] = f def comms_process_wrap(state, input): '''A wrapper around comms_process that converts the argument