ibutton_cli.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <cli/cli.h>
  4. #include <toolbox/args.h>
  5. #include <ibutton/ibutton_key.h>
  6. #include <ibutton/ibutton_worker.h>
  7. #include <ibutton/ibutton_protocols.h>
  8. static void ibutton_cli(Cli* cli, FuriString* args, void* context);
  9. // app cli function
  10. void ibutton_on_system_start() {
  11. #ifdef SRV_CLI
  12. Cli* cli = furi_record_open(RECORD_CLI);
  13. cli_add_command(cli, "ikey", CliCommandFlagDefault, ibutton_cli, cli);
  14. furi_record_close(RECORD_CLI);
  15. #else
  16. UNUSED(ibutton_cli);
  17. #endif
  18. }
  19. static void ibutton_cli_print_usage() {
  20. printf("Usage:\r\n");
  21. printf("ikey read\r\n");
  22. printf("ikey emulate <key_type> <key_data>\r\n");
  23. printf("ikey write Dallas <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), must contain correct parity\r\n");
  28. printf("\t<key_data> are hex-formatted\r\n");
  29. };
  30. static bool ibutton_cli_parse_key(iButtonProtocols* protocols, iButtonKey* key, FuriString* args) {
  31. bool result = false;
  32. FuriString* name = furi_string_alloc();
  33. do {
  34. // Read protocol name
  35. if(!args_read_string_and_trim(args, name)) break;
  36. // Make the protocol name uppercase
  37. const char first = furi_string_get_char(name, 0);
  38. furi_string_set_char(name, 0, toupper((int)first));
  39. const iButtonProtocolId id =
  40. ibutton_protocols_get_id_by_name(protocols, furi_string_get_cstr(name));
  41. if(id == iButtonProtocolIdInvalid) break;
  42. ibutton_key_set_protocol_id(key, id);
  43. // Get the data pointer
  44. iButtonEditableData data;
  45. ibutton_protocols_get_editable_data(protocols, key, &data);
  46. // Read data
  47. if(!args_read_hex_bytes(args, data.ptr, data.size)) break;
  48. result = true;
  49. } while(false);
  50. furi_string_free(name);
  51. return result;
  52. }
  53. static void ibutton_cli_print_key(iButtonProtocols* protocols, iButtonKey* key) {
  54. const char* name = ibutton_protocols_get_name(protocols, ibutton_key_get_protocol_id(key));
  55. if(strncmp(name, "DS", 2) == 0) {
  56. name = "Dallas";
  57. }
  58. printf("%s ", name);
  59. iButtonEditableData data;
  60. ibutton_protocols_get_editable_data(protocols, key, &data);
  61. for(size_t i = 0; i < data.size; i++) {
  62. printf("%02X", data.ptr[i]);
  63. }
  64. printf("\r\n");
  65. }
  66. #define EVENT_FLAG_IBUTTON_COMPLETE (1 << 0)
  67. static void ibutton_cli_worker_read_cb(void* context) {
  68. furi_assert(context);
  69. FuriEventFlag* event = context;
  70. furi_event_flag_set(event, EVENT_FLAG_IBUTTON_COMPLETE);
  71. }
  72. static void ibutton_cli_read(Cli* cli) {
  73. iButtonProtocols* protocols = ibutton_protocols_alloc();
  74. iButtonWorker* worker = ibutton_worker_alloc(protocols);
  75. iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
  76. FuriEventFlag* event = furi_event_flag_alloc();
  77. ibutton_worker_start_thread(worker);
  78. ibutton_worker_read_set_callback(worker, ibutton_cli_worker_read_cb, event);
  79. printf("Reading iButton...\r\nPress Ctrl+C to abort\r\n");
  80. ibutton_worker_read_start(worker, key);
  81. while(true) {
  82. uint32_t flags =
  83. furi_event_flag_wait(event, EVENT_FLAG_IBUTTON_COMPLETE, FuriFlagWaitAny, 100);
  84. if(flags & EVENT_FLAG_IBUTTON_COMPLETE) {
  85. ibutton_cli_print_key(protocols, key);
  86. break;
  87. }
  88. if(cli_cmd_interrupt_received(cli)) break;
  89. }
  90. ibutton_worker_stop(worker);
  91. ibutton_worker_stop_thread(worker);
  92. ibutton_key_free(key);
  93. ibutton_worker_free(worker);
  94. ibutton_protocols_free(protocols);
  95. furi_event_flag_free(event);
  96. };
  97. typedef struct {
  98. FuriEventFlag* event;
  99. iButtonWorkerWriteResult result;
  100. } iButtonWriteContext;
  101. static void ibutton_cli_worker_write_cb(void* context, iButtonWorkerWriteResult result) {
  102. furi_assert(context);
  103. iButtonWriteContext* write_context = (iButtonWriteContext*)context;
  104. write_context->result = result;
  105. furi_event_flag_set(write_context->event, EVENT_FLAG_IBUTTON_COMPLETE);
  106. }
  107. void ibutton_cli_write(Cli* cli, FuriString* args) {
  108. iButtonProtocols* protocols = ibutton_protocols_alloc();
  109. iButtonWorker* worker = ibutton_worker_alloc(protocols);
  110. iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
  111. iButtonWriteContext write_context;
  112. write_context.event = furi_event_flag_alloc();
  113. ibutton_worker_start_thread(worker);
  114. ibutton_worker_write_set_callback(worker, ibutton_cli_worker_write_cb, &write_context);
  115. do {
  116. if(!ibutton_cli_parse_key(protocols, key, args)) {
  117. ibutton_cli_print_usage();
  118. break;
  119. }
  120. if(!(ibutton_protocols_get_features(protocols, ibutton_key_get_protocol_id(key)) &
  121. iButtonProtocolFeatureWriteBlank)) {
  122. ibutton_cli_print_usage();
  123. break;
  124. }
  125. printf("Writing key ");
  126. ibutton_cli_print_key(protocols, key);
  127. printf("Press Ctrl+C to abort\r\n");
  128. ibutton_worker_write_blank_start(worker, key);
  129. while(true) {
  130. uint32_t flags = furi_event_flag_wait(
  131. write_context.event, EVENT_FLAG_IBUTTON_COMPLETE, FuriFlagWaitAny, 100);
  132. if(flags & EVENT_FLAG_IBUTTON_COMPLETE) {
  133. if(write_context.result == iButtonWorkerWriteSameKey ||
  134. write_context.result == iButtonWorkerWriteOK) {
  135. printf("Write success\r\n");
  136. break;
  137. } else if(write_context.result == iButtonWorkerWriteCannotWrite) {
  138. printf("Write fail\r\n");
  139. break;
  140. }
  141. }
  142. if(cli_cmd_interrupt_received(cli)) break;
  143. }
  144. } while(false);
  145. ibutton_worker_stop(worker);
  146. ibutton_worker_stop_thread(worker);
  147. ibutton_key_free(key);
  148. ibutton_worker_free(worker);
  149. ibutton_protocols_free(protocols);
  150. furi_event_flag_free(write_context.event);
  151. }
  152. void ibutton_cli_emulate(Cli* cli, FuriString* args) {
  153. iButtonProtocols* protocols = ibutton_protocols_alloc();
  154. iButtonWorker* worker = ibutton_worker_alloc(protocols);
  155. iButtonKey* key = ibutton_key_alloc(ibutton_protocols_get_max_data_size(protocols));
  156. ibutton_worker_start_thread(worker);
  157. do {
  158. if(!ibutton_cli_parse_key(protocols, key, args)) {
  159. ibutton_cli_print_usage();
  160. break;
  161. }
  162. printf("Emulating key ");
  163. ibutton_cli_print_key(protocols, key);
  164. printf("Press Ctrl+C to abort\r\n");
  165. ibutton_worker_emulate_start(worker, key);
  166. while(!cli_cmd_interrupt_received(cli)) {
  167. furi_delay_ms(100);
  168. };
  169. } while(false);
  170. ibutton_worker_stop(worker);
  171. ibutton_worker_stop_thread(worker);
  172. ibutton_key_free(key);
  173. ibutton_worker_free(worker);
  174. ibutton_protocols_free(protocols);
  175. };
  176. void ibutton_cli(Cli* cli, FuriString* args, void* context) {
  177. UNUSED(cli);
  178. UNUSED(context);
  179. FuriString* cmd;
  180. cmd = furi_string_alloc();
  181. if(!args_read_string_and_trim(args, cmd)) {
  182. furi_string_free(cmd);
  183. ibutton_cli_print_usage();
  184. return;
  185. }
  186. if(furi_string_cmp_str(cmd, "read") == 0) {
  187. ibutton_cli_read(cli);
  188. } else if(furi_string_cmp_str(cmd, "write") == 0) {
  189. ibutton_cli_write(cli, args);
  190. } else if(furi_string_cmp_str(cmd, "emulate") == 0) {
  191. ibutton_cli_emulate(cli, args);
  192. } else {
  193. ibutton_cli_print_usage();
  194. }
  195. furi_string_free(cmd);
  196. }