-1

I'm trying to set configuration of WIFI with ESP32 dynamically.

Depends on UUID of device ID, I would like to set SSID.

I was trying to put character variable. However, I failed



 // Original code
   wifi_config_t ap_config =
       {
           .ap = {
               .ssid = 'WIFI_SSID', // I would like to change .ssid as variable
               .ssid_len = strlen(WIFI_AP_SSID),
               .password = WIFI_AP_PASSWORD,
               .channel = WIFI_AP_CHANNEL,
               .ssid_hidden = WIFI_AP_SSID_HIDDEN,
               .authmode = WIFI_AUTH_WPA2_PSK,
               .max_connection = WIFI_AP_MAX_CONNECTIONS,
               .beacon_interval = WIFI_AP_BEACON_INTERVAL,
           },
       };


// For example, I would like to set SSID name by using another variable
char ssid[10] = "anoter_ssid_name";

wifi_config_t ap_config =
       {
           .ap = {
               .ssid = ssid, // I would like to change .ssid as variable
               .ssid_len = strlen(WIFI_AP_SSID),
           },
       };


Jaeseo Lee
  • 172
  • 10
  • 2
    This has nothing to do with unions. [`ssid` is a `char[32]`](https://github.com/espressif/esp-idf/blob/master/components/esp_wifi/include/esp_wifi_types.h#L236) and array names are non-modifiable lvalues. You can simply `memcpy`/`strcpy` into `.ssid`. You should take the proper time to learn the basic of C though. – Margaret Bloom Oct 14 '22 at 09:15
  • char ssid[10] = "anoter_ssid_name"; will not give you a C string... 10 characters will fill the buffer, without a terminating '\0'... Maybe this idea is beyond your abilities at present... – Fe2O3 Oct 14 '22 at 09:29

3 Answers3

1

You need to copy the ssid string in because the ssid field of the struct is declared as uint8_t [10], so already has the space allocated. Your attempt above would have worked if it was declared as a pointer.

strcpy(ap_config.ap.ssid, ssid);
ap_config.ap.ssid_len = strlen(ssid);
possum
  • 1,837
  • 3
  • 9
  • 18
  • I just posted a comment that the "string-ish" thing in the example will not be a C string in the eyes of `strcpy()`... 10 bytes cannot hold all the OP shows to be packing into the buffer, including the required '\0' to make it a C string... – Fe2O3 Oct 14 '22 at 09:31
1

If you want to initilaize:

wifi_config_t ap_config =
       {
           .ap = {
               .ssid = { ssid[0], ssid[1], ssid[2], ssid[3], ssid[4], ssid[5], ssid[6], ssid[7], ssid[8], ssid[9] },
               .ssid_len = strlen(WIFI_AP_SSID),
           },
       };

You can wrap every element initialization in a check against the length to protect against overflow:

 .ssid = {
     strlen(ssid) > 0 ? ssid[0] : 0,
     strlen(ssid) > 1 ? ssid[1] : 0,
     strlen(ssid) > 2 ? ssid[2] : 0,
     strlen(ssid) > 3 ? ssid[3] : 0,
     // etc...

But such code is not optimal. The simplest sounds like you really want to just copy:

wifi_config_t ap_config;
strncpy(ap_config.ap.ssid, ssid, sizeof(ap_config.ap.ssid));
ap_config.ap.ssid_len = strlen(ssid);
KamilCuk
  • 120,984
  • 8
  • 59
  • 111
  • Null termination not guaranteed, though... Does the app understand the reported length is 16 when the buffer holds only the first 10 characters? – Fe2O3 Oct 14 '22 at 09:34
  • How do you want to use not const expressions to initialize static storage duration objects? – 0___________ Oct 14 '22 at 10:58
1

This is definitely wrong:

.ssid = 'WIFI_SSID', 

Next: Basically, you can't use not const expressions in static storage duration initializers. You need to do it in the function.

wifi_config_t ap_config =
{
    .ap = {
        .ssid = "WIFI_SSID", // I would like to change .ssid as variable
        .ssid_len = sizeof("WIFI_SSID") - 1,
        .password = WIFI_AP_PASSWORD,
        .channel = WIFI_AP_CHANNEL,
        .ssid_hidden = WIFI_AP_SSID_HIDDEN,
        .authmode = WIFI_AUTH_WPA2_PSK,
        .max_connection = WIFI_AP_MAX_CONNECTIONS,
        .beacon_interval = WIFI_AP_BEACON_INTERVAL,
    },
};

wifi_config_t *setSSID(wifi_config_t *cfg, const char *ssid)
{
    strcpy(cfg -> ssid, ssid);
    cfg -> ssid_len = strlen(ssid);
    rerutn cfg;
}
0___________
  • 60,014
  • 4
  • 34
  • 74