Added functions for adding and listing added cards

This commit is contained in:
Arti Zirk 2016-12-18 18:27:20 +02:00
parent 111699cae1
commit 9c135dac88
6 changed files with 167 additions and 3 deletions

View File

@ -7,6 +7,7 @@
#include "print_helper.h" #include "print_helper.h"
#include "cli_microrl.h" #include "cli_microrl.h"
#include "../lib/matejx_avr_lib/mfrc522.h" #include "../lib/matejx_avr_lib/mfrc522.h"
#include "rfid.h"
typedef struct cli_cmd { typedef struct cli_cmd {
PGM_P cmd; PGM_P cmd;
@ -21,7 +22,10 @@ const cli_cmd_t cli_cmds[] = {
{ver_cmd, ver_help, cli_print_ver, 0}, {ver_cmd, ver_help, cli_print_ver, 0},
{ascii_cmd, ascii_help, cli_print_ascii_tbls, 0}, {ascii_cmd, ascii_help, cli_print_ascii_tbls, 0},
{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, 0},
{remove_cmd, remove_help, cli_rfid_remove, 0},
{list_cmd, list_help, cli_rfid_list, 0}
}; };
@ -118,7 +122,7 @@ void cli_rfid_read(const char *const *argv)
(void) argv; (void) argv;
Uid uid; Uid uid;
Uid *uid_ptr = &uid; Uid *uid_ptr = &uid;
printf_P(PSTR("\n")); putc('\n', stdout);
if (PICC_IsNewCardPresent()) { if (PICC_IsNewCardPresent()) {
printf("Card selected!\n"); printf("Card selected!\n");
PICC_ReadCardSerial(uid_ptr); PICC_ReadCardSerial(uid_ptr);
@ -135,6 +139,38 @@ void cli_rfid_read(const char *const *argv)
} }
void cli_rfid_add(const char *const *argv) {
(void) argv;
putc('\n', stdout);
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);
card.user = "A user";
rfid_add_card(&card);
} else {
printf_P(PSTR("Unable to detect card.\n"));
}
}
void cli_rfid_remove(const char *const *argv) {
(void) argv;
putc('\n', stdout);
printf("TODO: Remove card\n");
}
void cli_rfid_list(const char *const *argv) {
(void) argv;
putc('\n', stdout);
rfid_list_cards();
}
void cli_print_cmd_error(void) void cli_print_cmd_error(void)
{ {
putc('\n', stdout); putc('\n', stdout);

View File

@ -12,6 +12,9 @@ void cli_print_ver(const char *const *argv);
void cli_print_ascii_tbls(const char *const *argv); void cli_print_ascii_tbls(const char *const *argv);
void cli_handle_month(const char *const *argv); void cli_handle_month(const char *const *argv);
void cli_rfid_read(const char *const *argv); void cli_rfid_read(const char *const *argv);
void cli_rfid_add(const char *const *argv);
void cli_rfid_remove(const char *const *argv);
void cli_rfid_list(const char *const *argv);
void cli_print_cmd_error(void); void cli_print_cmd_error(void);
void cli_print_cmd_arg_error(void); void cli_print_cmd_arg_error(void);
int cli_execute(int argc, const char *const *argv); int cli_execute(int argc, const char *const *argv);

View File

@ -20,3 +20,9 @@ const char month_cmd[] PROGMEM = MONTH_CMD;
const char month_help[] PROGMEM = MONTH_HELP; const char month_help[] PROGMEM = MONTH_HELP;
const char read_cmd[] PROGMEM = READ_CMD; const char read_cmd[] PROGMEM = READ_CMD;
const char read_help[] PROGMEM = READ_HELP; const char read_help[] PROGMEM = READ_HELP;
const char add_cmd[] PROGMEM = ADD_CMD;
const char add_help[] PROGMEM = ADD_HELP;
const char remove_cmd[] PROGMEM = REMOVE_CMD;
const char remove_help[] PROGMEM = REMOVE_HELP;
const char list_cmd[] PROGMEM = LIST_CMD;
const char list_help[] PROGMEM = LIST_HELP;

View File

@ -19,7 +19,13 @@
#define CLI_NO_CMD "Command not implemented.\n Use <help> to get help." #define CLI_NO_CMD "Command not implemented.\n Use <help> to get help."
#define CLI_ARGS_MSG "To few or to many arguments for this command\nUse <help>" #define CLI_ARGS_MSG "To few or to many arguments for this command\nUse <help>"
#define READ_CMD "read" #define READ_CMD "read"
#define READ_HELP "Read and print out card info that is currently\nin proximity of the reader" #define READ_HELP "Read and print out card info that is currently in proximity of the reader"
#define ADD_CMD "add"
#define ADD_HELP "Add a new card to the system"
#define REMOVE_CMD "remove"
#define REMOVE_HELP "Remove a card from the system"
#define LIST_CMD "list"
#define LIST_HELP "List all added cards"
extern PGM_P const months[]; extern PGM_P const months[];
@ -34,5 +40,11 @@ extern const char month_cmd[];
extern const char month_help[]; extern const char month_help[];
extern const char read_cmd[]; extern const char read_cmd[];
extern const char read_help[]; extern const char read_help[];
extern const char add_cmd[];
extern const char add_help[];
extern const char remove_cmd[];
extern const char remove_help[];
extern const char list_cmd[];
extern const char list_help[];
#endif /* _HMI_MSG_H_ */ #endif /* _HMI_MSG_H_ */

92
src/rfid.c Normal file
View File

@ -0,0 +1,92 @@
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <avr/pgmspace.h>
#include "rfid.h"
card_t *head = NULL;
void rfid_print_card(const card_t *card) {
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) ||
!memcmp(current->uid, card->uid, current->uid_size) ||
!strcmp(current->user, card->user)) {
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) {
rfid_print_card(found_card);
printf("\n");
return;
}
// Card doesn't exist, add it
card_t *new_card;
char *new_card_user;
new_card = malloc(sizeof(card_t));
new_card_user = malloc(strlen(card->user)+1);
if (!new_card || !new_card_user) {
printf(PSTR("Out of memory. Please remove cards.\n"));
return;
}
// Copy card data
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;
// 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;
}
void rfid_list_cards(void) {
if (head == NULL) {
printf_P(PSTR("No cards added"));
} else {
card_t *current;
current = head;
while (current->next != NULL) {
rfid_print_card(current);
printf("\n");
current = current->next;
}
rfid_print_card(current);
printf("\n");
}
}

15
src/rfid.h Normal file
View File

@ -0,0 +1,15 @@
#ifndef _RFID_H_
#define _RFID_H_
typedef struct card {
uint8_t uid_size;
uint8_t uid[10];
char *user;
struct card *next;
} card_t;
extern card_t *head;
extern void rfid_add_card(const card_t *card);
extern void rfid_list_cards(void);
#endif /* _RFID_H_ */