Home > Software engineering >  construct union dynamically in C
construct union dynamically in C

Time:10-15

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),
           },
       };


CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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;
}
  • Related