All of lore.kernel.org
 help / color / mirror / Atom feed
From: Patrick Menschel <menschel.p@posteo.de>
To: Kurt Van Dijck <dev.kurt@vandijck-laurijssen.be>
Cc: linux-can <linux-can@vger.kernel.org>
Subject: Re: J1939 Questions on Intended usage
Date: Sat, 15 May 2021 18:10:20 +0000	[thread overview]
Message-ID: <72aa8b79-5ba9-26ee-3918-09532e0f1eae@posteo.de> (raw)
In-Reply-To: <38c85980-a569-b714-2643-9623b3dbc973@posteo.de>

Am 15.05.21 um 16:01 schrieb Patrick Menschel:
> Am 15.05.21 um 14:26 schrieb Kurt Van Dijck:
>> On Fri, 14 May 2021 12:04:47 +0000, Patrick Menschel wrote:
>>> Do I need to open one socket per PGN I'm sending?
>>> e.g.
>>>
>>> s1 = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_J1939)
>>> s1.bind(interface_name, MY_NAME, PGN_OF_TSC1, MY_SA)
>>> s1.write(bytes(8))
>>>
>>> s2 = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_J1939)
>>> s2.bind(interface_name, MY_NAME, PGN_OF_EBC1, MY_SA)
>>> s2.write(bytes(8))
>>
>> No, you don't _need_ to. You can.
>>
>> If you need quite some different PGN's, it may be more interesting to:
>> s = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_J1939)
>> s.bind(interface_name, MY_NAME, ANY_PGN, MY_SA)
>> s.sendto(bytes(8), DST_1, PGN_1)
>> s.sendto(bytes(8), DST_2, PGN_2)
>> ...
>>
>> I'm not a python expert, I just assume something like that is possible.
> 
> Yes, the method exists
> 
> sendto()
> https://docs.python.org/3/library/socket.html#socket.socket.sendto
> https://github.com/python/cpython/blob/main/Modules/socketmodule.c#L4279
> 
> but apparently sockaddr_can is not yet expanded to individual parameters
> as it was done with the
> 
> bind()
> https://github.com/python/cpython/blob/main/Modules/socketmodule.c#L2207
> 
> Then I'll start by passing in the sockaddr_can struct as a first test
> and make a PR to that repo in the long run.

Guess I have to amend that impression, everything works fine,
except for broadcast which gives me a PermissionError ?!

python -i
Python 3.9.5 (default, May 13 2021, 13:29:45)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket
>>> s = socket.socket(socket.AF_CAN, socket.SOCK_DGRAM, socket.CAN_J1939)
>>> s.bind(("mcp0", 0, 0x40000, 0x20))
>>> s.sendto(bytes(range(8)), ("", 0, 0x12300, 0x30))
8
>>> s.sendto(bytes(range(0,0x88,0x11)), ("", 0, 0x12300, 0x30))
8
>>>

Took me some try and error to get rid of

OSError: [Errno 77] File descriptor in bad state

but at least something comes out.

candump mcp0
  mcp0  19233020   [8]  00 01 02 03 04 05 06 07
  mcp0  19233020   [8]  00 11 22 33 44 55 66 77

Maybe I didn't get the concept at all.



The transport protocol also does something.

s.sendto(bytes(range(64)), ("mcp0", 0, 0xFECA, 0x20))


TP.CM.RTS
  mcp0  18EC2020   [8]  10 40 00 0A 0A CA FE 00
TP.CM.CTS
  mcp0  18EC2020   [8]  11 0A 01 FF FF CA FE 00

TP.DT
  mcp0  18EB2020   [8]  01 00 01 02 03 04 05 06
  mcp0  18EB2020   [8]  02 07 08 09 0A 0B 0C 0D
  mcp0  18EB2020   [8]  03 0E 0F 10 11 12 13 14
  mcp0  18EB2020   [8]  04 15 16 17 18 19 1A 1B
  mcp0  18EB2020   [8]  05 1C 1D 1E 1F 20 21 22
  mcp0  18EB2020   [8]  06 23 24 25 26 27 28 29
  mcp0  18EB2020   [8]  07 2A 2B 2C 2D 2E 2F 30
  mcp0  18EB2020   [8]  08 31 32 33 34 35 36 37
  mcp0  18EB2020   [8]  09 38 39 3A 3B 3C 3D 3E
  mcp0  18EB2020   [8]  0A 3F FF FF FF FF FF FF

TP.CM.EndMsgAck
  mcp0  18EC2020   [8]  13 40 00 0A FF CA FE 00

Those with a little knowledge of J1939 will now roll on the floor ;-)


The only thing that I didn't get to work is send to broadcast.
That PermissionError is somewhat strange.

>>> s.sendto(bytes(range(8)), ("", 0, 0xFECA, 0xFF))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied

>>> s.sendto(bytes(range(64)), ("mcp0", 0, 0xFECA, 0xFF))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied


Best Regards,
Patrick

  reply	other threads:[~2021-05-15 18:10 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-14 12:04 J1939 Questions on Intended usage Patrick Menschel
2021-05-15 12:26 ` Kurt Van Dijck
2021-05-15 14:01   ` Patrick Menschel
2021-05-15 18:10     ` Patrick Menschel [this message]
2021-05-15 18:41       ` Marc Kleine-Budde
2021-05-15 18:42       ` Kurt Van Dijck
2021-05-15 19:00         ` Patrick Menschel
2021-05-15 19:07           ` Kurt Van Dijck
2021-05-15 19:17           ` Marc Kleine-Budde

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=72aa8b79-5ba9-26ee-3918-09532e0f1eae@posteo.de \
    --to=menschel.p@posteo.de \
    --cc=dev.kurt@vandijck-laurijssen.be \
    --cc=linux-can@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.