aem_adapter_flash.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #include <zephyr.h>
  2. #include <stdio.h>
  3. #include <errno.h>
  4. #include <fs/fs.h>
  5. #include "aem_platform.h"
  6. #include "aem_log.h"
  7. #include "aem_adapter_err_if.h"
  8. #include "aem_adapter_flash.h"
  9. #include "sys_wakelock.h"
  10. #define APP_DATA_PARTITION_NAME CONFIG_APP_FAT_DISK
  11. static ssize_t aem_fs_write(const char *pname, uint32_t offset, const uint8_t *buff, size_t size)
  12. {
  13. char file_name[50] = {0};
  14. struct fs_file_t zfp = {0};
  15. ssize_t ret_val = 0;
  16. snprintf(file_name, sizeof(file_name), "%s/%s", APP_DATA_PARTITION_NAME, pname);
  17. fs_mode_t flags = FS_O_RDWR | FS_O_CREATE;
  18. fs_file_t_init(&zfp);
  19. if (fs_open(&zfp, file_name, flags) != 0)
  20. {
  21. AEM_LOG_E("write open file (%s) faile\n", file_name);
  22. return -ENOENT;
  23. }
  24. AEM_LOG_I("write open file (%s) ok\n", file_name);
  25. fs_seek(&zfp, 0, FS_SEEK_END);
  26. off_t file_size = fs_tell(&zfp);
  27. if (offset > file_size)
  28. {
  29. uint32_t fill_size = offset - file_size;
  30. uint32_t fill_offset = 0;
  31. uint16_t fill_size_once = 4096; // 每次写4K
  32. AEM_LOG_I("file fill data: %d, offset: %d, file_size: %d\n", fill_size, offset, (int)file_size);
  33. char *pfill = aem_malloc(fill_size_once);
  34. if (pfill == NULL)
  35. {
  36. AEM_LOG_E("malloc fill mem\n");
  37. ret_val = -ENOMEM;
  38. goto fs_write_end;
  39. }
  40. aem_memset(pfill, 0xFF, fill_size_once);
  41. // 填充空数据
  42. while (fill_size > fill_offset)
  43. {
  44. if ((fill_size - fill_offset) < fill_size_once)
  45. {
  46. fill_size_once = fill_size - fill_offset;
  47. }
  48. ssize_t temp = fs_write(&zfp, pfill, fill_size_once);
  49. if (temp != fill_size_once)
  50. {
  51. AEM_LOG_E("temp = %d,file_size_once = %d fs_write fill data\n", temp, fill_size_once);
  52. ret_val = -EAGAIN;
  53. aem_free(pfill);
  54. goto fs_write_end;
  55. }
  56. fill_offset += fill_size_once;
  57. }
  58. aem_free(pfill);
  59. }
  60. if (fs_seek(&zfp, offset, FS_SEEK_SET) < 0)
  61. {
  62. AEM_LOG_E("open fs_seek, offset: %d\n", offset);
  63. ret_val = -ESPIPE;
  64. goto fs_write_end;
  65. }
  66. ret_val = fs_write(&zfp, buff, size);
  67. if (ret_val != size)
  68. {
  69. AEM_LOG_E("fs_write, size: %d, ret_val: %d\n", size, ret_val);
  70. ret_val = -EAGAIN;
  71. goto fs_write_end;
  72. }
  73. fs_seek(&zfp, 0, FS_SEEK_END);
  74. off_t new_file_size = fs_tell(&zfp);
  75. if (file_size < new_file_size)
  76. {
  77. AEM_LOG_I("fs_write, file_size : %ld, new_file_size: %ld\n", file_size, new_file_size);
  78. }
  79. fs_write_end:
  80. AEM_LOG_I("fs_write end\n");
  81. fs_close(&zfp);
  82. AEM_LOG_I("fs_write ret\n");
  83. return ret_val;
  84. }
  85. uint32_t flash_write(const char *name, uint32_t offset, void *data, uint32_t size)
  86. {
  87. if (NULL == name)
  88. {
  89. return AEM_ADAPTER_ERR_PARAM_NULL;
  90. }
  91. sys_wake_lock(PARTIAL_WAKE_LOCK);
  92. ssize_t w_size = aem_fs_write(name, offset, data, size);
  93. sys_wake_unlock(PARTIAL_WAKE_LOCK);
  94. if (w_size >= 0)
  95. {
  96. return AEM_ADAPTER_OK;
  97. }
  98. return AEM_ADAPTER_ERR_INVALID_DATA;
  99. }
  100. static size_t aem_fs_read(const char *pname, uint32_t offset, uint8_t *buff, size_t size)
  101. {
  102. char file_name[50] = {0};
  103. struct fs_file_t fd = {0};
  104. ssize_t ret_val = 0;
  105. snprintf(file_name, sizeof(file_name), "%s/%s", APP_DATA_PARTITION_NAME, pname);
  106. fs_mode_t flags = FS_O_RDWR;
  107. fs_file_t_init(&fd);
  108. if (fs_open(&fd, file_name, flags) != 0)
  109. {
  110. AEM_LOG_E("read open file (%s) faile\n", file_name);
  111. return -ENOENT;
  112. }
  113. AEM_LOG_I("read open file (%s) ok\n", file_name);
  114. fs_seek(&fd, 0, FS_SEEK_END);
  115. off_t file_size = fs_tell(&fd);
  116. if (offset > file_size)
  117. {
  118. AEM_LOG_E("read offset is big then file\n");
  119. ret_val = -ESPIPE;
  120. goto fs_read_end;
  121. }
  122. if (fs_seek(&fd, offset, FS_SEEK_SET) < 0)
  123. {
  124. AEM_LOG_E("read fs_seek err, offset: %d\n", offset);
  125. ret_val = -ESPIPE;
  126. goto fs_read_end;
  127. }
  128. ret_val = fs_read(&fd, buff, size);
  129. if (ret_val != size)
  130. {
  131. memset(buff, 0, size);
  132. AEM_LOG_E("fs_read, size: %d, ret_val: %d\n", size, ret_val);
  133. ret_val = -EAGAIN;
  134. goto fs_read_end;
  135. }
  136. fs_read_end:
  137. fs_close(&fd);
  138. return ret_val;
  139. }
  140. static int aem_fs_delete(const char *pname)
  141. {
  142. char file_name[50] = {0};
  143. struct fs_file_t fd = {0};
  144. int ret_val = 0;
  145. snprintf(file_name, sizeof(file_name), "%s/%s", APP_DATA_PARTITION_NAME, pname);
  146. fs_mode_t flags = FS_O_RDWR;
  147. AEM_LOG_I("delete file (%s) \n", file_name);
  148. ret_val = fs_unlink(file_name);
  149. if (ret_val != 0)
  150. {
  151. AEM_LOG_W("delete file (%s) fail ret(%d)\r\n", file_name, ret_val);
  152. }
  153. return ret_val;
  154. }
  155. uint32_t flash_read(const char *name, uint32_t offset, void *data, uint32_t size)
  156. {
  157. if (name == NULL)
  158. {
  159. return AEM_ADAPTER_ERR_PARAM_NULL;
  160. }
  161. sys_wake_lock(PARTIAL_WAKE_LOCK);
  162. ssize_t r_size = aem_fs_read(name, offset, data, size);
  163. sys_wake_unlock(PARTIAL_WAKE_LOCK);
  164. if (r_size == -EAGAIN || r_size >= 0)
  165. {
  166. return AEM_ADAPTER_OK;
  167. }
  168. return AEM_ADAPTER_ERR_INVALID_DATA;
  169. }
  170. static uint32_t flash_erase(const char *name)
  171. {
  172. if (name == NULL)
  173. {
  174. return AEM_ADAPTER_ERR_PARAM_NULL;
  175. }
  176. sys_wake_lock(PARTIAL_WAKE_LOCK);
  177. int ret = aem_fs_delete(name);
  178. sys_wake_unlock(PARTIAL_WAKE_LOCK);
  179. if (ret != 0)
  180. {
  181. return AEM_ADAPTER_ERR_INTERNAL;
  182. }
  183. return AEM_ADAPTER_OK;
  184. }
  185. static const aem_flash_ops_t s_flash_ops = {
  186. .flash_read = flash_read,
  187. .flash_write = flash_write,
  188. .flash_erase = flash_erase,
  189. };
  190. const aem_flash_ops_t *aem_get_flash_ops(void)
  191. {
  192. return &s_flash_ops;
  193. }