subbrute_worker.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. #pragma once
  2. #include "../subbrute_protocols.h"
  3. #include "subbrute_radio_device_loader.h"
  4. /**
  5. * @enum SubBruteWorkerState
  6. * @brief Enumeration defining the different states of the SubBruteWorker.
  7. *
  8. * This enumeration defines the possible states of the SubBruteWorker, which is responsible for performing
  9. * sub-brute forcing tasks.
  10. *
  11. * @var SubBruteWorkerStateIDLE The worker is in idle state and not performing any task.
  12. * @var SubBruteWorkerStateReady The worker is ready to start a new task.
  13. * @var SubBruteWorkerStateTx The worker is currently performing a sub-brute forcing task.
  14. * @var SubBruteWorkerStateFinished The worker has finished the sub-brute forcing task.
  15. */
  16. typedef enum {
  17. SubBruteWorkerStateIDLE,
  18. SubBruteWorkerStateReady,
  19. SubBruteWorkerStateTx,
  20. SubBruteWorkerStateFinished
  21. } SubBruteWorkerState;
  22. typedef void (*SubBruteWorkerCallback)(void* context, SubBruteWorkerState state);
  23. typedef struct SubBruteWorker SubBruteWorker;
  24. /**
  25. * @brief Allocates memory for a SubBruteWorker object and initializes it with the given radio_device.
  26. *
  27. * This function creates a new SubBruteWorker object by allocating memory for it on the heap and
  28. * initializes it with the provided radio_device. The radio_device parameter must not be NULL.
  29. *
  30. * @param radio_device A pointer to a valid SubGhzDevice object.
  31. * @return A pointer to the newly allocated SubBruteWorker object, or NULL if memory allocation failed.
  32. */
  33. SubBruteWorker* subbrute_worker_alloc(const SubGhzDevice* radio_device);
  34. /**
  35. * @brief Frees the memory associated with the given SubBruteWorker instance.
  36. *
  37. * This function must be called to release the resources allocated to the SubBruteWorker instance.
  38. *
  39. * @param instance Pointer to the SubBruteWorker instance to be freed.
  40. */
  41. void subbrute_worker_free(SubBruteWorker* instance);
  42. /**
  43. * @brief Get the current step of a sub brute force worker.
  44. *
  45. * @param instance A pointer to the SubBruteWorker instance.
  46. * @return The current step of the sub brute force worker as a uint64_t value.
  47. */
  48. uint64_t subbrute_worker_get_step(SubBruteWorker* instance);
  49. /**
  50. * @brief Set the step value for a SubBruteWorker instance.
  51. *
  52. * This function sets the step value for a given SubBruteWorker instance. The step value determines the increment that
  53. * will be used for each iteration in the worker's execution loop.
  54. *
  55. * @param instance Pointer to the SubBruteWorker instance.
  56. * @param step The step value to set.
  57. * @return true if the step value was successfully set, false otherwise.
  58. */
  59. bool subbrute_worker_set_step(SubBruteWorker* instance, uint64_t step);
  60. /**
  61. * @brief Check if the SubBruteWorker is currently running.
  62. *
  63. * @param instance Pointer to the SubBruteWorker instance.
  64. *
  65. * @return true if the SubBruteWorker is running, false otherwise.
  66. */
  67. bool subbrute_worker_is_running(SubBruteWorker* instance);
  68. /**
  69. * @brief Initializes a SubBruteWorker with default attack settings.
  70. *
  71. * This function initializes a SubBruteWorker instance with default attack settings. The attack_type parameter determines the type of attack to be performed. The step parameter specifies
  72. * the iteration step to be used during the attack. The protocol parameter provides the necessary protocol information for the attack. The repeats parameter specifies the number of times
  73. * the attack should be repeated.
  74. *
  75. * @param instance A pointer to a SubBruteWorker instance.
  76. * @param attack_type The type of attack to be performed.
  77. * @param step The iteration step to be used during the attack.
  78. * @param protocol A pointer to the SubBruteProtocol structure containing protocol information.
  79. * @param repeats The number of times the attack should be repeated.
  80. * @return Returns true if the initialization is successful, false otherwise.
  81. */
  82. bool subbrute_worker_init_default_attack(
  83. SubBruteWorker* instance,
  84. SubBruteAttacks attack_type,
  85. uint64_t step,
  86. const SubBruteProtocol* protocol,
  87. uint8_t repeats);
  88. /**
  89. * @brief Initializes a file-based attack worker for the sub-brute algorithm.
  90. *
  91. * This function initializes a sub-brute worker to perform file-based attacks.
  92. * It sets the necessary parameters for the attack, such as the attack step,
  93. * the load index, the file key, the protocol to use, the number of repeats,
  94. * and whether to use two bytes.
  95. *
  96. * @param instance A pointer to the sub-brute worker instance.
  97. * @param step The attack step value to set.
  98. * @param load_index The load index value to set.
  99. * @param file_key The file key value to set.
  100. * @param protocol The sub-brute protocol to use for the attack.
  101. * @param repeats The number of times to repeat the attack.
  102. * @param two_bytes A flag indicating whether to use two bytes for the attack.
  103. *
  104. * @return True if the initialization was successful, false otherwise.
  105. */
  106. bool subbrute_worker_init_file_attack(
  107. SubBruteWorker* instance,
  108. uint64_t step,
  109. uint8_t load_index,
  110. uint64_t file_key,
  111. SubBruteProtocol* protocol,
  112. uint8_t repeats,
  113. bool two_bytes);
  114. /**
  115. * @brief Start the SubBruteWorker instance.
  116. *
  117. * This function starts the SubBruteWorker instance, allowing it to begin its work.
  118. *
  119. * @param instance Pointer to the SubBruteWorker instance to start.
  120. * @return Whether starting the SubBruteWorker instance was successful.
  121. * - true: Starting the SubBruteWorker instance was successful.
  122. * - false: Starting the SubBruteWorker instance failed.
  123. *
  124. * @note Before calling this function, make sure all the necessary inputs and configurations
  125. * have been set on the SubBruteWorker instance.
  126. */
  127. bool subbrute_worker_start(SubBruteWorker* instance);
  128. /**
  129. * @brief Stops the given SubBruteWorker instance.
  130. *
  131. * This function stops the SubBruteWorker instance by performing necessary clean-up operations.
  132. * After calling this function, the instance is no longer usable.
  133. *
  134. * @param instance A pointer to the SubBruteWorker instance to stop.
  135. */
  136. void subbrute_worker_stop(SubBruteWorker* instance);
  137. /**
  138. * @brief Transmits the current key of the SubBruteWorker instance to another device.
  139. *
  140. * This function transmits the current key of the SubBruteWorker instance to another device
  141. * using the specified step value.
  142. *
  143. * @param instance The pointer to the SubBruteWorker instance.
  144. * @param step The step value used for transmission.
  145. *
  146. * @return True if the key was successfully transmitted, otherwise false.
  147. */
  148. bool subbrute_worker_transmit_current_key(SubBruteWorker* instance, uint64_t step);
  149. /**
  150. * @brief Check if the `SubBruteWorker` instance can transmit manually.
  151. *
  152. * This function is used to determine if the `SubBruteWorker` instance is capable of manual transmission.
  153. *
  154. * @param instance Pointer to the `SubBruteWorker` instance.
  155. * @return `true` if the `SubBruteWorker` instance can transmit manually, `false` otherwise.
  156. */
  157. bool subbrute_worker_can_manual_transmit(SubBruteWorker* instance);
  158. /**
  159. * @brief Set the callback function and its context for the SubBruteWorker.
  160. *
  161. * This function allows you to set the callback function and its context for
  162. * the given SubBruteWorker instance. The callback function will be called
  163. * by the SubBruteWorker at specific events, providing information to the
  164. * caller through the callback parameters.
  165. *
  166. * @param instance The SubBruteWorker instance to set the callback for.
  167. * @param callback The callback function to set.
  168. * @param context The context to be passed to the callback function.
  169. *
  170. * @note The callback function should be of the form:
  171. * void callback(SubBruteWorker* instance, void* context);
  172. * The instance parameter is the SubBruteWorker instance that triggered
  173. * the callback, while the context parameter is the user-defined context
  174. * that was set with this function.
  175. *
  176. * @see SubBruteWorkerCallback
  177. */
  178. void subbrute_worker_set_callback(
  179. SubBruteWorker* instance,
  180. SubBruteWorkerCallback callback,
  181. void* context);
  182. /**
  183. * @brief Get the timeout value of the SubBruteWorker instance.
  184. *
  185. * This function returns the timeout value of the SubBruteWorker instance.
  186. *
  187. * @param instance Pointer to the SubBruteWorker instance.
  188. *
  189. * @return The timeout value as a uint8_t.
  190. */
  191. uint8_t subbrute_worker_get_timeout(SubBruteWorker* instance);
  192. /**
  193. * @brief Set the timeout for the SubBruteWorker instance.
  194. *
  195. * This function sets the timeout value for the SubBruteWorker instance. The timeout defines the maximum time allowed for the worker to complete its task before it is interrupted.
  196. *
  197. * @param instance A pointer to the SubBruteWorker instance.
  198. * @param timeout The timeout value in milliseconds.
  199. */
  200. void subbrute_worker_set_timeout(SubBruteWorker* instance, uint8_t timeout);
  201. /**
  202. * @brief Retrieves the number of repeated substrings found by the SubBruteWorker instance.
  203. *
  204. * This function returns the count of repeated substrings found during the execution of the SubBruteWorker instance.
  205. *
  206. * @param instance A pointer to the SubBruteWorker instance.
  207. * @return The number of repeated substrings.
  208. */
  209. uint8_t subbrute_worker_get_repeats(SubBruteWorker* instance);
  210. /**
  211. * @brief Set the number of repeats for the SubBruteWorker instance.
  212. *
  213. * This function sets the number of repeats for the SubBruteWorker instance. The repeats
  214. * determines how many times a certain operation should be repeated.
  215. *
  216. * @param instance A pointer to the SubBruteWorker instance.
  217. * @param repeats The number of repeats to be set.
  218. */
  219. void subbrute_worker_set_repeats(SubBruteWorker* instance, uint8_t repeats);
  220. /**
  221. * @brief Get the value of te from a SubBruteWorker instance.
  222. *
  223. * This function returns the value of the te member variable of the SubBruteWorker instance.
  224. *
  225. * @param instance A pointer to the SubBruteWorker instance.
  226. * @return The value of the te member variable.
  227. */
  228. uint32_t subbrute_worker_get_te(SubBruteWorker* instance);
  229. /**
  230. * @brief Set the value of te for the SubBruteWorker instance.
  231. *
  232. * This function sets the value of the te member variable for the given SubBruteWorker instance.
  233. * The te value determines the threshold for the worker to stop processing.
  234. *
  235. * @param instance Pointer to the SubBruteWorker instance
  236. * @param te The threshold value to set
  237. */
  238. void subbrute_worker_set_te(SubBruteWorker* instance, uint32_t te);
  239. // void subbrute_worker_timeout_inc(SubBruteWorker* instance);
  240. // void subbrute_worker_timeout_dec(SubBruteWorker* instance);
  241. /**
  242. * @brief Checks if transmission is allowed for the given value.
  243. *
  244. * This function checks the transmission allowance for the given value based on the state of the SubBruteWorker instance.
  245. *
  246. * @param instance A pointer to the SubBruteWorker instance.
  247. * @param value The value to check transmission allowance for.
  248. *
  249. * @return True if transmission is allowed for the given value, false otherwise.
  250. */
  251. bool subbrute_worker_is_tx_allowed(SubBruteWorker* instance, uint32_t value);