Skip to content

Commit

Permalink
Added SocketCAN support to the hub application on the Raspberry Pi
Browse files Browse the repository at this point in the history
  • Loading branch information
kiwi64ajs committed Oct 20, 2019
1 parent 239d139 commit ce9cb5b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
53 changes: 52 additions & 1 deletion applications/hub/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
#include <stdio.h>
#include <unistd.h>

#if defined(__linux__) || defined(__MACH__)
#include <net/if.h>
#include <termios.h> /* tc* functions */
#endif

#if defined(__linux__)
#include "utils/HubDeviceSelect.hxx"
#include <linux/sockios.h>
#include <sys/ioctl.h>
#endif

#include <memory>

#include "os/os.h"
Expand All @@ -58,6 +69,7 @@ OVERRIDE_CONST(gridconnect_buffer_delay_usec, 2000);

int port = 12021;
const char *device_path = nullptr;
const char *socket_can_path = nullptr;
int upstream_port = 12021;
const char *upstream_host = nullptr;
bool timestamped = false;
Expand All @@ -79,6 +91,8 @@ void usage(const char *e)
fprintf(stderr, "\t-d device is a path to a physical device doing "
"serial-CAN or USB-CAN. If specified, opens device and "
"adds it to the hub.\n");
fprintf(stderr, "\t-s socketcan device is a path to a socketcan device. "
"If specified, opens device and adds it to the hub.\n");
fprintf(stderr, "\t-u upstream_host is the host name for an upstream "
"hub. If specified, this hub will connect to an upstream "
"hub.\n");
Expand All @@ -100,7 +114,7 @@ void usage(const char *e)
void parse_args(int argc, char *argv[])
{
int opt;
while ((opt = getopt(argc, argv, "hp:d:u:q:tlmn:")) >= 0)
while ((opt = getopt(argc, argv, "hp:d:s:u:q:tlmn:")) >= 0)
{
switch (opt)
{
Expand All @@ -110,6 +124,9 @@ void parse_args(int argc, char *argv[])
case 'd':
device_path = optarg;
break;
case 's':
socket_can_path = optarg;
break;
case 'p':
port = atoi(optarg);
break;
Expand Down Expand Up @@ -178,6 +195,40 @@ int appl_main(int argc, char *argv[])
new DeviceConnectionClient("device", &can_hub0, device_path));
}

if (socket_can_path)
{
int s;
struct sockaddr_can addr;
struct ifreq ifr;
int loopback = 1;

s = socket(PF_CAN, SOCK_RAW, CAN_RAW);

// Set the blocking limit to the minimum allowed, typically 1024 in Linux
int sndbuf = 0;
setsockopt(s, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));

// turn on/off 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;
setsockopt(s, SOL_CAN_RAW, CAN_RAW_ERR_FILTER, &err_mask, sizeof(err_mask));
strcpy(ifr.ifr_name, socket_can_path);

::ioctl(s, SIOCGIFINDEX, &ifr);

addr.can_family = AF_CAN;
addr.can_ifindex = ifr.ifr_ifindex;

bind(s, (struct sockaddr *)&addr, sizeof(addr));

new HubDeviceSelect<CanHubFlow>(&can_hub0, s);
// connections.emplace_back(port);
}

while (1)
{
for (const auto &p : connections)
Expand Down
5 changes: 2 additions & 3 deletions src/openlcb/SimpleNodeInfo.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,8 @@ void init_snip_user_file(int fd, const char *user_name,
SimpleNodeDynamicValues data;
memset(&data, 0, sizeof(data));
data.version = 2;
strncpy(data.user_name, user_name, sizeof(data.user_name));
strncpy(data.user_description, user_description,
sizeof(data.user_description));
memcpy(data.user_name, user_name, sizeof(data.user_name));
memcpy(data.user_description, user_description, sizeof(data.user_description));
int ofs = 0;
auto *p = (const uint8_t *)&data;
const int len = sizeof(data);
Expand Down
2 changes: 1 addition & 1 deletion src/openlcb/SimpleNodeInfoMockUserFile.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ openlcb::MockSNIPUserFile::MockSNIPUserFile(const char *user_name,
{
init_snip_user_file(userFile_.fd(), user_name, user_description);
HASSERT(userFile_.name().size() < sizeof(snip_user_file_path));
strncpy(snip_user_file_path, userFile_.name().c_str(),
memcpy(snip_user_file_path, userFile_.name().c_str(),
sizeof(snip_user_file_path));
}

Expand Down

0 comments on commit ce9cb5b

Please sign in to comment.