ftp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  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 || s->control_pcb) {
  517. s->error = "previous connection is incomplete";
  518. return ERR_ARG;
  519. }
  520. if (!s->settings->remote_path) {
  521. s->error = "empty remote path";
  522. return ERR_ARG;
  523. }
  524. if (!s->settings->user) {
  525. s->error = "empty user name";
  526. return ERR_ARG;
  527. }
  528. if (!s->settings->pass) {
  529. s->error = "empty password";
  530. return ERR_ARG;
  531. }
  532. if (s->data_pcb) {
  533. lwftp_pcb_close(s->data_pcb);
  534. }
  535. // Get sessions pcb
  536. s->control_pcb = tcp_new();
  537. if (!s->control_pcb) {
  538. s->error = "cannot alloc control_pcb (low memory?)";
  539. error = ERR_MEM;
  540. goto exit;
  541. }
  542. s->data_pcb = tcp_new();
  543. if (!s->data_pcb) {
  544. s->error = "cannot alloc data_pcb (low memory?)";
  545. error = ERR_MEM;
  546. goto close_pcb;
  547. }
  548. // Open control session
  549. tcp_arg(s->control_pcb, s);
  550. tcp_err(s->control_pcb, lwftp_control_err);
  551. tcp_recv(s->control_pcb, lwftp_control_recv);
  552. tcp_sent(s->control_pcb, lwftp_control_sent);
  553. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  554. if ( error == ERR_OK ) goto exit;
  555. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  556. s->error = "cannot connet control_pcb";
  557. close_pcb:
  558. // Release pcbs in case of failure
  559. lwftp_control_close(s, -1);
  560. exit:
  561. return error;
  562. }
  563. //static void ftp_retr_callback(void *arg, int result)
  564. //{
  565. // lwftp_session_t *s = (lwftp_session_t *)arg;
  566. //
  567. // if (result != LWFTP_RESULT_OK) {
  568. // //LOG_ERROR("retr failed (%d)", result);
  569. // return lwftp_close(s);
  570. // }
  571. // lwftp_close(s);
  572. //}
  573. static bool validate_spif_firmware(uint32_t fw_len)
  574. {
  575. const uint32_t spif_firmware_offset = SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET;
  576. // check the firmware revision id
  577. char rev[HW_REV_LEN];
  578. spi_flash_read(spif_firmware_offset + HW_REV_OFFSET, &rev, sizeof(rev), 0);
  579. if (strncmp(rev, HW_REV, sizeof(rev))) {
  580. printf("HW revision mismatch: expected %s, found %s\r\n", HW_REV, rev);
  581. return false;
  582. }
  583. // check if the firmware length is correct
  584. if (fw_len != MAIN_FW_SIZE) {
  585. printf("ftp: firmware size mismatch: expected %d, got %ld\r\n", MAIN_FW_SIZE, fw_len);
  586. return false;
  587. }
  588. uint32_t expected_crc;
  589. const uint32_t crc_offset = USER_FLASH_CRC_ADDRESS - USER_FLASH_FIRST_PAGE_ADDRESS;
  590. spi_flash_read(spif_firmware_offset + crc_offset, &expected_crc, sizeof(expected_crc), 0);
  591. // calculate CRC
  592. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
  593. CRC->CR = 1;
  594. for (uint32_t offset = 0; offset < crc_offset; offset += 4) {
  595. uint32_t data;
  596. spi_flash_read(spif_firmware_offset + offset, &data, sizeof(data), 0);
  597. CRC->DR = data;
  598. }
  599. uint32_t calculated_crc = CRC->DR;
  600. if (expected_crc != calculated_crc) {
  601. printf("ftp: calculated CRC (%lx) doesn't match the expected CRC (%lx)!\r\n", calculated_crc, expected_crc);
  602. return false;
  603. }
  604. return true;
  605. }
  606. static void erase_spif_firmware()
  607. {
  608. printf("ftp: erasing SPI firmware partition...\r\n");
  609. for (int sec = 0; sec < FIRMWARE_UPDATE_SECTOR_COUNT; ++sec) {
  610. spi_flash_erase_sector(SPI_FLASH_SECTOR_SIZE * (FIRMWARE_UPDATE_SECTOR_OFFSET + sec), 0);
  611. }
  612. }
  613. static unsigned data_sink(void *arg, const char* ptr, unsigned len)
  614. {
  615. (void)arg;
  616. if (ptr && len) {
  617. // we received some data from the FTP server
  618. if (received_bytes_count == 0) {
  619. // we need to erase the flash before we can write it
  620. erase_spif_firmware();
  621. }
  622. if (received_bytes_count + len > MAIN_FW_SIZE) {
  623. printf("ftp: the firmware is too big! aborting the download\r\n");
  624. return 0;
  625. }
  626. spi_flash_write(SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET + received_bytes_count, ptr, len, 0);
  627. received_bytes_count += len;
  628. } else {
  629. printf("ftp: the firmware is downloaded, verifying...\r\n");
  630. uint32_t fw_size = received_bytes_count;
  631. // next time it comes, overwrite the existing SPI contents
  632. received_bytes_count = 0;
  633. bool good_firmware = validate_spif_firmware(fw_size);
  634. if (good_firmware) {
  635. printf("ftp: the firmware is valid, rebooting...\r\n");
  636. HTTP_StartResetTask(true);
  637. } else {
  638. // erase it so the bootloader won't try to verify it every time
  639. erase_spif_firmware();
  640. }
  641. }
  642. return len;
  643. }
  644. void start_ftp_client(lwftp_session_t *s)
  645. {
  646. received_bytes_count = 0;
  647. s->error = NULL;
  648. error_ptr = &s->error;
  649. s->data_sink = data_sink;
  650. //s->done_fn = ftp_retr_callback;
  651. lwftp_retr(s);
  652. // FTP session will continue with the connection callback
  653. }
  654. char *get_ftp_progress()
  655. {
  656. if (*error_ptr) {
  657. return *error_ptr;
  658. } else {
  659. unsigned progress = received_bytes_count * 100 / MAIN_FW_SIZE;
  660. static char progress_str_buf[4];
  661. snprintf(progress_str_buf, sizeof(progress_str_buf), "%u", progress);
  662. return progress_str_buf;
  663. }
  664. }
  665. #endif // !BT6702_SERVICE
  666. #endif // HARDWARE_BT6711