Removing a card works now

This commit is contained in:
Arti Zirk 2016-12-18 20:31:30 +02:00
parent 08870f2b9b
commit 41d61f9752
3 changed files with 51 additions and 6 deletions

View File

@ -25,7 +25,7 @@ const cli_cmd_t cli_cmds[] = {
{month_cmd, month_help, cli_handle_month, 1}, {month_cmd, month_help, cli_handle_month, 1},
{read_cmd, read_help, cli_rfid_read, 0}, {read_cmd, read_help, cli_rfid_read, 0},
{add_cmd, add_help, cli_rfid_add, 1}, {add_cmd, add_help, cli_rfid_add, 1},
{remove_cmd, remove_help, cli_rfid_remove, 0}, {remove_cmd, remove_help, cli_rfid_remove, 1},
{list_cmd, list_help, cli_rfid_list, 0} {list_cmd, list_help, cli_rfid_list, 0}
}; };
@ -146,7 +146,6 @@ void cli_rfid_add(const char *const *argv) {
Uid uid; Uid uid;
card_t card; card_t card;
if (PICC_IsNewCardPresent()) { if (PICC_IsNewCardPresent()) {
printf_P(PSTR("Adding a card\n"));
PICC_ReadCardSerial(&uid); PICC_ReadCardSerial(&uid);
card.uid_size = uid.size; card.uid_size = uid.size;
memcpy(&card.uid, &uid.uidByte, uid.size); memcpy(&card.uid, &uid.uidByte, uid.size);
@ -164,7 +163,7 @@ void cli_rfid_add(const char *const *argv) {
void cli_rfid_remove(const char *const *argv) { void cli_rfid_remove(const char *const *argv) {
(void) argv; (void) argv;
putc('\n', stdout); putc('\n', stdout);
printf("TODO: Remove card\n"); rfid_remove_card_by_user(argv[1]);
} }
void cli_rfid_list(const char *const *argv) { void cli_rfid_list(const char *const *argv) {

View File

@ -36,8 +36,9 @@ void rfid_add_card(const card_t *card)
{ {
card_t *found_card = rfid_find_card(card); card_t *found_card = rfid_find_card(card);
if (found_card) { if (found_card) {
printf("Found card \"");
rfid_print_card(found_card); rfid_print_card(found_card);
printf("\n"); printf("\", not adding it again.\n");
return; return;
} }
@ -88,5 +89,49 @@ void rfid_list_cards(void) {
} }
} }
void rfid_remove_card_by_user(const char *user) {
card_t *curr;
card_t *prev;
curr = head;
prev = NULL;
if (head == NULL) {
printf_P(PSTR("No cards added\n"));
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);
} else if(prev != NULL && curr->next == NULL) {
// this is the last card in the list
prev->next = NULL;
free(curr->user);
free(curr);
} else {
fprintf_P(stderr, PSTR("Invalid situation when removing card\n"));
}
} else {
printf_P(PSTR("Card not found\n"));
}
}

View File

@ -11,5 +11,6 @@ typedef struct card {
extern card_t *head; extern card_t *head;
extern void rfid_add_card(const card_t *card); extern void rfid_add_card(const card_t *card);
extern void rfid_list_cards(void); extern void rfid_list_cards(void);
extern void rfid_remove_card_by_user(const char *user);
#endif /* _RFID_H_ */ #endif /* _RFID_H_ */