@@ -1057,6 +1057,26 @@ struct timeval {
10571057#define MG_TCPIP_GW MG_IPV4 (0 , 0 , 0 , 0 ) // Default is 0.0.0.0 (DHCP)
10581058#endif
10591059
1060+ #if 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_IPV6_LINKLOCAL
1067+ #define MG_TCPIP_IPV6_LINKLOCAL 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+
10601080#ifndef MG_SET_MAC_ADDRESS
10611081#define MG_SET_MAC_ADDRESS (mac )
10621082#endif
@@ -1368,6 +1388,16 @@ void mg_delayms(unsigned int ms);
13681388
13691389#define MG_IPV4 (a, b, c, d ) mg_htonl(MG_U32(a, b, c, d))
13701390
1391+ #define MG_IPV6 (a, b, c, d, e, f, g ,h ) \
1392+ { (uint8_t )((a)>>8 ),(uint8_t )(a), \
1393+ (uint8_t )((b)>>8 ),(uint8_t )(b), \
1394+ (uint8_t )((c)>>8 ),(uint8_t )(c), \
1395+ (uint8_t )((d)>>8 ),(uint8_t )(d), \
1396+ (uint8_t )((e)>>8 ),(uint8_t )(e), \
1397+ (uint8_t )((f)>>8 ),(uint8_t )(f), \
1398+ (uint8_t )((g)>>8 ),(uint8_t )(g), \
1399+ (uint8_t )((h)>>8 ),(uint8_t )(h) }
1400+
13711401// For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
13721402#define MG_U8P (ADDR ) ((uint8_t *) (ADDR))
13731403#define MG_IPADDR_PARTS (ADDR ) \
@@ -1382,6 +1412,14 @@ void mg_delayms(unsigned int ms);
13821412 ((uint32_t ) (((uint32_t ) MG_U8P(p)[0 ] << 24U ) | \
13831413 ((uint32_t ) MG_U8P(p)[1 ] << 16U ) | \
13841414 ((uint32_t ) MG_U8P(p)[2 ] << 8U ) | MG_U8P(p)[3 ]))
1415+ #define MG_LOAD_BE64 (p ) \
1416+ ((uint64_t ) (((uint64_t ) MG_U8P(p)[0 ] << 56U ) | \
1417+ ((uint64_t ) MG_U8P(p)[1 ] << 48U ) | \
1418+ ((uint64_t ) MG_U8P(p)[2 ] << 40U ) | \
1419+ ((uint64_t ) MG_U8P(p)[3 ] << 32U ) | \
1420+ ((uint64_t ) MG_U8P(p)[4 ] << 24U ) | \
1421+ ((uint64_t ) MG_U8P(p)[5 ] << 16U ) | \
1422+ ((uint64_t ) MG_U8P(p)[6 ] << 8U ) | MG_U8P(p)[7 ]))
13851423#define MG_STORE_BE16 (p, n ) \
13861424 do { \
13871425 MG_U8P (p)[0 ] = ((n) >> 8U ) & 255 ; \
@@ -1400,11 +1438,24 @@ void mg_delayms(unsigned int ms);
14001438 MG_U8P (p)[2 ] = ((n) >> 8U ) & 255 ; \
14011439 MG_U8P (p)[3 ] = (n) &255 ; \
14021440 } while (0 )
1441+ #define MG_STORE_BE64 (p, n ) \
1442+ do { \
1443+ MG_U8P (p)[0 ] = ((n) >> 56U ) & 255 ; \
1444+ MG_U8P (p)[1 ] = ((n) >> 48U ) & 255 ; \
1445+ MG_U8P (p)[2 ] = ((n) >> 40U ) & 255 ; \
1446+ MG_U8P (p)[3 ] = ((n) >> 32U ) & 255 ; \
1447+ MG_U8P (p)[4 ] = ((n) >> 24U ) & 255 ; \
1448+ MG_U8P (p)[5 ] = ((n) >> 16U ) & 255 ; \
1449+ MG_U8P (p)[6 ] = ((n) >> 8U ) & 255 ; \
1450+ MG_U8P (p)[7 ] = (n) &255 ; \
1451+ } while (0 )
14031452
14041453uint16_t mg_ntohs (uint16_t net);
14051454uint32_t mg_ntohl (uint32_t net);
1455+ uint64_t mg_ntohll (uint64_t net);
14061456#define mg_htons (x ) mg_ntohs(x)
14071457#define mg_htonl (x ) mg_ntohl(x)
1458+ #define mg_htonll (x ) mg_ntohll(x)
14081459
14091460#define MG_REG (x ) ((volatile uint32_t *) (x))[0 ]
14101461#define MG_BIT (x ) (((uint32_t ) 1U ) << (x))
@@ -3084,18 +3135,17 @@ typedef void (*mg_tcpip_event_handler_t)(struct mg_tcpip_if *ifp, int ev,
30843135 void *ev_data);
30853136
30863137enum {
3087- MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
3088- MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
3089- MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
3090- MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
3091- MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
3092- MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct
3093- // mg_wifi_scan_bss_data *
3094- MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
3095- MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and
3096- // chip specific
3097- MG_TCPIP_EV_DRIVER, // Driver event driver specific
3098- MG_TCPIP_EV_USER // Starting ID for user events
3138+ MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
3139+ MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
3140+ MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
3141+ MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
3142+ MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
3143+ MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct mg_wifi_scan_bss_data *
3144+ MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
3145+ MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and chip specific
3146+ MG_TCPIP_EV_DRIVER, // Driver event driver specific
3147+ MG_TCPIP_EV_ST6_CHG, // state6 change uint8_t * (&ifp->state6)
3148+ MG_TCPIP_EV_USER // Starting ID for user events
30993149};
31003150
31013151// Network interface
@@ -3120,6 +3170,13 @@ struct mg_tcpip_if {
31203170 char dhcp_name[MG_TCPIP_DHCPNAME_SIZE]; // Name for DHCP, "mip" if unset
31213171 uint16_t mtu; // Interface MTU
31223172#define MG_TCPIP_MTU_DEFAULT 1500
3173+ #if MG_ENABLE_IPV6
3174+ uint64_t ip6ll[2 ], ip6[2 ]; // IPv6 link-local and global addresses
3175+ uint8_t prefix_len; // Prefix length
3176+ uint64_t gw6[2 ]; // Default gateway
3177+ bool enable_slaac; // Enable IPv6 address autoconfiguration
3178+ bool enable_dhcp6_client; // Enable DCHPv6 client
3179+ #endif
31233180
31243181 // Internal state, user can use it but should not change it
31253182 uint8_t gwmac[6 ]; // Router's MAC
@@ -3132,14 +3189,17 @@ struct mg_tcpip_if {
31323189 volatile uint32_t nrecv; // Number of received frames
31333190 volatile uint32_t nsent; // Number of transmitted frames
31343191 volatile uint32_t nerr; // Number of driver errors
3135- uint8_t state; // Current state
3192+ uint8_t state; // Current link and IPv4 state
31363193#define MG_TCPIP_STATE_DOWN 0 // Interface is down
31373194#define MG_TCPIP_STATE_UP 1 // Interface is up
31383195#define MG_TCPIP_STATE_REQ 2 // Interface is up, DHCP REQUESTING state
31393196#define MG_TCPIP_STATE_IP 3 // Interface is up and has an IP assigned
31403197#define MG_TCPIP_STATE_READY 4 // Interface has fully come up, ready to work
3198+ #if MG_ENABLE_IPV6
3199+ uint8_t gw6mac[6 ]; // IPv6 Router's MAC
3200+ uint8_t state6; // Current IPv6 state
3201+ #endif
31413202};
3142-
31433203void mg_tcpip_init (struct mg_mgr *, struct mg_tcpip_if *);
31443204void mg_tcpip_free (struct mg_tcpip_if *);
31453205void mg_tcpip_qwrite (void *buf, size_t len, struct mg_tcpip_if *ifp);
@@ -3675,6 +3735,18 @@ struct mg_tcpip_driver_stm32h_data {
36753735#define MG_ENABLE_ETH_IRQ ()
36763736#endif
36773737
3738+ #if MG_ENABLE_IPV6
3739+ #define MG_IPV6_INIT (mif ) \
3740+ do { \
3741+ memcpy (mif.ip6ll , (uint8_t [16 ]) MG_TCPIP_IPV6_LINKLOCAL, 16 ); \
3742+ memcpy (mif.ip6 , (uint8_t [16 ]) MG_TCPIP_GLOBAL, 16 ); \
3743+ memcpy (mif.gw6 , (uint8_t [16 ]) MG_TCPIP_GW6, 16 ); \
3744+ mif.prefix_len = MG_TCPIP_PREFIX_LEN; \
3745+ } while (0 )
3746+ #else
3747+ #define MG_IPV6_INIT (mif )
3748+ #endif
3749+
36783750#define MG_TCPIP_DRIVER_INIT (mgr ) \
36793751 do { \
36803752 static struct mg_tcpip_driver_stm32h_data driver_data_; \
@@ -3688,6 +3760,7 @@ struct mg_tcpip_driver_stm32h_data {
36883760 mif_.driver = &mg_tcpip_driver_stm32h; \
36893761 mif_.driver_data = &driver_data_; \
36903762 MG_SET_MAC_ADDRESS (mif_.mac ); \
3763+ MG_IPV6_INIT (mif_); \
36913764 mg_tcpip_init (mgr, &mif_); \
36923765 MG_ENABLE_ETH_IRQ (); \
36933766 MG_INFO ((" Driver: stm32h, MAC: %M" , mg_print_mac, mif_.mac )); \
0 commit comments