@@ -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
@@ -1368,6 +1388,16 @@ void mg_delayms(unsigned int ms);
1368
1388
1369
1389
#define MG_IPV4 (a, b, c, d ) mg_htonl(MG_U32(a, b, c, d))
1370
1390
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
+
1371
1401
// For printing IPv4 addresses: printf("%d.%d.%d.%d\n", MG_IPADDR_PARTS(&ip))
1372
1402
#define MG_U8P (ADDR ) ((uint8_t *) (ADDR))
1373
1403
#define MG_IPADDR_PARTS (ADDR ) \
@@ -1382,6 +1412,14 @@ void mg_delayms(unsigned int ms);
1382
1412
((uint32_t ) (((uint32_t ) MG_U8P(p)[0 ] << 24U ) | \
1383
1413
((uint32_t ) MG_U8P(p)[1 ] << 16U ) | \
1384
1414
((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 ]))
1385
1423
#define MG_STORE_BE16 (p, n ) \
1386
1424
do { \
1387
1425
MG_U8P (p)[0 ] = ((n) >> 8U ) & 255 ; \
@@ -1400,11 +1438,24 @@ void mg_delayms(unsigned int ms);
1400
1438
MG_U8P (p)[2 ] = ((n) >> 8U ) & 255 ; \
1401
1439
MG_U8P (p)[3 ] = (n) &255 ; \
1402
1440
} 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 )
1403
1452
1404
1453
uint16_t mg_ntohs (uint16_t net);
1405
1454
uint32_t mg_ntohl (uint32_t net);
1455
+ uint64_t mg_ntohll (uint64_t net);
1406
1456
#define mg_htons (x ) mg_ntohs(x)
1407
1457
#define mg_htonl (x ) mg_ntohl(x)
1458
+ #define mg_htonll (x ) mg_ntohll(x)
1408
1459
1409
1460
#define MG_REG (x ) ((volatile uint32_t *) (x))[0 ]
1410
1461
#define MG_BIT (x ) (((uint32_t ) 1U ) << (x))
@@ -3085,18 +3136,17 @@ typedef void (*mg_tcpip_event_handler_t)(struct mg_tcpip_if *ifp, int ev,
3085
3136
void *ev_data);
3086
3137
3087
3138
enum {
3088
- MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
3089
- MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
3090
- MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
3091
- MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
3092
- MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
3093
- MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct
3094
- // mg_wifi_scan_bss_data *
3095
- MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
3096
- MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and
3097
- // chip specific
3098
- MG_TCPIP_EV_DRIVER, // Driver event driver specific
3099
- MG_TCPIP_EV_USER // Starting ID for user events
3139
+ MG_TCPIP_EV_ST_CHG, // state change uint8_t * (&ifp->state)
3140
+ MG_TCPIP_EV_DHCP_DNS, // DHCP DNS assignment uint32_t *ipaddr
3141
+ MG_TCPIP_EV_DHCP_SNTP, // DHCP SNTP assignment uint32_t *ipaddr
3142
+ MG_TCPIP_EV_ARP, // Got ARP packet struct mg_str *
3143
+ MG_TCPIP_EV_TIMER_1S, // 1 second timer NULL
3144
+ MG_TCPIP_EV_WIFI_SCAN_RESULT, // Wi-Fi scan results struct mg_wifi_scan_bss_data *
3145
+ MG_TCPIP_EV_WIFI_SCAN_END, // Wi-Fi scan has finished NULL
3146
+ MG_TCPIP_EV_WIFI_CONNECT_ERR, // Wi-Fi connect has failed driver and chip specific
3147
+ MG_TCPIP_EV_DRIVER, // Driver event driver specific
3148
+ MG_TCPIP_EV_ST6_CHG, // state6 change uint8_t * (&ifp->state6)
3149
+ MG_TCPIP_EV_USER // Starting ID for user events
3100
3150
};
3101
3151
3102
3152
// Network interface
@@ -3121,6 +3171,13 @@ struct mg_tcpip_if {
3121
3171
char dhcp_name[MG_TCPIP_DHCPNAME_SIZE]; // Name for DHCP, "mip" if unset
3122
3172
uint16_t mtu; // Interface MTU
3123
3173
#define MG_TCPIP_MTU_DEFAULT 1500
3174
+ #if MG_ENABLE_IPV6
3175
+ uint64_t ip6ll[2 ], ip6[2 ]; // IPv6 link-local and global addresses
3176
+ uint8_t prefix_len; // Prefix length
3177
+ uint64_t gw6[2 ]; // Default gateway
3178
+ bool enable_slaac; // Enable IPv6 address autoconfiguration
3179
+ bool enable_dhcp6_client; // Enable DCHPv6 client
3180
+ #endif
3124
3181
3125
3182
// Internal state, user can use it but should not change it
3126
3183
uint8_t gwmac[6 ]; // Router's MAC
@@ -3133,14 +3190,17 @@ struct mg_tcpip_if {
3133
3190
volatile uint32_t nrecv; // Number of received frames
3134
3191
volatile uint32_t nsent; // Number of transmitted frames
3135
3192
volatile uint32_t nerr; // Number of driver errors
3136
- uint8_t state; // Current state
3193
+ uint8_t state; // Current link and IPv4 state
3137
3194
#define MG_TCPIP_STATE_DOWN 0 // Interface is down
3138
3195
#define MG_TCPIP_STATE_UP 1 // Interface is up
3139
3196
#define MG_TCPIP_STATE_REQ 2 // Interface is up, DHCP REQUESTING state
3140
3197
#define MG_TCPIP_STATE_IP 3 // Interface is up and has an IP assigned
3141
3198
#define MG_TCPIP_STATE_READY 4 // Interface has fully come up, ready to work
3199
+ #if MG_ENABLE_IPV6
3200
+ uint8_t gw6mac[6 ]; // IPv6 Router's MAC
3201
+ uint8_t state6; // Current IPv6 state
3202
+ #endif
3142
3203
};
3143
-
3144
3204
void mg_tcpip_init (struct mg_mgr *, struct mg_tcpip_if *);
3145
3205
void mg_tcpip_free (struct mg_tcpip_if *);
3146
3206
void mg_tcpip_qwrite (void *buf, size_t len, struct mg_tcpip_if *ifp);
@@ -3637,6 +3697,18 @@ struct mg_tcpip_driver_stm32h_data {
3637
3697
#define MG_ENABLE_ETH_IRQ ()
3638
3698
#endif
3639
3699
3700
+ #if defined(MG_ENABLE_IPV6) && MG_ENABLE_IPV6
3701
+ #define MG_IPV6_INIT (mif ) \
3702
+ do { \
3703
+ memcpy (mif.ip6ll , (uint8_t [16 ]) MG_TCPIP_LINK_LOCAL, 16 ); \
3704
+ memcpy (mif.ip6 , (uint8_t [16 ]) MG_TCPIP_GLOBAL, 16 ); \
3705
+ memcpy (mif.gw6 , (uint8_t [16 ]) MG_TCPIP_GW6, 16 ); \
3706
+ mif.prefix_len = MG_TCPIP_PREFIX_LEN; \
3707
+ } while (0 )
3708
+ #else
3709
+ #define MG_IPV6_INIT (mif )
3710
+ #endif
3711
+
3640
3712
#define MG_TCPIP_DRIVER_INIT (mgr ) \
3641
3713
do { \
3642
3714
static struct mg_tcpip_driver_stm32h_data driver_data_; \
@@ -3650,6 +3722,7 @@ struct mg_tcpip_driver_stm32h_data {
3650
3722
mif_.driver = &mg_tcpip_driver_stm32h; \
3651
3723
mif_.driver_data = &driver_data_; \
3652
3724
MG_SET_MAC_ADDRESS (mif_.mac ); \
3725
+ MG_IPV6_INIT (mif_); \
3653
3726
mg_tcpip_init (mgr, &mif_); \
3654
3727
MG_ENABLE_ETH_IRQ (); \
3655
3728
MG_INFO ((" Driver: stm32h, MAC: %M" , mg_print_mac, mif_.mac )); \
0 commit comments