rfid_decoder.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include "../common/common_defs.h"
  5. typedef struct rfid_decode_pattern_struct
  6. {
  7. unsigned int bits;
  8. unsigned int shift;
  9. unsigned long long mask;
  10. unsigned long long match;
  11. unsigned int site_shift;
  12. unsigned long long site_mask;
  13. unsigned int id_shift;
  14. unsigned long long id_mask;
  15. } rfid_decode_pattern;
  16. static rfid_decode_pattern rfid_pattern_buffer[NUM_RFID_PATTERNS] = {{0}};
  17. static int rfid_pattern_used = -1;
  18. int rfid_pattern_loaded()
  19. {
  20. return rfid_pattern_used;
  21. }
  22. int load_rfid_decode_patterns(char *filename)
  23. {
  24. char line[LINE_BUFFER_SIZE];
  25. int i, retval;
  26. FILE *f;
  27. char *c;
  28. if(!filename)
  29. {
  30. fprintf(stderr, "Cannot open RFID Pattern Match file %s", "(null)");
  31. rfid_pattern_used = -1;
  32. return -1;
  33. }
  34. i = 0;
  35. f = fopen(filename, "rb");
  36. if(!f)
  37. {
  38. fprintf(stderr, "Cannot open RFID Pattern Match file %s", filename);
  39. rfid_pattern_used = -1;
  40. return -1;
  41. }
  42. while(i < NUM_RFID_PATTERNS)
  43. {
  44. fgets(line, LINE_BUFFER_SIZE, f);
  45. if(feof(f))
  46. {
  47. break;
  48. }
  49. c = line;
  50. //skip any blank lines, or comment lines starting with a # as their first non-whitespace character
  51. while( (*c == ' ') || (*c == '\t') || (*c == '\r') || (*c == '\n') ) c++;
  52. if( (*c == '#') || (*c == '\0') ) continue;
  53. retval = sscanf(c, "%x %x %llx %llx %x %llx %x %llx", &rfid_pattern_buffer[i].bits, &rfid_pattern_buffer[i].shift,
  54. &rfid_pattern_buffer[i].mask, &rfid_pattern_buffer[i].match,
  55. &rfid_pattern_buffer[i].site_shift, &rfid_pattern_buffer[i].site_mask,
  56. &rfid_pattern_buffer[i].id_shift, &rfid_pattern_buffer[i].id_mask
  57. );
  58. if(retval == 8)
  59. {
  60. i++;
  61. }
  62. else
  63. {
  64. memset(&rfid_pattern_buffer[i], 0, sizeof(rfid_decode_pattern));
  65. fprintf(stderr, "Cannot parse RFID Pattern Match line \"%s\"\n", line);
  66. }
  67. }
  68. fclose(f);
  69. if(i >= NUM_RFID_PATTERNS)
  70. {
  71. fprintf(stderr, "RFID Pattern Match file %s contains more than %d records!", filename, NUM_RFID_PATTERNS);
  72. rfid_pattern_used = -1;
  73. return -1;
  74. }
  75. memset(&rfid_pattern_buffer[i], 0, sizeof(rfid_decode_pattern));
  76. rfid_pattern_used = i;
  77. return i;
  78. }
  79. int decode_rfid_string(char *raw_rfid, unsigned long long *site, unsigned long long *id, unsigned int *bits)
  80. {
  81. unsigned long long raw;
  82. unsigned int nbits;
  83. char *rest;
  84. int i;
  85. if(!raw_rfid)
  86. {
  87. fprintf(stderr, "NULL RFID String!\n");
  88. return -1;
  89. }
  90. if(rfid_pattern_used < 1)
  91. {
  92. fprintf(stderr, "rfid_pattern_used < 1!\n");
  93. }
  94. nbits = strtol(raw_rfid, &rest, 16);
  95. if(*rest++ != '|')
  96. {
  97. fprintf(stderr, "Malformed RFID String!\n");
  98. return -1;
  99. }
  100. raw = strtoll(rest, NULL, 16);
  101. for(i = 0; i < rfid_pattern_used; i++)
  102. {
  103. //If this RFID string has the right number of bits AND matches our shift-and-mask criteria
  104. if( (nbits == rfid_pattern_buffer[i].bits) &&
  105. (((raw >> rfid_pattern_buffer[i].shift) & rfid_pattern_buffer[i].mask) == rfid_pattern_buffer[i].match)
  106. )
  107. {
  108. if(bits)
  109. {
  110. *bits = nbits;
  111. }
  112. if(site)
  113. {
  114. *site = (raw >> rfid_pattern_buffer[i].site_shift) & rfid_pattern_buffer[i].site_mask;
  115. }
  116. if(id)
  117. {
  118. *id = (raw >> rfid_pattern_buffer[i].id_shift) & rfid_pattern_buffer[i].id_mask;
  119. }
  120. return 0;
  121. }
  122. }
  123. return -1;
  124. }