Skip to content

Initial: porting Ethernet library #114

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
Mar 8, 2021
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
24 changes: 24 additions & 0 deletions libraries/Ethernet/README.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
= Ethernet Library for Arduino =

Enable Ethernet connectivity for Arduino Portenta and a shield/carrier with Ethernet connector.

For more information about this library please visit us at
http://www.arduino.cc/en/Reference/Ethernet

== License ==

Copyright (c) 2020 Arduino SA. All right 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.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
67 changes: 67 additions & 0 deletions libraries/Ethernet/keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#######################################
# Syntax Coloring Map For Ethernet
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

Ethernet KEYWORD1 Ethernet
EthernetClient KEYWORD1 EthernetClient
EthernetServer KEYWORD1 EthernetServer
IPAddress KEYWORD1 EthernetIPAddress

#######################################
# Methods and Functions (KEYWORD2)
#######################################

status KEYWORD2
connect KEYWORD2
write KEYWORD2
available KEYWORD2
availableForWrite KEYWORD2
read KEYWORD2
peek KEYWORD2
flush KEYWORD2
stop KEYWORD2
connected KEYWORD2
accept KEYWORD2
begin KEYWORD2
beginMulticast KEYWORD2
beginPacket KEYWORD2
endPacket KEYWORD2
parsePacket KEYWORD2
remoteIP KEYWORD2
remotePort KEYWORD2
getSocketNumber KEYWORD2
localIP KEYWORD2
localPort KEYWORD2
maintain KEYWORD2
linkStatus KEYWORD2
hardwareStatus KEYWORD2
MACAddress KEYWORD2
subnetMask KEYWORD2
gatewayIP KEYWORD2
dnsServerIP KEYWORD2
setMACAddress KEYWORD2
setLocalIP KEYWORD2
setSubnetMask KEYWORD2
setGatewayIP KEYWORD2
setDnsServerIP KEYWORD2
setRetransmissionTimeout KEYWORD2
setRetransmissionCount KEYWORD2
setConnectionTimeout KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

EthernetLinkStatus LITERAL1
Unknown LITERAL1
LinkON LITERAL1
LinkOFF LITERAL1
EthernetHardwareStatus LITERAL1
EthernetNoHardware LITERAL1
EthernetW5100 LITERAL1
EthernetW5200 LITERAL1
EthernetW5500 LITERAL1
10 changes: 10 additions & 0 deletions libraries/Ethernet/library.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name=Ethernet
version=1.0.0
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=Enables network connection (local and Internet) using Ethernet on mbed enabled boards
paragraph=With this library you can connect to Internet via Ethernet. The library provides both Client and server functionalities. The library permits you to connect to a local network also with DHCP and to resolve DNS.
category=Communication
url=http://www.arduino.cc/en/Reference/Ethernet
architectures=*
includes=Ethernet.h
143 changes: 143 additions & 0 deletions libraries/Ethernet/src/Ethernet.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#include "Ethernet.h"

#define SSID_MAX_LENGTH 32

arduino::IPAddress arduino::EthernetClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {
nsapi_addr_t address = socketAddress.get_addr();
return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]);
}

SocketAddress arduino::EthernetClass::socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port) {
nsapi_addr_t convertedIP = {NSAPI_IPv4, {ip[0], ip[1], ip[2], ip[3]}};
return SocketAddress(convertedIP, port);
}

int arduino::EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) {
if (eth_if == nullptr) {
//Q: What is the callback for?
_initializerCallback();
if(eth_if == nullptr) return 0;
}

unsigned long start = millis();
eth_if->set_blocking(false);
nsapi_error_t result = eth_if->connect();

while ((millis() - start < timeout) && (linkStatus() != LinkON)) {
delay(10);
}

return (linkStatus() != LinkON ? 1 : 0);
}

void arduino::EthernetClass::end() {
disconnect();
}

EthernetLinkStatus arduino::EthernetClass::linkStatus() {
return (eth_if->get_connection_status() == NSAPI_STATUS_GLOBAL_UP ? LinkON : LinkOFF);
}

EthernetHardwareStatus arduino::EthernetClass::hardwareStatus() {
return EthernetMbed;
}


int arduino::EthernetClass::disconnect() {
eth_if->disconnect();
}

void arduino::EthernetClass::config(arduino::IPAddress local_ip){
nsapi_addr_t convertedIP = {NSAPI_IPv4, {local_ip[0], local_ip[1], local_ip[2], local_ip[3]}};
_ip = SocketAddress(convertedIP);
}

void arduino::EthernetClass::config(const char *local_ip){
_ip = SocketAddress(local_ip);
}

void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server){
config(local_ip);
setDNS(dns_server);
}

void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway){
config(local_ip, dns_server);
nsapi_addr_t convertedGatewayIP = {NSAPI_IPv4, {gateway[0], gateway[1], gateway[2], gateway[3]}};
_gateway = SocketAddress(convertedGatewayIP);
}

void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet){
config(local_ip, dns_server, gateway);
nsapi_addr_t convertedSubnetMask = {NSAPI_IPv4, {subnet[0], subnet[1], subnet[2], subnet[3]}};
_netmask = SocketAddress(convertedSubnetMask);
}

void arduino::EthernetClass::setDNS(IPAddress dns_server1){
nsapi_addr_t convertedDNSServer = {NSAPI_IPv4, {dns_server1[0], dns_server1[1], dns_server1[2], dns_server1[3]}};
_dnsServer1 = SocketAddress(convertedDNSServer);
}

void arduino::EthernetClass::setDNS(IPAddress dns_server1, IPAddress dns_server2){
setDNS(dns_server1);
nsapi_addr_t convertedDNSServer2 = {NSAPI_IPv4, {dns_server2[0], dns_server2[1], dns_server2[2], dns_server2[3]}};
_dnsServer2 = SocketAddress(convertedDNSServer2);
}

uint8_t arduino::EthernetClass::status() {
return _currentNetworkStatus;
}

int arduino::EthernetClass::hostByName(const char* aHostname, IPAddress& aResult){
SocketAddress socketAddress = SocketAddress();
nsapi_error_t returnCode = getNetwork()->gethostbyname(aHostname, &socketAddress);
nsapi_addr_t address = socketAddress.get_addr();
aResult[0] = address.bytes[0];
aResult[1] = address.bytes[1];
aResult[2] = address.bytes[2];
aResult[3] = address.bytes[3];
return returnCode == NSAPI_ERROR_OK ? 1 : 0;
}

uint8_t* arduino::EthernetClass::macAddress(uint8_t* mac) {
const char *mac_str = getNetwork()->get_mac_address();
for( int b = 0; b < 6; b++ )
{
uint32_t tmp;
sscanf( &mac_str[b * 2 + (b)], "%02x", &tmp) ;
mac[5-b] = (uint8_t)tmp ;
}
//sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac[5], &mac[4], &mac[3], &mac[2], &mac[1], &mac[0]);
return mac;
}

arduino::IPAddress arduino::EthernetClass::localIP() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_ip_address(&ip);
return ipAddressFromSocketAddress(ip);
}

arduino::IPAddress arduino::EthernetClass::subnetMask() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_netmask(&ip);
return ipAddressFromSocketAddress(ip);
}

arduino::IPAddress arduino::EthernetClass::gatewayIP() {
SocketAddress ip;
NetworkInterface *interface = getNetwork();
interface->get_gateway(&ip);
return ipAddressFromSocketAddress(ip);
}

NetworkInterface *arduino::EthernetClass::getNetwork() {
return eth_if;
}

unsigned long arduino::EthernetClass::getTime() {
return 0;
}

arduino::EthernetClass Ethernet;
128 changes: 128 additions & 0 deletions libraries/Ethernet/src/Ethernet.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/* Copyright 2018 Paul Stoffregen
* Copyright 2020 Arduino SA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifndef ethernet_h_
#define ethernet_h_

#include "Arduino.h"
#include "api/IPAddress.h"
#include "EthernetClient.h"
#include "EthernetServer.h"
#include "EthernetUdp.h"

#include "netsocket/NetworkInterface.h"
#include "EthernetInterface.h"

enum EthernetLinkStatus {
Unknown,
LinkON,
LinkOFF
};

enum EthernetHardwareStatus {
EthernetNoHardware,
EthernetMbed = 6
};

namespace arduino {

typedef void* (*voidPrtFuncPtr)(void);

class EthernetClass {

public:
// Initialise the Ethernet shield to use the provided MAC address and
// gain the rest of the configuration through DHCP.
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
EthernetClass(EthernetInterface* _if) : eth_if(_if) {};
EthernetClass() {};

EthernetClass(voidPrtFuncPtr _cb) : _initializerCallback(_cb) {};

int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
int maintain();
EthernetLinkStatus linkStatus();
EthernetHardwareStatus hardwareStatus();

// Manaul configuration
void begin(uint8_t *mac, IPAddress ip) {}
void begin(uint8_t *mac, IPAddress ip, IPAddress dns) {}
void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {}
void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {}
void init(uint8_t sspin = 10);

void MACAddress(uint8_t *mac_address);
uint8_t* macAddress(uint8_t* mac);
IPAddress localIP();
IPAddress subnetMask();
IPAddress gatewayIP();
IPAddress dnsServerIP() { return ipAddressFromSocketAddress(_dnsServer1); }

void config(IPAddress local_ip);
void config(const char *local_ip);
void config(IPAddress local_ip, IPAddress dns_server);
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
void setDNS(IPAddress dns_server1);
void setDNS(IPAddress dns_server1, IPAddress dns_server2);
void setHostname(const char* name);

int disconnect(void);
void end(void);

uint8_t status();
int hostByName(const char* aHostname, IPAddress& aResult);
unsigned long getTime();

void setMACAddress(const uint8_t *mac_address);
void setLocalIP(const IPAddress local_ip);
void setSubnetMask(const IPAddress subnet);
void setGatewayIP(const IPAddress gateway);
void setDnsServerIP(const IPAddress dns_server) { _dnsServer1 = socketAddressFromIpAddress(dns_server, 0); }
void setRetransmissionTimeout(uint16_t milliseconds);
void setRetransmissionCount(uint8_t num);

friend class EthernetClient;
friend class EthernetServer;
friend class EthernetUDP;

NetworkInterface *getNetwork();

private:

volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
EthernetInterface net;
SocketAddress _ip = nullptr;
SocketAddress _gateway = nullptr;
SocketAddress _netmask = nullptr;
SocketAddress _dnsServer1 = nullptr;
SocketAddress _dnsServer2 = nullptr;
EthernetInterface* eth_if = &net;
voidPrtFuncPtr _initializerCallback;
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
SocketAddress socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port);
};

}

extern arduino::EthernetClass Ethernet;

#endif
Loading