|
| 1 | +# Copyright (C) 2022. BMW CTW PT. All rights reserved. |
| 2 | +"""v2.18.8 specific class definitions""" |
| 3 | +import ctypes |
| 4 | +import logging |
| 5 | + |
| 6 | +from dlt.core.core_base import dltlib |
| 7 | + |
| 8 | +# DltClientMode from dlt_client.h |
| 9 | +DLT_CLIENT_MODE_UNDEFINED = -1 |
| 10 | +DLT_CLIENT_MODE_TCP = 0 |
| 11 | +DLT_CLIENT_MODE_SERIAL = 1 |
| 12 | +DLT_CLIENT_MODE_UNIX = 2 |
| 13 | +DLT_CLIENT_MODE_UDP_MULTICAST = 3 |
| 14 | + |
| 15 | +# DltReceiverType from dlt_common.h |
| 16 | +DLT_RECEIVE_SOCKET = 0 |
| 17 | +DLT_RECEIVE_UDP_SOCKET = 1 |
| 18 | +DLT_RECEIVE_FD = 2 |
| 19 | +DLT_ID_SIZE = 4 |
| 20 | +DLT_FILTER_MAX = 30 # Maximum number of filters |
| 21 | +DLT_RETURN_ERROR = -1 |
| 22 | + |
| 23 | +# Return value for DLTFilter.add() - exceeded maximum number of filters |
| 24 | +MAX_FILTER_REACHED = 1 |
| 25 | +# Return value for DLTFilter.add() - specified filter already exists |
| 26 | +REPEATED_FILTER = 2 |
| 27 | + |
| 28 | +logger = logging.getLogger(__name__) # pylint: disable=invalid-name |
| 29 | + |
| 30 | + |
| 31 | +class sockaddr_in(ctypes.Structure): # pylint: disable=invalid-name |
| 32 | + """Auxiliary definition for cDltReceiver. Defined in netinet/in.h header""" |
| 33 | + |
| 34 | + _fields_ = [ |
| 35 | + ("sa_family", ctypes.c_ushort), # sin_family |
| 36 | + ("sin_port", ctypes.c_ushort), |
| 37 | + ("sin_addr", ctypes.c_byte * 4), |
| 38 | + ("__pad", ctypes.c_byte * 8), |
| 39 | + ] # struct sockaddr_in is 16 |
| 40 | + |
| 41 | + |
| 42 | +class cDltReceiver(ctypes.Structure): # pylint: disable=invalid-name |
| 43 | + """The structure is used to organise the receiving of data including buffer handling. |
| 44 | + This structure is used by the corresponding functions. |
| 45 | +
|
| 46 | + typedef struct |
| 47 | + { |
| 48 | + int32_t lastBytesRcvd; /**< bytes received in last receive call */ |
| 49 | + int32_t bytesRcvd; /**< received bytes */ |
| 50 | + int32_t totalBytesRcvd; /**< total number of received bytes */ |
| 51 | + char *buffer; /**< pointer to receiver buffer */ |
| 52 | + char *buf; /**< pointer to position within receiver buffer */ |
| 53 | + char *backup_buf; /** pointer to the buffer with partial messages if any **/ |
| 54 | + int fd; /**< connection handle */ |
| 55 | + DltReceiverType type; /**< type of connection handle */ |
| 56 | + int32_t buffersize; /**< size of receiver buffer */ |
| 57 | + struct sockaddr_in addr; /**< socket address information */ |
| 58 | + } DltReceiver; |
| 59 | + """ |
| 60 | + |
| 61 | + _fields_ = [ |
| 62 | + ("lastBytesRcvd", ctypes.c_int32), |
| 63 | + ("bytesRcvd", ctypes.c_int32), |
| 64 | + ("totalBytesRcvd", ctypes.c_int32), |
| 65 | + ("buffer", ctypes.POINTER(ctypes.c_char)), |
| 66 | + ("buf", ctypes.POINTER(ctypes.c_char)), |
| 67 | + ("backup_buf", ctypes.POINTER(ctypes.c_char)), |
| 68 | + ("fd", ctypes.c_int), |
| 69 | + ("type", ctypes.c_int), |
| 70 | + ("buffersize", ctypes.c_int32), |
| 71 | + ("addr", sockaddr_in), |
| 72 | + ] |
| 73 | + |
| 74 | + |
| 75 | +class cDltClient(ctypes.Structure): # pylint: disable=invalid-name |
| 76 | + """ |
| 77 | + typedef struct |
| 78 | + { |
| 79 | + DltReceiver receiver; /**< receiver pointer to dlt receiver structure */ |
| 80 | + int sock; /**< sock Connection handle/socket */ |
| 81 | + char *servIP; /**< servIP IP adress/Hostname of TCP/IP interface */ |
| 82 | + char *hostip; /**< IP multicast address of group */ |
| 83 | + int port; /**< Port for TCP connections (optional) */ |
| 84 | + char *serialDevice; /**< serialDevice Devicename of serial device */ |
| 85 | + char *socketPath; /**< socketPath Unix socket path */ |
| 86 | + char ecuid[4]; /**< ECUiD */ |
| 87 | + speed_t baudrate; /**< baudrate Baudrate of serial interface, as speed_t */ |
| 88 | + DltClientMode mode; /**< mode DltClientMode */ |
| 89 | + int send_serial_header; /**< (Boolean) Send DLT messages with serial header */ |
| 90 | + int resync_serial_header; /**< (Boolean) Resync to serial header on all connection */ |
| 91 | + } DltClient; |
| 92 | + """ |
| 93 | + |
| 94 | + _fields_ = [ |
| 95 | + ("receiver", cDltReceiver), |
| 96 | + ("sock", ctypes.c_int), |
| 97 | + ("servIP", ctypes.c_char_p), |
| 98 | + ("hostip", ctypes.c_char_p), |
| 99 | + ("port", ctypes.c_int), |
| 100 | + ("serialDevice", ctypes.c_char_p), |
| 101 | + ("socketPath", ctypes.c_char_p), |
| 102 | + ("ecuid", ctypes.c_char * 4), |
| 103 | + ("baudrate", ctypes.c_uint), |
| 104 | + ("mode", ctypes.c_int), |
| 105 | + ("send_serial_header", ctypes.c_int), |
| 106 | + ("resync_serial_header", ctypes.c_int), |
| 107 | + ] |
| 108 | + |
| 109 | + |
| 110 | +class cDLTFilter(ctypes.Structure): # pylint: disable=invalid-name |
| 111 | + """ |
| 112 | + typedef struct |
| 113 | + { |
| 114 | + char apid[DLT_FILTER_MAX][DLT_ID_SIZE]; /**< application id */ |
| 115 | + char ctid[DLT_FILTER_MAX][DLT_ID_SIZE]; /**< context id */ |
| 116 | + int log_level[DLT_FILTER_MAX]; /**< log level */ |
| 117 | + int32_t payload_max[DLT_FILTER_MAX]; /**< upper border for payload */ |
| 118 | + int32_t payload_min[DLT_FILTER_MAX]; /**< lower border for payload */ |
| 119 | + int counter; /**< number of filters */ |
| 120 | + } DltFilter; |
| 121 | + """ |
| 122 | + |
| 123 | + _fields_ = [ |
| 124 | + ("apid", (ctypes.c_char * DLT_ID_SIZE) * DLT_FILTER_MAX), |
| 125 | + ("ctid", (ctypes.c_char * DLT_ID_SIZE) * DLT_FILTER_MAX), |
| 126 | + ("log_level", ctypes.c_int * DLT_FILTER_MAX), |
| 127 | + ("payload_max", (ctypes.c_int32 * DLT_FILTER_MAX)), |
| 128 | + ("payload_min", (ctypes.c_int32 * DLT_FILTER_MAX)), |
| 129 | + ("counter", ctypes.c_int), |
| 130 | + ] |
| 131 | + |
| 132 | + # pylint: disable=too-many-arguments |
| 133 | + def add(self, apid, ctid, log_level=0, payload_min=0, payload_max=ctypes.c_uint32(-1).value // 2): |
| 134 | + """Add new filter pair""" |
| 135 | + if isinstance(apid, str): |
| 136 | + apid = bytes(apid, "ascii") |
| 137 | + if isinstance(ctid, str): |
| 138 | + ctid = bytes(ctid, "ascii") |
| 139 | + if ( |
| 140 | + dltlib.dlt_filter_add( |
| 141 | + ctypes.byref(self), apid or b"", ctid or b"", log_level, payload_min, payload_max, self.verbose |
| 142 | + ) |
| 143 | + == DLT_RETURN_ERROR |
| 144 | + ): |
| 145 | + if self.counter >= DLT_FILTER_MAX: |
| 146 | + logger.error("Maximum number (%d) of allowed filters reached, ignoring filter!\n", DLT_FILTER_MAX) |
| 147 | + return MAX_FILTER_REACHED |
| 148 | + logger.debug("Filter ('%s', '%s') already exists", apid, ctid) |
| 149 | + return REPEATED_FILTER |
| 150 | + return 0 |
0 commit comments