-
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 remote-tracking branch 'alex/master' into bracz-socketcan-merge
# 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
Showing
3 changed files
with
203 additions
and
25 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,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); | ||
} |
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` |