[136] | 1 | #include <GSM3ShieldV1SMSProvider.h>
|
---|
| 2 | #include <Arduino.h>
|
---|
| 3 |
|
---|
| 4 | GSM3ShieldV1SMSProvider::GSM3ShieldV1SMSProvider()
|
---|
| 5 | {
|
---|
| 6 | theGSM3SMSProvider=this;
|
---|
| 7 | };
|
---|
| 8 |
|
---|
| 9 | //Send SMS begin function.
|
---|
| 10 | int GSM3ShieldV1SMSProvider::beginSMS(const char* to)
|
---|
| 11 | {
|
---|
| 12 | if((theGSM3ShieldV1ModemCore.getStatus() != GSM_READY)&&(theGSM3ShieldV1ModemCore.getStatus() != GPRS_READY))
|
---|
| 13 | return 2;
|
---|
| 14 |
|
---|
| 15 | theGSM3ShieldV1ModemCore.setPhoneNumber((char*)to);
|
---|
| 16 | theGSM3ShieldV1ModemCore.openCommand(this,BEGINSMS);
|
---|
| 17 | beginSMSContinue();
|
---|
| 18 | return theGSM3ShieldV1ModemCore.getCommandError();
|
---|
| 19 | }
|
---|
| 20 |
|
---|
| 21 | //Send SMS continue function.
|
---|
| 22 | void GSM3ShieldV1SMSProvider::beginSMSContinue()
|
---|
| 23 | {
|
---|
| 24 | bool resp;
|
---|
| 25 | // 1: Send AT
|
---|
| 26 | // 2: wait for > and write text
|
---|
| 27 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
| 28 | case 1:
|
---|
| 29 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
| 30 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CMGS=\""), false);
|
---|
| 31 | theGSM3ShieldV1ModemCore.print(theGSM3ShieldV1ModemCore.getPhoneNumber());
|
---|
| 32 | theGSM3ShieldV1ModemCore.print("\"\r");
|
---|
| 33 | break;
|
---|
| 34 | case 2:
|
---|
| 35 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp, ">"))
|
---|
| 36 | {
|
---|
| 37 | if (resp) theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
| 38 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
| 39 | }
|
---|
| 40 | break;
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | //Send SMS write function.
|
---|
| 45 | void GSM3ShieldV1SMSProvider::writeSMS(char c)
|
---|
| 46 | {
|
---|
| 47 | theGSM3ShieldV1ModemCore.write(c);
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | //Send SMS begin function.
|
---|
| 51 | int GSM3ShieldV1SMSProvider::endSMS()
|
---|
| 52 | {
|
---|
| 53 | theGSM3ShieldV1ModemCore.openCommand(this,ENDSMS);
|
---|
| 54 | endSMSContinue();
|
---|
| 55 | while(ready()==0) delay(100);
|
---|
| 56 | return theGSM3ShieldV1ModemCore.getCommandError();
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | //Send SMS continue function.
|
---|
| 60 | void GSM3ShieldV1SMSProvider::endSMSContinue()
|
---|
| 61 | {
|
---|
| 62 | bool resp;
|
---|
| 63 | // 1: Send #26
|
---|
| 64 | // 2: wait for OK
|
---|
| 65 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
| 66 | case 1:
|
---|
| 67 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
| 68 | theGSM3ShieldV1ModemCore.write(26);
|
---|
| 69 | theGSM3ShieldV1ModemCore.print("\r");
|
---|
| 70 | break;
|
---|
| 71 | case 2:
|
---|
| 72 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
| 73 | {
|
---|
| 74 | if (resp)
|
---|
| 75 | theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
| 76 | else
|
---|
| 77 | theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
| 78 | }
|
---|
| 79 | break;
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | //Available SMS main function.
|
---|
| 84 | int GSM3ShieldV1SMSProvider::availableSMS()
|
---|
| 85 | {
|
---|
| 86 | flagReadingSMS = 0;
|
---|
| 87 | theGSM3ShieldV1ModemCore.openCommand(this,AVAILABLESMS);
|
---|
| 88 | availableSMSContinue();
|
---|
| 89 | return theGSM3ShieldV1ModemCore.getCommandError();
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | //Available SMS continue function.
|
---|
| 93 | void GSM3ShieldV1SMSProvider::availableSMSContinue()
|
---|
| 94 | {
|
---|
| 95 | // 1: AT+CMGL="REC UNREAD",1
|
---|
| 96 | // 2: Receive +CMGL: _id_ ... READ","_numero_" ... \n_mensaje_\nOK
|
---|
| 97 | // 3: Send AT+CMGD= _id_
|
---|
| 98 | // 4: Receive OK
|
---|
| 99 | // 5: Remaining SMS text in case full buffer.
|
---|
| 100 | // This implementation really does not care much if the modem aswers trash to CMGL
|
---|
| 101 | bool resp;
|
---|
| 102 | //int msglength_aux;
|
---|
| 103 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
| 104 | case 1:
|
---|
| 105 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CMGL=\"REC UNREAD\",1"));
|
---|
| 106 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
| 107 | break;
|
---|
| 108 | case 2:
|
---|
| 109 | if(parseCMGL_available(resp))
|
---|
| 110 | {
|
---|
| 111 | if (!resp) theGSM3ShieldV1ModemCore.closeCommand(4);
|
---|
| 112 | else theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
| 113 | }
|
---|
| 114 | break;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | //SMS available parse.
|
---|
| 120 | bool GSM3ShieldV1SMSProvider::parseCMGL_available(bool& rsp)
|
---|
| 121 | {
|
---|
| 122 | fullBufferSMS = (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()<=4);
|
---|
| 123 | if (!(theGSM3ShieldV1ModemCore.theBuffer().chopUntil("+CMGL:", true)))
|
---|
| 124 | rsp = false;
|
---|
| 125 | else
|
---|
| 126 | rsp = true;
|
---|
| 127 | idSMS=theGSM3ShieldV1ModemCore.theBuffer().readInt();
|
---|
| 128 |
|
---|
| 129 | //If there are 2 SMS in buffer, response is ...CRLFCRLF+CMGL
|
---|
| 130 | twoSMSinBuffer = theGSM3ShieldV1ModemCore.theBuffer().locate("\r\n\r\n+");
|
---|
| 131 |
|
---|
| 132 | checkSecondBuffer = 0;
|
---|
| 133 |
|
---|
| 134 | return true;
|
---|
| 135 | }
|
---|
| 136 |
|
---|
| 137 | //remoteNumber SMS function.
|
---|
| 138 | int GSM3ShieldV1SMSProvider::remoteSMSNumber(char* number, int nlength)
|
---|
| 139 | {
|
---|
| 140 | theGSM3ShieldV1ModemCore.theBuffer().extractSubstring("READ\",\"", "\"", number, nlength);
|
---|
| 141 |
|
---|
| 142 | return 1;
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | //remoteNumber SMS function.
|
---|
| 146 | int GSM3ShieldV1SMSProvider::readSMS()
|
---|
| 147 | {
|
---|
| 148 | char charSMS;
|
---|
| 149 | //First char.
|
---|
| 150 | if (!flagReadingSMS)
|
---|
| 151 | {
|
---|
| 152 | flagReadingSMS = 1;
|
---|
| 153 | theGSM3ShieldV1ModemCore.theBuffer().chopUntil("\n", true);
|
---|
| 154 | }
|
---|
| 155 | charSMS = theGSM3ShieldV1ModemCore.theBuffer().read();
|
---|
| 156 |
|
---|
| 157 | //Second Buffer.
|
---|
| 158 | if (checkSecondBuffer)
|
---|
| 159 | {
|
---|
| 160 | checkSecondBuffer = 0;
|
---|
| 161 | twoSMSinBuffer = theGSM3ShieldV1ModemCore.theBuffer().locate("\r\n\r\n+");
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 | //Case the last char in buffer.
|
---|
| 165 | if ((!twoSMSinBuffer)&&fullBufferSMS&&(theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==127))
|
---|
| 166 | {
|
---|
| 167 | theGSM3ShieldV1ModemCore.theBuffer().flush();
|
---|
| 168 | fullBufferSMS = 0;
|
---|
| 169 | checkSecondBuffer = 1;
|
---|
| 170 | theGSM3ShieldV1ModemCore.openCommand(this,XON);
|
---|
| 171 | theGSM3ShieldV1ModemCore.gss.spaceAvailable();
|
---|
| 172 | delay(10);
|
---|
| 173 |
|
---|
| 174 | return charSMS;
|
---|
| 175 | }
|
---|
| 176 | //Case two SMS in buffer
|
---|
| 177 | else if (twoSMSinBuffer)
|
---|
| 178 | {
|
---|
| 179 | if (theGSM3ShieldV1ModemCore.theBuffer().locate("\r\n\r\n+"))
|
---|
| 180 | {
|
---|
| 181 | return charSMS;
|
---|
| 182 | }
|
---|
| 183 | else
|
---|
| 184 | {
|
---|
| 185 | theGSM3ShieldV1ModemCore.theBuffer().flush();
|
---|
| 186 | theGSM3ShieldV1ModemCore.openCommand(this,XON);
|
---|
| 187 | theGSM3ShieldV1ModemCore.gss.spaceAvailable();
|
---|
| 188 | delay(10);
|
---|
| 189 | return 0;
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 | //Case 1 SMS and buffer not full
|
---|
| 193 | else if (!fullBufferSMS)
|
---|
| 194 | {
|
---|
| 195 | if (theGSM3ShieldV1ModemCore.theBuffer().locate("\r\n\r\nOK"))
|
---|
| 196 | {
|
---|
| 197 | return charSMS;
|
---|
| 198 | }
|
---|
| 199 | else
|
---|
| 200 | {
|
---|
| 201 | theGSM3ShieldV1ModemCore.theBuffer().flush();
|
---|
| 202 | theGSM3ShieldV1ModemCore.openCommand(this,XON);
|
---|
| 203 | theGSM3ShieldV1ModemCore.gss.spaceAvailable();
|
---|
| 204 | delay(10);
|
---|
| 205 | return 0;
|
---|
| 206 | }
|
---|
| 207 | }
|
---|
| 208 | //Case to read all the chars in buffer to the end.
|
---|
| 209 | else
|
---|
| 210 | {
|
---|
| 211 | return charSMS;
|
---|
| 212 | }
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | //Read socket main function.
|
---|
| 216 | int GSM3ShieldV1SMSProvider::peekSMS()
|
---|
| 217 | {
|
---|
| 218 | if (!flagReadingSMS)
|
---|
| 219 | {
|
---|
| 220 | flagReadingSMS = 1;
|
---|
| 221 | theGSM3ShieldV1ModemCore.theBuffer().chopUntil("\n", true);
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | return theGSM3ShieldV1ModemCore.theBuffer().peek(0);
|
---|
| 225 | }
|
---|
| 226 |
|
---|
| 227 | //Flush SMS main function.
|
---|
| 228 | void GSM3ShieldV1SMSProvider::flushSMS()
|
---|
| 229 | {
|
---|
| 230 |
|
---|
| 231 | //With this, sms data can fill up to 2x128+5x128 bytes.
|
---|
| 232 | for (int aux = 0;aux<5;aux++)
|
---|
| 233 | {
|
---|
| 234 | theGSM3ShieldV1ModemCore.theBuffer().flush();
|
---|
| 235 | theGSM3ShieldV1ModemCore.gss.spaceAvailable();
|
---|
| 236 | delay(10);
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | theGSM3ShieldV1ModemCore.openCommand(this,FLUSHSMS);
|
---|
| 240 | flushSMSContinue();
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | //Send SMS continue function.
|
---|
| 244 | void GSM3ShieldV1SMSProvider::flushSMSContinue()
|
---|
| 245 | {
|
---|
| 246 | bool resp;
|
---|
| 247 | // 1: Deleting SMS
|
---|
| 248 | // 2: wait for OK
|
---|
| 249 | switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
|
---|
| 250 | case 1:
|
---|
| 251 | theGSM3ShieldV1ModemCore.setCommandCounter(2);
|
---|
| 252 | theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+CMGD="), false);
|
---|
| 253 | theGSM3ShieldV1ModemCore.print(idSMS);
|
---|
| 254 | theGSM3ShieldV1ModemCore.print("\r");
|
---|
| 255 | break;
|
---|
| 256 | case 2:
|
---|
| 257 | if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
|
---|
| 258 | {
|
---|
| 259 | if (resp) theGSM3ShieldV1ModemCore.closeCommand(1);
|
---|
| 260 | else theGSM3ShieldV1ModemCore.closeCommand(3);
|
---|
| 261 | }
|
---|
| 262 | break;
|
---|
| 263 | }
|
---|
| 264 | }
|
---|
| 265 |
|
---|
| 266 | void GSM3ShieldV1SMSProvider::manageResponse(byte from, byte to)
|
---|
| 267 | {
|
---|
| 268 | switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
|
---|
| 269 | {
|
---|
| 270 | /* case XON:
|
---|
| 271 | if (flagReadingSocket)
|
---|
| 272 | {
|
---|
| 273 | // flagReadingSocket = 0;
|
---|
| 274 | fullBufferSocket = (theGSM3ShieldV1ModemCore.theBuffer().availableBytes()<3);
|
---|
| 275 | }
|
---|
| 276 | else theGSM3ShieldV1ModemCore.openCommand(this,NONE);
|
---|
| 277 | break;
|
---|
| 278 | */ case NONE:
|
---|
| 279 | theGSM3ShieldV1ModemCore.gss.cb.deleteToTheEnd(from);
|
---|
| 280 | break;
|
---|
| 281 | case BEGINSMS:
|
---|
| 282 | beginSMSContinue();
|
---|
| 283 | break;
|
---|
| 284 | case ENDSMS:
|
---|
| 285 | endSMSContinue();
|
---|
| 286 | break;
|
---|
| 287 | case AVAILABLESMS:
|
---|
| 288 | availableSMSContinue();
|
---|
| 289 | break;
|
---|
| 290 | case FLUSHSMS:
|
---|
| 291 | flushSMSContinue();
|
---|
| 292 | break;
|
---|
| 293 | }
|
---|
| 294 | }
|
---|