Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8e02ebb

Browse files
Merge pull request #114 from facchinm/ethernet_port
Initial: porting Ethernet library
2 parents 004f05f + b480d4a commit 8e02ebb

File tree

11 files changed

+988
-0
lines changed

11 files changed

+988
-0
lines changed

‎libraries/Ethernet/README.adoc‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
= Ethernet Library for Arduino =
2+
3+
Enable Ethernet connectivity for Arduino Portenta and a shield/carrier with Ethernet connector.
4+
5+
For more information about this library please visit us at
6+
http://www.arduino.cc/en/Reference/Ethernet
7+
8+
== License ==
9+
10+
Copyright (c) 2020 Arduino SA. All right reserved.
11+
12+
This library is free software; you can redistribute it and/or
13+
modify it under the terms of the GNU Lesser General Public
14+
License as published by the Free Software Foundation; either
15+
version 2.1 of the License, or (at your option) any later version.
16+
17+
This library is distributed in the hope that it will be useful,
18+
but WITHOUT ANY WARRANTY; without even the implied warranty of
19+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20+
Lesser General Public License for more details.
21+
22+
You should have received a copy of the GNU Lesser General Public
23+
License along with this library; if not, write to the Free Software
24+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

‎libraries/Ethernet/keywords.txt‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#######################################
2+
# Syntax Coloring Map For Ethernet
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Ethernet KEYWORD1 Ethernet
10+
EthernetClient KEYWORD1 EthernetClient
11+
EthernetServer KEYWORD1 EthernetServer
12+
IPAddress KEYWORD1 EthernetIPAddress
13+
14+
#######################################
15+
# Methods and Functions (KEYWORD2)
16+
#######################################
17+
18+
status KEYWORD2
19+
connect KEYWORD2
20+
write KEYWORD2
21+
available KEYWORD2
22+
availableForWrite KEYWORD2
23+
read KEYWORD2
24+
peek KEYWORD2
25+
flush KEYWORD2
26+
stop KEYWORD2
27+
connected KEYWORD2
28+
accept KEYWORD2
29+
begin KEYWORD2
30+
beginMulticast KEYWORD2
31+
beginPacket KEYWORD2
32+
endPacket KEYWORD2
33+
parsePacket KEYWORD2
34+
remoteIP KEYWORD2
35+
remotePort KEYWORD2
36+
getSocketNumber KEYWORD2
37+
localIP KEYWORD2
38+
localPort KEYWORD2
39+
maintain KEYWORD2
40+
linkStatus KEYWORD2
41+
hardwareStatus KEYWORD2
42+
MACAddress KEYWORD2
43+
subnetMask KEYWORD2
44+
gatewayIP KEYWORD2
45+
dnsServerIP KEYWORD2
46+
setMACAddress KEYWORD2
47+
setLocalIP KEYWORD2
48+
setSubnetMask KEYWORD2
49+
setGatewayIP KEYWORD2
50+
setDnsServerIP KEYWORD2
51+
setRetransmissionTimeout KEYWORD2
52+
setRetransmissionCount KEYWORD2
53+
setConnectionTimeout KEYWORD2
54+
55+
#######################################
56+
# Constants (LITERAL1)
57+
#######################################
58+
59+
EthernetLinkStatus LITERAL1
60+
Unknown LITERAL1
61+
LinkON LITERAL1
62+
LinkOFF LITERAL1
63+
EthernetHardwareStatus LITERAL1
64+
EthernetNoHardware LITERAL1
65+
EthernetW5100 LITERAL1
66+
EthernetW5200 LITERAL1
67+
EthernetW5500 LITERAL1
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=Ethernet
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <info@arduino.cc>
5+
sentence=Enables network connection (local and Internet) using Ethernet on mbed enabled boards
6+
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.
7+
category=Communication
8+
url=http://www.arduino.cc/en/Reference/Ethernet
9+
architectures=*
10+
includes=Ethernet.h

‎libraries/Ethernet/src/Ethernet.cpp‎

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#include "Ethernet.h"
2+
3+
#define SSID_MAX_LENGTH 32
4+
5+
arduino::IPAddress arduino::EthernetClass::ipAddressFromSocketAddress(SocketAddress socketAddress) {
6+
nsapi_addr_t address = socketAddress.get_addr();
7+
return IPAddress(address.bytes[0], address.bytes[1], address.bytes[2], address.bytes[3]);
8+
}
9+
10+
SocketAddress arduino::EthernetClass::socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port) {
11+
nsapi_addr_t convertedIP = {NSAPI_IPv4, {ip[0], ip[1], ip[2], ip[3]}};
12+
return SocketAddress(convertedIP, port);
13+
}
14+
15+
int arduino::EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned long responseTimeout) {
16+
if (eth_if == nullptr) {
17+
//Q: What is the callback for?
18+
_initializerCallback();
19+
if(eth_if == nullptr) return 0;
20+
}
21+
22+
unsigned long start = millis();
23+
eth_if->set_blocking(false);
24+
nsapi_error_t result = eth_if->connect();
25+
26+
while ((millis() - start < timeout) && (linkStatus() != LinkON)) {
27+
delay(10);
28+
}
29+
30+
return (linkStatus() != LinkON ? 1 : 0);
31+
}
32+
33+
void arduino::EthernetClass::end() {
34+
disconnect();
35+
}
36+
37+
EthernetLinkStatus arduino::EthernetClass::linkStatus() {
38+
return (eth_if->get_connection_status() == NSAPI_STATUS_GLOBAL_UP ? LinkON : LinkOFF);
39+
}
40+
41+
EthernetHardwareStatus arduino::EthernetClass::hardwareStatus() {
42+
return EthernetMbed;
43+
}
44+
45+
46+
int arduino::EthernetClass::disconnect() {
47+
eth_if->disconnect();
48+
}
49+
50+
void arduino::EthernetClass::config(arduino::IPAddress local_ip){
51+
nsapi_addr_t convertedIP = {NSAPI_IPv4, {local_ip[0], local_ip[1], local_ip[2], local_ip[3]}};
52+
_ip = SocketAddress(convertedIP);
53+
}
54+
55+
void arduino::EthernetClass::config(const char *local_ip){
56+
_ip = SocketAddress(local_ip);
57+
}
58+
59+
void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server){
60+
config(local_ip);
61+
setDNS(dns_server);
62+
}
63+
64+
void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway){
65+
config(local_ip, dns_server);
66+
nsapi_addr_t convertedGatewayIP = {NSAPI_IPv4, {gateway[0], gateway[1], gateway[2], gateway[3]}};
67+
_gateway = SocketAddress(convertedGatewayIP);
68+
}
69+
70+
void arduino::EthernetClass::config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet){
71+
config(local_ip, dns_server, gateway);
72+
nsapi_addr_t convertedSubnetMask = {NSAPI_IPv4, {subnet[0], subnet[1], subnet[2], subnet[3]}};
73+
_netmask = SocketAddress(convertedSubnetMask);
74+
}
75+
76+
void arduino::EthernetClass::setDNS(IPAddress dns_server1){
77+
nsapi_addr_t convertedDNSServer = {NSAPI_IPv4, {dns_server1[0], dns_server1[1], dns_server1[2], dns_server1[3]}};
78+
_dnsServer1 = SocketAddress(convertedDNSServer);
79+
}
80+
81+
void arduino::EthernetClass::setDNS(IPAddress dns_server1, IPAddress dns_server2){
82+
setDNS(dns_server1);
83+
nsapi_addr_t convertedDNSServer2 = {NSAPI_IPv4, {dns_server2[0], dns_server2[1], dns_server2[2], dns_server2[3]}};
84+
_dnsServer2 = SocketAddress(convertedDNSServer2);
85+
}
86+
87+
uint8_t arduino::EthernetClass::status() {
88+
return _currentNetworkStatus;
89+
}
90+
91+
int arduino::EthernetClass::hostByName(const char* aHostname, IPAddress& aResult){
92+
SocketAddress socketAddress = SocketAddress();
93+
nsapi_error_t returnCode = getNetwork()->gethostbyname(aHostname, &socketAddress);
94+
nsapi_addr_t address = socketAddress.get_addr();
95+
aResult[0] = address.bytes[0];
96+
aResult[1] = address.bytes[1];
97+
aResult[2] = address.bytes[2];
98+
aResult[3] = address.bytes[3];
99+
return returnCode == NSAPI_ERROR_OK ? 1 : 0;
100+
}
101+
102+
uint8_t* arduino::EthernetClass::macAddress(uint8_t* mac) {
103+
const char *mac_str = getNetwork()->get_mac_address();
104+
for( int b = 0; b < 6; b++ )
105+
{
106+
uint32_t tmp;
107+
sscanf( &mac_str[b * 2 + (b)], "%02x", &tmp) ;
108+
mac[5-b] = (uint8_t)tmp ;
109+
}
110+
//sscanf(mac_str, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx", &mac[5], &mac[4], &mac[3], &mac[2], &mac[1], &mac[0]);
111+
return mac;
112+
}
113+
114+
arduino::IPAddress arduino::EthernetClass::localIP() {
115+
SocketAddress ip;
116+
NetworkInterface *interface = getNetwork();
117+
interface->get_ip_address(&ip);
118+
return ipAddressFromSocketAddress(ip);
119+
}
120+
121+
arduino::IPAddress arduino::EthernetClass::subnetMask() {
122+
SocketAddress ip;
123+
NetworkInterface *interface = getNetwork();
124+
interface->get_netmask(&ip);
125+
return ipAddressFromSocketAddress(ip);
126+
}
127+
128+
arduino::IPAddress arduino::EthernetClass::gatewayIP() {
129+
SocketAddress ip;
130+
NetworkInterface *interface = getNetwork();
131+
interface->get_gateway(&ip);
132+
return ipAddressFromSocketAddress(ip);
133+
}
134+
135+
NetworkInterface *arduino::EthernetClass::getNetwork() {
136+
return eth_if;
137+
}
138+
139+
unsigned long arduino::EthernetClass::getTime() {
140+
return 0;
141+
}
142+
143+
arduino::EthernetClass Ethernet;

‎libraries/Ethernet/src/Ethernet.h‎

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/* Copyright 2018 Paul Stoffregen
2+
* Copyright 2020 Arduino SA
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
5+
* software and associated documentation files (the "Software"), to deal in the Software
6+
* without restriction, including without limitation the rights to use, copy, modify,
7+
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
8+
* permit persons to whom the Software is furnished to do so, subject to the following
9+
* conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
15+
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
16+
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17+
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
18+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
19+
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20+
*/
21+
22+
#ifndef ethernet_h_
23+
#define ethernet_h_
24+
25+
#include "Arduino.h"
26+
#include "api/IPAddress.h"
27+
#include "EthernetClient.h"
28+
#include "EthernetServer.h"
29+
#include "EthernetUdp.h"
30+
31+
#include "netsocket/NetworkInterface.h"
32+
#include "EthernetInterface.h"
33+
34+
enum EthernetLinkStatus {
35+
Unknown,
36+
LinkON,
37+
LinkOFF
38+
};
39+
40+
enum EthernetHardwareStatus {
41+
EthernetNoHardware,
42+
EthernetMbed = 6
43+
};
44+
45+
namespace arduino {
46+
47+
typedef void* (*voidPrtFuncPtr)(void);
48+
49+
class EthernetClass {
50+
51+
public:
52+
// Initialise the Ethernet shield to use the provided MAC address and
53+
// gain the rest of the configuration through DHCP.
54+
// Returns 0 if the DHCP configuration failed, and 1 if it succeeded
55+
EthernetClass(EthernetInterface* _if) : eth_if(_if) {};
56+
EthernetClass() {};
57+
58+
EthernetClass(voidPrtFuncPtr _cb) : _initializerCallback(_cb) {};
59+
60+
int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
61+
int maintain();
62+
EthernetLinkStatus linkStatus();
63+
EthernetHardwareStatus hardwareStatus();
64+
65+
// Manaul configuration
66+
void begin(uint8_t *mac, IPAddress ip) {}
67+
void begin(uint8_t *mac, IPAddress ip, IPAddress dns) {}
68+
void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {}
69+
void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {}
70+
void init(uint8_t sspin = 10);
71+
72+
void MACAddress(uint8_t *mac_address);
73+
uint8_t* macAddress(uint8_t* mac);
74+
IPAddress localIP();
75+
IPAddress subnetMask();
76+
IPAddress gatewayIP();
77+
IPAddress dnsServerIP() { return ipAddressFromSocketAddress(_dnsServer1); }
78+
79+
void config(IPAddress local_ip);
80+
void config(const char *local_ip);
81+
void config(IPAddress local_ip, IPAddress dns_server);
82+
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway);
83+
void config(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
84+
void setDNS(IPAddress dns_server1);
85+
void setDNS(IPAddress dns_server1, IPAddress dns_server2);
86+
void setHostname(const char* name);
87+
88+
int disconnect(void);
89+
void end(void);
90+
91+
uint8_t status();
92+
int hostByName(const char* aHostname, IPAddress& aResult);
93+
unsigned long getTime();
94+
95+
void setMACAddress(const uint8_t *mac_address);
96+
void setLocalIP(const IPAddress local_ip);
97+
void setSubnetMask(const IPAddress subnet);
98+
void setGatewayIP(const IPAddress gateway);
99+
void setDnsServerIP(const IPAddress dns_server) { _dnsServer1 = socketAddressFromIpAddress(dns_server, 0); }
100+
void setRetransmissionTimeout(uint16_t milliseconds);
101+
void setRetransmissionCount(uint8_t num);
102+
103+
friend class EthernetClient;
104+
friend class EthernetServer;
105+
friend class EthernetUDP;
106+
107+
NetworkInterface *getNetwork();
108+
109+
private:
110+
111+
volatile EthernetLinkStatus _currentNetworkStatus = Unknown;
112+
EthernetInterface net;
113+
SocketAddress _ip = nullptr;
114+
SocketAddress _gateway = nullptr;
115+
SocketAddress _netmask = nullptr;
116+
SocketAddress _dnsServer1 = nullptr;
117+
SocketAddress _dnsServer2 = nullptr;
118+
EthernetInterface* eth_if = &net;
119+
voidPrtFuncPtr _initializerCallback;
120+
arduino::IPAddress ipAddressFromSocketAddress(SocketAddress socketAddress);
121+
SocketAddress socketAddressFromIpAddress(arduino::IPAddress ip, uint16_t port);
122+
};
123+
124+
}
125+
126+
extern arduino::EthernetClass Ethernet;
127+
128+
#endif

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /