Skip to content

Commit 0e70548

Browse files
committed
Refactor MIP strategy and first driver (CMSIS)
1 parent 2807e98 commit 0e70548

File tree

11 files changed

+108
-108
lines changed

11 files changed

+108
-108
lines changed

mongoose.c

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,6 @@ void mg_resolve(struct mg_connection *c, const char *url) {
398398
}
399399
}
400400

401-
#if MG_ENABLE_MDNS
402401
static const uint8_t mdns_answer[] = {
403402
0, 1, // 2 bytes - record type, A
404403
0, 1, // 2 bytes - address class, INET
@@ -460,14 +459,13 @@ static void mdns_cb(struct mg_connection *c, int ev, void *ev_data) {
460459
(void) ev_data;
461460
}
462461

463-
void mg_mcast_add(char *ip, MG_SOCKET_TYPE fd);
462+
void mg_multicast_add(struct mg_connection *c, char *ip);
464463
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, char *name) {
465464
struct mg_connection *c =
466465
mg_listen(mgr, "udp://224.0.0.251:5353", mdns_cb, name);
467-
if (c != NULL) mg_mcast_add("224.0.0.251", (MG_SOCKET_TYPE) (size_t) c->fd);
466+
if (c != NULL) mg_multicast_add(c, "224.0.0.251");
468467
return c;
469468
}
470-
#endif
471469

472470
#ifdef MG_ENABLE_LINES
473471
#line 1 "src/event.c"
@@ -4446,13 +4444,13 @@ static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req,
44464444
uint8_t extra = (uint8_t) ((ifp->enable_req_dns ? 1 : 0) +
44474445
(ifp->enable_req_sntp ? 1 : 0));
44484446
size_t len = strlen(ifp->dhcp_name);
4449-
size_t olen = 21 + len + extra + 2 + 1; // Total length of options
4450-
#define OPTS_MAXLEN (21 + sizeof(ifp->dhcp_name) + 2 + 2 + 1)
4451-
uint8_t opts[OPTS_MAXLEN]; // Allocate options (max size possible)
4447+
size_t olen = 21 + len + extra + 2 + 1; // Total length of options
4448+
#define OPTS_MAXLEN (21 + sizeof(ifp->dhcp_name) + 2 + 2 + 1)
4449+
uint8_t opts[OPTS_MAXLEN]; // Allocate options (max size possible)
44524450
uint8_t *p = opts;
44534451
assert(olen <= sizeof(opts));
44544452
memset(opts, 0, sizeof(opts));
4455-
*p++ = 53, *p++ = 1, *p++ = 3; // Type: DHCP request
4453+
*p++ = 53, *p++ = 1, *p++ = 3; // Type: DHCP request
44564454
*p++ = 54, *p++ = 4, memcpy(p, &ip_srv, 4), p += 4; // DHCP server ID
44574455
*p++ = 50, *p++ = 4, memcpy(p, &ip_req, 4), p += 4; // Requested IP
44584456
*p++ = 12, *p++ = (uint8_t) (len & 255); // DHCP host
@@ -5252,6 +5250,13 @@ static void mac_resolved(struct mg_connection *c) {
52525250
}
52535251
}
52545252

5253+
static void ip4_mcastmac(uint8_t *mac, uint32_t *ip) {
5254+
uint8_t mcastp[3] = {0x01, 0x00, 0x5E}; // multicast group MAC
5255+
memcpy(mac, mcastp, 3);
5256+
memcpy(mac + 3, ((uint8_t *) ip) + 1, 3); // 23 LSb
5257+
mac[3] &= 0x7F;
5258+
}
5259+
52555260
void mg_connect_resolved(struct mg_connection *c) {
52565261
struct mg_tcpip_if *ifp = c->mgr->ifp;
52575262
uint32_t rem_ip;
@@ -5277,10 +5282,7 @@ void mg_connect_resolved(struct mg_connection *c) {
52775282
c->is_arplooking = 1;
52785283
} else if ((*((uint8_t *) &rem_ip) & 0xE0) == 0xE0) {
52795284
struct connstate *s = (struct connstate *) (c + 1); // 224 to 239, E0 to EF
5280-
uint8_t mcastp[3] = {0x01, 0x00, 0x5E}; // multicast group
5281-
memcpy(s->mac, mcastp, 3);
5282-
memcpy(s->mac + 3, ((uint8_t *) &rem_ip) + 1, 3); // 23 LSb
5283-
s->mac[3] &= 0x7F;
5285+
ip4_mcastmac(s->mac, &rem_ip); // multicast group
52845286
mac_resolved(c);
52855287
} else {
52865288
struct connstate *s = (struct connstate *) (c + 1);
@@ -5383,11 +5385,12 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
53835385
return res;
53845386
}
53855387

5386-
void mg_mcast_add(char *ip, MG_SOCKET_TYPE fd) { (void) ip; (void) fd; }
5387-
5388-
#if MG_TCPIP_MCAST
5389-
const uint8_t mcast_addr[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb};
5390-
#endif
5388+
uint8_t mcast_addr[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb};
5389+
void mg_multicast_add(struct mg_connection *c, char *ip) {
5390+
(void) ip; // ip4_mcastmac(mcast_mac, &ip);
5391+
// TODO(): actual IP -> MAC; check database, update
5392+
c->mgr->ifp->update_mac_hash_table = true; // mark dirty
5393+
}
53915394

53925395
#endif // MG_ENABLE_TCPIP
53935396

@@ -8381,8 +8384,7 @@ static void mg_set_non_blocking_mode(MG_SOCKET_TYPE fd) {
83818384
#endif
83828385
}
83838386

8384-
#if MG_ENABLE_MDNS
8385-
void mg_mcast_add(char *ip, MG_SOCKET_TYPE fd) {
8387+
void mg_multicast_add(struct mg_connection *c, char *ip) {
83868388
#if MG_ENABLE_RL
83878389
#error UNSUPPORTED
83888390
#elif MG_ENABLE_FREERTOS_TCP
@@ -8392,10 +8394,9 @@ void mg_mcast_add(char *ip, MG_SOCKET_TYPE fd) {
83928394
struct ip_mreq mreq;
83938395
mreq.imr_multiaddr.s_addr = inet_addr(ip);
83948396
mreq.imr_interface.s_addr = mg_htonl(INADDR_ANY);
8395-
setsockopt(fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq));
8397+
setsockopt(FD(c), IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq));
83968398
#endif
83978399
}
8398-
#endif
83998400

84008401
bool mg_open_listener(struct mg_connection *c, const char *url) {
84018402
MG_SOCKET_TYPE fd = MG_INVALID_SOCKET;
@@ -19822,10 +19823,6 @@ static bool cmsis_init(struct mg_tcpip_if *ifp) {
1982219823
memcpy(&addr, ifp->mac, sizeof(addr));
1982319824
mac->SetMacAddress(&addr);
1982419825
}
19825-
#if MG_TCPIP_MCAST
19826-
memcpy(&addr, mcast_addr, sizeof(addr));
19827-
mac->SetAddressFilter(&addr, 1);
19828-
#endif
1982919826
phy->PowerControl(ARM_POWER_FULL);
1983019827
phy->SetInterface(cap.media_interface);
1983119828
phy->SetMode(ARM_ETH_PHY_AUTO_NEGOTIATE);
@@ -19842,7 +19839,20 @@ static size_t cmsis_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
1984219839
return len;
1984319840
}
1984419841

19842+
static void cmsis_update_hash_table(struct mg_tcpip_if *ifp) {
19843+
// TODO(): read database, rebuild hash table
19844+
ARM_DRIVER_ETH_MAC *mac = &Driver_ETH_MAC0;
19845+
ARM_ETH_MAC_ADDR addr;
19846+
memcpy(&addr, mcast_addr, sizeof(addr));
19847+
mac->SetAddressFilter(&addr, 1);
19848+
(void) ifp;
19849+
}
19850+
1984519851
static bool cmsis_poll(struct mg_tcpip_if *ifp, bool s1) {
19852+
if (ifp->update_mac_hash_table) {
19853+
cmsis_update_hash_table(ifp);
19854+
ifp->update_mac_hash_table = false;
19855+
}
1984619856
if (!s1) return false;
1984719857
ARM_DRIVER_ETH_PHY *phy = &Driver_ETH_PHY0;
1984819858
ARM_DRIVER_ETH_MAC *mac = &Driver_ETH_MAC0;
@@ -20689,10 +20699,6 @@ static bool cyw_init(uint8_t *mac) {
2068920699
MG_ERROR(("read MAC failed"));
2069020700
}
2069120701
}
20692-
#if MG_TCPIP_MCAST
20693-
val = 1; if (!cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *)&val, sizeof(val), (uint8_t *)mcast_addr, sizeof(mcast_addr))) return false;
20694-
mg_delayms(50);
20695-
#endif
2069620702
return true;
2069720703
}
2069820704
// clang-format on
@@ -21093,6 +21099,11 @@ bool mg_wifi_ap_stop(void) {
2109321099
return cyw_wifi_ap_stop();
2109421100
}
2109521101

21102+
void mg_tcpip_driver_multicast_add(const uint8_t mcast_addr) {
21103+
val = 1; cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *)&val, sizeof(val), (uint8_t *)mcast_addr, sizeof(mcast_addr));
21104+
//mg_delayms(50);
21105+
}
21106+
2109621107
#endif
2109721108

2109821109
#ifdef MG_ENABLE_LINES
@@ -22795,7 +22806,7 @@ static bool mg_tcpip_driver_stm32f_init(struct mg_tcpip_if *ifp) {
2279522806
ETH->MACA0LR = (uint32_t) (ifp->mac[3] << 24) |
2279622807
((uint32_t) ifp->mac[2] << 16) |
2279722808
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
22798-
#if MG_TCPIP_MCAST
22809+
#if 0 //MG_TCPIP_MCAST
2279922810
// enable multicast
2280022811
ETH->MACA1LR = (uint32_t) mcast_addr[3] << 24 |
2280122812
(uint32_t) mcast_addr[2] << 16 |

mongoose.h

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -937,19 +937,6 @@ struct timeval {
937937
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
938938
#endif
939939

940-
#ifndef MG_ENABLE_MDNS
941-
#define MG_ENABLE_MDNS 0
942-
#endif
943-
944-
#if MG_ENABLE_TCPIP
945-
#if MG_ENABLE_MDNS
946-
#undef MG_TCPIP_MCAST
947-
#define MG_TCPIP_MCAST 1
948-
#elif !defined(MG_TCPIP_MCAST)
949-
#define MG_TCPIP_MCAST 0
950-
#endif
951-
#endif
952-
953940

954941

955942

@@ -2853,19 +2840,20 @@ typedef void (*mg_tcpip_event_handler_t)(struct mg_tcpip_if *ifp, int ev,
28532840
void *ev_data);
28542841

28552842
enum {
2856-
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
2857-
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
2858-
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
2859-
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
2860-
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
2861-
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct mg_wifi_scan_bss_data *
2862-
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
2863-
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and chip specific
2864-
MG_TCPIP_EV_DRIVER, // Driver event driver specific
2865-
MG_TCPIP_EV_USER // Starting ID for user events
2843+
MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
2844+
MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
2845+
MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
2846+
MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
2847+
MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
2848+
MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct
2849+
// mg_wifi_scan_bss_data *
2850+
MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
2851+
MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and
2852+
// chip specific
2853+
MG_TCPIP_EV_DRIVER, // Driver event driver specific
2854+
MG_TCPIP_EV_USER // Starting ID for user events
28662855
};
28672856

2868-
28692857
// Network interface
28702858
struct mg_tcpip_if {
28712859
uint8_t mac[6]; // MAC address. Must be set to a valid MAC
@@ -2878,6 +2866,7 @@ struct mg_tcpip_if {
28782866
bool enable_req_sntp; // DCHP client requests SNTP server
28792867
bool enable_crc32_check; // Do a CRC check on RX frames and strip it
28802868
bool enable_mac_check; // Do a MAC check on RX frames
2869+
bool update_mac_hash_table; // Signal drivers to update MAC controller
28812870
struct mg_tcpip_driver *driver; // Low level driver
28822871
void *driver_data; // Driver-specific data
28832872
mg_tcpip_event_handler_t fn; // User-specified event handler function

src/config.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,3 @@
182182
#ifndef MG_ENABLE_TCPIP_PRINT_DEBUG_STATS
183183
#define MG_ENABLE_TCPIP_PRINT_DEBUG_STATS 0
184184
#endif
185-
186-
#ifndef MG_ENABLE_MDNS
187-
#define MG_ENABLE_MDNS 0
188-
#endif
189-
190-
#if MG_ENABLE_TCPIP
191-
#if MG_ENABLE_MDNS
192-
#undef MG_TCPIP_MCAST
193-
#define MG_TCPIP_MCAST 1
194-
#elif !defined(MG_TCPIP_MCAST)
195-
#define MG_TCPIP_MCAST 0
196-
#endif
197-
#endif

src/dns.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ void mg_resolve(struct mg_connection *c, const char *url) {
277277
}
278278
}
279279

280-
#if MG_ENABLE_MDNS
281280
static const uint8_t mdns_answer[] = {
282281
0, 1, // 2 bytes - record type, A
283282
0, 1, // 2 bytes - address class, INET
@@ -339,11 +338,10 @@ static void mdns_cb(struct mg_connection *c, int ev, void *ev_data) {
339338
(void) ev_data;
340339
}
341340

342-
void mg_mcast_add(char *ip, MG_SOCKET_TYPE fd);
341+
void mg_multicast_add(struct mg_connection *c, char *ip);
343342
struct mg_connection *mg_mdns_listen(struct mg_mgr *mgr, char *name) {
344343
struct mg_connection *c =
345344
mg_listen(mgr, "udp://224.0.0.251:5353", mdns_cb, name);
346-
if (c != NULL) mg_mcast_add("224.0.0.251", (MG_SOCKET_TYPE) (size_t) c->fd);
345+
if (c != NULL) mg_multicast_add(c, "224.0.0.251");
347346
return c;
348347
}
349-
#endif

src/drivers/cmsis.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ static bool cmsis_init(struct mg_tcpip_if *ifp) {
4040
memcpy(&addr, ifp->mac, sizeof(addr));
4141
mac->SetMacAddress(&addr);
4242
}
43-
#if MG_TCPIP_MCAST
44-
memcpy(&addr, mcast_addr, sizeof(addr));
45-
mac->SetAddressFilter(&addr, 1);
46-
#endif
4743
phy->PowerControl(ARM_POWER_FULL);
4844
phy->SetInterface(cap.media_interface);
4945
phy->SetMode(ARM_ETH_PHY_AUTO_NEGOTIATE);
@@ -60,7 +56,20 @@ static size_t cmsis_tx(const void *buf, size_t len, struct mg_tcpip_if *ifp) {
6056
return len;
6157
}
6258

59+
static void cmsis_update_hash_table(struct mg_tcpip_if *ifp) {
60+
// TODO(): read database, rebuild hash table
61+
ARM_DRIVER_ETH_MAC *mac = &Driver_ETH_MAC0;
62+
ARM_ETH_MAC_ADDR addr;
63+
memcpy(&addr, mcast_addr, sizeof(addr));
64+
mac->SetAddressFilter(&addr, 1);
65+
(void) ifp;
66+
}
67+
6368
static bool cmsis_poll(struct mg_tcpip_if *ifp, bool s1) {
69+
if (ifp->update_mac_hash_table) {
70+
cmsis_update_hash_table(ifp);
71+
ifp->update_mac_hash_table = false;
72+
}
6473
if (!s1) return false;
6574
ARM_DRIVER_ETH_PHY *phy = &Driver_ETH_PHY0;
6675
ARM_DRIVER_ETH_MAC *mac = &Driver_ETH_MAC0;

src/drivers/cyw.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -784,10 +784,6 @@ static bool cyw_init(uint8_t *mac) {
784784
MG_ERROR(("read MAC failed"));
785785
}
786786
}
787-
#if MG_TCPIP_MCAST
788-
val = 1; if (!cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *)&val, sizeof(val), (uint8_t *)mcast_addr, sizeof(mcast_addr))) return false;
789-
mg_delayms(50);
790-
#endif
791787
return true;
792788
}
793789
// clang-format on
@@ -1188,4 +1184,9 @@ bool mg_wifi_ap_stop(void) {
11881184
return cyw_wifi_ap_stop();
11891185
}
11901186

1187+
void mg_tcpip_driver_multicast_add(const uint8_t mcast_addr) {
1188+
val = 1; cyw_ioctl_iovar_set2_(0, "mcast_list", (uint8_t *)&val, sizeof(val), (uint8_t *)mcast_addr, sizeof(mcast_addr));
1189+
//mg_delayms(50);
1190+
}
1191+
11911192
#endif

src/drivers/stm32f.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static bool mg_tcpip_driver_stm32f_init(struct mg_tcpip_if *ifp) {
152152
ETH->MACA0LR = (uint32_t) (ifp->mac[3] << 24) |
153153
((uint32_t) ifp->mac[2] << 16) |
154154
((uint32_t) ifp->mac[1] << 8) | ifp->mac[0];
155-
#if MG_TCPIP_MCAST
155+
#if 0 //MG_TCPIP_MCAST
156156
// enable multicast
157157
ETH->MACA1LR = (uint32_t) mcast_addr[3] << 24 |
158158
(uint32_t) mcast_addr[2] << 16 |

src/net_builtin.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -283,13 +283,13 @@ static void tx_dhcp_request_sel(struct mg_tcpip_if *ifp, uint32_t ip_req,
283283
uint8_t extra = (uint8_t) ((ifp->enable_req_dns ? 1 : 0) +
284284
(ifp->enable_req_sntp ? 1 : 0));
285285
size_t len = strlen(ifp->dhcp_name);
286-
size_t olen = 21 + len + extra + 2 + 1; // Total length of options
287-
#define OPTS_MAXLEN (21 + sizeof(ifp->dhcp_name) + 2 + 2 + 1)
288-
uint8_t opts[OPTS_MAXLEN]; // Allocate options (max size possible)
286+
size_t olen = 21 + len + extra + 2 + 1; // Total length of options
287+
#define OPTS_MAXLEN (21 + sizeof(ifp->dhcp_name) + 2 + 2 + 1)
288+
uint8_t opts[OPTS_MAXLEN]; // Allocate options (max size possible)
289289
uint8_t *p = opts;
290290
assert(olen <= sizeof(opts));
291291
memset(opts, 0, sizeof(opts));
292-
*p++ = 53, *p++ = 1, *p++ = 3; // Type: DHCP request
292+
*p++ = 53, *p++ = 1, *p++ = 3; // Type: DHCP request
293293
*p++ = 54, *p++ = 4, memcpy(p, &ip_srv, 4), p += 4; // DHCP server ID
294294
*p++ = 50, *p++ = 4, memcpy(p, &ip_req, 4), p += 4; // Requested IP
295295
*p++ = 12, *p++ = (uint8_t) (len & 255); // DHCP host
@@ -1089,6 +1089,13 @@ static void mac_resolved(struct mg_connection *c) {
10891089
}
10901090
}
10911091

1092+
static void ip4_mcastmac(uint8_t *mac, uint32_t *ip) {
1093+
uint8_t mcastp[3] = {0x01, 0x00, 0x5E}; // multicast group MAC
1094+
memcpy(mac, mcastp, 3);
1095+
memcpy(mac + 3, ((uint8_t *) ip) + 1, 3); // 23 LSb
1096+
mac[3] &= 0x7F;
1097+
}
1098+
10921099
void mg_connect_resolved(struct mg_connection *c) {
10931100
struct mg_tcpip_if *ifp = c->mgr->ifp;
10941101
uint32_t rem_ip;
@@ -1114,10 +1121,7 @@ void mg_connect_resolved(struct mg_connection *c) {
11141121
c->is_arplooking = 1;
11151122
} else if ((*((uint8_t *) &rem_ip) & 0xE0) == 0xE0) {
11161123
struct connstate *s = (struct connstate *) (c + 1); // 224 to 239, E0 to EF
1117-
uint8_t mcastp[3] = {0x01, 0x00, 0x5E}; // multicast group
1118-
memcpy(s->mac, mcastp, 3);
1119-
memcpy(s->mac + 3, ((uint8_t *) &rem_ip) + 1, 3); // 23 LSb
1120-
s->mac[3] &= 0x7F;
1124+
ip4_mcastmac(s->mac, &rem_ip); // multicast group
11211125
mac_resolved(c);
11221126
} else {
11231127
struct connstate *s = (struct connstate *) (c + 1);
@@ -1220,9 +1224,11 @@ bool mg_send(struct mg_connection *c, const void *buf, size_t len) {
12201224
return res;
12211225
}
12221226

1223-
#if MG_TCPIP_MCAST
1224-
const uint8_t mcast_addr[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb};
1225-
void mg_mcast_add(char *ip, MG_SOCKET_TYPE fd) { (void) ip; (void) fd; }
1226-
#endif
1227+
uint8_t mcast_addr[6] = {0x01, 0x00, 0x5e, 0x00, 0x00, 0xfb};
1228+
void mg_multicast_add(struct mg_connection *c, char *ip) {
1229+
(void) ip; // ip4_mcastmac(mcast_mac, &ip);
1230+
// TODO(): actual IP -> MAC; check database, update
1231+
c->mgr->ifp->update_mac_hash_table = true; // mark dirty
1232+
}
12271233

12281234
#endif // MG_ENABLE_TCPIP

0 commit comments

Comments
 (0)