esp-compiler: add designated init macros to make esp configuration pattern viable under C++

This commit is contained in:
David Cermak 2020-02-06 12:28:56 +01:00
parent d8da662dec
commit 50588ff3f7
3 changed files with 33 additions and 28 deletions

View File

@ -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

View File

@ -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;

View File

@ -19,6 +19,7 @@
#include <stdbool.h>
#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") \
} \
}