Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of DISCO_L475VG_IOT01A_wifi by
wifi.c
00001 /** 00002 ****************************************************************************** 00003 * @file wifi.c 00004 * @author MCD Application Team 00005 * @version V1.8.0 00006 * @date 21-April-2017 00007 * @brief WIFI interface file. 00008 ****************************************************************************** 00009 * @attention 00010 * 00011 * <h2><center>© Copyright (c) 2017 STMicroelectronics International N.V. 00012 * All rights reserved.</center></h2> 00013 * 00014 * Redistribution and use in source and binary forms, with or without 00015 * modification, are permitted, provided that the following conditions are met: 00016 * 00017 * 1. Redistribution of source code must retain the above copyright notice, 00018 * this list of conditions and the following disclaimer. 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 3. Neither the name of STMicroelectronics nor the names of other 00023 * contributors to this software may be used to endorse or promote products 00024 * derived from this software without specific written permission. 00025 * 4. This software, including modifications and/or derivative works of this 00026 * software, must execute solely and exclusively on microcontroller or 00027 * microprocessor devices manufactured by or for STMicroelectronics. 00028 * 5. Redistribution and use of this software other than as permitted under 00029 * this license is void and will automatically terminate your rights under 00030 * this license. 00031 * 00032 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 00033 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 00034 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 00035 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY 00036 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 00037 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00038 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00039 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00040 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00041 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00042 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00043 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00044 * 00045 ****************************************************************************** 00046 */ 00047 /* Includes ------------------------------------------------------------------*/ 00048 #include "wifi.h" 00049 00050 /* Private define ------------------------------------------------------------*/ 00051 /* Private variables ---------------------------------------------------------*/ 00052 ES_WIFIObject_t EsWifiObj; 00053 00054 /* Private functions ---------------------------------------------------------*/ 00055 /** 00056 * @brief Initialiaze the WIFI core 00057 * @param None 00058 * @retval Operation status 00059 */ 00060 WIFI_Status_t WIFI_Init(void) 00061 { 00062 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00063 00064 if(ES_WIFI_RegisterBusIO(&EsWifiObj, 00065 SPI_WIFI_Init, 00066 SPI_WIFI_DeInit, 00067 SPI_WIFI_Delay, 00068 SPI_WIFI_SendData, 00069 SPI_WIFI_ReceiveData) == ES_WIFI_STATUS_OK) 00070 { 00071 00072 if(ES_WIFI_Init(&EsWifiObj) == ES_WIFI_STATUS_OK) 00073 { 00074 ret = WIFI_STATUS_OK; 00075 } 00076 } 00077 return ret; 00078 } 00079 00080 /** 00081 * @brief List a defined number of vailable access points 00082 * @param APs : pointer to APs structure 00083 * @param AP_MaxNbr : Max APs number to be listed 00084 * @retval Operation status 00085 */ 00086 WIFI_Status_t WIFI_ListAccessPoints(WIFI_APs_t *APs, uint8_t AP_MaxNbr) 00087 { 00088 uint8_t APCount; 00089 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00090 ES_WIFI_APs_t esWifiAPs; 00091 00092 if(ES_WIFI_ListAccessPoints(&EsWifiObj, &esWifiAPs) == ES_WIFI_STATUS_OK) 00093 { 00094 if(esWifiAPs.nbr > 0) 00095 { 00096 APs->count = MIN(esWifiAPs.nbr, AP_MaxNbr); 00097 for(APCount = 0; APCount < APs->count; APCount++) 00098 { 00099 APs->ap[APCount].Ecn = (WIFI_Ecn_t)esWifiAPs.AP[APCount].Security; 00100 strncpy( (char *)APs->ap[APCount].SSID, (char *)esWifiAPs.AP[APCount].SSID, MIN (WIFI_MAX_SSID_NAME, WIFI_MAX_SSID_NAME)); 00101 APs->ap[APCount].RSSI = esWifiAPs.AP[APCount].RSSI; 00102 memcpy(APs->ap[APCount].MAC, esWifiAPs.AP[APCount].MAC, 6); 00103 } 00104 } 00105 ret = WIFI_STATUS_OK; 00106 } 00107 return ret; 00108 } 00109 00110 /** 00111 * @brief Join an Access Point 00112 * @param SSID : SSID string 00113 * @param Password : Password string 00114 * @param ecn : Encryption type 00115 * @param IP_Addr : Got IP Address 00116 * @param IP_Mask : Network IP mask 00117 * @param Gateway_Addr : Gateway IP address 00118 * @param MAC : pointer to MAC Address 00119 * @retval Operation status 00120 */ 00121 WIFI_Status_t WIFI_Connect( 00122 const char* SSID, 00123 const char* Password, 00124 WIFI_Ecn_t ecn) 00125 { 00126 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00127 00128 if(ES_WIFI_Connect(&EsWifiObj, SSID, Password, (ES_WIFI_SecurityType_t) ecn) == ES_WIFI_STATUS_OK) 00129 { 00130 if(ES_WIFI_GetNetworkSettings(&EsWifiObj) == ES_WIFI_STATUS_OK) 00131 { 00132 ret = WIFI_STATUS_OK; 00133 } 00134 } 00135 return ret; 00136 } 00137 00138 /** 00139 * @brief This function put the es-wifi module in power save mode 00140 * @param beacon_int: beacon interval in sec (typ. 1, 5, 10) 00141 * @param sleep_ms: time in ms spent in sleep mode (typically: 1000ms -> 5000ms) 00142 * @retval Operation Status. 00143 */ 00144 WIFI_Status_t WIFI_SetPowerSaveMode(uint8_t beacon_int, uint32_t sleep_ms) { 00145 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00146 if(ES_WIFI_SetPowerSaveMode(&EsWifiObj, beacon_int, sleep_ms) == ES_WIFI_STATUS_OK) { 00147 ret = WIFI_STATUS_OK; 00148 } 00149 return ret; 00150 } 00151 00152 /** 00153 * @brief This function retrieves the WiFi interface's MAC address. 00154 * @retval Operation Status. 00155 */ 00156 WIFI_Status_t WIFI_GetMAC_Address(uint8_t *mac) 00157 { 00158 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00159 00160 if(ES_WIFI_GetMACAddress(&EsWifiObj, mac) == ES_WIFI_STATUS_OK) 00161 { 00162 ret = WIFI_STATUS_OK; 00163 } 00164 return ret; 00165 } 00166 00167 /** 00168 * @brief This function retrieves the WiFi interface's IP address. 00169 * @retval Operation Status. 00170 */ 00171 WIFI_Status_t WIFI_GetIP_Address (uint8_t *ipaddr) 00172 { 00173 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00174 00175 if(EsWifiObj.NetSettings.IsConnected) 00176 { 00177 memcpy(ipaddr, EsWifiObj.NetSettings.IP_Addr, 4); 00178 ret = WIFI_STATUS_OK; 00179 } 00180 return ret; 00181 } 00182 00183 /** 00184 * @brief Disconnect from a network 00185 * @param None 00186 * @retval Operation status 00187 */ 00188 WIFI_Status_t WIFI_Disconnect(void) 00189 { 00190 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00191 if( ES_WIFI_Disconnect(&EsWifiObj)== ES_WIFI_STATUS_OK) 00192 { 00193 ret = WIFI_STATUS_OK; 00194 } 00195 00196 return ret; 00197 } 00198 00199 /** 00200 * @brief Configure an Access Point 00201 00202 * @param ssid : SSID string 00203 * @param pass : Password string 00204 * @param ecn : Encryption type 00205 * @param channel : channel number 00206 * @param max_conn : Max allowed connections 00207 * @retval Operation status 00208 */ 00209 WIFI_Status_t WIFI_ConfigureAP(uint8_t *ssid, uint8_t *pass, WIFI_Ecn_t ecn, uint8_t channel, uint8_t max_conn) 00210 { 00211 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00212 ES_WIFI_APConfig_t ApConfig; 00213 00214 strncpy((char*)ApConfig.SSID, (char*)ssid, ES_WIFI_MAX_SSID_NAME_SIZE); 00215 strncpy((char*)ApConfig.Pass, (char*)pass, ES_WIFI_MAX_PSWD_NAME_SIZE); 00216 ApConfig.Channel = channel; 00217 ApConfig.MaxConnections = WIFI_MAX_CONNECTED_STATIONS; 00218 ApConfig.Security = (ES_WIFI_SecurityType_t)ecn; 00219 00220 if(ES_WIFI_ActivateAP(&EsWifiObj, &ApConfig) == ES_WIFI_STATUS_OK) 00221 { 00222 ret = WIFI_STATUS_OK; 00223 } 00224 return ret; 00225 } 00226 00227 /** 00228 * @brief Handle the background events of the wifi module 00229 00230 * @retval None 00231 */ 00232 WIFI_Status_t WIFI_HandleAPEvents(WIFI_APSettings_t *setting) 00233 { 00234 WIFI_Status_t ret = WIFI_STATUS_OK; 00235 ES_WIFI_APState_t State; 00236 00237 State= ES_WIFI_WaitAPStateChange(&EsWifiObj); 00238 00239 switch (State) 00240 { 00241 case ES_WIFI_AP_ASSIGNED: 00242 memcpy(setting->IP_Addr, EsWifiObj.APSettings.IP_Addr, 4); 00243 memcpy(setting->MAC_Addr, EsWifiObj.APSettings.MAC_Addr, 6); 00244 ret = WIFI_STATUS_ASSIGNED; 00245 break; 00246 00247 case ES_WIFI_AP_JOINED: 00248 strncpy((char *)setting->SSID, (char *)EsWifiObj.APSettings.SSID, WIFI_MAX_SSID_NAME); 00249 memcpy(setting->IP_Addr, EsWifiObj.APSettings.IP_Addr, 4); 00250 ret = WIFI_STATUS_JOINED; 00251 break; 00252 00253 case ES_WIFI_AP_ERROR: 00254 ret = WIFI_STATUS_ERROR; 00255 break; 00256 00257 default: 00258 break; 00259 } 00260 00261 return ret; 00262 } 00263 00264 /** 00265 * @brief Ping an IP address in the network 00266 * @param ipaddr : array of the IP address 00267 * @retval Operation status 00268 */ 00269 WIFI_Status_t WIFI_Ping(uint8_t* ipaddr, uint16_t count, uint16_t interval_ms) 00270 { 00271 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00272 00273 if(ES_WIFI_Ping(&EsWifiObj, ipaddr, count, interval_ms) == ES_WIFI_STATUS_OK) 00274 { 00275 ret = WIFI_STATUS_OK; 00276 } 00277 return ret; 00278 } 00279 00280 /** 00281 * @brief Get IP address from URL using DNS 00282 * @param location : Host URL 00283 * @param ipaddr : array of the IP address 00284 * @retval Operation status 00285 */ 00286 WIFI_Status_t WIFI_GetHostAddress( char* location, uint8_t* ipaddr) 00287 { 00288 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00289 00290 if (ES_WIFI_DNS_LookUp(&EsWifiObj, location, ipaddr) == ES_WIFI_STATUS_OK) 00291 { 00292 return WIFI_STATUS_OK; 00293 } 00294 00295 return ret; 00296 } 00297 /** 00298 * @brief Configure and start a client connection 00299 * @param type : Connection type TCP/UDP 00300 * @param name : name of the connection 00301 * @param ipaddr : Client IP address 00302 * @param port : Remote port 00303 * @param local_port : Local port 00304 * @retval Operation status 00305 */ 00306 WIFI_Status_t WIFI_OpenClientConnection(uint32_t socket, WIFI_Protocol_t type, const char* name, uint8_t* ipaddr, uint16_t port, uint16_t local_port) 00307 { 00308 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00309 ES_WIFI_Conn_t conn; 00310 00311 conn.Number = socket; 00312 conn.RemotePort = port; 00313 conn.LocalPort = local_port; 00314 conn.Type = (type == WIFI_TCP_PROTOCOL)? ES_WIFI_TCP_CONNECTION : ES_WIFI_UDP_CONNECTION; 00315 conn.RemoteIP[0] = ipaddr[0]; 00316 conn.RemoteIP[1] = ipaddr[1]; 00317 conn.RemoteIP[2] = ipaddr[2]; 00318 conn.RemoteIP[3] = ipaddr[3]; 00319 if(ES_WIFI_StartClientConnection(&EsWifiObj, &conn)== ES_WIFI_STATUS_OK) 00320 { 00321 ret = WIFI_STATUS_OK; 00322 } 00323 return ret; 00324 } 00325 00326 /** 00327 * @brief Close client connection 00328 * @param type : Connection type TCP/UDP 00329 * @param name : name of the connection 00330 * @param location : Client address 00331 * @param port : Remote port 00332 * @param local_port : Local port 00333 * @retval Operation status 00334 */ 00335 WIFI_Status_t WIFI_CloseClientConnection(uint32_t socket) 00336 { 00337 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00338 ES_WIFI_Conn_t conn; 00339 conn.Number = socket; 00340 00341 if(ES_WIFI_StopClientConnection(&EsWifiObj, &conn)== ES_WIFI_STATUS_OK) 00342 { 00343 ret = WIFI_STATUS_OK; 00344 } 00345 return ret; 00346 } 00347 00348 /** 00349 * @brief Configure and start a Server 00350 * @param type : Connection type TCP/UDP 00351 * @param name : name of the connection 00352 * @param port : Remote port 00353 * @retval Operation status 00354 */ 00355 WIFI_Status_t WIFI_StartServer(uint32_t socket, WIFI_Protocol_t protocol, const char* name, uint16_t port) 00356 { 00357 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00358 ES_WIFI_Conn_t conn; 00359 conn.Number = socket; 00360 conn.LocalPort = port; 00361 conn.Type = (protocol == WIFI_TCP_PROTOCOL)? ES_WIFI_TCP_CONNECTION : ES_WIFI_UDP_CONNECTION; 00362 if(ES_WIFI_StartServerSingleConn(&EsWifiObj, &conn)== ES_WIFI_STATUS_OK) 00363 { 00364 ret = WIFI_STATUS_OK; 00365 } 00366 return ret; 00367 } 00368 00369 /** 00370 * @brief Stop a server 00371 * @retval Operation status 00372 */ 00373 WIFI_Status_t WIFI_StopServer(uint32_t socket) 00374 { 00375 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00376 00377 if(ES_WIFI_StopServerSingleConn(&EsWifiObj)== ES_WIFI_STATUS_OK) 00378 { 00379 ret = WIFI_STATUS_OK; 00380 } 00381 return ret; 00382 } 00383 /** 00384 * @brief Send Data on a socket 00385 * @param pdata : pointer to data to be sent 00386 * @param len : length of data to be sent 00387 * @retval Operation status 00388 */ 00389 WIFI_Status_t WIFI_SendData(uint8_t socket, uint8_t *pdata, uint16_t Reqlen, uint16_t *SentDatalen, uint32_t Timeout) 00390 { 00391 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00392 00393 if(ES_WIFI_SendData(&EsWifiObj, socket, pdata, Reqlen, SentDatalen, Timeout) == ES_WIFI_STATUS_OK) 00394 { 00395 ret = WIFI_STATUS_OK; 00396 } 00397 00398 return ret; 00399 } 00400 00401 /** 00402 * @brief Receive Data from a socket 00403 * @param pdata : pointer to Rx buffer 00404 * @param *len : pointer to length of data 00405 * @retval Operation status 00406 */ 00407 WIFI_Status_t WIFI_ReceiveData(uint8_t socket, uint8_t *pdata, uint16_t Reqlen, uint16_t *RcvDatalen, uint32_t Timeout) 00408 { 00409 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00410 00411 if(ES_WIFI_ReceiveData(&EsWifiObj, socket, pdata, Reqlen, RcvDatalen, Timeout) == ES_WIFI_STATUS_OK) 00412 { 00413 ret = WIFI_STATUS_OK; 00414 } 00415 return ret; 00416 } 00417 00418 /** 00419 * @brief Customize module data 00420 * @param name : MFC name 00421 * @param Mac : Mac Address 00422 * @retval Operation status 00423 */ 00424 WIFI_Status_t WIFI_SetOEMProperties(const char *name, uint8_t *Mac) 00425 { 00426 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00427 00428 if(ES_WIFI_SetProductName(&EsWifiObj, (uint8_t *)name) == ES_WIFI_STATUS_OK) 00429 { 00430 if(ES_WIFI_SetMACAddress(&EsWifiObj, Mac) == ES_WIFI_STATUS_OK) 00431 { 00432 ret = WIFI_STATUS_OK; 00433 } 00434 } 00435 return ret; 00436 } 00437 00438 /** 00439 * @brief Reset the WIFI module 00440 * @retval Operation status 00441 */ 00442 WIFI_Status_t WIFI_ResetModule(void) 00443 { 00444 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00445 00446 if(ES_WIFI_ResetModule(&EsWifiObj) == ES_WIFI_STATUS_OK) 00447 { 00448 ret = WIFI_STATUS_OK; 00449 } 00450 return ret; 00451 } 00452 00453 /** 00454 * @brief Restore module default configuration 00455 * @retval Operation status 00456 */ 00457 WIFI_Status_t WIFI_SetModuleDefault(void) 00458 { 00459 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00460 00461 if(ES_WIFI_ResetToFactoryDefault(&EsWifiObj) == ES_WIFI_STATUS_OK) 00462 { 00463 ret = WIFI_STATUS_OK; 00464 } 00465 return ret; 00466 } 00467 00468 00469 /** 00470 * @brief Update module firmware 00471 * @param location : Binary Location IP address 00472 * @retval Operation status 00473 */ 00474 WIFI_Status_t WIFI_ModuleFirmwareUpdate(const char *location) 00475 { 00476 return WIFI_STATUS_NOT_SUPPORTED; 00477 } 00478 00479 /** 00480 * @brief Return Module firmware revision 00481 * @param rev : revision string 00482 * @retval Operation status 00483 */ 00484 WIFI_Status_t WIFI_GetModuleFwRevision(char *rev) 00485 { 00486 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00487 00488 if(EsWifiObj.FW_Rev != NULL) 00489 { 00490 strncpy(rev, (char *)EsWifiObj.FW_Rev, ES_WIFI_FW_REV_SIZE); 00491 ret = WIFI_STATUS_OK; 00492 } 00493 return ret; 00494 } 00495 00496 /** 00497 * @brief Return Module ID 00498 * @param Info : Module ID string 00499 * @retval Operation status 00500 */ 00501 WIFI_Status_t WIFI_GetModuleID(char *Id) 00502 { 00503 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00504 00505 if(EsWifiObj.Product_ID != NULL) 00506 { 00507 strncpy(Id, (char *)EsWifiObj.Product_ID, ES_WIFI_PRODUCT_ID_SIZE); 00508 ret = WIFI_STATUS_OK; 00509 } 00510 return ret; 00511 } 00512 00513 /** 00514 * @brief Return Module Name 00515 * @param Info : Module Name string 00516 * @retval Operation status 00517 */ 00518 WIFI_Status_t WIFI_GetModuleName(char *ModuleName) 00519 { 00520 WIFI_Status_t ret = WIFI_STATUS_ERROR; 00521 00522 if(EsWifiObj.Product_Name != NULL) 00523 { 00524 strncpy(ModuleName, (char *)EsWifiObj.Product_Name, ES_WIFI_PRODUCT_NAME_SIZE); 00525 ret = WIFI_STATUS_OK; 00526 } 00527 return ret; 00528 } 00529 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 00530
Generated on Wed Jul 20 2022 22:18:56 by
1.7.2
