123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <nfc/nfc.h>
- #include <freefare.h>
- MifareClassicKey SecretKey[] = { 0x00, 0x01, 0x31, 0xB9, 0x3F, 0x28 };
- MifareClassicBlock data_read;
- void show_menu(){
- printf("How it works? So easy !\n");
- printf(" --read\tRead credits and date\n");
- printf(" --write\tWrite <credit> <buy_date> <expire_date>\n");
- printf("\t\tEx: ./poolset --write 10 25/12/16 25/06/16\n\n");
- exit(0);
- }
- void rw_tag(int coins, char *buy_date, char *expire_date) {
-
- nfc_context *context; //init libnfc
- nfc_device *pnd; //trouver un lecteur
- MifareTag *tags = NULL; //stockage des tags
- int i, nbsect;
- nfc_init(&context);
- if(context == NULL) {
- printf("[x] Unable to init libnf\n");
- exit(EXIT_FAILURE);
- }
- pnd = nfc_open(context, NULL);
- if (pnd == NULL) {
- printf("[x] Unable to open NFC device\n");
- exit(EXIT_FAILURE);
- }
- printf("NFC reader: %s opened\n", nfc_device_get_name(pnd));
-
- //tags = freefare_get_tags(pnd);
- while(!tags) {
- printf("Waiting for Mifare 1k...\n");
- sleep(1);
- tags = freefare_get_tags(pnd);
- }
-
- for(i=0; tags[i]; i++) {
- switch(freefare_get_tag_type(tags[i])) {
- case CLASSIC_1K:
- printf("=> Tag number %u with UID %s type Mifare 1k (S50) found !\n", i, freefare_get_tag_uid(tags[i]) );
- 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]);
- if(mifare_classic_connect(tags[i]) == OPERATION_OK) {
- printf("=> Mifare tag connected !\n");
- }
- else printf("Non ca chie dans la colle mais grave, ton tag est moisi\n");
-
- if(mifare_classic_authenticate(tags[i], 5, SecretKey[0], MFC_KEY_A) == OPERATION_OK) {
- printf("=> Tag authenticated !\n");
- }
- else printf("Non ca chie dans la colle mais grave, check ta clé\n");
- if(coins != 0 && buy_date != NULL && expire_date != NULL) {
- char *token;
- token = strtok (buy_date,"/");
- int j=0;
- while (token != NULL) {
- printf ("%s ", token);
- data_read[j] = atoi(token);
- j++;
- token = strtok (NULL, "/");
- }
- token = strtok (expire_date, "/");
- while (token != NULL) {
- printf ("%s ", token);
- data_read[j] = atoi(token);
- j++;
- token = strtok (NULL, "/");
- }
- data_read[6] = 0;
- data_read[7] = coins;
- printf("=> Writing new datas...\n");
- if(mifare_classic_write(tags[i], 5, &data_read) == OPERATION_OK) {
- if(mifare_classic_read(tags[i], 5, &data_read) == OPERATION_OK) {
- printf("=> %.2d credits found now !\n", data_read[7]);
- }
- }
- }
- if(mifare_classic_read(tags[i], 5, &data_read) == OPERATION_OK) {
- printf(" => Infos\n");
- printf(" => credits :\t\t%.2d\n", data_read[7]);
- printf(" => buy date :\t\t%.2d/%.2d/%.2d\n", data_read[0], data_read[1], data_read[2]);
- printf(" => expire date :\t%.2d/%.2d/%.2d\n", data_read[3], data_read[4], data_read[5]);
- }
- mifare_classic_disconnect(tags[i]);
- break;
- default:
- printf("[x] Wrong tag Mr Dugland !");
- break;
- }
- }
- freefare_free_tags(tags);
- nfc_close(pnd);
- nfc_exit(context);
- exit(EXIT_SUCCESS);
- }
- int main(int argc, const char **argv) {
- printf("=========================\n");
- printf("RFID Poolset Read/Write !\n");
- printf("=========================\n");
- if(argc == 1)
- show_menu();
- //else coins = atoi(argv[1]);
- if (strcmp(argv[1], "--read") == 0)
- rw_tag(NULL, NULL, NULL);
-
- if (strcmp(argv[1], "--write") == 0)
- rw_tag(atoi(argv[2]), argv[3], argv[4]);
- }
|