[136] | 1 | /*
|
---|
| 2 | This file is part of the GSM3 communications library for Arduino
|
---|
| 3 | -- Multi-transport communications platform
|
---|
| 4 | -- Fully asynchronous
|
---|
| 5 | -- Includes code for the Arduino-Telefonica GSM/GPRS Shield V1
|
---|
| 6 | -- Voice calls
|
---|
| 7 | -- SMS
|
---|
| 8 | -- TCP/IP connections
|
---|
| 9 | -- HTTP basic clients
|
---|
| 10 |
|
---|
| 11 | This library has been developed by Telefónica Digital - PDI -
|
---|
| 12 | - Physical Internet Lab, as part as its collaboration with
|
---|
| 13 | Arduino and the Open Hardware Community.
|
---|
| 14 |
|
---|
| 15 | September-December 2012
|
---|
| 16 |
|
---|
| 17 | This library is free software; you can redistribute it and/or
|
---|
| 18 | modify it under the terms of the GNU Lesser General Public
|
---|
| 19 | License as published by the Free Software Foundation; either
|
---|
| 20 | version 2.1 of the License, or (at your option) any later version.
|
---|
| 21 |
|
---|
| 22 | This library is distributed in the hope that it will be useful,
|
---|
| 23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
| 25 | Lesser General Public License for more details.
|
---|
| 26 |
|
---|
| 27 | You should have received a copy of the GNU Lesser General Public
|
---|
| 28 | License along with this library; if not, write to the Free Software
|
---|
| 29 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
| 30 |
|
---|
| 31 | The latest version of this library can always be found at
|
---|
| 32 | https://github.com/BlueVia/Official-Arduino
|
---|
| 33 | */
|
---|
| 34 | #ifndef __GSM3_SOFTSERIAL__
|
---|
| 35 | #define __GSM3_SOFTSERIAL__
|
---|
| 36 |
|
---|
| 37 | // An adaptation of NewSoftSerial for Modem Shields
|
---|
| 38 | // Assumes directly that Serial is attached to Pins 2 and 3, not inverse
|
---|
| 39 | // We are implementing it because NewSoftSerial does not deal correctly with floods
|
---|
| 40 | // of data
|
---|
| 41 | #include "GSM3CircularBuffer.h"
|
---|
| 42 | #include <avr/pgmspace.h>
|
---|
| 43 |
|
---|
| 44 | /*
|
---|
| 45 | #define _COMSTATUS_ANSWERRECEIVED_ 0x100
|
---|
| 46 | #define _COMSTATUS_SMSRECEIVED_ 0x80
|
---|
| 47 | #define _COMSTATUS_CALLRECEIVED_ 0x40
|
---|
| 48 |
|
---|
| 49 | // PLEASE, when accessing the sockets use "for" and >> (bitwise operator)
|
---|
| 50 | #define _COMSTATUS_SOCKET6RECEIVED_ 0x20
|
---|
| 51 | #define _COMSTATUS_SOCKET5RECEIVED_ 0x10
|
---|
| 52 | #define _COMSTATUS_SOCKET4RECEIVED_ 0x08
|
---|
| 53 | #define _COMSTATUS_SOCKET3RECEIVED_ 0x04
|
---|
| 54 | #define _COMSTATUS_SOCKET2RECEIVED_ 0x02
|
---|
| 55 | #define _COMSTATUS_SOCKET1RECEIVED_ 0x01
|
---|
| 56 |
|
---|
| 57 | #define __CALLTABLEMASK__ 0x3
|
---|
| 58 | */
|
---|
| 59 |
|
---|
| 60 | class GSM3SoftSerialMgr
|
---|
| 61 | {
|
---|
| 62 | public:
|
---|
| 63 |
|
---|
| 64 | /** Manages soft serial message
|
---|
| 65 | @param from Initial byte
|
---|
| 66 | @param to Final byte
|
---|
| 67 | */
|
---|
| 68 | virtual void manageMsg(byte from, byte to);
|
---|
| 69 | };
|
---|
| 70 |
|
---|
| 71 | // This class manages software serial communications
|
---|
| 72 | // Changing it so it doesn't know about modems or whatever
|
---|
| 73 |
|
---|
| 74 | class GSM3SoftSerial : public GSM3CircularBufferManager
|
---|
| 75 | {
|
---|
| 76 | private:
|
---|
| 77 |
|
---|
| 78 | uint8_t _receiveBitMask;
|
---|
| 79 | volatile uint8_t *_receivePortRegister;
|
---|
| 80 | uint8_t _transmitBitMask;
|
---|
| 81 | volatile uint8_t *_transmitPortRegister;
|
---|
| 82 |
|
---|
| 83 | static GSM3SoftSerial* _activeObject;
|
---|
| 84 | GSM3SoftSerialMgr* mgr;
|
---|
| 85 |
|
---|
| 86 | uint16_t _rx_delay_centering;
|
---|
| 87 | uint16_t _rx_delay_intrabit;
|
---|
| 88 | uint16_t _rx_delay_stopbit;
|
---|
| 89 | uint16_t _tx_delay;
|
---|
| 90 | uint8_t _flags;
|
---|
| 91 |
|
---|
| 92 | /** Write in tx_pin
|
---|
| 93 | @param pin_state Pin state
|
---|
| 94 | */
|
---|
| 95 | void tx_pin_write(uint8_t pin_state);
|
---|
| 96 |
|
---|
| 97 | /** Set transmission
|
---|
| 98 | */
|
---|
| 99 | void setTX();
|
---|
| 100 |
|
---|
| 101 | /** Set receiver
|
---|
| 102 | */
|
---|
| 103 | void setRX();
|
---|
| 104 |
|
---|
| 105 | /** Receive
|
---|
| 106 | */
|
---|
| 107 | void recv();
|
---|
| 108 |
|
---|
| 109 | /** Read from rx_pin
|
---|
| 110 | @return receive bit mask
|
---|
| 111 | */
|
---|
| 112 | uint8_t rx_pin_read();
|
---|
| 113 |
|
---|
| 114 | void setComsReceived();
|
---|
| 115 |
|
---|
| 116 | /** Write a character in serial connection, final action after escaping
|
---|
| 117 | @param c Character
|
---|
| 118 | @return 1 if succesful, 0 if transmission delay = 0
|
---|
| 119 | */
|
---|
| 120 | virtual size_t finalWrite(uint8_t);
|
---|
| 121 |
|
---|
| 122 | /** Decide, attending to escapes, if the received character should we
|
---|
| 123 | kept, forgotten, or changed
|
---|
| 124 | @param c Character, may be changed
|
---|
| 125 | @return 1 if shall be kept, 0 if forgotten
|
---|
| 126 | */
|
---|
| 127 | bool keepThisChar(uint8_t* c);
|
---|
| 128 |
|
---|
| 129 | // Checks the buffer for well-known events.
|
---|
| 130 | //bool recognizeUnsolicitedEvent(byte oldTail);
|
---|
| 131 |
|
---|
| 132 | public:
|
---|
| 133 |
|
---|
| 134 | /** Tuned delay in microcontroller
|
---|
| 135 | @param delay Time to delay
|
---|
| 136 | */
|
---|
| 137 | static /*inline */void tunedDelay(uint16_t delay);
|
---|
| 138 |
|
---|
| 139 | GSM3CircularBuffer cb; // Circular buffer
|
---|
| 140 |
|
---|
| 141 | /** Register serial manager
|
---|
| 142 | @param manager Serial manager
|
---|
| 143 | */
|
---|
| 144 | inline void registerMgr(GSM3SoftSerialMgr* manager){mgr=manager;};
|
---|
| 145 |
|
---|
| 146 | /** If there is spaceAvailable in the buffer, lets send a XON
|
---|
| 147 | */
|
---|
| 148 | void spaceAvailable();
|
---|
| 149 |
|
---|
| 150 | /** Write a character in serial connection
|
---|
| 151 | @param c Character
|
---|
| 152 | @return 1 if succesful, 0 if transmission delay = 0
|
---|
| 153 | */
|
---|
| 154 | virtual size_t write(uint8_t);
|
---|
| 155 |
|
---|
| 156 | /** Constructor */
|
---|
| 157 | GSM3SoftSerial();
|
---|
| 158 |
|
---|
| 159 | /** Establish serial connection
|
---|
| 160 | @param speed Baudrate
|
---|
| 161 | @return
|
---|
| 162 | */
|
---|
| 163 | int begin(long speed);
|
---|
| 164 |
|
---|
| 165 | /** Manage interruptions
|
---|
| 166 | */
|
---|
| 167 | static inline void handle_interrupt();
|
---|
| 168 |
|
---|
| 169 | /** Close serial connection
|
---|
| 170 | */
|
---|
| 171 | void close();
|
---|
| 172 | };
|
---|
| 173 |
|
---|
| 174 | #endif
|
---|