我正在尝试编写一个函数,它将采用ssid、密码、信道和最大连接设备来初始化WLAN AP。下面是我的密码。
void wifi_init_softap(const char * ssid, const char *password, uint8_t channel, uint8_t max_conn)
{
ESP_ERROR_CHECK(esp_netif_init()); // Initialise ESP Netif, Which manages the DHCP Server
ESP_ERROR_CHECK(esp_event_loop_create_default()); // Create new event loop
// esp_netif_create_default_wifi_ap();
esp_netif_t *p_netif = esp_netif_create_default_wifi_ap(); // Create Default Wifi AP
esp_netif_ip_info_t ipInfo; // Stores IP & Gateway & Netmask
// Assignes default values to ip stack
IP4_ADDR(&ipInfo.ip, 192, 168, 1, 1);
IP4_ADDR(&ipInfo.gw, 192, 168, 1, 1);
IP4_ADDR(&ipInfo.netmask, 255, 255, 255, 0);
// Stop DHCP Server on p_netif, assign the new IP stack, and restart it
esp_netif_dhcps_stop(p_netif);
esp_netif_set_ip_info(p_netif, &ipInfo);
esp_netif_dhcps_start(p_netif);
//----------------------------------------------------------------------------------------//
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID,
&wifi_event_handler,
NULL,
NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
//----------------------------------------------------------------------------------------//
// uint8_t *buffer_ssid = (uint8_t *)malloc((strlen(ssid)) * sizeof(char)+1);
// memcpy(buffer_ssid, ssid, (strlen(ssid)) * sizeof(char) );
wifi_config_t wifi_config = {
.ap = {
.ssid = "*ssid",
.ssid_len = strlen(ssid),
.channel = channel,
.password = "(*password)",
.max_connection = max_conn,
.authmode = WIFI_AUTH_WPA_WPA2_PSK},
};
if (strlen(password) == 0)
{
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s channel:%d",
ssid, password, max_conn);
}这一问题涉及以下部分:
wifi_config_t wifi_config = {
.ap = {
.ssid = "*ssid",
.ssid_len = strlen(ssid),
.channel = channel,
.password = "(*password)",
.max_connection = max_conn,
.authmode = WIFI_AUTH_WPA_WPA2_PSK},
};当我像上面一样将ssid和passord作为字符串常量传递时,它正确地编译和运行。但是,如果我尝试将它们作为变量来初始化wifi_config.ap.ssid,就像下面一样,会产生一个我无法理解的错误。
代码:
wifi_config_t wifi_config = {
.ap = {
.ssid = *ssid,
.ssid_len = strlen(ssid),
.channel = channel,
.password = *password,
.max_connection = max_conn,
.authmode = WIFI_AUTH_WPA_WPA2_PSK},
};错误信息:
missing braces around initializer [-Werror=missing-braces]我没能想出解决办法。在"esp_wifi_types.h“中,wifi_ap_config_t成员变量的类型如下所示。
typedef struct {
uint8_t ssid[32]; /**< SSID of ESP32 soft-AP. If ssid_len field is 0, this must be a Null terminated string. Otherwise, length is set according to ssid_len. */
uint8_t password[64]; /**< Password of ESP32 soft-AP. */
uint8_t ssid_len; /**< Optional length of SSID field. */
uint8_t channel; /**< Channel of ESP32 soft-AP */
wifi_auth_mode_t authmode; /**< Auth mode of ESP32 soft-AP. Do not support AUTH_WEP in soft-AP mode */
uint8_t ssid_hidden; /**< Broadcast SSID or not, default 0, broadcast the SSID */
uint8_t max_connection; /**< Max number of stations allowed to connect in, default 4, max 10 */
uint16_t beacon_interval; /**< Beacon interval which should be multiples of 100. Unit: TU(time unit, 1 TU = 1024 us). Range: 100 ~ 60000. Default value: 100 */
wifi_cipher_type_t pairwise_cipher; /**< pairwise cipher of SoftAP, group cipher will be derived using this. cipher values are valid starting from WIFI_CIPHER_TYPE_TKIP, enum values before that will be considered as invalid and default cipher suites(TKIP+CCMP) will be used. Valid cipher suites in softAP mode are WIFI_CIPHER_TYPE_TKIP, WIFI_CIPHER_TYPE_CCMP and WIFI_CIPHER_TYPE_TKIP_CCMP. */
bool ftm_responder; /**< Enable FTM Responder mode */
} wifi_ap_config_t;发布于 2022-11-23 11:41:41
由于ssid字段wifi_config_t是数组,而ssid函数参数是指针,所以不能将后者赋值给前者。您需要的是将指针所指向的数据复制到数组。如果您的SSID是一个以空结尾的字符串,那么您应该能够使用strlcpy函数这样做,如下所示:
// destination first, source second, max size of buffer last
strlcpy(wifi_config.ap.ssid, ssid, 32);如果strlcpy不可用,则可能需要使用strncpy,但如果可用,则应使用strlcpy,因为它保证在目标数组中终止字符串。如果两者都不可用,则需要使用ssid函数手动查找strlen指向的字符串的长度,然后使用memcpy将这许多字节从ssid复制到wifi_config.ap.ssid,类似于使用strlcpy的方式。
size_t ssid_length = strlen(ssid);
memcpy(wifi_config.ap.ssid, ssid, ssid_length);同样,这都是假设ssid指针指向的字符串为空终止的.如果不是,则需要其他方法来查找SSID字符串的长度。
这是因为赋值=不适用于数组。您现在正在使用的一个显著的例外是常量字符串的赋值。如果将常量字符串分配给数组,则某些编译器将自动将其转换为memcpy调用。但是,它不适用于像ssid指针这样的非常数字符串。在这种情况下,编译器试图将指针的值(字符串的地址,这是一个类似整数的值)分配给uint8_t数组。可以理解,操作没有意义,所以编译器会抛出一个错误。
https://stackoverflow.com/questions/74546125
复制相似问题