source: rtos_arduino/trunk/arduino_lib/libraries/GSM/src/GSM3ShieldV1ClientProvider.cpp@ 136

Last change on this file since 136 was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 7.6 KB
Line 
1#include <GSM3ShieldV1ClientProvider.h>
2#include <GSM3ShieldV1ModemCore.h>
3
4GSM3ShieldV1ClientProvider::GSM3ShieldV1ClientProvider()
5{
6 theGSM3MobileClientProvider=this;
7};
8
9//Response management.
10void GSM3ShieldV1ClientProvider::manageResponse(byte from, byte to)
11{
12 switch(theGSM3ShieldV1ModemCore.getOngoingCommand())
13 {
14 case NONE:
15 theGSM3ShieldV1ModemCore.gss.cb.deleteToTheEnd(from);
16 break;
17 case CONNECTTCPCLIENT:
18 connectTCPClientContinue();
19 break;
20 case FLUSHSOCKET:
21 flushSocketContinue();
22 break;
23 }
24}
25
26//Connect TCP main function.
27int GSM3ShieldV1ClientProvider::connectTCPClient(const char* server, int port, int id_socket)
28{
29 theGSM3ShieldV1ModemCore.setPort(port);
30 idSocket = id_socket;
31
32 theGSM3ShieldV1ModemCore.setPhoneNumber((char*)server);
33 theGSM3ShieldV1ModemCore.openCommand(this,CONNECTTCPCLIENT);
34 theGSM3ShieldV1ModemCore.registerUMProvider(this);
35 connectTCPClientContinue();
36 return theGSM3ShieldV1ModemCore.getCommandError();
37}
38
39int GSM3ShieldV1ClientProvider::connectTCPClient(IPAddress add, int port, int id_socket)
40{
41 remoteIP=add;
42 theGSM3ShieldV1ModemCore.setPhoneNumber(0);
43 return connectTCPClient(0, port, id_socket);
44}
45
46//Connect TCP continue function.
47void GSM3ShieldV1ClientProvider::connectTCPClientContinue()
48{
49 bool resp;
50 // 0: Dot or DNS notation activation
51 // 1: Disable SW flow control
52 // 2: Waiting for IFC OK
53 // 3: Start-up TCP connection "AT+QIOPEN"
54 // 4: Wait for connection OK
55 // 5: Wait for CONNECT
56
57 switch (theGSM3ShieldV1ModemCore.getCommandCounter()) {
58 case 1:
59 theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIDNSIP="), false);
60 if ((theGSM3ShieldV1ModemCore.getPhoneNumber()!=0)&&
61 ((*(theGSM3ShieldV1ModemCore.getPhoneNumber())<'0')||((*(theGSM3ShieldV1ModemCore.getPhoneNumber())>'9'))))
62 {
63 theGSM3ShieldV1ModemCore.print('1');
64 theGSM3ShieldV1ModemCore.print('\r');
65 }
66 else
67 {
68 theGSM3ShieldV1ModemCore.print('0');
69 theGSM3ShieldV1ModemCore.print('\r');
70 }
71 theGSM3ShieldV1ModemCore.setCommandCounter(2);
72 break;
73 case 2:
74 if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
75 {
76 //Response received
77 if(resp)
78 {
79 // AT+QIOPEN
80 theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QIOPEN="),false);
81 theGSM3ShieldV1ModemCore.print("\"TCP\",\"");
82 if(theGSM3ShieldV1ModemCore.getPhoneNumber()!=0)
83 {
84 theGSM3ShieldV1ModemCore.print(theGSM3ShieldV1ModemCore.getPhoneNumber());
85 }
86 else
87 {
88 remoteIP.printTo(theGSM3ShieldV1ModemCore);
89 }
90 theGSM3ShieldV1ModemCore.print('"');
91 theGSM3ShieldV1ModemCore.print(',');
92 theGSM3ShieldV1ModemCore.print(theGSM3ShieldV1ModemCore.getPort());
93 theGSM3ShieldV1ModemCore.print('\r');
94 theGSM3ShieldV1ModemCore.setCommandCounter(3);
95 }
96 else theGSM3ShieldV1ModemCore.closeCommand(3);
97 }
98 break;
99
100 case 3:
101 if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp))
102 {
103 // Response received
104 if(resp)
105 {
106 // OK Received
107 // Great. Go for the next step
108 theGSM3ShieldV1ModemCore.setCommandCounter(4);
109 }
110 else theGSM3ShieldV1ModemCore.closeCommand(3);
111 }
112 break;
113 case 4:
114 char auxLocate [12];
115 prepareAuxLocate(PSTR("CONNECT\r\n"), auxLocate);
116 if(theGSM3ShieldV1ModemCore.genericParse_rsp(resp,auxLocate))
117 {
118 // Response received
119 if(resp)
120 {
121 // Received CONNECT OK
122 // Great. We're done
123 theGSM3ShieldV1ModemCore.setStatus(TRANSPARENT_CONNECTED);
124 theGSM3ShieldV1ModemCore.theBuffer().chopUntil(auxLocate, true);
125 theGSM3ShieldV1ModemCore.closeCommand(1);
126 }
127 else
128 theGSM3ShieldV1ModemCore.closeCommand(3);
129 }
130 break;
131
132 }
133}
134
135//Disconnect TCP main function.
136int GSM3ShieldV1ClientProvider::disconnectTCP(bool client1Server0, int id_socket)
137{
138 // id Socket does not really mean anything, in this case we have
139 // only one socket running
140 theGSM3ShieldV1ModemCore.openCommand(this,DISCONNECTTCP);
141
142 // If we are not closed, launch the command
143//[ZZ] if(theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED)
144// {
145 delay(1000);
146 theGSM3ShieldV1ModemCore.print("+++");
147 delay(1000);
148 theGSM3ShieldV1ModemCore.genericCommand_rq(PSTR("AT+QICLOSE"));
149 theGSM3ShieldV1ModemCore.setStatus(GPRS_READY);
150// }
151 // Looks like it runs everytime, so we simply flush to death and go on
152 do
153 {
154 // Empty the local buffer, and tell the modem to XON
155 // If meanwhile we receive a DISCONNECT we should detect it as URC.
156 theGSM3ShieldV1ModemCore.theBuffer().flush();
157 theGSM3ShieldV1ModemCore.gss.spaceAvailable();
158 // Give some time for the buffer to refill
159 delay(100);
160 theGSM3ShieldV1ModemCore.closeCommand(1);
161 }while(theGSM3ShieldV1ModemCore.theBuffer().storedBytes()>0);
162
163 theGSM3ShieldV1ModemCore.unRegisterUMProvider(this);
164 return theGSM3ShieldV1ModemCore.getCommandError();
165}
166
167
168//Write socket first chain main function.
169void GSM3ShieldV1ClientProvider::beginWriteSocket(bool client1Server0, int id_socket)
170{
171}
172
173
174//Write socket next chain function.
175void GSM3ShieldV1ClientProvider::writeSocket(const char* buf)
176{
177 if(theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED)
178 theGSM3ShieldV1ModemCore.print(buf);
179}
180
181//Write socket character function.
182void GSM3ShieldV1ClientProvider::writeSocket(uint8_t c)
183{
184 if(theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED)
185 theGSM3ShieldV1ModemCore.print((char)c);
186}
187
188//Write socket last chain main function.
189void GSM3ShieldV1ClientProvider::endWriteSocket()
190{
191}
192
193
194//Available socket main function.
195int GSM3ShieldV1ClientProvider::availableSocket(bool client1Server0, int id_socket)
196{
197
198 if(!(theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED))
199 theGSM3ShieldV1ModemCore.closeCommand(4);
200
201 if(theGSM3ShieldV1ModemCore.theBuffer().storedBytes())
202 theGSM3ShieldV1ModemCore.closeCommand(1);
203 else
204 theGSM3ShieldV1ModemCore.closeCommand(4);
205
206 return theGSM3ShieldV1ModemCore.getCommandError();
207}
208
209int GSM3ShieldV1ClientProvider::readSocket()
210{
211 char charSocket;
212
213 if(theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==0)
214 {
215 return 0;
216 }
217
218 charSocket = theGSM3ShieldV1ModemCore.theBuffer().read();
219
220 if(theGSM3ShieldV1ModemCore.theBuffer().availableBytes()==100)
221 theGSM3ShieldV1ModemCore.gss.spaceAvailable();
222
223 return charSocket;
224
225}
226
227//Read socket main function.
228int GSM3ShieldV1ClientProvider::peekSocket()
229{
230 return theGSM3ShieldV1ModemCore.theBuffer().peek(0);
231}
232
233
234//Flush SMS main function.
235void GSM3ShieldV1ClientProvider::flushSocket()
236{
237 theGSM3ShieldV1ModemCore.openCommand(this,FLUSHSOCKET);
238
239 flushSocketContinue();
240}
241
242//Send SMS continue function.
243void GSM3ShieldV1ClientProvider::flushSocketContinue()
244{
245 // If we have incomed data
246 if(theGSM3ShieldV1ModemCore.theBuffer().storedBytes()>0)
247 {
248 // Empty the local buffer, and tell the modem to XON
249 // If meanwhile we receive a DISCONNECT we should detect it as URC.
250 theGSM3ShieldV1ModemCore.theBuffer().flush();
251 theGSM3ShieldV1ModemCore.gss.spaceAvailable();
252 }
253 else
254 {
255 //We're done
256 theGSM3ShieldV1ModemCore.closeCommand(1);
257 }
258}
259
260// URC recognize.
261// Yes, we recognize "closes" in client mode
262bool GSM3ShieldV1ClientProvider::recognizeUnsolicitedEvent(byte oldTail)
263{
264 char auxLocate [12];
265 prepareAuxLocate(PSTR("CLOSED"), auxLocate);
266
267 if((theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED) & theGSM3ShieldV1ModemCore.theBuffer().chopUntil(auxLocate, false, false))
268 {
269 theGSM3ShieldV1ModemCore.setStatus(GPRS_READY);
270 theGSM3ShieldV1ModemCore.unRegisterUMProvider(this);
271 return true;
272 }
273
274 return false;
275}
276
277int GSM3ShieldV1ClientProvider::getSocket(int socket)
278{
279 return 0;
280}
281
282void GSM3ShieldV1ClientProvider::releaseSocket(int socket)
283{
284
285}
286
287bool GSM3ShieldV1ClientProvider::getStatusSocketClient(uint8_t socket)
288{
289 return (theGSM3ShieldV1ModemCore.getStatus()==TRANSPARENT_CONNECTED);
290
291};
292
293
294
Note: See TracBrowser for help on using the repository browser.