ibutton_cli.cpp 7.2 KB

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