What I provided *writes* to the CANbus. When I say "wrapper" I don't mean shelling or piping out from perl but calling the can-util functions directly.

If you are familiar with C, have a look at cansend.c (of the distribution I posted) and particularly the cansend(...) function. You will see there how to open the socketCAN and write to it. In candump.c, of can-utils distribution, you will see how they open the socketCAN for reading and read from it (and print to stdout).

If you are familiar with C, again, I suggest that you build up a small library, following can-utils's code, where you provide funcionality like socketCAN_open(interface), socketCAN_close(socket), socketCAN_read(socket), socketCAN_write(socker, data). Then you proceed implementing your sniffer in C utilising these functions.

FInally, write the XS wrapper to call your function. But here you have a major problem: you need to poll the CANbus at regular intervals and when it has data you need to push that back to Perl somehow. One way to do this is via a user-specified Perl callback which is called whenever the bus has data. That's possible and in Net::Ncap you can see how they do it (their XS code is at https://metacpan.org/release/MSISK/Net-Ncap-1.01/source). Others here may have better ideas

The other way is via pure-perl, opening a Socket (in Perl) and do all I describe above in Perl. BUT I don't think they have implemented the socket type AF_CAN which is already specified in include/linux/can.h. So how do you open the socketCAN? Hmm. perhaps posting an issue at https://rt.cpan.org/Public/Dist/Display.html?Name=Socket and ask them what their plan is for socketCAN. Or better, post a patch for AF_CAN to be covered.

Finally, what you have suggested and doing already (piping out to candump) is OK, AFAIC. Although it may suffer if you aim for realtime performance.

So, depending on your C experience and time to invest, chose your route. I suggest the 1st route: building your C library (and don't worry too much about XS as I and others here can help you, and with C obviously). The 2nd route is mostly effortless and ready to use now, as you have already tried it. Check its realtime performance. Edit: Either way you chose, if you have the time, please post an issue about socketCAN at the Socket's bug tracker (Or I can do it sometime in the future).

bw, bliako


In reply to Re^3: Access socketCAN from Perl by bliako
in thread Access socketCAN from Perl by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.