Skip to content

Commit e2477e9

Browse files
committed
ubuntu: Move networking bits to the ethernet library
1 parent 3693159 commit e2477e9

File tree

11 files changed

+235
-57
lines changed

11 files changed

+235
-57
lines changed

src/mgos_eth.h renamed to include/mgos_eth.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18-
#ifndef CS_MOS_LIBS_ETHERNET_SRC_MGOS_ETH_H_
19-
#define CS_MOS_LIBS_ETHERNET_SRC_MGOS_ETH_H_
18+
#pragma once
2019

2120
#include <stdbool.h>
2221
#include <stdint.h>
2322

24-
#include "lwip/ip_addr.h"
23+
#include "mgos_net.h"
2524

2625
#ifdef __cplusplus
2726
extern "C" {
@@ -56,10 +55,10 @@ const char *mgos_eth_speed_str(enum mgos_eth_speed speed);
5655
const char *mgos_eth_duplex_str(enum mgos_eth_duplex duplex);
5756
bool mgos_eth_phy_opts_from_str(const char *str,
5857
struct mgos_eth_phy_opts *opts);
59-
bool mgos_eth_get_static_ip_config(ip4_addr_t *ip, ip4_addr_t *netmask,
60-
ip4_addr_t *gw);
58+
bool mgos_eth_get_static_ip_config(struct sockaddr_in *ip,
59+
struct sockaddr_in *netmask,
60+
struct sockaddr_in *gw);
6161

6262
#ifdef __cplusplus
6363
}
6464
#endif
65-
#endif /* CS_MOS_LIBS_ETHERNET_SRC_MGOS_ETH_H_ */
File renamed without changes.
File renamed without changes.

include/ubuntu/ubuntu_eth.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
#include "mgos_net.h"
20+
21+
#ifdef __cplusplus
22+
extern "C" {
23+
#endif
24+
25+
bool ubuntu_get_default_gateway(char *dev, size_t devlen,
26+
struct sockaddr_in *gw);
27+
28+
#ifdef __cplusplus
29+
}
30+
#endif

mos.yml

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ description: Ethernet support
33
type: lib
44
version: 1.0
55

6-
platforms: [ esp32, stm32 ]
7-
8-
libs:
9-
- origin: https://github.com/mongoose-os-libs/core
10-
- origin: https://github.com/mongoose-os-libs/lwip
6+
platforms: [ esp32, stm32, ubuntu ]
117

128
sources:
139
- src
10+
- src/${mos.platform}
11+
12+
includes:
13+
- include
14+
- include/${mos.platform}
1415

1516
config_schema:
1617
- ["eth", "o", {title: "Ethernet settings"}]
@@ -20,9 +21,44 @@ config_schema:
2021
- ["eth.netmask", "s", {title: "Static Netmask"}]
2122
- ["eth.gw", "s", {title: "Static Default Gateway"}]
2223

24+
conds:
25+
- when: mos.platform == "esp32"
26+
apply:
27+
libs:
28+
- location: https://github.com/mongoose-os-libs/lwip
29+
config_schema:
30+
- ["eth.dhcp_hostname", "s", {title: "Host name to include in DHCP requests"}]
31+
- ["eth.clk_mode", "i", 0, {title: "50 MHz clock source: 0 in <- GPIO0, 1 out -> GPIO0, 2 out -> GPIO16, 3 out -> GPIO17 (inverted)"}]
32+
- ["eth.mdc_gpio", "i", 23, {title: "GPIO to use for RMII MDC signal"}]
33+
- ["eth.mdio_gpio", "i", 18, {title: "GPIO to use for RMII MDIO signal"}]
34+
- ["eth.phy_pwr_gpio", "i", -1, {title: "GPIO to use for PHY PWR control signal"}]
35+
build_vars:
36+
ESP_IDF_EXTRA_COMPONENTS: >
37+
${build_vars.ESP_IDF_EXTRA_COMPONENTS}
38+
esp_eth
39+
cdefs:
40+
# Select the PHY model. Only one at a time.
41+
MGOS_ETH_PHY_IP101: 0
42+
MGOS_ETH_PHY_RTL8201: 0
43+
MGOS_ETH_PHY_LAN87x0: 1
44+
MGOS_ETH_PHY_DP83848: 0
45+
46+
- when: mos.platform == "stm32"
47+
apply:
48+
libs:
49+
- location: https://github.com/mongoose-os-libs/lwip
50+
config_schema:
51+
# OUI = 12:34:56 happens to be a valid Locally Administered Address prefix. Win!
52+
- ["eth.mac", "s", "12:34:56:??:??:??", {title: "MAC address. ?? are replaced with bits from chip's unique ID."}]
53+
- ["eth.speed", "s", "auto", {title: "Speed and duplex selection: auto, 10HD, 10FD, 100HD, 100FD."}]
54+
- ["eth.mtu", "i", 1460, {title: "Interface MTU"}]
55+
build_vars:
56+
MGOS_ENABLE_LWIP: 1
57+
2358
tags:
2459
- c
2560
- net
2661
- docs:net:Ethernet
2762

28-
manifest_version: 2017-09-29
63+
no_implicit_init_deps: true
64+
manifest_version: 2018-06-20

mos_esp32.yml

Lines changed: 0 additions & 21 deletions
This file was deleted.

mos_stm32.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/esp32/esp32_eth.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,12 +142,15 @@ bool mgos_ethernet_init(void) {
142142
uint8_t mac_addr[6];
143143
mac->get_addr(mac, mac_addr);
144144

145-
esp_netif_ip_info_t static_ip = {0};
146-
if (!mgos_eth_get_static_ip_config((ip4_addr_t *) &static_ip.ip,
147-
(ip4_addr_t *) &static_ip.netmask,
148-
(ip4_addr_t *) &static_ip.gw)) {
145+
struct sockaddr_in ip = {0}, netmask = {0}, gw = {0};
146+
if (!mgos_eth_get_static_ip_config(&ip, &netmask, &gw)) {
149147
goto out;
150148
}
149+
esp_netif_ip_info_t static_ip = {
150+
.ip.addr = ip.sin_addr.s_addr,
151+
.netmask.addr = netmask.sin_addr.s_addr,
152+
.gw.addr = gw.sin_addr.s_addr,
153+
};
151154

152155
bool is_dhcp =
153156
(static_ip.ip.addr == IPADDR_ANY || static_ip.netmask.addr == IPADDR_ANY);

src/mgos_eth.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020
#include <stdlib.h>
2121
#include <string.h>
2222

23-
#include "lwip/ip_addr.h"
24-
2523
#include "common/cs_dbg.h"
2624

25+
#include "mgos_net.h"
2726
#include "mgos_sys_config.h"
2827

2928
const char *mgos_eth_speed_str(enum mgos_eth_speed speed) {
@@ -88,8 +87,9 @@ bool mgos_eth_phy_opts_from_str(const char *str,
8887
return true;
8988
}
9089

91-
bool mgos_eth_get_static_ip_config(ip4_addr_t *ip, ip4_addr_t *netmask,
92-
ip4_addr_t *gw) {
90+
bool mgos_eth_get_static_ip_config(struct sockaddr_in *ip,
91+
struct sockaddr_in *netmask,
92+
struct sockaddr_in *gw) {
9393
bool res = false;
9494
memset(ip, 0, sizeof(*ip));
9595
memset(netmask, 0, sizeof(*netmask));
@@ -100,17 +100,17 @@ bool mgos_eth_get_static_ip_config(ip4_addr_t *ip, ip4_addr_t *netmask,
100100
goto clean;
101101
}
102102

103-
if (!ip4addr_aton(mgos_sys_config_get_eth_ip(), ip)) {
103+
if (!mgos_net_str_to_ip(mgos_sys_config_get_eth_ip(), ip)) {
104104
LOG(LL_ERROR, ("Invalid eth.ip!"));
105105
goto clean;
106106
}
107107
if (mgos_sys_config_get_eth_netmask() == NULL ||
108-
!ip4addr_aton(mgos_sys_config_get_eth_netmask(), netmask)) {
108+
!mgos_net_str_to_ip(mgos_sys_config_get_eth_netmask(), netmask)) {
109109
LOG(LL_ERROR, ("Invalid eth.netmask!"));
110110
goto clean;
111111
}
112112
if (mgos_sys_config_get_eth_gw() != NULL &&
113-
!ip4addr_aton(mgos_sys_config_get_eth_gw(), gw)) {
113+
!mgos_net_str_to_ip(mgos_sys_config_get_eth_gw(), gw)) {
114114
LOG(LL_ERROR, ("Invalid eth.gw!"));
115115
goto clean;
116116
}

src/stm32/stm32_eth.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,15 @@ bool mgos_ethernet_init(void) {
8989
opts.phy_addr = mgos_sys_config_get_eth_phy_addr();
9090
opts.mtu = mgos_sys_config_get_eth_mtu();
9191

92-
ip4_addr_t ip, netmask, gw;
93-
if (!mgos_eth_get_static_ip_config(&ip, &netmask, &gw)) {
92+
struct sockaddr_in sip, snetmask, sgw;
93+
if (!mgos_eth_get_static_ip_config(&sip, &snetmask, &sgw)) {
9494
goto clean;
9595
}
9696

97+
ip4_addr_t ip = {.addr = sip.sin_addr.s_addr};
98+
ip4_addr_t netmask = {.addr = snetmask.sin_addr.s_addr};
99+
ip4_addr_t gw = {.addr = sgw.sin_addr.s_addr};
100+
97101
struct netif *netif = (struct netif *) calloc(1, sizeof(*netif));
98102
if (netif_add(netif, &ip, &netmask, &gw, &opts, stm32_eth_netif_init,
99103
ethernet_input) == NULL) {

src/ubuntu/ubuntu_eth.c

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright 2019 Google Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <ifaddrs.h>
18+
#include <netdb.h>
19+
20+
#include "ubuntu_eth.h"
21+
22+
#include "mgos_hal.h"
23+
#include "mgos_mongoose.h"
24+
#include "mgos_net_hal.h"
25+
#include "ubuntu.h"
26+
#include "ubuntu_ipc.h"
27+
28+
bool ubuntu_get_default_gateway(char *dev, size_t devlen,
29+
struct sockaddr_in *gw) {
30+
int fd;
31+
FILE *f = NULL;
32+
char line[100], *p, *c, *g, *saveptr;
33+
bool ret = false;
34+
35+
if (!dev || !gw) {
36+
return false;
37+
}
38+
39+
fd = ubuntu_ipc_open("/proc/net/route", O_RDONLY);
40+
if (fd < 0) {
41+
LOG(LL_ERROR, ("Could not open /proc/net/route"));
42+
return false;
43+
}
44+
f = fdopen(fd, "r");
45+
if (!f) {
46+
return false;
47+
}
48+
49+
while (fgets(line, 100, f)) {
50+
p = strtok_r(line, " \t", &saveptr);
51+
c = strtok_r(NULL, " \t", &saveptr);
52+
g = strtok_r(NULL, " \t", &saveptr);
53+
54+
if (p != NULL && c != NULL) {
55+
if (strcmp(c, "00000000") == 0) {
56+
strncpy(dev, p, devlen - 1);
57+
dev[devlen - 1] = 0;
58+
if (g) {
59+
char *pEnd;
60+
int ng = strtol(g, &pEnd, 16);
61+
gw->sin_addr.s_addr = ng;
62+
gw->sin_port = 0;
63+
gw->sin_family = AF_INET;
64+
ret = true;
65+
}
66+
break;
67+
}
68+
}
69+
}
70+
71+
fclose(f);
72+
close(fd);
73+
return ret;
74+
}
75+
76+
// Will always return the IP address which has a default gateway attached,
77+
// regardless of 'if_instance'.
78+
bool mgos_eth_dev_get_ip_info(int if_instance,
79+
struct mgos_net_ip_info *ip_info) {
80+
struct ifaddrs *ifaddr, *ifa;
81+
char gw_dev[64];
82+
struct sockaddr_in gw;
83+
84+
if (ip_info == NULL) return false;
85+
86+
memset(ip_info, 0, sizeof(*ip_info));
87+
88+
if (!ubuntu_get_default_gateway(gw_dev, sizeof(gw_dev), &gw)) {
89+
memcpy(gw_dev, "lo", 3);
90+
}
91+
92+
LOG(LL_INFO, ("External interface: %s", gw_dev));
93+
94+
if (getifaddrs(&ifaddr) == -1) {
95+
LOG(LL_ERROR, ("Cannot get interfaces"));
96+
return false;
97+
}
98+
99+
bool found = false;
100+
for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) {
101+
char host[NI_MAXHOST], netmask[NI_MAXHOST], gateway[NI_MAXHOST];
102+
if (ifa->ifa_addr == NULL || strcmp(gw_dev, ifa->ifa_name) != 0) {
103+
continue;
104+
}
105+
if (0 != getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in), host,
106+
NI_MAXHOST, NULL, 0, NI_NUMERICHOST)) {
107+
continue;
108+
}
109+
if (0 != getnameinfo(ifa->ifa_netmask, sizeof(struct sockaddr_in), netmask,
110+
NI_MAXHOST, NULL, 0, NI_NUMERICHOST)) {
111+
continue;
112+
}
113+
memcpy((void *) &ip_info->ip, (void *) ifa->ifa_addr,
114+
sizeof(struct sockaddr_in));
115+
memcpy((void *) &ip_info->netmask, (void *) ifa->ifa_netmask,
116+
sizeof(struct sockaddr_in));
117+
if (getnameinfo((const struct sockaddr *) &gw, sizeof(gw), gateway,
118+
NI_MAXHOST, NULL, 0, NI_NUMERICHOST) == 0) {
119+
memcpy((void *) &ip_info->gw, (void *) &gw, sizeof(gw));
120+
}
121+
found = true;
122+
break;
123+
}
124+
125+
freeifaddrs(ifaddr);
126+
127+
if (!found) {
128+
LOG(LL_ERROR, ("Failed to get interface configuration"));
129+
}
130+
131+
return found;
132+
133+
(void) if_instance;
134+
}
135+
136+
bool mgos_ethernet_init(void) {
137+
return true;
138+
}

0 commit comments

Comments
 (0)