subbrute_device.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #pragma once
  2. #include "subbrute_protocols.h"
  3. #include <lib/subghz/protocols/base.h>
  4. #include <lib/subghz/transmitter.h>
  5. #include <lib/subghz/receiver.h>
  6. #include <lib/subghz/environment.h>
  7. #include "helpers/subbrute_radio_device_loader.h"
  8. #define SUBBRUTE_MAX_LEN_NAME 64
  9. #define SUBBRUTE_PATH EXT_PATH("subghz")
  10. #define SUBBRUTE_FILE_EXT ".sub"
  11. /**
  12. * @enum SubBruteFileResult
  13. * @brief Represents the possible results of a sub-brute force file operation.
  14. *
  15. * This enumeration defines a set of possible results that can occur when performing
  16. * a sub-brute force operation on a file. Each result represents a specific error or
  17. * condition that can occur during the operation.
  18. */
  19. typedef enum {
  20. SubBruteFileResultUnknown,
  21. SubBruteFileResultOk,
  22. SubBruteFileResultErrorOpenFile,
  23. SubBruteFileResultMissingOrIncorrectHeader,
  24. SubBruteFileResultFrequencyNotAllowed,
  25. SubBruteFileResultMissingOrIncorrectFrequency,
  26. SubBruteFileResultPresetInvalid,
  27. SubBruteFileResultMissingProtocol,
  28. SubBruteFileResultProtocolNotSupported,
  29. SubBruteFileResultDynamicProtocolNotValid,
  30. SubBruteFileResultProtocolNotFound,
  31. SubBruteFileResultMissingOrIncorrectBit,
  32. SubBruteFileResultMissingOrIncorrectKey,
  33. SubBruteFileResultMissingOrIncorrectTe,
  34. } SubBruteFileResult;
  35. /**
  36. * @struct SubBruteDevice
  37. * @brief Represents a device for SubBrute attack.
  38. *
  39. * This structure contains information and state variables required for performing
  40. * a SubBrute attack.
  41. */
  42. typedef struct {
  43. const SubBruteProtocol* protocol_info;
  44. SubBruteProtocol* file_protocol_info;
  45. // Current step
  46. uint64_t current_step;
  47. // SubGhz
  48. SubGhzReceiver* receiver;
  49. SubGhzProtocolDecoderBase* decoder_result;
  50. SubGhzEnvironment* environment;
  51. const SubGhzDevice* radio_device;
  52. // Attack state
  53. SubBruteAttacks attack;
  54. uint64_t max_value;
  55. uint8_t extra_repeats;
  56. // Loaded info for attack type
  57. uint64_t key_from_file;
  58. uint64_t current_key_from_file;
  59. bool two_bytes;
  60. // Index of group to bruteforce in loaded file
  61. uint8_t bit_index;
  62. } SubBruteDevice;
  63. /**
  64. * @brief Allocates memory for a SubBruteDevice structure.
  65. *
  66. * This function allocates memory for a SubBruteDevice structure and
  67. * initializes it using the given SubGhzDevice.
  68. *
  69. * @param radio_device The SubGhzDevice used to initialize the SubBruteDevice.
  70. * @return A pointer to the allocated SubBruteDevice structure.
  71. */
  72. SubBruteDevice* subbrute_device_alloc(const SubGhzDevice* radio_device);
  73. /**
  74. * @brief Frees the memory allocated for a SubBruteDevice instance.
  75. *
  76. * This function frees the memory allocated for a SubBruteDevice instance.
  77. * After calling this function, the instance is no longer valid and should not be used.
  78. *
  79. * @param instance Pointer to the SubBruteDevice instance to be freed.
  80. */
  81. void subbrute_device_free(SubBruteDevice* instance);
  82. /**
  83. * Saves a file with the specified key name using the given SubBruteDevice instance.
  84. *
  85. * @param instance The SubBruteDevice instance to use for saving the file.
  86. * @param key_name The name of the key to be used for saving the file.
  87. * @return True if the file is successfully saved, False otherwise.
  88. */
  89. bool subbrute_device_save_file(SubBruteDevice* instance, const char* key_name);
  90. /**
  91. * @brief Retrieves the description for a specific device error.
  92. *
  93. * This function returns a string that describes the given device error ID. The error ID
  94. * should be obtained from a SubBruteFileResult value. The returned string provides
  95. * additional information about the error, such as its cause or possible solutions.
  96. *
  97. * @param error_id The device error ID for which to retrieve the description.
  98. *
  99. * @return A pointer to a constant string containing the description of the device error.
  100. */
  101. const char* subbrute_device_error_get_desc(SubBruteFileResult error_id);
  102. SubBruteFileResult subbrute_device_attack_set(
  103. SubBruteDevice* context,
  104. SubBruteAttacks type,
  105. uint8_t extra_repeats);
  106. /**
  107. * @brief Loads data from a file into a SubBruteDevice context.
  108. *
  109. * This function reads data from the specified file and populates the
  110. * SubBruteDevice context with the loaded data. The file contents are
  111. * expected to be in a specific format supported by the SubBruteDevice.
  112. * Use this function to initialize the context with pre-existing data.
  113. *
  114. * @param context Pointer to the SubBruteDevice context to be populated.
  115. * @param file_path Path of the file to load data from.
  116. * @return The status of the loading operation.
  117. * - Returns 0 if the loading is successful.
  118. * - Returns a non-zero value if an error occurs during loading.
  119. */
  120. uint8_t subbrute_device_load_from_file(SubBruteDevice* context, const char* file_path);
  121. /**
  122. * @brief Adds a step to the SubBruteDevice instance.
  123. *
  124. * This function adds a step to the given SubBruteDevice instance. It updates
  125. * the internal state of the device accordingly.
  126. *
  127. * @param[in,out] instance The SubBruteDevice instance.
  128. * @param[in] step The step to be added.
  129. *
  130. * @return The updated value of the device state after adding the step.
  131. */
  132. uint64_t subbrute_device_add_step(SubBruteDevice* instance, int8_t step);
  133. /**
  134. * @brief Frees the memory allocated for the protocol information of a SubBruteDevice instance.
  135. *
  136. * This function will deallocate the memory used by the protocol information of a SubBruteDevice instance,
  137. * including all the individual protocol objects and their data.
  138. *
  139. * @param instance Pointer to the SubBruteDevice instance.
  140. */
  141. void subbrute_device_free_protocol_info(SubBruteDevice* instance);
  142. /**
  143. * @brief Set the default values for a SubBruteDevice attack configuration.
  144. *
  145. * This function sets the default values for a SubBruteDevice attack configuration based on the specified default_attack type.
  146. *
  147. * @param context Pointer to a SubBruteDevice instance.
  148. * @param default_attack The default attack type to use.
  149. *
  150. * @see SubBruteDevice
  151. */
  152. void subbrute_device_attack_set_default_values(
  153. SubBruteDevice* context,
  154. SubBruteAttacks default_attack);