token_info_iterator.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. #pragma once
  2. #include "../../types/token_info.h"
  3. #include <flipper_format/flipper_format.h>
  4. #include "constants.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. typedef int TotpIteratorUpdateTokenResult;
  9. typedef TotpIteratorUpdateTokenResult (
  10. *TOTP_ITERATOR_UPDATE_TOKEN_ACTION)(TokenInfo* const token_info, const void* context);
  11. typedef struct TokenInfoIteratorContext TokenInfoIteratorContext;
  12. enum TotpIteratorUpdateTokenResults {
  13. /**
  14. * @brief Token successfully updated
  15. */
  16. TotpIteratorUpdateTokenResultSuccess = 0,
  17. /**
  18. * @brief An error ocurred during updating config file
  19. */
  20. TotpIteratorUpdateTokenResultFileUpdateFailed = -1
  21. };
  22. /**
  23. * @brief Initializes a new token info iterator
  24. * @param storage storage reference
  25. * @param config_file config file to use
  26. * @param crypto_settings crypto settings
  27. * @return Token info iterator context
  28. */
  29. TokenInfoIteratorContext* totp_token_info_iterator_alloc(
  30. Storage* storage,
  31. FlipperFormat* config_file,
  32. CryptoSettings* crypto_settings);
  33. /**
  34. * @brief Navigates iterator to the token with given index
  35. * @param context token info iterator context
  36. * @param token_index token index to navigate to
  37. * @return \c true if navigation succeeded; \c false otherwise
  38. */
  39. bool totp_token_info_iterator_go_to(TokenInfoIteratorContext* context, size_t token_index);
  40. /**
  41. * @brief Moves current token to a given new index
  42. * @param context token info iterator context
  43. * @param new_index new token index to move current token to
  44. * @return \c true if operation succeeded; \c false otherwise
  45. */
  46. bool totp_token_info_iterator_move_current_token_info(
  47. TokenInfoIteratorContext* context,
  48. size_t new_index);
  49. /**
  50. * @brief Updates current token info using given update action
  51. * @param context token info iterator context
  52. * @param update action which is responsible to make all the necessary updates to token info
  53. * @param update_context update action context
  54. * @return \c true if operation succeeded; \c false otherwise
  55. */
  56. TotpIteratorUpdateTokenResult totp_token_info_iterator_update_current_token(
  57. TokenInfoIteratorContext* context,
  58. TOTP_ITERATOR_UPDATE_TOKEN_ACTION update,
  59. const void* update_context);
  60. /**
  61. * @brief Increments token counter
  62. * @param context token info iterator context
  63. * @return \c true if operation succeeded; \c false otherwise
  64. */
  65. TotpIteratorUpdateTokenResult
  66. totp_token_info_iterator_current_token_inc_counter(TokenInfoIteratorContext* context);
  67. /**
  68. * @brief Adds new token info to the end of the list using given update action
  69. * @param context token info iterator context
  70. * @param update action which is responsible to make all the necessary updates to token info
  71. * @param update_context update action context
  72. * @return \c true if operation succeeded; \c false otherwise
  73. */
  74. TotpIteratorUpdateTokenResult totp_token_info_iterator_add_new_token(
  75. TokenInfoIteratorContext* context,
  76. TOTP_ITERATOR_UPDATE_TOKEN_ACTION update,
  77. const void* update_context);
  78. /**
  79. * @brief Remvoves current token info
  80. * @param context token info iterator context
  81. * @return \c true if operation succeeded; \c false otherwise
  82. */
  83. bool totp_token_info_iterator_remove_current_token_info(TokenInfoIteratorContext* context);
  84. /**
  85. * @brief Disposes token info iterator and releases all the resources
  86. * @param context token info iterator context
  87. */
  88. void totp_token_info_iterator_free(TokenInfoIteratorContext* context);
  89. /**
  90. * @brief Gets current token info
  91. * @param context token info iterator context
  92. * @return current token info
  93. */
  94. const TokenInfo*
  95. totp_token_info_iterator_get_current_token(const TokenInfoIteratorContext* context);
  96. /**
  97. * @brief Gets current token info index
  98. * @param context token info iterator context
  99. * @return current token info index
  100. */
  101. size_t totp_token_info_iterator_get_current_token_index(const TokenInfoIteratorContext* context);
  102. /**
  103. * @brief Gets total amount of token infos found
  104. * @param context token info iterator context
  105. * @return amount of token infos found
  106. */
  107. size_t totp_token_info_iterator_get_total_count(const TokenInfoIteratorContext* context);
  108. /**
  109. * @brief Attaches token info iterator to another config file
  110. * @param context token info iterator context
  111. * @param config_file config file reference to attach token info iterator to
  112. */
  113. void totp_token_info_iterator_attach_to_config_file(
  114. TokenInfoIteratorContext* context,
  115. FlipperFormat* config_file);
  116. #ifdef __cplusplus
  117. }
  118. #endif