ftp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  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->error = "The server doesn't greet us";
  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->error = "Wrong user name";
  278. s->control_state = LWFTP_QUIT;
  279. }
  280. }
  281. break;
  282. case LWFTP_PASS_SENT:
  283. if (response>0) {
  284. if (response==230) {
  285. anonymous:
  286. lwftp_send_msg(s, PTRNLEN("TYPE I\n"));
  287. s->control_state = LWFTP_TYPE_SENT;
  288. } else {
  289. s->error = "Wrong password";
  290. s->control_state = LWFTP_QUIT;
  291. }
  292. }
  293. break;
  294. case LWFTP_TYPE_SENT:
  295. if (response>0) {
  296. if (response==200) {
  297. lwftp_send_msg(s, PTRNLEN("PASV\n"));
  298. s->control_state = LWFTP_PASV_SENT;
  299. } else {
  300. s->error = "The server doesn't support binary files";
  301. s->control_state = LWFTP_QUIT;
  302. }
  303. }
  304. break;
  305. case LWFTP_PASV_SENT:
  306. if (response>0) {
  307. if (response==227) {
  308. lwftp_data_open(s,p);
  309. switch (s->target_state) {
  310. case LWFTP_STOR_SENT:
  311. lwftp_send_msg(s, PTRNLEN("STOR "));
  312. break;
  313. case LWFTP_RETR_SENT:
  314. lwftp_send_msg(s, PTRNLEN("RETR "));
  315. break;
  316. default:
  317. //LOG_ERROR("Unexpected internal state");
  318. s->target_state = LWFTP_QUIT;
  319. }
  320. lwftp_send_msg(s, s->settings->remote_path, strlen(s->settings->remote_path));
  321. lwftp_send_msg(s, PTRNLEN("\n"));
  322. s->control_state = s->target_state;
  323. } else {
  324. s->error = "The server doesn't support PASV";
  325. s->control_state = LWFTP_QUIT;
  326. }
  327. }
  328. break;
  329. case LWFTP_RETR_SENT:
  330. if (response>0) {
  331. if (response==150) {
  332. s->control_state = LWFTP_XFERING;
  333. } else if (response==550) {
  334. s->control_state = LWFTP_QUIT;
  335. s->error = "Failed to open file";
  336. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s '%s'\n", error, s->settings->remote_path));
  337. }
  338. else {
  339. s->error = "The server doesn't start sending the file";
  340. s->control_state = LWFTP_QUIT;
  341. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 150, received %d\n",response));
  342. }
  343. }
  344. break;
  345. case LWFTP_STOR_SENT:
  346. if (response>0) {
  347. if (response==150) {
  348. s->control_state = LWFTP_XFERING;
  349. lwftp_data_sent(s,NULL,0);
  350. } else {
  351. s->control_state = LWFTP_QUIT;
  352. }
  353. }
  354. break;
  355. case LWFTP_XFERING:
  356. if (response>0) {
  357. if (response==226) {
  358. s->data_state = LWFTP_XFERING; // signal transfer OK
  359. } else {
  360. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 226, received %d\n",response));
  361. }
  362. // Quit anyway after any message received during STOR
  363. s->control_state = LWFTP_QUIT;
  364. }
  365. break;
  366. case LWFTP_QUIT_SENT:
  367. if (response>0) {
  368. if (response==221) {
  369. if (s->data_state == LWFTP_XFERING){ // check for transfer OK
  370. result = LWFTP_RESULT_OK;
  371. }
  372. } else {
  373. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:expected 221, received %d\n",response));
  374. }
  375. s->control_state = LWFTP_CLOSING;
  376. }
  377. break;
  378. default:
  379. LWIP_DEBUGF(LWFTP_SEVERE, ("lwftp:unhandled state (%d)\n",s->control_state));
  380. }
  381. // Free receiving pbuf if any
  382. if (p) {
  383. pbuf_free(p);
  384. }
  385. // Handle second step in state machine
  386. switch ( s->control_state ) {
  387. case LWFTP_QUIT:
  388. lwftp_send_msg(s, PTRNLEN("QUIT\n"));
  389. s->control_state = LWFTP_QUIT_SENT;
  390. break;
  391. case LWFTP_CLOSING:
  392. // this function frees s, no use of s is allowed after
  393. return lwftp_control_close(s, result);
  394. default:;
  395. }
  396. }
  397. /** Handle control connection incoming data
  398. * @param pointer to lwftp session data
  399. * @param pointer to PCB
  400. * @param pointer to incoming pbuf
  401. * @param state of incoming process
  402. */
  403. static err_t lwftp_control_recv(void *arg, struct tcp_pcb *tpcb, struct pbuf *p, err_t err)
  404. {
  405. lwftp_session_t *s = (lwftp_session_t*)arg;
  406. if ( err == ERR_OK ) {
  407. if (p) {
  408. tcp_recved(tpcb, p->tot_len);
  409. lwftp_control_process(s, tpcb, p);
  410. } else {
  411. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  412. lwftp_control_close(s, LWFTP_RESULT_ERR_CLOSED);
  413. }
  414. } else {
  415. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:failed to receive (%s)\n",lwip_strerr(err)));
  416. lwftp_control_close(s, LWFTP_RESULT_ERR_UNKNOWN);
  417. }
  418. return err;
  419. }
  420. /** Handle control connection acknowledge of sent data
  421. * @param pointer to lwftp session data
  422. * @param pointer to PCB
  423. * @param number of bytes sent
  424. */
  425. static err_t lwftp_control_sent(void *arg, struct tcp_pcb *tpcb, u16_t len)
  426. {
  427. (void)arg;
  428. (void)tpcb;
  429. (void)len;
  430. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:successfully sent %d bytes\n",len));
  431. return ERR_OK;
  432. }
  433. /** Handle control connection error
  434. * @param pointer to lwftp session data
  435. * @param state of connection
  436. */
  437. static void lwftp_control_err(void *arg, err_t err)
  438. {
  439. LWIP_UNUSED_ARG(err);
  440. if (arg != NULL) {
  441. lwftp_session_t *s = (lwftp_session_t*)arg;
  442. int result;
  443. if( s->control_state == LWFTP_CLOSED ) {
  444. s->error = "failed to connect to server";
  445. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp: %s (%s)\n",lwip_strerr(err)));
  446. result = LWFTP_RESULT_ERR_CONNECT;
  447. } else {
  448. s->error = "connection closed by remote host";
  449. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:connection closed by remote host\n"));
  450. result = LWFTP_RESULT_ERR_CLOSED;
  451. }
  452. s->control_pcb = NULL; // No need to de-allocate PCB
  453. lwftp_control_close(s, result);
  454. }
  455. }
  456. /** Process newly connected PCB
  457. * @param pointer to lwftp session data
  458. * @param pointer to PCB
  459. * @param state of connection
  460. */
  461. static err_t lwftp_control_connected(void *arg, struct tcp_pcb *tpcb, err_t err)
  462. {
  463. (void)tpcb;
  464. lwftp_session_t *s = (lwftp_session_t*)arg;
  465. if ( err == ERR_OK ) {
  466. LWIP_DEBUGF(LWFTP_TRACE, ("lwftp:connected to server\n"));
  467. s->control_state = LWFTP_CONNECTED;
  468. } else {
  469. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:err in control_connected (%s)\n",lwip_strerr(err)));
  470. }
  471. return err;
  472. }
  473. /** Store data to a remote file
  474. * @param Session structure
  475. */
  476. err_t lwftp_store(lwftp_session_t *s)
  477. {
  478. err_t error;
  479. // Check user supplied data
  480. if ( (s->control_state!=LWFTP_CLOSED) ||
  481. !s->settings->remote_path ||
  482. s->control_pcb ||
  483. s->data_pcb ||
  484. !s->settings->user ||
  485. !s->settings->pass )
  486. {
  487. LWIP_DEBUGF(LWFTP_WARNING, ("lwftp:invalid session data\n"));
  488. return ERR_ARG;
  489. }
  490. // Get sessions pcb
  491. s->control_pcb = tcp_new();
  492. if (!s->control_pcb) {
  493. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc control_pcb (low memory?)\n"));
  494. error = ERR_MEM;
  495. goto exit;
  496. }
  497. s->data_pcb = tcp_new();
  498. if (!s->data_pcb) {
  499. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot alloc data_pcb (low memory?)\n"));
  500. error = ERR_MEM;
  501. goto close_pcb;
  502. }
  503. // Open control session
  504. tcp_arg(s->control_pcb, s);
  505. tcp_err(s->control_pcb, lwftp_control_err);
  506. tcp_recv(s->control_pcb, lwftp_control_recv);
  507. tcp_sent(s->control_pcb, lwftp_control_sent);
  508. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  509. if ( error == ERR_OK ) goto exit;
  510. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  511. close_pcb:
  512. // Release pcbs in case of failure
  513. lwftp_control_close(s, -1);
  514. exit:
  515. return error;
  516. }
  517. err_t lwftp_retr(lwftp_session_t *s)
  518. {
  519. s->target_state = LWFTP_RETR_SENT;
  520. err_t error;
  521. // Check user supplied data
  522. if (!s->settings->remote_path) {
  523. s->error = "empty remote path";
  524. return ERR_ARG;
  525. }
  526. if (!s->settings->user) {
  527. s->error = "empty user name";
  528. return ERR_ARG;
  529. }
  530. if (!s->settings->pass) {
  531. s->error = "empty password";
  532. return ERR_ARG;
  533. }
  534. if (s->control_state != LWFTP_CLOSED || s->control_pcb) {
  535. // previous connection is incomplete
  536. lwftp_control_close(s, -1);
  537. }
  538. if (s->data_pcb) {
  539. lwftp_pcb_close(s->data_pcb);
  540. }
  541. // Get sessions pcb
  542. s->control_pcb = tcp_new();
  543. if (!s->control_pcb) {
  544. s->error = "cannot alloc control_pcb (low memory?)";
  545. error = ERR_MEM;
  546. goto exit;
  547. }
  548. s->data_pcb = tcp_new();
  549. if (!s->data_pcb) {
  550. s->error = "cannot alloc data_pcb (low memory?)";
  551. error = ERR_MEM;
  552. goto close_pcb;
  553. }
  554. // Open control session
  555. tcp_arg(s->control_pcb, s);
  556. tcp_err(s->control_pcb, lwftp_control_err);
  557. tcp_recv(s->control_pcb, lwftp_control_recv);
  558. tcp_sent(s->control_pcb, lwftp_control_sent);
  559. error = tcp_connect(s->control_pcb, &s->settings->server_ip, s->settings->server_port, lwftp_control_connected);
  560. if ( error == ERR_OK ) goto exit;
  561. LWIP_DEBUGF(LWFTP_SERIOUS, ("lwftp:cannot connect control_pcb (%s)\n", lwip_strerr(error)));
  562. s->error = "cannot connet control_pcb";
  563. close_pcb:
  564. // Release pcbs in case of failure
  565. lwftp_control_close(s, -1);
  566. exit:
  567. return error;
  568. }
  569. //static void ftp_retr_callback(void *arg, int result)
  570. //{
  571. // lwftp_session_t *s = (lwftp_session_t *)arg;
  572. //
  573. // if (result != LWFTP_RESULT_OK) {
  574. // //LOG_ERROR("retr failed (%d)", result);
  575. // return lwftp_close(s);
  576. // }
  577. // lwftp_close(s);
  578. //}
  579. static bool validate_spif_firmware(uint32_t fw_len)
  580. {
  581. const uint32_t spif_firmware_offset = SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET;
  582. // check the firmware revision id
  583. char rev[HW_REV_LEN];
  584. spi_flash_read(spif_firmware_offset + HW_REV_OFFSET, &rev, sizeof(rev), 0);
  585. if (strncmp(rev, HW_REV, sizeof(rev))) {
  586. printf("HW revision mismatch: expected %s, found %s\r\n", HW_REV, rev);
  587. return false;
  588. }
  589. // check if the firmware length is correct
  590. if (fw_len != MAIN_FW_SIZE) {
  591. printf("ftp: firmware size mismatch: expected %d, got %ld\r\n", MAIN_FW_SIZE, fw_len);
  592. return false;
  593. }
  594. uint32_t expected_crc;
  595. const uint32_t crc_offset = USER_FLASH_CRC_ADDRESS - USER_FLASH_FIRST_PAGE_ADDRESS;
  596. spi_flash_read(spif_firmware_offset + crc_offset, &expected_crc, sizeof(expected_crc), 0);
  597. // calculate CRC
  598. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_CRC, ENABLE);
  599. CRC->CR = 1;
  600. for (uint32_t offset = 0; offset < crc_offset; offset += 4) {
  601. uint32_t data;
  602. spi_flash_read(spif_firmware_offset + offset, &data, sizeof(data), 0);
  603. CRC->DR = data;
  604. }
  605. uint32_t calculated_crc = CRC->DR;
  606. if (expected_crc != calculated_crc) {
  607. printf("ftp: calculated CRC (%lx) doesn't match the expected CRC (%lx)!\r\n", calculated_crc, expected_crc);
  608. return false;
  609. }
  610. return true;
  611. }
  612. static void erase_spif_firmware()
  613. {
  614. printf("ftp: erasing SPI firmware partition...\r\n");
  615. for (int sec = 0; sec < FIRMWARE_UPDATE_SECTOR_COUNT; ++sec) {
  616. spi_flash_erase_sector(SPI_FLASH_SECTOR_SIZE * (FIRMWARE_UPDATE_SECTOR_OFFSET + sec), 0);
  617. }
  618. }
  619. static unsigned data_sink(void *arg, const char* ptr, unsigned len)
  620. {
  621. (void)arg;
  622. if (ptr && len) {
  623. // we received some data from the FTP server
  624. if (received_bytes_count == 0) {
  625. // we need to erase the flash before we can write it
  626. erase_spif_firmware();
  627. }
  628. if (received_bytes_count + len > MAIN_FW_SIZE) {
  629. printf("ftp: the firmware is too big! aborting the download\r\n");
  630. return 0;
  631. }
  632. spi_flash_write(SPI_FLASH_SECTOR_SIZE * FIRMWARE_UPDATE_SECTOR_OFFSET + received_bytes_count, ptr, len, 0);
  633. received_bytes_count += len;
  634. } else {
  635. printf("ftp: the firmware is downloaded, verifying...\r\n");
  636. uint32_t fw_size = received_bytes_count;
  637. bool good_firmware = validate_spif_firmware(fw_size);
  638. if (good_firmware) {
  639. printf("ftp: the firmware is valid, rebooting...\r\n");
  640. HTTP_StartResetTask(true);
  641. } else {
  642. // erase it so the bootloader won't try to verify it every time
  643. erase_spif_firmware();
  644. }
  645. }
  646. return len;
  647. }
  648. void start_ftp_client(lwftp_session_t *s)
  649. {
  650. received_bytes_count = 0;
  651. s->error = NULL;
  652. error_ptr = &s->error;
  653. s->data_sink = data_sink;
  654. //s->done_fn = ftp_retr_callback;
  655. lwftp_retr(s);
  656. // FTP session will continue with the connection callback
  657. }
  658. char *get_ftp_progress()
  659. {
  660. if (*error_ptr) {
  661. return *error_ptr;
  662. } else {
  663. unsigned progress = received_bytes_count * 100 / MAIN_FW_SIZE;
  664. static char progress_str_buf[4];
  665. snprintf(progress_str_buf, sizeof(progress_str_buf), "%u", progress);
  666. return progress_str_buf;
  667. }
  668. }
  669. #endif // !BT6702_SERVICE
  670. #endif // HARDWARE_BT6711