diff --git a/components/esp_common/include/esp_compiler.h b/components/esp_common/include/esp_compiler.h index 94ec29c23c..6922c50adc 100644 --- a/components/esp_common/include/esp_compiler.h +++ b/components/esp_common/include/esp_compiler.h @@ -30,4 +30,22 @@ #define unlikely(x) (x) #endif +/* + * Utility macros used for designated initializers, which work differently + * in C99 and C++ standards mainly for aggregate types. + * The member separator, comma, is already part of the macro, please omit the trailing comma. + * Usage example: + * struct config_t { char* pchr; char arr[SIZE]; } config = { + * ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(pchr) + * ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(arr, "Value") + * }; + */ +#ifdef __cplusplus +#define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(member, value) { .member = value }, +#define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(member) .member = { }, +#else +#define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(member, value) .member = value, +#define ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(member) +#endif + #endif \ No newline at end of file diff --git a/components/esp_netif/include/esp_netif_defaults.h b/components/esp_netif/include/esp_netif_defaults.h index 71f861abd3..3c41d9c2b9 100644 --- a/components/esp_netif/include/esp_netif_defaults.h +++ b/components/esp_netif/include/esp_netif_defaults.h @@ -15,19 +15,12 @@ #ifndef _ESP_NETIF_DEFAULTS_H #define _ESP_NETIF_DEFAULTS_H +#include "esp_compiler.h" + #ifdef __cplusplus extern "C" { #endif -// -// Designated initializers work differently under C++ and C compiler -// -#ifdef __cplusplus -#define _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(item) .item = { }, -#else -#define _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(item) -#endif - // // Macros to assemble master configs with partial configs from netif, stack and driver // @@ -35,8 +28,8 @@ extern "C" { #define ESP_NETIF_INHERENT_DEFAULT_WIFI_STA() \ { \ .flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), \ - _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(mac) \ - _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(ip_info) \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(mac) \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(ip_info) \ .get_ip_event = IP_EVENT_STA_GOT_IP, \ .lost_ip_event = IP_EVENT_STA_LOST_IP, \ .if_key = "WIFI_STA_DEF", \ @@ -47,7 +40,7 @@ extern "C" { #define ESP_NETIF_INHERENT_DEFAULT_WIFI_AP() \ { \ .flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_SERVER | ESP_NETIF_FLAG_AUTOUP), \ - _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(mac) \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(mac) \ .ip_info = &_g_esp_netif_soft_ap_ip, \ .get_ip_event = 0, \ .lost_ip_event = 0, \ @@ -59,8 +52,8 @@ extern "C" { #define ESP_NETIF_INHERENT_DEFAULT_ETH() \ { \ .flags = (esp_netif_flags_t)(ESP_NETIF_DHCP_CLIENT | ESP_NETIF_FLAG_GARP | ESP_NETIF_FLAG_EVENT_IP_MODIFIED), \ - _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(mac) \ - _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(ip_info) \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(mac) \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(ip_info) \ .get_ip_event = IP_EVENT_ETH_GOT_IP, \ .lost_ip_event = 0, \ .if_key = "ETH_DEF", \ @@ -71,8 +64,8 @@ extern "C" { #define ESP_NETIF_INHERENT_DEFAULT_PPP() \ { \ .flags = ESP_NETIF_FLAG_IS_PPP, \ - _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(mac) \ - _ESP_NETIF_EMPTY_AGGREGATE_MEMBER(ip_info) \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(mac) \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_EMPTY(ip_info) \ .get_ip_event = IP_EVENT_PPP_GOT_IP, \ .lost_ip_event = IP_EVENT_PPP_LOST_IP, \ .if_key = "PPP_DEF", \ @@ -159,7 +152,7 @@ extern const esp_netif_netstack_config_t *_g_esp_netif_netstack_default_ppp; // // Include default common configs inherent to esp-netif // - These inherent configs are defined in esp_netif_defaults.c and describe -// common behavioural patters for common interfaces such as STA, AP, ETH +// common behavioural patterns for common interfaces such as STA, AP, ETH, PPP // extern const esp_netif_inherent_config_t _g_esp_netif_inherent_sta_config; extern const esp_netif_inherent_config_t _g_esp_netif_inherent_ap_config; diff --git a/components/wpa_supplicant/include/esp_supplicant/esp_wps.h b/components/wpa_supplicant/include/esp_supplicant/esp_wps.h index c06ae2e368..76c96730cd 100644 --- a/components/wpa_supplicant/include/esp_supplicant/esp_wps.h +++ b/components/wpa_supplicant/include/esp_supplicant/esp_wps.h @@ -19,6 +19,7 @@ #include #include "esp_err.h" #include "esp_wifi_crypto_types.h" +#include "esp_compiler.h" #ifdef __cplusplus extern "C" { @@ -71,20 +72,13 @@ typedef struct { wps_factory_information_t factory_info; } esp_wps_config_t; -/* C & C++ compilers have different rules about C99-style named initializers */ -#ifdef __cplusplus -#define WPS_AGG(X) { X } -#else -#define WPS_AGG(X) X -#endif - #define WPS_CONFIG_INIT_DEFAULT(type) { \ .wps_type = type, \ .factory_info = { \ - WPS_AGG( .manufacturer = "ESPRESSIF" ), \ - WPS_AGG( .model_number = "ESP32" ), \ - WPS_AGG( .model_name = "ESPRESSIF IOT" ), \ - WPS_AGG( .device_name = "ESP STATION" ), \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(manufacturer, "ESPRESSIF") \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(model_number, "ESP32") \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(model_name, "ESPRESSIF IOT") \ + ESP_COMPILER_DESIGNATED_INIT_AGGREGATE_TYPE_STR(device_name, "ESP STATION") \ } \ }