ibutton-app.cpp 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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 = "/any/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. make_app_folder();
  14. if(args && load_key((const char*)args)) {
  15. current_scene = Scene::SceneEmulate;
  16. }
  17. scenes[current_scene]->on_enter(this);
  18. while(!exit) {
  19. view.receive_event(&event);
  20. consumed = scenes[current_scene]->on_event(this, &event);
  21. if(!consumed) {
  22. if(event.type == iButtonEvent::Type::EventTypeBack) {
  23. exit = switch_to_previous_scene();
  24. }
  25. }
  26. };
  27. scenes[current_scene]->on_exit(this);
  28. }
  29. iButtonApp::iButtonApp()
  30. : notification{"notification"} {
  31. api_hal_power_insomnia_enter();
  32. key_worker = new KeyWorker(&ibutton_gpio);
  33. // we need random
  34. srand(DWT->CYCCNT);
  35. }
  36. iButtonApp::~iButtonApp() {
  37. for(std::map<Scene, iButtonScene*>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
  38. delete it->second;
  39. scenes.erase(it);
  40. }
  41. delete key_worker;
  42. api_hal_power_insomnia_exit();
  43. }
  44. iButtonAppViewManager* iButtonApp::get_view_manager() {
  45. return &view;
  46. }
  47. void iButtonApp::switch_to_next_scene(Scene next_scene) {
  48. previous_scenes_list.push_front(current_scene);
  49. if(next_scene != Scene::SceneExit) {
  50. scenes[current_scene]->on_exit(this);
  51. current_scene = next_scene;
  52. scenes[current_scene]->on_enter(this);
  53. }
  54. }
  55. void iButtonApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  56. Scene previous_scene = Scene::SceneStart;
  57. bool scene_found = false;
  58. while(!scene_found) {
  59. previous_scene = get_previous_scene();
  60. for(Scene element : scenes_list) {
  61. if(previous_scene == element || previous_scene == Scene::SceneStart) {
  62. scene_found = true;
  63. break;
  64. }
  65. }
  66. }
  67. scenes[current_scene]->on_exit(this);
  68. current_scene = previous_scene;
  69. scenes[current_scene]->on_enter(this);
  70. }
  71. bool iButtonApp::switch_to_previous_scene(uint8_t count) {
  72. Scene previous_scene = Scene::SceneStart;
  73. for(uint8_t i = 0; i < count; i++) {
  74. previous_scene = get_previous_scene();
  75. if(previous_scene == Scene::SceneExit) break;
  76. }
  77. if(previous_scene == Scene::SceneExit) {
  78. return true;
  79. } else {
  80. scenes[current_scene]->on_exit(this);
  81. current_scene = previous_scene;
  82. scenes[current_scene]->on_enter(this);
  83. return false;
  84. }
  85. }
  86. iButtonApp::Scene iButtonApp::get_previous_scene() {
  87. Scene scene = previous_scenes_list.front();
  88. previous_scenes_list.pop_front();
  89. return scene;
  90. }
  91. const GpioPin* iButtonApp::get_ibutton_pin() {
  92. // TODO open record
  93. return &ibutton_gpio;
  94. }
  95. KeyWorker* iButtonApp::get_key_worker() {
  96. return key_worker;
  97. }
  98. iButtonKey* iButtonApp::get_key() {
  99. return &key;
  100. }
  101. char* iButtonApp::get_file_name() {
  102. return file_name;
  103. }
  104. uint8_t iButtonApp::get_file_name_size() {
  105. return file_name_size;
  106. }
  107. void iButtonApp::notify_green_blink() {
  108. notification_message(notification, &sequence_blink_green_10);
  109. }
  110. void iButtonApp::notify_yellow_blink() {
  111. notification_message(notification, &sequence_blink_yellow_10);
  112. }
  113. void iButtonApp::notify_red_blink() {
  114. notification_message(notification, &sequence_blink_red_10);
  115. }
  116. void iButtonApp::notify_error() {
  117. notification_message(notification, &sequence_error);
  118. }
  119. void iButtonApp::notify_success() {
  120. notification_message(notification, &sequence_success);
  121. }
  122. void iButtonApp::notify_green_on() {
  123. notification_message_block(notification, &sequence_set_green_255);
  124. }
  125. void iButtonApp::notify_green_off() {
  126. notification_message(notification, &sequence_reset_green);
  127. }
  128. void iButtonApp::notify_red_on() {
  129. notification_message_block(notification, &sequence_set_red_255);
  130. }
  131. void iButtonApp::notify_red_off() {
  132. notification_message(notification, &sequence_reset_red);
  133. }
  134. void iButtonApp::set_text_store(const char* text...) {
  135. va_list args;
  136. va_start(args, text);
  137. vsnprintf(text_store, text_store_size, text, args);
  138. va_end(args);
  139. }
  140. char* iButtonApp::get_text_store() {
  141. return text_store;
  142. }
  143. uint8_t iButtonApp::get_text_store_size() {
  144. return text_store_size;
  145. }
  146. void iButtonApp::generate_random_name(char* name, uint8_t max_name_size) {
  147. const uint8_t prefix_size = 9;
  148. const char* prefix[prefix_size] = {
  149. "ancient",
  150. "hollow",
  151. "strange",
  152. "disappeared",
  153. "unknown",
  154. "unthinkable",
  155. "unnamable",
  156. "nameless",
  157. "my",
  158. };
  159. const uint8_t suffix_size = 8;
  160. const char* suffix[suffix_size] = {
  161. "door",
  162. "entrance",
  163. "doorway",
  164. "entry",
  165. "portal",
  166. "entree",
  167. "opening",
  168. "crack",
  169. };
  170. sniprintf(
  171. name, max_name_size, "%s_%s", prefix[rand() % prefix_size], suffix[rand() % suffix_size]);
  172. // to upper
  173. name[0] = name[0] - 0x20;
  174. }
  175. // file managment
  176. bool iButtonApp::save_key(const char* key_name) {
  177. // Create ibutton directory if necessary
  178. make_app_folder();
  179. FileWorkerCpp file_worker;
  180. string_t key_file_name;
  181. bool result = false;
  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. FileWorkerCpp 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. result = load_key_data(key_path);
  263. if(result) {
  264. path_extract_filename_no_ext(key_name, key_path);
  265. get_key()->set_name(string_get_cstr(key_path));
  266. }
  267. string_clear(key_path);
  268. return result;
  269. }
  270. bool iButtonApp::load_key() {
  271. bool result = false;
  272. FileWorkerCpp file_worker;
  273. // Input events and views are managed by file_select
  274. bool res = file_worker.file_select(
  275. app_folder, app_extension, get_file_name(), get_file_name_size(), get_key()->get_name());
  276. if(res) {
  277. string_t key_str;
  278. // Get key file path
  279. string_init_printf(key_str, "%s/%s%s", app_folder, get_file_name(), app_extension);
  280. result = load_key_data(key_str);
  281. if(result) {
  282. get_key()->set_name(get_file_name());
  283. }
  284. string_clear(key_str);
  285. }
  286. return result;
  287. }
  288. bool iButtonApp::delete_key() {
  289. string_t file_name;
  290. bool result = false;
  291. FileWorkerCpp file_worker;
  292. string_init_printf(file_name, "%s/%s%s", app_folder, get_key()->get_name(), app_extension);
  293. result = file_worker.remove(string_get_cstr(file_name));
  294. string_clear(file_name);
  295. return result;
  296. }
  297. void iButtonApp::make_app_folder() {
  298. FileWorkerCpp file_worker;
  299. file_worker.mkdir(app_folder);
  300. }