poolset.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <nfc/nfc.h>
  4. #include <freefare.h>
  5. MifareClassicKey SecretKey[] = { 0x00, 0x01, 0x31, 0xB9, 0x3F, 0x28 };
  6. MifareClassicBlock data_read;
  7. void show_menu(){
  8. printf("How it works? So easy !\n");
  9. printf(" --read\tRead credits and date\n");
  10. printf(" --write\tWrite <credit> <buy_date> <expire_date>\n");
  11. printf("\t\tEx: ./poolset --write 10 25/12/16 25/06/16\n\n");
  12. exit(0);
  13. }
  14. void read_tag(int coins, const char *buy_date, const char *expire_date) {
  15. nfc_context *context; //init libnfc
  16. nfc_device *pnd; //trouver un lecteur
  17. MifareTag *tags = NULL; //stockage des tags
  18. int i, nbsect;
  19. nfc_init(&context);
  20. if(context == NULL) {
  21. printf("[x] Unable to init libnf\n");
  22. exit(EXIT_FAILURE);
  23. }
  24. pnd = nfc_open(context, NULL);
  25. if (pnd == NULL) {
  26. printf("[x] Unable to open NFC device\n");
  27. exit(EXIT_FAILURE);
  28. }
  29. printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
  30. //tags = freefare_get_tags(pnd);
  31. while(!tags) {
  32. printf("Waiting for Mifare 1k...\n");
  33. sleep(1);
  34. tags = freefare_get_tags(pnd);
  35. }
  36. for(i=0; tags[i]; i++) {
  37. switch(freefare_get_tag_type(tags[i])) {
  38. case CLASSIC_1K:
  39. printf("=> Tag number %u with UID %s type Mifare 1k (S50) found !\n", i, freefare_get_tag_uid(tags[i]) );
  40. printf("=> Trying to authenticate with SecretKey %.2x %.2x %.2x %.2x %.2x %.2x ...\n", SecretKey[0][0], SecretKey[0][1], SecretKey[0][2], SecretKey[0][3], SecretKey[0][4], SecretKey[0][5]);
  41. if(mifare_classic_connect(tags[i]) == OPERATION_OK) {
  42. printf("=> Mifare tag connected !\n");
  43. }
  44. else printf("Non ca chie dans la colle mais grave, ton tag est moisi\n");
  45. if(mifare_classic_authenticate(tags[i], 5, SecretKey[0], MFC_KEY_A) == OPERATION_OK) {
  46. printf("=> Tag authenticated !\n");
  47. }
  48. else printf("Non ca chie dans la colle mais grave, check ta clé\n");
  49. if(coins != NULL && buy_date != NULL && expire_date != NULL) {
  50. char *token;
  51. token = strtok (buy_date,"/");
  52. int j=0;
  53. while (token != NULL) {
  54. printf ("%s ", token);
  55. data_read[j] = atoi(token);
  56. j++;
  57. token = strtok (NULL, "/");
  58. }
  59. token = strtok (expire_date, "/");
  60. while (token != NULL) {
  61. printf ("%s ", token);
  62. data_read[j] = atoi(token);
  63. j++;
  64. token = strtok (NULL, "/");
  65. }
  66. data_read[6] = 0;
  67. data_read[7] = coins;
  68. printf("=> Writing new datas...\n");
  69. if(mifare_classic_write(tags[i], 5, &data_read) == OPERATION_OK) {
  70. if(mifare_classic_read(tags[i], 5, &data_read) == OPERATION_OK) {
  71. printf("=> %.2d credits found now !\n", data_read[7]);
  72. }
  73. }
  74. }
  75. if(mifare_classic_read(tags[i], 5, &data_read) == OPERATION_OK) {
  76. printf(" => Infos\n");
  77. printf(" => credits :\t\t%.2d\n", data_read[7]);
  78. printf(" => buy date :\t\t%.2d/%.2d/%.2d\n", data_read[0], data_read[1], data_read[2]);
  79. printf(" => expire date :\t%.2d/%.2d/%.2d\n", data_read[3], data_read[4], data_read[5]);
  80. }
  81. mifare_classic_disconnect(tags[i]);
  82. break;
  83. default:
  84. printf("[x] Wrong tag Mr Dugland !");
  85. break;
  86. }
  87. }
  88. freefare_free_tags(tags);
  89. nfc_close(pnd);
  90. nfc_exit(context);
  91. exit(EXIT_SUCCESS);
  92. }
  93. int main(int argc, const char **argv) {
  94. printf("=========================\n");
  95. printf("RFID Poolset Read/Write !\n");
  96. printf("=========================\n");
  97. if(argc == 1)
  98. show_menu();
  99. //else coins = atoi(argv[1]);
  100. if (strcmp(argv[1], "--read") == 0)
  101. read_tag(NULL, NULL, NULL);
  102. if (strcmp(argv[1], "--write") == 0)
  103. read_tag(atoi(argv[2]), argv[3], argv[4]);
  104. }