1 | /*
|
---|
2 | This 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 |
|
---|
11 | This library has been developed by Telefónica Digital - PDI -
|
---|
12 | - Physical Internet Lab, as part as its collaboration with
|
---|
13 | Arduino and the Open Hardware Community.
|
---|
14 |
|
---|
15 | September-December 2012
|
---|
16 |
|
---|
17 | This library is free software; you can redistribute it and/or
|
---|
18 | modify it under the terms of the GNU Lesser General Public
|
---|
19 | License as published by the Free Software Foundation; either
|
---|
20 | version 2.1 of the License, or (at your option) any later version.
|
---|
21 |
|
---|
22 | This library is distributed in the hope that it will be useful,
|
---|
23 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
24 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
25 | Lesser General Public License for more details.
|
---|
26 |
|
---|
27 | You should have received a copy of the GNU Lesser General Public
|
---|
28 | License along with this library; if not, write to the Free Software
|
---|
29 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
30 |
|
---|
31 | The latest version of this library can always be found at
|
---|
32 | https://github.com/BlueVia/Official-Arduino
|
---|
33 | */
|
---|
34 | #include <GSM3SMSService.h>
|
---|
35 | #include <GSM3MobileNetworkProvider.h>
|
---|
36 | #include <Arduino.h>
|
---|
37 |
|
---|
38 | // While there is only a shield (ShieldV1) we will include it by default
|
---|
39 | #include <GSM3ShieldV1SMSProvider.h>
|
---|
40 | GSM3ShieldV1SMSProvider theShieldV1SMSProvider;
|
---|
41 |
|
---|
42 | #define GSM3SMSSERVICE_SYNCH 0x01 // 1: synchronous 0: asynchronous
|
---|
43 | #define __TOUT__ 10000
|
---|
44 |
|
---|
45 |
|
---|
46 | GSM3SMSService::GSM3SMSService(bool synch)
|
---|
47 | {
|
---|
48 | if(synch)
|
---|
49 | flags |= GSM3SMSSERVICE_SYNCH;
|
---|
50 | }
|
---|
51 |
|
---|
52 | // Returns 0 if last command is still executing
|
---|
53 | // 1 if success
|
---|
54 | // >1 if error
|
---|
55 | int GSM3SMSService::ready()
|
---|
56 | {
|
---|
57 | return theGSM3SMSProvider->ready();
|
---|
58 | }
|
---|
59 |
|
---|
60 | int GSM3SMSService::beginSMS(const char *number)
|
---|
61 | {
|
---|
62 | return waitForAnswerIfNeeded(theGSM3SMSProvider->beginSMS(number));
|
---|
63 | };
|
---|
64 |
|
---|
65 | int GSM3SMSService::endSMS()
|
---|
66 | {
|
---|
67 | return waitForAnswerIfNeeded(theGSM3SMSProvider->endSMS());
|
---|
68 | };
|
---|
69 |
|
---|
70 | size_t GSM3SMSService::write(uint8_t c)
|
---|
71 | {
|
---|
72 | theGSM3SMSProvider->writeSMS(c);
|
---|
73 | return 1;
|
---|
74 | }
|
---|
75 |
|
---|
76 | void GSM3SMSService::flush()
|
---|
77 | {
|
---|
78 | theGSM3SMSProvider->flushSMS();
|
---|
79 | waitForAnswerIfNeeded(1);
|
---|
80 | };
|
---|
81 |
|
---|
82 | int GSM3SMSService::available()
|
---|
83 | {
|
---|
84 | return waitForAnswerIfNeeded(theGSM3SMSProvider->availableSMS());
|
---|
85 | };
|
---|
86 |
|
---|
87 | int GSM3SMSService::remoteNumber(char* number, int nlength)
|
---|
88 | {
|
---|
89 | return theGSM3SMSProvider->remoteSMSNumber(number, nlength);
|
---|
90 |
|
---|
91 | }
|
---|
92 |
|
---|
93 | int GSM3SMSService::read()
|
---|
94 | {
|
---|
95 | return theGSM3SMSProvider->readSMS();
|
---|
96 | };
|
---|
97 | int GSM3SMSService::peek()
|
---|
98 | {
|
---|
99 | return theGSM3SMSProvider->peekSMS();
|
---|
100 | };
|
---|
101 |
|
---|
102 | int GSM3SMSService::waitForAnswerIfNeeded(int returnvalue)
|
---|
103 | {
|
---|
104 | // If synchronous
|
---|
105 | if(flags & GSM3SMSSERVICE_SYNCH )
|
---|
106 | {
|
---|
107 | unsigned long m;
|
---|
108 | m=millis();
|
---|
109 | // Wait for __TOUT__
|
---|
110 | while(((millis()-m)< __TOUT__ )&&(ready()==0))
|
---|
111 | delay(100);
|
---|
112 | // If everything was OK, return 1
|
---|
113 | // else (timeout or error codes) return 0;
|
---|
114 | if(ready()==1)
|
---|
115 | return 1;
|
---|
116 | else
|
---|
117 | return 0;
|
---|
118 | }
|
---|
119 | // If not synchronous just kick ahead the coming result
|
---|
120 | return ready();
|
---|
121 | }
|
---|
122 |
|
---|
123 |
|
---|
124 |
|
---|
125 |
|
---|
126 |
|
---|