Vodafone K3770/K3772-Z modems driver & networking library

Dependencies:   Socket USBHostWANDongle lwip-sys lwip

Dependents:   VodafoneUSBModemHTTPClientTest VodafoneUSBModemNTPClientTest VodafoneUSBModemSMSTest VodafoneUSBModemUSSDTest ... more

Fork of VodafoneUSBModem_bleedingedge by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MtxCircBuffer.h Source File

MtxCircBuffer.h

00001 /* MtxCircBuf.h */
00002 /* Copyright (C) 2012 mbed.org, MIT License
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00005  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00006  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00007  * sublicense, and/or sell 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 copies or
00011  * substantial portions of the Software.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00014  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00015  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00016  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00017  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00018  */
00019 
00020 #ifndef MTXCIRCBUFFER_H
00021 #define MTXCIRCBUFFER_H
00022 
00023 #include "fwk.h"
00024 
00025 #include "rtos.h"
00026 
00027 //Mutex protected circualr buffer
00028 template<typename T, int size>
00029 class MtxCircBuffer
00030 {
00031 public:
00032   MtxCircBuffer() //:
00033       //mtx()
00034   {
00035     write = 0;
00036     read = 0;
00037   }
00038 
00039   bool isFull()
00040   {
00041     mtx.lock();
00042     bool r = (((write + 1) % size) == read);
00043     mtx.unlock();
00044     return r;
00045   }
00046 
00047   bool isEmpty()
00048   {
00049     mtx.lock();
00050     bool r = (read == write);
00051     mtx.unlock();
00052     return r;
00053   }
00054 
00055   void queue(T k)
00056   {
00057     // XXX Some modems spew stuff out on multiple interfaces
00058     // which can cause the circular buffer to block if
00059     // the unread queues are not flushed. Need to make this
00060     // non-blocking: just overwrite oldest chars for example.
00061     mtx.lock();
00062     while (((write + 1) % size) == read) //if (isFull())
00063     {
00064       /*while((((write + 1) % size) == read))
00065       {*/
00066         mtx.unlock();
00067         Thread::wait(10);
00068         mtx.lock();
00069       /*}*/
00070       //read++;
00071       //read %= size;
00072     }
00073     buf[write++] = k;
00074     write %= size;
00075     mtx.unlock();
00076   }
00077 
00078   uint16_t available()
00079   {
00080     mtx.lock();
00081     uint16_t a = (write >= read) ? (write - read) : (size - read + write);
00082     mtx.unlock();
00083     return a;
00084   }
00085 
00086   bool dequeue(T * c)
00087   {
00088     mtx.lock();
00089     bool empty = (read == write);
00090     if (!empty)
00091     {
00092       *c = buf[read++];
00093       read %= size;
00094     }
00095     mtx.unlock();
00096     return (!empty);
00097   }
00098 
00099 private:
00100   volatile uint16_t write;
00101   volatile uint16_t read;
00102   volatile T buf[size];
00103   Mutex mtx;
00104 };
00105 
00106 #endif
00107