ibutton-app.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <lib/toolbox/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. }
  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. // file managment
  145. bool iButtonApp::save_key(const char* key_name) {
  146. // Create ibutton directory if necessary
  147. make_app_folder();
  148. FileWorkerCpp file_worker;
  149. string_t key_file_name;
  150. bool result = false;
  151. // First remove key if it was saved
  152. string_init_printf(key_file_name, "%s/%s%s", app_folder, get_key()->get_name(), app_extension);
  153. if(!file_worker.remove(string_get_cstr(key_file_name))) {
  154. string_clear(key_file_name);
  155. return false;
  156. };
  157. // Save the key
  158. get_key()->set_name(key_name);
  159. string_printf(key_file_name, "%s/%s%s", app_folder, get_key()->get_name(), app_extension);
  160. bool res = file_worker.open(string_get_cstr(key_file_name), FSAM_WRITE, FSOM_CREATE_ALWAYS);
  161. string_clear(key_file_name);
  162. if(res) {
  163. // type header
  164. const char* key_type = "E ";
  165. switch(get_key()->get_key_type()) {
  166. case iButtonKeyType::KeyCyfral:
  167. key_type = "C ";
  168. break;
  169. case iButtonKeyType::KeyDallas:
  170. key_type = "D ";
  171. break;
  172. case iButtonKeyType::KeyMetakom:
  173. key_type = "M ";
  174. break;
  175. }
  176. if(!file_worker.write(key_type, 2)) {
  177. file_worker.close();
  178. return false;
  179. }
  180. if(!file_worker.write_hex(get_key()->get_data(), get_key()->get_type_data_size())) {
  181. file_worker.close();
  182. return false;
  183. }
  184. result = true;
  185. }
  186. file_worker.close();
  187. return result;
  188. }
  189. bool iButtonApp::load_key_data(string_t key_path) {
  190. FileWorkerCpp file_worker;
  191. // Open key file
  192. if(!file_worker.open(string_get_cstr(key_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  193. file_worker.close();
  194. return false;
  195. }
  196. const uint8_t byte_text_size = 4;
  197. char byte_text[byte_text_size] = {0, 0, 0, 0};
  198. // Load type header
  199. if(!file_worker.read(byte_text, 2)) {
  200. file_worker.close();
  201. return false;
  202. }
  203. iButtonKeyType key_type = iButtonKeyType::KeyCyfral;
  204. if(strcmp(byte_text, "C ") == 0) {
  205. key_type = iButtonKeyType::KeyCyfral;
  206. } else if(strcmp(byte_text, "M ") == 0) {
  207. key_type = iButtonKeyType::KeyMetakom;
  208. } else if(strcmp(byte_text, "D ") == 0) {
  209. key_type = iButtonKeyType::KeyDallas;
  210. } else {
  211. file_worker.show_error("Cannot parse\nkey file");
  212. file_worker.close();
  213. return false;
  214. }
  215. iButtonKeyType old_type = get_key()->get_key_type();
  216. get_key()->set_type(key_type);
  217. uint8_t key_data[IBUTTON_KEY_DATA_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0};
  218. if(!file_worker.read_hex(key_data, get_key()->get_type_data_size())) {
  219. get_key()->set_type(old_type);
  220. file_worker.close();
  221. return false;
  222. }
  223. file_worker.close();
  224. get_key()->set_data(key_data, IBUTTON_KEY_DATA_SIZE);
  225. return true;
  226. }
  227. bool iButtonApp::load_key(const char* key_name) {
  228. bool result = false;
  229. string_t key_path;
  230. string_init_set_str(key_path, key_name);
  231. result = load_key_data(key_path);
  232. if(result) {
  233. path_extract_filename_no_ext(key_name, key_path);
  234. get_key()->set_name(string_get_cstr(key_path));
  235. }
  236. string_clear(key_path);
  237. return result;
  238. }
  239. bool iButtonApp::load_key() {
  240. bool result = false;
  241. FileWorkerCpp file_worker;
  242. // Input events and views are managed by file_select
  243. bool res = file_worker.file_select(
  244. app_folder, app_extension, get_file_name(), get_file_name_size(), get_key()->get_name());
  245. if(res) {
  246. string_t key_str;
  247. // Get key file path
  248. string_init_printf(key_str, "%s/%s%s", app_folder, get_file_name(), app_extension);
  249. result = load_key_data(key_str);
  250. if(result) {
  251. get_key()->set_name(get_file_name());
  252. }
  253. string_clear(key_str);
  254. }
  255. return result;
  256. }
  257. bool iButtonApp::delete_key() {
  258. string_t file_name;
  259. bool result = false;
  260. FileWorkerCpp file_worker;
  261. string_init_printf(file_name, "%s/%s%s", app_folder, get_key()->get_name(), app_extension);
  262. result = file_worker.remove(string_get_cstr(file_name));
  263. string_clear(file_name);
  264. return result;
  265. }
  266. void iButtonApp::make_app_folder() {
  267. FileWorkerCpp file_worker;
  268. file_worker.mkdir(app_folder);
  269. }