|  | @@ -0,0 +1,89 @@
 | 
	
		
			
				|  |  | +#include <stdlib.h>
 | 
	
		
			
				|  |  | +#include <string.h>
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +#include <nfc/nfc.h>
 | 
	
		
			
				|  |  | +#include <freefare.h>
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +MifareClassicKey SecretKey[] = { 0x00, 0x01, 0x31, 0xB9, 0x3F, 0x28 };
 | 
	
		
			
				|  |  | +MifareClassicBlock data_read;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +int main(int argc, const char *argv[]) {
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	nfc_context *context;		//init libnfc
 | 
	
		
			
				|  |  | +	nfc_device *pnd;			//trouver un lecteur
 | 
	
		
			
				|  |  | +	MifareTag *tags = NULL;		//stockage des tags
 | 
	
		
			
				|  |  | +	int i, nbsect, coins;
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	printf("-=- RFID Poolset -=-\n");
 | 
	
		
			
				|  |  | +	if(argc == 1) {
 | 
	
		
			
				|  |  | +		printf("No arguments found, read only mode...\n");
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	else coins = atoi(argv[1]);
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +	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(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]);
 | 
	
		
			
				|  |  | +				} 
 | 
	
		
			
				|  |  | +	
 | 
	
		
			
				|  |  | +				if(argc > 1) {
 | 
	
		
			
				|  |  | +					data_read[7] = coins;
 | 
	
		
			
				|  |  | +					printf("=> Writing new amount...\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]);
 | 
	
		
			
				|  |  | +						} 
 | 
	
		
			
				|  |  | +					}
 | 
	
		
			
				|  |  | +				} 
 | 
	
		
			
				|  |  | +				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);
 | 
	
		
			
				|  |  | +}
 |