ibutton-app.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. #include "ibutton-app.h"
  2. #include <stdarg.h>
  3. #include <callback-connector.h>
  4. #include <m-string.h>
  5. const char* iButtonApp::app_folder = "ibutton";
  6. const char* iButtonApp::app_extension = ".ibtn";
  7. void iButtonApp::run(void) {
  8. iButtonEvent event;
  9. bool consumed;
  10. bool exit = false;
  11. scenes[current_scene]->on_enter(this);
  12. while(!exit) {
  13. view.receive_event(&event);
  14. consumed = scenes[current_scene]->on_event(this, &event);
  15. if(!consumed) {
  16. if(event.type == iButtonEvent::Type::EventTypeBack) {
  17. exit = switch_to_previous_scene();
  18. }
  19. }
  20. };
  21. scenes[current_scene]->on_exit(this);
  22. }
  23. iButtonApp::iButtonApp() {
  24. api_hal_power_insomnia_enter();
  25. key_worker = new KeyWorker(&ibutton_gpio);
  26. sd_ex_api = static_cast<SdCard_Api*>(furi_record_open("sdcard-ex"));
  27. fs_api = static_cast<FS_Api*>(furi_record_open("sdcard"));
  28. notification = static_cast<NotificationApp*>(furi_record_open("notification"));
  29. // we need random
  30. srand(DWT->CYCCNT);
  31. }
  32. iButtonApp::~iButtonApp() {
  33. cli_delete_command(cli, "tm");
  34. furi_record_close("sdcard-ex");
  35. furi_record_close("sdcard");
  36. furi_record_close("notification");
  37. for(std::map<Scene, iButtonScene*>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
  38. delete it->second;
  39. scenes.erase(it);
  40. }
  41. api_hal_power_insomnia_exit();
  42. }
  43. iButtonAppViewManager* iButtonApp::get_view_manager() {
  44. return &view;
  45. }
  46. void iButtonApp::switch_to_next_scene(Scene next_scene) {
  47. previous_scenes_list.push_front(current_scene);
  48. if(next_scene != Scene::SceneExit) {
  49. scenes[current_scene]->on_exit(this);
  50. current_scene = next_scene;
  51. scenes[current_scene]->on_enter(this);
  52. }
  53. }
  54. void iButtonApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  55. Scene previous_scene = Scene::SceneStart;
  56. bool scene_found = false;
  57. while(!scene_found) {
  58. previous_scene = get_previous_scene();
  59. for(Scene element : scenes_list) {
  60. if(previous_scene == element || previous_scene == Scene::SceneStart) {
  61. scene_found = true;
  62. break;
  63. }
  64. }
  65. }
  66. scenes[current_scene]->on_exit(this);
  67. current_scene = previous_scene;
  68. scenes[current_scene]->on_enter(this);
  69. }
  70. bool iButtonApp::switch_to_previous_scene(uint8_t count) {
  71. Scene previous_scene = Scene::SceneStart;
  72. for(uint8_t i = 0; i < count; i++) {
  73. previous_scene = get_previous_scene();
  74. if(previous_scene == Scene::SceneExit) break;
  75. }
  76. if(previous_scene == Scene::SceneExit) {
  77. return true;
  78. } else {
  79. scenes[current_scene]->on_exit(this);
  80. current_scene = previous_scene;
  81. scenes[current_scene]->on_enter(this);
  82. return false;
  83. }
  84. }
  85. iButtonApp::Scene iButtonApp::get_previous_scene() {
  86. Scene scene = previous_scenes_list.front();
  87. previous_scenes_list.pop_front();
  88. return scene;
  89. }
  90. const GpioPin* iButtonApp::get_ibutton_pin() {
  91. // TODO open record
  92. return &ibutton_gpio;
  93. }
  94. KeyWorker* iButtonApp::get_key_worker() {
  95. return key_worker;
  96. }
  97. iButtonKey* iButtonApp::get_key() {
  98. return &key;
  99. }
  100. SdCard_Api* iButtonApp::get_sd_ex_api() {
  101. return sd_ex_api;
  102. }
  103. FS_Api* iButtonApp::get_fs_api() {
  104. return fs_api;
  105. }
  106. char* iButtonApp::get_file_name() {
  107. return file_name;
  108. }
  109. uint8_t iButtonApp::get_file_name_size() {
  110. return file_name_size;
  111. }
  112. void iButtonApp::notify_green_blink() {
  113. notification_message(notification, &sequence_blink_green_10);
  114. }
  115. void iButtonApp::notify_yellow_blink() {
  116. notification_message(notification, &sequence_blink_yellow_10);
  117. }
  118. void iButtonApp::notify_red_blink() {
  119. notification_message(notification, &sequence_blink_red_10);
  120. }
  121. void iButtonApp::notify_error() {
  122. notification_message(notification, &sequence_error);
  123. }
  124. void iButtonApp::notify_success() {
  125. notification_message(notification, &sequence_success);
  126. }
  127. void iButtonApp::notify_green_on() {
  128. notification_message_block(notification, &sequence_set_green_255);
  129. }
  130. void iButtonApp::notify_green_off() {
  131. notification_message(notification, &sequence_reset_green);
  132. }
  133. void iButtonApp::notify_red_on() {
  134. notification_message_block(notification, &sequence_set_red_255);
  135. }
  136. void iButtonApp::notify_red_off() {
  137. notification_message(notification, &sequence_reset_red);
  138. }
  139. void iButtonApp::set_text_store(const char* text...) {
  140. va_list args;
  141. va_start(args, text);
  142. vsnprintf(text_store, text_store_size, text, args);
  143. va_end(args);
  144. }
  145. char* iButtonApp::get_text_store() {
  146. return text_store;
  147. }
  148. uint8_t iButtonApp::get_text_store_size() {
  149. return text_store_size;
  150. }
  151. void iButtonApp::generate_random_name(char* name, uint8_t max_name_size) {
  152. const uint8_t prefix_size = 9;
  153. const char* prefix[prefix_size] = {
  154. "ancient",
  155. "hollow",
  156. "strange",
  157. "disappeared",
  158. "unknown",
  159. "unthinkable",
  160. "unnamable",
  161. "nameless",
  162. "my",
  163. };
  164. const uint8_t suffix_size = 8;
  165. const char* suffix[suffix_size] = {
  166. "door",
  167. "entrance",
  168. "doorway",
  169. "entry",
  170. "portal",
  171. "entree",
  172. "opening",
  173. "crack",
  174. };
  175. sniprintf(
  176. name, max_name_size, "%s_%s", prefix[rand() % prefix_size], suffix[rand() % suffix_size]);
  177. // to upper
  178. name[0] = name[0] - 0x20;
  179. }
  180. // file managment
  181. void iButtonApp::show_file_error_message(const char* error_text) {
  182. set_text_store(error_text);
  183. get_sd_ex_api()->show_error(get_sd_ex_api()->context, get_text_store());
  184. }
  185. bool iButtonApp::save_key(const char* key_name) {
  186. File key_file;
  187. string_t key_file_name;
  188. bool result = false;
  189. FS_Error fs_result;
  190. uint16_t write_count;
  191. // Create ibutton directory if necessary
  192. fs_result = get_fs_api()->common.mkdir(app_folder);
  193. if(fs_result != FSE_OK && fs_result != FSE_EXIST) {
  194. show_file_error_message("Cannot create\napplication folder");
  195. return false;
  196. };
  197. // First remove key if it was saved
  198. string_init_set_str(key_file_name, app_folder);
  199. string_cat_str(key_file_name, "/");
  200. string_cat_str(key_file_name, get_key()->get_name());
  201. string_cat_str(key_file_name, app_extension);
  202. fs_result = get_fs_api()->common.remove(string_get_cstr(key_file_name));
  203. if(fs_result != FSE_OK && fs_result != FSE_NOT_EXIST) {
  204. string_clear(key_file_name);
  205. show_file_error_message("Cannot remove\nold key file");
  206. return false;
  207. };
  208. // Save the key
  209. get_key()->set_name(key_name);
  210. string_set_str(key_file_name, app_folder);
  211. string_cat_str(key_file_name, "/");
  212. string_cat_str(key_file_name, get_key()->get_name());
  213. string_cat_str(key_file_name, app_extension);
  214. bool res = get_fs_api()->file.open(
  215. &key_file, string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS);
  216. string_clear(key_file_name);
  217. if(res) {
  218. // type header
  219. const char* key_type = "E";
  220. switch(get_key()->get_key_type()) {
  221. case iButtonKeyType::KeyCyfral:
  222. key_type = "C";
  223. break;
  224. case iButtonKeyType::KeyDallas:
  225. key_type = "D";
  226. break;
  227. case iButtonKeyType::KeyMetakom:
  228. key_type = "M";
  229. break;
  230. }
  231. write_count = get_fs_api()->file.write(&key_file, key_type, 1);
  232. if(key_file.error_id != FSE_OK || write_count != 1) {
  233. show_file_error_message("Cannot write\nto key file");
  234. get_fs_api()->file.close(&key_file);
  235. return false;
  236. }
  237. const uint8_t byte_text_size = 4;
  238. char byte_text[byte_text_size];
  239. for(uint8_t i = 0; i < get_key()->get_type_data_size(); i++) {
  240. sniprintf(byte_text, byte_text_size, " %02X", get_key()->get_data()[i]);
  241. write_count = get_fs_api()->file.write(&key_file, byte_text, 3);
  242. if(key_file.error_id != FSE_OK || write_count != 3) {
  243. show_file_error_message("Cannot write\nto key file");
  244. get_fs_api()->file.close(&key_file);
  245. return false;
  246. }
  247. }
  248. result = true;
  249. } else {
  250. show_file_error_message("Cannot create\nnew key file");
  251. }
  252. get_fs_api()->file.close(&key_file);
  253. get_sd_ex_api()->check_error(get_sd_ex_api()->context);
  254. return result;
  255. }
  256. bool iButtonApp::load_key() {
  257. bool result = false;
  258. // Input events and views are managed by file_select
  259. bool res = get_sd_ex_api()->file_select(
  260. get_sd_ex_api()->context,
  261. app_folder,
  262. app_extension,
  263. get_file_name(),
  264. get_file_name_size(),
  265. get_key()->get_name());
  266. if(res) {
  267. string_t key_str;
  268. File key_file;
  269. uint16_t read_count;
  270. // Get key file path
  271. string_init_set_str(key_str, app_folder);
  272. string_cat_str(key_str, "/");
  273. string_cat_str(key_str, get_file_name());
  274. string_cat_str(key_str, app_extension);
  275. // Open key file
  276. get_fs_api()->file.open(
  277. &key_file, string_get_cstr(key_str), FSAM_READ, FSOM_OPEN_EXISTING);
  278. string_clear(key_str);
  279. if(key_file.error_id != FSE_OK) {
  280. show_file_error_message("Cannot open\nkey file");
  281. get_fs_api()->file.close(&key_file);
  282. return false;
  283. }
  284. const uint8_t byte_text_size = 4;
  285. char byte_text[byte_text_size] = {0, 0, 0, 0};
  286. // load type header
  287. read_count = get_fs_api()->file.read(&key_file, byte_text, 1);
  288. if(key_file.error_id != FSE_OK || read_count != 1) {
  289. show_file_error_message("Cannot read\nkey file");
  290. get_fs_api()->file.close(&key_file);
  291. return false;
  292. }
  293. iButtonKeyType key_type = iButtonKeyType::KeyCyfral;
  294. if(strcmp(byte_text, "C") == 0) {
  295. key_type = iButtonKeyType::KeyCyfral;
  296. } else if(strcmp(byte_text, "M") == 0) {
  297. key_type = iButtonKeyType::KeyMetakom;
  298. } else if(strcmp(byte_text, "D") == 0) {
  299. key_type = iButtonKeyType::KeyDallas;
  300. } else {
  301. show_file_error_message("Cannot parse\nkey file");
  302. get_fs_api()->file.close(&key_file);
  303. return false;
  304. }
  305. get_key()->set_type(key_type);
  306. // load data
  307. uint8_t key_data[IBUTTON_KEY_DATA_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0};
  308. for(uint8_t i = 0; i < get_key()->get_type_data_size(); i++) {
  309. // space
  310. read_count = get_fs_api()->file.read(&key_file, byte_text, 1);
  311. if(key_file.error_id != FSE_OK || read_count != 1) {
  312. show_file_error_message("Cannot read\nkey file");
  313. get_fs_api()->file.close(&key_file);
  314. return false;
  315. }
  316. // value
  317. read_count = get_fs_api()->file.read(&key_file, byte_text, 2);
  318. if(key_file.error_id != FSE_OK || read_count != 2) {
  319. show_file_error_message("Cannot read\nkey file");
  320. get_fs_api()->file.close(&key_file);
  321. return false;
  322. }
  323. // convert hex value to byte
  324. key_data[i] = strtol(byte_text, NULL, 16);
  325. }
  326. get_fs_api()->file.close(&key_file);
  327. get_key()->set_name(get_file_name());
  328. get_key()->set_type(key_type);
  329. get_key()->set_data(key_data, IBUTTON_KEY_DATA_SIZE);
  330. result = true;
  331. }
  332. get_sd_ex_api()->check_error(get_sd_ex_api()->context);
  333. return result;
  334. }
  335. bool iButtonApp::delete_key() {
  336. iButtonKey* key = get_key();
  337. string_t key_file_name;
  338. bool result = false;
  339. string_init_set_str(key_file_name, app_folder);
  340. string_cat_str(key_file_name, "/");
  341. string_cat_str(key_file_name, key->get_name());
  342. string_cat_str(key_file_name, app_extension);
  343. result = (get_fs_api()->common.remove(string_get_cstr(key_file_name)) == FSE_OK);
  344. string_clear(key_file_name);
  345. return result;
  346. }