rpc_storage.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. #include "flipper.pb.h"
  2. #include "furi/common_defines.h"
  3. #include "furi/memmgr.h"
  4. #include "furi/record.h"
  5. #include "pb_decode.h"
  6. #include "rpc/rpc.h"
  7. #include "rpc_i.h"
  8. #include "storage.pb.h"
  9. #include "storage/filesystem-api-defines.h"
  10. #include "storage/storage.h"
  11. #include <stdint.h>
  12. #include <lib/toolbox/md5.h>
  13. #define RPC_TAG "RPC_STORAGE"
  14. #define MAX_NAME_LENGTH 255
  15. #define MAX_DATA_SIZE 512
  16. typedef enum {
  17. RpcStorageStateIdle = 0,
  18. RpcStorageStateWriting,
  19. } RpcStorageState;
  20. typedef struct {
  21. Rpc* rpc;
  22. Storage* api;
  23. File* file;
  24. RpcStorageState state;
  25. uint32_t current_command_id;
  26. } RpcStorageSystem;
  27. void rpc_print_message(const PB_Main* message);
  28. static void rpc_system_storage_reset_state(RpcStorageSystem* rpc_storage, bool send_error) {
  29. furi_assert(rpc_storage);
  30. if(rpc_storage->state != RpcStorageStateIdle) {
  31. if(send_error) {
  32. rpc_encode_and_send_empty(
  33. rpc_storage->rpc,
  34. rpc_storage->current_command_id,
  35. PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED);
  36. }
  37. if(rpc_storage->state == RpcStorageStateWriting) {
  38. storage_file_close(rpc_storage->file);
  39. storage_file_free(rpc_storage->file);
  40. furi_record_close("storage");
  41. }
  42. rpc_storage->state = RpcStorageStateIdle;
  43. }
  44. }
  45. static PB_CommandStatus rpc_system_storage_get_error(FS_Error fs_error) {
  46. PB_CommandStatus pb_error;
  47. switch(fs_error) {
  48. case FSE_OK:
  49. pb_error = PB_CommandStatus_OK;
  50. break;
  51. case FSE_INVALID_NAME:
  52. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  53. break;
  54. case FSE_INVALID_PARAMETER:
  55. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_PARAMETER;
  56. break;
  57. case FSE_INTERNAL:
  58. pb_error = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
  59. break;
  60. case FSE_ALREADY_OPEN:
  61. pb_error = PB_CommandStatus_ERROR_STORAGE_ALREADY_OPEN;
  62. break;
  63. case FSE_DENIED:
  64. pb_error = PB_CommandStatus_ERROR_STORAGE_DENIED;
  65. break;
  66. case FSE_EXIST:
  67. pb_error = PB_CommandStatus_ERROR_STORAGE_EXIST;
  68. break;
  69. case FSE_NOT_EXIST:
  70. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_EXIST;
  71. break;
  72. case FSE_NOT_READY:
  73. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_READY;
  74. break;
  75. case FSE_NOT_IMPLEMENTED:
  76. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_IMPLEMENTED;
  77. break;
  78. default:
  79. pb_error = PB_CommandStatus_ERROR;
  80. break;
  81. }
  82. return pb_error;
  83. }
  84. static PB_CommandStatus rpc_system_storage_get_file_error(File* file) {
  85. return rpc_system_storage_get_error(storage_file_get_error(file));
  86. }
  87. static void rpc_system_storage_list_process(const PB_Main* request, void* context) {
  88. furi_assert(request);
  89. furi_assert(context);
  90. furi_assert(request->which_content == PB_Main_storage_list_request_tag);
  91. RpcStorageSystem* rpc_storage = context;
  92. rpc_system_storage_reset_state(rpc_storage, true);
  93. Storage* fs_api = furi_record_open("storage");
  94. File* dir = storage_file_alloc(fs_api);
  95. PB_Main response = {
  96. .command_id = request->command_id,
  97. .has_next = false,
  98. .which_content = PB_Main_storage_list_request_tag,
  99. .command_status = PB_CommandStatus_OK,
  100. };
  101. PB_Storage_ListResponse* list = &response.content.storage_list_response;
  102. response.which_content = PB_Main_storage_list_response_tag;
  103. bool finish = false;
  104. int i = 0;
  105. if(!storage_dir_open(dir, request->content.storage_list_request.path)) {
  106. response.command_status = rpc_system_storage_get_file_error(dir);
  107. response.which_content = PB_Main_empty_tag;
  108. finish = true;
  109. }
  110. while(!finish) {
  111. FileInfo fileinfo;
  112. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  113. if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  114. if(i == COUNT_OF(list->file)) {
  115. list->file_count = i;
  116. response.has_next = true;
  117. rpc_encode_and_send(rpc_storage->rpc, &response);
  118. pb_release(&PB_Main_msg, &response);
  119. i = 0;
  120. }
  121. list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ? PB_Storage_File_FileType_DIR :
  122. PB_Storage_File_FileType_FILE;
  123. list->file[i].size = fileinfo.size;
  124. list->file[i].data = NULL;
  125. list->file[i].name = name;
  126. ++i;
  127. } else {
  128. list->file_count = i;
  129. finish = true;
  130. free(name);
  131. }
  132. }
  133. response.has_next = false;
  134. rpc_encode_and_send(rpc_storage->rpc, &response);
  135. pb_release(&PB_Main_msg, &response);
  136. storage_dir_close(dir);
  137. storage_file_free(dir);
  138. furi_record_close("storage");
  139. }
  140. static void rpc_system_storage_read_process(const PB_Main* request, void* context) {
  141. furi_assert(request);
  142. furi_assert(request->which_content == PB_Main_storage_read_request_tag);
  143. RpcStorageSystem* rpc_storage = context;
  144. rpc_system_storage_reset_state(rpc_storage, true);
  145. /* use same message memory to send reponse */
  146. PB_Main* response = furi_alloc(sizeof(PB_Main));
  147. response->command_id = request->command_id;
  148. response->which_content = PB_Main_storage_read_response_tag;
  149. response->command_status = PB_CommandStatus_OK;
  150. const char* path = request->content.storage_read_request.path;
  151. Storage* fs_api = furi_record_open("storage");
  152. File* file = storage_file_alloc(fs_api);
  153. bool result = false;
  154. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  155. size_t size_left = storage_file_size(file);
  156. response->content.storage_read_response.has_file = true;
  157. response->content.storage_read_response.file.data =
  158. furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
  159. do {
  160. uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
  161. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  162. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  163. *read_size_msg = storage_file_read(file, buffer, read_size);
  164. size_left -= read_size;
  165. result = (*read_size_msg == read_size);
  166. if(result) {
  167. response->has_next = (size_left > 0);
  168. rpc_encode_and_send(rpc_storage->rpc, response);
  169. // no pb_release(...);
  170. }
  171. } while((size_left != 0) && result);
  172. if(!result) {
  173. rpc_encode_and_send_empty(
  174. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  175. }
  176. } else {
  177. rpc_encode_and_send_empty(
  178. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  179. }
  180. pb_release(&PB_Main_msg, response);
  181. free(response);
  182. storage_file_close(file);
  183. storage_file_free(file);
  184. furi_record_close("storage");
  185. }
  186. static void rpc_system_storage_write_process(const PB_Main* request, void* context) {
  187. furi_assert(request);
  188. furi_assert(request->which_content == PB_Main_storage_write_request_tag);
  189. RpcStorageSystem* rpc_storage = context;
  190. bool result = true;
  191. if((request->command_id != rpc_storage->current_command_id) &&
  192. (rpc_storage->state == RpcStorageStateWriting)) {
  193. rpc_system_storage_reset_state(rpc_storage, true);
  194. }
  195. if(rpc_storage->state != RpcStorageStateWriting) {
  196. rpc_storage->api = furi_record_open("storage");
  197. rpc_storage->file = storage_file_alloc(rpc_storage->api);
  198. rpc_storage->current_command_id = request->command_id;
  199. rpc_storage->state = RpcStorageStateWriting;
  200. const char* path = request->content.storage_write_request.path;
  201. result = storage_file_open(rpc_storage->file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS);
  202. }
  203. File* file = rpc_storage->file;
  204. if(result) {
  205. uint8_t* buffer = request->content.storage_write_request.file.data->bytes;
  206. size_t buffer_size = request->content.storage_write_request.file.data->size;
  207. uint16_t written_size = storage_file_write(file, buffer, buffer_size);
  208. result = (written_size == buffer_size);
  209. if(result && !request->has_next) {
  210. rpc_encode_and_send_empty(
  211. rpc_storage->rpc, rpc_storage->current_command_id, PB_CommandStatus_OK);
  212. rpc_system_storage_reset_state(rpc_storage, false);
  213. }
  214. }
  215. if(!result) {
  216. rpc_encode_and_send_empty(
  217. rpc_storage->rpc,
  218. rpc_storage->current_command_id,
  219. rpc_system_storage_get_file_error(file));
  220. rpc_system_storage_reset_state(rpc_storage, false);
  221. }
  222. }
  223. static void rpc_system_storage_delete_process(const PB_Main* request, void* context) {
  224. furi_assert(request);
  225. furi_assert(request->which_content == PB_Main_storage_delete_request_tag);
  226. furi_assert(context);
  227. RpcStorageSystem* rpc_storage = context;
  228. PB_CommandStatus status;
  229. rpc_system_storage_reset_state(rpc_storage, true);
  230. Storage* fs_api = furi_record_open("storage");
  231. char* path = request->content.storage_mkdir_request.path;
  232. if(path) {
  233. FS_Error error = storage_common_remove(fs_api, path);
  234. status = rpc_system_storage_get_error(error);
  235. } else {
  236. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  237. }
  238. rpc_encode_and_send_empty(rpc_storage->rpc, request->command_id, status);
  239. }
  240. static void rpc_system_storage_mkdir_process(const PB_Main* request, void* context) {
  241. furi_assert(request);
  242. furi_assert(request->which_content == PB_Main_storage_mkdir_request_tag);
  243. furi_assert(context);
  244. RpcStorageSystem* rpc_storage = context;
  245. PB_CommandStatus status;
  246. rpc_system_storage_reset_state(rpc_storage, true);
  247. Storage* fs_api = furi_record_open("storage");
  248. char* path = request->content.storage_mkdir_request.path;
  249. if(path) {
  250. FS_Error error = storage_common_mkdir(fs_api, path);
  251. status = rpc_system_storage_get_error(error);
  252. } else {
  253. status = PB_CommandStatus_ERROR_INVALID_PARAMETERS;
  254. }
  255. rpc_encode_and_send_empty(rpc_storage->rpc, request->command_id, status);
  256. }
  257. static void rpc_system_storage_md5sum_process(const PB_Main* request, void* context) {
  258. furi_assert(request);
  259. furi_assert(request->which_content == PB_Main_storage_md5sum_request_tag);
  260. furi_assert(context);
  261. RpcStorageSystem* rpc_storage = context;
  262. rpc_system_storage_reset_state(rpc_storage, true);
  263. const char* filename = request->content.storage_md5sum_request.path;
  264. if(!filename) {
  265. rpc_encode_and_send_empty(
  266. rpc_storage->rpc, request->command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS);
  267. return;
  268. }
  269. Storage* fs_api = furi_record_open("storage");
  270. File* file = storage_file_alloc(fs_api);
  271. if(storage_file_open(file, filename, FSAM_READ, FSOM_OPEN_EXISTING)) {
  272. const uint16_t read_size = 512;
  273. const uint8_t hash_size = 16;
  274. uint8_t* data = malloc(read_size);
  275. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  276. md5_context* md5_ctx = malloc(sizeof(md5_context));
  277. md5_starts(md5_ctx);
  278. while(true) {
  279. uint16_t readed_size = storage_file_read(file, data, read_size);
  280. if(readed_size == 0) break;
  281. md5_update(md5_ctx, data, readed_size);
  282. }
  283. md5_finish(md5_ctx, hash);
  284. free(md5_ctx);
  285. PB_Main response = {
  286. .command_id = request->command_id,
  287. .command_status = PB_CommandStatus_OK,
  288. .which_content = PB_Main_storage_md5sum_response_tag,
  289. .has_next = false,
  290. };
  291. char* md5sum = response.content.storage_md5sum_response.md5sum;
  292. size_t md5sum_size = sizeof(response.content.storage_md5sum_response.md5sum);
  293. furi_assert(hash_size <= ((md5sum_size - 1) / 2));
  294. for(uint8_t i = 0; i < hash_size; i++) {
  295. md5sum += sprintf(md5sum, "%02x", hash[i]);
  296. }
  297. free(hash);
  298. free(data);
  299. storage_file_close(file);
  300. rpc_encode_and_send(rpc_storage->rpc, &response);
  301. } else {
  302. rpc_encode_and_send_empty(
  303. rpc_storage->rpc, request->command_id, rpc_system_storage_get_file_error(file));
  304. }
  305. storage_file_free(file);
  306. furi_record_close("storage");
  307. }
  308. void* rpc_system_storage_alloc(Rpc* rpc) {
  309. furi_assert(rpc);
  310. RpcStorageSystem* rpc_storage = furi_alloc(sizeof(RpcStorageSystem));
  311. rpc_storage->api = furi_record_open("storage");
  312. rpc_storage->rpc = rpc;
  313. rpc_storage->state = RpcStorageStateIdle;
  314. RpcHandler rpc_handler = {
  315. .message_handler = NULL,
  316. .decode_submessage = NULL,
  317. .context = rpc_storage,
  318. };
  319. rpc_handler.message_handler = rpc_system_storage_list_process;
  320. rpc_add_handler(rpc, PB_Main_storage_list_request_tag, &rpc_handler);
  321. rpc_handler.message_handler = rpc_system_storage_read_process;
  322. rpc_add_handler(rpc, PB_Main_storage_read_request_tag, &rpc_handler);
  323. rpc_handler.message_handler = rpc_system_storage_write_process;
  324. rpc_add_handler(rpc, PB_Main_storage_write_request_tag, &rpc_handler);
  325. rpc_handler.message_handler = rpc_system_storage_delete_process;
  326. rpc_add_handler(rpc, PB_Main_storage_delete_request_tag, &rpc_handler);
  327. rpc_handler.message_handler = rpc_system_storage_mkdir_process;
  328. rpc_add_handler(rpc, PB_Main_storage_mkdir_request_tag, &rpc_handler);
  329. rpc_handler.message_handler = rpc_system_storage_md5sum_process;
  330. rpc_add_handler(rpc, PB_Main_storage_md5sum_request_tag, &rpc_handler);
  331. return rpc_storage;
  332. }
  333. void rpc_system_storage_free(void* ctx) {
  334. RpcStorageSystem* rpc_storage = ctx;
  335. rpc_system_storage_reset_state(rpc_storage, false);
  336. free(rpc_storage);
  337. }