ibutton-app.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "ibutton-app.h"
  2. #include <stdarg.h>
  3. #include <callback-connector.h>
  4. #include <m-string.h>
  5. void iButtonApp::run(void) {
  6. iButtonEvent event;
  7. bool consumed;
  8. bool exit = false;
  9. scenes[current_scene]->on_enter(this);
  10. while(!exit) {
  11. view.receive_event(&event);
  12. consumed = scenes[current_scene]->on_event(this, &event);
  13. if(!consumed) {
  14. if(event.type == iButtonEvent::Type::EventTypeBack) {
  15. exit = switch_to_previous_scene();
  16. }
  17. }
  18. };
  19. scenes[current_scene]->on_exit(this);
  20. }
  21. void iButtonApp::print_key_data(void) {
  22. uint8_t* key_data = key.get_data();
  23. switch(key.get_key_type()) {
  24. case iButtonKeyType::KeyDallas:
  25. printf(
  26. "Dallas %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
  27. key_data[0],
  28. key_data[1],
  29. key_data[2],
  30. key_data[3],
  31. key_data[4],
  32. key_data[5],
  33. key_data[6],
  34. key_data[7]);
  35. break;
  36. case iButtonKeyType::KeyCyfral:
  37. printf("Cyfral %02X %02X\r\n", key_data[0], key_data[1]);
  38. break;
  39. case iButtonKeyType::KeyMetakom:
  40. printf(
  41. "Metakom %02X %02X %02X %02X\r\n", key_data[0], key_data[1], key_data[2], key_data[3]);
  42. break;
  43. }
  44. }
  45. bool iButtonApp::read_hex_byte(string_t args, uint8_t* byte) {
  46. char* endptr;
  47. *byte = strtoul(string_get_cstr(args), &endptr, 16);
  48. if(*endptr == '\0') {
  49. return false;
  50. }
  51. size_t ws = string_search_char(args, ' ');
  52. if(ws != 2) {
  53. return false;
  54. }
  55. string_right(args, ws);
  56. string_strim(args);
  57. return true;
  58. }
  59. void iButtonApp::cli_cmd_callback(Cli* cli, string_t args, void* context) {
  60. iButtonApp::Scene scene;
  61. string_t cmd;
  62. string_init(cmd);
  63. if(!string_cmp_str(args, "read")) {
  64. scene = iButtonApp::Scene::SceneCliRead;
  65. printf("Reading key ...\r\n");
  66. } else {
  67. // Parse write / emulate commands
  68. size_t ws = string_search_char(args, ' ');
  69. if(ws == STRING_FAILURE) {
  70. printf("Incorrect input. Try tm <read | write | emulate> [key_type] [key_data]");
  71. string_clear(cmd);
  72. return;
  73. } else {
  74. string_set_n(cmd, args, 0, ws);
  75. string_right(args, ws);
  76. string_strim(args);
  77. }
  78. if(!string_cmp_str(cmd, "write")) {
  79. scene = iButtonApp::Scene::SceneCliWrite;
  80. } else if(!string_cmp_str(cmd, "emulate")) {
  81. scene = iButtonApp::Scene::SceneCliEmulate;
  82. } else {
  83. printf("Incorrect input. Try tm <write | emulate> <key_type> <key_data>");
  84. string_clear(cmd);
  85. return;
  86. }
  87. string_clear(cmd);
  88. // Parse key type
  89. string_t key_type;
  90. string_init(key_type);
  91. ws = string_search_char(args, ' ');
  92. string_set_n(key_type, args, 0, ws);
  93. uint8_t bytes_to_read = 0;
  94. if(!string_cmp_str(key_type, "0")) {
  95. key.set_type(iButtonKeyType::KeyDallas);
  96. bytes_to_read = 8;
  97. } else if(!string_cmp_str(key_type, "1")) {
  98. key.set_type(iButtonKeyType::KeyCyfral);
  99. bytes_to_read = 2;
  100. } else if(!string_cmp_str(key_type, "2")) {
  101. key.set_type(iButtonKeyType::KeyMetakom);
  102. bytes_to_read = 4;
  103. } else {
  104. printf("Incorrect key type. Try 0 - KeyDallas, 1 - KeyCyfral, 2 - KeyMetakom");
  105. string_clear(key_type);
  106. return;
  107. }
  108. string_clear(key_type);
  109. // Read key data
  110. string_right(args, 1);
  111. string_strim(args);
  112. uint8_t key_data[8] = {};
  113. uint8_t i = 0;
  114. bool ret = true;
  115. while((i < bytes_to_read) && ret) {
  116. ret = read_hex_byte(args, &key_data[i++]);
  117. }
  118. if(i != bytes_to_read) {
  119. printf("Incorrect key data. Type %d key data hex digits", bytes_to_read);
  120. return;
  121. }
  122. key.set_data(key_data, bytes_to_read);
  123. if(scene == iButtonApp::Scene::SceneCliWrite) {
  124. printf("Writing key ");
  125. } else {
  126. printf("Emulating key ");
  127. }
  128. print_key_data();
  129. }
  130. switch_to_next_scene(scene);
  131. // Wait return event
  132. iButtonApp::CliEvent result;
  133. if(osMessageQueueGet(cli_event_result, &result, NULL, osWaitForever) != osOK) {
  134. printf("Command execution error");
  135. return;
  136. }
  137. // Process return event
  138. switch(result) {
  139. case iButtonApp::CliEvent::CliReadSuccess:
  140. case iButtonApp::CliEvent::CliReadCRCError:
  141. print_key_data();
  142. if(result == iButtonApp::CliEvent::CliReadCRCError) {
  143. printf("Warning: invalid CRC");
  144. }
  145. break;
  146. case iButtonApp::CliEvent::CliReadNotKeyError:
  147. printf("Read error: not a key");
  148. break;
  149. case iButtonApp::CliEvent::CliTimeout:
  150. printf("Timeout error");
  151. break;
  152. case iButtonApp::CliEvent::CliInterrupt:
  153. printf("Command interrupted");
  154. break;
  155. case iButtonApp::CliEvent::CliWriteSuccess:
  156. printf("Write success");
  157. break;
  158. case iButtonApp::CliEvent::CliWriteFail:
  159. printf("Write fail");
  160. break;
  161. default:
  162. break;
  163. }
  164. return;
  165. }
  166. void iButtonApp::cli_send_event(iButtonApp::CliEvent scene) {
  167. osMessageQueuePut(cli_event_result, &scene, 0, osWaitForever);
  168. }
  169. iButtonApp::iButtonApp() {
  170. api_hal_power_insomnia_enter();
  171. cli_event_result = osMessageQueueNew(1, sizeof(iButtonApp::Scene), NULL);
  172. key_worker = new KeyWorker(&ibutton_gpio);
  173. sd_ex_api = static_cast<SdCard_Api*>(furi_record_open("sdcard-ex"));
  174. fs_api = static_cast<FS_Api*>(furi_record_open("sdcard"));
  175. cli = static_cast<Cli*>(furi_record_open("cli"));
  176. notification = static_cast<NotificationApp*>(furi_record_open("notification"));
  177. auto callback = cbc::obtain_connector(this, &iButtonApp::cli_cmd_callback);
  178. cli_add_command(cli, "tm", callback, cli);
  179. // we need random
  180. srand(DWT->CYCCNT);
  181. }
  182. iButtonApp::~iButtonApp() {
  183. cli_delete_command(cli, "tm");
  184. furi_record_close("sdcard-ex");
  185. furi_record_close("sdcard");
  186. furi_record_close("cli");
  187. furi_record_close("notification");
  188. osMessageQueueDelete(cli_event_result);
  189. for(std::map<Scene, iButtonScene*>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
  190. delete it->second;
  191. scenes.erase(it);
  192. }
  193. api_hal_power_insomnia_exit();
  194. }
  195. iButtonAppViewManager* iButtonApp::get_view_manager() {
  196. return &view;
  197. }
  198. void iButtonApp::switch_to_next_scene(Scene next_scene) {
  199. previous_scenes_list.push_front(current_scene);
  200. if(next_scene != Scene::SceneExit) {
  201. scenes[current_scene]->on_exit(this);
  202. current_scene = next_scene;
  203. scenes[current_scene]->on_enter(this);
  204. }
  205. }
  206. void iButtonApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  207. Scene previous_scene = Scene::SceneStart;
  208. bool scene_found = false;
  209. while(!scene_found) {
  210. previous_scene = get_previous_scene();
  211. for(Scene element : scenes_list) {
  212. if(previous_scene == element || previous_scene == Scene::SceneStart) {
  213. scene_found = true;
  214. break;
  215. }
  216. }
  217. }
  218. scenes[current_scene]->on_exit(this);
  219. current_scene = previous_scene;
  220. scenes[current_scene]->on_enter(this);
  221. }
  222. bool iButtonApp::switch_to_previous_scene(uint8_t count) {
  223. Scene previous_scene = Scene::SceneStart;
  224. for(uint8_t i = 0; i < count; i++) {
  225. previous_scene = get_previous_scene();
  226. if(previous_scene == Scene::SceneExit) break;
  227. }
  228. if(previous_scene == Scene::SceneExit) {
  229. return true;
  230. } else {
  231. scenes[current_scene]->on_exit(this);
  232. current_scene = previous_scene;
  233. scenes[current_scene]->on_enter(this);
  234. return false;
  235. }
  236. }
  237. iButtonApp::Scene iButtonApp::get_previous_scene() {
  238. Scene scene = previous_scenes_list.front();
  239. previous_scenes_list.pop_front();
  240. return scene;
  241. }
  242. const GpioPin* iButtonApp::get_ibutton_pin() {
  243. // TODO open record
  244. return &ibutton_gpio;
  245. }
  246. KeyWorker* iButtonApp::get_key_worker() {
  247. return key_worker;
  248. }
  249. iButtonKey* iButtonApp::get_key() {
  250. return &key;
  251. }
  252. SdCard_Api* iButtonApp::get_sd_ex_api() {
  253. return sd_ex_api;
  254. }
  255. FS_Api* iButtonApp::get_fs_api() {
  256. return fs_api;
  257. }
  258. char* iButtonApp::get_file_name() {
  259. return file_name;
  260. }
  261. uint8_t iButtonApp::get_file_name_size() {
  262. return file_name_size;
  263. }
  264. void iButtonApp::notify_green_blink() {
  265. notification_message(notification, &sequence_blink_green_10);
  266. }
  267. void iButtonApp::notify_yellow_blink() {
  268. notification_message(notification, &sequence_blink_yellow_10);
  269. }
  270. void iButtonApp::notify_red_blink() {
  271. notification_message(notification, &sequence_blink_red_10);
  272. }
  273. void iButtonApp::notify_error() {
  274. notification_message(notification, &sequence_error);
  275. }
  276. void iButtonApp::notify_success() {
  277. notification_message(notification, &sequence_success);
  278. }
  279. void iButtonApp::notify_green_on() {
  280. notification_message_block(notification, &sequence_set_green_255);
  281. }
  282. void iButtonApp::notify_green_off() {
  283. notification_message(notification, &sequence_reset_green);
  284. }
  285. void iButtonApp::notify_red_on() {
  286. notification_message_block(notification, &sequence_set_red_255);
  287. }
  288. void iButtonApp::notify_red_off() {
  289. notification_message(notification, &sequence_reset_red);
  290. }
  291. void iButtonApp::set_text_store(const char* text...) {
  292. va_list args;
  293. va_start(args, text);
  294. vsnprintf(text_store, text_store_size, text, args);
  295. va_end(args);
  296. }
  297. char* iButtonApp::get_text_store() {
  298. return text_store;
  299. }
  300. uint8_t iButtonApp::get_text_store_size() {
  301. return text_store_size;
  302. }
  303. void iButtonApp::generate_random_name(char* name, uint8_t max_name_size) {
  304. const uint8_t prefix_size = 9;
  305. const char* prefix[prefix_size] = {
  306. "ancient",
  307. "hollow",
  308. "strange",
  309. "disappeared",
  310. "unknown",
  311. "unthinkable",
  312. "unnamable",
  313. "nameless",
  314. "my",
  315. };
  316. const uint8_t suffix_size = 8;
  317. const char* suffix[suffix_size] = {
  318. "door",
  319. "entrance",
  320. "doorway",
  321. "entry",
  322. "portal",
  323. "entree",
  324. "opening",
  325. "crack",
  326. };
  327. sniprintf(
  328. name, max_name_size, "%s_%s", prefix[rand() % prefix_size], suffix[rand() % suffix_size]);
  329. // to upper
  330. name[0] = name[0] - 0x20;
  331. }