ibutton-app.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. #include "ibutton-app.h"
  2. #include <stdarg.h>
  3. #include <callback-connector.h>
  4. #include <m-string.h>
  5. #include <file-worker-cpp.h>
  6. #include <path.h>
  7. const char* iButtonApp::app_folder = "ibutton";
  8. const char* iButtonApp::app_extension = ".ibtn";
  9. void iButtonApp::run(void* args) {
  10. iButtonEvent event;
  11. bool consumed;
  12. bool exit = false;
  13. if(args && load_key((const char*)args)) {
  14. current_scene = Scene::SceneEmulate;
  15. }
  16. scenes[current_scene]->on_enter(this);
  17. while(!exit) {
  18. view.receive_event(&event);
  19. consumed = scenes[current_scene]->on_event(this, &event);
  20. if(!consumed) {
  21. if(event.type == iButtonEvent::Type::EventTypeBack) {
  22. exit = switch_to_previous_scene();
  23. }
  24. }
  25. };
  26. scenes[current_scene]->on_exit(this);
  27. }
  28. iButtonApp::iButtonApp()
  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. 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_green_blink() {
  107. notification_message(notification, &sequence_blink_green_10);
  108. }
  109. void iButtonApp::notify_yellow_blink() {
  110. notification_message(notification, &sequence_blink_yellow_10);
  111. }
  112. void iButtonApp::notify_red_blink() {
  113. notification_message(notification, &sequence_blink_red_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. void iButtonApp::generate_random_name(char* name, uint8_t max_name_size) {
  146. const uint8_t prefix_size = 9;
  147. const char* prefix[prefix_size] = {
  148. "ancient",
  149. "hollow",
  150. "strange",
  151. "disappeared",
  152. "unknown",
  153. "unthinkable",
  154. "unnamable",
  155. "nameless",
  156. "my",
  157. };
  158. const uint8_t suffix_size = 8;
  159. const char* suffix[suffix_size] = {
  160. "door",
  161. "entrance",
  162. "doorway",
  163. "entry",
  164. "portal",
  165. "entree",
  166. "opening",
  167. "crack",
  168. };
  169. sniprintf(
  170. name, max_name_size, "%s_%s", prefix[rand() % prefix_size], suffix[rand() % suffix_size]);
  171. // to upper
  172. name[0] = name[0] - 0x20;
  173. }
  174. // file managment
  175. bool iButtonApp::save_key(const char* key_name) {
  176. FileWorkerCpp file_worker;
  177. string_t key_file_name;
  178. bool result = false;
  179. // Create ibutton directory if necessary
  180. if(!file_worker.mkdir(app_folder)) {
  181. return false;
  182. };
  183. // First remove key if it was saved
  184. string_init_printf(key_file_name, "%s/%s%s", app_folder, get_key()->get_name(), app_extension);
  185. if(!file_worker.remove(string_get_cstr(key_file_name))) {
  186. string_clear(key_file_name);
  187. return false;
  188. };
  189. // Save the key
  190. get_key()->set_name(key_name);
  191. string_printf(key_file_name, "%s/%s%s", app_folder, get_key()->get_name(), app_extension);
  192. bool res = file_worker.open(string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS);
  193. string_clear(key_file_name);
  194. if(res) {
  195. // type header
  196. const char* key_type = "E ";
  197. switch(get_key()->get_key_type()) {
  198. case iButtonKeyType::KeyCyfral:
  199. key_type = "C ";
  200. break;
  201. case iButtonKeyType::KeyDallas:
  202. key_type = "D ";
  203. break;
  204. case iButtonKeyType::KeyMetakom:
  205. key_type = "M ";
  206. break;
  207. }
  208. if(!file_worker.write(key_type, 2)) {
  209. file_worker.close();
  210. return false;
  211. }
  212. if(!file_worker.write_hex(get_key()->get_data(), get_key()->get_type_data_size())) {
  213. file_worker.close();
  214. return false;
  215. }
  216. result = true;
  217. }
  218. file_worker.close();
  219. return result;
  220. }
  221. bool iButtonApp::load_key_data(string_t key_path) {
  222. FileWorkerCpp file_worker;
  223. // Open key file
  224. if(!file_worker.open(string_get_cstr(key_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  225. file_worker.close();
  226. return false;
  227. }
  228. const uint8_t byte_text_size = 4;
  229. char byte_text[byte_text_size] = {0, 0, 0, 0};
  230. // Load type header
  231. if(!file_worker.read(byte_text, 2)) {
  232. file_worker.close();
  233. return false;
  234. }
  235. iButtonKeyType key_type = iButtonKeyType::KeyCyfral;
  236. if(strcmp(byte_text, "C ") == 0) {
  237. key_type = iButtonKeyType::KeyCyfral;
  238. } else if(strcmp(byte_text, "M ") == 0) {
  239. key_type = iButtonKeyType::KeyMetakom;
  240. } else if(strcmp(byte_text, "D ") == 0) {
  241. key_type = iButtonKeyType::KeyDallas;
  242. } else {
  243. file_worker.show_error("Cannot parse\nkey file");
  244. file_worker.close();
  245. return false;
  246. }
  247. iButtonKeyType old_type = get_key()->get_key_type();
  248. get_key()->set_type(key_type);
  249. uint8_t key_data[IBUTTON_KEY_DATA_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0};
  250. if(!file_worker.read_hex(key_data, get_key()->get_type_data_size())) {
  251. get_key()->set_type(old_type);
  252. file_worker.close();
  253. return false;
  254. }
  255. file_worker.close();
  256. get_key()->set_data(key_data, IBUTTON_KEY_DATA_SIZE);
  257. return true;
  258. }
  259. bool iButtonApp::load_key(const char* key_name) {
  260. bool result = false;
  261. string_t key_path;
  262. string_init_set_str(key_path, key_name);
  263. result = load_key_data(key_path);
  264. if(result) {
  265. path_extract_filename_no_ext(key_name, key_path);
  266. get_key()->set_name(string_get_cstr(key_path));
  267. }
  268. string_clear(key_path);
  269. return result;
  270. }
  271. bool iButtonApp::load_key() {
  272. bool result = false;
  273. FileWorkerCpp file_worker;
  274. // Input events and views are managed by file_select
  275. bool res = file_worker.file_select(
  276. app_folder, app_extension, get_file_name(), get_file_name_size(), get_key()->get_name());
  277. if(res) {
  278. string_t key_str;
  279. // Get key file path
  280. string_init_printf(key_str, "%s/%s%s", app_folder, get_file_name(), app_extension);
  281. result = load_key_data(key_str);
  282. if(result) {
  283. get_key()->set_name(get_file_name());
  284. }
  285. string_clear(key_str);
  286. }
  287. return result;
  288. }
  289. bool iButtonApp::delete_key() {
  290. string_t file_name;
  291. bool result = false;
  292. FileWorkerCpp file_worker;
  293. string_init_printf(file_name, "%s/%s%s", app_folder, get_key()->get_name(), app_extension);
  294. result = file_worker.remove(string_get_cstr(file_name));
  295. string_clear(file_name);
  296. return result;
  297. }