Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug #38

Merged
merged 7 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add debug command
  • Loading branch information
n1tram1 committed Apr 27, 2021
commit ed5ade69c3e04da11e78ace33ccc624b9ad9a218
63 changes: 56 additions & 7 deletions module/mfrc522_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <linux/regmap.h>
#include <linux/fs.h>

#include "mfrc522_module.h"
#include "mfrc522_user_command.h"
#include "mfrc522_parser.h"
#include "mfrc522_spi.h"
Expand All @@ -19,18 +20,62 @@

#define MFRC522_MAX_ANSWER_SIZE 256 // FIXME

struct mfrc522_state {
struct miscdevice misc;
bool buffer_full;
char answer[MFRC522_MAX_ANSWER_SIZE];
};

static struct mfrc522_state *g_state;

MODULE_LICENSE("GPL v2");
MODULE_AUTHOR("ks0n");
MODULE_DESCRIPTION("Driver for the MFRC522 RFID Chip");

static void do_debug_read(const char *answer, int answer_size)
{

pr_info("RD\n");

for (int i = 0; i < answer_size; i++) {
pr_info("%02x", answer[i]);

if (i % 5 == 0)
pr_info("\n");
else
pr_info(" ");

}

if (answer_size % 5 != 0)
pr_info("\n");
}

static void do_debug_write(const char *cmd)
{
int i;

for (i = 0; cmd[i]; i++) {
pr_info("%02x", answer[i]);

if (i % 5 == 0)
pr_info("\n");
else
pr_info(" ");

}

if (i % 5 != 0)
pr_info("\n");
}

static void do_debug(const struct mfrc522_command *cmd, const char *answer,
int answer_size)
{
switch(cmd->cmd) {
case MFRC522_CMD_MEM_READ:
do_debug_read(answer, answer_size);
case MFRC522_CMD_MEM_WRITE:
do_debug_write(cmd->data);
default:
}

}

static ssize_t __mfrc522_write(struct mfrc522_state *state, const char *buffer,
size_t len)
{
Expand All @@ -48,14 +93,17 @@ static ssize_t __mfrc522_write(struct mfrc522_state *state, const char *buffer,
if (command.data[0])
pr_info("[MFRC522] With extra data: `%s`\n", command.data);

answer_size = mfrc522_execute(state->answer, &command);
answer_size = mfrc522_execute(state, state->answer, &command);
n1tram1 marked this conversation as resolved.
Show resolved Hide resolved

if (answer_size < 0) {
// Error
pr_err("[MFRC522] Error when executing command\n");
return -EBADE;
}

if (state->debug_on)
do_debug(&command, answer, answer_size);

// Non-empty answer
pr_info("[MFRC522] Answer: \"%.*s\"\n", answer_size, state->answer);
state->buffer_full = true;
Expand Down Expand Up @@ -196,6 +244,7 @@ static int __init mfrc522_init(void)
.minor = MISC_DYNAMIC_MINOR,
.name = "mfrc522_misc",
.fops = &mfrc522_fops,
.debug_on = false,
};

ret = misc_register(&state->misc);
Expand Down
11 changes: 11 additions & 0 deletions module/mfrc522_module.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef MFRC522_MODULE_H
#define MFRC522_MODULE_H

struct mfrc522_state {
struct miscdevice misc;
bool buffer_full;
char answer[MFRC522_MAX_ANSWER_SIZE];
bool debug_on;
};

#endif /* ! MFRC522_MODULE_H */
3 changes: 3 additions & 0 deletions module/mfrc522_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ static const struct driver_command commands[MFRC522_CMD_AMOUNT] = {
{ .input = "version",
.parameter_amount = 0,
.cmd = MFRC522_CMD_GET_VERSION },
{ .input = "debug",
.parameter_amount = 1,
.cmd = MFRC522_CMD_DEBUG },
};

/**
Expand Down
16 changes: 15 additions & 1 deletion module/mfrc522_user_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,18 @@ static int generate_random(char *answer)
return 0;
}

int mfrc522_execute(char *answer, struct mfrc522_command *cmd)
static int set_debug(struct mfrc522_state *state, const struct mfrc522_command *cmd) {
if (!strncmp(cmd->data, "on", 3))
state->debug_on = true;
else if (!strncmp(cmd->data, "off", 4))
state->debug_on = false;
else
return -1;

return 0;
}

int mfrc522_execute(struct mfrc522_state *state, char *answer, struct mfrc522_command *cmd)
{
int ret = -1;

Expand All @@ -144,6 +155,9 @@ int mfrc522_execute(char *answer, struct mfrc522_command *cmd)
case MFRC522_CMD_GEN_RANDOM:
ret = generate_random(answer);
break;
case MFRC522_CMD_DEBUG:
ret = set_debug(state, cmd->data);
break;
default:
ret = sprintf(answer, "%s", "Command unimplemented");
}
Expand Down
3 changes: 2 additions & 1 deletion module/mfrc522_user_command.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum mfrc522_commands {
MFRC522_CMD_MEM_READ,
MFRC522_CMD_GET_VERSION,
MFRC522_CMD_GEN_RANDOM,
MFRC522_CMD_DEBUG,
};

struct mfrc522_command {
Expand Down Expand Up @@ -52,6 +53,6 @@ int mfrc522_command_simple_init(struct mfrc522_command *cmd, u8 cmd_byte);
*
* @return The size of the answer on success, -1 on error
*/
int mfrc522_execute(char *answer, struct mfrc522_command *cmd);
int mfrc522_execute(struct mfrc522_state *state, char *answer, struct mfrc522_command *cmd);

#endif /* ! MFRC522_COMMAND_H */