AT-AT Robot Ops / DISCO_L475VG_IOT01A_wifi-client-baseclient

Fork of DISCO_L475VG_IOT01A_wifi by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers wifi.c Source File

wifi.c

Go to the documentation of this file.
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>&copy; 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   }
00136   return ret;
00137 }
00138 
00139 /**
00140   * @brief  This function retrieves the WiFi interface's MAC address.
00141   * @retval Operation Status.
00142   */
00143 WIFI_Status_t WIFI_GetMAC_Address(uint8_t  *mac)
00144 {
00145   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00146   
00147   if(ES_WIFI_GetMACAddress(&EsWifiObj, mac) == ES_WIFI_STATUS_OK)
00148   {
00149     ret = WIFI_STATUS_OK;
00150   }
00151   return ret;
00152 }
00153 
00154 /**
00155   * @brief  This function retrieves the WiFi interface's IP address.
00156   * @retval Operation Status.
00157   */
00158 WIFI_Status_t WIFI_GetIP_Address (uint8_t  *ipaddr)
00159 {
00160   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00161   
00162   if(EsWifiObj.NetSettings.IsConnected)
00163   {
00164     memcpy(ipaddr, EsWifiObj.NetSettings.IP_Addr, 4);
00165     ret = WIFI_STATUS_OK;
00166   }
00167   return ret;
00168 }
00169 
00170 /**
00171   * @brief  Disconnect from a network
00172   * @param  None
00173   * @retval Operation status
00174   */
00175 WIFI_Status_t WIFI_Disconnect(void)
00176 {
00177   WIFI_Status_t ret = WIFI_STATUS_ERROR;    
00178   if( ES_WIFI_Disconnect(&EsWifiObj)== ES_WIFI_STATUS_OK)
00179   {
00180       ret = WIFI_STATUS_OK; 
00181   }
00182   
00183   return ret;
00184 }
00185 
00186 /**
00187   * @brief  Configure an Access Point
00188 
00189   * @param  ssid : SSID string
00190   * @param  pass : Password string
00191   * @param  ecn : Encryption type
00192   * @param  channel : channel number
00193   * @param  max_conn : Max allowed connections
00194   * @retval Operation status
00195   */
00196 WIFI_Status_t WIFI_ConfigureAP(uint8_t *ssid, uint8_t *pass, WIFI_Ecn_t ecn, uint8_t channel, uint8_t max_conn)
00197 {
00198   WIFI_Status_t ret = WIFI_STATUS_ERROR;  
00199   ES_WIFI_APConfig_t ApConfig;
00200   
00201   strncpy((char*)ApConfig.SSID, (char*)ssid, ES_WIFI_MAX_SSID_NAME_SIZE);
00202   strncpy((char*)ApConfig.Pass, (char*)pass, ES_WIFI_MAX_PSWD_NAME_SIZE);
00203   ApConfig.Channel = channel;
00204   ApConfig.MaxConnections = WIFI_MAX_CONNECTED_STATIONS;
00205   ApConfig.Security = (ES_WIFI_SecurityType_t)ecn;
00206   
00207   if(ES_WIFI_ActivateAP(&EsWifiObj, &ApConfig) == ES_WIFI_STATUS_OK)
00208   {
00209       ret = WIFI_STATUS_OK; 
00210   }
00211   return ret;
00212 }
00213 
00214 /**
00215   * @brief  Handle the background events of the wifi module
00216 
00217   * @retval None
00218 */
00219 WIFI_Status_t WIFI_HandleAPEvents(WIFI_APSettings_t *setting)
00220 {
00221   WIFI_Status_t ret = WIFI_STATUS_OK;   
00222   ES_WIFI_APState_t State;
00223   
00224   State= ES_WIFI_WaitAPStateChange(&EsWifiObj);
00225   
00226   switch (State)
00227   {
00228   case ES_WIFI_AP_ASSIGNED:
00229     memcpy(setting->IP_Addr, EsWifiObj.APSettings.IP_Addr, 4);  
00230     memcpy(setting->MAC_Addr, EsWifiObj.APSettings.MAC_Addr, 6);
00231     ret = WIFI_STATUS_ASSIGNED;    
00232     break;
00233     
00234   case ES_WIFI_AP_JOINED:
00235     strncpy((char *)setting->SSID, (char *)EsWifiObj.APSettings.SSID, WIFI_MAX_SSID_NAME);
00236     memcpy(setting->IP_Addr, EsWifiObj.APSettings.IP_Addr, 4); 
00237     ret = WIFI_STATUS_JOINED;
00238     break;
00239     
00240   case ES_WIFI_AP_ERROR:
00241     ret = WIFI_STATUS_ERROR;  
00242     break;
00243     
00244   default:
00245     break;
00246   }
00247   
00248   return ret;
00249 }
00250 
00251 /**
00252   * @brief  Ping an IP address in the network
00253   * @param  ipaddr : array of the IP address
00254   * @retval Operation status
00255   */
00256 WIFI_Status_t WIFI_Ping(uint8_t* ipaddr, uint16_t count, uint16_t interval_ms)
00257 {
00258   WIFI_Status_t ret = WIFI_STATUS_ERROR;  
00259 
00260   if(ES_WIFI_Ping(&EsWifiObj, ipaddr, count, interval_ms) == ES_WIFI_STATUS_OK)
00261   {
00262     ret = WIFI_STATUS_OK;
00263   }
00264   return ret;
00265 }
00266 
00267 /**
00268   * @brief  Get IP address from URL using DNS
00269   * @param  location : Host URL
00270   * @param  ipaddr : array of the IP address
00271   * @retval Operation status
00272   */
00273 WIFI_Status_t WIFI_GetHostAddress( char* location, uint8_t* ipaddr)
00274 {
00275   WIFI_Status_t ret = WIFI_STATUS_ERROR;  
00276   
00277   if (ES_WIFI_DNS_LookUp(&EsWifiObj, location, ipaddr) == ES_WIFI_STATUS_OK)
00278   {
00279     return WIFI_STATUS_OK;
00280   }
00281   
00282   return ret;
00283 }
00284 /**
00285   * @brief  Configure and start a client connection
00286   * @param  type : Connection type TCP/UDP
00287   * @param  name : name of the connection
00288   * @param  ipaddr : Client IP address
00289   * @param  port : Remote port
00290   * @param  local_port : Local port
00291   * @retval Operation status
00292   */
00293 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)
00294 {
00295   WIFI_Status_t ret = WIFI_STATUS_ERROR;
00296   ES_WIFI_Conn_t conn;
00297   
00298   conn.Number = socket;
00299   conn.RemotePort = port;
00300   conn.LocalPort = local_port;
00301   conn.Type = (type == WIFI_TCP_PROTOCOL)? ES_WIFI_TCP_CONNECTION : ES_WIFI_UDP_CONNECTION;
00302   conn.RemoteIP[0] = ipaddr[0];
00303   conn.RemoteIP[1] = ipaddr[1];
00304   conn.RemoteIP[2] = ipaddr[2];
00305   conn.RemoteIP[3] = ipaddr[3];
00306   if(ES_WIFI_StartClientConnection(&EsWifiObj, &conn)== ES_WIFI_STATUS_OK)
00307   {
00308     ret = WIFI_STATUS_OK;
00309   }
00310   return ret;
00311 }
00312 
00313 /**
00314   * @brief  Close client connection
00315   * @param  type : Connection type TCP/UDP
00316   * @param  name : name of the connection
00317   * @param  location : Client address
00318   * @param  port : Remote port
00319   * @param  local_port : Local port
00320   * @retval Operation status
00321   */
00322 WIFI_Status_t WIFI_CloseClientConnection(uint32_t socket)
00323 {
00324   WIFI_Status_t ret = WIFI_STATUS_ERROR;  
00325   ES_WIFI_Conn_t conn;
00326   conn.Number = socket;
00327   
00328   if(ES_WIFI_StopClientConnection(&EsWifiObj, &conn)== ES_WIFI_STATUS_OK)
00329   {
00330     
00331     ret = WIFI_STATUS_OK;
00332   }
00333   return ret; 
00334 }
00335 
00336 /**
00337   * @brief  Configure and start a Server
00338   * @param  type : Connection type TCP/UDP
00339   * @param  name : name of the connection
00340   * @param  port : Remote port
00341   * @retval Operation status
00342   */
00343 WIFI_Status_t WIFI_StartServer(uint32_t socket, WIFI_Protocol_t protocol, const char* name, uint16_t port)
00344 {
00345   WIFI_Status_t ret = WIFI_STATUS_ERROR;
00346   ES_WIFI_Conn_t conn;
00347   conn.Number = socket;
00348   conn.LocalPort = port;
00349   conn.Type = (protocol == WIFI_TCP_PROTOCOL)? ES_WIFI_TCP_CONNECTION : ES_WIFI_UDP_CONNECTION;
00350   if(ES_WIFI_StartServerSingleConn(&EsWifiObj, &conn)== ES_WIFI_STATUS_OK)
00351   {
00352     ret = WIFI_STATUS_OK;
00353   }
00354   return ret;
00355 }
00356 
00357 /**
00358   * @brief  Stop a server
00359   * @retval Operation status
00360   */
00361 WIFI_Status_t WIFI_StopServer(uint32_t socket)
00362 {
00363   WIFI_Status_t ret = WIFI_STATUS_ERROR;
00364   
00365   if(ES_WIFI_StopServerSingleConn(&EsWifiObj)== ES_WIFI_STATUS_OK)
00366   {
00367     ret = WIFI_STATUS_OK;
00368   }
00369   return ret;
00370 }
00371 /**
00372   * @brief  Send Data on a socket
00373   * @param  pdata : pointer to data to be sent
00374   * @param  len : length of data to be sent
00375   * @retval Operation status
00376   */
00377 WIFI_Status_t WIFI_SendData(uint8_t socket, uint8_t *pdata, uint16_t Reqlen, uint16_t *SentDatalen, uint32_t Timeout)
00378 {
00379   WIFI_Status_t ret = WIFI_STATUS_ERROR;
00380 
00381     if(ES_WIFI_SendData(&EsWifiObj, socket, pdata, Reqlen, SentDatalen, Timeout) == ES_WIFI_STATUS_OK)
00382     {
00383       ret = WIFI_STATUS_OK;
00384     }
00385 
00386   return ret;
00387 }
00388 
00389 /**
00390   * @brief  Receive Data from a socket
00391   * @param  pdata : pointer to Rx buffer
00392   * @param  *len :  pointer to length of data
00393   * @retval Operation status
00394   */
00395 WIFI_Status_t WIFI_ReceiveData(uint8_t socket, uint8_t *pdata, uint16_t Reqlen, uint16_t *RcvDatalen, uint32_t Timeout)
00396 {
00397   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00398 
00399   if(ES_WIFI_ReceiveData(&EsWifiObj, socket, pdata, Reqlen, RcvDatalen, Timeout) == ES_WIFI_STATUS_OK)
00400   {
00401     ret = WIFI_STATUS_OK; 
00402   }
00403   return ret;
00404 }
00405 
00406 /**
00407   * @brief  Customize module data
00408   * @param  name : MFC name
00409   * @param  Mac :  Mac Address
00410   * @retval Operation status
00411   */
00412 WIFI_Status_t WIFI_SetOEMProperties(const char *name, uint8_t *Mac)
00413 {
00414   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00415   
00416   if(ES_WIFI_SetProductName(&EsWifiObj, (uint8_t *)name) == ES_WIFI_STATUS_OK)
00417   {
00418     if(ES_WIFI_SetMACAddress(&EsWifiObj, Mac) == ES_WIFI_STATUS_OK)
00419     {
00420       ret = WIFI_STATUS_OK;
00421     }
00422   }
00423   return ret;
00424 }
00425 
00426 /**
00427   * @brief  Reset the WIFI module
00428   * @retval Operation status
00429   */
00430 WIFI_Status_t WIFI_ResetModule(void)
00431 {
00432   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00433   
00434   if(ES_WIFI_ResetModule(&EsWifiObj) == ES_WIFI_STATUS_OK)
00435   {
00436       ret = WIFI_STATUS_OK;
00437   }
00438   return ret;
00439 }
00440 
00441 /**
00442   * @brief  Restore module default configuration
00443   * @retval Operation status
00444   */
00445 WIFI_Status_t WIFI_SetModuleDefault(void)
00446 {
00447   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00448   
00449   if(ES_WIFI_ResetToFactoryDefault(&EsWifiObj) == ES_WIFI_STATUS_OK)
00450   {
00451       ret = WIFI_STATUS_OK;
00452   }
00453   return ret;
00454 }
00455 
00456 
00457 /**
00458   * @brief  Update module firmware
00459   * @param  location : Binary Location IP address
00460   * @retval Operation status
00461   */
00462 WIFI_Status_t WIFI_ModuleFirmwareUpdate(const char *location)
00463 {
00464   return WIFI_STATUS_NOT_SUPPORTED;
00465 }
00466 
00467 /**
00468   * @brief  Return Module firmware revision
00469   * @param  rev : revision string
00470   * @retval Operation status
00471   */
00472 WIFI_Status_t WIFI_GetModuleFwRevision(char *rev)
00473 {
00474   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00475   
00476   if(EsWifiObj.FW_Rev != NULL)
00477   {
00478     strncpy(rev, (char *)EsWifiObj.FW_Rev, ES_WIFI_FW_REV_SIZE);
00479     ret = WIFI_STATUS_OK; 
00480   }
00481   return ret;
00482 }
00483 
00484 /**
00485   * @brief  Return Module ID
00486   * @param  Info : Module ID string
00487   * @retval Operation status
00488   */
00489 WIFI_Status_t WIFI_GetModuleID(char *Id)
00490 {
00491   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00492   
00493   if(EsWifiObj.Product_ID != NULL)
00494   {
00495     strncpy(Id, (char *)EsWifiObj.Product_ID, ES_WIFI_PRODUCT_ID_SIZE);
00496     ret = WIFI_STATUS_OK; 
00497   }
00498   return ret;
00499 }
00500 
00501 /**
00502   * @brief  Return Module Name
00503   * @param  Info : Module Name string
00504   * @retval Operation status
00505   */
00506 WIFI_Status_t WIFI_GetModuleName(char *ModuleName)
00507 {
00508   WIFI_Status_t ret = WIFI_STATUS_ERROR; 
00509   
00510   if(EsWifiObj.Product_Name != NULL)
00511   {
00512     strncpy(ModuleName, (char *)EsWifiObj.Product_Name, ES_WIFI_PRODUCT_NAME_SIZE);
00513     ret = WIFI_STATUS_OK; 
00514   }
00515   return ret;
00516 }
00517 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00518