host_file_io.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. //
  7. // Hot It Works
  8. // ************
  9. //
  10. // This module implements host file I/O protocol on top of apptrace module.
  11. // The protocol is enough simple. It sends command with arguments to the host and receives response from it.
  12. // Responses contains return values of respective file I/O API. This value is returned to the caller.
  13. // Commands has the following format:
  14. // * Header. See esp_apptrace_fcmd_hdr_t.
  15. // * Operation arguments. See file operation helper structures below.
  16. #include <string.h>
  17. #include "esp_app_trace.h"
  18. #if CONFIG_APPTRACE_ENABLE
  19. #include "esp_log.h"
  20. const static char *TAG = "esp_host_file_io";
  21. #define ESP_APPTRACE_FILE_CMD_FOPEN 0x0
  22. #define ESP_APPTRACE_FILE_CMD_FCLOSE 0x1
  23. #define ESP_APPTRACE_FILE_CMD_FWRITE 0x2
  24. #define ESP_APPTRACE_FILE_CMD_FREAD 0x3
  25. #define ESP_APPTRACE_FILE_CMD_FSEEK 0x4
  26. #define ESP_APPTRACE_FILE_CMD_FTELL 0x5
  27. #define ESP_APPTRACE_FILE_CMD_STOP 0x6 // indicates that there is no files to transfer
  28. #define ESP_APPTRACE_FILE_CMD_FEOF 0x7
  29. /** File operation header */
  30. typedef struct {
  31. uint8_t cmd; ///< Command ID
  32. } esp_apptrace_fcmd_hdr_t;
  33. /** Helper structure for fopen */
  34. typedef struct {
  35. const char *path;
  36. uint16_t path_len;
  37. const char *mode;
  38. uint16_t mode_len;
  39. } esp_apptrace_fopen_args_t;
  40. /** Helper structure for fclose */
  41. typedef struct {
  42. void *file;
  43. } esp_apptrace_fclose_args_t;
  44. /** Helper structure for fwrite */
  45. typedef struct {
  46. void * buf;
  47. size_t size;
  48. void * file;
  49. } esp_apptrace_fwrite_args_t;
  50. /** Helper structure for fread */
  51. typedef struct {
  52. size_t size;
  53. void * file;
  54. } esp_apptrace_fread_args_t;
  55. /** Helper structure for fseek */
  56. typedef struct {
  57. long offset;
  58. int whence;
  59. void * file;
  60. } esp_apptrace_fseek_args_t;
  61. /** Helper structure for feof */
  62. typedef struct {
  63. void *file;
  64. } esp_apptrace_feof_args_t;
  65. /** Helper structure for ftell */
  66. typedef struct {
  67. void *file;
  68. } esp_apptrace_ftell_args_t;
  69. static esp_err_t esp_apptrace_file_cmd_send(esp_apptrace_dest_t dest, uint8_t cmd, void (*prep_args)(uint8_t *, void *), void *args, uint32_t args_len)
  70. {
  71. esp_err_t ret;
  72. esp_apptrace_fcmd_hdr_t *hdr;
  73. ESP_EARLY_LOGV(TAG, "%s %d", __func__, cmd);
  74. uint8_t *ptr = esp_apptrace_buffer_get(dest, sizeof(*hdr) + args_len, ESP_APPTRACE_TMO_INFINITE); //TODO: finite tmo
  75. if (ptr == NULL) {
  76. return ESP_ERR_NO_MEM;
  77. }
  78. hdr = (esp_apptrace_fcmd_hdr_t *)ptr;
  79. hdr->cmd = cmd;
  80. if (prep_args) {
  81. prep_args(ptr + sizeof(hdr->cmd), args);
  82. }
  83. // now indicate that this buffer is ready to be sent off to host
  84. ret = esp_apptrace_buffer_put(dest, ptr, ESP_APPTRACE_TMO_INFINITE);//TODO: finite tmo
  85. if (ret != ESP_OK) {
  86. ESP_EARLY_LOGE(TAG, "Failed to put apptrace buffer (%d)!", ret);
  87. return ret;
  88. }
  89. ret = esp_apptrace_flush(dest, ESP_APPTRACE_TMO_INFINITE);//TODO: finite tmo
  90. if (ret != ESP_OK) {
  91. ESP_EARLY_LOGE(TAG, "Failed to flush apptrace buffer (%d)!", ret);
  92. return ret;
  93. }
  94. return ESP_OK;
  95. }
  96. static esp_err_t esp_apptrace_file_rsp_recv(esp_apptrace_dest_t dest, uint8_t *buf, uint32_t buf_len)
  97. {
  98. uint32_t tot_rd = 0;
  99. while (tot_rd < buf_len) {
  100. uint32_t rd_size = buf_len - tot_rd;
  101. esp_err_t ret = esp_apptrace_read(dest, buf + tot_rd, &rd_size, ESP_APPTRACE_TMO_INFINITE); //TODO: finite tmo
  102. if (ret != ESP_OK) {
  103. ESP_EARLY_LOGE(TAG, "Failed to read (%d)!", ret);
  104. return ret;
  105. }
  106. ESP_EARLY_LOGV(TAG, "%s read %d bytes", __FUNCTION__, rd_size);
  107. tot_rd += rd_size;
  108. }
  109. return ESP_OK;
  110. }
  111. static void esp_apptrace_fopen_args_prepare(uint8_t *buf, void *priv)
  112. {
  113. esp_apptrace_fopen_args_t *args = priv;
  114. memcpy(buf, args->path, args->path_len);
  115. memcpy(buf + args->path_len, args->mode, args->mode_len);
  116. }
  117. void *esp_apptrace_fopen(esp_apptrace_dest_t dest, const char *path, const char *mode)
  118. {
  119. esp_apptrace_fopen_args_t cmd_args;
  120. ESP_EARLY_LOGV(TAG, "esp_apptrace_fopen '%s' '%s'", path, mode);
  121. if (path == NULL || mode == NULL) {
  122. return 0;
  123. }
  124. cmd_args.path = path;
  125. cmd_args.path_len = strlen(path) + 1;
  126. cmd_args.mode = mode;
  127. cmd_args.mode_len = strlen(mode) + 1;
  128. esp_err_t ret = esp_apptrace_file_cmd_send(dest, ESP_APPTRACE_FILE_CMD_FOPEN, esp_apptrace_fopen_args_prepare,
  129. &cmd_args, cmd_args.path_len+cmd_args.mode_len);
  130. if (ret != ESP_OK) {
  131. ESP_EARLY_LOGE(TAG, "Failed to send file cmd (%d)!", ret);
  132. return NULL;
  133. }
  134. // now read the answer
  135. void *resp;
  136. ret = esp_apptrace_file_rsp_recv(dest, (uint8_t *)&resp, sizeof(resp));
  137. if (ret != ESP_OK) {
  138. ESP_EARLY_LOGE(TAG, "Failed to read response (%d)!", ret);
  139. return NULL;
  140. }
  141. return resp;
  142. }
  143. static void esp_apptrace_fclose_args_prepare(uint8_t *buf, void *priv)
  144. {
  145. esp_apptrace_fclose_args_t *args = priv;
  146. memcpy(buf, &args->file, sizeof(args->file));
  147. }
  148. int esp_apptrace_fclose(esp_apptrace_dest_t dest, void *stream)
  149. {
  150. esp_apptrace_fclose_args_t cmd_args;
  151. cmd_args.file = stream;
  152. esp_err_t ret = esp_apptrace_file_cmd_send(dest, ESP_APPTRACE_FILE_CMD_FCLOSE, esp_apptrace_fclose_args_prepare,
  153. &cmd_args, sizeof(cmd_args));
  154. if (ret != ESP_OK) {
  155. ESP_EARLY_LOGE(TAG, "Failed to send file cmd (%d)!", ret);
  156. return EOF;
  157. }
  158. // now read the answer
  159. int resp;
  160. ret = esp_apptrace_file_rsp_recv(dest, (uint8_t *)&resp, sizeof(resp));
  161. if (ret != ESP_OK) {
  162. ESP_EARLY_LOGE(TAG, "Failed to read response (%d)!", ret);
  163. return EOF;
  164. }
  165. return resp;
  166. }
  167. static void esp_apptrace_fwrite_args_prepare(uint8_t *buf, void *priv)
  168. {
  169. esp_apptrace_fwrite_args_t *args = priv;
  170. memcpy(buf, &args->file, sizeof(args->file));
  171. memcpy(buf + sizeof(args->file), args->buf, args->size);
  172. }
  173. size_t esp_apptrace_fwrite(esp_apptrace_dest_t dest, const void *ptr, size_t size, size_t nmemb, void *stream)
  174. {
  175. esp_apptrace_fwrite_args_t cmd_args;
  176. ESP_EARLY_LOGV(TAG, "esp_apptrace_fwrite f %p l %d", stream, size*nmemb);
  177. if (ptr == NULL) {
  178. return 0;
  179. }
  180. cmd_args.buf = (void *)ptr;
  181. cmd_args.size = size * nmemb;
  182. cmd_args.file = stream;
  183. esp_err_t ret = esp_apptrace_file_cmd_send(dest, ESP_APPTRACE_FILE_CMD_FWRITE, esp_apptrace_fwrite_args_prepare,
  184. &cmd_args, sizeof(cmd_args.file)+cmd_args.size);
  185. if (ret != ESP_OK) {
  186. ESP_EARLY_LOGE(TAG, "Failed to send file cmd (%d)!", ret);
  187. return 0;
  188. }
  189. // now read the answer
  190. size_t resp;
  191. ret = esp_apptrace_file_rsp_recv(dest, (uint8_t *)&resp, sizeof(resp));
  192. if (ret != ESP_OK) {
  193. ESP_EARLY_LOGE(TAG, "Failed to read response (%d)!", ret);
  194. return 0;
  195. }
  196. /* OpenOCD writes it like that:
  197. * fwrite(buf, size, 1, file);
  198. * So, if 1 was returned that means fwrite succeed
  199. */
  200. return resp == 1 ? nmemb : 0;
  201. }
  202. static void esp_apptrace_fread_args_prepare(uint8_t *buf, void *priv)
  203. {
  204. esp_apptrace_fread_args_t *args = priv;
  205. memcpy(buf, &args->file, sizeof(args->file));
  206. memcpy(buf + sizeof(args->file), &args->size, sizeof(args->size));
  207. }
  208. size_t esp_apptrace_fread(esp_apptrace_dest_t dest, void *ptr, size_t size, size_t nmemb, void *stream)
  209. {
  210. esp_apptrace_fread_args_t cmd_args;
  211. ESP_EARLY_LOGV(TAG, "esp_apptrace_fread f %p l %d", stream, size*nmemb);
  212. if (ptr == NULL) {
  213. return 0;
  214. }
  215. cmd_args.size = size * nmemb;
  216. cmd_args.file = stream;
  217. esp_err_t ret = esp_apptrace_file_cmd_send(dest, ESP_APPTRACE_FILE_CMD_FREAD, esp_apptrace_fread_args_prepare,
  218. &cmd_args, sizeof(cmd_args));
  219. if (ret != ESP_OK) {
  220. ESP_EARLY_LOGE(TAG, "Failed to send file cmd (%d)!", ret);
  221. return 0;
  222. }
  223. // now read the answer
  224. size_t resp;
  225. ret = esp_apptrace_file_rsp_recv(dest, (uint8_t *)&resp, sizeof(resp));
  226. if (ret != ESP_OK) {
  227. ESP_EARLY_LOGE(TAG, "Failed to read response (%d)!", ret);
  228. return 0;
  229. }
  230. if (resp == 0) {
  231. return 0;
  232. }
  233. ret = esp_apptrace_file_rsp_recv(dest, ptr, resp);
  234. if (ret != ESP_OK) {
  235. ESP_EARLY_LOGE(TAG, "Failed to read file data (%d)!", ret);
  236. return 0;
  237. }
  238. /* OpenOCD reads it like that:
  239. * fread(buf, 1 ,size, file);
  240. * So, total read bytes count returns
  241. */
  242. return resp/size; // return the number of items read
  243. }
  244. static void esp_apptrace_fseek_args_prepare(uint8_t *buf, void *priv)
  245. {
  246. esp_apptrace_fseek_args_t *args = priv;
  247. memcpy(buf, &args->file, sizeof(args->file));
  248. memcpy(buf + sizeof(args->file), &args->offset, sizeof(args->offset));
  249. memcpy(buf + sizeof(args->file) + sizeof(args->offset), &args->whence, sizeof(args->whence));
  250. }
  251. int esp_apptrace_fseek(esp_apptrace_dest_t dest, void *stream, long offset, int whence)
  252. {
  253. esp_apptrace_fseek_args_t cmd_args;
  254. ESP_EARLY_LOGV(TAG, "esp_apptrace_fseek f %p o 0x%lx w %d", stream, offset, whence);
  255. cmd_args.file = stream;
  256. cmd_args.offset = offset;
  257. cmd_args.whence = whence;
  258. esp_err_t ret = esp_apptrace_file_cmd_send(dest, ESP_APPTRACE_FILE_CMD_FSEEK, esp_apptrace_fseek_args_prepare,
  259. &cmd_args, sizeof(cmd_args));
  260. if (ret != ESP_OK) {
  261. ESP_EARLY_LOGE(TAG, "Failed to send file cmd (%d)!", ret);
  262. return -1;
  263. }
  264. // now read the answer
  265. int resp;
  266. ret = esp_apptrace_file_rsp_recv(dest, (uint8_t *)&resp, sizeof(resp));
  267. if (ret != ESP_OK) {
  268. ESP_EARLY_LOGE(TAG, "Failed to read response (%d)!", ret);
  269. return -1;
  270. }
  271. return resp;
  272. }
  273. static void esp_apptrace_ftell_args_prepare(uint8_t *buf, void *priv)
  274. {
  275. esp_apptrace_ftell_args_t *args = priv;
  276. memcpy(buf, &args->file, sizeof(args->file));
  277. }
  278. int esp_apptrace_ftell(esp_apptrace_dest_t dest, void *stream)
  279. {
  280. esp_apptrace_ftell_args_t cmd_args;
  281. cmd_args.file = stream;
  282. esp_err_t ret = esp_apptrace_file_cmd_send(dest, ESP_APPTRACE_FILE_CMD_FTELL, esp_apptrace_ftell_args_prepare,
  283. &cmd_args, sizeof(cmd_args));
  284. if (ret != ESP_OK) {
  285. ESP_EARLY_LOGE(TAG, "Failed to send file cmd (%d)!", ret);
  286. return -1;
  287. }
  288. // now read the answer
  289. int resp;
  290. ret = esp_apptrace_file_rsp_recv(dest, (uint8_t *)&resp, sizeof(resp));
  291. if (ret != ESP_OK) {
  292. ESP_EARLY_LOGE(TAG, "Failed to read response (%d)!", ret);
  293. return -1;
  294. }
  295. return resp;
  296. }
  297. int esp_apptrace_fstop(esp_apptrace_dest_t dest)
  298. {
  299. ESP_EARLY_LOGV(TAG, "%s", __func__);
  300. esp_err_t ret = esp_apptrace_file_cmd_send(dest, ESP_APPTRACE_FILE_CMD_STOP, NULL, NULL, 0);
  301. if (ret != ESP_OK) {
  302. ESP_EARLY_LOGE(TAG, "Failed to send files transfer stop cmd (%d)!", ret);
  303. }
  304. return ret;
  305. }
  306. static void esp_apptrace_feof_args_prepare(uint8_t *buf, void *priv)
  307. {
  308. esp_apptrace_feof_args_t *args = priv;
  309. memcpy(buf, &args->file, sizeof(args->file));
  310. }
  311. int esp_apptrace_feof(esp_apptrace_dest_t dest, void *stream)
  312. {
  313. esp_apptrace_feof_args_t cmd_args;
  314. cmd_args.file = stream;
  315. esp_err_t ret = esp_apptrace_file_cmd_send(dest, ESP_APPTRACE_FILE_CMD_FEOF, esp_apptrace_feof_args_prepare,
  316. &cmd_args, sizeof(cmd_args));
  317. if (ret != ESP_OK) {
  318. ESP_EARLY_LOGE(TAG, "Failed to send file cmd (%d)!", ret);
  319. return EOF;
  320. }
  321. // now read the answer
  322. int resp;
  323. ret = esp_apptrace_file_rsp_recv(dest, (uint8_t *)&resp, sizeof(resp));
  324. if (ret != ESP_OK) {
  325. ESP_EARLY_LOGE(TAG, "Failed to read response (%d)!", ret);
  326. return EOF;
  327. }
  328. return resp;
  329. }
  330. #endif