4.3.2 Main Application
The user can port any ASF application with MLA using the following steps.
- Create
main.h
andmain.c
files under the “source files” of the project and copy the following code.The reference application scans the nearby access points (AP) and connects to the configured AP.
- Copy the following to the
main.h
file:#define BOARD_NAME "16-bit 28-pin Demo Board" /** Wi-Fi Settings */ #define MAIN_WLAN_SSID "DEMO_AP_T" /**< Destination SSID */ #define MAIN_WLAN_AUTH M2M_WIFI_SEC_OPEN /**< Security manner */ #define MAIN_WLAN_PSK "Atmel123" /**< Password for Destination SSID */ #define STRING_EOL "\r\n" #define STRING_HEADER "-- WINC1500 AP scan example --"STRING_EOL \ "-- "BOARD_NAME " --"STRING_EOL \ "-- Compiled: "__DATE__ " "__TIME__ " --"STRING_EOL
- Copy the following to the
main.c
file:#include <stdio.h> #include <string.h> #include "mcc_generated_files/mcc.h" #include "main.h" #include "common/include/nm_common.h" #include "driver/include/m2m_wifi.h" /** Index of scan list to request scan result. */ static uint8_t scan_request_index = 0; /** Number of APs found. */ static uint8_t num_founded_ap = 0; static void wifi_cb(uint8_t u8MsgType, void *pvMsg) { switch (u8MsgType) { case M2M_WIFI_RESP_SCAN_DONE: { tstrM2mScanDone *pstrInfo = (tstrM2mScanDone *)pvMsg; scan_request_index = 0; if (pstrInfo->u8NumofCh >= 1) { m2m_wifi_req_scan_result(scan_request_index); scan_request_index++; } else { m2m_wifi_request_scan(M2M_WIFI_CH_ALL); } break; } case M2M_WIFI_RESP_SCAN_RESULT: { tstrM2mWifiscanResult *pstrScanResult = (tstrM2mWifiscanResult *)pvMsg; uint16_t demo_ssid_len; uint16_t scan_ssid_len = strlen((const char *)pstrScanResult->au8SSID); /* display founded AP. */ printf("[%d] SSID:%s\r\n", scan_request_index, pstrScanResult->au8SSID); num_founded_ap = m2m_wifi_get_num_ap_found(); if (scan_ssid_len) { /* check same SSID. */ demo_ssid_len = strlen((const char *)MAIN_WLAN_SSID); if ((demo_ssid_len == scan_ssid_len) && (!memcmp(pstrScanResult->au8SSID, (uint8_t *)MAIN_WLAN_SSID, demo_ssid_len))) { /* A scan result matches an entry in the preferred AP List. * Initiate a connection request. */ printf("Found %s \r\n", MAIN_WLAN_SSID); m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (void *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL); break; } } if (scan_request_index < num_founded_ap) { m2m_wifi_req_scan_result(scan_request_index); scan_request_index++; } else { printf("can not find AP %s\r\n", MAIN_WLAN_SSID); m2m_wifi_request_scan(M2M_WIFI_CH_ALL); } break; } case M2M_WIFI_RESP_CON_STATE_CHANGED: { tstrM2mWifiStateChanged *pstrWifiState = (tstrM2mWifiStateChanged *)pvMsg; if (pstrWifiState->u8CurrState == M2M_WIFI_CONNECTED) { m2m_wifi_request_dhcp_client(); } else if (pstrWifiState->u8CurrState == M2M_WIFI_DISCONNECTED) { printf("Wi-Fi disconnected\r\n"); /* Request scan. */ m2m_wifi_request_scan(M2M_WIFI_CH_ALL); } break; } case M2M_WIFI_REQ_DHCP_CONF: { uint8_t *pu8IPAddress = (uint8_t *)pvMsg; printf("Wi-Fi connected\r\n"); printf("Wi-Fi IP is %u.%u.%u.%u\r\n", pu8IPAddress[0], pu8IPAddress[1], pu8IPAddress[2], pu8IPAddress[3]); break; } default: { break; }} } /* Main application */ int main(void) { tstrWifiInitParam param; int8_t ret; // initialize the device SYSTEM_Initialize(); delay_ms(1000); printf(STRING_HEADER); /* Initialize the BSP. */ nm_bsp_init(); /* Initialize Wi-Fi parameters structure. */ memset((uint8_t *)¶m, 0, sizeof(tstrWifiInitParam)); /* Initialize Wi-Fi driver with data and status callbacks. */ param.pfAppWifiCb = wifi_cb; ret = m2m_wifi_init(¶m); if (M2M_SUCCESS != ret) { printf("main: m2m_wifi_init call error!(%d)\r\n", ret); while (1) { } } /* Request scan. */ m2m_wifi_request_scan(M2M_WIFI_CH_ALL); /* Connect to router. */ printf("main: m2m_wifi_connect()\r\n"); m2m_wifi_connect((char *)MAIN_WLAN_SSID, sizeof(MAIN_WLAN_SSID), MAIN_WLAN_AUTH, (char *)MAIN_WLAN_PSK, M2M_WIFI_CH_ALL); while (1) { /* Handle pending events from network controller. */ while (m2m_wifi_handle_events(NULL) != M2M_SUCCESS) { } } return 0; }
- Compile the project, then load the image into the hardware. The following output can be observed on the console.