ibutton-app.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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(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]\r\n");
  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>\r\n");
  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\r\n");
  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\r\n");
  135. return;
  136. }
  137. // Process return event
  138. switch(result) {
  139. case iButtonApp::CliEvent::CliReadSuccess:
  140. print_key_data();
  141. case iButtonApp::CliEvent::CliReadCRCError:
  142. printf("Read error: invalid CRC\r\n");
  143. break;
  144. case iButtonApp::CliEvent::CliReadNotKeyError:
  145. printf("Read error: not a key\r\n");
  146. break;
  147. case iButtonApp::CliEvent::CliTimeout:
  148. printf("Timeout error\r\n");
  149. break;
  150. case iButtonApp::CliEvent::CliInterrupt:
  151. printf("Command interrupted\r\n");
  152. break;
  153. case iButtonApp::CliEvent::CliWriteSuccess:
  154. printf("Write success\r\n");
  155. break;
  156. case iButtonApp::CliEvent::CliWriteFail:
  157. printf("Write fail\r\n");
  158. break;
  159. default:
  160. break;
  161. }
  162. return;
  163. }
  164. void iButtonApp::cli_send_event(iButtonApp::CliEvent scene) {
  165. osMessageQueuePut(cli_event_result, &scene, 0, osWaitForever);
  166. }
  167. iButtonApp::iButtonApp() {
  168. notify_init();
  169. api_hal_power_insomnia_enter();
  170. cli_event_result = osMessageQueueNew(1, sizeof(iButtonApp::Scene), NULL);
  171. key_worker = new KeyWorker(&ibutton_gpio);
  172. sd_ex_api = static_cast<SdCard_Api*>(furi_record_open("sdcard-ex"));
  173. fs_api = static_cast<FS_Api*>(furi_record_open("sdcard"));
  174. cli = static_cast<Cli*>(furi_record_open("cli"));
  175. auto callback = cbc::obtain_connector(this, &iButtonApp::cli_cmd_callback);
  176. cli_add_command(cli, "tm", callback, cli);
  177. // we need random
  178. srand(DWT->CYCCNT);
  179. }
  180. iButtonApp::~iButtonApp() {
  181. furi_record_close("sdcard-ex");
  182. furi_record_close("sdcard");
  183. cli_delete_command(cli, "tm");
  184. furi_record_close("cli");
  185. osMessageQueueDelete(cli_event_result);
  186. for(std::map<Scene, iButtonScene*>::iterator it = scenes.begin(); it != scenes.end(); ++it) {
  187. delete it->second;
  188. scenes.erase(it);
  189. }
  190. api_hal_power_insomnia_exit();
  191. }
  192. iButtonAppViewManager* iButtonApp::get_view_manager() {
  193. return &view;
  194. }
  195. void iButtonApp::switch_to_next_scene(Scene next_scene) {
  196. previous_scenes_list.push_front(current_scene);
  197. if(next_scene != Scene::SceneExit) {
  198. scenes[current_scene]->on_exit(this);
  199. current_scene = next_scene;
  200. scenes[current_scene]->on_enter(this);
  201. }
  202. }
  203. void iButtonApp::search_and_switch_to_previous_scene(std::initializer_list<Scene> scenes_list) {
  204. Scene previous_scene = Scene::SceneStart;
  205. bool scene_found = false;
  206. while(!scene_found) {
  207. previous_scene = get_previous_scene();
  208. for(Scene element : scenes_list) {
  209. if(previous_scene == element || previous_scene == Scene::SceneStart) {
  210. scene_found = true;
  211. break;
  212. }
  213. }
  214. }
  215. scenes[current_scene]->on_exit(this);
  216. current_scene = previous_scene;
  217. scenes[current_scene]->on_enter(this);
  218. }
  219. bool iButtonApp::switch_to_previous_scene(uint8_t count) {
  220. Scene previous_scene = Scene::SceneStart;
  221. for(uint8_t i = 0; i < count; i++) {
  222. previous_scene = get_previous_scene();
  223. if(previous_scene == Scene::SceneExit) break;
  224. }
  225. if(previous_scene == Scene::SceneExit) {
  226. return true;
  227. } else {
  228. scenes[current_scene]->on_exit(this);
  229. current_scene = previous_scene;
  230. scenes[current_scene]->on_enter(this);
  231. return false;
  232. }
  233. }
  234. iButtonApp::Scene iButtonApp::get_previous_scene() {
  235. Scene scene = previous_scenes_list.front();
  236. previous_scenes_list.pop_front();
  237. return scene;
  238. }
  239. const GpioPin* iButtonApp::get_ibutton_pin() {
  240. // TODO open record
  241. return &ibutton_gpio;
  242. }
  243. KeyWorker* iButtonApp::get_key_worker() {
  244. return key_worker;
  245. }
  246. iButtonKey* iButtonApp::get_key() {
  247. return &key;
  248. }
  249. SdCard_Api* iButtonApp::get_sd_ex_api() {
  250. return sd_ex_api;
  251. }
  252. FS_Api* iButtonApp::get_fs_api() {
  253. return fs_api;
  254. }
  255. char* iButtonApp::get_file_name() {
  256. return file_name;
  257. }
  258. uint8_t iButtonApp::get_file_name_size() {
  259. return file_name_size;
  260. }
  261. void iButtonApp::notify_init() {
  262. // TODO open record
  263. const GpioPin* vibro_record = &vibro_gpio;
  264. hal_gpio_init(vibro_record, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  265. hal_gpio_write(vibro_record, false);
  266. }
  267. void iButtonApp::notify_green_blink() {
  268. notify_green_on();
  269. delay(10);
  270. notify_green_off();
  271. }
  272. void iButtonApp::notify_yellow_blink() {
  273. notify_red_on();
  274. notify_green_on();
  275. delay(10);
  276. notify_green_off();
  277. notify_red_off();
  278. }
  279. void iButtonApp::notify_red_blink() {
  280. notify_red_on();
  281. delay(10);
  282. notify_red_off();
  283. }
  284. void iButtonApp::notify_green_on() {
  285. api_hal_light_set(LightGreen, 0xFF);
  286. }
  287. void iButtonApp::notify_green_off() {
  288. api_hal_light_set(LightGreen, 0x00);
  289. }
  290. void iButtonApp::notify_red_on() {
  291. api_hal_light_set(LightRed, 0xFF);
  292. }
  293. void iButtonApp::notify_red_off() {
  294. api_hal_light_set(LightRed, 0x00);
  295. }
  296. void iButtonApp::notify_error() {
  297. notify_vibro_on();
  298. delay(50);
  299. notify_vibro_off();
  300. delay(100);
  301. notify_vibro_on();
  302. delay(50);
  303. notify_vibro_off();
  304. }
  305. void iButtonApp::notify_success() {
  306. notify_vibro_on();
  307. hal_pwm_set(0.5, 1760, &SPEAKER_TIM, SPEAKER_CH);
  308. delay(50);
  309. hal_pwm_stop(&SPEAKER_TIM, SPEAKER_CH);
  310. notify_vibro_off();
  311. }
  312. void iButtonApp::notify_vibro_on() {
  313. hal_gpio_write(&vibro_gpio, true);
  314. }
  315. void iButtonApp::notify_vibro_off() {
  316. hal_gpio_write(&vibro_gpio, false);
  317. }
  318. void iButtonApp::set_text_store(const char* text...) {
  319. va_list args;
  320. va_start(args, text);
  321. vsnprintf(text_store, text_store_size, text, args);
  322. va_end(args);
  323. }
  324. char* iButtonApp::get_text_store() {
  325. return text_store;
  326. }
  327. uint8_t iButtonApp::get_text_store_size() {
  328. return text_store_size;
  329. }
  330. void iButtonApp::generate_random_name(char* name, uint8_t max_name_size) {
  331. const uint8_t prefix_size = 9;
  332. const char* prefix[prefix_size] = {
  333. "ancient",
  334. "hollow",
  335. "strange",
  336. "disappeared",
  337. "unknown",
  338. "unthinkable",
  339. "unnamable",
  340. "nameless",
  341. "my",
  342. };
  343. const uint8_t suffix_size = 8;
  344. const char* suffix[suffix_size] = {
  345. "door",
  346. "entrance",
  347. "doorway",
  348. "entry",
  349. "portal",
  350. "entree",
  351. "opening",
  352. "crack",
  353. };
  354. sniprintf(
  355. name, max_name_size, "%s_%s", prefix[rand() % prefix_size], suffix[rand() % suffix_size]);
  356. // to upper
  357. name[0] = name[0] - 0x20;
  358. }