gen4_poller_i.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. #include "gen4_poller_i.h"
  2. #include <nfc/protocols/iso14443_3a/iso14443_3a_poller.h>
  3. #include <nfc/helpers/nfc_util.h>
  4. #define GEN4_CMD_PREFIX (0xCF)
  5. #define GEN4_CMD_GET_CFG (0xC6)
  6. #define GEN4_CMD_WRITE (0xCD)
  7. #define GEN4_CMD_READ (0xCE)
  8. #define GEN4_CMD_SET_CFG (0xF0)
  9. #define GEN4_CMD_FUSE_CFG (0xF1)
  10. #define GEN4_CMD_SET_PWD (0xFE)
  11. static Gen4PollerError gen4_poller_process_error(Iso14443_3aError error) {
  12. Gen4PollerError ret = Gen4PollerErrorNone;
  13. if(error == Iso14443_3aErrorNone) {
  14. ret = Gen4PollerErrorNone;
  15. } else {
  16. ret = Gen4PollerErrorTimeout;
  17. }
  18. return ret;
  19. }
  20. Gen4PollerError gen4_poller_set_config(
  21. Gen4Poller* instance,
  22. uint32_t password,
  23. const uint8_t* config,
  24. size_t config_size,
  25. bool fuse) {
  26. Gen4PollerError ret = Gen4PollerErrorNone;
  27. bit_buffer_reset(instance->tx_buffer);
  28. do {
  29. uint8_t password_arr[4] = {};
  30. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  31. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  32. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  33. uint8_t fuse_config = fuse ? GEN4_CMD_FUSE_CFG : GEN4_CMD_SET_CFG;
  34. bit_buffer_append_byte(instance->tx_buffer, fuse_config);
  35. bit_buffer_append_bytes(instance->tx_buffer, config, config_size);
  36. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  37. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  38. if(error != Iso14443_3aErrorNone) {
  39. ret = gen4_poller_process_error(error);
  40. break;
  41. }
  42. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  43. if(rx_bytes != 2) {
  44. ret = Gen4PollerErrorProtocol;
  45. break;
  46. }
  47. } while(false);
  48. return ret;
  49. }
  50. Gen4PollerError gen4_poller_write_block(
  51. Gen4Poller* instance,
  52. uint32_t password,
  53. uint8_t block_num,
  54. const uint8_t* data) {
  55. Gen4PollerError ret = Gen4PollerErrorNone;
  56. bit_buffer_reset(instance->tx_buffer);
  57. do {
  58. uint8_t password_arr[4] = {};
  59. nfc_util_num2bytes(password, COUNT_OF(password_arr), password_arr);
  60. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  61. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  62. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_WRITE);
  63. bit_buffer_append_byte(instance->tx_buffer, block_num);
  64. bit_buffer_append_bytes(instance->tx_buffer, data, GEN4_POLLER_BLOCK_SIZE);
  65. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  66. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  67. if(error != Iso14443_3aErrorNone) {
  68. ret = gen4_poller_process_error(error);
  69. break;
  70. }
  71. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  72. if(rx_bytes != 2) {
  73. ret = Gen4PollerErrorProtocol;
  74. break;
  75. }
  76. } while(false);
  77. return ret;
  78. }
  79. Gen4PollerError
  80. gen4_poller_change_password(Gen4Poller* instance, uint32_t pwd_current, uint32_t pwd_new) {
  81. Gen4PollerError ret = Gen4PollerErrorNone;
  82. bit_buffer_reset(instance->tx_buffer);
  83. do {
  84. uint8_t password_arr[4] = {};
  85. nfc_util_num2bytes(pwd_current, COUNT_OF(password_arr), password_arr);
  86. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_PREFIX);
  87. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  88. bit_buffer_append_byte(instance->tx_buffer, GEN4_CMD_SET_PWD);
  89. nfc_util_num2bytes(pwd_new, COUNT_OF(password_arr), password_arr);
  90. bit_buffer_append_bytes(instance->tx_buffer, password_arr, COUNT_OF(password_arr));
  91. Iso14443_3aError error = iso14443_3a_poller_send_standard_frame(
  92. instance->iso3_poller, instance->tx_buffer, instance->rx_buffer, GEN4_POLLER_MAX_FWT);
  93. if(error != Iso14443_3aErrorNone) {
  94. ret = gen4_poller_process_error(error);
  95. break;
  96. }
  97. size_t rx_bytes = bit_buffer_get_size_bytes(instance->rx_buffer);
  98. if(rx_bytes != 2) {
  99. ret = Gen4PollerErrorProtocol;
  100. break;
  101. }
  102. } while(false);
  103. return ret;
  104. }