@@ -1057,6 +1057,26 @@ struct timeval {
1057
1057
#define MG_TCPIP_GW MG_IPV4 (0 , 0 , 0 , 0 ) // Default is 0.0.0.0 (DHCP)
1058
1058
#endif
1059
1059
1060
+ #if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
1061
+
1062
+ #ifndef MG_TCPIP_GLOBAL
1063
+ #define MG_TCPIP_GLOBAL MG_IPV6 (0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
1064
+ #endif
1065
+
1066
+ #ifndef MG_TCPIP_LINK_LOCAL
1067
+ #define MG_TCPIP_LINK_LOCAL MG_IPV6 (0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
1068
+ #endif
1069
+
1070
+ #ifndef MG_TCPIP_PREFIX_LEN
1071
+ #define MG_TCPIP_PREFIX_LEN 0
1072
+ #endif
1073
+
1074
+ #ifndef MG_TCPIP_GW6
1075
+ #define MG_TCPIP_GW6 MG_IPV6 (0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 )
1076
+ #endif
1077
+
1078
+ #endif
1079
+
1060
1080
#ifndef MG_SET_MAC_ADDRESS
1061
1081
#define MG_SET_MAC_ADDRESS (mac )
1062
1082
#endif
@@ -1369,6 +1389,16 @@ void mg_delayms(unsigned int ms);
1369
1389
1370
1390
#define MG_IPV4 (a, b, c, d ) mg_htonl(MG_U32(a, b, c, d))
1371
1391
1392
+ #define MG_IPV6 (a, b, c, d, e, f, g ,h ) \
1393
+ { (uint8_t )((a)>>8 ),(uint8_t )(a), \
1394
+ (uint8_t )((b)>>8 ),(uint8_t )(b), \
1395
+ (uint8_t )((c)>>8 ),(uint8_t )(c), \
1396
+ (uint8_t )((d)>>8 ),(uint8_t )(d), \
1397
+ (uint8_t )((e)>>8 ),(uint8_t )(e), \
1398
+ (uint8_t )((f)>>8 ),(uint8_t )(f), \
1399
+ (uint8_t )((g)>>8 ),(uint8_t )(g), \
1400
+ (uint8_t )((h)>>8 ),(uint8_t )(h) }
1401
+
1372
1402
// For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
1373
1403
#define MG_U8P (ADDR ) ((uint8_t *) (ADDR))
1374
1404
#define MG_IPADDR_PARTS (ADDR ) \
@@ -1383,6 +1413,14 @@ void mg_delayms(unsigned int ms);
1383
1413
((uint32_t ) (((uint32_t ) MG_U8P(p)[0 ] << 24U ) | \
1384
1414
((uint32_t ) MG_U8P(p)[1 ] << 16U ) | \
1385
1415
((uint32_t ) MG_U8P(p)[2 ] << 8U ) | MG_U8P(p)[3 ]))
1416
+ #define MG_LOAD_BE64 (p ) \
1417
+ ((uint64_t ) (((uint64_t ) MG_U8P(p)[0 ] << 56U ) | \
1418
+ ((uint64_t ) MG_U8P(p)[1 ] << 48U ) | \
1419
+ ((uint64_t ) MG_U8P(p)[2 ] << 40U ) | \
1420
+ ((uint64_t ) MG_U8P(p)[3 ] << 32U ) | \
1421
+ ((uint64_t ) MG_U8P(p)[4 ] << 24U ) | \
1422
+ ((uint64_t ) MG_U8P(p)[5 ] << 16U ) | \
1423
+ ((uint64_t ) MG_U8P(p)[6 ] << 8U ) | MG_U8P(p)[7 ]))
1386
1424
#define MG_STORE_BE16 (p, n ) \
1387
1425
do { \
1388
1426
MG_U8P (p)[0 ] = ((n) >> 8U ) & 255 ; \
@@ -1401,11 +1439,24 @@ void mg_delayms(unsigned int ms);
1401
1439
MG_U8P (p)[2 ] = ((n) >> 8U ) & 255 ; \
1402
1440
MG_U8P (p)[3 ] = (n) &255 ; \
1403
1441
} while (0 )
1442
+ #define MG_STORE_BE64 (p, n ) \
1443
+ do { \
1444
+ MG_U8P (p)[0 ] = ((n) >> 56U ) & 255 ; \
1445
+ MG_U8P (p)[1 ] = ((n) >> 48U ) & 255 ; \
1446
+ MG_U8P (p)[2 ] = ((n) >> 40U ) & 255 ; \
1447
+ MG_U8P (p)[3 ] = ((n) >> 32U ) & 255 ; \
1448
+ MG_U8P (p)[4 ] = ((n) >> 24U ) & 255 ; \
1449
+ MG_U8P (p)[5 ] = ((n) >> 16U ) & 255 ; \
1450
+ MG_U8P (p)[6 ] = ((n) >> 8U ) & 255 ; \
1451
+ MG_U8P (p)[7 ] = (n) &255 ; \
1452
+ } while (0 )
1404
1453
1405
1454
uint16_t mg_ntohs (uint16_t net);
1406
1455
uint32_t mg_ntohl (uint32_t net);
1456
+ uint64_t mg_ntohll (uint64_t net);
1407
1457
#define mg_htons (x ) mg_ntohs(x)
1408
1458
#define mg_htonl (x ) mg_ntohl(x)
1459
+ #define mg_htonll (x ) mg_ntohll(x)
1409
1460
1410
1461
#define MG_REG (x ) ((volatile uint32_t *) (x))[0 ]
1411
1462
#define MG_BIT (x ) (((uint32_t ) 1U ) << (x))
@@ -3086,18 +3137,17 @@ typedef void (*mg_tcpip_event_handler_t)(struct mg_tcpip_if *ifp, int ev,
3086
3137
void *ev_data);
3087
3138
3088
3139
enum {
3089
- MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
3090
- MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
3091
- MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
3092
- MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
3093
- MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
3094
- MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct
3095
- // mg_wifi_scan_bss_data *
3096
- MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
3097
- MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and
3098
- // chip specific
3099
- MG_TCPIP_EV_DRIVER, // Driver event driver specific
3100
- MG_TCPIP_EV_USER // Starting ID for user events
3140
+ MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
3141
+ MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
3142
+ MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
3143
+ MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
3144
+ MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
3145
+ MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct mg_wifi_scan_bss_data *
3146
+ MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
3147
+ MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and chip specific
3148
+ MG_TCPIP_EV_DRIVER, // Driver event driver specific
3149
+ MG_TCPIP_EV_ST6_CHG, // state6 change uint8_t * (&ifp->state6)
3150
+ MG_TCPIP_EV_USER // Starting ID for user events
3101
3151
};
3102
3152
3103
3153
// Network interface
@@ -3122,6 +3172,13 @@ struct mg_tcpip_if {
3122
3172
char dhcp_name[MG_TCPIP_DHCPNAME_SIZE]; // Name for DHCP, "mip" if unset
3123
3173
uint16_t mtu; // Interface MTU
3124
3174
#define MG_TCPIP_MTU_DEFAULT 1500
3175
+ #if MG_ENABLE_IPV6
3176
+ uint64_t ip6ll[2 ], ip6[2 ]; // IPv6 link-local and global addresses
3177
+ uint8_t prefix_len; // Prefix length
3178
+ uint64_t gw6[2 ]; // Default gateway
3179
+ bool enable_slaac; // Enable IPv6 address autoconfiguration
3180
+ bool enable_dhcp6_client; // Enable DCHPv6 client
3181
+ #endif
3125
3182
3126
3183
// Internal state, user can use it but should not change it
3127
3184
uint8_t gwmac[6 ]; // Router's MAC
@@ -3134,14 +3191,17 @@ struct mg_tcpip_if {
3134
3191
volatile uint32_t nrecv; // Number of received frames
3135
3192
volatile uint32_t nsent; // Number of transmitted frames
3136
3193
volatile uint32_t nerr; // Number of driver errors
3137
- uint8_t state; // Current state
3194
+ uint8_t state; // Current link and IPv4 state
3138
3195
#define MG_TCPIP_STATE_DOWN 0 // Interface is down
3139
3196
#define MG_TCPIP_STATE_UP 1 // Interface is up
3140
3197
#define MG_TCPIP_STATE_REQ 2 // Interface is up, DHCP REQUESTING state
3141
3198
#define MG_TCPIP_STATE_IP 3 // Interface is up and has an IP assigned
3142
3199
#define MG_TCPIP_STATE_READY 4 // Interface has fully come up, ready to work
3200
+ #if MG_ENABLE_IPV6
3201
+ uint8_t gw6mac[6 ]; // IPv6 Router's MAC
3202
+ uint8_t state6; // Current IPv6 state
3203
+ #endif
3143
3204
};
3144
-
3145
3205
void mg_tcpip_init (struct mg_mgr *, struct mg_tcpip_if *);
3146
3206
void mg_tcpip_free (struct mg_tcpip_if *);
3147
3207
void mg_tcpip_qwrite (void *buf, size_t len, struct mg_tcpip_if *ifp);
@@ -3635,6 +3695,18 @@ struct mg_tcpip_driver_stm32h_data {
3635
3695
#define MG_ENABLE_ETH_IRQ ()
3636
3696
#endif
3637
3697
3698
+ #if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
3699
+ #define MG_IPV6_INIT (mif ) \
3700
+ do { \
3701
+ memcpy (mif.ip6ll , (uint8_t [16 ]) MG_TCPIP_LINK_LOCAL, 16 ); \
3702
+ memcpy (mif.ip6 , (uint8_t [16 ]) MG_TCPIP_GLOBAL, 16 ); \
3703
+ memcpy (mif.gw6 , (uint8_t [16 ]) MG_TCPIP_GW6, 16 ); \
3704
+ mif.prefix_len = MG_TCPIP_PREFIX_LEN; \
3705
+ } while (0 )
3706
+ #else
3707
+ #define MG_IPV6_INIT (mif )
3708
+ #endif
3709
+
3638
3710
#define MG_TCPIP_DRIVER_INIT (mgr ) \
3639
3711
do { \
3640
3712
static struct mg_tcpip_driver_stm32h_data driver_data_; \
@@ -3648,6 +3720,7 @@ struct mg_tcpip_driver_stm32h_data {
3648
3720
mif_.driver = &mg_tcpip_driver_stm32h; \
3649
3721
mif_.driver_data = &driver_data_; \
3650
3722
MG_SET_MAC_ADDRESS (mif_.mac ); \
3723
+ MG_IPV6_INIT (mif_); \
3651
3724
mg_tcpip_init (mgr, &mif_); \
3652
3725
MG_ENABLE_ETH_IRQ (); \
3653
3726
MG_INFO ((" Driver: stm32h, MAC: %M" , mg_print_mac, mif_.mac )); \
0 commit comments