source: arduino-1-6-7/trunk/fuentes/arduino-ide-amd64/libraries/GSM/src/GSM3ShieldV1ClientProvider.cpp @ 46

Last change on this file since 46 was 46, checked in by jrpelegrina, 4 years ago

First release to Xenial

File size: 8.8 KB
Line 
1/*
2This 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
11This library has been developed by Telefónica Digital - PDI -
12- Physical Internet Lab, as part as its collaboration with
13Arduino and the Open Hardware Community.
14
15September-December 2012
16
17This library is free software; you can redistribute it and/or
18modify it under the terms of the GNU Lesser General Public
19License as published by the Free Software Foundation; either
20version 2.1 of the License, or (at your option) any later version.
21
22This library is distributed in the hope that it will be useful,
23but WITHOUT ANY WARRANTY; without even the implied warranty of
24MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25Lesser General Public License for more details.
26
27You should have received a copy of the GNU Lesser General Public
28License along with this library; if not, write to the Free Software
29Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
30
31The latest version of this library can always be found at
32https://github.com/BlueVia/Official-Arduino
33*/
34#include <GSM3ShieldV1ClientProvider.h>
35#include <GSM3ShieldV1ModemCore.h>
36
37GSM3ShieldV1ClientProvider::GSM3ShieldV1ClientProvider()
38{
39        theGSM3MobileClientProvider=this;
40};
41
42//Response management.
43void GSM3ShieldV1ClientProvider::manageResponse(byte from, byte to)
44{
45        switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
46        {
47                case NONE:
48                        theGSM3ShieldV1ModemCore.gss.cb.deleteToTheEnd(from);
49                        break;
50                case CONNECTTCPCLIENT:
51                        connectTCPClientContinue();
52                        break;
53                case FLUSHSOCKET:
54                        flushSocketContinue();
55                        break; 
56        }
57}
58
59//Connect TCP main function.
60int GSM3ShieldV1ClientProvider::connectTCPClient(const char* server, int port, int id_socket)
61{
62        theGSM3ShieldV1ModemCore.setPort(port);         
63        idSocket = id_socket;
64       
65        theGSM3ShieldV1ModemCore.setPhoneNumber((char*)server);
66        theGSM3ShieldV1ModemCore.openCommand(this,CONNECTTCPCLIENT);
67        theGSM3ShieldV1ModemCore.registerUMProvider(this);
68        connectTCPClientContinue();
69        return theGSM3ShieldV1ModemCore.getCommandError();     
70}
71
72int GSM3ShieldV1ClientProvider::connectTCPClient(IPAddress add, int port, int id_socket)
73{
74        remoteIP=add;
75        theGSM3ShieldV1ModemCore.setPhoneNumber(0);
76        return connectTCPClient(0, port, id_socket);
77}
78
79//Connect TCP continue function.
80void GSM3ShieldV1ClientProvider::connectTCPClientContinue()
81{
82        bool resp;
83        // 0: Dot or DNS notation activation
84        // 1: Disable SW flow control
85        // 2: Waiting for IFC OK
86        // 3: Start-up TCP connection "AT+QIOPEN"
87        // 4: Wait for connection OK
88        // 5: Wait for CONNECT
89
90        switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
91        case 1:
92                theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIDNSIP="), false);
93                if ((theGSM3ShieldV1ModemCore.getPhoneNumber()!=0)&&
94                        ((*(theGSM3ShieldV1ModemCore.getPhoneNumber())<'0')||((*(theGSM3ShieldV1ModemCore.getPhoneNumber())>'9'))))
95                {
96                        theGSM3ShieldV1ModemCore.print('1');
97                        theGSM3ShieldV1ModemCore.print('\r');
98                }
99                else 
100                {
101                        theGSM3ShieldV1ModemCore.print('0');
102                        theGSM3ShieldV1ModemCore.print('\r');
103                }
104                theGSM3ShieldV1ModemCore.setCommandCounter(2);
105                break;
106        case 2:
107                if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
108            {
109                        //Response received
110                        if(resp)
111                        {                               
112                                // AT+QIOPEN
113                                theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIOPEN="),false);
114                                theGSM3ShieldV1ModemCore.print("\"TCP\",\"");
115                                if(theGSM3ShieldV1ModemCore.getPhoneNumber()!=0)
116                                {
117                                        theGSM3ShieldV1ModemCore.print(theGSM3ShieldV1ModemCore.getPhoneNumber());
118                                }
119                                else
120                                {
121                                        remoteIP.printTo(theGSM3ShieldV1ModemCore);
122                                }
123                                theGSM3ShieldV1ModemCore.print('"');
124                                theGSM3ShieldV1ModemCore.print(',');
125                                theGSM3ShieldV1ModemCore.print(theGSM3ShieldV1ModemCore.getPort());
126                                theGSM3ShieldV1ModemCore.print('\r');
127                                theGSM3ShieldV1ModemCore.setCommandCounter(3);
128                        }
129                        else theGSM3ShieldV1ModemCore.closeCommand(3);
130                }       
131                break;
132       
133        case 3:
134                if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
135            {
136                        // Response received
137                        if(resp)
138                        {
139                                // OK Received
140                                // Great. Go for the next step
141                                theGSM3ShieldV1ModemCore.setCommandCounter(4);
142                        }
143                        else theGSM3ShieldV1ModemCore.closeCommand(3);
144                }       
145                break;
146        case 4:
147                char auxLocate [12];
148                prepareAuxLocate(PSTR("CONNECT\r\n"), auxLocate);
149                if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp,auxLocate))
150            {
151                        // Response received
152                        if(resp)
153                        {
154                                // Received CONNECT OK
155                                // Great. We're done
156                                theGSM3ShieldV1ModemCore.setStatus(TRANSPARENT_CONNECTED);
157                                theGSM3ShieldV1ModemCore.theBuffer().chopUntil(auxLocate, true);
158                                theGSM3ShieldV1ModemCore.closeCommand(1);
159                        }
160                        else 
161                                theGSM3ShieldV1ModemCore.closeCommand(3);
162                }               
163                break;
164               
165        }
166}
167
168//Disconnect TCP main function.
169int GSM3ShieldV1ClientProvider::disconnectTCP(bool client1Server0, int id_socket)
170{               
171        // id Socket does not really mean anything, in this case we have
172        // only one socket running
173        theGSM3ShieldV1ModemCore.openCommand(this,DISCONNECTTCP);
174       
175        // If we are not closed, launch the command
176//[ZZ]  if(theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED)
177//      {
178                delay(1000);
179                theGSM3ShieldV1ModemCore.print("+++");
180                delay(1000);
181                theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QICLOSE"));
182                theGSM3ShieldV1ModemCore.setStatus(GPRS_READY);
183//      }
184        // Looks like it runs everytime, so we simply flush to death and go on
185        do
186        {
187                // Empty the local buffer, and tell the modem to XON
188                // If meanwhile we receive a DISCONNECT we should detect it as URC.
189                theGSM3ShieldV1ModemCore.theBuffer().flush();
190                theGSM3ShieldV1ModemCore.gss.spaceAvailable();
191                // Give some time for the buffer to refill
192                delay(100);
193                theGSM3ShieldV1ModemCore.closeCommand(1);
194        }while(theGSM3ShieldV1ModemCore.theBuffer().storedBytes()>0);
195
196        theGSM3ShieldV1ModemCore.unRegisterUMProvider(this);
197        return theGSM3ShieldV1ModemCore.getCommandError();
198}
199
200
201//Write socket first chain main function.
202void GSM3ShieldV1ClientProvider::beginWriteSocket(bool client1Server0, int id_socket)
203{
204}
205
206
207//Write socket next chain function.
208void GSM3ShieldV1ClientProvider::writeSocket(const char* buf)
209{
210        if(theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED)
211                theGSM3ShieldV1ModemCore.print(buf);
212}
213
214//Write socket character function.
215void GSM3ShieldV1ClientProvider::writeSocket(uint8_t c)
216{
217        if(theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED)
218                theGSM3ShieldV1ModemCore.print((char)c);
219}
220
221//Write socket last chain main function.
222void GSM3ShieldV1ClientProvider::endWriteSocket()
223{               
224}
225
226
227//Available socket main function.
228int GSM3ShieldV1ClientProvider::availableSocket(bool client1Server0, int id_socket)
229{
230               
231        if(!(theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED))
232                theGSM3ShieldV1ModemCore.closeCommand(4);
233               
234        if(theGSM3ShieldV1ModemCore.theBuffer().storedBytes())
235                theGSM3ShieldV1ModemCore.closeCommand(1);
236        else
237                theGSM3ShieldV1ModemCore.closeCommand(4);
238               
239        return theGSM3ShieldV1ModemCore.getCommandError();
240}
241
242int GSM3ShieldV1ClientProvider::readSocket()
243{
244        char charSocket;
245               
246        if(theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==0)
247        {
248                return 0;
249        }
250       
251        charSocket = theGSM3ShieldV1ModemCore.theBuffer().read(); 
252       
253        if(theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==100)
254                theGSM3ShieldV1ModemCore.gss.spaceAvailable();
255
256        return charSocket;
257
258}
259
260//Read socket main function.
261int GSM3ShieldV1ClientProvider::peekSocket()
262{
263        return theGSM3ShieldV1ModemCore.theBuffer().peek(0); 
264}
265
266
267//Flush SMS main function.
268void GSM3ShieldV1ClientProvider::flushSocket()
269{
270        theGSM3ShieldV1ModemCore.openCommand(this,FLUSHSOCKET);
271
272        flushSocketContinue();
273}
274
275//Send SMS continue function.
276void GSM3ShieldV1ClientProvider::flushSocketContinue()
277{
278        // If we have incomed data
279        if(theGSM3ShieldV1ModemCore.theBuffer().storedBytes()>0)
280        {
281                // Empty the local buffer, and tell the modem to XON
282                // If meanwhile we receive a DISCONNECT we should detect it as URC.
283                theGSM3ShieldV1ModemCore.theBuffer().flush();
284                theGSM3ShieldV1ModemCore.gss.spaceAvailable();
285        }
286        else 
287        {
288                //We're done           
289                theGSM3ShieldV1ModemCore.closeCommand(1);
290        }
291}
292
293// URC recognize.
294// Yes, we recognize "closes" in client mode
295bool GSM3ShieldV1ClientProvider::recognizeUnsolicitedEvent(byte oldTail)
296{
297        char auxLocate [12];
298        prepareAuxLocate(PSTR("CLOSED"), auxLocate);
299
300        if((theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED) & theGSM3ShieldV1ModemCore.theBuffer().chopUntil(auxLocate, false, false))
301        {
302                theGSM3ShieldV1ModemCore.setStatus(GPRS_READY);
303                theGSM3ShieldV1ModemCore.unRegisterUMProvider(this);
304                return true;
305        }
306               
307        return false;
308}
309
310int GSM3ShieldV1ClientProvider::getSocket(int socket)
311{
312        return 0;
313}
314
315void GSM3ShieldV1ClientProvider::releaseSocket(int socket)
316{
317
318}
319
320bool GSM3ShieldV1ClientProvider::getStatusSocketClient(uint8_t socket)
321{
322        return (theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED);
323
324};
325
326
327
Note: See TracBrowser for help on using the repository browser.