ibutton-cli.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include <furi.h>
  2. #include <api-hal.h>
  3. #include <stdarg.h>
  4. #include <cli/cli.h>
  5. #include <args.h>
  6. #include "helpers/key-info.h"
  7. #include "helpers/key-worker.h"
  8. #include <memory>
  9. void ibutton_cli(Cli* cli, string_t args, void* context);
  10. // app cli function
  11. extern "C" void ibutton_cli_init() {
  12. Cli* cli = static_cast<Cli*>(furi_record_open("cli"));
  13. cli_add_command(cli, "tm", ibutton_cli, cli);
  14. furi_record_close("cli");
  15. }
  16. void ibutton_cli_print_usage() {
  17. printf("Usage:\r\n");
  18. printf("tm read\r\n");
  19. printf("tm <write | emulate> <key_type> <key_data>\r\n");
  20. printf("\t<key_type> choose from:\r\n");
  21. printf("\tDallas (8 bytes key_data)\r\n");
  22. printf("\tCyfral (2 bytes key_data)\r\n");
  23. printf("\tMetakom (4 bytes key_data)\r\n");
  24. printf("\t<key_data> are hex-formatted\r\n");
  25. };
  26. bool ibutton_cli_get_key_type(string_t data, iButtonKeyType* type) {
  27. bool result = false;
  28. if(string_cmp_str(data, "Dallas") == 0 || string_cmp_str(data, "dallas") == 0) {
  29. result = true;
  30. *type = iButtonKeyType::KeyDallas;
  31. } else if(string_cmp_str(data, "Cyfral") == 0 || string_cmp_str(data, "cyfral") == 0) {
  32. result = true;
  33. *type = iButtonKeyType::KeyCyfral;
  34. } else if(string_cmp_str(data, "Metakom") == 0 || string_cmp_str(data, "metakom") == 0) {
  35. result = true;
  36. *type = iButtonKeyType::KeyMetakom;
  37. }
  38. return result;
  39. }
  40. void ibutton_cli_print_key_data(iButtonKey* key) {
  41. uint8_t* key_data = key->get_data();
  42. switch(key->get_key_type()) {
  43. case iButtonKeyType::KeyDallas:
  44. printf(
  45. "Dallas %02X%02X%02X%02X%02X%02X%02X%02X\r\n",
  46. key_data[0],
  47. key_data[1],
  48. key_data[2],
  49. key_data[3],
  50. key_data[4],
  51. key_data[5],
  52. key_data[6],
  53. key_data[7]);
  54. break;
  55. case iButtonKeyType::KeyCyfral:
  56. printf("Cyfral %02X%02X\r\n", key_data[0], key_data[1]);
  57. break;
  58. case iButtonKeyType::KeyMetakom:
  59. printf("Metakom %02X%02X%02X%02X\r\n", key_data[0], key_data[1], key_data[2], key_data[3]);
  60. break;
  61. }
  62. }
  63. void ibutton_cli_read(Cli* cli) {
  64. iButtonKey key;
  65. std::unique_ptr<KeyWorker> worker(new KeyWorker(&ibutton_gpio));
  66. bool exit = false;
  67. worker->start_read();
  68. printf("Reading iButton...\r\nPress Ctrl+C to abort\r\n");
  69. while(!exit) {
  70. exit = cli_cmd_interrupt_received(cli);
  71. switch(worker->read(&key)) {
  72. case KeyReader::Error::EMPTY:
  73. break;
  74. case KeyReader::Error::CRC_ERROR:
  75. ibutton_cli_print_key_data(&key);
  76. printf("Warning: invalid CRC\r\n");
  77. exit = true;
  78. break;
  79. case KeyReader::Error::OK:
  80. ibutton_cli_print_key_data(&key);
  81. exit = true;
  82. break;
  83. case KeyReader::Error::NOT_ARE_KEY:
  84. ibutton_cli_print_key_data(&key);
  85. printf("Warning: not a key\r\n");
  86. exit = true;
  87. break;
  88. }
  89. delay(100);
  90. }
  91. worker->stop_read();
  92. };
  93. void ibutton_cli_write(Cli* cli, string_t args) {
  94. iButtonKey key;
  95. iButtonKeyType type;
  96. std::unique_ptr<KeyWorker> worker(new KeyWorker(&ibutton_gpio));
  97. bool exit = false;
  98. string_t data;
  99. string_init(data);
  100. if(!args_read_string_and_trim(args, data)) {
  101. ibutton_cli_print_usage();
  102. string_clear(data);
  103. return;
  104. }
  105. if(!ibutton_cli_get_key_type(data, &type)) {
  106. ibutton_cli_print_usage();
  107. string_clear(data);
  108. return;
  109. }
  110. key.set_type(type);
  111. if(!args_read_hex_bytes(args, key.get_data(), key.get_type_data_size())) {
  112. ibutton_cli_print_usage();
  113. string_clear(data);
  114. return;
  115. }
  116. printf("Writing key ");
  117. ibutton_cli_print_key_data(&key);
  118. printf("Press Ctrl+C to abort\r\n");
  119. worker->start_write();
  120. while(!exit) {
  121. exit = cli_cmd_interrupt_received(cli);
  122. KeyWriter::Error result = worker->write(&key);
  123. switch(result) {
  124. case KeyWriter::Error::SAME_KEY:
  125. case KeyWriter::Error::OK:
  126. printf("Write success\r\n");
  127. exit = true;
  128. break;
  129. case KeyWriter::Error::NO_DETECT:
  130. break;
  131. case KeyWriter::Error::CANNOT_WRITE:
  132. printf("Write fail\r\n");
  133. exit = true;
  134. break;
  135. }
  136. };
  137. worker->stop_write();
  138. string_clear(data);
  139. };
  140. void ibutton_cli_emulate(Cli* cli, string_t args) {
  141. iButtonKey key;
  142. iButtonKeyType type;
  143. std::unique_ptr<KeyWorker> worker(new KeyWorker(&ibutton_gpio));
  144. bool exit = false;
  145. string_t data;
  146. string_init(data);
  147. if(!args_read_string_and_trim(args, data)) {
  148. ibutton_cli_print_usage();
  149. string_clear(data);
  150. return;
  151. }
  152. if(!ibutton_cli_get_key_type(data, &type)) {
  153. ibutton_cli_print_usage();
  154. string_clear(data);
  155. return;
  156. }
  157. key.set_type(type);
  158. if(!args_read_hex_bytes(args, key.get_data(), key.get_type_data_size())) {
  159. ibutton_cli_print_usage();
  160. string_clear(data);
  161. return;
  162. }
  163. printf("Emulating key ");
  164. ibutton_cli_print_key_data(&key);
  165. printf("Press Ctrl+C to abort\r\n");
  166. worker->start_emulate(&key);
  167. while(!exit) {
  168. exit = cli_cmd_interrupt_received(cli);
  169. };
  170. worker->stop_emulate();
  171. string_clear(data);
  172. };
  173. void ibutton_cli(Cli* cli, string_t args, void* context) {
  174. string_t cmd;
  175. string_init(cmd);
  176. if(!args_read_string_and_trim(args, cmd)) {
  177. string_clear(cmd);
  178. ibutton_cli_print_usage();
  179. return;
  180. }
  181. if(string_cmp_str(cmd, "read") == 0) {
  182. ibutton_cli_read(cli);
  183. } else if(string_cmp_str(cmd, "write") == 0) {
  184. ibutton_cli_write(cli, args);
  185. } else if(string_cmp_str(cmd, "emulate") == 0) {
  186. ibutton_cli_emulate(cli, args);
  187. } else {
  188. ibutton_cli_print_usage();
  189. }
  190. string_clear(cmd);
  191. }