ibutton-cli.cpp 7.1 KB

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