Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
matsujirushi opened this issue Mar 11, 2022 · 1 comment
Open

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

matsujirushi opened this issue Mar 11, 2022 · 1 comment

Comments

@matsujirushi
Copy link
Contributor

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
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants