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