2016-12-18 22:43:53 +02:00
|
|
|
/* Copyright (C) 2016 Arti Zirk <arti.zirk@gmail.com>
|
|
|
|
*
|
|
|
|
* This file is part of I237 Door Access program.
|
|
|
|
*
|
|
|
|
* I237 Door Access is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* I237 Door Access is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with I237 Door Access. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-12-18 18:27:20 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <avr/pgmspace.h>
|
|
|
|
#include "rfid.h"
|
2016-12-18 22:21:34 +02:00
|
|
|
#include "hmi_msg.h"
|
2016-12-18 18:27:20 +02:00
|
|
|
|
|
|
|
card_t *head = NULL;
|
|
|
|
|
2016-12-18 22:43:53 +02:00
|
|
|
void rfid_print_card(const card_t *card)
|
|
|
|
{
|
2016-12-18 18:27:20 +02:00
|
|
|
for (uint8_t i = 0; i < card->uid_size; i++) {
|
|
|
|
printf("%02X", card->uid[i]);
|
|
|
|
}
|
|
|
|
printf(": %s", card->user);
|
|
|
|
}
|
|
|
|
|
|
|
|
card_t* rfid_find_card(const card_t *card)
|
|
|
|
{
|
|
|
|
if (head != NULL) {
|
|
|
|
card_t *current;
|
|
|
|
current = head;
|
|
|
|
while (current != NULL) {
|
|
|
|
if ((current->uid_size != card->uid_size) ||
|
2016-12-18 22:43:53 +02:00
|
|
|
!memcmp(current->uid, card->uid, current->uid_size) ||
|
|
|
|
((card->user != NULL) && !strcmp(current->user, card->user))) {
|
|
|
|
|
2016-12-18 18:27:20 +02:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rfid_add_card(const card_t *card)
|
|
|
|
{
|
|
|
|
card_t *found_card = rfid_find_card(card);
|
|
|
|
if (found_card) {
|
2016-12-18 22:19:53 +02:00
|
|
|
printf_P(PSTR(NOT_ADDING_CARD_MSG1));
|
2016-12-18 18:27:20 +02:00
|
|
|
rfid_print_card(found_card);
|
2016-12-18 22:19:53 +02:00
|
|
|
printf_P(PSTR(NOT_ADDING_CARD_MSG2 "\n"));
|
2016-12-18 18:27:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-12-18 22:43:53 +02:00
|
|
|
|
2016-12-18 18:27:20 +02:00
|
|
|
// Card doesn't exist, add it
|
|
|
|
card_t *new_card;
|
|
|
|
char *new_card_user;
|
|
|
|
new_card = malloc(sizeof(card_t));
|
2016-12-18 22:43:53 +02:00
|
|
|
new_card_user = malloc(strlen(card->user) + 1);
|
2016-12-18 18:27:20 +02:00
|
|
|
if (!new_card || !new_card_user) {
|
2016-12-18 22:19:53 +02:00
|
|
|
printf_P(PSTR(OUT_OF_MEMORY_MSG "\n"));
|
2016-12-18 18:27:20 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-12-18 22:43:53 +02:00
|
|
|
|
|
|
|
// Copy card data
|
2016-12-18 18:27:20 +02:00
|
|
|
new_card->uid_size = card->uid_size;
|
|
|
|
memcpy(new_card->uid, card->uid, card->uid_size);
|
|
|
|
strcpy(new_card_user, card->user);
|
|
|
|
new_card->user = new_card_user;
|
2016-12-18 19:07:10 +02:00
|
|
|
new_card->next = NULL;
|
2016-12-18 18:27:20 +02:00
|
|
|
|
|
|
|
// Update card list
|
|
|
|
if (head == NULL) {
|
|
|
|
head = new_card;
|
|
|
|
} else {
|
|
|
|
card_t *current;
|
|
|
|
current = head;
|
|
|
|
while (current->next != NULL) {
|
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
current->next = new_card;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-18 22:43:53 +02:00
|
|
|
void rfid_list_cards(void)
|
|
|
|
{
|
2016-12-18 18:27:20 +02:00
|
|
|
if (head == NULL) {
|
2016-12-18 22:19:53 +02:00
|
|
|
printf_P(PSTR(NO_CARDS_ADDED_MSG "\n"));
|
2016-12-18 18:27:20 +02:00
|
|
|
} else {
|
|
|
|
card_t *current;
|
|
|
|
current = head;
|
|
|
|
while (current->next != NULL) {
|
|
|
|
rfid_print_card(current);
|
2016-12-18 22:19:53 +02:00
|
|
|
putc('\n', stdout);
|
2016-12-18 18:27:20 +02:00
|
|
|
current = current->next;
|
|
|
|
}
|
|
|
|
rfid_print_card(current);
|
2016-12-18 22:19:53 +02:00
|
|
|
putc('\n', stdout);
|
2016-12-18 18:27:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-18 22:43:53 +02:00
|
|
|
void rfid_remove_card_by_user(const char *user)
|
|
|
|
{
|
2016-12-18 20:31:30 +02:00
|
|
|
card_t *curr;
|
|
|
|
card_t *prev;
|
|
|
|
curr = head;
|
|
|
|
prev = NULL;
|
|
|
|
if (head == NULL) {
|
2016-12-18 22:19:53 +02:00
|
|
|
printf_P(PSTR(NO_CARDS_ADDED_MSG "\n"));
|
2016-12-18 20:31:30 +02:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
while (curr->next != NULL) {
|
|
|
|
if (strcmp(curr->user, user) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = curr;
|
|
|
|
curr = curr->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (strcmp(curr->user, user) == 0) {
|
|
|
|
if (prev == NULL && curr->next == NULL) {
|
|
|
|
// this is the single card in the list
|
|
|
|
free(curr->user);
|
|
|
|
free(curr);
|
|
|
|
head = NULL;
|
|
|
|
} else if (prev == NULL && curr->next != NULL) {
|
|
|
|
// this is the first card in the list, with more after it
|
|
|
|
head = curr->next;
|
|
|
|
free(curr->user);
|
|
|
|
free(curr);
|
|
|
|
} else if (prev != NULL && curr->next != NULL) {
|
|
|
|
// this card is somewhere in the middle of the list
|
|
|
|
prev->next = curr->next;
|
|
|
|
free(curr->user);
|
|
|
|
free(curr);
|
2016-12-18 22:43:53 +02:00
|
|
|
} else if (prev != NULL && curr->next == NULL) {
|
2016-12-18 20:31:30 +02:00
|
|
|
// this is the last card in the list
|
|
|
|
prev->next = NULL;
|
|
|
|
free(curr->user);
|
|
|
|
free(curr);
|
|
|
|
} else {
|
2016-12-18 22:19:53 +02:00
|
|
|
printf_P(PSTR(LINKED_LIST_ERROR_MSG "\n"));
|
2016-12-18 20:31:30 +02:00
|
|
|
}
|
|
|
|
} else {
|
2016-12-18 22:19:53 +02:00
|
|
|
printf_P(PSTR(CARD_NOT_FOUND_MSG));
|
2016-12-18 20:31:30 +02:00
|
|
|
}
|
|
|
|
}
|