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 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
|
#include <GSM3ShieldV1MultiClientProvider.h>
#include <GSM3ShieldV1ModemCore.h>
const char _command_MultiQISRVC[] PROGMEM = "AT+QISRVC=";
#define __TOUTFLUSH__ 10000
GSM3ShieldV1MultiClientProvider::GSM3ShieldV1MultiClientProvider()
{
theGSM3MobileClientProvider=this;
theGSM3ShieldV1ModemCore.registerUMProvider(this);
};
//Response management.
void GSM3ShieldV1MultiClientProvider::manageResponse(byte from, byte to)
{
switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
{
case XON:
if (flagReadingSocket)
{
// flagReadingSocket = 0;
fullBufferSocket = (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()<3);
}
else theGSM3ShieldV1ModemCore.setOngoingCommand(NONE);
break;
case NONE:
theGSM3ShieldV1ModemCore.gss.cb.deleteToTheEnd(from);
break;
case CONNECTTCPCLIENT:
connectTCPClientContinue();
break;
case DISCONNECTTCP:
disconnectTCPContinue();
break;
case BEGINWRITESOCKET:
beginWriteSocketContinue();
break;
case ENDWRITESOCKET:
endWriteSocketContinue();
break;
case AVAILABLESOCKET:
availableSocketContinue();
break;
case FLUSHSOCKET:
fullBufferSocket = (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()<3);
flushSocketContinue();
break;
}
}
//Connect TCP main function.
int GSM3ShieldV1MultiClientProvider::connectTCPClient(const char* server, int port, int id_socket)
{
theGSM3ShieldV1ModemCore.setPort(port);
idSocket = id_socket;
theGSM3ShieldV1ModemCore.setPhoneNumber((char*)server);
theGSM3ShieldV1ModemCore.openCommand(this,CONNECTTCPCLIENT);
connectTCPClientContinue();
return theGSM3ShieldV1ModemCore.getCommandError();
}
int GSM3ShieldV1MultiClientProvider::connectTCPClient(IPAddress add, int port, int id_socket)
{
remoteIP=add;
theGSM3ShieldV1ModemCore.setPhoneNumber(0);
return connectTCPClient(0, port, id_socket);
}
//Connect TCP continue function.
void GSM3ShieldV1MultiClientProvider::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(idSocket);
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 OK"), auxLocate);
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp,auxLocate))
{
// Response received
if(resp)
{
// Received CONNECT OK
// Great. We're done
theGSM3ShieldV1ModemCore.closeCommand(1);
}
else
theGSM3ShieldV1ModemCore.closeCommand(3);
}
break;
}
}
//Disconnect TCP main function.
int GSM3ShieldV1MultiClientProvider::disconnectTCP(bool client1Server0, int id_socket)
{
idSocket = id_socket;
// First of all, we will flush the socket synchronously
unsigned long m;
m=millis();
flushSocket();
while(((millis()-m)< __TOUTFLUSH__ )&&(ready()==0))
delay(10);
// Could not flush the communications... strange
if(ready()==0)
{
theGSM3ShieldV1ModemCore.setCommandError(2);
return theGSM3ShieldV1ModemCore.getCommandError();
}
// Set up the command
client1_server0 = client1Server0;
flagReadingSocket=0;
theGSM3ShieldV1ModemCore.openCommand(this,DISCONNECTTCP);
disconnectTCPContinue();
return theGSM3ShieldV1ModemCore.getCommandError();
}
//Disconnect TCP continue function
void GSM3ShieldV1MultiClientProvider::disconnectTCPContinue()
{
bool resp;
// 1: Send AT+QISRVC
// 2: "AT+QICLOSE"
// 3: Wait for OK
switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
case 1:
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_MultiQISRVC, false);
if (client1_server0) theGSM3ShieldV1ModemCore.print('1');
else theGSM3ShieldV1ModemCore.print('2');
theGSM3ShieldV1ModemCore.print('\r');
theGSM3ShieldV1ModemCore.setCommandCounter(2);
break;
case 2:
// Parse response to QISRVC
theGSM3ShieldV1ModemCore.genericParse_rsp(resp);
if(resp)
{
// Send QICLOSE command
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QICLOSE="),false);
theGSM3ShieldV1ModemCore.print(idSocket);
theGSM3ShieldV1ModemCore.print('\r');
theGSM3ShieldV1ModemCore.setCommandCounter(3);
}
else
theGSM3ShieldV1ModemCore.closeCommand(3);
break;
case 3:
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
theGSM3ShieldV1ModemCore.setCommandCounter(0);
if (resp)
theGSM3ShieldV1ModemCore.closeCommand(1);
else
theGSM3ShieldV1ModemCore.closeCommand(3);
}
break;
}
}
//Write socket first chain main function.
void GSM3ShieldV1MultiClientProvider::beginWriteSocket(bool client1Server0, int id_socket)
{
idSocket = id_socket;
client1_server0 = client1Server0;
theGSM3ShieldV1ModemCore.openCommand(this,BEGINWRITESOCKET);
beginWriteSocketContinue();
}
//Write socket first chain continue function.
void GSM3ShieldV1MultiClientProvider::beginWriteSocketContinue()
{
bool resp;
// 1: Send AT+QISRVC
// 2: Send AT+QISEND
// 3: wait for > and Write text
switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
case 1:
// AT+QISRVC
theGSM3ShieldV1ModemCore.genericCommand_rq(_command_MultiQISRVC, false);
if (client1_server0)
theGSM3ShieldV1ModemCore.print('1');
else
theGSM3ShieldV1ModemCore.print('2');
theGSM3ShieldV1ModemCore.print('\r');
theGSM3ShieldV1ModemCore.setCommandCounter(2);
break;
case 2:
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
// Response received
if(resp)
{
// AT+QISEND
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QISEND="), false);
theGSM3ShieldV1ModemCore.print(idSocket);
theGSM3ShieldV1ModemCore.print('\r');
theGSM3ShieldV1ModemCore.setCommandCounter(3);
}
else
{
theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
break;
case 3:
char aux[2];
aux[0]='>';
aux[1]=0;
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp, aux))
{
if(resp)
{
// Received ">"
theGSM3ShieldV1ModemCore.closeCommand(1);
}
else
{
theGSM3ShieldV1ModemCore.closeCommand(3);
}
}
break;
}
}
//Write socket next chain function.
void GSM3ShieldV1MultiClientProvider::writeSocket(const char* buf)
{
theGSM3ShieldV1ModemCore.print(buf);
}
//Write socket character function.
void GSM3ShieldV1MultiClientProvider::writeSocket(char c)
{
theGSM3ShieldV1ModemCore.print(c);
}
//Write socket last chain main function.
void GSM3ShieldV1MultiClientProvider::endWriteSocket()
{
theGSM3ShieldV1ModemCore.openCommand(this,ENDWRITESOCKET);
endWriteSocketContinue();
}
//Write socket last chain continue function.
void GSM3ShieldV1MultiClientProvider::endWriteSocketContinue()
{
bool resp;
// 1: Write text (ctrl-Z)
// 2: Wait for OK
switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
case 1:
theGSM3ShieldV1ModemCore.write(26); // Ctrl-Z
theGSM3ShieldV1ModemCore.setCommandCounter(2);
break;
case 2:
if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
{
// OK received
if (resp) theGSM3ShieldV1ModemCore.closeCommand(1);
else theGSM3ShieldV1ModemCore.closeCommand(3);
}
break;
}
}
//Available socket main function.
int GSM3ShieldV1MultiClientProvider::availableSocket(bool client1Server0, int id_socket)
{
if(flagReadingSocket==1)
{
theGSM3ShieldV1ModemCore.setCommandError(1);
return 1;
}
client1_server0 = client1Server0;
idSocket = id_socket;
theGSM3ShieldV1ModemCore.openCommand(this,AVAILABLESOCKET);
availableSocketContinue();
return theGSM3ShieldV1ModemCore.getCommandError();
}
//Available socket continue function.
void GSM3ShieldV1MultiClientProvider::availableSocketContinue()
{
bool resp;
// 1: AT+QIRD
// 2: Wait for OK and Next necessary AT+QIRD
switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
case 1:
theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIRD=0,"),false);
if (client1_server0)
theGSM3ShieldV1ModemCore.print('1');
else
theGSM3ShieldV1ModemCore.print('2');
theGSM3ShieldV1ModemCore.print(',');
theGSM3ShieldV1ModemCore.print(idSocket);
theGSM3ShieldV1ModemCore.print(",1500");
// theGSM3ShieldV1ModemCore.print(",120");
theGSM3ShieldV1ModemCore.print('\r');
theGSM3ShieldV1ModemCore.setCommandCounter(2);
break;
case 2:
if(parseQIRD_head(resp))
{
if (!resp)
{
theGSM3ShieldV1ModemCore.closeCommand(4);
}
else
{
flagReadingSocket=1;
theGSM3ShieldV1ModemCore.closeCommand(1);
}
}
else
{
theGSM3ShieldV1ModemCore.closeCommand(3);
}
break;
}
}
//Read Socket Parse head.
bool GSM3ShieldV1MultiClientProvider::parseQIRD_head(bool& rsp)
{
char _qird [8];
prepareAuxLocate(PSTR("+QIRD:"), _qird);
fullBufferSocket = (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()<3);
if(theGSM3ShieldV1ModemCore.theBuffer().locate(_qird))
{
theGSM3ShieldV1ModemCore.theBuffer().chopUntil(_qird, true);
// Saving more memory, reuse _qird
_qird[0]='\n';
_qird[1]=0;
theGSM3ShieldV1ModemCore.theBuffer().chopUntil(_qird, true);
rsp = true;
return true;
}
else if(theGSM3ShieldV1ModemCore.theBuffer().locate("OK"))
{
rsp = false;
return true;
}
else
{
rsp = false;
return false;
}
}
/*
//Read socket main function.
int GSM3ShieldV1MultiClientProvider::readSocket()
{
char charSocket;
charSocket = theGSM3ShieldV1ModemCore.theBuffer().read();
//Case buffer not full
if (!fullBufferSocket)
{
//The last part of the buffer after data is CRLFOKCRLF
if (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==125)
{
//Start again availableSocket function.
flagReadingSocket=0;
theGSM3ShieldV1ModemCore.openCommand(this,AVAILABLESOCKET);
availableSocketContinue();
}
}
else if (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==127)
{
// The buffer is full, no more action is possible until we have read()
theGSM3ShieldV1ModemCore.theBuffer().flush();
flagReadingSocket = 1;
theGSM3ShieldV1ModemCore.openCommand(this,XON);
theGSM3ShieldV1ModemCore.gss.spaceAvailable();
//A small delay to assure data received after xon.
delay(10);
}
//To distinguish the case no more available data in socket.
if (ready()==1)
return charSocket;
else
return 0;
}
*/
int GSM3ShieldV1MultiClientProvider::readSocket()
{
char charSocket;
if(theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==0)
{
Serial.println();Serial.println("*");
return 0;
}
charSocket = theGSM3ShieldV1ModemCore.theBuffer().read();
//Case buffer not full
if (!fullBufferSocket)
{
//The last part of the buffer after data is CRLFOKCRLF
if (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==125)
{
//Start again availableSocket function.
flagReadingSocket=0;
theGSM3ShieldV1ModemCore.openCommand(this,AVAILABLESOCKET);
availableSocketContinue();
}
}
else if (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()>=100)
{
// The buffer was full, we have to let the data flow again
// theGSM3ShieldV1ModemCore.theBuffer().flush();
flagReadingSocket = 1;
theGSM3ShieldV1ModemCore.openCommand(this,XON);
theGSM3ShieldV1ModemCore.gss.spaceAvailable();
//A small delay to assure data received after xon.
delay(100);
if(theGSM3ShieldV1ModemCore.theBuffer().availableBytes() >=6)
fullBufferSocket=false;
}
return charSocket;
}
//Read socket main function.
int GSM3ShieldV1MultiClientProvider::peekSocket()
{
return theGSM3ShieldV1ModemCore.theBuffer().peek(0);
}
//Flush SMS main function.
void GSM3ShieldV1MultiClientProvider::flushSocket()
{
flagReadingSocket=0;
theGSM3ShieldV1ModemCore.openCommand(this,FLUSHSOCKET);
flushSocketContinue();
}
//Send SMS continue function.
void GSM3ShieldV1MultiClientProvider::flushSocketContinue()
{
bool resp;
// 1: Deleting SMS
// 2: wait for OK
switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
case 1:
//DEBUG
//Serial.println("Flushing Socket.");
theGSM3ShieldV1ModemCore.theBuffer().flush();
if (fullBufferSocket)
{
//Serial.println("Buffer flushed.");
theGSM3ShieldV1ModemCore.gss.spaceAvailable();
}
else
{
//Serial.println("Socket flushed completely.");
theGSM3ShieldV1ModemCore.closeCommand(1);
}
break;
}
}
//URC recognize.
// Momentarily, we will not recognize "closes" in client mode
bool GSM3ShieldV1MultiClientProvider::recognizeUnsolicitedEvent(byte oldTail)
{
return false;
}
int GSM3ShieldV1MultiClientProvider::getSocket(int socket)
{
if(socket==-1)
{
int i;
for(i=minSocket(); i<=maxSocket(); i++)
{
if (!(sockets&(0x0001<<i)))
{
sockets|=((0x0001)<<i);
return i;
}
}
}
else
{
if (!(sockets&(0x0001<<socket)))
{
sockets|=((0x0001)<<socket);
return socket;
}
}
return -1;
}
void GSM3ShieldV1MultiClientProvider::releaseSocket(int socket)
{
if (sockets&((0x0001)<<socket))
sockets^=((0x0001)<<socket);
}
bool GSM3ShieldV1MultiClientProvider::getStatusSocketClient(uint8_t socket)
{
if(socket>8)
return 0;
if(sockets&(0x0001<<socket))
return 1;
else
return 0;
};
|