Skip to content

added server mode to StandardFirmataEthernet #375

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions examples/StandardFirmataEthernet/StandardFirmataEthernet.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
/*
README

StandardFirmataEthernet is a TCP client implementation. You will need a Firmata client library
with a network transport that can act as a TCP server in order to establish a connection between
StandardFirmataEthernet is a TCP client/server implementation. You will need a Firmata client library
with a network transport that can act as a TCP server or client in order to establish a connection between
StandardFirmataEthernet and the Firmata client application.

To use StandardFirmataEthernet you will need to have one of the following
Expand Down Expand Up @@ -68,6 +68,7 @@
// follow the instructions in ethernetConfig.h to configure your particular hardware
#include "ethernetConfig.h"
#include "utility/EthernetClientStream.h"
#include "utility/EthernetServerStream.h"

/*
* Uncomment the following include to enable interfacing with Serial devices via hardware or
Expand Down Expand Up @@ -103,17 +104,25 @@

#if defined remote_ip && !defined remote_host
#ifdef local_ip
EthernetClientStream stream(client, local_ip, remote_ip, NULL, remote_port);
EthernetClientStream stream(client, local_ip, remote_ip, NULL, network_port);
#else
EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), remote_ip, NULL, remote_port);
EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), remote_ip, NULL, network_port);
#endif
#endif

#if !defined remote_ip && defined remote_host
#ifdef local_ip
EthernetClientStream stream(client, local_ip, IPAddress(0, 0, 0, 0), remote_host, remote_port);
EthernetClientStream stream(client, local_ip, IPAddress(0, 0, 0, 0), remote_host, network_port );
#else
EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), IPAddress(0, 0, 0, 0), remote_host, remote_port);
EthernetClientStream stream(client, IPAddress(0, 0, 0, 0), IPAddress(0, 0, 0, 0), remote_host, network_port);
#endif
#endif

#if !defined remote_ip && !defined remote_host
#ifdef local_ip
EthernetServerStream stream(local_ip, network_port);
#else
EthernetServerStream stream(IPAddress(0, 0, 0, 0), network_port);
#endif
#endif

Expand Down Expand Up @@ -864,6 +873,10 @@ void initTransport()
#endif

DEBUG_PRINTLN("connecting...");

DEBUG_PRINT("IP Address: ");
IPAddress ip = Ethernet.localIP();
DEBUG_PRINTLN(ip);
}

void initFirmata()
Expand Down
16 changes: 10 additions & 6 deletions examples/StandardFirmataEthernet/ethernetConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
*
* You must configure your particular hardware. Follow the steps below.
*
* Currently StandardFirmataEthernet is configured as a TCP client. An
* option to configure as a server may be added in the future.
* By default, StandardFirmataEthernet is configured as a TCP client.
* To configure as a TCP server, see STEP 2
*============================================================================*/

// STEP 1 [REQUIRED]
Expand Down Expand Up @@ -46,16 +46,20 @@ EthernetClient client;
YunClient client;
#endif

// STEP 2[REQUIRED for all boards and shields]
// replace with IP of the server you want to connect to, comment out if using 'remote_host'
// STEP 2 [REQUIRED for all boards and shields]
// TCP Client configuration:
// To configure your board as a TCP client, set the IP address of the server you want to connect to.
// TCP Server configuration:
// To configure your board as a TCP server, comment out the following line and also ensure that
// remote_host is also commented out.
#define remote_ip IPAddress(10, 0, 0, 3)
// *** REMOTE HOST IS NOT YET WORKING ***
// replace with hostname of server you want to connect to, comment out if using 'remote_ip'
// #define remote_host "server.local"

// STEP 3 [REQUIRED]
// Replace with the port that your server is listening on
#define remote_port 3030
// Replace with the port that your client or server is listening on.
#define network_port 3030

// STEP 4 [REQUIRED unless using DHCP]
// Replace with your board or ethernet shield's IP address
Expand Down
3 changes: 3 additions & 0 deletions utility/EthernetServerStream.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*
* Implementation is in EthernetServerStream.h to avoid linker issues.
*/
146 changes: 146 additions & 0 deletions utility/EthernetServerStream.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*
EthernetServerStream.h

Copyright (C) 2017 Marc Josef Pees. All rights reserved.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

See file LICENSE.txt for further informations on licensing terms.

Last updated July 10th, 2017
*/

#ifndef ETHERNETSERVERSTREAM_H
#define ETHERNETSERVERSTREAM_H

#include <inttypes.h>
#include <Stream.h>
#include <Ethernet.h>

//#define SERIAL_DEBUG
#include "firmataDebug.h"

class EthernetServerStream : public Stream
{
public:
EthernetServerStream(IPAddress localip, uint16_t port);
int available();
int read();
int peek();
void flush();
size_t write(uint8_t);
void maintain(IPAddress localip);

private:
EthernetClient client;
IPAddress localip;
uint16_t port;
bool connected;
bool maintain();
void stop();

protected:
EthernetServer server = EthernetServer(3030);
bool listening = false;
bool connect_client();
};


/*
* EthernetServerStream.cpp
* Copied here as a hack to linker issues with 3rd party board packages that don't properly
* implement the Arduino network APIs.
*/
EthernetServerStream::EthernetServerStream(IPAddress localip, uint16_t port)
: localip(localip),
port(port),
connected(false)
{
}

bool EthernetServerStream::connect_client()
{
if ( connected )
{
if ( client && client.connected() ) return true;
stop();
}

EthernetClient newClient = server.available();
if ( !newClient ) return false;
client = newClient;
connected = true;
return true;
}

int
EthernetServerStream::available()
{
return maintain() ? client.available() : 0;
}

int
EthernetServerStream::read()
{
return maintain() ? client.read() : -1;
}

int
EthernetServerStream::peek()
{
return maintain() ? client.peek() : -1;
}

void EthernetServerStream::flush()
{
if (maintain())
client.flush();
}

size_t
EthernetServerStream::write(uint8_t c)
{
return maintain() ? client.write(c) : 0;
}

void
EthernetServerStream::maintain(IPAddress localip)
{
// ensure the local IP is updated in the case that it is changed by the DHCP server
if (this->localip != localip) {
this->localip = localip;
if (connected)
stop();
}
}

void
EthernetServerStream::stop()
{
if(client)
{
client.stop();
}
connected = false;
}

bool
EthernetServerStream::maintain()
{
if (connect_client()) return true;

stop();

if(!listening)
{
server = EthernetServer(port);
server.begin();
listening = true;
}
return false;
}

#endif /* ETHERNETSERVERSTREAM_H */