Skip to main content

Wrapper for Python 3.7+ simplifying the usage of Oliver Hartkopp's Linux kernel module enabling ISO-15765 sockets

Project description

# python-can-isotp

## What it is ##
A Python wrapper helping the use of [can-isotp Loadable Kernel Module](https://github.com/hartkopp/can-isotp) extending SocketCAN under Linux.
It provides a friendly and pythonic interface to interact with SocketCAN using the ISO-TP (ISO-15765-2) protocol.

## What it's not ##
- An implementation of the ISO-TP protocol. It simplifies the access to the already available features within Python and your operating system
- A portable module. It is designed to work under Linux only.
- A way to magically do ISO-TP regardless of your environment. You need to load a module into your Linux kernel before using this. Otherwise, your OS will deny the creation of the socket.
- A revolutionary module. It just makes the syntax easier :)

## Why is it differents from other projects ##
Other Python libraries enabling the use of ISO-TP protocol makes an implementation of the standard in Python, in the user space.
As mentioned by the authors of SocketCAN in their documentation, this approach has many downsides, mainly when comes to respecting the protocol timings.

The best way do ISO-TP communication is within the kernel space, just like [hartkopp/can-isotp](https://github.com/hartkopp/can-isotp) module does by using a socket interface following the mentality of SocketCAN. The well known duality between complexity and flexibility makes the usage of sockets onerous and non-intuitive to the uninitiated. This is where this project becomes handy, it wraps the socket object so that a programmer can configure and use it quickly, in an intuitive way.

Also, it will tells you if you do something wrong, like setting a socket options after binding the socket to the addresse. The native implementation will silently ignore the options, which can causes some headaches!

## Troubleshooting ##
- **My socket module does not include the `CAN_ISOTP` constant**

That means that your Python version does not include support for ISOTP protocol. It should be included starting from Python 3.7, under Linux build only.

- **When I create the socket, I get `OSError [errno 19] : Protocol not supported`.**

The Loadable Kernel Module is not loaded in your Linux kernel. Follow the steps given it the module repository. You needs to compile the module, install the `.ko` file and then run `modprobe can-isotp` as Super User. Then your OS will accept to create a ISO-TP sockets.

## Examples ##

### Without this project ###
```python
SOL_CAN_ISOTP = 106 # These constants exist in the module header, not in Python.
CAN_ISOTP_RECV_FC = 2
# Many more exists.

import socket
import struct

s = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_ISOTP)
s2 = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_ISOTP)
# Configuring the sockets with ugly struct.pack() that requires knowledge of the LKM structure
s.setsockopt(SOL_CAN_ISOTP, CAN_ISOTP_RECV_FC, struct.pack("=BBB", 0x10, 3,0)) #bs, stmin, wftmax
#s.setsockopt(SOL_CAN_ISOTP, CAN_ISOTP_OPTS, struct.pack(...))
#s.setsockopt(SOL_CAN_ISOTP, CAN_ISOTP_LL_OPTS, struct.pack(...))

s.bind(("vcan0", 0x123, 0x456)) #rxid, txid with confusing order.
s2.bind(("vcan0", 0x456, 0x123)) #rxid, txid
s2.send(b"Hello, this is a long payload sent in small chunks of 8 bytes.")
print(s.recv(4095))
```

### With this project ###
```python
import isotp

s = isotp.socket()
s2 = isotp.socket()
# Configuring the sockets.
s.set_fc_opts(stmin=5, bs=10)
#s.set_general_opts(...)
#s.set_ll_opts(...)

s.bind("vcan0" rxid=0x123 txid=0x456) # We love named parameters!
s2.bind("vcan0", rxid=0x456, txid=0x123)
s2.send(b"Hello, this is a long payload sent in small chunks of 8 bytes.")
print(s.recv())
```

## Don't like playing with a simili-socket ? ##

You don't want to reinvent the wheel by using a fake socket object, but still would like to simplify your work?
Say no more, you can use some helpers availables in the `isotp` module.

``` python
import isotp
import socket
s = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_ISOTP) # native socket.
isotp.opts.flowcontrol.write(s, stmin=5)
isotp.opts.general.write(optflags = isotp.opts.flags.CAN_ISOTP_TX_PADDING | isotp.opts.flags.CAN_ISOTP_RX_PADDING)
s.bind(("vcan0", 0x123, 0x456))
```

Or you can access the native socket within the wrapper

``` python
import isotp
s = isotp.socket()
s.bind("vcan0", rxid=0x123, txid=0x456)
print(s._socket.getsockname())
```


Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

can-isotp-0.2.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

can_isotp-0.2-py2.py3-none-any.whl (8.2 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file can-isotp-0.2.tar.gz.

File metadata

  • Download URL: can-isotp-0.2.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for can-isotp-0.2.tar.gz
Algorithm Hash digest
SHA256 dc49c04b100819bfa2eb973fdd9e2864b4d53032bcf975f85089a3ead05f0eae
MD5 4d262bb4050eae419c13986729f3d1f1
BLAKE2b-256 416c4f01c1c1c44b6fe1609e76d5ee11189bbbb4013f5a48009e3556d319457e

See more details on using hashes here.

File details

Details for the file can_isotp-0.2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for can_isotp-0.2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 4514eaab2866e9610b2d8ee2dfa4030ce95795edb370eb98656e5cad6b42136c
MD5 c51093ef0f67de30960b752a8b6e6aeb
BLAKE2b-256 3217412fc60848ba15ca8337d33cd2bb8ef5cdc62a1b42eae5a0d5d17763beef

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page