ibutton-app.cpp 9.5 KB

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