ibutton_app.cpp 8.9 KB

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