[136] | 1 | #include <GSM3ShieldV1MultiServerProvider.h>
|
---|
| 2 | #include <GSM3ShieldV1ModemCore.h>
|
---|
| 3 | #include <Arduino.h>
|
---|
| 4 |
|
---|
| 5 | #define __NCLIENTS_MAX__ 3
|
---|
| 6 |
|
---|
| 7 | const char _command_QILOCIP[] PROGMEM = "AT+QILOCIP";
|
---|
| 8 |
|
---|
| 9 | GSM3ShieldV1MultiServerProvider::GSM3ShieldV1MultiServerProvider()
|
---|
| 10 | {
|
---|
| 11 | theGSM3MobileServerProvider=this;
|
---|
| 12 | socketsAsServer=0;
|
---|
| 13 | socketsAccepted=0;
|
---|
| 14 | theGSM3ShieldV1ModemCore.registerUMProvider(this);
|
---|
| 15 | };
|
---|
| 16 |
|
---|
| 17 | //Response management.
|
---|
| 18 | void GSM3ShieldV1MultiServerProvider::manageResponse(byte from, byte to)
|
---|
| 19 | {
|
---|
| 20 | switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
|
---|
| 21 | {
|
---|
| 22 | case NONE:
|
---|
| 23 | theGSM3ShieldV1ModemCore.gss.cb.deleteToTheEnd(from);
|
---|
| 24 | break;
|
---|
| 25 | case CONNECTSERVER:
|
---|
| 26 | connectTCPServerContinue();
|
---|
| 27 | break;
|
---|
| 28 | case GETIP:
|
---|
| 29 | getIPContinue();
|
---|
| 30 | break;
|
---|
| 31 | }
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | //Connect Server main function.
|
---|
| 35 | int GSM3ShieldV1MultiServerProvider::connectTCPServer(int port)
|
---|
| 36 | {
|
---|
| 37 | // We forget about LocalIP as it has no real use, the modem does whatever it likes
|
---|
| 38 | theGSM3ShieldV1ModemCore.setPort(port);
|
---|
| 39 | theGSM3ShieldV1ModemCore.openCommand(this,CONNECTSERVER);
|
---|
| 40 | connectTCPServerContinue();
|
---|
| 41 | return theGSM3ShieldV1ModemCore.getCommandError();
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | //Connect Server continue function.
|
---|
| 45 | void GSM3ShieldV1MultiServerProvider::connectTCPServerContinue()
|
---|
| 46 | {
|
---|
| 47 |
|
---|
| 48 | bool resp;
|
---|
| 49 | // 1: Read Local IP "AT+QILOCIP"
|
---|
| 50 | // 2: Waiting for IP and Set local port "AT+QILPORT"
|
---|
| 51 | // 3: Waiting for QILPOR OK andConfigure as server "AT+QISERVER"
|
---|
| 52 | // 4: Wait for SERVER OK
|
---|
| 53 |
|
---|
| 54 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
| 55 | case 1:
|
---|
| 56 | //"AT+QILOCIP."
|
---|
| 57 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_QILOCIP);
|
---|
| 58 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
| 59 | break;
|
---|
| 60 | case 2:
|
---|
| 61 | //Not IP storing but the command is necessary.
|
---|
| 62 | //if(parseQILOCIP_rsp(local_IP, local_IP_Length, resp))
|
---|
| 63 | // This awful trick saves some RAM bytes
|
---|
| 64 | char aux[3];
|
---|
| 65 | aux[0]='\r';aux[1]='\n';aux[2]=0;
|
---|
| 66 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp, aux))
|
---|
| 67 | {
|
---|
| 68 | //Response received
|
---|
| 69 | if(resp)
|
---|
| 70 | {
|
---|
| 71 | // Great. Go for the next step
|
---|
| 72 | // AT+QILPORT
|
---|
| 73 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QILPORT=\"TCP\","),false);
|
---|
| 74 | theGSM3ShieldV1ModemCore.print( theGSM3ShieldV1ModemCore.getPort());
|
---|
| 75 | theGSM3ShieldV1ModemCore.print('\r');
|
---|
| 76 | theGSM3ShieldV1ModemCore.setCommandCounter(3);
|
---|
| 77 | }
|
---|
| 78 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
| 79 | }
|
---|
| 80 | break;
|
---|
| 81 | case 3:
|
---|
| 82 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
| 83 | {
|
---|
| 84 | // Response received
|
---|
| 85 | if(resp)
|
---|
| 86 | {
|
---|
| 87 | // OK received
|
---|
| 88 | // Great. Go for the next step
|
---|
| 89 | // AT+QISERVER
|
---|
| 90 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QISERVER=0,"),false);
|
---|
| 91 | theGSM3ShieldV1ModemCore.print(__NCLIENTS_MAX__);
|
---|
| 92 | theGSM3ShieldV1ModemCore.print('\r');
|
---|
| 93 | theGSM3ShieldV1ModemCore.setCommandCounter(4);
|
---|
| 94 | }
|
---|
| 95 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
| 96 | }
|
---|
| 97 | break;
|
---|
| 98 | case 4:
|
---|
| 99 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
| 100 | {
|
---|
| 101 | // Response received
|
---|
| 102 | // OK received, kathapoon, chessespoon
|
---|
| 103 | if (resp) theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
| 104 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
| 105 | }
|
---|
| 106 | break;
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 | //QILOCIP parse.
|
---|
| 111 | bool GSM3ShieldV1MultiServerProvider::parseQILOCIP_rsp(char* LocalIP, int LocalIPlength, bool& rsp)
|
---|
| 112 | {
|
---|
| 113 | if (!(theGSM3ShieldV1ModemCore.theBuffer().extractSubstring("\r\n","\r\n", LocalIP, LocalIPlength)))
|
---|
| 114 | rsp = false;
|
---|
| 115 | else
|
---|
| 116 | rsp = true;
|
---|
| 117 | return true;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | //Get IP main function.
|
---|
| 121 | int GSM3ShieldV1MultiServerProvider::getIP(char* LocalIP, int LocalIPlength)
|
---|
| 122 | {
|
---|
| 123 | theGSM3ShieldV1ModemCore.setPhoneNumber(LocalIP);
|
---|
| 124 | theGSM3ShieldV1ModemCore.setPort(LocalIPlength);
|
---|
| 125 | theGSM3ShieldV1ModemCore.openCommand(this,GETIP);
|
---|
| 126 | getIPContinue();
|
---|
| 127 | return theGSM3ShieldV1ModemCore.getCommandError();
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | void GSM3ShieldV1MultiServerProvider::getIPContinue()
|
---|
| 131 | {
|
---|
| 132 |
|
---|
| 133 | bool resp;
|
---|
| 134 | // 1: Read Local IP "AT+QILOCIP"
|
---|
| 135 | // 2: Waiting for IP.
|
---|
| 136 |
|
---|
| 137 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
| 138 | case 1:
|
---|
| 139 | //AT+QILOCIP
|
---|
| 140 | theGSM3ShieldV1ModemCore.genericCommand_rq(_command_QILOCIP);
|
---|
| 141 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
| 142 | break;
|
---|
| 143 | case 2:
|
---|
| 144 | if(parseQILOCIP_rsp(theGSM3ShieldV1ModemCore.getPhoneNumber(), theGSM3ShieldV1ModemCore.getPort(), resp))
|
---|
| 145 | {
|
---|
| 146 | if (resp)
|
---|
| 147 | theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
| 148 | else
|
---|
| 149 | theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
| 150 | }
|
---|
| 151 | break;
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | bool GSM3ShieldV1MultiServerProvider::getSocketAsServerModemStatus(int s)
|
---|
| 156 | {
|
---|
| 157 | if (socketsAccepted&(0x0001<<s))
|
---|
| 158 | return true;
|
---|
| 159 | else return false;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 |
|
---|
| 163 | //URC recognize.
|
---|
| 164 | bool GSM3ShieldV1MultiServerProvider::recognizeUnsolicitedEvent(byte oldTail)
|
---|
| 165 | {
|
---|
| 166 |
|
---|
| 167 | int nlength;
|
---|
| 168 | char auxLocate [15];
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 | //REMOTE SOCKET CLOSED.
|
---|
| 172 | prepareAuxLocate(PSTR("0, CLOSED\r\n"), auxLocate);
|
---|
| 173 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 174 | {
|
---|
| 175 | //To detect remote socket closed for example inside socket data.
|
---|
| 176 | releaseSocket(0);
|
---|
| 177 | socketsAccepted &= ~(0x0001);
|
---|
| 178 | //Serial.println("JCR_DB REMOTE CLOSED");
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | //REMOTE SOCKET CLOSED.
|
---|
| 182 |
|
---|
| 183 | prepareAuxLocate(PSTR("1, CLOSED\r\n"), auxLocate);
|
---|
| 184 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 185 | {
|
---|
| 186 | //To detect remote socket closed for example inside socket data.
|
---|
| 187 | releaseSocket(1);
|
---|
| 188 | socketsAccepted &= ~(0x0002);
|
---|
| 189 | }
|
---|
| 190 |
|
---|
| 191 | //REMOTE SOCKET CLOSED.
|
---|
| 192 | prepareAuxLocate(PSTR("2, CLOSED\r\n"), auxLocate);
|
---|
| 193 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 194 | {
|
---|
| 195 | //To detect remote socket closed for example inside socket data.
|
---|
| 196 | releaseSocket(2);
|
---|
| 197 | socketsAccepted &= ~(0x0004);
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | //REMOTE SOCKET CLOSED.
|
---|
| 201 | prepareAuxLocate(PSTR("3, CLOSED\r\n"), auxLocate);
|
---|
| 202 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 203 | {
|
---|
| 204 | //To detect remote socket closed for example inside socket data.
|
---|
| 205 | releaseSocket(3);
|
---|
| 206 | socketsAccepted &= ~(0x0008);
|
---|
| 207 | }
|
---|
| 208 |
|
---|
| 209 | //REMOTE SOCKET CLOSED.
|
---|
| 210 | prepareAuxLocate(PSTR("4, CLOSED\r\n"), auxLocate);
|
---|
| 211 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 212 | {
|
---|
| 213 | //To detect remote socket closed for example inside socket data.
|
---|
| 214 | releaseSocket(4);
|
---|
| 215 | socketsAccepted &= ~(0x0010);
|
---|
| 216 | }
|
---|
| 217 |
|
---|
| 218 | //REMOTE SOCKET CLOSED.
|
---|
| 219 | prepareAuxLocate(PSTR("5, CLOSED\r\n"), auxLocate);
|
---|
| 220 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 221 | {
|
---|
| 222 | //To detect remote socket closed for example inside socket data.
|
---|
| 223 | releaseSocket(5);
|
---|
| 224 | socketsAccepted &= ~(0x0020);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | //REMOTE SOCKET CLOSED.
|
---|
| 228 | prepareAuxLocate(PSTR("6, CLOSED\r\n"), auxLocate);
|
---|
| 229 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 230 | {
|
---|
| 231 | //To detect remote socket closed for example inside socket data.
|
---|
| 232 | releaseSocket(6);
|
---|
| 233 | socketsAccepted &= ~(0x0040);
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | //REMOTE SOCKET CLOSED.
|
---|
| 237 | prepareAuxLocate(PSTR("7, CLOSED\r\n"), auxLocate);
|
---|
| 238 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 239 | {
|
---|
| 240 | //To detect remote socket closed for example inside socket data.
|
---|
| 241 | releaseSocket(7);
|
---|
| 242 | socketsAccepted &= ~(0x0080);
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | //REMOTE SOCKET ACCEPTED.
|
---|
| 246 | prepareAuxLocate(PSTR("0, REMOTE IP"), auxLocate);
|
---|
| 247 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 248 | {
|
---|
| 249 | //To detect remote socket closed for example inside socket data.
|
---|
| 250 | theGSM3ShieldV1ModemCore.gss.cb.flush();
|
---|
| 251 | socketsAccepted |= (0x0001);
|
---|
| 252 | return true;
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | //REMOTE SOCKET ACCEPTED.
|
---|
| 256 | prepareAuxLocate(PSTR("1, REMOTE IP"), auxLocate);
|
---|
| 257 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 258 | {
|
---|
| 259 | //To detect remote socket closed for example inside socket data.
|
---|
| 260 | theGSM3ShieldV1ModemCore.gss.cb.flush();
|
---|
| 261 | socketsAccepted |= (0x0002);
|
---|
| 262 | return true;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
| 265 | //REMOTE SOCKET ACCEPTED.
|
---|
| 266 | prepareAuxLocate(PSTR("2, REMOTE IP"), auxLocate);
|
---|
| 267 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 268 | {
|
---|
| 269 | //To detect remote socket closed for example inside socket data.
|
---|
| 270 | theGSM3ShieldV1ModemCore.gss.cb.flush();
|
---|
| 271 | socketsAccepted |= (0x0004);
|
---|
| 272 | return true;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | //REMOTE SOCKET ACCEPTED.
|
---|
| 276 | prepareAuxLocate(PSTR("3, REMOTE IP"), auxLocate);
|
---|
| 277 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 278 | {
|
---|
| 279 | //To detect remote socket closed for example inside socket data.
|
---|
| 280 | theGSM3ShieldV1ModemCore.gss.cb.flush();
|
---|
| 281 | socketsAccepted |= (0x0008);
|
---|
| 282 | return true;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | //REMOTE SOCKET ACCEPTED.
|
---|
| 286 | prepareAuxLocate(PSTR("4, REMOTE IP"), auxLocate);
|
---|
| 287 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 288 | {
|
---|
| 289 | //To detect remote socket closed for example inside socket data.
|
---|
| 290 | theGSM3ShieldV1ModemCore.gss.cb.flush();
|
---|
| 291 | socketsAccepted |= (0x0010);
|
---|
| 292 | return true;
|
---|
| 293 | }
|
---|
| 294 |
|
---|
| 295 | //REMOTE SOCKET ACCEPTED.
|
---|
| 296 | prepareAuxLocate(PSTR("5, REMOTE IP"), auxLocate);
|
---|
| 297 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 298 | {
|
---|
| 299 | //To detect remote socket closed for example inside socket data.
|
---|
| 300 | theGSM3ShieldV1ModemCore.gss.cb.flush();
|
---|
| 301 | socketsAccepted |= (0x0020);
|
---|
| 302 | return true;
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | //REMOTE SOCKET ACCEPTED.
|
---|
| 306 | prepareAuxLocate(PSTR("6, REMOTE IP"), auxLocate);
|
---|
| 307 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 308 | {
|
---|
| 309 | //To detect remote socket closed for example inside socket data.
|
---|
| 310 | theGSM3ShieldV1ModemCore.gss.cb.flush();
|
---|
| 311 | socketsAccepted |= (0x0040);
|
---|
| 312 | return true;
|
---|
| 313 | }
|
---|
| 314 |
|
---|
| 315 | //REMOTE SOCKET ACCEPTED.
|
---|
| 316 | prepareAuxLocate(PSTR("7, REMOTE IP"), auxLocate);
|
---|
| 317 | if(theGSM3ShieldV1ModemCore.gss.cb.locate(auxLocate))
|
---|
| 318 | {
|
---|
| 319 | //To detect remote socket closed for example inside socket data.
|
---|
| 320 | theGSM3ShieldV1ModemCore.gss.cb.flush();
|
---|
| 321 | socketsAccepted |= (0x0080);
|
---|
| 322 | return true;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 |
|
---|
| 326 | return false;
|
---|
| 327 | }
|
---|
| 328 |
|
---|
| 329 | bool GSM3ShieldV1MultiServerProvider::getStatusSocketAsServer(uint8_t socket)
|
---|
| 330 | {
|
---|
| 331 | if(socketsAsServer&(0x0001<<socket))
|
---|
| 332 | return 1;
|
---|
| 333 | else
|
---|
| 334 | return 0;
|
---|
| 335 | };
|
---|
| 336 |
|
---|
| 337 | void GSM3ShieldV1MultiServerProvider::releaseSocket(int socket)
|
---|
| 338 | {
|
---|
| 339 | if (socketsAsServer&((0x0001)<<socket))
|
---|
| 340 | socketsAsServer^=((0x0001)<<socket);
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | int GSM3ShieldV1MultiServerProvider::getNewOccupiedSocketAsServer()
|
---|
| 344 | {
|
---|
| 345 | int i;
|
---|
| 346 | ready();
|
---|
| 347 | for(i=minSocketAsServer(); i<=maxSocketAsServer(); i++)
|
---|
| 348 | {
|
---|
| 349 | if ((!(socketsAsServer&(0x0001<<i))) && getSocketAsServerModemStatus(i))
|
---|
| 350 | {
|
---|
| 351 | socketsAsServer|=((0x0001)<<i);
|
---|
| 352 | return i;
|
---|
| 353 | }
|
---|
| 354 | }
|
---|
| 355 | // No new occupied
|
---|
| 356 | return -1;
|
---|
| 357 | }
|
---|