PHS module SMA-01 library. see: https://developer.mbed.org/users/phsfan/notebook/abitusbmodem/

Dependencies:   Socket lwip-sys lwip

Dependents:   AbitUSBModem_HTTPTest AbitUSBModem_MQTTTest AbitUSBModem_WebsocketTest AbitUSBModem_SMSTest

Fork of VodafoneUSBModem by mbed official

/media/uploads/phsfan/sma01_003.png

Committer:
phsfan
Date:
Wed Feb 25 14:34:13 2015 +0000
Revision:
99:514e67a69ad6
Parent:
97:7d9cc95e2ea7
supported SMS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phsfan 97:7d9cc95e2ea7 1 /* MtxCircBuf.h */
phsfan 97:7d9cc95e2ea7 2 /* Copyright (C) 2012 mbed.org, MIT License
phsfan 97:7d9cc95e2ea7 3 *
phsfan 97:7d9cc95e2ea7 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
phsfan 97:7d9cc95e2ea7 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
phsfan 97:7d9cc95e2ea7 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
phsfan 97:7d9cc95e2ea7 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
phsfan 97:7d9cc95e2ea7 8 * furnished to do so, subject to the following conditions:
phsfan 97:7d9cc95e2ea7 9 *
phsfan 97:7d9cc95e2ea7 10 * The above copyright notice and this permission notice shall be included in all copies or
phsfan 97:7d9cc95e2ea7 11 * substantial portions of the Software.
phsfan 97:7d9cc95e2ea7 12 *
phsfan 97:7d9cc95e2ea7 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
phsfan 97:7d9cc95e2ea7 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
phsfan 97:7d9cc95e2ea7 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
phsfan 97:7d9cc95e2ea7 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
phsfan 97:7d9cc95e2ea7 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
phsfan 97:7d9cc95e2ea7 18 */
phsfan 97:7d9cc95e2ea7 19
phsfan 97:7d9cc95e2ea7 20 #ifndef MTXCIRCBUFFER_H
phsfan 97:7d9cc95e2ea7 21 #define MTXCIRCBUFFER_H
phsfan 97:7d9cc95e2ea7 22
phsfan 97:7d9cc95e2ea7 23 #include "fwk.h"
phsfan 97:7d9cc95e2ea7 24
phsfan 97:7d9cc95e2ea7 25 #include "rtos.h"
phsfan 97:7d9cc95e2ea7 26
phsfan 97:7d9cc95e2ea7 27 //Mutex protected circualr buffer
phsfan 97:7d9cc95e2ea7 28 template<typename T, int size>
phsfan 97:7d9cc95e2ea7 29 class MtxCircBuffer
phsfan 97:7d9cc95e2ea7 30 {
phsfan 97:7d9cc95e2ea7 31 public:
phsfan 97:7d9cc95e2ea7 32 MtxCircBuffer() //:
phsfan 97:7d9cc95e2ea7 33 //mtx()
phsfan 97:7d9cc95e2ea7 34 {
phsfan 97:7d9cc95e2ea7 35 write = 0;
phsfan 97:7d9cc95e2ea7 36 read = 0;
phsfan 97:7d9cc95e2ea7 37 }
phsfan 97:7d9cc95e2ea7 38
phsfan 97:7d9cc95e2ea7 39 bool isFull()
phsfan 97:7d9cc95e2ea7 40 {
phsfan 97:7d9cc95e2ea7 41 mtx.lock();
phsfan 97:7d9cc95e2ea7 42 bool r = (((write + 1) % size) == read);
phsfan 97:7d9cc95e2ea7 43 mtx.unlock();
phsfan 97:7d9cc95e2ea7 44 return r;
phsfan 97:7d9cc95e2ea7 45 }
phsfan 97:7d9cc95e2ea7 46
phsfan 97:7d9cc95e2ea7 47 bool isEmpty()
phsfan 97:7d9cc95e2ea7 48 {
phsfan 97:7d9cc95e2ea7 49 mtx.lock();
phsfan 97:7d9cc95e2ea7 50 bool r = (read == write);
phsfan 97:7d9cc95e2ea7 51 mtx.unlock();
phsfan 97:7d9cc95e2ea7 52 return r;
phsfan 97:7d9cc95e2ea7 53 }
phsfan 97:7d9cc95e2ea7 54
phsfan 97:7d9cc95e2ea7 55 void queue(T k)
phsfan 97:7d9cc95e2ea7 56 {
phsfan 97:7d9cc95e2ea7 57 // XXX Some modems spew stuff out on multiple interfaces
phsfan 97:7d9cc95e2ea7 58 // which can cause the circular buffer to block if
phsfan 97:7d9cc95e2ea7 59 // the unread queues are not flushed. Need to make this
phsfan 97:7d9cc95e2ea7 60 // non-blocking: just overwrite oldest chars for example.
phsfan 97:7d9cc95e2ea7 61 mtx.lock();
phsfan 97:7d9cc95e2ea7 62 while (((write + 1) % size) == read) //if (isFull())
phsfan 97:7d9cc95e2ea7 63 {
phsfan 97:7d9cc95e2ea7 64 /*while((((write + 1) % size) == read))
phsfan 97:7d9cc95e2ea7 65 {*/
phsfan 97:7d9cc95e2ea7 66 mtx.unlock();
phsfan 97:7d9cc95e2ea7 67 Thread::wait(10);
phsfan 97:7d9cc95e2ea7 68 mtx.lock();
phsfan 97:7d9cc95e2ea7 69 /*}*/
phsfan 97:7d9cc95e2ea7 70 //read++;
phsfan 97:7d9cc95e2ea7 71 //read %= size;
phsfan 97:7d9cc95e2ea7 72 }
phsfan 97:7d9cc95e2ea7 73 buf[write++] = k;
phsfan 97:7d9cc95e2ea7 74 write %= size;
phsfan 97:7d9cc95e2ea7 75 mtx.unlock();
phsfan 97:7d9cc95e2ea7 76 }
phsfan 97:7d9cc95e2ea7 77
phsfan 97:7d9cc95e2ea7 78 uint16_t available()
phsfan 97:7d9cc95e2ea7 79 {
phsfan 97:7d9cc95e2ea7 80 mtx.lock();
phsfan 97:7d9cc95e2ea7 81 uint16_t a = (write >= read) ? (write - read) : (size - read + write);
phsfan 97:7d9cc95e2ea7 82 mtx.unlock();
phsfan 97:7d9cc95e2ea7 83 return a;
phsfan 97:7d9cc95e2ea7 84 }
phsfan 97:7d9cc95e2ea7 85
phsfan 97:7d9cc95e2ea7 86 bool dequeue(T * c)
phsfan 97:7d9cc95e2ea7 87 {
phsfan 97:7d9cc95e2ea7 88 mtx.lock();
phsfan 97:7d9cc95e2ea7 89 bool empty = (read == write);
phsfan 97:7d9cc95e2ea7 90 if (!empty)
phsfan 97:7d9cc95e2ea7 91 {
phsfan 97:7d9cc95e2ea7 92 *c = buf[read++];
phsfan 97:7d9cc95e2ea7 93 read %= size;
phsfan 97:7d9cc95e2ea7 94 }
phsfan 97:7d9cc95e2ea7 95 mtx.unlock();
phsfan 97:7d9cc95e2ea7 96 return (!empty);
phsfan 97:7d9cc95e2ea7 97 }
phsfan 97:7d9cc95e2ea7 98
phsfan 97:7d9cc95e2ea7 99 private:
phsfan 97:7d9cc95e2ea7 100 volatile uint16_t write;
phsfan 97:7d9cc95e2ea7 101 volatile uint16_t read;
phsfan 97:7d9cc95e2ea7 102 volatile T buf[size];
phsfan 97:7d9cc95e2ea7 103 Mutex mtx;
phsfan 97:7d9cc95e2ea7 104 };
phsfan 97:7d9cc95e2ea7 105
phsfan 97:7d9cc95e2ea7 106 #endif
phsfan 97:7d9cc95e2ea7 107