added debugging

Fork of BLE_nRF8001 by RedBearLab

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers aci_queue.cpp Source File

aci_queue.cpp

Go to the documentation of this file.
00001 /* Copyright (c) 2014, Nordic Semiconductor ASA
00002  *
00003  * Permission is hereby granted, free of charge, to any person obtaining a copy
00004  * of this software and associated documentation files (the "Software"), to deal
00005  * in the Software without restriction, including without limitation the rights
00006  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00007  * copies of the Software, and to permit persons to whom the Software is
00008  * furnished to do so, subject to the following conditions:
00009  *
00010  * The above copyright notice and this permission notice shall be included in all
00011  * copies or substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00014  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00015  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00016  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00017  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00018  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
00019  * SOFTWARE.
00020  */
00021 
00022  /** @file
00023 @brief Implementation of a circular queue for ACI data
00024 */
00025 
00026 #include "hal_aci_tl.h"
00027 #include "aci_queue.h"
00028 #include "ble_assert.h"
00029 
00030 void aci_queue_init(aci_queue_t *aci_q)
00031 {
00032   uint8_t loop;
00033 
00034   ble_assert(NULL != aci_q);
00035 
00036   aci_q->head = 0;
00037   aci_q->tail = 0;
00038   for(loop=0; loop<ACI_QUEUE_SIZE; loop++)
00039   {
00040     aci_q->aci_data[loop].buffer[0] = 0x00;
00041     aci_q->aci_data[loop].buffer[1] = 0x00;
00042   }
00043 }
00044 
00045 bool aci_queue_dequeue(aci_queue_t *aci_q, hal_aci_data_t *p_data)
00046 {
00047   ble_assert(NULL != aci_q);
00048   ble_assert(NULL != p_data);
00049 
00050   if (aci_queue_is_empty(aci_q))
00051   {
00052     return false;
00053   }
00054 
00055   memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t));
00056   aci_q->head = (aci_q->head + 1) % ACI_QUEUE_SIZE;
00057 
00058   return true;
00059 }
00060 
00061 bool aci_queue_dequeue_from_isr(aci_queue_t *aci_q, hal_aci_data_t *p_data)
00062 {
00063   ble_assert(NULL != aci_q);
00064   ble_assert(NULL != p_data);
00065 
00066   if (aci_queue_is_empty_from_isr(aci_q))
00067   {
00068     return false;
00069   }
00070 
00071   memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t));
00072   aci_q->head = (aci_q->head + 1) % ACI_QUEUE_SIZE;
00073 
00074   return true;
00075 }
00076 
00077 bool aci_queue_enqueue(aci_queue_t *aci_q, hal_aci_data_t *p_data)
00078 {
00079   const uint8_t length = p_data->buffer[0];
00080 
00081   ble_assert(NULL != aci_q);
00082   ble_assert(NULL != p_data);
00083 
00084   if (aci_queue_is_full(aci_q))
00085   {
00086     return false;
00087   }
00088 
00089   aci_q->aci_data[aci_q->tail].status_byte = 0;
00090   memcpy((uint8_t *)&(aci_q->aci_data[aci_q->tail].buffer[0]), (uint8_t *)&p_data->buffer[0], length + 1);
00091   aci_q->tail = (aci_q->tail + 1) % ACI_QUEUE_SIZE;
00092 
00093   return true;
00094 }
00095 
00096 bool aci_queue_enqueue_from_isr(aci_queue_t *aci_q, hal_aci_data_t *p_data)
00097 {
00098   const uint8_t length = p_data->buffer[0];
00099 
00100   ble_assert(NULL != aci_q);
00101   ble_assert(NULL != p_data);
00102 
00103   if (aci_queue_is_full_from_isr(aci_q))
00104   {
00105     return false;
00106   }
00107 
00108   aci_q->aci_data[aci_q->tail].status_byte = 0;
00109   memcpy((uint8_t *)&(aci_q->aci_data[aci_q->tail].buffer[0]), (uint8_t *)&p_data->buffer[0], length + 1);
00110   aci_q->tail = (aci_q->tail + 1) % ACI_QUEUE_SIZE;
00111 
00112   return true;
00113 }
00114 
00115 bool aci_queue_is_empty(aci_queue_t *aci_q)
00116 {
00117   bool state = false;
00118 
00119   ble_assert(NULL != aci_q);
00120 
00121   //Critical section
00122   //noInterrupts();
00123   if (aci_q->head == aci_q->tail)
00124   {
00125     state = true;
00126   }
00127   //interrupts();
00128 
00129   return state;
00130 }
00131 
00132 bool aci_queue_is_empty_from_isr(aci_queue_t *aci_q)
00133 {
00134   ble_assert(NULL != aci_q);
00135 
00136   return aci_q->head == aci_q->tail;
00137 }
00138 
00139 bool aci_queue_is_full(aci_queue_t *aci_q)
00140 {
00141   uint8_t next;
00142   bool state;
00143 
00144   ble_assert(NULL != aci_q);
00145 
00146   //This should be done in a critical section
00147   //noInterrupts();
00148   next = (aci_q->tail + 1) % ACI_QUEUE_SIZE;
00149 
00150   if (next == aci_q->head)
00151   {
00152     state = true;
00153   }
00154   else
00155   {
00156     state = false;
00157   }
00158 
00159   //interrupts();
00160   //end
00161 
00162   return state;
00163 }
00164 
00165 bool aci_queue_is_full_from_isr(aci_queue_t *aci_q)
00166 {
00167   const uint8_t next = (aci_q->tail + 1) % ACI_QUEUE_SIZE;
00168 
00169   ble_assert(NULL != aci_q);
00170 
00171   return next == aci_q->head;
00172 }
00173 
00174 bool aci_queue_peek(aci_queue_t *aci_q, hal_aci_data_t *p_data)
00175 {
00176   ble_assert(NULL != aci_q);
00177   ble_assert(NULL != p_data);
00178 
00179   if (aci_queue_is_empty(aci_q))
00180   {
00181     return false;
00182   }
00183 
00184   memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t));
00185 
00186   return true;
00187 }
00188 
00189 bool aci_queue_peek_from_isr(aci_queue_t *aci_q, hal_aci_data_t *p_data)
00190 {
00191   ble_assert(NULL != aci_q);
00192   ble_assert(NULL != p_data);
00193 
00194   if (aci_queue_is_empty_from_isr(aci_q))
00195   {
00196     return false;
00197   }
00198 
00199   memcpy((uint8_t *)p_data, (uint8_t *)&(aci_q->aci_data[aci_q->head]), sizeof(hal_aci_data_t));
00200 
00201   return true;
00202 }