ibutton-app.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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* args) {
  8. iButtonEvent event;
  9. bool consumed;
  10. bool exit = false;
  11. if(args && load_key((const char*)args)) {
  12. current_scene = Scene::SceneEmulate;
  13. }
  14. scenes[current_scene]->on_enter(this);
  15. while(!exit) {
  16. view.receive_event(&event);
  17. consumed = scenes[current_scene]->on_event(this, &event);
  18. if(!consumed) {
  19. if(event.type == iButtonEvent::Type::EventTypeBack) {
  20. exit = switch_to_previous_scene();
  21. }
  22. }
  23. };
  24. scenes[current_scene]->on_exit(this);
  25. }
  26. iButtonApp::iButtonApp()
  27. : fs_api{"sdcard"}
  28. , sd_ex_api{"sdcard-ex"}
  29. , notification{"notification"} {
  30. api_hal_power_insomnia_enter();
  31. key_worker = new KeyWorker(&ibutton_gpio);
  32. // we need random
  33. srand(DWT->CYCCNT);
  34. }
  35. iButtonApp::~iButtonApp() {
  36. for(std::map<Scene, iButtonScene*>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
  37. delete it->second;
  38. scenes.erase(it);
  39. }
  40. delete key_worker;
  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_data(string_t key_path) {
  257. File key_file;
  258. uint16_t read_count;
  259. // Open key file
  260. get_fs_api()->file.open(&key_file, string_get_cstr(key_path), FSAM_READ, FSOM_OPEN_EXISTING);
  261. if(key_file.error_id != FSE_OK) {
  262. show_file_error_message("Cannot open\nkey file");
  263. get_fs_api()->file.close(&key_file);
  264. return false;
  265. }
  266. const uint8_t byte_text_size = 4;
  267. char byte_text[byte_text_size] = {0, 0, 0, 0};
  268. // load type header
  269. read_count = get_fs_api()->file.read(&key_file, byte_text, 1);
  270. if(key_file.error_id != FSE_OK || read_count != 1) {
  271. show_file_error_message("Cannot read\nkey file");
  272. get_fs_api()->file.close(&key_file);
  273. return false;
  274. }
  275. iButtonKeyType key_type = iButtonKeyType::KeyCyfral;
  276. if(strcmp(byte_text, "C") == 0) {
  277. key_type = iButtonKeyType::KeyCyfral;
  278. } else if(strcmp(byte_text, "M") == 0) {
  279. key_type = iButtonKeyType::KeyMetakom;
  280. } else if(strcmp(byte_text, "D") == 0) {
  281. key_type = iButtonKeyType::KeyDallas;
  282. } else {
  283. show_file_error_message("Cannot parse\nkey file");
  284. get_fs_api()->file.close(&key_file);
  285. return false;
  286. }
  287. get_key()->set_type(key_type);
  288. // load data
  289. uint8_t key_data[IBUTTON_KEY_DATA_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0};
  290. for(uint8_t i = 0; i < get_key()->get_type_data_size(); i++) {
  291. // space
  292. read_count = get_fs_api()->file.read(&key_file, byte_text, 1);
  293. if(key_file.error_id != FSE_OK || read_count != 1) {
  294. show_file_error_message("Cannot read\nkey file");
  295. get_fs_api()->file.close(&key_file);
  296. return false;
  297. }
  298. // value
  299. read_count = get_fs_api()->file.read(&key_file, byte_text, 2);
  300. if(key_file.error_id != FSE_OK || read_count != 2) {
  301. show_file_error_message("Cannot read\nkey file");
  302. get_fs_api()->file.close(&key_file);
  303. return false;
  304. }
  305. // convert hex value to byte
  306. key_data[i] = strtol(byte_text, NULL, 16);
  307. }
  308. get_fs_api()->file.close(&key_file);
  309. get_key()->set_type(key_type);
  310. get_key()->set_data(key_data, IBUTTON_KEY_DATA_SIZE);
  311. return true;
  312. }
  313. bool iButtonApp::load_key(const char* key_name) {
  314. bool result = false;
  315. string_t key_path;
  316. string_init_set_str(key_path, key_name);
  317. if(!string_start_with_str_p(key_path, app_folder) ||
  318. !string_end_with_str_p(key_path, app_extension)) {
  319. string_clear(key_path);
  320. return false;
  321. }
  322. result = load_key_data(key_path);
  323. if(result) {
  324. uint8_t folder_end = strlen(app_folder) + 1;
  325. uint8_t extension_start = string_size(key_path) - strlen(app_extension);
  326. string_mid(key_path, folder_end, extension_start - folder_end);
  327. get_key()->set_name(string_get_cstr(key_path));
  328. }
  329. string_clear(key_path);
  330. return result;
  331. }
  332. bool iButtonApp::load_key() {
  333. bool result = false;
  334. // Input events and views are managed by file_select
  335. bool res = get_sd_ex_api()->file_select(
  336. get_sd_ex_api()->context,
  337. app_folder,
  338. app_extension,
  339. get_file_name(),
  340. get_file_name_size(),
  341. get_key()->get_name());
  342. if(res) {
  343. string_t key_str;
  344. // Get key file path
  345. string_init_set_str(key_str, app_folder);
  346. string_cat_str(key_str, "/");
  347. string_cat_str(key_str, get_file_name());
  348. string_cat_str(key_str, app_extension);
  349. result = load_key_data(key_str);
  350. if(result) {
  351. get_key()->set_name(get_file_name());
  352. }
  353. string_clear(key_str);
  354. }
  355. get_sd_ex_api()->check_error(get_sd_ex_api()->context);
  356. return result;
  357. }
  358. bool iButtonApp::delete_key() {
  359. iButtonKey* key = get_key();
  360. string_t key_file_name;
  361. bool result = false;
  362. string_init_set_str(key_file_name, app_folder);
  363. string_cat_str(key_file_name, "/");
  364. string_cat_str(key_file_name, key->get_name());
  365. string_cat_str(key_file_name, app_extension);
  366. result = (get_fs_api()->common.remove(string_get_cstr(key_file_name)) == FSE_OK);
  367. string_clear(key_file_name);
  368. return result;
  369. }