subbrute_worker.h 11 KB

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