ST / X_NUCLEO_IDW01M1

Dependents:   SpwfInterface_NSAPI_Tests HelloWorld_IDW01M1

Fork of X_NUCLEO_IDW01M1 by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers event_buffer.c Source File

event_buffer.c

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    event_buffer.c
00004  * @author  Central LAB
00005  * @version V2.0.0
00006  * @date    10-February-2016
00007  * @brief   Implements the Event Buffer management of the Wi-Fi module
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include "event_buffer.h"
00041 #include "wifi_module.h"
00042 
00043 /** @addtogroup BSP
00044 * @{
00045 */ 
00046 
00047 
00048 /** @defgroup  NUCLEO_WIFI_BUFFER_MGMT
00049   * @brief Wi-Fi_driver modules
00050   * @{
00051   */ 
00052 
00053 
00054 /** @defgroup NUCLEO_WIFI_BUFFER_MGMT_Private_Defines
00055   * @{
00056   */
00057 
00058 /**
00059   * @}
00060   */
00061 
00062 /** @defgroup NUCLEO_WIFI_BUFFER_MGMT_Private_Variables
00063   * @{
00064   */
00065 extern event_s_TypeDef event_buffer[1024];
00066 #define ELEMENT_SIZE sizeof(event_s_TypeDef)   //1
00067 
00068 //extern UART_HandleTypeDef UartMsgHandle;
00069 //extern char print_msg_buff[512];
00070 extern event_s_TypeDef element;
00071 
00072 /*
00073 #ifdef USART_PRINT_MSG
00074 #define printf(arg)    sprintf((char*)print_msg_buff,arg);   \
00075                        HAL_UART_Transmit(&UartMsgHandle, (uint8_t*)print_msg_buff, strlen(print_msg_buff), 1000);
00076 #endif 
00077 */
00078                        
00079 /**
00080   * @}
00081   */
00082 
00083 /** @defgroup NUCLEO_WIFI_BUFFER_MGMT_Private_Functions
00084   * @{
00085   */
00086 
00087 /**
00088   * @brief  init
00089   *         Initialize a circular buffer of type buffer_t
00090   * @param  None
00091   * @retval None
00092   */
00093 void event_init(buffer_e *buffer, int size) {
00094     uint32_t element_size;
00095     element_size = sizeof(event_s_TypeDef);
00096     buffer->size = element_size*size;
00097     buffer->start = 0;
00098     buffer->count = 0;
00099     buffer->end = 0;
00100     buffer->element = event_buffer;
00101 }
00102 
00103 /**
00104   * @brief  full
00105   *         indicates if the given buffer is full or not
00106   * @param  None
00107   * @retval None
00108   */
00109 int event_full(buffer_e *buffer) 
00110 {
00111   int bufsize = buffer->size;
00112   if (buffer->count == (bufsize/ELEMENT_SIZE)) 
00113   { 
00114     return 1;
00115   } 
00116   else 
00117   {
00118     return 0;
00119   }
00120 }
00121 
00122 /**
00123   * @brief  empty
00124   *         indicates if the given buffer is empty or not
00125   * @param  None
00126   * @retval None
00127   */
00128 int event_empty(buffer_e *buffer) {
00129     if (buffer->count == 0) {
00130         return 1;
00131     } else {
00132         return 0;
00133     }
00134 }
00135 
00136 /**
00137   * @brief  push_buffer
00138   *         pushes the data structure onto the circular buffer (queues it)
00139   * @param  None
00140   * @retval None
00141   */
00142 void push_eventbuffer(buffer_e *buffer, event_s_TypeDef data) 
00143 {
00144   int bufsize;
00145   uint32_t index;
00146   
00147   if (event_full(buffer)) 
00148   {     
00149     //Buffer overflow and no more space
00150     //MPD: No Action taken here; in case of buffer overflow, do we need to overwrite last buffer?
00151     //printf("\r\nRing Buffer Full!!\r\n");
00152     //printf(data);
00153     return;
00154   } else 
00155   {
00156     index=buffer->end/ELEMENT_SIZE;
00157     buffer->element[index].data_length = data.data_length;
00158     buffer->element[index].wind64_pending_packet_no = data.wind64_pending_packet_no;
00159     buffer->element[index].enc = data.enc;
00160     buffer->element[index].ok_eval = data.ok_eval;
00161     buffer->element[index].socket_id = data.socket_id;
00162     buffer->element[index].wind = data.wind;
00163     buffer->element[index].event = data.event;
00164     buffer->element[index].event_pop = data.event_pop;
00165     buffer->count++;
00166     buffer->end = buffer->end + ELEMENT_SIZE;
00167     
00168     //wrap around if max size is reached
00169     bufsize = (buffer->size);
00170     if (buffer->end >= bufsize) 
00171     {
00172       buffer->end = 0;
00173     }
00174   }
00175 }
00176 
00177 /**
00178   * @brief  pop_buffer_queue
00179   *         dequeues the circular buffer
00180   * @param  None
00181   * @retval None
00182   */ 
00183 event_s_TypeDef * pop_eventbuffer_queue(buffer_e *buffer) 
00184 {
00185   int bufsize;
00186   uint32_t index;
00187 
00188   if (event_empty(buffer)) 
00189   {
00190     //printf("\r\Event Buffer Empty!!\r\n");
00191     return NULL;
00192   } 
00193   else 
00194   {       
00195     /* First in First Out*/
00196     index=buffer->start/ELEMENT_SIZE;
00197     element.data_length = buffer->element[index].data_length;
00198     element.enc = buffer->element[index].enc;
00199     element.ok_eval = buffer->element[index].ok_eval;
00200     element.socket_id = buffer->element[index].socket_id;
00201     element.wind64_pending_packet_no = buffer->element[index].wind64_pending_packet_no;
00202     element.wind = buffer->element[index].wind;
00203     element.event = buffer->element[index].event;
00204     element.event_pop = buffer->element[index].event_pop;
00205     buffer->start = buffer->start + ELEMENT_SIZE;
00206     buffer->count--;
00207 
00208     bufsize = (buffer->size);
00209     if (buffer->start >= bufsize) 
00210     {
00211       buffer->start = 0;
00212     }    
00213     return &element;
00214   }
00215 }
00216 
00217 void reset_event(event_s_TypeDef *r_event)
00218 {
00219   r_event->data_length = 0;
00220   r_event->enc         = WIFI_FALSE;
00221   r_event->event       = WIFI_NO_EVENT; 
00222   r_event->ok_eval     = WIFI_FALSE;
00223   r_event->socket_id   = 9; //Socket ID 0-7
00224   r_event->wind64_pending_packet_no = 9; //Max pending packets = 4
00225   r_event->wind        = 99; //any default value
00226   r_event->event_pop   = WIFI_TRUE;
00227 }
00228 
00229 /**
00230   * @}
00231   */ 
00232 
00233 /**
00234   * @}
00235   */ 
00236 
00237 
00238 /**
00239   * @}
00240   */ 
00241 
00242 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/