poolset.c 3.5 KB

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