Step 3: Trying WPA
|Top Previous Next|
This and other projects in the Code Examples section are published on our website under the name "test_wln_lib".
OK then, now let's try to associate with an access point configured for WPA2-PSK security. Your can try WPA-PSK by yourself.
With WPA, things get a bit tricker because of a pre-shared master key (PMK). This is the actual password used between your device and the access point. The key is calculated from a "human" password (that you define) and the access point's SSID (name). Wln_wpa_mkey_get() does the math. The bad news is that it takes 2 minutes... yep, sorry, 8000 iterations involving sha1 need that long. The good news is that you only need to do this once after changing the password or SSID. You can then just save the resulting key for the future use. STG library comes handy for this.
Our project now has a new function — connect_to_ap() — that calls wln_start(). This function takes all the same arguments as wln_start(). Connect_to_ap() stores the SSID, human password, and pre-shared master key into the EEPROM. Wln_wpa_mkey_get() is only called if the SSID or human password change, or if the "PMK" setting is invalid. For the "PMK" setting to be valid, it must contain a 32-character string — the PMK always has this length. Keeping the PMK in the EEPROM allows us to avoid recalculating it — a huge time saver. Callback_wln_mkey_progress_update() is called repeatedly from within wln_wpa_mkey_get() while the PMK calculation is in progress. We put pat.play there to assure the user that the system isn't dead.
Below is the debug output we've got when connecting to the access point named "TIBB1".
The new iteration of our project is listed below. Notice how we set #define WLN_WPA 1 in global.tbh. This is necessary to enable WPA support.
We show one more trick in this example. Instead of having a listening TCP socket, we have a loopback socket that connects to a certain destination IP whenever there is a successful association. To ensure persistent connection, we put a polling code into on_sys_timer. Not the most beautiful of solutions, but gets the job done. The TCP connection is discarded whenever we get callback_wln_failiure().