-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #422 from bakerstu/bracz-hub-socketcan
Adds SocketCan support to the openmrn hub.
- Loading branch information
Showing
8 changed files
with
249 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../linux.x86/AvaHiMDNS.cxx |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Adding SocketCAN support on the Raspberry Pi 4 | ||
|
||
These are my notes from adding SocketCAN support and building the **hub** application in a Raspberry Pi. | ||
|
||
- Start with the pre-built JMRI - RPI disk image from: [M Steve Todd's - JMRI RaspberryPi as Access Point](https://mstevetodd.com/rpi) | ||
|
||
- Install the MCP2517 CAN interface hardware from: [2-Channel CAN-BUS(FD) Shield for Raspberry Pi](https://www.seeedstudio.com/2-Channel-CAN-BUS-FD-Shield-for-Raspberry-Pi-p-4072.html) and followed their instructions for building the MCP2517 kernel device driver on their [Support Wiki](http://wiki.seeedstudio.com/2-Channel-CAN-BUS-FD-Shield-for-Raspberry-Pi/#software) page. | ||
|
||
- Install some needed packages on the RPi: | ||
|
||
`sudo apt install git doxygen libavahi-client-dev` | ||
|
||
- Download the OpenMRN source code to the RPi: | ||
|
||
`cd ~` | ||
|
||
`git clone https://github.com/bakerstu/openmrn.git` | ||
|
||
- Build the **hub** application: | ||
|
||
`cd openmrn/applications/hub/targets/linux.rpi1/` | ||
|
||
`make` | ||
|
||
- Configure the **can0** interface for 125,000 bps and run the **hub** application at system at start-up by creating the file: `/etc/network/interfaces.d/can0` with the following lines: | ||
``` | ||
allow-hotplug can0 | ||
iface can0 can static | ||
bitrate 125000 | ||
up /home/pi/openmrn/applications/hub/targets/linux.rpi1/hub -s $IFACE & | ||
``` | ||
|
||
- Configure the LCC Layout Connection in JMRI to use | ||
- System Connection: `CAN via GridConnect Network Interface` | ||
- IP Address/Host Name: `localhost` | ||
- TCP/UDP Port: `12021` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** \copyright | ||
* Copyright (c) 2020, Balazs Racz | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* \file SocketCan.cxx | ||
* | ||
* Helper functions to connect to CAN devices via SocketCan. | ||
* | ||
* @author Balazs Racz | ||
* @date 1 Sep 2020 | ||
*/ | ||
|
||
#include "utils/SocketCan.hxx" | ||
#include "can_frame.h" | ||
#include <string.h> | ||
|
||
#if defined(__linux__) | ||
|
||
#include <errno.h> | ||
#include <linux/sockios.h> | ||
#include <net/if.h> | ||
#include <stdio.h> | ||
#include <sys/ioctl.h> | ||
|
||
/// This macro executes an OS call, and if it returns negative result, then | ||
/// prints the errno to stderr, and terminates the current function with -1 | ||
/// return value. | ||
/// @param where textual description of what function was called | ||
/// (e.g. "socket") | ||
/// @param x... the function call. | ||
#define ERRNOLOG(where, x...) \ | ||
do \ | ||
{ \ | ||
if ((x) < 0) \ | ||
{ \ | ||
perror(where); \ | ||
return -1; \ | ||
} \ | ||
} while (0) | ||
|
||
int socketcan_open(const char *device, int loopback) | ||
{ | ||
int s; | ||
struct sockaddr_can addr; | ||
struct ifreq ifr; | ||
|
||
s = socket(PF_CAN, SOCK_RAW, CAN_RAW); | ||
ERRNOLOG("socket", s); | ||
|
||
// Set the blocking limit to the minimum allowed, typically 1024 in Linux | ||
int sndbuf = 0; | ||
ERRNOLOG("setsockopt(sndbuf)", | ||
setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf))); | ||
|
||
// turn on/off loopback | ||
ERRNOLOG("setsockopt(loopback)", | ||
setsockopt( | ||
s, SOL_CAN_RAW, CAN_RAW_LOOPBACK, &loopback, sizeof(loopback))); | ||
|
||
// setup error notifications | ||
can_err_mask_t err_mask = CAN_ERR_TX_TIMEOUT | CAN_ERR_LOSTARB | | ||
CAN_ERR_CRTL | CAN_ERR_PROT | CAN_ERR_TRX | CAN_ERR_ACK | | ||
CAN_ERR_BUSOFF | CAN_ERR_BUSERROR | CAN_ERR_RESTARTED; | ||
ERRNOLOG("setsockopt(filter)", | ||
setsockopt( | ||
s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask))); | ||
strcpy(ifr.ifr_name, device); | ||
|
||
ERRNOLOG("interface set", ::ioctl(s, SIOCGIFINDEX, &ifr)); | ||
|
||
addr.can_family = AF_CAN; | ||
addr.can_ifindex = ifr.ifr_ifindex; | ||
|
||
ERRNOLOG("bind", bind(s, (struct sockaddr *)&addr, sizeof(addr))); | ||
|
||
return s; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** \copyright | ||
* Copyright (c) 2020, Balazs Racz | ||
* All rights reserved. | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* - Redistributions of source code must retain the above copyright notice, | ||
* this list of conditions and the following disclaimer. | ||
* | ||
* - Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* \file SocketCan.hxx | ||
* | ||
* Helper functions to connect to CAN devices via SocketCan. | ||
* | ||
* @author Balazs Racz | ||
* @date 1 Sep 2020 | ||
*/ | ||
|
||
#ifndef _UTILS_SOCKETCAN_HXX_ | ||
#define _UTILS_SOCKETCAN_HXX_ | ||
|
||
#if defined(__linux__) | ||
|
||
/// Opens a SocketCan socket. | ||
/// @param device the name of the CAN device, e.g. can0 | ||
/// @param loopback 1 to enable loopback locally to other open references, | ||
/// 0 to disable loopback locally to other open references. | ||
/// @return an open socket file descriptor, or -1 if there was an error. | ||
int socketcan_open(const char *device, int loopback); | ||
|
||
#endif | ||
|
||
#endif // _UTILS_SOCKETCAN_HXX_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters