2626 */
2727
2828#include " Ftp.h"
29- #include < string.h>
3029
3130const char AT_FTPCID[] PROGMEM = " AT+FTPCID=1\r\n " ;
3231const char AT_FTPSERV[] PROGMEM = " AT+FTPSERV=\" %s\"\r\n " ;
@@ -46,135 +45,68 @@ const char REGISTRATION_STATUS[] PROGMEM = "AT+CREG?\r\n";
4645const char SLEEP_MODE_2[] PROGMEM = " AT+CSCLK=2\r\n " ;
4746
4847const char OK[] PROGMEM = " OK" ;
48+ const char OK_[] = " OK" ;
4949const char AT_FTPPUT1_RESP[] PROGMEM = " 1,1" ;
5050const char AT_FTPPUT2_RESP[] PROGMEM = " +FTPPUT: 2" ;
5151const char AT_FTPPUT20_RESP[] PROGMEM = " 1,0" ;
5252const char CONNECTED[] PROGMEM = " +CREG: 0,1" ;
5353const char ROAMING[] PROGMEM = " +CREG: 0,5" ;
5454const char BEARER_OPEN[] PROGMEM = " +SAPBR: 1,1" ;
5555
56- Result FTP::configureBearer (const char *apn)
57- {
58- Result result = SUCCESS;
59- char buffer[64 ];
60- char resp1[12 ];
61- char resp2[12 ];
62- unsigned int attempts = 0 ;
63- unsigned int MAX_ATTEMPTS = 10 ;
64-
65- sendATTest ();
66-
67- strcpy_P (buffer, REGISTRATION_STATUS);
68- strcpy_P (resp1, CONNECTED);
69- strcpy_P (resp2, ROAMING);
70- while ((sendCmdAndWaitForResp (buffer, resp1, 2000 ) != TRUE &&
71- sendCmdAndWaitForResp (buffer, resp2, 2000 ) != TRUE ) &&
72- attempts < MAX_ATTEMPTS)
73- {
74- attempts++;
75- delay (1000 * attempts);
76- if (attempts == MAX_ATTEMPTS)
77- {
78- attempts = 0 ;
79- preInit ();
80- }
81- }
82- attempts = 0 ;
83-
84- strcpy_P (buffer, BEARER_PROFILE_GPRS);
85- strcpy_P (resp1, OK);
86- if (sendCmdAndWaitForResp (buffer, resp1, 2000 ) == FALSE )
87- result = ERROR_BEARER_PROFILE_GPRS;
88-
89- sprintf_P (buffer, BEARER_PROFILE_APN, apn);
90- strcpy_P (resp1, OK);
91- if (sendCmdAndWaitForResp (buffer, resp1, 2000 ) == FALSE )
92- result = ERROR_BEARER_PROFILE_APN;
93-
94- strcpy_P (buffer, QUERY_BEARER);
95- strcpy_P (resp1, BEARER_OPEN);
96- while (sendCmdAndWaitForResp (buffer, resp1, 2000 ) == FALSE && attempts < MAX_ATTEMPTS)
97- {
98- attempts++;
99- if (attempts == MAX_ATTEMPTS)
100- {
101- result = ERROR_OPEN_GPRS_CONTEXT;
102- }
103- }
104-
105- attempts = 0 ;
106-
107- strcpy_P (buffer, OPEN_GPRS_CONTEXT);
108- strcpy_P (resp1, OK);
109- while (sendCmdAndWaitForResp (buffer, resp1, 2000 ) == FALSE && attempts < MAX_ATTEMPTS)
110- {
111- attempts++;
112- if (attempts == MAX_ATTEMPTS)
113- {
114- result = ERROR_QUERY_GPRS_CONTEXT;
115- }
116- }
56+ #include " GPRS.h"
11757
118- return result;
119- }
120-
121- Result FTP::putBegin (const char *fileName,
58+ Result FTP::putBegin (const char *apn,
59+ const char *fileName,
12260 const char *server,
12361 const char *usr,
12462 const char *pass,
12563 const char *path)
12664{
127- Result result = SUCCESS ;
65+ Result result = openGPRSContext (* this , apn) ;
12866
12967 char buffer[64 ];
130- char resp[ 12 ];
68+ char tmp[ 24 ];
13169
13270 delay (10000 );
133- strcpy_P (buffer, AT_FTPCID);
134- strcpy_P (resp, OK);
135- if (sendCmdAndWaitForResp (buffer, resp, 2000 ) == FALSE )
71+ if (sendCmdAndWaitForResp_P (AT_FTPCID, OK, 2000 ) == FALSE )
13672 {
13773 return ERROR_FTPCID;
13874 }
13975
140- sprintf_P (buffer, AT_FTPSERV , server);
141- strcpy_P (resp, OK );
142- if (sendCmdAndWaitForResp (buffer, resp , 2000 ) == FALSE )
76+ strcpy_P (tmp , server);
77+ sprintf_P (buffer, AT_FTPSERV, tmp );
78+ if (sendCmdAndWaitForResp (buffer, OK , 2000 ) == FALSE )
14379 {
14480 return ERROR_FTPSERV;
14581 }
14682
147- sprintf_P (buffer, AT_FTPUN , usr);
148- strcpy_P (resp, OK );
149- if (sendCmdAndWaitForResp (buffer, resp , 2000 ) == FALSE )
83+ strcpy_P (tmp , usr);
84+ sprintf_P (buffer, AT_FTPUN, tmp );
85+ if (sendCmdAndWaitForResp (buffer, OK_ , 2000 ) == FALSE )
15086 {
15187 return ERROR_FTPUN;
15288 }
15389
154- sprintf_P (buffer, AT_FTPPW , pass);
155- strcpy_P (resp, OK );
156- if (sendCmdAndWaitForResp (buffer, resp , 2000 ) == FALSE )
90+ strcpy_P (tmp , pass);
91+ sprintf_P (buffer, AT_FTPPW, tmp );
92+ if (sendCmdAndWaitForResp (buffer, OK_ , 2000 ) == FALSE )
15793 {
15894 return ERROR_FTPPW;
15995 }
16096
16197 sprintf_P (buffer, AT_FTPPUTNAME, fileName);
162- strcpy_P (resp, OK);
163- if (sendCmdAndWaitForResp (buffer, resp, 2000 ) == FALSE )
98+ if (sendCmdAndWaitForResp (buffer, OK_, 2000 ) == FALSE )
16499 {
165100 return ERROR_FTPPUTNAME;
166101 }
167102
168103 sprintf_P (buffer, AT_FTPPUTPATH, path);
169- strcpy_P (resp, OK);
170- if (sendCmdAndWaitForResp (buffer, resp, 2000 ) == FALSE )
104+ if (sendCmdAndWaitForResp (buffer, OK_, 2000 ) == FALSE )
171105 {
172106 return ERROR_FTPPUTPATH;
173107 }
174108
175- strcpy_P (buffer, AT_FTPPUT1);
176- strcpy_P (resp, AT_FTPPUT1_RESP);
177- if (sendCmdAndWaitForResp (buffer, resp, 10000 ) == FALSE )
109+ if (sendCmdAndWaitForResp_P (AT_FTPPUT1, AT_FTPPUT1_RESP, 10000 ) == FALSE )
178110 {
179111 return ERROR_FTPPUT1;
180112 }
@@ -186,8 +118,8 @@ Result FTP::putWrite(const char *data, unsigned int size)
186118{
187119 Result result = SUCCESS;
188120
189- unsigned int attempts = 0 ;
190- unsigned int MAX_ATTEMPTS = 10 ;
121+ uint8_t attempts = 0 ;
122+ uint8_t MAX_ATTEMPTS = 10 ;
191123
192124 while (putWriteStart (size) != SUCCESS || putWriteEnd (data, size) != SUCCESS)
193125 {
@@ -221,12 +153,10 @@ Result FTP::putWriteStart(unsigned int size)
221153Result FTP::putWriteEnd (const char *data, unsigned int size)
222154{
223155 Result result = SUCCESS;
224-
225- char buffer[32 ];
226- char resp[32 ];
156+ char resp[8 ];
227157
228158 write (data, size);
229-
159+
230160 strcpy_P (resp, AT_FTPPUT1_RESP);
231161 if (waitForResp (resp, 2000 ) == FALSE )
232162 {
@@ -238,15 +168,9 @@ Result FTP::putWriteEnd(const char *data, unsigned int size)
238168
239169Result FTP::putEnd ()
240170{
241- Result result = SUCCESS;
242- serialSIM800.flush ();
243-
244- char buffer[32 ];
245- char resp[12 ];
171+ Result result = closeGPRSContext (*this );
246172
247- strcpy_P (buffer, AT_FTPPUT20);
248- strcpy_P (resp, AT_FTPPUT20_RESP);
249- if (sendCmdAndWaitForResp (AT_FTPPUT20, AT_FTPPUT20_RESP, 2000 ) == FALSE )
173+ if (sendCmdAndWaitForResp_P (AT_FTPPUT20, AT_FTPPUT20_RESP, 2000 ) == FALSE )
250174 {
251175 return ERROR_FTPPUT20;
252176 }
0 commit comments