ibutton-app.cpp 11 KB

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