Source code for canlib.kvrlib.dll

import ctypes as ct

from .. import dllLoader
from . import constants as const
from .structures import kvrAddress, kvrDeviceInfo, kvrVersion
from .exceptions import kvr_error


class KvrlibDll(dllLoader.MyDll):
    function_prototypes = {
        'kvrGetVersion': [[], kvrVersion, dllLoader.no_errcheck],  # No error function
        'kvrAddressFromString': [[ct.c_int32, ct.POINTER(kvrAddress), ct.c_char_p]],
        'kvrStringFromAddress': [[ct.c_char_p, ct.c_uint32, ct.POINTER(kvrAddress)]],
        'kvrDeviceGetServiceStatus': [[ct.POINTER(kvrDeviceInfo), ct.POINTER(ct.c_int32), ct.POINTER(ct.c_int32)]],
        'kvrDeviceGetServiceStatusText': [[ct.POINTER(kvrDeviceInfo), ct.c_char_p, ct.c_uint32]],
        'kvrDiscoveryClearDevicesAtExit': [[ct.c_uint]],
        'kvrDiscoveryClose': [[ct.c_int32]],
        'kvrDiscoveryOpen': [[ct.POINTER(ct.c_int32)]],
        'kvrDiscoveryGetDefaultAddresses': [[ct.POINTER(kvrAddress), ct.c_uint32, ct.POINTER(ct.c_uint32), ct.c_uint32]],
        'kvrDiscoveryGetResults': [[ct.c_int32, ct.POINTER(kvrDeviceInfo)]],
        'kvrDiscoverySetAddresses': [[ct.c_int32, ct.POINTER(kvrAddress), ct.c_uint32]],
        'kvrDiscoverySetEncryptionKey': [[ct.POINTER(kvrDeviceInfo), ct.c_char_p]],
        'kvrDiscoverySetPassword': [[ct.POINTER(kvrDeviceInfo), ct.c_char_p]],
        'kvrDiscoveryStart': [[ct.c_int32, ct.c_uint32, ct.c_uint32]],
        'kvrDiscoveryStoreDevices': [[ct.POINTER(kvrDeviceInfo), ct.c_uint32]],
        'kvrConfigActiveProfileSet': [[ct.c_int32, ct.c_int32]],
        'kvrConfigActiveProfileGet': [[ct.c_int32, ct.POINTER(ct.c_int32)]],
        'kvrConfigNoProfilesGet': [[ct.c_int32, ct.POINTER(ct.c_int32)]],
        'kvrConfigClose': [[ct.c_int32], None, dllLoader.no_errcheck],  # Returns void, never fails
        'kvrConfigOpen': [[ct.c_int32, ct.c_int32, ct.POINTER(ct.c_char), ct.POINTER(ct.c_int32)]],
        'kvrConfigClear': [[ct.c_int32]],
        'kvrConfigOpenEx': [[ct.c_int32, ct.c_int32, ct.POINTER(ct.c_char), ct.POINTER(ct.c_int32), ct.c_uint32]],
        'kvrConfigSet': [[ct.c_int32, ct.c_char_p]],
        'kvrConfigGet': [[ct.c_int32, ct.c_char_p, ct.c_uint32]],
        'kvrGetErrorText': [[ct.c_int32, ct.c_char_p, ct.c_uint32]],
        'kvrInitializeLibrary': [[], None, dllLoader.no_errcheck],  # Returns void, no errcheck function
        'kvrHostName': [[ct.c_uint32, ct.c_uint32, ct.c_uint32, ct.c_char_p, ct.c_uint32]],
        'kvrNetworkConnectionTest': [[ct.c_int32, ct.c_int32]],
        'kvrUnloadLibrary': [[], None, dllLoader.no_errcheck],
        'kvrServiceQuery': [[ct.POINTER(ct.c_int)]],
        'kvrServiceStart': [[ct.POINTER(ct.c_int)]],
        'kvrServiceStop': [[ct.POINTER(ct.c_int)]],
    }

    def __init__(self, ct_dll):
        # set default values for function_prototypes
        self.default_restype = ct.c_int
        self.default_errcheck = self._error_check
        super(KvrlibDll, self).__init__(ct_dll, **self.function_prototypes)

    def _error_check(self, result, func, arguments):
        if result < 0:
            raise kvr_error(result)
        else:
            return result