ftp.c 21 KB

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