ibutton_app.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. #include "ibutton_app.h"
  2. #include <stdarg.h>
  3. #include <furi_hal.h>
  4. #include <m-string.h>
  5. #include <toolbox/path.h>
  6. #include <flipper_format/flipper_format.h>
  7. const char* iButtonApp::app_folder = "/any/ibutton";
  8. const char* iButtonApp::app_extension = ".ibtn";
  9. const char* iButtonApp::app_filetype = "Flipper iButton key";
  10. void iButtonApp::run(void* args) {
  11. iButtonEvent event;
  12. bool consumed;
  13. bool exit = false;
  14. make_app_folder();
  15. if(args && load_key((const char*)args)) {
  16. current_scene = Scene::SceneEmulate;
  17. }
  18. scenes[current_scene]->on_enter(this);
  19. while(!exit) {
  20. view.receive_event(&event);
  21. consumed = scenes[current_scene]->on_event(this, &event);
  22. if(!consumed) {
  23. if(event.type == iButtonEvent::Type::EventTypeBack) {
  24. exit = switch_to_previous_scene();
  25. }
  26. }
  27. };
  28. scenes[current_scene]->on_exit(this);
  29. }
  30. iButtonApp::iButtonApp()
  31. : notification{"notification"}
  32. , storage{"storage"}
  33. , dialogs{"dialogs"} {
  34. key = ibutton_key_alloc();
  35. key_worker = ibutton_worker_alloc();
  36. ibutton_worker_start_thread(key_worker);
  37. }
  38. iButtonApp::~iButtonApp() {
  39. for(std::map<Scene, iButtonScene*>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
  40. delete it->second;
  41. }
  42. scenes.clear();
  43. ibutton_worker_stop_thread(key_worker);
  44. ibutton_worker_free(key_worker);
  45. ibutton_key_free(key);
  46. }
  47. iButtonAppViewManager* iButtonApp::get_view_manager() {
  48. return &view;
  49. }
  50. void iButtonApp::switch_to_next_scene(Scene next_scene) {
  51. previous_scenes_list.push_front(current_scene);
  52. if(next_scene != Scene::SceneExit) {
  53. scenes[current_scene]->on_exit(this);
  54. current_scene = next_scene;
  55. scenes[current_scene]->on_enter(this);
  56. }
  57. }
  58. void iButtonApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  59. Scene previous_scene = Scene::SceneStart;
  60. bool scene_found = false;
  61. while(!scene_found) {
  62. previous_scene = get_previous_scene();
  63. for(Scene element : scenes_list) {
  64. if(previous_scene == element || previous_scene == Scene::SceneStart) {
  65. scene_found = true;
  66. break;
  67. }
  68. }
  69. }
  70. scenes[current_scene]->on_exit(this);
  71. current_scene = previous_scene;
  72. scenes[current_scene]->on_enter(this);
  73. }
  74. bool iButtonApp::switch_to_previous_scene(uint8_t count) {
  75. Scene previous_scene = Scene::SceneStart;
  76. for(uint8_t i = 0; i < count; i++) {
  77. previous_scene = get_previous_scene();
  78. if(previous_scene == Scene::SceneExit) break;
  79. }
  80. if(previous_scene == Scene::SceneExit) {
  81. return true;
  82. } else {
  83. scenes[current_scene]->on_exit(this);
  84. current_scene = previous_scene;
  85. scenes[current_scene]->on_enter(this);
  86. return false;
  87. }
  88. }
  89. iButtonApp::Scene iButtonApp::get_previous_scene() {
  90. Scene scene = previous_scenes_list.front();
  91. previous_scenes_list.pop_front();
  92. return scene;
  93. }
  94. iButtonWorker* iButtonApp::get_key_worker() {
  95. return key_worker;
  96. }
  97. iButtonKey* iButtonApp::get_key() {
  98. return key;
  99. }
  100. char* iButtonApp::get_file_name() {
  101. return file_name;
  102. }
  103. uint8_t iButtonApp::get_file_name_size() {
  104. return file_name_size;
  105. }
  106. void iButtonApp::notify_read() {
  107. notification_message(notification, &sequence_blink_cyan_10);
  108. }
  109. void iButtonApp::notify_emulate() {
  110. notification_message(notification, &sequence_blink_magenta_10);
  111. }
  112. void iButtonApp::notify_yellow_blink() {
  113. notification_message(notification, &sequence_blink_yellow_10);
  114. }
  115. void iButtonApp::notify_error() {
  116. notification_message(notification, &sequence_error);
  117. }
  118. void iButtonApp::notify_success() {
  119. notification_message(notification, &sequence_success);
  120. }
  121. void iButtonApp::notify_green_on() {
  122. notification_message_block(notification, &sequence_set_green_255);
  123. }
  124. void iButtonApp::notify_green_off() {
  125. notification_message(notification, &sequence_reset_green);
  126. }
  127. void iButtonApp::notify_red_on() {
  128. notification_message_block(notification, &sequence_set_red_255);
  129. }
  130. void iButtonApp::notify_red_off() {
  131. notification_message(notification, &sequence_reset_red);
  132. }
  133. void iButtonApp::set_text_store(const char* text...) {
  134. va_list args;
  135. va_start(args, text);
  136. vsnprintf(text_store, text_store_size, text, args);
  137. va_end(args);
  138. }
  139. char* iButtonApp::get_text_store() {
  140. return text_store;
  141. }
  142. uint8_t iButtonApp::get_text_store_size() {
  143. return text_store_size;
  144. }
  145. // file managment
  146. bool iButtonApp::save_key(const char* key_name) {
  147. // Create ibutton directory if necessary
  148. make_app_folder();
  149. FlipperFormat* file = flipper_format_file_alloc(storage);
  150. string_t key_file_name;
  151. bool result = false;
  152. string_init(key_file_name);
  153. do {
  154. // First remove key if it was saved (we rename the key)
  155. if(!delete_key()) break;
  156. // Save the key
  157. ibutton_key_set_name(key, key_name);
  158. // Set full file name, for new key
  159. string_printf(
  160. key_file_name, "%s/%s%s", app_folder, ibutton_key_get_name_p(key), app_extension);
  161. // Open file for write
  162. if(!flipper_format_file_open_always(file, string_get_cstr(key_file_name))) break;
  163. // Write header
  164. if(!flipper_format_write_header_cstr(file, iButtonApp::app_filetype, 1)) break;
  165. // Write key type
  166. if(!flipper_format_write_comment_cstr(file, "Key type can be Cyfral, Dallas or Metakom"))
  167. break;
  168. const char* key_type = ibutton_key_get_string_by_type(ibutton_key_get_type(key));
  169. if(!flipper_format_write_string_cstr(file, "Key type", key_type)) break;
  170. // Write data
  171. if(!flipper_format_write_comment_cstr(
  172. file, "Data size for Cyfral is 2, for Metakom is 4, for Dallas is 8"))
  173. break;
  174. if(!flipper_format_write_hex(
  175. file, "Data", ibutton_key_get_data_p(key), ibutton_key_get_data_size(key)))
  176. break;
  177. result = true;
  178. } while(false);
  179. flipper_format_free(file);
  180. string_clear(key_file_name);
  181. if(!result) {
  182. dialog_message_show_storage_error(dialogs, "Cannot save\nkey file");
  183. }
  184. return result;
  185. }
  186. bool iButtonApp::load_key_data(string_t key_path) {
  187. FlipperFormat* file = flipper_format_file_alloc(storage);
  188. bool result = false;
  189. string_t data;
  190. string_init(data);
  191. do {
  192. if(!flipper_format_file_open_existing(file, string_get_cstr(key_path))) break;
  193. // header
  194. uint32_t version;
  195. if(!flipper_format_read_header(file, data, &version)) break;
  196. if(string_cmp_str(data, iButtonApp::app_filetype) != 0) break;
  197. if(version != 1) break;
  198. // key type
  199. iButtonKeyType type;
  200. if(!flipper_format_read_string(file, "Key type", data)) break;
  201. if(!ibutton_key_get_type_by_string(string_get_cstr(data), &type)) break;
  202. // key data
  203. uint8_t key_data[IBUTTON_KEY_DATA_SIZE] = {0};
  204. if(!flipper_format_read_hex(file, "Data", key_data, ibutton_key_get_size_by_type(type)))
  205. break;
  206. ibutton_key_set_type(key, type);
  207. ibutton_key_set_data(key, key_data, IBUTTON_KEY_DATA_SIZE);
  208. result = true;
  209. } while(false);
  210. flipper_format_free(file);
  211. string_clear(data);
  212. if(!result) {
  213. dialog_message_show_storage_error(dialogs, "Cannot load\nkey file");
  214. }
  215. return result;
  216. }
  217. bool iButtonApp::load_key(const char* key_name) {
  218. bool result = false;
  219. string_t key_path;
  220. string_init_set_str(key_path, key_name);
  221. result = load_key_data(key_path);
  222. if(result) {
  223. path_extract_filename_no_ext(key_name, key_path);
  224. ibutton_key_set_name(key, string_get_cstr(key_path));
  225. }
  226. string_clear(key_path);
  227. return result;
  228. }
  229. bool iButtonApp::load_key() {
  230. bool result = false;
  231. // Input events and views are managed by file_select
  232. bool res = dialog_file_select_show(
  233. dialogs,
  234. app_folder,
  235. app_extension,
  236. get_file_name(),
  237. get_file_name_size(),
  238. ibutton_key_get_name_p(key));
  239. if(res) {
  240. string_t key_str;
  241. // Get key file path
  242. string_init_printf(key_str, "%s/%s%s", app_folder, get_file_name(), app_extension);
  243. result = load_key_data(key_str);
  244. if(result) {
  245. ibutton_key_set_name(key, get_file_name());
  246. }
  247. string_clear(key_str);
  248. }
  249. return result;
  250. }
  251. bool iButtonApp::delete_key() {
  252. string_t file_name;
  253. bool result = false;
  254. string_init_printf(
  255. file_name, "%s/%s%s", app_folder, ibutton_key_get_name_p(key), app_extension);
  256. result = storage_simply_remove(storage, string_get_cstr(file_name));
  257. string_clear(file_name);
  258. return result;
  259. }
  260. void iButtonApp::make_app_folder() {
  261. if(!storage_simply_mkdir(storage, app_folder)) {
  262. dialog_message_show_storage_error(dialogs, "Cannot create\napp folder");
  263. }
  264. }