nfclib 0.0.128
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +4 -0
- data/ext/extconf.rb +12 -0
- data/ext/nfclib.c +467 -0
- data/ext/nfclib.h +43 -0
- data/lib/nfc.rb +112 -0
- data/nfclib.gemspec +14 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9aa76f9d24205d6d1be2c84a17d72f90181b5772
|
4
|
+
data.tar.gz: 1784f373e270807a65f1ed3b28a60181f141e35d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 509197bfc7bf3deaf94dabc5b27eb9b7c271a19f957e6854388f5fd9c76c56885a558165789dc40f96a07501dba60ae7dd3883f4f259c6bacf537d0123fa744a
|
7
|
+
data.tar.gz: 939e608a5a176e1dc364f884691c94029d78cef622bbb1e88a14d15de4e75f417c28099be0238bae0533c8ad9b24e3eb52fdbbad2da069b2d4f6626830875a4e
|
data/README.md
ADDED
data/ext/extconf.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'mkmf'
|
2
|
+
|
3
|
+
spec = eval(File.read(File.dirname(__FILE__) + '/../nfclib.gemspec'))
|
4
|
+
PKG_NAME="nfclib"
|
5
|
+
|
6
|
+
$CFLAGS = '-Wall -pedantic -O3 -c -fPIC ' + $CFLAGS
|
7
|
+
$LDFLAGS = '-pthread ' + $LDFLAGS
|
8
|
+
$LIBPATH.push(RbConfig::CONFIG['libdir'])
|
9
|
+
|
10
|
+
|
11
|
+
create_header()
|
12
|
+
create_makefile(PKG_NAME)
|
data/ext/nfclib.c
ADDED
@@ -0,0 +1,467 @@
|
|
1
|
+
#include <stdio.h>
|
2
|
+
#include <stdlib.h>
|
3
|
+
#include <fcntl.h>
|
4
|
+
#include <termios.h>
|
5
|
+
#include <pthread.h>
|
6
|
+
#include <unistd.h>
|
7
|
+
#include <string.h>
|
8
|
+
#include <ctype.h>
|
9
|
+
#include "nfclib.h"
|
10
|
+
|
11
|
+
void (**nfc_what_happens_if)(nfc_reader* x) = NULL;
|
12
|
+
int what_happens_len;
|
13
|
+
int what_happens_max_len;
|
14
|
+
|
15
|
+
/*nfc_set_cmd # {{{ */
|
16
|
+
int nfc_set_cmd(nfc_reader* x,char* str){
|
17
|
+
|
18
|
+
int str_len = strlen(str);
|
19
|
+
int i = 0;
|
20
|
+
if(!str_len)
|
21
|
+
return 1;
|
22
|
+
for(;i<str_len;i++)
|
23
|
+
x->command[i]=(uint8_t) str[i];
|
24
|
+
x->command_len = i;
|
25
|
+
return 0;
|
26
|
+
}
|
27
|
+
/* # }}}*/
|
28
|
+
|
29
|
+
|
30
|
+
/* expand # {{{ */
|
31
|
+
void dance_the_expanding_dance(nfc_reader *x){
|
32
|
+
|
33
|
+
int i;
|
34
|
+
void(**temp)(nfc_reader* y) = malloc(((x->what_happens_max_len)+2) * sizeof(void (*)(nfc_reader*)));
|
35
|
+
for(i=0;i<(x->what_happens_len);i++){
|
36
|
+
temp[i] = (x->nfc_what_happens_if)[i];
|
37
|
+
}
|
38
|
+
free((x->nfc_what_happens_if));
|
39
|
+
/* fprintf(stdout,"YES, WE DANCED!\n"); */
|
40
|
+
(x->nfc_what_happens_if)=temp;
|
41
|
+
}
|
42
|
+
/* # }}}*/
|
43
|
+
|
44
|
+
/* nfc_reader_on_tag # {{{ */
|
45
|
+
void* nfc_reader_on_tag(nfc_reader* x,void (*y)(nfc_reader*)){
|
46
|
+
if(x->nfc_what_happens_if == NULL){
|
47
|
+
x->what_happens_len = 0;
|
48
|
+
x->what_happens_max_len = 2;
|
49
|
+
x->nfc_what_happens_if = malloc(2 * sizeof(void (*)(nfc_reader*)));
|
50
|
+
}
|
51
|
+
if((x->what_happens_len)==(x->what_happens_max_len)){
|
52
|
+
dance_the_expanding_dance(x);
|
53
|
+
}
|
54
|
+
(x->nfc_what_happens_if)[((x->what_happens_len))++] = y;
|
55
|
+
return NULL;
|
56
|
+
}
|
57
|
+
/* # }}}*/
|
58
|
+
|
59
|
+
/* nfc_reader_init # {{{ */
|
60
|
+
nfc_reader* nfc_reader_init(const char* name, int mem_len) {
|
61
|
+
nfc_reader* xptr;
|
62
|
+
nfc_reader x;
|
63
|
+
int len = strlen(name);
|
64
|
+
int i;
|
65
|
+
struct termios tconfig;
|
66
|
+
x.nfc_port = (char*) malloc(sizeof(char)*(len+1));
|
67
|
+
for(i = 0;i<len;i++)
|
68
|
+
x.nfc_port[i] = name[i];
|
69
|
+
x.nfc_port[i] = '\0';
|
70
|
+
x.fh = open(x.nfc_port,O_RDWR|O_NOCTTY|O_NONBLOCK);
|
71
|
+
if(x.fh==-1)
|
72
|
+
byebye("Could not open a filehandler for writing\n");
|
73
|
+
/* TCONFIG #{{{ */
|
74
|
+
tconfig.c_cc[VTIME] = 3;
|
75
|
+
tconfig.c_cc[VMIN] = 5;
|
76
|
+
/* Tconfig setup */
|
77
|
+
tconfig.c_iflag &= ~(IGNBRK|ISTRIP|IXON);
|
78
|
+
tconfig.c_lflag &= ~(ECHO | ECHONL | ICANON | IEXTEN | ISIG);
|
79
|
+
tconfig.c_cflag &= ~(CSIZE | PARENB);
|
80
|
+
tconfig.c_cflag |= CS8;
|
81
|
+
tconfig.c_oflag &= ~(OPOST);
|
82
|
+
if(cfsetispeed(&tconfig, B115200) < 0 || cfsetospeed(&tconfig, B115200) < 0)
|
83
|
+
fprintf(stderr,"ERROR %s:%d\n",__FILE__,__LINE__);
|
84
|
+
if(tcsetattr(x.fh, TCSANOW, &tconfig) < 0)
|
85
|
+
fprintf(stderr,"ERROR %s:%d\n",__FILE__,__LINE__);
|
86
|
+
/* # }}} */
|
87
|
+
|
88
|
+
x.reddit = (unsigned char*) malloc(sizeof(unsigned char)*100);
|
89
|
+
x.reddit_len = 0;
|
90
|
+
x.command_len = 0;
|
91
|
+
x.what_happens_len = 0;
|
92
|
+
x.what_happens_max_len = 0;
|
93
|
+
x.nfc_what_happens_if=NULL;
|
94
|
+
x.tag_mem_max_len = mem_len;
|
95
|
+
x.tag_mem_len = 0;
|
96
|
+
x.tag_mem_block_len = 4 ;
|
97
|
+
x.tag_mem = (unsigned char*) malloc(sizeof(unsigned char)*(mem_len*((x.tag_mem_block_len)+1))) ;
|
98
|
+
xptr = malloc(sizeof(nfc_reader)*1);
|
99
|
+
*xptr = x;
|
100
|
+
return xptr;
|
101
|
+
}
|
102
|
+
/* # }}}*/
|
103
|
+
|
104
|
+
/* nfc_reader_do # {{{ */
|
105
|
+
void* nfc_reader_do(void* y){
|
106
|
+
nfc_reader* x= y;
|
107
|
+
int write_pos = 0;
|
108
|
+
int i;
|
109
|
+
int j=0;
|
110
|
+
int limit = -1;
|
111
|
+
int len = x->command_len;
|
112
|
+
int read_bytes;
|
113
|
+
int b_read =0;
|
114
|
+
int tries = 0;
|
115
|
+
int max_tries = 2;
|
116
|
+
unsigned char bcc = 0;
|
117
|
+
unsigned char ucmd [50];
|
118
|
+
fd_set set;
|
119
|
+
int written_bytes;
|
120
|
+
struct timeval timeout;
|
121
|
+
timeout.tv_sec=1;
|
122
|
+
timeout.tv_usec=0;
|
123
|
+
for(i=0;i<len;i++){
|
124
|
+
if(isalpha((x->command)[i])){
|
125
|
+
ucmd[j++] = (x->command)[i];
|
126
|
+
}
|
127
|
+
if(isdigit((x->command)[i])){
|
128
|
+
int temp = (x->command)[i++] - '0';
|
129
|
+
while(i<len && isdigit((x->command)[i])){
|
130
|
+
temp *= 10;
|
131
|
+
temp += (x->command)[i++] - '0';
|
132
|
+
}
|
133
|
+
if(temp>255)
|
134
|
+
byebye("Adresse zu groß");
|
135
|
+
ucmd[j++] = temp;
|
136
|
+
if(i==len)
|
137
|
+
break;
|
138
|
+
}
|
139
|
+
}
|
140
|
+
/*CLEAR */
|
141
|
+
written_bytes = write(x->fh,"\0",1);
|
142
|
+
ucmd[j] = '\0';
|
143
|
+
x->write_puffer[write_pos++] = 2;
|
144
|
+
x->write_puffer[write_pos++] = 255;
|
145
|
+
x->write_puffer[write_pos++] = ustrlen(ucmd);
|
146
|
+
for(i=0;i<ustrlen(ucmd);i++)
|
147
|
+
x->write_puffer[write_pos++] = ucmd[i];
|
148
|
+
for(i=1;i<(len)+3;i++)
|
149
|
+
bcc ^= x->write_puffer[i];
|
150
|
+
x->write_puffer[write_pos++] = bcc;
|
151
|
+
x->write_puffer[write_pos++] = 3;
|
152
|
+
/* for(i=0;i<write_pos;i++)
|
153
|
+
fprintf(stderr,"written: %d\n",(x->write_puffer)[i]);
|
154
|
+
*/
|
155
|
+
|
156
|
+
for(tries=0;tries<max_tries;){
|
157
|
+
written_bytes = write(x->fh,x->write_puffer,write_pos);
|
158
|
+
if(written_bytes != write_pos){
|
159
|
+
fprintf(stderr,"Error while writing\n");
|
160
|
+
return y;
|
161
|
+
}
|
162
|
+
j=0;
|
163
|
+
while(1){
|
164
|
+
/* INITIALIZE FD READ SET */
|
165
|
+
FD_ZERO (&set);
|
166
|
+
FD_SET (x->fh,&set);
|
167
|
+
|
168
|
+
if(select(FD_SETSIZE,&set,NULL,NULL,&timeout)>0){
|
169
|
+
if((read_bytes=read(x->fh,x->read_puffer,37))>0){
|
170
|
+
for(i=0;i<read_bytes;i++,j++)
|
171
|
+
(x->reddit)[j] = (x->read_puffer)[i];
|
172
|
+
|
173
|
+
if(j>2 && limit==-1) limit=(x->reddit)[2]+5;
|
174
|
+
b_read+=read_bytes;
|
175
|
+
}
|
176
|
+
if(b_read==limit) {
|
177
|
+
x->reddit_len = b_read;
|
178
|
+
tries=max_tries;
|
179
|
+
break;
|
180
|
+
}
|
181
|
+
if(limit!=-1 && b_read>limit){
|
182
|
+
/*
|
183
|
+
fprintf(stdout,"Error. Read too many bytes\n");
|
184
|
+
*/
|
185
|
+
x->reddit_len = 0;
|
186
|
+
break;
|
187
|
+
}
|
188
|
+
}
|
189
|
+
else{
|
190
|
+
/*
|
191
|
+
int ii= 0;
|
192
|
+
fprintf(stderr,"Read Bytes: %d\n",b_read);
|
193
|
+
fprintf(stderr,"Expected: %d\n",limit);
|
194
|
+
|
195
|
+
for(ii=0;ii<b_read;ii++)
|
196
|
+
fprintf(stderr,"Read: %d\n",(x->reddit)[ii]);
|
197
|
+
fprintf(stdout,"Could not catch the correct length of the answer\n");
|
198
|
+
*/
|
199
|
+
x->reddit_len = 0;
|
200
|
+
break;
|
201
|
+
}
|
202
|
+
}
|
203
|
+
tries++;
|
204
|
+
if(nfc_what_happens_if!=NULL)
|
205
|
+
nfc_what_happens_if[(0)](x);
|
206
|
+
}
|
207
|
+
return y; /*Bessere Returns; */
|
208
|
+
}
|
209
|
+
/* # }}}*/
|
210
|
+
|
211
|
+
/*nfc_reader_poll # {{{ */
|
212
|
+
int nfc_reader_poll(nfc_reader* x){
|
213
|
+
int rc;
|
214
|
+
rc = pthread_create(&(x->polling),NULL,&nfc_reader_read,x);
|
215
|
+
if( rc != 0)
|
216
|
+
byebye("Problem bei Threaderzeugung\n");
|
217
|
+
return 0;
|
218
|
+
}
|
219
|
+
/* # }}}*/
|
220
|
+
|
221
|
+
/* nfc_reader_stop_poll # {{{ */
|
222
|
+
int nfc_reader_stop_poll(nfc_reader* x){
|
223
|
+
int ret = pthread_cancel(x->polling);
|
224
|
+
pthread_join(x->polling,NULL);
|
225
|
+
return ret;
|
226
|
+
}
|
227
|
+
/* # }}}*/
|
228
|
+
|
229
|
+
/* Use in a Thread */
|
230
|
+
/* nfc_reader_read # {{{ */
|
231
|
+
void* nfc_reader_read(void *y){
|
232
|
+
nfc_reader *x = y;
|
233
|
+
int read_bytes;
|
234
|
+
fd_set set;
|
235
|
+
struct timeval timeout;
|
236
|
+
int i,j=0;
|
237
|
+
int limit=-1;
|
238
|
+
int b_read = 0;
|
239
|
+
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE,NULL);
|
240
|
+
while(1){
|
241
|
+
/* INITIALIZE FD READ SET */
|
242
|
+
timeout.tv_sec=1;
|
243
|
+
timeout.tv_usec=0;
|
244
|
+
FD_ZERO (&set);
|
245
|
+
FD_SET (x->fh,&set);
|
246
|
+
if(select(FD_SETSIZE,&set,NULL,NULL,&timeout)>0){
|
247
|
+
if((read_bytes=read(x->fh,x->read_puffer,3))>0){
|
248
|
+
for(i=0;i<read_bytes;i++,j++)
|
249
|
+
(x->reddit)[j] = x->read_puffer[i];
|
250
|
+
b_read += read_bytes;
|
251
|
+
if(limit==-1 && b_read>3)
|
252
|
+
limit = (x->reddit)[2]+5;
|
253
|
+
}
|
254
|
+
if(limit==b_read){
|
255
|
+
int count;
|
256
|
+
x->reddit_len=b_read;
|
257
|
+
if((x->nfc_what_happens_if)!=NULL){
|
258
|
+
for(count=0;count<(x->what_happens_len);count++)
|
259
|
+
(x->nfc_what_happens_if)[(count)](x);
|
260
|
+
}
|
261
|
+
/* nfc_reader_read_mem(x); */
|
262
|
+
j=0;
|
263
|
+
b_read =0;
|
264
|
+
limit=-1;
|
265
|
+
}
|
266
|
+
if(limit!=-1 && b_read>limit){
|
267
|
+
/*
|
268
|
+
fprintf(stdout,"Error. Read too many bytes\n");
|
269
|
+
*/
|
270
|
+
limit=-1;
|
271
|
+
b_read=0;
|
272
|
+
j=0;
|
273
|
+
}
|
274
|
+
}
|
275
|
+
else{
|
276
|
+
if(limit!=-1){
|
277
|
+
int testi = 0;
|
278
|
+
fprintf(stdout,"Seltsam hier zu sein B_Read: %d Limit: %d\n",b_read,limit);
|
279
|
+
for(testi=0;testi<b_read;testi++){
|
280
|
+
fprintf(stdout,"%d\t",(x->reddit)[testi]);
|
281
|
+
}
|
282
|
+
fprintf(stdout,"\n");
|
283
|
+
limit = -1;
|
284
|
+
j=0;
|
285
|
+
b_read =0;
|
286
|
+
}
|
287
|
+
}
|
288
|
+
}
|
289
|
+
}
|
290
|
+
/* # }}}*/
|
291
|
+
/* # {{{ nfc_reader_read_mem */
|
292
|
+
void nfc_reader_read_mem(nfc_reader* x){
|
293
|
+
|
294
|
+
int i,count;
|
295
|
+
unsigned char* tag_mems= x->tag_mem;
|
296
|
+
char str[10];
|
297
|
+
x->tag_mem[0] ='\0';
|
298
|
+
nfc_set_cmd(x,"s");
|
299
|
+
nfc_reader_do(x);
|
300
|
+
nfc_reader_do(x);
|
301
|
+
for(i=4;i<8;i++){
|
302
|
+
/* for(i=4;i<(x->tag_mem_max_len);i++){ */
|
303
|
+
x->reddit_len = 0;
|
304
|
+
while((x->reddit_len)==0){
|
305
|
+
sprintf(str,"r%d",i);
|
306
|
+
nfc_set_cmd(x,str);
|
307
|
+
nfc_reader_do(x);
|
308
|
+
/*
|
309
|
+
fprintf(stdout,"READ_MEM: %d\n",x->reddit_len);
|
310
|
+
*/
|
311
|
+
if((x->reddit_len)==(5+(x->tag_mem_block_len))){
|
312
|
+
int j;
|
313
|
+
int hex;
|
314
|
+
char test[9];
|
315
|
+
char temp[3];
|
316
|
+
char* ptemp = &(temp[0]);
|
317
|
+
char* p_test= &(test[0]);
|
318
|
+
if((x->reddit)[6] == 0) break;
|
319
|
+
for(j=3;j<((x->reddit_len)-2);j++){
|
320
|
+
/* fprintf(stdout,"%02x ",(x->reddit)[j]);
|
321
|
+
*/
|
322
|
+
sprintf(ptemp,"%02x",(x->reddit)[j]);
|
323
|
+
if(j==3){
|
324
|
+
strcpy(p_test,ptemp);
|
325
|
+
if(i==4) strcpy((x->tag_mem),ptemp);
|
326
|
+
else strcat(tag_mems,ptemp);
|
327
|
+
tag_mems+=4;
|
328
|
+
*tag_mems='\0';
|
329
|
+
tag_mems++;
|
330
|
+
}
|
331
|
+
else{
|
332
|
+
strcat(p_test,ptemp);
|
333
|
+
strcat(tag_mems,ptemp);
|
334
|
+
tag_mems+=4;
|
335
|
+
*tag_mems='\0';
|
336
|
+
tag_mems++;
|
337
|
+
}
|
338
|
+
}
|
339
|
+
/*
|
340
|
+
hex = (int)strtol(p_test,NULL,16);
|
341
|
+
fprintf(stdout,"%s\n",p_test);
|
342
|
+
fprintf(stdout,"%d\n",hex);
|
343
|
+
fprintf(stdout,"\n");
|
344
|
+
*/
|
345
|
+
}
|
346
|
+
/*
|
347
|
+
if((x->nfc_what_happens_if)!=NULL){
|
348
|
+
for(count=0;count<(x->what_happens_len);count++)
|
349
|
+
(x->nfc_what_happens_if)[(count)](x);
|
350
|
+
}
|
351
|
+
*/
|
352
|
+
if((x->reddit_len)==0 || (x->reddit_len)==6){
|
353
|
+
nfc_set_cmd(x,"s");
|
354
|
+
nfc_reader_do(x);
|
355
|
+
x->reddit_len = 0;
|
356
|
+
}
|
357
|
+
}
|
358
|
+
if((x->reddit)[6] == 0) break;
|
359
|
+
|
360
|
+
}
|
361
|
+
|
362
|
+
for(i=0;i<(x->tag_mem_max_len);i+=((x->tag_mem_block_len)+1)){
|
363
|
+
if((x->tag_mem)[i]=='\0') break;
|
364
|
+
/*
|
365
|
+
else {
|
366
|
+
fprintf(stdout,"String: %s",(x->tag_mem));
|
367
|
+
fprintf(stdout,"HUCH: %c \n",(x->tag_mem)[5]);
|
368
|
+
fprintf(stdout,"HUCH: %c \n",(x->tag_mem)[10]);
|
369
|
+
fprintf(stdout,"\n");
|
370
|
+
}
|
371
|
+
*/
|
372
|
+
}
|
373
|
+
sleep(1);
|
374
|
+
nfc_set_cmd(x,"dp");
|
375
|
+
nfc_reader_do(x);
|
376
|
+
}
|
377
|
+
/* # }}} */
|
378
|
+
|
379
|
+
/* nfc_reader_1_read # {{{ */
|
380
|
+
void* nfc_reader_1_read(void *y){
|
381
|
+
nfc_reader *x = y;
|
382
|
+
int read_bytes;
|
383
|
+
fd_set set;
|
384
|
+
struct timeval timeout;
|
385
|
+
int i,j=0;
|
386
|
+
int limit=-1;
|
387
|
+
int b_read = 0;
|
388
|
+
while(1){
|
389
|
+
/* INITIALIZE FD READ SET */
|
390
|
+
timeout.tv_sec=1;
|
391
|
+
timeout.tv_usec=0;
|
392
|
+
FD_ZERO (&set);
|
393
|
+
FD_SET (x->fh,&set);
|
394
|
+
if(select(FD_SETSIZE,&set,NULL,NULL,&timeout)>0){
|
395
|
+
if((read_bytes=read(x->fh,x->read_puffer,3))>0){
|
396
|
+
for(i=0;i<read_bytes;i++,j++)
|
397
|
+
(x->reddit)[j] = x->read_puffer[i];
|
398
|
+
b_read += read_bytes;
|
399
|
+
if(limit==-1 && b_read>3)
|
400
|
+
limit = (x->reddit)[2]+5;
|
401
|
+
}
|
402
|
+
if(limit==b_read){
|
403
|
+
int count;
|
404
|
+
x->reddit_len=b_read;
|
405
|
+
if((x->nfc_what_happens_if)!=NULL){
|
406
|
+
for(count=0;count<(x->what_happens_len);count++)
|
407
|
+
(x->nfc_what_happens_if)[(count)](x);
|
408
|
+
}
|
409
|
+
j=0;
|
410
|
+
b_read =0;
|
411
|
+
limit=-1;
|
412
|
+
break;
|
413
|
+
}
|
414
|
+
if(limit!=-1 && b_read>limit){
|
415
|
+
fprintf(stdout,"Error. Read too many bytes\n");
|
416
|
+
limit=-1;
|
417
|
+
b_read=0;
|
418
|
+
j=0;
|
419
|
+
break;
|
420
|
+
}
|
421
|
+
}
|
422
|
+
else{
|
423
|
+
if(limit!=-1){
|
424
|
+
limit = -1;
|
425
|
+
j=0;
|
426
|
+
b_read =0;
|
427
|
+
fprintf(stdout,"Selstam hier zu sein\n");
|
428
|
+
break;
|
429
|
+
}
|
430
|
+
}
|
431
|
+
}
|
432
|
+
return y;
|
433
|
+
}
|
434
|
+
/* # }}} */
|
435
|
+
|
436
|
+
/* nfc_reader_destroy # {{{ */
|
437
|
+
int nfc_reader_destroy(nfc_reader* x){
|
438
|
+
if(x!=NULL){
|
439
|
+
if(x->reddit != NULL)
|
440
|
+
free(x->reddit);
|
441
|
+
if(x->nfc_port != NULL)
|
442
|
+
free(x->nfc_port);
|
443
|
+
if((x->nfc_what_happens_if) != NULL)
|
444
|
+
free((x->nfc_what_happens_if));
|
445
|
+
if((x->tag_mem)!=NULL)
|
446
|
+
free(x->tag_mem);
|
447
|
+
free(x);
|
448
|
+
}
|
449
|
+
return 0;
|
450
|
+
}
|
451
|
+
/* # }}}*/
|
452
|
+
|
453
|
+
/* byebye # {{{ */
|
454
|
+
void byebye(char *str){
|
455
|
+
fprintf(stderr,"Error\nErrormsg: %s\n",str);
|
456
|
+
exit(EXIT_FAILURE);
|
457
|
+
}
|
458
|
+
/* # }}}*/
|
459
|
+
|
460
|
+
/* ustrlen # {{{ */
|
461
|
+
int ustrlen(unsigned char* str){
|
462
|
+
int ret;
|
463
|
+
for(ret=0;str[ret]!='\0';ret++);
|
464
|
+
return ret;
|
465
|
+
|
466
|
+
}
|
467
|
+
/* # }}}*/
|
data/ext/nfclib.h
ADDED
@@ -0,0 +1,43 @@
|
|
1
|
+
#ifndef NFCLIB_H
|
2
|
+
#define NFCLIB_H
|
3
|
+
|
4
|
+
#include <pthread.h>
|
5
|
+
#include <stdint.h>
|
6
|
+
|
7
|
+
struct nfc_struct;
|
8
|
+
|
9
|
+
typedef struct nfc_struct{
|
10
|
+
char* nfc_port; /* Port of NFC_Reader, e.g '/dev/ttyUSB0' , gets initialize with nfc_reader_init(); */
|
11
|
+
int fh;
|
12
|
+
pthread_t polling;
|
13
|
+
uint8_t read_puffer[100];
|
14
|
+
uint8_t write_puffer[100];
|
15
|
+
uint8_t* reddit;
|
16
|
+
int reddit_len;
|
17
|
+
unsigned char* tag_mem;
|
18
|
+
int tag_mem_max_len;
|
19
|
+
int tag_mem_len;
|
20
|
+
int tag_mem_block_len;
|
21
|
+
uint8_t command[100];
|
22
|
+
int command_len;
|
23
|
+
int what_happens_len;
|
24
|
+
int what_happens_max_len;
|
25
|
+
void (**nfc_what_happens_if)(struct nfc_struct* x);
|
26
|
+
} nfc_reader ;
|
27
|
+
|
28
|
+
|
29
|
+
void* nfc_reader_on_tag(nfc_reader* x,void (*y)(nfc_reader*));
|
30
|
+
|
31
|
+
nfc_reader* nfc_reader_init(const char* name,int mem_len);
|
32
|
+
int nfc_reader_stop_poll(nfc_reader*);
|
33
|
+
int nfc_reader_poll( nfc_reader*);
|
34
|
+
int nfc_reader_destroy(nfc_reader* x);
|
35
|
+
void* nfc_reader_read(void* y);
|
36
|
+
void byebye(char *str);
|
37
|
+
void* nfc_reader_do(void* y);
|
38
|
+
void set_cmd(nfc_reader *x,char * y);
|
39
|
+
int ustrlen(unsigned char* str);
|
40
|
+
int nfc_set_cmd(nfc_reader* x,char* str);
|
41
|
+
void* nfc_reader_1_read(void *y);
|
42
|
+
void nfc_reader_read_mem(nfc_reader* x);
|
43
|
+
#endif /* NFCLIB_H */
|
data/lib/nfc.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'ffi'
|
2
|
+
|
3
|
+
module NFC
|
4
|
+
# FFI #{{{
|
5
|
+
extend FFI::Library
|
6
|
+
ffi_lib ::File.dirname(__FILE__) + '/nfclib.so'
|
7
|
+
callback :on_tag_callback, [:pointer], :void
|
8
|
+
attach_function :nfc_reader_on_tag, [:pointer,:on_tag_callback], :pointer
|
9
|
+
attach_function :nfc_reader_init, [:string,:int], :pointer
|
10
|
+
attach_function :nfc_reader_poll, [:pointer], :int
|
11
|
+
attach_function :nfc_reader_stop_poll, [:pointer], :int
|
12
|
+
attach_function :nfc_reader_destroy, [:pointer], :int
|
13
|
+
attach_function :nfc_reader_read, [:pointer], :void
|
14
|
+
attach_function :nfc_reader_do, [:pointer], :void
|
15
|
+
attach_function :byebye, [:string], :void
|
16
|
+
attach_function :ustrlen, [:string], :void
|
17
|
+
attach_function :nfc_reader_1_read, [:pointer], :pointer
|
18
|
+
attach_function :nfc_set_cmd, [:pointer, :string], :int;
|
19
|
+
#}}}
|
20
|
+
|
21
|
+
class Struct < FFI::Struct #{{{
|
22
|
+
layout :nfc_port, :string,
|
23
|
+
:fh, :int,
|
24
|
+
:polling, :pthread_t,
|
25
|
+
:read_puffer, [:u_int8_t, 100],
|
26
|
+
:write_puffer, [:u_int8_t,100],
|
27
|
+
:reddit, :pointer,
|
28
|
+
:reddit_len, :int,
|
29
|
+
:tag_mem, [:pointer,36],
|
30
|
+
:tag_mem_max_len, :int,
|
31
|
+
:tag_mem_len, :int,
|
32
|
+
:tag_mem_block_len, :int,
|
33
|
+
:command, [:u_int8_t,100],
|
34
|
+
:command_len, :int,
|
35
|
+
:what_happens_len, :int,
|
36
|
+
:what_happens_max_len, :int,
|
37
|
+
:nfc_what_happens_if, :pointer
|
38
|
+
end #}}}
|
39
|
+
|
40
|
+
class Result #{{{
|
41
|
+
def initialize(res)
|
42
|
+
@res = res.unpack("C*")
|
43
|
+
end
|
44
|
+
|
45
|
+
def hex
|
46
|
+
@res.map{|e| e.to_s(16) }
|
47
|
+
end
|
48
|
+
def dec
|
49
|
+
@res
|
50
|
+
end
|
51
|
+
end #}}}
|
52
|
+
|
53
|
+
class Reader
|
54
|
+
attr_reader :reader
|
55
|
+
attr_accessor :serialnr
|
56
|
+
def initialize(tty = "/dev/ttyUSB0",mem_len=36) #{{{
|
57
|
+
@reader = NFC.nfc_reader_init(tty,mem_len)
|
58
|
+
@thread = nil
|
59
|
+
@serialnr = nil
|
60
|
+
command('x')
|
61
|
+
|
62
|
+
begin
|
63
|
+
@serialnr = command('b')
|
64
|
+
end while @serialnr == nil
|
65
|
+
p "SERIAL"
|
66
|
+
p @serialnr
|
67
|
+
p '-' * 24
|
68
|
+
Signal::trap("INT") do
|
69
|
+
puts 'stopping...'
|
70
|
+
stop_poll
|
71
|
+
@thread.kill
|
72
|
+
end
|
73
|
+
end #}}}
|
74
|
+
|
75
|
+
def command( cmd) #{{{
|
76
|
+
obj = Struct.new(@reader)
|
77
|
+
NFC.nfc_set_cmd(@reader,cmd)
|
78
|
+
NFC.nfc_reader_do(@reader)
|
79
|
+
obj[:reddit].read_string(obj[:reddit_len])[3..-3]
|
80
|
+
end #}}}
|
81
|
+
|
82
|
+
def command_print(cmd) #{{{
|
83
|
+
obj = Struct.new(@reader)
|
84
|
+
NFC.nfc_set_cmd(@reader,cmd)
|
85
|
+
NFC.nfc_reader_do(@reader)
|
86
|
+
if (obj[:reddit].read_string(obj[:reddit_len])[3..-3]== nil)
|
87
|
+
temp = nil
|
88
|
+
else
|
89
|
+
temp=Result.new(obj[:reddit].read_string(obj[:reddit_len])[3..-3])
|
90
|
+
end
|
91
|
+
p temp
|
92
|
+
end #}}}
|
93
|
+
|
94
|
+
def read_once #{{{
|
95
|
+
NFC.nfc_reader_1_read(@reader)
|
96
|
+
end #}}}
|
97
|
+
def stop_poll #{{{
|
98
|
+
NFC.nfc_reader_stop_poll(@reader)
|
99
|
+
end #}}}
|
100
|
+
def poll(&bl) #{{{
|
101
|
+
pr = Proc.new do |obj|
|
102
|
+
obj = Struct.new(@reader)
|
103
|
+
bl.call(Result.new(obj[:reddit].read_string(obj[:reddit_len])[3..-3]))
|
104
|
+
end
|
105
|
+
NFC.nfc_reader_on_tag(@reader,pr)
|
106
|
+
command('dp')
|
107
|
+
NFC.nfc_reader_poll(@reader)
|
108
|
+
@thread = Thread.new { Thread::stop }
|
109
|
+
@thread.join
|
110
|
+
end #}}}
|
111
|
+
end
|
112
|
+
end
|
data/nfclib.gemspec
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'nfclib'
|
3
|
+
s.version = '0.0.128'
|
4
|
+
s.date = '2014-08-05'
|
5
|
+
s.summary ="NFC-Reader Library."
|
6
|
+
s.description ="Executes command and gets results in c, everthing else ruby-yo"
|
7
|
+
s.authors = ["Florian Stertz"]
|
8
|
+
s.email = ['florian.stertz@gmail.com']
|
9
|
+
s.files = Dir['ext/extconf.rb'] + Dir['lib/nfc.rb'] + ['nfclib.gemspec','README.md'] + Dir['ext/nfclib.c'] + Dir['ext/nfclib.h']
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.require_paths = ['lib']
|
12
|
+
s.extensions = Dir['ext/extconf.rb']
|
13
|
+
s.homepage = 'https://github.com/Triarier/lagiacrus'
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nfclib
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.128
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Florian Stertz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Executes command and gets results in c, everthing else ruby-yo
|
14
|
+
email:
|
15
|
+
- florian.stertz@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions:
|
18
|
+
- ext/extconf.rb
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- README.md
|
22
|
+
- ext/extconf.rb
|
23
|
+
- ext/nfclib.c
|
24
|
+
- ext/nfclib.h
|
25
|
+
- lib/nfc.rb
|
26
|
+
- nfclib.gemspec
|
27
|
+
homepage: https://github.com/Triarier/lagiacrus
|
28
|
+
licenses: []
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.2.2
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: NFC-Reader Library.
|
50
|
+
test_files: []
|