subghz_cli.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. #include "subghz_cli.h"
  2. #include <furi.h>
  3. #include <furi-hal.h>
  4. #include <stream_buffer.h>
  5. #include <lib/toolbox/args.h>
  6. #include <lib/subghz/subghz_parser.h>
  7. #include <lib/subghz/subghz_keystore.h>
  8. #include <lib/subghz/protocols/subghz_protocol_common.h>
  9. #include <lib/subghz/protocols/subghz_protocol_princeton.h>
  10. #define SUBGHZ_FREQUENCY_RANGE_STR \
  11. "299999755...348000000 or 386999938...464000000 or 778999847...928000000"
  12. void subghz_cli_command_tx_carrier(Cli* cli, string_t args, void* context) {
  13. uint32_t frequency = 433920000;
  14. if(string_size(args)) {
  15. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  16. if(ret != 1) {
  17. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  18. cli_print_usage("subghz_tx_carrier", "<Frequency in HZ>", string_get_cstr(args));
  19. return;
  20. }
  21. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  22. printf(
  23. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  24. frequency);
  25. return;
  26. }
  27. }
  28. furi_hal_subghz_reset();
  29. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  30. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  31. hal_gpio_init(&gpio_cc1101_g0, GpioModeOutputPushPull, GpioPullNo, GpioSpeedLow);
  32. hal_gpio_write(&gpio_cc1101_g0, true);
  33. furi_hal_power_suppress_charge_enter();
  34. if(furi_hal_subghz_tx()) {
  35. printf("Transmitting at frequency %lu Hz\r\n", frequency);
  36. printf("Press CTRL+C to stop\r\n");
  37. while(!cli_cmd_interrupt_received(cli)) {
  38. osDelay(250);
  39. }
  40. } else {
  41. printf("This frequency can only be used for RX in your region\r\n");
  42. }
  43. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  44. furi_hal_subghz_sleep();
  45. furi_hal_power_suppress_charge_exit();
  46. }
  47. void subghz_cli_command_rx_carrier(Cli* cli, string_t args, void* context) {
  48. uint32_t frequency = 433920000;
  49. if(string_size(args)) {
  50. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  51. if(ret != 1) {
  52. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  53. cli_print_usage("subghz_tx_carrier", "<Frequency in HZ>", string_get_cstr(args));
  54. return;
  55. }
  56. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  57. printf(
  58. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  59. frequency);
  60. return;
  61. }
  62. }
  63. furi_hal_subghz_reset();
  64. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  65. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  66. printf("Receiving at frequency %lu Hz\r\n", frequency);
  67. printf("Press CTRL+C to stop\r\n");
  68. furi_hal_power_suppress_charge_enter();
  69. furi_hal_subghz_rx();
  70. while(!cli_cmd_interrupt_received(cli)) {
  71. osDelay(250);
  72. printf("RSSI: %03.1fdbm\r", furi_hal_subghz_get_rssi());
  73. fflush(stdout);
  74. }
  75. furi_hal_power_suppress_charge_exit();
  76. furi_hal_subghz_set_path(FuriHalSubGhzPathIsolate);
  77. furi_hal_subghz_sleep();
  78. }
  79. void subghz_cli_command_tx(Cli* cli, string_t args, void* context) {
  80. uint32_t frequency = 433920000;
  81. uint32_t key = 0x0074BADE;
  82. size_t repeat = 10;
  83. if(string_size(args)) {
  84. int ret = sscanf(string_get_cstr(args), "%lx %lu %u", &key, &frequency, &repeat);
  85. if(ret != 3) {
  86. printf(
  87. "sscanf returned %d, key: %lx, frequency: %lu, repeat: %u\r\n",
  88. ret,
  89. key,
  90. frequency,
  91. repeat);
  92. cli_print_usage(
  93. "subghz_rx",
  94. "<3 Byte Key in hex> <Frequency in HZ> <Repeat count>",
  95. string_get_cstr(args));
  96. return;
  97. }
  98. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  99. printf(
  100. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  101. frequency);
  102. return;
  103. }
  104. }
  105. printf(
  106. "Transmitting at %lu, key %lx, repeat %u. Press CTRL+C to stop\r\n",
  107. frequency,
  108. key,
  109. repeat);
  110. SubGhzDecoderPrinceton* protocol = subghz_decoder_princeton_alloc();
  111. protocol->common.code_last_found = key;
  112. protocol->common.code_last_count_bit = 24;
  113. SubGhzProtocolCommonEncoder* encoder = subghz_protocol_encoder_common_alloc();
  114. encoder->repeat = repeat;
  115. subghz_protocol_princeton_send_key(protocol, encoder);
  116. furi_hal_subghz_reset();
  117. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  118. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  119. furi_hal_power_suppress_charge_enter();
  120. furi_hal_subghz_start_async_tx(subghz_protocol_encoder_common_yield, encoder);
  121. while(!(furi_hal_subghz_is_async_tx_complete() || cli_cmd_interrupt_received(cli))) {
  122. printf(".");
  123. fflush(stdout);
  124. osDelay(333);
  125. }
  126. furi_hal_subghz_stop_async_tx();
  127. furi_hal_subghz_sleep();
  128. furi_hal_power_suppress_charge_exit();
  129. subghz_decoder_princeton_free(protocol);
  130. subghz_protocol_encoder_common_free(encoder);
  131. }
  132. typedef struct {
  133. volatile bool overrun;
  134. StreamBufferHandle_t stream;
  135. size_t packet_count;
  136. } SubGhzCliCommandRx;
  137. static void subghz_cli_command_rx_callback(bool level, uint32_t duration, void* context) {
  138. SubGhzCliCommandRx* instance = context;
  139. BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  140. LevelDuration level_duration = level_duration_make(level, duration);
  141. if(instance->overrun) {
  142. instance->overrun = false;
  143. level_duration = level_duration_reset();
  144. }
  145. size_t ret = xStreamBufferSendFromISR(
  146. instance->stream, &level_duration, sizeof(LevelDuration), &xHigherPriorityTaskWoken);
  147. if(sizeof(LevelDuration) != ret) instance->overrun = true;
  148. portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
  149. }
  150. static void subghz_cli_command_rx_text_callback(string_t text, void* context) {
  151. SubGhzCliCommandRx* instance = context;
  152. instance->packet_count++;
  153. printf("%s", string_get_cstr(text));
  154. }
  155. void subghz_cli_command_rx(Cli* cli, string_t args, void* context) {
  156. uint32_t frequency = 433920000;
  157. if(string_size(args)) {
  158. int ret = sscanf(string_get_cstr(args), "%lu", &frequency);
  159. if(ret != 1) {
  160. printf("sscanf returned %d, frequency: %lu\r\n", ret, frequency);
  161. cli_print_usage("subghz_rx", "<Frequency in HZ>", string_get_cstr(args));
  162. return;
  163. }
  164. if(!furi_hal_subghz_is_frequency_valid(frequency)) {
  165. printf(
  166. "Frequency must be in " SUBGHZ_FREQUENCY_RANGE_STR " range, not %lu\r\n",
  167. frequency);
  168. return;
  169. }
  170. }
  171. // Allocate context and buffers
  172. SubGhzCliCommandRx* instance = furi_alloc(sizeof(SubGhzCliCommandRx));
  173. instance->stream = xStreamBufferCreate(sizeof(LevelDuration) * 1024, sizeof(LevelDuration));
  174. furi_check(instance->stream);
  175. SubGhzParser* parser = subghz_parser_alloc();
  176. subghz_parser_load_keeloq_file(parser, "/ext/subghz/keeloq_mfcodes");
  177. subghz_parser_load_keeloq_file(parser, "/ext/subghz/keeloq_mfcodes_user");
  178. subghz_parser_load_nice_flor_s_file(parser, "/ext/subghz/nice_flor_s_rx");
  179. subghz_parser_load_came_atomo_file(parser, "/ext/subghz/came_atomo");
  180. subghz_parser_enable_dump_text(parser, subghz_cli_command_rx_text_callback, instance);
  181. // Configure radio
  182. furi_hal_subghz_reset();
  183. furi_hal_subghz_load_preset(FuriHalSubGhzPresetOok650Async);
  184. frequency = furi_hal_subghz_set_frequency_and_path(frequency);
  185. hal_gpio_init(&gpio_cc1101_g0, GpioModeInput, GpioPullNo, GpioSpeedLow);
  186. furi_hal_power_suppress_charge_enter();
  187. // Prepare and start RX
  188. furi_hal_subghz_start_async_rx(subghz_cli_command_rx_callback, instance);
  189. // Wait for packets to arrive
  190. printf("Listening at %lu. Press CTRL+C to stop\r\n", frequency);
  191. LevelDuration level_duration;
  192. while(!cli_cmd_interrupt_received(cli)) {
  193. int ret =
  194. xStreamBufferReceive(instance->stream, &level_duration, sizeof(LevelDuration), 10);
  195. if(ret == sizeof(LevelDuration)) {
  196. if(level_duration_is_reset(level_duration)) {
  197. printf(".");
  198. subghz_parser_reset(parser);
  199. } else {
  200. bool level = level_duration_get_level(level_duration);
  201. uint32_t duration = level_duration_get_duration(level_duration);
  202. subghz_parser_parse(parser, level, duration);
  203. }
  204. }
  205. }
  206. // Shutdown radio
  207. furi_hal_subghz_stop_async_rx();
  208. furi_hal_subghz_sleep();
  209. furi_hal_power_suppress_charge_exit();
  210. printf("\r\nPackets recieved %u\r\n", instance->packet_count);
  211. // Cleanup
  212. subghz_parser_free(parser);
  213. vStreamBufferDelete(instance->stream);
  214. free(instance);
  215. }
  216. void subghz_cli_command_print_usage() {
  217. printf("Usage:\r\n");
  218. printf("subghz <cmd> <args>\r\n");
  219. printf("Cmd list:\r\n");
  220. printf(
  221. "\tencrypt_keeloq <path_decrypted_file> <path_encrypted_file> <IV:16 bytes in hex>\t - Encrypt keeloq manufacture keys\r\n");
  222. printf(
  223. "\tencrypt_raw <path_decrypted_file> <path_encrypted_file> <IV:16 bytes in hex>\t - Encrypt RAW data\r\n");
  224. }
  225. void subghz_cli_command_encrypt_keeloq(Cli* cli, string_t args) {
  226. uint8_t iv[16];
  227. string_t source;
  228. string_t destination;
  229. string_init(source);
  230. string_init(destination);
  231. SubGhzKeystore* keystore = subghz_keystore_alloc();
  232. do {
  233. if(!args_read_string_and_trim(args, source)) {
  234. subghz_cli_command_print_usage();
  235. break;
  236. }
  237. if(!args_read_string_and_trim(args, destination)) {
  238. subghz_cli_command_print_usage();
  239. break;
  240. }
  241. if(!args_read_hex_bytes(args, iv, 16)) {
  242. subghz_cli_command_print_usage();
  243. break;
  244. }
  245. if(!subghz_keystore_load(keystore, string_get_cstr(source))) {
  246. printf("Failed to load Keystore");
  247. break;
  248. }
  249. if(!subghz_keystore_save(keystore, string_get_cstr(destination), iv)) {
  250. printf("Failed to save Keystore");
  251. break;
  252. }
  253. } while(false);
  254. subghz_keystore_free(keystore);
  255. string_clear(destination);
  256. string_clear(source);
  257. }
  258. void subghz_cli_command_encrypt_raw(Cli* cli, string_t args) {
  259. uint8_t iv[16];
  260. string_t source;
  261. string_t destination;
  262. string_init(source);
  263. string_init(destination);
  264. do {
  265. if(!args_read_string_and_trim(args, source)) {
  266. subghz_cli_command_print_usage();
  267. break;
  268. }
  269. if(!args_read_string_and_trim(args, destination)) {
  270. subghz_cli_command_print_usage();
  271. break;
  272. }
  273. if(!args_read_hex_bytes(args, iv, 16)) {
  274. subghz_cli_command_print_usage();
  275. break;
  276. }
  277. if(!subghz_keystore_raw_encrypted_save(
  278. string_get_cstr(source), string_get_cstr(destination), iv)) {
  279. printf("Failed to save Keystore");
  280. break;
  281. }
  282. } while(false);
  283. string_clear(destination);
  284. string_clear(source);
  285. }
  286. void subghz_cli_command(Cli* cli, string_t args, void* context) {
  287. string_t cmd;
  288. string_init(cmd);
  289. do {
  290. if(!args_read_string_and_trim(args, cmd)) {
  291. subghz_cli_command_print_usage();
  292. break;
  293. }
  294. if(string_cmp_str(cmd, "encrypt_keeloq") == 0) {
  295. subghz_cli_command_encrypt_keeloq(cli, args);
  296. break;
  297. }
  298. if(string_cmp_str(cmd, "encrypt_raw") == 0) {
  299. subghz_cli_command_encrypt_raw(cli, args);
  300. break;
  301. }
  302. subghz_cli_command_print_usage();
  303. } while(false);
  304. string_clear(cmd);
  305. }
  306. void subghz_cli_init() {
  307. Cli* cli = furi_record_open("cli");
  308. cli_add_command(
  309. cli, "subghz_tx_carrier", CliCommandFlagDefault, subghz_cli_command_tx_carrier, NULL);
  310. cli_add_command(
  311. cli, "subghz_rx_carrier", CliCommandFlagDefault, subghz_cli_command_rx_carrier, NULL);
  312. cli_add_command(cli, "subghz_tx", CliCommandFlagDefault, subghz_cli_command_tx, NULL);
  313. cli_add_command(cli, "subghz_rx", CliCommandFlagDefault, subghz_cli_command_rx, NULL);
  314. cli_add_command(cli, "subghz", CliCommandFlagDefault, subghz_cli_command, NULL);
  315. furi_record_close("cli");
  316. }