Blame view

nfc.c 2.93 KB
3bdc9daf   henyxia   NFC Added
1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <stdio.h>
  #include <stdlib.h>
  #include <termios.h>
  #include <unistd.h>
  #include <errno.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>
  #include <strings.h>
  #include <inttypes.h>
  #include "ui.h"
  #include "nfc.h"
  #include "printx.h"
9e9f365e   henyxia   Added HVC interface
14
  #include "serial.h"
3bdc9daf   henyxia   NFC Added
15
16
17
18
  
  #define	DEVICE1				"/dev/ttyACM0"
  #define	SPEED				B19200
  #define NFC_POLLING_TIME	500000
3bdc9daf   henyxia   NFC Added
19
20
21
22
23
24
25
26
27
28
  #define	NFC_TAG_LENGTH		9
  #define	GET_DEVICE_MODEL	'A'
  #define	DEVICE_MODEL_NFC	'B'
  #define	GET_NFC_STATUS		'C'
  #define	NO_TAG_DETECTED		'D'
  #define	PROFESSOR_TAG		'E'
  #define	STUDENT_TAG			'F'
  #define	GET_TAG_DETAILS		'H'
  
  // Globals
9e9f365e   henyxia   Added HVC interface
29
  int		nfc_fd = -1;
d4d5bb6d   henyxia   Bus working
30
  bool	nfcStop = false;
3bdc9daf   henyxia   NFC Added
31
32
  struct	termios saveterm;
  
3bdc9daf   henyxia   NFC Added
33
34
35
36
  bool initNFC()
  {
  	unsigned char data;
  	printx(DEBUG, NFC, "Connecting to interface 1\n");
9e9f365e   henyxia   Added HVC interface
37
  	if(init_serial(DEVICE1, SPEED, &nfc_fd, &saveterm))
3bdc9daf   henyxia   NFC Added
38
39
  	{
  		printx(DEBUG, NFC, "Connected to an interface\n");
9e9f365e   henyxia   Added HVC interface
40
  		sendData(&nfc_fd, GET_DEVICE_MODEL);
3bdc9daf   henyxia   NFC Added
41
  		printx(DEBUG, NFC, "Sended identification request %02x\n", GET_DEVICE_MODEL);
9e9f365e   henyxia   Added HVC interface
42
  		data = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
43
44
45
46
47
48
49
50
51
52
53
54
55
56
  		if(data == DEVICE_MODEL_NFC)
  			printx(INFO, NFC, "NFC Arduino connected\n");
  		else
  			printx(DEBUG, NFC, "NFC Arduino not found, response was %02x\n", data);
  	}
  	else
  	{
  		printx(WARNING, NFC, "Unable to connect to such interface\n");
  		return false;
  	}
  
  	return true;
  }
  
9e9f365e   henyxia   Added HVC interface
57
58
59
60
61
62
  void stopNFC()
  {
  	close_serial(&nfc_fd, &saveterm);
  	nfcStop = true;
  }
  
3bdc9daf   henyxia   NFC Added
63
64
65
  bool isTagPresent(char* tag)
  {
  	unsigned char data;
9e9f365e   henyxia   Added HVC interface
66
67
  	sendData(&nfc_fd, GET_NFC_STATUS);
  	data = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
68
69
  	if(data == PROFESSOR_TAG)
  	{
9e9f365e   henyxia   Added HVC interface
70
  		sendData(&nfc_fd, GET_TAG_DETAILS);
8dca7aeb   henyxia   Automatic Pumping...
71
  		printx(INFO, NFC, "Professor Tag Detected %02x\n", data);
9e9f365e   henyxia   Added HVC interface
72
  		//return (read(serial_fd, tag, 6) == 6);
3bdc9daf   henyxia   NFC Added
73
74
75
  	}
  	else if(data == STUDENT_TAG)
  	{
9e9f365e   henyxia   Added HVC interface
76
  		sendData(&nfc_fd, GET_TAG_DETAILS);
3bdc9daf   henyxia   NFC Added
77
  		printx(INFO, NFC, "Student Tag Detected\n");
9e9f365e   henyxia   Added HVC interface
78
  		tag[0] = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
79
  		printx(DEBUG, NFC, "Last read %02x\n", tag[0]);
9e9f365e   henyxia   Added HVC interface
80
  		tag[1] = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
81
  		printx(DEBUG, NFC, "Last read %02x\n", tag[1]);
9e9f365e   henyxia   Added HVC interface
82
  		tag[2] = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
83
  		printx(DEBUG, NFC, "Last read %02x\n", tag[2]);
9e9f365e   henyxia   Added HVC interface
84
  		tag[3] = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
85
  		printx(DEBUG, NFC, "Last read %02x\n", tag[3]);
9e9f365e   henyxia   Added HVC interface
86
  		tag[4] = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
87
  		printx(DEBUG, NFC, "Last read %02x\n", tag[4]);
9e9f365e   henyxia   Added HVC interface
88
  		tag[5] = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
89
  		printx(DEBUG, NFC, "Last read %02x\n", tag[5]);
9e9f365e   henyxia   Added HVC interface
90
  		tag[6] = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
91
  		printx(DEBUG, NFC, "Last read %02x\n", tag[6]);
9e9f365e   henyxia   Added HVC interface
92
  		tag[7] = getData(&nfc_fd);
3bdc9daf   henyxia   NFC Added
93
94
95
96
97
98
99
100
101
102
103
104
105
  		printx(DEBUG, NFC, "Last read %02x\n", tag[7]);
  		return true;
  	}
  	else if(data == NO_TAG_DETECTED)
  	{
  		printx(DEBUG, NFC, "No Tag Detected\n");
  		return false;
  	}
  	else
  	{
  		printx(DEBUG, NFC, "Read data did not matched with expected data %02x\n", data);
  		return false;
  	}
9e9f365e   henyxia   Added HVC interface
106
  	return false;
3bdc9daf   henyxia   NFC Added
107
108
109
110
111
112
113
  }
  
  void* processNFC(void* we)
  {
  	char myTag[NFC_TAG_LENGTH];
  	char buffer[NFC_TAG_LENGTH];
  
d4d5bb6d   henyxia   Bus working
114
  	while(!nfcStop)
3bdc9daf   henyxia   NFC Added
115
116
117
118
119
120
121
122
123
124
125
126
127
128
  	{
  		printx(DEBUG, NFC, "Scanning for tags\n");
  		if(isTagPresent(buffer))
  		{
  			sprintf(myTag, "%s", buffer);
  			printx(DEBUG, NFC, "Want to send %s as new tag\n", myTag);
  			setTagName(myTag);
  		}
  
  		usleep(NFC_POLLING_TIME);
  	}
  
  	return NULL;
  }