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 |
|
---|
35 | #ifndef __GSM3_SHIELDV1MODEMCORE__
|
---|
36 | #define __GSM3_SHIELDV1MODEMCORE__
|
---|
37 |
|
---|
38 | #include <GSM3SoftSerial.h>
|
---|
39 | #include <GSM3ShieldV1BaseProvider.h>
|
---|
40 | #include <GSM3MobileAccessProvider.h>
|
---|
41 | #include <Print.h>
|
---|
42 |
|
---|
43 | #define UMPROVIDERS 3
|
---|
44 |
|
---|
45 | class GSM3ShieldV1ModemCore : public GSM3SoftSerialMgr, public Print
|
---|
46 | {
|
---|
47 | private:
|
---|
48 |
|
---|
49 | // Phone number, used when calling, sending SMS and reading calling numbers
|
---|
50 | // Also PIN in modem configuration
|
---|
51 | // Also APN
|
---|
52 | // Also remote server
|
---|
53 | char* phoneNumber;
|
---|
54 |
|
---|
55 | // Working port. Port used in the ongoing command, while opening a server
|
---|
56 | // Also for IP address length
|
---|
57 | int port;
|
---|
58 |
|
---|
59 | // 0 = ongoing
|
---|
60 | // 1 = OK
|
---|
61 | // 2 = Error. Incorrect state
|
---|
62 | // 3 = Unexpected modem message
|
---|
63 | // 4 = OK but not available data.
|
---|
64 | uint8_t commandError;
|
---|
65 |
|
---|
66 | // Counts the steps by the command
|
---|
67 | uint8_t commandCounter;
|
---|
68 |
|
---|
69 | // Presently ongoing command
|
---|
70 | GSM3_commandType_e ongoingCommand;
|
---|
71 |
|
---|
72 | // Enable/disable debug
|
---|
73 | bool _debug;
|
---|
74 | byte _dataInBufferFrom;
|
---|
75 | byte _dataInBufferTo;
|
---|
76 |
|
---|
77 | // This is the modem (known) status
|
---|
78 | GSM3_NetworkStatus_t _status;
|
---|
79 |
|
---|
80 | GSM3ShieldV1BaseProvider* UMProvider[UMPROVIDERS];
|
---|
81 | GSM3ShieldV1BaseProvider* activeProvider;
|
---|
82 |
|
---|
83 | // Private function for anage message
|
---|
84 | void manageMsgNow(byte from, byte to);
|
---|
85 |
|
---|
86 | unsigned long milliseconds;
|
---|
87 |
|
---|
88 | public:
|
---|
89 |
|
---|
90 | /** Constructor */
|
---|
91 | GSM3ShieldV1ModemCore();
|
---|
92 |
|
---|
93 | GSM3SoftSerial gss; // Direct access to modem
|
---|
94 |
|
---|
95 | /** Get phone number
|
---|
96 | @return phone number
|
---|
97 | */
|
---|
98 | char *getPhoneNumber(){return phoneNumber;};
|
---|
99 |
|
---|
100 | /** Establish a new phone number
|
---|
101 | @param n Phone number
|
---|
102 | */
|
---|
103 | void setPhoneNumber(char *n){phoneNumber=n;};
|
---|
104 |
|
---|
105 | /** Get port used
|
---|
106 | @return port
|
---|
107 | */
|
---|
108 | int getPort(){return port;};
|
---|
109 |
|
---|
110 | /** Establish a new port for use
|
---|
111 | @param p Port
|
---|
112 | */
|
---|
113 | void setPort(int p){port=p;};
|
---|
114 |
|
---|
115 | /** Get command error
|
---|
116 | @return command error
|
---|
117 | */
|
---|
118 | uint8_t getCommandError(){return commandError;};
|
---|
119 |
|
---|
120 | /** Establish a command error
|
---|
121 | @param n Command error
|
---|
122 | */
|
---|
123 | void setCommandError(uint8_t n){commandError=n;};
|
---|
124 |
|
---|
125 | /** Get command counter
|
---|
126 | @return command counter
|
---|
127 | */
|
---|
128 | uint8_t getCommandCounter(){return commandCounter;};
|
---|
129 |
|
---|
130 | /** Set command counter
|
---|
131 | @param c Initial value
|
---|
132 | */
|
---|
133 | void setCommandCounter(uint8_t c){commandCounter=c;};
|
---|
134 |
|
---|
135 | /** Get ongoing command
|
---|
136 | @return command
|
---|
137 | */
|
---|
138 | GSM3_commandType_e getOngoingCommand(){return ongoingCommand;};
|
---|
139 |
|
---|
140 | /** Set ongoing command
|
---|
141 | @param c New ongoing command
|
---|
142 | */
|
---|
143 | void setOngoingCommand(GSM3_commandType_e c){ongoingCommand=c;};
|
---|
144 |
|
---|
145 | /** Open command
|
---|
146 | @param activeProvider Active provider
|
---|
147 | @param c Command for open
|
---|
148 | */
|
---|
149 | void openCommand(GSM3ShieldV1BaseProvider* activeProvider, GSM3_commandType_e c);
|
---|
150 |
|
---|
151 | /** Close command
|
---|
152 | @param code Close code
|
---|
153 | */
|
---|
154 | void closeCommand(int code);
|
---|
155 |
|
---|
156 | // These functions allow writing to the SoftwareSerial
|
---|
157 | // If debug is set, dump to the console
|
---|
158 |
|
---|
159 | /** Write a character in serial
|
---|
160 | @param c Character
|
---|
161 | @return size
|
---|
162 | */
|
---|
163 | size_t write(uint8_t c);
|
---|
164 |
|
---|
165 | /** Write PGM
|
---|
166 | @param str Buffer for write
|
---|
167 | @param CR Carriadge return adding automatically
|
---|
168 | @return size
|
---|
169 | */
|
---|
170 | virtual size_t writePGM(PGM_P str, bool CR=true);
|
---|
171 |
|
---|
172 | /** Establish debug mode
|
---|
173 | @param db Boolean that indicates debug on or off
|
---|
174 | */
|
---|
175 | void setDebug(bool db){_debug=db;};
|
---|
176 |
|
---|
177 | /** Generic response parser
|
---|
178 | @param rsp Returns true if expected response exists
|
---|
179 | @param string Substring expected in response
|
---|
180 | @param string2 Second substring expected in response
|
---|
181 | @return true if parsed correctly
|
---|
182 | */
|
---|
183 | bool genericParse_rsp(bool& rsp, char* string=0, char* string2=0);
|
---|
184 |
|
---|
185 | /** Generates a generic AT command request from PROGMEM buffer
|
---|
186 | @param str Buffer with AT command
|
---|
187 | @param addCR Carriadge return adding automatically
|
---|
188 | */
|
---|
189 | void genericCommand_rq(PGM_P str, bool addCR=true);
|
---|
190 |
|
---|
191 | /** Generates a generic AT command request from a simple char buffer
|
---|
192 | @param str Buffer with AT command
|
---|
193 | @param addCR Carriadge return adding automatically
|
---|
194 | */
|
---|
195 | void genericCommand_rqc(const char* str, bool addCR=true);
|
---|
196 |
|
---|
197 | /** Returns the circular buffer
|
---|
198 | @return circular buffer
|
---|
199 | */
|
---|
200 | inline GSM3CircularBuffer& theBuffer(){return gss.cb;};
|
---|
201 |
|
---|
202 | /** Establish a new network status
|
---|
203 | @param status Network status
|
---|
204 | */
|
---|
205 | inline void setStatus(GSM3_NetworkStatus_t status) { _status = status; };
|
---|
206 |
|
---|
207 | /** Returns actual network status
|
---|
208 | @return network status
|
---|
209 | */
|
---|
210 | inline GSM3_NetworkStatus_t getStatus() { return _status; };
|
---|
211 |
|
---|
212 | /** Register provider as willing to receive unsolicited messages
|
---|
213 | @param provider Pointer to provider able to receive unsolicited messages
|
---|
214 | */
|
---|
215 | void registerUMProvider(GSM3ShieldV1BaseProvider* provider);
|
---|
216 |
|
---|
217 | /** unegister provider as willing to receive unsolicited messages
|
---|
218 | @param provider Pointer to provider able to receive unsolicited messages
|
---|
219 | */
|
---|
220 | void unRegisterUMProvider(GSM3ShieldV1BaseProvider* provider);
|
---|
221 |
|
---|
222 |
|
---|
223 | /** Register a provider as "dialoguing" talking in facto with the modem
|
---|
224 | @param provider Pointer to provider receiving responses
|
---|
225 | */
|
---|
226 | void registerActiveProvider(GSM3ShieldV1BaseProvider* provider){activeProvider=provider;};
|
---|
227 |
|
---|
228 | /** Needed to manage the SoftSerial. Receives the call when received data
|
---|
229 | If _debugging, no code is called
|
---|
230 | @param from Starting byte to read
|
---|
231 | @param to Last byte to read
|
---|
232 | */
|
---|
233 | void manageMsg(byte from, byte to);
|
---|
234 |
|
---|
235 | /** If _debugging, this call is assumed to be made out of interrupts
|
---|
236 | Prints incoming info and calls manageMsgNow
|
---|
237 | */
|
---|
238 | void manageReceivedData();
|
---|
239 |
|
---|
240 | /** Chronometer. Measure milliseconds from last call
|
---|
241 | @return milliseconds from las time function was called
|
---|
242 | */
|
---|
243 | unsigned long takeMilliseconds();
|
---|
244 |
|
---|
245 | /** Delay for interrupts
|
---|
246 | @param milliseconds Delay time in milliseconds
|
---|
247 | */
|
---|
248 | void delayInsideInterrupt(unsigned long milliseconds);
|
---|
249 |
|
---|
250 | };
|
---|
251 |
|
---|
252 | extern GSM3ShieldV1ModemCore theGSM3ShieldV1ModemCore;
|
---|
253 |
|
---|
254 | #endif
|
---|