ftp.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  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. /** Close control or data pcb
  58. * @param pointer to lwftp session data
  59. */
  60. static err_t lwftp_pcb_close(struct tcp_pcb *tpcb)
  61. {
  62. err_t error;
  63. tcp_err(tpcb, NULL);
  64. tcp_recv(tpcb, NULL);
  65. tcp_sent(tpcb, NULL);
  66. error = tcp_close(tpcb);
  67. if ( error != ERR_OK ) {
  68. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:pcb close failure, not implemented\n"));
  69. }
  70. return ERR_OK;
  71. }
  72. /** Send data
  73. * @param pointer to lwftp session data
  74. * @param pointer to PCB
  75. * @param number of bytes sent
  76. */
  77. static err_t lwftp_send_next_data(lwftp_session_t *s)
  78. {
  79. const char *data;
  80. int len = 0;
  81. err_t error = ERR_OK;
  82. if (s->data_source) {
  83. len = s->data_source(s->data_handle, &data, s->data_pcb->mss);
  84. if (len) {
  85. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:sending %d bytes of data\n",len));
  86. error = tcp_write(s->data_pcb, data, len, 0);
  87. if (error!=ERR_OK) {
  88. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:write failure (%s), not implemented\n",lwip_strerr(error)));
  89. }
  90. }
  91. }
  92. if (!len) {
  93. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:end of file\n"));
  94. lwftp_pcb_close(s->data_pcb);
  95. s->data_pcb = NULL;
  96. }
  97. return ERR_OK;
  98. }
  99. /** Handle data connection incoming data
  100. * @param pointer to lwftp session data
  101. * @param pointer to PCB
  102. * @param pointer to incoming pbuf
  103. * @param state of incoming process
  104. */
  105. static err_t lwftp_data_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  106. {
  107. (void)err;
  108. lwftp_session_t *s = (lwftp_session_t*)arg;
  109. if (p) {
  110. // notify the web interface that we can proceed
  111. s->error = "1";
  112. if (s->data_sink) {
  113. struct pbuf *q;
  114. for (q=p; q; q=q->next) {
  115. s->data_sink(s->data_handle, q->payload, q->len);
  116. }
  117. } else {
  118. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp: sinking %d bytes\n",p->tot_len));
  119. }
  120. tcp_recved(tpcb, p->tot_len);
  121. pbuf_free(p);
  122. } else {
  123. if (s->data_sink) {
  124. s->data_sink(s->data_handle, NULL, 0);
  125. }
  126. // NULL pbuf shall lead to close the pcb.
  127. if (s->data_pcb) {
  128. lwftp_pcb_close(s->data_pcb);
  129. s->data_pcb = NULL;
  130. }
  131. }
  132. return ERR_OK;
  133. }
  134. /** Handle data connection acknowledge of sent data
  135. * @param pointer to lwftp session data
  136. * @param pointer to PCB
  137. * @param number of bytes sent
  138. */
  139. static err_t lwftp_data_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  140. {
  141. (void)tpcb;
  142. lwftp_session_t *s = (lwftp_session_t*)arg;
  143. if ( s->data_source ) {
  144. s->data_source(s->data_handle, NULL, len);
  145. }
  146. return lwftp_send_next_data(s);
  147. }
  148. /** Handle data connection error
  149. * @param pointer to lwftp session data
  150. * @param state of connection
  151. */
  152. static void lwftp_data_err(void *arg, err_t err)
  153. {
  154. LWIP_UNUSED_ARG(err);
  155. if (arg != NULL) {
  156. lwftp_session_t *s = (lwftp_session_t*)arg;
  157. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:failed/error connecting for data to server (%s)\n",lwip_strerr(err)));
  158. s->control_state = LWFTP_QUIT; // gracefully exit on data error
  159. s->data_pcb = NULL; // No need to de-allocate PCB
  160. }
  161. }
  162. /** Process newly connected PCB
  163. * @param pointer to lwftp session data
  164. * @param pointer to PCB
  165. * @param state of connection
  166. */
  167. static err_t lwftp_data_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  168. {
  169. (void)tpcb;
  170. lwftp_session_t *s = (lwftp_session_t*)arg;
  171. if ( err == ERR_OK ) {
  172. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:connected for data to server\n"));
  173. s->data_state = LWFTP_CONNECTED;
  174. } else {
  175. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:err in data_connected (%s)\n",lwip_strerr(err)));
  176. }
  177. return err;
  178. }
  179. /** Open data connection for passive transfer
  180. * @param pointer to lwftp session data
  181. * @param pointer to incoming PASV response
  182. */
  183. static err_t lwftp_data_open(lwftp_session_t *s, struct pbuf *p)
  184. {
  185. err_t error;
  186. char *ptr;
  187. ip_addr_t data_server;
  188. u16_t data_port;
  189. // Find server connection parameter
  190. ptr = strchr(p->payload, '(');
  191. if (!ptr) return ERR_BUF;
  192. do {
  193. unsigned int a = strtoul(ptr+1,&ptr,10);
  194. unsigned int b = strtoul(ptr+1,&ptr,10);
  195. unsigned int c = strtoul(ptr+1,&ptr,10);
  196. unsigned int d = strtoul(ptr+1,&ptr,10);
  197. IP4_ADDR(&data_server,a,b,c,d);
  198. } while(0);
  199. data_port = strtoul(ptr+1,&ptr,10) << 8;
  200. data_port |= strtoul(ptr+1,&ptr,10) & 255;
  201. if (*ptr!=')') return ERR_BUF;
  202. // Open data session
  203. tcp_arg(s->data_pcb, s);
  204. tcp_err(s->data_pcb, lwftp_data_err);
  205. tcp_recv(s->data_pcb, lwftp_data_recv);
  206. tcp_sent(s->data_pcb, lwftp_data_sent);
  207. error = tcp_connect(s->data_pcb, &data_server, data_port, lwftp_data_connected);
  208. return error;
  209. }
  210. /** Send a message to control connection
  211. * @param pointer to lwftp session data
  212. * @param pointer to message string
  213. */
  214. static err_t lwftp_send_msg(lwftp_session_t *s, const char* msg, size_t len)
  215. {
  216. err_t error;
  217. LWIP_DEBUGF(LWFTP_TRACE,("lwftp:sending %s",msg));
  218. error = tcp_write(s->control_pcb, msg, len, 0);
  219. if ( error != ERR_OK ) {
  220. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:cannot write (%s)\n",lwip_strerr(error)));
  221. }
  222. return error;
  223. }
  224. /** Close control connection
  225. * @param pointer to lwftp session data
  226. * @param result to pass to callback fn (if called)
  227. */
  228. static void lwftp_control_close(lwftp_session_t *s, int result)
  229. {
  230. if (s->control_pcb) {
  231. lwftp_pcb_close(s->control_pcb);
  232. s->control_pcb = NULL;
  233. }
  234. s->control_state = LWFTP_CLOSED;
  235. if ( (result >= 0) && s->done_fn ) {
  236. s->done_fn(s->data_handle, result);
  237. }
  238. }
  239. /** Main client state machine
  240. * @param pointer to lwftp session data
  241. * @param pointer to PCB
  242. * @param pointer to incoming data
  243. */
  244. static void lwftp_control_process(lwftp_session_t *s, struct tcp_pcb *tpcb, struct pbuf *p)
  245. {
  246. (void)tpcb;
  247. unsigned response = 0;
  248. int result = LWFTP_RESULT_ERR_SRVR_RESP;
  249. // Try to get response number
  250. if (p) {
  251. response = strtoul(p->payload, NULL, 10);
  252. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:got response %d\n",response));
  253. }
  254. switch (s->control_state) {
  255. case LWFTP_CONNECTED:
  256. if (response>0) {
  257. if (response==220) {
  258. lwftp_send_msg(s, PTRNLEN("USER "));
  259. lwftp_send_msg(s, s->settings->user, strlen(s->settings->user));
  260. lwftp_send_msg(s, PTRNLEN("\n"));
  261. s->control_state = LWFTP_USER_SENT;
  262. } else {
  263. s->control_state = LWFTP_QUIT;
  264. }
  265. }
  266. break;
  267. case LWFTP_USER_SENT:
  268. if (response>0) {
  269. if (response==331) {
  270. lwftp_send_msg(s, PTRNLEN("PASS "));
  271. lwftp_send_msg(s, s->settings->pass, strlen(s->settings->pass));
  272. lwftp_send_msg(s, PTRNLEN("\n"));
  273. s->control_state = LWFTP_PASS_SENT;
  274. } else if (response==230) {
  275. goto anonymous;
  276. } else {
  277. s->control_state = LWFTP_QUIT;
  278. }
  279. }
  280. break;
  281. case LWFTP_PASS_SENT:
  282. if (response>0) {
  283. if (response==230) {
  284. anonymous:
  285. lwftp_send_msg(s, PTRNLEN("TYPE I\n"));
  286. s->control_state = LWFTP_TYPE_SENT;
  287. } else {
  288. s->control_state = LWFTP_QUIT;
  289. }
  290. }
  291. break;
  292. case LWFTP_TYPE_SENT:
  293. if (response>0) {
  294. if (response==200) {
  295. lwftp_send_msg(s, PTRNLEN("PASV\n"));
  296. s->control_state = LWFTP_PASV_SENT;
  297. } else {
  298. s->control_state = LWFTP_QUIT;
  299. }
  300. }
  301. break;
  302. case LWFTP_PASV_SENT:
  303. if (response>0) {
  304. if (response==227) {
  305. lwftp_data_open(s,p);
  306. switch (s->target_state) {
  307. case LWFTP_STOR_SENT:
  308. lwftp_send_msg(s, PTRNLEN("STOR "));
  309. break;
  310. case LWFTP_RETR_SENT:
  311. lwftp_send_msg(s, PTRNLEN("RETR "));
  312. break;
  313. default:
  314. //LOG_ERROR("Unexpected internal state");
  315. s->target_state = LWFTP_QUIT;
  316. }
  317. lwftp_send_msg(s, s->settings->remote_path, strlen(s->settings->remote_path));
  318. lwftp_send_msg(s, PTRNLEN("\n"));
  319. s->control_state = s->target_state;
  320. } else {
  321. s->control_state = LWFTP_QUIT;
  322. }
  323. }
  324. break;
  325. case LWFTP_RETR_SENT:
  326. if (response>0) {
  327. if (response==150) {
  328. s->control_state = LWFTP_XFERING;
  329. } else if (response==550) {
  330. s->control_state = LWFTP_QUIT;
  331. s->error = "Failed to open file"
  332. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s '%s'\n", error, s->settings->remote_path));
  333. }
  334. else {
  335. s->control_state = LWFTP_QUIT;
  336. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 150, received %d\n",response));
  337. }
  338. }
  339. break;
  340. case LWFTP_STOR_SENT:
  341. if (response>0) {
  342. if (response==150) {
  343. s->control_state = LWFTP_XFERING;
  344. lwftp_data_sent(s,NULL,0);
  345. } else {
  346. s->control_state = LWFTP_QUIT;
  347. }
  348. }
  349. break;
  350. case LWFTP_XFERING:
  351. if (response>0) {
  352. if (response==226) {
  353. s->data_state = LWFTP_XFERING; // signal transfer OK
  354. } else {
  355. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 226, received %d\n",response));
  356. }
  357. // Quit anyway after any message received during STOR
  358. s->control_state = LWFTP_QUIT;
  359. }
  360. break;
  361. case LWFTP_QUIT_SENT:
  362. if (response>0) {
  363. if (response==221) {
  364. if (s->data_state == LWFTP_XFERING){ // check for transfer OK
  365. result = LWFTP_RESULT_OK;
  366. }
  367. } else {
  368. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 221, received %d\n",response));
  369. }
  370. s->control_state = LWFTP_CLOSING;
  371. }
  372. break;
  373. default:
  374. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:unhandled state (%d)\n",s->control_state));
  375. }
  376. // Free receiving pbuf if any
  377. if (p) {
  378. pbuf_free(p);
  379. }
  380. // Handle second step in state machine
  381. switch ( s->control_state ) {
  382. case LWFTP_QUIT:
  383. lwftp_send_msg(s, PTRNLEN("QUIT\n"));
  384. s->control_state = LWFTP_QUIT_SENT;
  385. break;
  386. case LWFTP_CLOSING:
  387. // this function frees s, no use of s is allowed after
  388. return lwftp_control_close(s, result);
  389. default:;
  390. }
  391. }
  392. /** Handle control connection incoming data
  393. * @param pointer to lwftp session data
  394. * @param pointer to PCB
  395. * @param pointer to incoming pbuf
  396. * @param state of incoming process
  397. */
  398. static err_t lwftp_control_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  399. {
  400. lwftp_session_t *s = (lwftp_session_t*)arg;
  401. if ( err == ERR_OK ) {
  402. if (p) {
  403. tcp_recved(tpcb, p->tot_len);
  404. lwftp_control_process(s, tpcb, p);
  405. } else {
  406. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  407. lwftp_control_close(s, LWFTP_RESULT_ERR_CLOSED);
  408. }
  409. } else {
  410. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:failed to receive (%s)\n",lwip_strerr(err)));
  411. lwftp_control_close(s, LWFTP_RESULT_ERR_UNKNOWN);
  412. }
  413. return err;
  414. }
  415. /** Handle control connection acknowledge of sent data
  416. * @param pointer to lwftp session data
  417. * @param pointer to PCB
  418. * @param number of bytes sent
  419. */
  420. static err_t lwftp_control_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  421. {
  422. (void)arg;
  423. (void)tpcb;
  424. (void)len;
  425. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:successfully sent %d bytes\n",len));
  426. return ERR_OK;
  427. }
  428. /** Handle control connection error
  429. * @param pointer to lwftp session data
  430. * @param state of connection
  431. */
  432. static void lwftp_control_err(void *arg, err_t err)
  433. {
  434. LWIP_UNUSED_ARG(err);
  435. if (arg != NULL) {
  436. lwftp_session_t *s = (lwftp_session_t*)arg;
  437. int result;
  438. if( s->control_state == LWFTP_CLOSED ) {
  439. s->error = "failed to connect to server";
  440. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s (%s)\n",lwip_strerr(err)));
  441. result = LWFTP_RESULT_ERR_CONNECT;
  442. } else {
  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. char *start_ftp_client(lwftp_session_t *s)
  635. {
  636. received_bytes_count = 0;
  637. s->data_sink = data_sink;
  638. //s->done_fn = ftp_retr_callback;
  639. lwftp_retr(s);
  640. // FTP session will continue with the connection callback
  641. //// wait for the client to connect to the server and check whether the file exists
  642. //while (s->error == NULL) {
  643. // vTaskDelay(200);
  644. //}
  645. return "1";
  646. }
  647. uint32_t get_ftp_progress()
  648. {
  649. return received_bytes_count * 100 / MAIN_FW_SIZE;
  650. }
  651. #endif // !BT6702_SERVICE
  652. #endif // HARDWARE_BT6711