All of lore.kernel.org
 help / color / mirror / Atom feed
* J1939 Questions on Intended usage
@ 2021-05-14 12:04 Patrick Menschel
  2021-05-15 12:26 ` Kurt Van Dijck
  0 siblings, 1 reply; 9+ messages in thread
From: Patrick Menschel @ 2021-05-14 12:04 UTC (permalink / raw)
  To: dev.kurt, linux-can

Hi Kurt,

J1939 just hit the raspberrypi-kernel-headers and will soon be part of
regular raspberrypi-kernel [1] while it was already
available in Python 3.9 for a couple of month. [2]

I was about to give it a spin but was confused of the call parameters.

Could you shed some light on the intended usage.

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))


What about the cyclic transmitted PGNs? Do I drop those into
BroadcastManager somehow?


If I want to open an ISOTP Channel while a j1939 socket exists for my
SA, does anything weird happen on that socket?

e.g. I open a KWP2000 session from tester to engine ecu:

Tester 0xF1 <--> ECU 0x00
0x18DA00F1  >>
            << 0x18DAF100

Thanks and Best Regards,
Patrick Menschel



[1] https://github.com/raspberrypi/linux/pull/4346
[2] https://docs.python.org/3.9/library/socket.html#socket.socket.bind
> CAN_J1939 protocol require a tuple (interface, name, pgn, addr) where additional
> parameters are 64-bit unsigned integer representing the ECU name, a32-bit unsigned
> integer representing the Parameter Group Number (PGN), and an 8-bit integer
> representing the address.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: J1939 Questions on Intended usage
  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
  0 siblings, 1 reply; 9+ messages in thread
From: Kurt Van Dijck @ 2021-05-15 12:26 UTC (permalink / raw)
  To: Patrick Menschel; +Cc: linux-can

On Fri, 14 May 2021 12:04:47 +0000, Patrick Menschel wrote:
> Hi Kurt,
> 
> J1939 just hit the raspberrypi-kernel-headers and will soon be part of
> regular raspberrypi-kernel [1] while it was already
> available in Python 3.9 for a couple of month. [2]
> 
> I was about to give it a spin but was confused of the call parameters.
> 
> Could you shed some light on the intended usage.
> 
> 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.

> 
> 
> What about the cyclic transmitted PGNs? Do I drop those into
> BroadcastManager somehow?

The broadcast manager is seperate from j1939, so it's apart.
> 
> 
> If I want to open an ISOTP Channel while a j1939 socket exists for my
> SA, does anything weird happen on that socket?

No, nothing weird will happen.

The only possible disadvantage I can think of is that the messages sent
using ISOTP do not honor the NAME-SA mapping, so on a bus with dynamic
addressing, you should be carefull to use local/remote addresses.

Kind regards,
Kurt

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: J1939 Questions on Intended usage
  2021-05-15 12:26 ` Kurt Van Dijck
@ 2021-05-15 14:01   ` Patrick Menschel
  2021-05-15 18:10     ` Patrick Menschel
  0 siblings, 1 reply; 9+ messages in thread
From: Patrick Menschel @ 2021-05-15 14:01 UTC (permalink / raw)
  To: Kurt Van Dijck; +Cc: linux-can

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.

>> What about the cyclic transmitted PGNs? Do I drop those into
>> BroadcastManager somehow?
> 
> The broadcast manager is seperate from j1939, so it's apart.

Now that would have been the cherry on the cake  ;-)
99% of J1939 are information sharing, cyclic messages of PDU2 format.
Handing the timing over to the BCM would have been super convenient.

Thanks again and Best Regards,
Patrick

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: J1939 Questions on Intended usage
  2021-05-15 14:01   ` Patrick Menschel
@ 2021-05-15 18:10     ` Patrick Menschel
  2021-05-15 18:41       ` Marc Kleine-Budde
  2021-05-15 18:42       ` Kurt Van Dijck
  0 siblings, 2 replies; 9+ messages in thread
From: Patrick Menschel @ 2021-05-15 18:10 UTC (permalink / raw)
  To: Kurt Van Dijck; +Cc: linux-can

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

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: J1939 Questions on Intended usage
  2021-05-15 18:10     ` Patrick Menschel
@ 2021-05-15 18:41       ` Marc Kleine-Budde
  2021-05-15 18:42       ` Kurt Van Dijck
  1 sibling, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2021-05-15 18:41 UTC (permalink / raw)
  To: Patrick Menschel; +Cc: Kurt Van Dijck, linux-can

[-- Attachment #1: Type: text/plain, Size: 652 bytes --]

On 15.05.2021 18:10:20, Patrick Menschel wrote:
> The only thing that I didn't get to work is send to broadcast.
> That PermissionError is somewhat strange.

Does this from Documentation/networking/j1939.rst help?

| By default no broadcast packets can be send or received. To enable sending or
| receiving broadcast packets use the socket option ``SO_BROADCAST``:

Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: J1939 Questions on Intended usage
  2021-05-15 18:10     ` Patrick Menschel
  2021-05-15 18:41       ` Marc Kleine-Budde
@ 2021-05-15 18:42       ` Kurt Van Dijck
  2021-05-15 19:00         ` Patrick Menschel
  1 sibling, 1 reply; 9+ messages in thread
From: Kurt Van Dijck @ 2021-05-15 18:42 UTC (permalink / raw)
  To: Patrick Menschel; +Cc: linux-can

On Sat, 15 May 2021 18:10:20 +0000, Patrick Menschel wrote:
> 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.
> >>>
> Guess I have to amend that impression, everything works fine,
> except for broadcast which gives me a PermissionError ?!

Will you not forget to set the SO_BROADCAST socket option ( see: man 7 socket )

Kurt

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: J1939 Questions on Intended usage
  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
  0 siblings, 2 replies; 9+ messages in thread
From: Patrick Menschel @ 2021-05-15 19:00 UTC (permalink / raw)
  To: Marc Kleine-Budde, Kurt Van Dijck; +Cc: linux-can

Am 15.05.21 um 20:42 schrieb Kurt Van Dijck:
> On Sat, 15 May 2021 18:10:20 +0000, Patrick Menschel wrote:
>> 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.
>>>>>
>> Guess I have to amend that impression, everything works fine,
>> except for broadcast which gives me a PermissionError ?!
> 
> Will you not forget to set the SO_BROADCAST socket option ( see: man 7 socket )
> 
Thank you both,

that was the trick. But why does it throw a PermissionError ?

I would expect a ValueError, e.g. -EINVAL because I as a User did the
wrong input or rather did not set a socket option.
Alternatively -EPROTO.

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.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, True)
>>> s.bind(("mcp0", 0, 0x40000, 0x20))
>>> s.sendto(bytes(range(8)), ("mcp0", 0, 0xFECA, 0xFF))
8
>>> s.sendto(bytes(range(64)), ("mcp0", 0, 0xFECA, 0xFF))
64
>>>

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


Best Regards,
Patrick

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: J1939 Questions on Intended usage
  2021-05-15 19:00         ` Patrick Menschel
@ 2021-05-15 19:07           ` Kurt Van Dijck
  2021-05-15 19:17           ` Marc Kleine-Budde
  1 sibling, 0 replies; 9+ messages in thread
From: Kurt Van Dijck @ 2021-05-15 19:07 UTC (permalink / raw)
  To: Patrick Menschel; +Cc: Marc Kleine-Budde, linux-can

On Sat, 15 May 2021 19:00:43 +0000, Patrick Menschel wrote:
> Am 15.05.21 um 20:42 schrieb Kurt Van Dijck:
> > On Sat, 15 May 2021 18:10:20 +0000, Patrick Menschel wrote:
> >> 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.
> >>>>>
> >> Guess I have to amend that impression, everything works fine,
> >> except for broadcast which gives me a PermissionError ?!
> > 
> > Will you not forget to set the SO_BROADCAST socket option ( see: man 7 socket )
> > 
> Thank you both,
> 
> that was the trick. But why does it throw a PermissionError ?
> 
> I would expect a ValueError, e.g. -EINVAL because I as a User did the
> wrong input or rather did not set a socket option.
> Alternatively -EPROTO.
> 

Looking at it now, I have no good answer.

Kurt


^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: J1939 Questions on Intended usage
  2021-05-15 19:00         ` Patrick Menschel
  2021-05-15 19:07           ` Kurt Van Dijck
@ 2021-05-15 19:17           ` Marc Kleine-Budde
  1 sibling, 0 replies; 9+ messages in thread
From: Marc Kleine-Budde @ 2021-05-15 19:17 UTC (permalink / raw)
  To: Patrick Menschel; +Cc: Kurt Van Dijck, linux-can

[-- Attachment #1: Type: text/plain, Size: 485 bytes --]

On 15.05.2021 19:00:43, Patrick Menschel wrote:
> that was the trick. But why does it throw a PermissionError ?

So does ipv4:

https://elixir.bootlin.com/linux/v5.12.4/source/net/ipv4/datagram.c#L61

Marc

-- 
Pengutronix e.K.                 | Marc Kleine-Budde           |
Embedded Linux                   | https://www.pengutronix.de  |
Vertretung West/Dortmund         | Phone: +49-231-2826-924     |
Amtsgericht Hildesheim, HRA 2686 | Fax:   +49-5121-206917-5555 |

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-05-15 19:17 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

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.