ftp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. #ifdef HARDWARE_BT6711
  2. #ifndef BT6702_SERVICE
  3. /*
  4. * lwftp.c : a lightweight FTP client using raw API of LWIP
  5. *
  6. * Copyright (c) 2014 GEZEDO
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * Author: Laurent GONZALEZ <lwip@gezedo.com>
  32. *
  33. */
  34. #pragma GCC diagnostic error "-Wall"
  35. #pragma GCC diagnostic error "-Wextra"
  36. #include <string.h>
  37. #include <stdbool.h>
  38. #include "ftp.h"
  39. #include "lwip/tcp.h"
  40. #include "stm32f4xx.h"
  41. #include "common_config.h"
  42. #include "spi_flash.h"
  43. #include "web_params_api.h"
  44. #include "FreeRTOS.h"
  45. #include "task.h"
  46. /** Enable debugging for LWFTP */
  47. #ifndef LWFTP_DEBUG
  48. #define LWFTP_DEBUG LWIP_DBG_ON
  49. #endif
  50. #define LWFTP_TRACE (LWFTP_DEBUG|LWIP_DBG_TRACE)
  51. #define LWFTP_WARNING (LWFTP_DEBUG|LWIP_DBG_LEVEL_WARNING)
  52. #define LWFTP_SERIOUS (LWFTP_DEBUG|LWIP_DBG_LEVEL_SERIOUS)
  53. #define LWFTP_SEVERE (LWFTP_DEBUG|LWIP_DBG_LEVEL_SEVERE)
  54. #define PTRNLEN(s) s,(sizeof(s)-1)
  55. #define min(x,y) ((x)<(y)?(x):(y))
  56. static unsigned received_bytes_count = 0;
  57. static char **error_ptr;
  58. /** Close control or data pcb
  59. * @param pointer to lwftp session data
  60. */
  61. static err_t lwftp_pcb_close(struct tcp_pcb *tpcb)
  62. {
  63. err_t error;
  64. tcp_err(tpcb, NULL);
  65. tcp_recv(tpcb, NULL);
  66. tcp_sent(tpcb, NULL);
  67. error = tcp_close(tpcb);
  68. if ( error != ERR_OK ) {
  69. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:pcb close failure, not implemented\n"));
  70. }
  71. return ERR_OK;
  72. }
  73. /** Send data
  74. * @param pointer to lwftp session data
  75. * @param pointer to PCB
  76. * @param number of bytes sent
  77. */
  78. static err_t lwftp_send_next_data(lwftp_session_t *s)
  79. {
  80. const char *data;
  81. int len = 0;
  82. err_t error = ERR_OK;
  83. if (s->data_source) {
  84. len = s->data_source(s->data_handle, &data, s->data_pcb->mss);
  85. if (len) {
  86. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:sending %d bytes of data\n",len));
  87. error = tcp_write(s->data_pcb, data, len, 0);
  88. if (error!=ERR_OK) {
  89. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:write failure (%s), not implemented\n",lwip_strerr(error)));
  90. }
  91. }
  92. }
  93. if (!len) {
  94. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:end of file\n"));
  95. lwftp_pcb_close(s->data_pcb);
  96. s->data_pcb = NULL;
  97. }
  98. return ERR_OK;
  99. }
  100. /** Handle data connection incoming data
  101. * @param pointer to lwftp session data
  102. * @param pointer to PCB
  103. * @param pointer to incoming pbuf
  104. * @param state of incoming process
  105. */
  106. static err_t lwftp_data_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  107. {
  108. (void)err;
  109. lwftp_session_t *s = (lwftp_session_t*)arg;
  110. if (p) {
  111. if (s->data_sink) {
  112. struct pbuf *q;
  113. for (q=p; q; q=q->next) {
  114. s->data_sink(s->data_handle, q->payload, q->len);
  115. }
  116. } else {
  117. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp: sinking %d bytes\n",p->tot_len));
  118. }
  119. tcp_recved(tpcb, p->tot_len);
  120. pbuf_free(p);
  121. } else {
  122. if (s->data_sink) {
  123. s->data_sink(s->data_handle, NULL, 0);
  124. }
  125. // NULL pbuf shall lead to close the pcb.
  126. if (s->data_pcb) {
  127. lwftp_pcb_close(s->data_pcb);
  128. s->data_pcb = NULL;
  129. }
  130. }
  131. return ERR_OK;
  132. }
  133. /** Handle data connection acknowledge of sent data
  134. * @param pointer to lwftp session data
  135. * @param pointer to PCB
  136. * @param number of bytes sent
  137. */
  138. static err_t lwftp_data_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  139. {
  140. (void)tpcb;
  141. lwftp_session_t *s = (lwftp_session_t*)arg;
  142. if ( s->data_source ) {
  143. s->data_source(s->data_handle, NULL, len);
  144. }
  145. return lwftp_send_next_data(s);
  146. }
  147. /** Handle data connection error
  148. * @param pointer to lwftp session data
  149. * @param state of connection
  150. */
  151. static void lwftp_data_err(void *arg, err_t err)
  152. {
  153. LWIP_UNUSED_ARG(err);
  154. if (arg != NULL) {
  155. lwftp_session_t *s = (lwftp_session_t*)arg;
  156. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:failed/error connecting for data to server (%s)\n",lwip_strerr(err)));
  157. s->control_state = LWFTP_QUIT; // gracefully exit on data error
  158. s->data_pcb = NULL; // No need to de-allocate PCB
  159. }
  160. }
  161. /** Process newly connected PCB
  162. * @param pointer to lwftp session data
  163. * @param pointer to PCB
  164. * @param state of connection
  165. */
  166. static err_t lwftp_data_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  167. {
  168. (void)tpcb;
  169. lwftp_session_t *s = (lwftp_session_t*)arg;
  170. if ( err == ERR_OK ) {
  171. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:connected for data to server\n"));
  172. s->data_state = LWFTP_CONNECTED;
  173. } else {
  174. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:err in data_connected (%s)\n",lwip_strerr(err)));
  175. }
  176. return err;
  177. }
  178. /** Open data connection for passive transfer
  179. * @param pointer to lwftp session data
  180. * @param pointer to incoming PASV response
  181. */
  182. static err_t lwftp_data_open(lwftp_session_t *s, struct pbuf *p)
  183. {
  184. err_t error;
  185. char *ptr;
  186. ip_addr_t data_server;
  187. u16_t data_port;
  188. // Find server connection parameter
  189. ptr = strchr(p->payload, '(');
  190. if (!ptr) return ERR_BUF;
  191. do {
  192. unsigned int a = strtoul(ptr+1,&ptr,10);
  193. unsigned int b = strtoul(ptr+1,&ptr,10);
  194. unsigned int c = strtoul(ptr+1,&ptr,10);
  195. unsigned int d = strtoul(ptr+1,&ptr,10);
  196. IP4_ADDR(&data_server,a,b,c,d);
  197. } while(0);
  198. data_port = strtoul(ptr+1,&ptr,10) << 8;
  199. data_port |= strtoul(ptr+1,&ptr,10) & 255;
  200. if (*ptr!=')') return ERR_BUF;
  201. // Open data session
  202. tcp_arg(s->data_pcb, s);
  203. tcp_err(s->data_pcb, lwftp_data_err);
  204. tcp_recv(s->data_pcb, lwftp_data_recv);
  205. tcp_sent(s->data_pcb, lwftp_data_sent);
  206. error = tcp_connect(s->data_pcb, &data_server, data_port, lwftp_data_connected);
  207. return error;
  208. }
  209. /** Send a message to control connection
  210. * @param pointer to lwftp session data
  211. * @param pointer to message string
  212. */
  213. static err_t lwftp_send_msg(lwftp_session_t *s, const char* msg, size_t len)
  214. {
  215. err_t error;
  216. LWIP_DEBUGF(LWFTP_TRACE,("lwftp:sending %s",msg));
  217. error = tcp_write(s->control_pcb, msg, len, 0);
  218. if ( error != ERR_OK ) {
  219. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:cannot write (%s)\n",lwip_strerr(error)));
  220. }
  221. return error;
  222. }
  223. /** Close control connection
  224. * @param pointer to lwftp session data
  225. * @param result to pass to callback fn (if called)
  226. */
  227. static void lwftp_control_close(lwftp_session_t *s, int result)
  228. {
  229. if (s->control_pcb) {
  230. lwftp_pcb_close(s->control_pcb);
  231. s->control_pcb = NULL;
  232. }
  233. s->control_state = LWFTP_CLOSED;
  234. if ( (result >= 0) && s->done_fn ) {
  235. s->done_fn(s->data_handle, result);
  236. }
  237. }
  238. /** Main client state machine
  239. * @param pointer to lwftp session data
  240. * @param pointer to PCB
  241. * @param pointer to incoming data
  242. */
  243. static void lwftp_control_process(lwftp_session_t *s, struct tcp_pcb *tpcb, struct pbuf *p)
  244. {
  245. (void)tpcb;
  246. unsigned response = 0;
  247. int result = LWFTP_RESULT_ERR_SRVR_RESP;
  248. // Try to get response number
  249. if (p) {
  250. response = strtoul(p->payload, NULL, 10);
  251. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:got response %d\n",response));
  252. }
  253. switch (s->control_state) {
  254. case LWFTP_CONNECTED:
  255. if (response>0) {
  256. if (response==220) {
  257. lwftp_send_msg(s, PTRNLEN("USER "));
  258. lwftp_send_msg(s, s->settings->user, strlen(s->settings->user));
  259. lwftp_send_msg(s, PTRNLEN("\n"));
  260. s->control_state = LWFTP_USER_SENT;
  261. } else {
  262. s->control_state = LWFTP_QUIT;
  263. }
  264. }
  265. break;
  266. case LWFTP_USER_SENT:
  267. if (response>0) {
  268. if (response==331) {
  269. lwftp_send_msg(s, PTRNLEN("PASS "));
  270. lwftp_send_msg(s, s->settings->pass, strlen(s->settings->pass));
  271. lwftp_send_msg(s, PTRNLEN("\n"));
  272. s->control_state = LWFTP_PASS_SENT;
  273. } else if (response==230) {
  274. goto anonymous;
  275. } else {
  276. s->control_state = LWFTP_QUIT;
  277. }
  278. }
  279. break;
  280. case LWFTP_PASS_SENT:
  281. if (response>0) {
  282. if (response==230) {
  283. anonymous:
  284. lwftp_send_msg(s, PTRNLEN("TYPE I\n"));
  285. s->control_state = LWFTP_TYPE_SENT;
  286. } else {
  287. s->control_state = LWFTP_QUIT;
  288. }
  289. }
  290. break;
  291. case LWFTP_TYPE_SENT:
  292. if (response>0) {
  293. if (response==200) {
  294. lwftp_send_msg(s, PTRNLEN("PASV\n"));
  295. s->control_state = LWFTP_PASV_SENT;
  296. } else {
  297. s->control_state = LWFTP_QUIT;
  298. }
  299. }
  300. break;
  301. case LWFTP_PASV_SENT:
  302. if (response>0) {
  303. if (response==227) {
  304. lwftp_data_open(s,p);
  305. switch (s->target_state) {
  306. case LWFTP_STOR_SENT:
  307. lwftp_send_msg(s, PTRNLEN("STOR "));
  308. break;
  309. case LWFTP_RETR_SENT:
  310. lwftp_send_msg(s, PTRNLEN("RETR "));
  311. break;
  312. default:
  313. //LOG_ERROR("Unexpected internal state");
  314. s->target_state = LWFTP_QUIT;
  315. }
  316. lwftp_send_msg(s, s->settings->remote_path, strlen(s->settings->remote_path));
  317. lwftp_send_msg(s, PTRNLEN("\n"));
  318. s->control_state = s->target_state;
  319. } else {
  320. s->control_state = LWFTP_QUIT;
  321. }
  322. }
  323. break;
  324. case LWFTP_RETR_SENT:
  325. if (response>0) {
  326. if (response==150) {
  327. s->control_state = LWFTP_XFERING;
  328. } else if (response==550) {
  329. s->control_state = LWFTP_QUIT;
  330. s->error = "Failed to open file";
  331. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s '%s'\n", error, s->settings->remote_path));
  332. }
  333. else {
  334. s->control_state = LWFTP_QUIT;
  335. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 150, received %d\n",response));
  336. }
  337. }
  338. break;
  339. case LWFTP_STOR_SENT:
  340. if (response>0) {
  341. if (response==150) {
  342. s->control_state = LWFTP_XFERING;
  343. lwftp_data_sent(s,NULL,0);
  344. } else {
  345. s->control_state = LWFTP_QUIT;
  346. }
  347. }
  348. break;
  349. case LWFTP_XFERING:
  350. if (response>0) {
  351. if (response==226) {
  352. s->data_state = LWFTP_XFERING; // signal transfer OK
  353. } else {
  354. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 226, received %d\n",response));
  355. }
  356. // Quit anyway after any message received during STOR
  357. s->control_state = LWFTP_QUIT;
  358. }
  359. break;
  360. case LWFTP_QUIT_SENT:
  361. if (response>0) {
  362. if (response==221) {
  363. if (s->data_state == LWFTP_XFERING){ // check for transfer OK
  364. result = LWFTP_RESULT_OK;
  365. }
  366. } else {
  367. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 221, received %d\n",response));
  368. }
  369. s->control_state = LWFTP_CLOSING;
  370. }
  371. break;
  372. default:
  373. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:unhandled state (%d)\n",s->control_state));
  374. }
  375. // Free receiving pbuf if any
  376. if (p) {
  377. pbuf_free(p);
  378. }
  379. // Handle second step in state machine
  380. switch ( s->control_state ) {
  381. case LWFTP_QUIT:
  382. lwftp_send_msg(s, PTRNLEN("QUIT\n"));
  383. s->control_state = LWFTP_QUIT_SENT;
  384. break;
  385. case LWFTP_CLOSING:
  386. // this function frees s, no use of s is allowed after
  387. return lwftp_control_close(s, result);
  388. default:;
  389. }
  390. }
  391. /** Handle control connection incoming data
  392. * @param pointer to lwftp session data
  393. * @param pointer to PCB
  394. * @param pointer to incoming pbuf
  395. * @param state of incoming process
  396. */
  397. static err_t lwftp_control_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  398. {
  399. lwftp_session_t *s = (lwftp_session_t*)arg;
  400. if ( err == ERR_OK ) {
  401. if (p) {
  402. tcp_recved(tpcb, p->tot_len);
  403. lwftp_control_process(s, tpcb, p);
  404. } else {
  405. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  406. lwftp_control_close(s, LWFTP_RESULT_ERR_CLOSED);
  407. }
  408. } else {
  409. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:failed to receive (%s)\n",lwip_strerr(err)));
  410. lwftp_control_close(s, LWFTP_RESULT_ERR_UNKNOWN);
  411. }
  412. return err;
  413. }
  414. /** Handle control connection acknowledge of sent data
  415. * @param pointer to lwftp session data
  416. * @param pointer to PCB
  417. * @param number of bytes sent
  418. */
  419. static err_t lwftp_control_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  420. {
  421. (void)arg;
  422. (void)tpcb;
  423. (void)len;
  424. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:successfully sent %d bytes\n",len));
  425. return ERR_OK;
  426. }
  427. /** Handle control connection error
  428. * @param pointer to lwftp session data
  429. * @param state of connection
  430. */
  431. static void lwftp_control_err(void *arg, err_t err)
  432. {
  433. LWIP_UNUSED_ARG(err);
  434. if (arg != NULL) {
  435. lwftp_session_t *s = (lwftp_session_t*)arg;
  436. int result;
  437. if( s->control_state == LWFTP_CLOSED ) {
  438. s->error = "failed to connect to server";
  439. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s (%s)\n",lwip_strerr(err)));
  440. result = LWFTP_RESULT_ERR_CONNECT;
  441. } else {
  442. s->error = "connection closed by remote host";
  443. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  444. result = LWFTP_RESULT_ERR_CLOSED;
  445. }
  446. s->control_pcb = NULL; // No need to de-allocate PCB
  447. lwftp_control_close(s, result);
  448. }
  449. }
  450. /** Process newly connected PCB
  451. * @param pointer to lwftp session data
  452. * @param pointer to PCB
  453. * @param state of connection
  454. */
  455. static err_t lwftp_control_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  456. {
  457. (void)tpcb;
  458. lwftp_session_t *s = (lwftp_session_t*)arg;
  459. if ( err == ERR_OK ) {
  460. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:connected to server\n"));
  461. s->control_state = LWFTP_CONNECTED;
  462. } else {
  463. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:err in control_connected (%s)\n",lwip_strerr(err)));
  464. }
  465. return err;
  466. }
  467. /** Store data to a remote file
  468. * @param Session structure
  469. */
  470. err_t lwftp_store(lwftp_session_t *s)
  471. {
  472. err_t error;
  473. // Check user supplied data
  474. if ( (s->control_state!=LWFTP_CLOSED) ||
  475. !s->settings->remote_path ||
  476. s->control_pcb ||
  477. s->data_pcb ||
  478. !s->settings->user ||
  479. !s->settings->pass )
  480. {
  481. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:invalid session data\n"));
  482. return ERR_ARG;
  483. }
  484. // Get sessions pcb
  485. s->control_pcb = tcp_new();
  486. if (!s->control_pcb) {
  487. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc control_pcb (low memory?)\n"));
  488. error = ERR_MEM;
  489. goto exit;
  490. }
  491. s->data_pcb = tcp_new();
  492. if (!s->data_pcb) {
  493. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc data_pcb (low memory?)\n"));
  494. error = ERR_MEM;
  495. goto close_pcb;
  496. }
  497. // Open control session
  498. tcp_arg(s->control_pcb, s);
  499. tcp_err(s->control_pcb, lwftp_control_err);
  500. tcp_recv(s->control_pcb, lwftp_control_recv);
  501. tcp_sent(s->control_pcb, lwftp_control_sent);
  502. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  503. if ( error == ERR_OK ) goto exit;
  504. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  505. close_pcb:
  506. // Release pcbs in case of failure
  507. lwftp_control_close(s, -1);
  508. exit:
  509. return error;
  510. }
  511. err_t lwftp_retr(lwftp_session_t *s)
  512. {
  513. s->target_state = LWFTP_RETR_SENT;
  514. err_t error;
  515. // Check user supplied data
  516. if ( (s->control_state!=LWFTP_CLOSED) ||
  517. !s->settings->remote_path ||
  518. s->control_pcb ||
  519. s->data_pcb ||
  520. !s->settings->user ||
  521. !s->settings->pass )
  522. {
  523. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:invalid session data\n"));
  524. return ERR_ARG;
  525. }
  526. // Get sessions pcb
  527. s->control_pcb = tcp_new();
  528. if (!s->control_pcb) {
  529. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc control_pcb (low memory?)\n"));
  530. error = ERR_MEM;
  531. goto exit;
  532. }
  533. s->data_pcb = tcp_new();
  534. if (!s->data_pcb) {
  535. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc data_pcb (low memory?)\n"));
  536. error = ERR_MEM;
  537. goto close_pcb;
  538. }
  539. // Open control session
  540. tcp_arg(s->control_pcb, s);
  541. tcp_err(s->control_pcb, lwftp_control_err);
  542. tcp_recv(s->control_pcb, lwftp_control_recv);
  543. tcp_sent(s->control_pcb, lwftp_control_sent);
  544. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  545. if ( error == ERR_OK ) goto exit;
  546. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  547. close_pcb:
  548. // Release pcbs in case of failure
  549. lwftp_control_close(s, -1);
  550. exit:
  551. return error;
  552. }
  553. //static void ftp_retr_callback(void *arg, int result)
  554. //{
  555. // lwftp_session_t *s = (lwftp_session_t *)arg;
  556. //
  557. // if (result != LWFTP_RESULT_OK) {
  558. // //LOG_ERROR("retr failed (%d)", result);
  559. // return lwftp_close(s);
  560. // }
  561. // lwftp_close(s);
  562. //}
  563. static bool validate_spif_firmware(uint32_t fw_len)
  564. {
  565. const uint32_t spif_firmware_offset = SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET;
  566. // check the firmware revision id
  567. char rev[HW_REV_LEN];
  568. spi_flash_read(spif_firmware_offset + HW_REV_OFFSET, &rev, sizeof(rev), 0);
  569. if (strncmp(rev, HW_REV, sizeof(rev))) {
  570. printf("HW revision mismatch: expected %s, found %s\r\n", HW_REV, rev);
  571. return false;
  572. }
  573. // check if the firmware length is correct
  574. if (fw_len != MAIN_FW_SIZE) {
  575. printf("ftp: firmware size mismatch: expected %d, got %ld\r\n", MAIN_FW_SIZE, fw_len);
  576. return false;
  577. }
  578. uint32_t expected_crc;
  579. const uint32_t crc_offset = USER_FLASH_CRC_ADDRESS - USER_FLASH_FIRST_PAGE_ADDRESS;
  580. spi_flash_read(spif_firmware_offset + crc_offset, &expected_crc, sizeof(expected_crc), 0);
  581. // calculate CRC
  582. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
  583. CRC->CR = 1;
  584. for (uint32_t offset = 0; offset < crc_offset; offset += 4) {
  585. uint32_t data;
  586. spi_flash_read(spif_firmware_offset + offset, &data, sizeof(data), 0);
  587. CRC->DR = data;
  588. }
  589. uint32_t calculated_crc = CRC->DR;
  590. if (expected_crc != calculated_crc) {
  591. printf("ftp: calculated CRC (%lx) doesn't match the expected CRC (%lx)!\r\n", calculated_crc, expected_crc);
  592. return false;
  593. }
  594. return true;
  595. }
  596. static void erase_spif_firmware()
  597. {
  598. printf("ftp: erasing SPI firmware partition...\r\n");
  599. for (int sec = 0; sec < FIRMWARE_UPDATE_SECTOR_COUNT; ++sec) {
  600. spi_flash_erase_sector(SPI_FLASH_SECTOR_SIZE * (FIRMWARE_UPDATE_SECTOR_OFFSET + sec), 0);
  601. }
  602. }
  603. static unsigned data_sink(void *arg, const char* ptr, unsigned len)
  604. {
  605. (void)arg;
  606. if (ptr && len) {
  607. // we received some data from the FTP server
  608. if (received_bytes_count == 0) {
  609. // we need to erase the flash before we can write it
  610. erase_spif_firmware();
  611. }
  612. if (received_bytes_count + len > MAIN_FW_SIZE) {
  613. printf("ftp: the firmware is too big! aborting the download\r\n");
  614. return 0;
  615. }
  616. spi_flash_write(SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET + received_bytes_count, ptr, len, 0);
  617. received_bytes_count += len;
  618. } else {
  619. printf("ftp: the firmware is downloaded, verifying...\r\n");
  620. uint32_t fw_size = received_bytes_count;
  621. // next time it comes, overwrite the existing SPI contents
  622. received_bytes_count = 0;
  623. bool good_firmware = validate_spif_firmware(fw_size);
  624. if (good_firmware) {
  625. printf("ftp: the firmware is valid, rebooting...\r\n");
  626. HTTP_StartResetTask(true);
  627. } else {
  628. // erase it so the bootloader won't try to verify it every time
  629. erase_spif_firmware();
  630. }
  631. }
  632. return len;
  633. }
  634. void start_ftp_client(lwftp_session_t *s)
  635. {
  636. received_bytes_count = 0;
  637. s->error = NULL;
  638. error_ptr = &s->error;
  639. s->data_sink = data_sink;
  640. //s->done_fn = ftp_retr_callback;
  641. lwftp_retr(s);
  642. // FTP session will continue with the connection callback
  643. }
  644. char *get_ftp_progress()
  645. {
  646. if (*error_ptr) {
  647. return *error_ptr;
  648. } else {
  649. unsigned progress = received_bytes_count * 100 / MAIN_FW_SIZE;
  650. static char progress_str_buf[4];
  651. snprintf(progress_str_buf, sizeof(progress_str_buf), "%u", progress);
  652. return progress_str_buf;
  653. }
  654. }
  655. #endif // !BT6702_SERVICE
  656. #endif // HARDWARE_BT6711