Skip to content

Commit

Permalink
Merge remote-tracking branch 'alex/master' into bracz-socketcan-merge
Browse files Browse the repository at this point in the history
# By Alex Shepherd
# Via GitHub
* alex/master:
  Update main.cxx
  Added SocketCAN support to the hub application on the Raspberry Pi
  Added SocketCAN support to the hub application on the Raspberry Pi
  Added SocketCAN support to the hub application on the Raspberry Pi

# Conflicts:
#	applications/hub/main.cxx
  • Loading branch information
balazsracz committed Sep 2, 2020
2 parents c1271c1 + 74d22f9 commit 00eb2c2
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 25 deletions.
52 changes: 27 additions & 25 deletions applications/hub/main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,41 +60,43 @@ 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;
bool export_mdns = false;
const char* mdns_name = "openmrn_hub";
bool printpackets = false;
const char *socketcan_port = nullptr;

void usage(const char *e)
{
fprintf(stderr, "Usage: %s [-p port] [-d device_path] [-u upstream_host] "
"[-q upstream_port] [-m] [-n mdns_name] "
fprintf(stderr,
"Usage: %s [-p port] [-d device_path] [-u upstream_host] "
"[-q upstream_port] [-m] [-n mdns_name] "
#if defined(__linux__)
"[-s socketcan_interface] "
#endif
"[-t] [-l]\n\n",
e);
fprintf(stderr, "GridConnect CAN HUB.\nListens to a specific TCP port, "
"reads CAN packets from the incoming connections using "
"the GridConnect protocol, and forwards all incoming "
"packets to all other participants.\n\nArguments:\n");
"[-s socketcan_interface] "
#endif
"[-t] [-l]\n\n",
e);
fprintf(stderr,
"GridConnect CAN HUB.\nListens to a specific TCP port, "
"reads CAN packets from the incoming connections using "
"the GridConnect protocol, and forwards all incoming "
"packets to all other participants.\n\nArguments:\n");
fprintf(stderr, "\t-p port specifies the port number to listen on, "
"default is 12021.\n");
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");
#if defined(__linux__)
fprintf(stderr, "\t-s socketcan_interface is a socketcan device (e.g. 'can0'). "
"If specified, opens device and adds it to the hub.\n");
#endif
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");
fprintf(stderr,
"\t-q upstream_port is the port number for the upstream hub.\n");
#if defined(__linux__)
fprintf(stderr,
"\t-s can0 adds the SocketCan interface 'can0' to the hub.\n");
#endif
fprintf(stderr,
"\t-t prints timestamps for each packet.\n");
fprintf(stderr,
Expand All @@ -111,7 +113,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:s:")) >= 0)
while ((opt = getopt(argc, argv, "hp:d:s:u:q:tlmn:")) >= 0)
{
switch (opt)
{
Expand All @@ -121,6 +123,11 @@ void parse_args(int argc, char *argv[])
case 'd':
device_path = optarg;
break;
#if defined(__linux__)
case 's':
socket_can_path = optarg;
break;
#endif
case 'p':
port = atoi(optarg);
break;
Expand All @@ -143,11 +150,6 @@ void parse_args(int argc, char *argv[])
case 'l':
printpackets = true;
break;
#if defined(__linux__)
case 's':
socketcan_port = optarg;
break;
#endif
default:
fprintf(stderr, "Unknown option %c\n", opt);
usage(argv[0]);
Expand Down Expand Up @@ -183,17 +185,17 @@ int appl_main(int argc, char *argv[])
}
#endif
#if defined(__linux__)
if (socketcan_port)
if (socket_can_path)
{
int s = socketcan_open(socketcan_port, 1);
int s = socketcan_open(socket_can_path, 1);
if (s >= 0)
{
new HubDeviceSelect<CanHubFlow>(&can_hub0, s);
fprintf(stderr, "Opened SocketCan %s: fd %d\n", socketcan_port, s);
fprintf(stderr, "Opened SocketCan %s: fd %d\n", socket_can_path, s);
}
else
{
fprintf(stderr, "Failed to open SocketCan %s.\n", socketcan_port);
fprintf(stderr, "Failed to open SocketCan %s.\n", socket_can_path);
}
}
#endif
Expand Down
140 changes: 140 additions & 0 deletions applications/hub/targets/linux.rpi1/AvaHiMDNS.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/** \copyright
* Copyright (c) 2016, Stuart W. Baker
* 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 AvaHiMDNS.cxx
*
* A simple abstraction to publish mDNS sevices using Avahi.
*
* @author Stuart Baker
* @date 30 July 2016
*/

#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <avahi-client/client.h>
#include <avahi-client/publish.h>
#include <avahi-common/simple-watch.h>
#include <avahi-common/malloc.h>
#include <avahi-common/error.h>

#include "utils/macros.h"
#include "openlcb/TcpDefs.hxx"

static AvahiEntryGroup *group = nullptr;
static AvahiSimplePoll *simplePoll = nullptr;
static AvahiClient *client = nullptr;
static sem_t wait;

/** Avahi group callback.
*/
static void entry_group_callback(AvahiEntryGroup *g, AvahiEntryGroupState state,
void *userdata)
{
group = g;
}

/** Avahi client callback.
*/
static void client_callback(AvahiClient *c, AvahiClientState state,
void * userdata)
{
switch (state)
{
default:
break;
case AVAHI_CLIENT_S_RUNNING:
printf("client running\n");
break;
}
}

/** mdns polling thread.
* @param unused unused parameter
* @return should never return
*/
static void *mdns_thread(void *unused)
{
int error;

simplePoll = avahi_simple_poll_new();
HASSERT(simplePoll);

client = avahi_client_new(avahi_simple_poll_get(simplePoll),
(AvahiClientFlags)0, client_callback, nullptr,
&error);
printf("client created\n");
HASSERT(client);

sem_post(&wait);
avahi_simple_poll_loop(simplePoll);

return nullptr;
}

/** Start the mDNS client.
*/
void mdns_client_start()
{
sem_init(&wait, 0, 0);

pthread_t thread;
pthread_create(&thread, nullptr, mdns_thread, nullptr);
printf("client start\n");

sem_wait(&wait);
}

/** Publish an mDNS name.
*/
void mdns_publish(const char *name, uint16_t port)
{
name = avahi_strdup(name);

if (!group)
{
group = avahi_entry_group_new(client, entry_group_callback, nullptr);
if (!group) {
fprintf(stderr, "avahi_entry_group_new() failed: %s\n",
avahi_strerror(avahi_client_errno(client)));
}
HASSERT(group);
}

int result = avahi_entry_group_add_service(group, AVAHI_IF_UNSPEC,
AVAHI_PROTO_UNSPEC, (AvahiPublishFlags)0, name,
openlcb::TcpDefs::MDNS_SERVICE_NAME_GRIDCONNECT_CAN_TCP, NULL, NULL, port,
"platform=linux-x86-openmrn", NULL);

if (result != 0)
{
fprintf(stderr, "Error exporting mDNS name (%d) %s\n", result,
avahi_strerror(result));
}

HASSERT(result == 0);
avahi_entry_group_commit(group);
}
36 changes: 36 additions & 0 deletions applications/hub/targets/linux.rpi1/README.md
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`

0 comments on commit 00eb2c2

Please sign in to comment.