From 41d61f9752d6c9a10cad14548fe9001073ee4be7 Mon Sep 17 00:00:00 2001 From: Arti Zirk Date: Sun, 18 Dec 2016 20:31:30 +0200 Subject: [PATCH] Removing a card works now --- src/cli_microrl.c | 5 ++--- src/rfid.c | 51 ++++++++++++++++++++++++++++++++++++++++++++--- src/rfid.h | 1 + 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/cli_microrl.c b/src/cli_microrl.c index 50e9518..dfc2656 100644 --- a/src/cli_microrl.c +++ b/src/cli_microrl.c @@ -25,7 +25,7 @@ const cli_cmd_t cli_cmds[] = { {month_cmd, month_help, cli_handle_month, 1}, {read_cmd, read_help, cli_rfid_read, 0}, {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} }; @@ -146,7 +146,6 @@ void cli_rfid_add(const char *const *argv) { Uid uid; card_t card; if (PICC_IsNewCardPresent()) { - printf_P(PSTR("Adding a card\n")); PICC_ReadCardSerial(&uid); card.uid_size = 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) argv; putc('\n', stdout); - printf("TODO: Remove card\n"); + rfid_remove_card_by_user(argv[1]); } void cli_rfid_list(const char *const *argv) { diff --git a/src/rfid.c b/src/rfid.c index 906845d..1edb9eb 100644 --- a/src/rfid.c +++ b/src/rfid.c @@ -36,8 +36,9 @@ void rfid_add_card(const card_t *card) { card_t *found_card = rfid_find_card(card); if (found_card) { + printf("Found card \""); rfid_print_card(found_card); - printf("\n"); + printf("\", not adding it again.\n"); 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")); + } +} diff --git a/src/rfid.h b/src/rfid.h index ef18fcd..29f54ef 100644 --- a/src/rfid.h +++ b/src/rfid.h @@ -11,5 +11,6 @@ typedef struct card { extern card_t *head; extern void rfid_add_card(const card_t *card); extern void rfid_list_cards(void); +extern void rfid_remove_card_by_user(const char *user); #endif /* _RFID_H_ */