Azure / azure-sphere-samples

Samples for Azure Sphere
Other
223 stars 200 forks source link

How to obtain a network ID for a stored network #228

Open matsujirushi opened 2 years ago

matsujirushi commented 2 years ago

Hi, I used int WifiConfig_ForgetNetwork(const WifiConfig_StoredNetwork * storedNetwork). This function is obsolete.

I am trying to use WifiConfig_ForgetNetworkById instead, but I cannot get the networkID from WifiConfig_StoredNetwork.

Is there any way to do that?

jamesadevine commented 2 years ago

Hi @matsujirushi

Thank you for raising this issue and apologies for not seeing it earlier.

I agree there should be a more direct way to retrieve the NetworkID from WifiConfig_StoredNetwork. An additional member field containing the ID would be more than adequate.

Until such a time where the ID is directly returned via WifiConfig_StoredNetwork, please set the config name of the network when adding it via the CLI or your application.

Via the CLI

When a network is added via the CLI, a configuration name can be provided. The configration name is associated with the added network, and from your application you can get the NetworkID via int WifiConfig_GetNetworkIdByConfigName(const char *configName);. To specify the configuration name when adding a network use the --config-name param: azsphere device wifi add --ssid NETWORK1 --config-name TEST_CFG (note the config name length cannot be greater than WIFICONFIG_SSID_MAX_LENGTH which at this time is 16 bytes).

In your app, you can retrieve the network ID through the following code:

const char cfg_name[] = "TEST_CFG\0";
int id = WifiConfig_GetNetworkIdByConfigName(cfg_name);
Log_Debug("ID for %s is %d", cfg_name, id);

where the cfg_name variable contains a null terminated name of the config name provided via --config-name.

Via your application

I've drafted the following function which will create add a new network with a configuration name if it does not already exist. If the configuration name does exist, the function returns the ID of the given network associated to that configuration name.

int add_network_with_config_name(const char* ssid, int ssid_length, const char* config_name) {
    int id = WifiConfig_GetNetworkIdByConfigName(config_name);

    // return the id of the network if the configuration already exists
    if (id >= 0) {
        Log_Debug("Configuration with name %s exists with id %d\n", config_name, id);
        return id;
    }

    Log_Debug("Configuration does not exist. Creating...\n");
    // otherwise obtain a new network id and assign the configuration name.
    id = WifiConfig_AddNetwork();
    WifiConfig_SetSSID(id, (uint8_t *)ssid, ssid_length);
    WifiConfig_SetConfigName(id, config_name);
    Log_Debug("Added network configuration name %s and id %d\n", config_name, id);
}

The function can be called from your main application as follows:

int main(void) {
    const char ssid[] = "GREAT_SCOTT";
    const char cfg_name[] = "NEW_CFG1\0";

    add_network_with_config_name(ssid, sizeof(ssid), cfg_name);
}

The first time the program is run:

Configuration does not exist. Creating...
Added network configuration name NEW_CFG1 and id 8

The second time the program is run:

Configuration with name NEW_CFG1 exists with id 8